-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.go
More file actions
208 lines (184 loc) · 5.2 KB
/
utils.go
File metadata and controls
208 lines (184 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package arksdk
import (
"fmt"
"math"
"time"
arklib "github.com/arkade-os/arkd/pkg/ark-lib"
client "github.com/arkade-os/arkd/pkg/client-lib"
"github.com/arkade-os/arkd/pkg/client-lib/types"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
)
func getOffchainBalanceDetails(amountByExpiration map[int64]uint64) (int64, []client.VtxoDetails) {
nextExpiration := int64(0)
details := make([]client.VtxoDetails, 0)
for timestamp, amount := range amountByExpiration {
if nextExpiration == 0 || timestamp < nextExpiration {
nextExpiration = timestamp
}
fancyTime := time.Unix(timestamp, 0).Format(time.RFC3339)
details = append(details, client.VtxoDetails{
ExpiryTime: fancyTime,
Amount: amount,
})
}
return nextExpiration, details
}
func getFancyTimeExpiration(nextExpiration int64) string {
if nextExpiration == 0 {
return ""
}
fancyTimeExpiration := ""
t := time.Unix(nextExpiration, 0)
if t.Before(time.Now().Add(48 * time.Hour)) {
// print the duration instead of the absolute time
until := time.Until(t)
seconds := math.Abs(until.Seconds())
minutes := math.Abs(until.Minutes())
hours := math.Abs(until.Hours())
if hours < 1 {
if minutes < 1 {
fancyTimeExpiration = fmt.Sprintf("%d seconds", int(seconds))
} else {
fancyTimeExpiration = fmt.Sprintf("%d minutes", int(minutes))
}
} else {
fancyTimeExpiration = fmt.Sprintf("%d hours", int(hours))
}
} else {
fancyTimeExpiration = t.Format(time.RFC3339)
}
return fancyTimeExpiration
}
func findVtxosSpentInSettlement(vtxos []types.Vtxo, vtxo types.Vtxo) []types.Vtxo {
if vtxo.Preconfirmed {
return nil
}
return findVtxosSettled(vtxos, vtxo.CommitmentTxids[0])
}
func findVtxosSettled(vtxos []types.Vtxo, id string) []types.Vtxo {
var result []types.Vtxo
leftVtxos := make([]types.Vtxo, 0)
for _, v := range vtxos {
if v.SettledBy == id {
result = append(result, v)
} else {
leftVtxos = append(leftVtxos, v)
}
}
// Update the given list with only the left vtxos.
copy(vtxos, leftVtxos)
return result
}
func findVtxosSpent(vtxos []types.Vtxo, id string) []types.Vtxo {
var result []types.Vtxo
leftVtxos := make([]types.Vtxo, 0)
for _, v := range vtxos {
if v.ArkTxid == id {
result = append(result, v)
} else {
leftVtxos = append(leftVtxos, v)
}
}
// Update the given list with only the left vtxos.
copy(vtxos, leftVtxos)
return result
}
func reduceVtxosAmount(vtxos []types.Vtxo) uint64 {
var total uint64
for _, v := range vtxos {
total += v.Amount
}
return total
}
func findVtxosSpentInPayment(vtxos []types.Vtxo, vtxo types.Vtxo) []types.Vtxo {
return findVtxosSpent(vtxos, vtxo.Txid)
}
func findVtxosResultedFromSettledBy(vtxos []types.Vtxo, commitmentTxid string) []types.Vtxo {
var result []types.Vtxo
for _, v := range vtxos {
if v.Preconfirmed || len(v.CommitmentTxids) != 1 {
continue
}
if v.CommitmentTxids[0] == commitmentTxid {
result = append(result, v)
}
}
return result
}
func findVtxosResultedFromSpentBy(vtxos []types.Vtxo, spentByTxid string) []types.Vtxo {
var result []types.Vtxo
for _, v := range vtxos {
if v.Txid == spentByTxid {
result = append(result, v)
}
}
return result
}
func getVtxo(usedVtxos []types.Vtxo, spentByVtxos []types.Vtxo) types.Vtxo {
if len(usedVtxos) > 0 {
return usedVtxos[0]
} else if len(spentByVtxos) > 0 {
return spentByVtxos[0]
}
return types.Vtxo{}
}
func toOutputScript(onchainAddress string, network arklib.Network) ([]byte, error) {
netParams := toBitcoinNetwork(network)
rcvAddr, err := btcutil.DecodeAddress(onchainAddress, &netParams)
if err != nil {
return nil, err
}
return txscript.PayToAddrScript(rcvAddr)
}
func toOnchainAddress(arkAddress string, network arklib.Network) (string, error) {
netParams := toBitcoinNetwork(network)
decodedAddr, err := arklib.DecodeAddressV0(arkAddress)
if err != nil {
return "", err
}
witnessProgram := schnorr.SerializePubKey(decodedAddr.VtxoTapKey)
addr, err := btcutil.NewAddressTaproot(witnessProgram, &netParams)
if err != nil {
return "", err
}
return addr.String(), nil
}
func toBitcoinNetwork(net arklib.Network) chaincfg.Params {
switch net.Name {
case arklib.Bitcoin.Name:
return chaincfg.MainNetParams
case arklib.BitcoinTestNet.Name:
return chaincfg.TestNet3Params
//case arklib.BitcoinTestNet4.Name: //TODO uncomment once supported
// return chaincfg.TestNet4Params
case arklib.BitcoinSigNet.Name:
return chaincfg.SigNetParams
case arklib.BitcoinMutinyNet.Name:
return arklib.MutinyNetSigNetParams
case arklib.BitcoinRegTest.Name:
return chaincfg.RegressionNetParams
default:
return chaincfg.MainNetParams
}
}
func networkFromString(net string) arklib.Network {
switch net {
case arklib.Bitcoin.Name:
return arklib.Bitcoin
case arklib.BitcoinTestNet.Name:
return arklib.BitcoinTestNet
//case arklib.BitcoinTestNet4.Name: //TODO uncomment once supported
// return chaincfg.TestNet4Params
case arklib.BitcoinSigNet.Name:
return arklib.BitcoinSigNet
case arklib.BitcoinMutinyNet.Name:
return arklib.BitcoinMutinyNet
case arklib.BitcoinRegTest.Name:
return arklib.BitcoinRegTest
default:
return arklib.Bitcoin
}
}