-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_node.go
249 lines (212 loc) · 5.92 KB
/
btc_node.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"bitcoin-op-code/pkg/btcapi"
"bytes"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
)
type Transaction struct {
Txid string `json:"txid"`
Hash string `json:"hash"`
Version int `json:"version"`
Size int `json:"size"`
Vsize int `json:"vsize"`
Weight int `json:"weight"`
Locktime int `json:"locktime"`
Vin []Vin `json:"vin"`
Vout []Vout `json:"vout"`
Hex string `json:"hex"`
Blockhash string `json:"blockhash"`
Confirmations int `json:"confirmations"`
Time int64 `json:"time"`
Blocktime int64 `json:"blocktime"`
}
type Vin struct {
Txid string `json:"txid"`
Vout int `json:"vout"`
ScriptSig Sig `json:"scriptSig"`
Txinwitness []string `json:"txinwitness"`
Sequence uint32 `json:"sequence"`
}
type Vout struct {
Value string `json:"value"`
N int `json:"n"`
ScriptPubKey ScriptKey `json:"scriptPubKey"`
}
type Sig struct {
Asm string `json:"asm"`
Hex string `json:"hex"`
}
type ScriptKey struct {
Asm string `json:"asm"`
Desc string `json:"desc"`
Hex string `json:"hex"`
Address string `json:"address"`
Type string `json:"type"`
}
func get_node(network string) (*rpcclient.Client, error) {
var connCfg *rpcclient.ConnConfig = nil
if network == "mainnet" {
connCfg = &rpcclient.ConnConfig{
Host: getStrEnv("NODE_HOST", "127.0.0.1:8332"),
CookiePath: ".cookie",
Params: chaincfg.MainNetParams.Name,
HTTPPostMode: true,
DisableTLS: true,
}
} else if network == "testnet" {
connCfg = &rpcclient.ConnConfig{
Host: getStrEnv("TESTNET_HOST", "127.0.0.1:18332"),
User: "qwe",
Pass: "qwe",
Params: chaincfg.TestNet3Params.Name,
HTTPPostMode: true,
DisableTLS: true,
}
} else if network == "regtest" {
connCfg = &rpcclient.ConnConfig{
Host: getStrEnv("REGTEST_HOST", "127.0.0.1:18443"),
User: "qwe",
Pass: "qwe",
Params: chaincfg.RegressionNetParams.Name,
HTTPPostMode: true,
DisableTLS: true,
}
} else if network == "signet" {
return nil, errors.New("signet Not implemented")
}
client, err := rpcclient.New(connCfg, nil)
if err != nil {
defer client.Shutdown()
return nil, err
}
return client, nil
}
func getRawTransaction(network string, tx_hash string, tries ...int) (*btcjson.TxRawResult, error) {
hash, err := chainhash.NewHashFromStr(tx_hash)
if err != nil {
return nil, err
}
attempts := 5
if len(tries) > 0 {
attempts = tries[0]
}
var txResult *btcjson.TxRawResult = nil
var last_err error
for attempts > 0 {
attempts--
node, err := get_node(network)
if err != nil {
last_err = err
time.Sleep(time.Millisecond * 20)
continue
}
txResult, err = node.GetRawTransactionVerbose(hash)
if err != nil {
last_err = err
time.Sleep(time.Millisecond * 20)
continue
}
}
if txResult != nil {
return txResult, nil
}
return nil, last_err
}
func getUnspentOutput(network string, utxo string) (*btcapi.UnspentOutput, error) {
tx_hash, n, err := splitUtxoString(utxo)
if err != nil {
return nil, err
}
hash, err := chainhash.NewHashFromStr(tx_hash)
if err != nil {
return nil, err
}
tx, err := getRawTransaction(network, tx_hash)
if err != nil {
return nil, err
}
if int(n) >= len(tx.Vout) {
return nil, fmt.Errorf("wrong input number of utxo %v tx has only %d inputs", utxo, len(tx.Vout))
}
vout := tx.Vout[n]
script_bytes, err := hex.DecodeString(vout.ScriptPubKey.Hex)
if err != nil {
return nil, fmt.Errorf("cant hex decode utxos %v script", utxo)
}
sats_btc_amount, err := btcutil.NewAmount(vout.Value)
if err != nil {
return nil, err
}
sats_int64 := int64(sats_btc_amount)
return &btcapi.UnspentOutput{
Outpoint: wire.NewOutPoint(hash, n),
Output: wire.NewTxOut(sats_int64, script_bytes),
}, nil
}
type unspentOutputWorkerResult struct {
unspent *btcapi.UnspentOutput
index int
}
func getUnspentOutputWorker(network string, utxo string, index int, results chan<- unspentOutputWorkerResult) {
attempts := 3
var result *btcapi.UnspentOutput
var err error
for attempts > 0 {
attempts--
result, err = getUnspentOutput(network, utxo)
if result != nil && err == nil {
break
}
time.Sleep(time.Millisecond * 50)
}
results <- unspentOutputWorkerResult{unspent: result, index: index}
}
func getUnspentOutputList(network string, utxos []string) ([]*btcapi.UnspentOutput, error) {
results_channel := make(chan unspentOutputWorkerResult, len(utxos))
for i := 0; i < len(utxos); i++ {
go getUnspentOutputWorker(network, utxos[i], i, results_channel)
}
results := make([]*btcapi.UnspentOutput, len(utxos))
var err error
for range utxos {
worker_result := <-results_channel
results[worker_result.index] = worker_result.unspent
if results[worker_result.index] == nil {
err = fmt.Errorf("cant get utxo info %v", utxos[worker_result.index])
}
}
close(results_channel)
if err != nil {
return nil, err
}
return results, nil
}
func sendRawTransaction(network string, rawTx string) (string, error) {
node, err := get_node(network)
if err != nil {
return "", fmt.Errorf("error getting RPC node: %v", err)
}
defer node.Shutdown()
txBytes, err := hex.DecodeString(rawTx)
if err != nil {
return "", fmt.Errorf("error decoding raw transaction: %v", err)
}
msgTx := wire.NewMsgTx(wire.TxVersion)
if err := msgTx.Deserialize(bytes.NewReader(txBytes)); err != nil {
return "", fmt.Errorf("error deserializing transaction: %v", err)
}
txid, err := node.SendRawTransaction(msgTx, false)
if err != nil {
return "", fmt.Errorf("error sending transaction: %v", err)
}
return txid.String(), nil
}