This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_wrap.go
441 lines (402 loc) · 12.6 KB
/
cli_wrap.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package btccli
import (
"encoding/json"
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/dabankio/btccli/btcjson"
)
// Addmultisigaddress https://bitcoin.org/en/developer-reference#addmultisigaddress
func (cli *Cli) Addmultisigaddress(cmd btcjson.AddMultisigAddressCmd) (btcjson.CreateMultiSigResult, error) {
args := cli.AppendArgs("addmultisigaddress", strconv.Itoa(int(cmd.NRequired)), ToJSON(cmd.Keys))
if cmd.Label != nil {
args = append(args, *cmd.Label)
if cmd.AddressType != nil {
args = append(args, *cmd.AddressType)
}
}
cmdPrint := cmdAndPrint(exec.Command(CmdBitcoinCli, args...))
//TODO validate address
var resp btcjson.CreateMultiSigResult
err := json.Unmarshal([]byte(cmdPrint), &resp)
return resp, err
}
// Createmultisig https://bitcoin.org/en/developer-reference#createmultisig
func (cli *Cli) Createmultisig(nRequired uint8, keys []string, addressType *string) (btcjson.CreateMultiSigResult, error) {
args := cli.AppendArgs("createmultisig", strconv.Itoa(int(nRequired)), ToJSON(keys))
if addressType != nil {
args = append(args, *addressType)
}
cmdPrint := cmdAndPrint(exec.Command(CmdBitcoinCli, args...))
//TODO validate address
var resp btcjson.CreateMultiSigResult
err := json.Unmarshal([]byte(cmdPrint), &resp)
return resp, err
}
// Createrawtransaction https://bitcoin.org/en/developer-reference#createrawtransaction
func (cli *Cli) Createrawtransaction(cmd btcjson.CreateRawTransactionCmd) (string, error) {
args := cli.AppendArgs(
"createrawtransaction",
ToJSON(cmd.Inputs),
ToJSON(cmd.Outputs),
)
if cmd.LockTime != nil {
args = append(args, strconv.Itoa(int(*cmd.LockTime)))
}
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
//TODO validate hex
return cmdPrint, nil
}
// Decoderawtransaction https://bitcoin.org/en/developer-reference#decoderawtransaction
func (cli *Cli) Decoderawtransaction(cmd btcjson.DecodeRawTransactionCmd) (*btcjson.DecodeRawTransactionResult, error) {
args := cli.AppendArgs("decoderawtransaction", cmd.HexTx)
if cmd.Iswitness != nil {
args = append(args, strconv.FormatBool(*cmd.Iswitness))
}
cmdPrint := cmdAndPrint(exec.Command(CmdBitcoinCli, args...))
var res btcjson.DecodeRawTransactionResult
err := json.Unmarshal([]byte(cmdPrint), &res)
return &res, err
}
// Decodescript https://bitcoin.org/en/developer-reference#decodescript
func (cli *Cli) Decodescript(hex string) (btcjson.DecodeScriptResult, error) {
args := cli.AppendArgs("decodescript", hex)
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
var res btcjson.DecodeScriptResult
err := json.Unmarshal([]byte(cmdPrint), &res)
return res, err
}
// Dumpprivkey https://bitcoin.org/en/developer-reference#dumpprivkey
func (cli *Cli) Dumpprivkey(addr string) (string, error) {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, cli.AppendArgs("dumpprivkey", addr)...,
))
//TODO validate privKey
return cmdPrint, nil
}
// Generatetoaddress https://bitcoin.org/en/developer-reference#generatetoaddress
func (cli *Cli) Generatetoaddress(nBlocks uint, address string, maxtriesPtr *uint) ([]string, error) {
maxtries := 1000000
if maxtriesPtr != nil {
maxtries = int(*maxtriesPtr)
}
cmd := exec.Command(CmdBitcoinCli, cli.AppendArgs(
"generatetoaddress", strconv.Itoa(int(nBlocks)), address, strconv.Itoa(maxtries),
)...)
cmdPrint := cmdAndPrint(cmd)
var hashs []string
err := json.Unmarshal([]byte(cmdPrint), &hashs)
return hashs, err
}
// Getbestblockhash .
func (cli *Cli) Getbestblockhash() (string, error) {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, cli.AppendArgs("getbestblockhash")...,
))
//TODO validate hash
return cmdPrint, nil
}
// GetAddressInfo .
func (cli *Cli) GetAddressInfo(addr string) (*btcjson.GetAddressInfoResp, error) {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, cli.AppendArgs("getaddressinfo", addr)...,
))
var resp btcjson.GetAddressInfoResp
err := json.Unmarshal([]byte(cmdPrint), &resp)
return &resp, err
}
// Getbalance .
func (cli *Cli) Getbalance(_dummy *string, minconf *int, includeWatchonly *bool) (float64, error) {
args := cli.AppendArgs("getbalance")
if _dummy == nil {
args = append(args, "*")
} else {
args = append(args, *_dummy)
}
if minconf != nil {
args = append(args, strconv.Itoa(*minconf))
} else {
args = append(args, "0")
}
if includeWatchonly != nil {
if *includeWatchonly {
args = append(args, "true")
} else {
args = append(args, "false")
}
} else {
args = append(args, "false")
}
cmdPrint := cmdAndPrint(exec.Command(CmdBitcoinCli, args...))
if strings.Contains(cmdPrint, "error") {
return -1, fmt.Errorf("get balance response contains error string: %v", cmdPrint)
}
return strconv.ParseFloat(cmdPrint, 64)
}
// GetWalletInfo .
func (cli *Cli) GetWalletInfo() map[string]interface{} {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, cli.AppendArgs("getwalletinfo")...,
))
var info map[string]interface{}
json.Unmarshal([]byte(cmdPrint), &info)
return info
}
// Getblockcount .
func (cli *Cli) Getblockcount() (int, error) {
cmd := exec.Command(CmdBitcoinCli, cli.AppendArgs("getblockcount")...)
cmdPrint := cmdAndPrint(cmd)
cmdPrint = strings.TrimSpace(cmdPrint)
return strconv.Atoi(cmdPrint)
}
// Getblockhash .
func (cli *Cli) Getblockhash(height int) (string, error) {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli,
cli.AppendArgs("getblockhash", strconv.Itoa(height))...,
))
//TODO validate hash
return strings.TrimSpace(cmdPrint), nil
}
// Getblock https://bitcoin.org/en/developer-reference#getblock
func (cli *Cli) Getblock(hash string, verbosity int) (*string, *btcjson.GetBlockResultV1, *btcjson.GetBlockResultV2, error) {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, cli.AppendArgs(
"getblock",
hash,
strconv.Itoa(verbosity),
)...,
))
var (
hex string
b btcjson.GetBlockResultV1
b2 btcjson.GetBlockResultV2
err error
)
switch verbosity {
case 0:
hex = cmdPrint
case 1:
err = json.Unmarshal([]byte(cmdPrint), &b)
case 2:
err = json.Unmarshal([]byte(cmdPrint), &b2)
default:
err = fmt.Errorf("verbosity must one of 0/1/2, got: %d", verbosity)
}
return &hex, &b, &b2, err
}
// Getnewaddress https://bitcoin.org/en/developer-reference#getnewaddress
func (cli *Cli) Getnewaddress(labelPtr, addressTypePtr *string) (hexedAddress string, err error) {
label := ""
if labelPtr != nil {
label = *labelPtr
}
args := cli.AppendArgs("getnewaddress", label)
if addressTypePtr != nil {
args = append(args, *addressTypePtr)
}
cmdPrint := cmdAndPrint(exec.Command(CmdBitcoinCli, args...))
//TODO validate address
return cmdPrint, nil
}
// Gettransaction https://bitcoin.org/en/developer-reference#gettransaction
func (cli *Cli) Gettransaction(txid string, includeWatchonly bool) (*btcjson.GetTransactionResult, error) {
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, cli.AppendArgs("gettransaction", txid, strconv.FormatBool(includeWatchonly))...,
))
var tx btcjson.GetTransactionResult
err := json.Unmarshal([]byte(cmdPrint), &tx)
return &tx, err
}
// Getrawtransaction .
func (cli *Cli) Getrawtransaction(cmd btcjson.GetRawTransactionCmd) (*btcjson.RawTx, error) {
args := cli.AppendArgs(
"getrawtransaction",
cmd.Txid,
strconv.FormatBool(true),
)
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
var tx btcjson.RawTx
err := json.Unmarshal([]byte(cmdPrint), &tx)
return &tx, err
}
// Getreceivedbyaddress https://bitcoin.org/en/developer-reference#getreceivedbyaddress
func (cli *Cli) Getreceivedbyaddress(addr string, minconf int) (string, error) {
args := cli.AppendArgs(
"getreceivedbyaddress",
addr,
strconv.Itoa(minconf),
)
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
return cmdPrint, nil
}
// Importprivkey https://bitcoin.org/en/developer-reference#importprivkey
func (cli *Cli) Importprivkey(cmd btcjson.ImportPrivKeyCmd) error {
args := cli.AppendArgs("importprivkey", cmd.PrivKey)
if cmd.Label != nil {
args = append(args, *cmd.Label)
} else {
args = append(args, "")
}
if cmd.Rescan != nil {
args = append(args, strconv.FormatBool(*cmd.Rescan))
}
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
if strings.Contains(cmdPrint, "error") {
return fmt.Errorf("import privkey return error: %s", cmdPrint)
}
return nil
}
// Importpubkey https://bitcoin.org/en/developer-reference#importpubkey
func (cli *Cli) Importpubkey(cmd btcjson.ImportPubKeyCmd) error {
args := cli.AppendArgs("importpubkey", cmd.PubKey)
if cmd.Label != nil {
args = append(args, *cmd.Label)
} else {
args = append(args, "")
}
if cmd.Rescan != nil {
args = append(args, strconv.FormatBool(*cmd.Rescan))
}
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
if strings.Contains(cmdPrint, "error") {
return fmt.Errorf("import privkey return error: %s", cmdPrint)
}
return nil
}
// Importaddress .
func (cli *Cli) Importaddress(cmd btcjson.ImportAddressCmd) error {
args := cli.AppendArgs(
"importaddress",
cmd.Address,
"", //TODO process label
"true", //TODO process rescan
//TODO other options
)
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
if strings.Contains(cmdPrint, "error") {
return fmt.Errorf("Not null resp: %s", cmdPrint)
}
return nil
}
// Listunspent https://bitcoin.org/en/developer-reference#listunspent
func (cli *Cli) Listunspent(minconf, maxconf int, addresses []string, includeUnsafe *bool, query *btcjson.ListUnspentQueryOptions) ([]btcjson.ListUnspentResult, error) {
if includeUnsafe == nil {
includeUnsafe = btcjson.Bool(false)
}
args := cli.AppendArgs(
"listunspent",
strconv.Itoa(minconf),
strconv.Itoa(maxconf),
ToJSON(addresses),
strconv.FormatBool(*includeUnsafe),
)
if query != nil {
args = append(args, ToJSON(query))
}
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
var unspents []btcjson.ListUnspentResult
err := json.Unmarshal([]byte(cmdPrint), &unspents)
return unspents, err
}
// Sendtoaddress https://bitcoin.org/en/developer-reference#sendtoaddress
func (cli *Cli) Sendtoaddress(cmd *btcjson.SendToAddressCmd) (string, error) {
args := cli.AppendArgs(
"sendtoaddress",
cmd.Address,
strconv.FormatFloat(cmd.Amount, 'f', 6, 64),
)
if cmd.Comment != nil {
args = append(args, *cmd.Comment)
} else {
args = append(args, "")
}
if cmd.CommentTo != nil {
args = append(args, *cmd.CommentTo)
} else {
args = append(args, "")
}
//TODO support other params
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
//TODO validate hex
return cmdPrint, nil
}
// Sendrawtransaction https://bitcoin.org/en/developer-reference#sendrawtransaction
func (cli *Cli) Sendrawtransaction(cmd btcjson.SendRawTransactionCmd) (string, error) {
args := cli.AppendArgs(
"sendrawtransaction",
cmd.HexTx,
)
if cmd.AllowHighFees != nil {
args = append(args, strconv.FormatBool(*cmd.AllowHighFees))
}
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
//TODO validate hex
if strings.Contains(cmdPrint, "error") {
return "", fmt.Errorf("send rawtransaction return error: %s", cmdPrint)
}
return cmdPrint, nil
}
// Signrawtransactionwithkey https://bitcoin.org/en/developer-reference#signrawtransactionwithkey
func (cli *Cli) Signrawtransactionwithkey(cmd btcjson.SignRawTransactionCmd) (btcjson.SignRawTransactionResult, error) {
args := cli.AppendArgs(
"signrawtransactionwithkey",
cmd.RawTx,
ToJSON(cmd.PrivKeys),
IfOrString(len(cmd.Prevtxs) > 0, ToJSON(cmd.Prevtxs), ""),
)
if cmd.Sighashtype != nil {
args = append(args, *cmd.Sighashtype)
}
cmdPrint := cmdAndPrint(exec.Command(
CmdBitcoinCli, args...,
))
var res btcjson.SignRawTransactionResult
err := json.Unmarshal([]byte(cmdPrint), &res)
if err != nil {
return res, fmt.Errorf("failed to decode resp, %v, \n%s", err, cmdPrint)
}
return res, err
}
// ValidateAddressResp .
type ValidateAddressResp struct {
Isvalid bool `json:"isvalid"`
Address string `json:"address"`
ScriptPubKey string `json:"scriptPubKey"`
Isscript bool `json:"isscript"`
Iswitness bool `json:"iswitness"`
WitnessVersion string `json:"witness_version"` // version (numeric, optional) The version number of the witness program
WitnessProgram string `json:"witness_program"` // "hex" (string, optional) The hex value of the witness program
}
// Validateaddress .
func (cli *Cli) Validateaddress(addr string) (ValidateAddressResp, error) {
validateCmd := exec.Command(CmdBitcoinCli, cli.AppendArgs("validateaddress", addr)...)
cmdPrint := cmdAndPrint(validateCmd) //auto print result
var resp ValidateAddressResp
err := json.Unmarshal([]byte(cmdPrint), &resp)
if err != nil {
err = fmt.Errorf("Failed to decode validate address resp,(%s), err: %v", cmdPrint, err)
}
return resp, err
}