-
Notifications
You must be signed in to change notification settings - Fork 19
/
unspent.go
211 lines (178 loc) · 6.47 KB
/
unspent.go
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
209
210
211
package btc
import (
"encoding/json"
"sort"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/hiromaily/go-bitcoin/pkg/enum"
"github.com/pkg/errors"
)
// ListUnspentResult listunspentの戻り値
type ListUnspentResult struct {
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
Address string `json:"address"`
Label string `json:"label"` //to account
ScriptPubKey string `json:"scriptPubKey"`
Amount float64 `json:"amount"`
Confirmations int64 `json:"confirmations"`
RedeemScript string `json:"redeemScript"`
Spendable bool `json:"spendable"`
Solvable bool `json:"solvable"` //new
Safe bool `json:"safe"` //new
}
// UnlockAllUnspentTransaction Lockされたトランザクションの解除
func (b *Bitcoin) UnlockAllUnspentTransaction() error {
list, err := b.client.ListLockUnspent() //[]*wire.OutPoint
if err != nil {
return errors.Errorf("client.ListLockUnspent(): error: %s", err)
}
if len(list) != 0 {
err = b.client.LockUnspent(true, list)
if err != nil {
//FIXME: -8: Invalid parameter, expected unspent output たまにこのエラーが出る。。。Bitcoin Coreの再起動が必要
// Bitcoin Coreから先のP2Pネットワークへの接続が失敗しているときに起きる
// よって、Bitcoin Coreの再起動が必要
// loggingコマンド, もしくは ~/Library/Application Support/Bitcoin/testnet3/debug.logのチェック??
return errors.Errorf("client.LockUnspent(): error: %s", err)
}
}
return nil
}
// LockUnspent 渡されたtxIDをロックする
func (b *Bitcoin) LockUnspent(tx btcjson.ListUnspentResult) error {
txIDHash, err := chainhash.NewHashFromStr(tx.TxID)
if err != nil {
return errors.Errorf("chainhash.NewHashFromStr(): error: %s", err)
}
outpoint := wire.NewOutPoint(txIDHash, tx.Vout)
err = b.client.LockUnspent(false, []*wire.OutPoint{outpoint})
if err != nil {
return err
}
return nil
}
func (b *Bitcoin) convertListUnspent(listUnspent []btcjson.ListUnspentResult) []ListUnspentResult {
//[]btcjson.ListUnspentResult
converted := make([]ListUnspentResult, len(listUnspent))
for idx, val := range listUnspent {
converted[idx].TxID = val.TxID
converted[idx].Vout = val.Vout
converted[idx].Address = val.Address
converted[idx].Label = val.Account
converted[idx].ScriptPubKey = val.ScriptPubKey
converted[idx].Amount = val.Amount
converted[idx].Confirmations = val.Confirmations
converted[idx].Spendable = val.Spendable
}
return converted
}
// ListUnspent listunspentを呼び出す
func (b *Bitcoin) ListUnspent() ([]ListUnspentResult, error) {
if b.Version() >= enum.BTCVer17 {
return b.listUnspentVer17()
}
return b.listUnspentVer16()
}
func (b *Bitcoin) listUnspentVer16() ([]ListUnspentResult, error) {
listUnspentResult, err := b.client.ListUnspentMin(b.ConfirmationBlock())
if err != nil {
return nil, errors.Errorf("client.ListUnspentMin(): error: %s", err)
}
if len(listUnspentResult) == 0 {
return nil, nil
}
return b.convertListUnspent(listUnspentResult), nil
}
func (b *Bitcoin) listUnspentVer17() ([]ListUnspentResult, error) {
input, err := json.Marshal(uint64(b.confirmationBlock))
if err != nil {
return nil, errors.Errorf("json.Marchal(): error: %s", err)
}
rawResult, err := b.client.RawRequest("listunspent", []json.RawMessage{input})
if err != nil {
return nil, errors.Errorf("json.RawRequest(listunspent): error: %s", err)
}
var listunspentResult []ListUnspentResult
err = json.Unmarshal([]byte(rawResult), &listunspentResult)
if err != nil {
return nil, errors.Errorf("json.Unmarshal(): error: %s", err)
}
if len(listunspentResult) == 0 {
return nil, nil
}
return listunspentResult, nil
}
// ListUnspentByAccount 指定したアカウントのlistunspentを取得する
func (b *Bitcoin) ListUnspentByAccount(accountType enum.AccountType) ([]ListUnspentResult, []btcutil.Address, error) {
addrs, err := b.GetAddressesByAccount(string(accountType))
if err != nil {
return nil, nil, errors.Errorf("BTC.GetAddressesByAccount(): error: %s", err)
}
if len(addrs) == 0 {
return nil, nil, errors.Errorf("%s addresses could not be found", accountType)
}
var unspentList []ListUnspentResult
if b.Version() >= enum.BTCVer17 {
unspentList, err = b.listUnspentByAccountVer17(addrs)
if err != nil {
return nil, nil, errors.Errorf("BTC.listUnspentByAccountVer17() error: %s", err)
}
} else {
unspentList, err = b.listUnspentByAccountVer16(addrs)
if err != nil {
return nil, nil, errors.Errorf("BTC.listUnspentByAccountVer16() error: %s", err)
}
}
//送金の金額と近しいutxoでtxを作成するため、ソートしておく => 小さなutxoから利用していくのに便利だが、MUSTではない
sort.Slice(unspentList, func(i, j int) bool {
//small to big
return unspentList[i].Amount < unspentList[j].Amount
})
return unspentList, addrs, nil
}
func (b *Bitcoin) listUnspentByAccountVer16(addrs []btcutil.Address) ([]ListUnspentResult, error) {
listUnspentResult, err := b.client.ListUnspentMinMaxAddresses(b.ConfirmationBlock(), 9999999, addrs)
if err != nil {
//ListUnspentが実行できない。致命的なエラー。この場合BitcoinCoreの再起動が必要
return nil, err
}
if len(listUnspentResult) == 0 {
return nil, nil
}
return b.convertListUnspent(listUnspentResult), nil
}
func (b *Bitcoin) listUnspentByAccountVer17(addrs []btcutil.Address) ([]ListUnspentResult, error) {
input1, err := json.Marshal(uint64(b.confirmationBlock))
if err != nil {
return nil, errors.Errorf("json.Marchal(): error: %s", err)
}
input2, err := json.Marshal(uint64(9999999))
if err != nil {
return nil, errors.Errorf("json.Marchal(): error: %s", err)
}
//address
strAddrs := make([]string, len(addrs))
for idx, addr := range addrs {
strAddrs[idx] = addr.String()
}
input3, err := json.Marshal(strAddrs)
if err != nil {
return nil, errors.Errorf("json.Marchal(): error: %s", err)
}
rawResult, err := b.client.RawRequest("listunspent", []json.RawMessage{input1, input2, input3})
if err != nil {
return nil, errors.Errorf("json.RawRequest(listunspent): error: %s", err)
}
var listunspentResult []ListUnspentResult
err = json.Unmarshal([]byte(rawResult), &listunspentResult)
if err != nil {
return nil, errors.Errorf("json.Unmarshal(): error: %s", err)
}
if len(listunspentResult) == 0 {
return nil, nil
}
return listunspentResult, nil
}