forked from btcsuite/btcd
/
btcctl.go
1076 lines (940 loc) · 37.2 KB
/
btcctl.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
// "github.com/FactomProject/FactomCode/util"
"github.com/FactomProject/btcd/btcjson"
"github.com/FactomProject/btcd/btcjson/btcws"
flags "github.com/FactomProject/go-flags"
"github.com/davecgh/go-spew/spew"
)
// conversionHandler is a handler that is used to convert parameters from the
// command line to a specific type. This is needed since the btcjson API
// expects certain types for various parameters.
type conversionHandler func(string) (interface{}, error)
// displayHandler is a handler that takes an interface and displays it to
// standard out. It is used by the handler data to type assert replies and
// show them formatted as desired.
type displayHandler func(interface{}) error
// handlerData contains information about how a command should be handled.
type handlerData struct {
requiredArgs int
optionalArgs int
displayHandler displayHandler
conversionHandlers []conversionHandler
makeCmd func([]interface{}) (btcjson.Cmd, error)
usage string
}
// Errors used in the various handlers.
var (
ErrNoDisplayHandler = errors.New("no display handler specified")
ErrUsage = errors.New("btcctl usage") // Real usage is shown.
)
const outpointArrayStr = `"[{"txid":"id","vout":n},...]"`
// commandHandlers is a map of commands and associated handler data that is used
// to validate correctness and perform the command.
var commandHandlers = map[string]*handlerData{
"addmultisigaddress": {2, 1, displayGeneric, []conversionHandler{toInt, nil, nil}, makeAddMultiSigAddress, "<numrequired> <[\"pubkey\",...]> [account]"},
"addnode": {2, 0, displayJSONDump, nil, makeAddNode, "<ip> <add/remove/onetry>"},
"createencryptedwallet": {1, 0, displayGeneric, nil, makeCreateEncryptedWallet, "<passphrase>"},
"createnewaccount": {1, 0, displayGeneric, nil, makeCreateNewAccount, "<account>"},
// "createrawtransaction": {2, 0, displayGeneric, nil, makeCreateRawTransaction, outpointArrayStr + " " + "\"{\"address\":amount,...}\""},
"debuglevel": {1, 0, displayGeneric, nil, makeDebugLevel, "<levelspec>"},
// "decoderawtransaction": {1, 0, displayJSONDump, nil, makeDecodeRawTransaction, "<txhash>"},
"decodescript": {1, 0, displayJSONDump, nil, makeDecodeScript, "<hex>"},
"dumpprivkey": {1, 0, displayGeneric, nil, makeDumpPrivKey, "<bitcoinaddress>"},
"estimatefee": {1, 0, displayGeneric, []conversionHandler{toInt64}, makeEstimateFee, "<numblocks>"},
"estimatepriority": {1, 0, displayGeneric, []conversionHandler{toInt64}, makeEstimatePriority, "<numblocks>"},
"getaccount": {1, 0, displayGeneric, nil, makeGetAccount, "<address>"},
"getaccountaddress": {1, 0, displayGeneric, nil, makeGetAccountAddress, "<account>"},
"getaddednodeinfo": {1, 1, displayJSONDump, []conversionHandler{toBool, nil}, makeGetAddedNodeInfo, "<dns> [node]"},
"getaddressesbyaccount": {1, 0, displayJSONDump, nil, makeGetAddressesByAccount, "[account]"},
"getbalance": {0, 2, displayGeneric, []conversionHandler{nil, toInt}, makeGetBalance, "[account] [minconf=1]"},
"getbestblockhash": {0, 0, displayGeneric, nil, makeGetBestBlockHash, ""},
"getblock": {1, 2, displayJSONDump, []conversionHandler{nil, toBool, toBool}, makeGetBlock, "<blockhash>"},
"getblockchaininfo": {0, 0, displayJSONDump, nil, makeGetBlockChainInfo, ""},
"getblockcount": {0, 0, displayGeneric, nil, makeGetBlockCount, ""},
"getblockhash": {1, 0, displayGeneric, []conversionHandler{toInt64}, makeGetBlockHash, "<blocknumber>"},
"getblocktemplate": {0, 1, displayJSONDump, nil, makeGetBlockTemplate, "[jsonrequestobject]"},
"getconnectioncount": {0, 0, displayGeneric, nil, makeGetConnectionCount, ""},
"getdifficulty": {0, 0, displayFloat64, nil, makeGetDifficulty, ""},
"getgenerate": {0, 0, displayGeneric, nil, makeGetGenerate, ""},
"gethashespersec": {0, 0, displayGeneric, nil, makeGetHashesPerSec, ""},
"getinfo": {0, 0, displayJSONDump, nil, makeGetInfo, ""},
"getmininginfo": {0, 0, displayJSONDump, nil, makeGetMiningInfo, ""},
"getnetworkhashps": {0, 2, displayGeneric, []conversionHandler{toInt, toInt}, makeGetNetworkHashPS, "[blocks height]"},
"getnettotals": {0, 0, displayJSONDump, nil, makeGetNetTotals, ""},
"getnetworkinfo": {0, 0, displayJSONDump, nil, makeGetNetworkInfo, ""},
"getnewaddress": {0, 1, displayGeneric, nil, makeGetNewAddress, "[account]"},
"getpeerinfo": {0, 0, displayJSONDump, nil, makeGetPeerInfo, ""},
"getrawchangeaddress": {0, 0, displayGeneric, nil, makeGetRawChangeAddress, ""},
"getrawmempool": {0, 1, displayJSONDump, []conversionHandler{toBool}, makeGetRawMempool, "[verbose=false]"},
// "getrawtransaction": {1, 1, displayJSONDump, []conversionHandler{nil, toInt}, makeGetRawTransaction, "<txhash> [verbose=0]"},
"getreceivedbyaccount": {1, 1, displayGeneric, []conversionHandler{nil, toInt}, makeGetReceivedByAccount, "<account> [minconf=1]"},
"getreceivedbyaddress": {1, 1, displayGeneric, []conversionHandler{nil, toInt}, makeGetReceivedByAddress, "<address> [minconf=1]"},
// "gettransaction": {1, 1, displayJSONDump, nil, makeGetTransaction, "txid"},
"gettxout": {2, 1, displayJSONDump, []conversionHandler{nil, toInt, toBool}, makeGetTxOut, "<txid> <n> [includemempool=false]"},
"gettxoutsetinfo": {0, 0, displayJSONDump, nil, makeGetTxOutSetInfo, ""},
"getwork": {0, 1, displayJSONDump, nil, makeGetWork, "[data]"},
"help": {0, 1, displayGeneric, nil, makeHelp, "[commandName]"},
"importprivkey": {1, 2, displayGeneric, []conversionHandler{nil, nil, toBool}, makeImportPrivKey, "<wifprivkey> [label] [rescan=true]"},
"importwallet": {1, 0, displayGeneric, nil, makeImportWallet, "<filename>"},
"keypoolrefill": {0, 1, displayGeneric, []conversionHandler{toInt}, makeKeyPoolRefill, "[newsize]"},
"listaccounts": {0, 1, displayJSONDump, []conversionHandler{toInt}, makeListAccounts, "[minconf=1]"},
"listaddressgroupings": {0, 0, displayJSONDump, nil, makeListAddressGroupings, ""},
"listreceivedbyaccount": {0, 2, displayJSONDump, []conversionHandler{toInt, toBool}, makeListReceivedByAccount, "[minconf] [includeempty]"},
"listreceivedbyaddress": {0, 2, displayJSONDump, []conversionHandler{toInt, toBool}, makeListReceivedByAddress, "[minconf] [includeempty]"},
"listlockunspent": {0, 0, displayJSONDump, nil, makeListLockUnspent, ""},
"listsinceblock": {0, 2, displayJSONDump, []conversionHandler{nil, toInt}, makeListSinceBlock, "[blockhash] [minconf=10]"},
// "listtransactions": {0, 3, displayJSONDump, []conversionHandler{nil, toInt, toInt}, makeListTransactions, "[account] [count=10] [from=0]"},
"listunspent": {0, 3, displayJSONDump, []conversionHandler{toInt, toInt, nil}, makeListUnspent, "[minconf=1] [maxconf=9999999] [jsonaddressarray]"},
"lockunspent": {1, 2, displayJSONDump, []conversionHandler{toBool, nil}, makeLockUnspent, "<unlock> " + outpointArrayStr},
"ping": {0, 0, displayGeneric, nil, makePing, ""},
"renameaccount": {2, 0, displayGeneric, nil, makeRenameAccount, "<oldaccount> <newaccount>"},
/*
"searchrawtransactions": {1, 3, displayJSONDump, []conversionHandler{nil, toInt, toInt, toInt}, makeSearchRawTransactions, "<address> [verbose=1] [skip=0] [count=100]"},
"sendfrom": {3, 3, displayGeneric, []conversionHandler{nil, nil, toSatoshi, toInt, nil, nil},
makeSendFrom, "<account> <address> <amount> [minconf=1] [comment] [comment-to]"},
"sendmany": {2, 2, displayGeneric, []conversionHandler{nil, nil, toInt, nil}, makeSendMany, "<account> <{\"address\":amount,...}> [minconf=1] [comment]"},
"sendrawtransaction": {1, 0, displayGeneric, nil, makeSendRawTransaction, "<hextx>"},
"sendtoaddress": {2, 2, displayGeneric, []conversionHandler{nil, toSatoshi, nil, nil}, makeSendToAddress, "<address> <amount> [comment] [comment-to]"},
"setgenerate": {1, 1, displayGeneric, []conversionHandler{toBool, toInt}, makeSetGenerate, "<generate> [genproclimit]"},
"settxfee": {1, 0, displayGeneric, []conversionHandler{toSatoshi}, makeSetTxFee, "<amount>"},
"signmessage": {2, 2, displayGeneric, nil, makeSignMessage, "<address> <message>"},
"signrawtransaction": {1, 3, displayJSONDump, nil, makeSignRawTransaction, "<hex> [{\"txid\":txid,\"vout\":n,\"scriptPubKey\":hex,\"redeemScript\":hex},...] [<privatekey1>,...] [sighashtype=\"ALL\"]"},
*/
"stop": {0, 0, displayGeneric, nil, makeStop, ""},
"submitblock": {1, 1, displayGeneric, nil, makeSubmitBlock, "<hexdata> [jsonparametersobject]"},
"validateaddress": {1, 0, displayJSONDump, nil, makeValidateAddress, "<address>"},
"verifychain": {0, 2, displayJSONDump, []conversionHandler{toInt, toInt}, makeVerifyChain, "[level] [numblocks]"},
"verifymessage": {3, 0, displayGeneric, nil, makeVerifyMessage, "<address> <signature> <message>"},
"walletlock": {0, 0, displayGeneric, nil, makeWalletLock, ""},
"walletpassphrase": {1, 1, displayGeneric, []conversionHandler{nil, toInt64}, makeWalletPassphrase, "<passphrase> [timeout]"},
"walletpassphrasechange": {2, 0, displayGeneric, nil, makeWalletPassphraseChange, "<oldpassphrase> <newpassphrase>"},
}
/*
// toSatoshi attempts to convert the passed string to a satoshi amount returned
// as an int64. It returns the int64 packed into an interface so it can be used
// in the calls which expect interfaces. An error will be returned if the string
// can't be converted first to a float64.
func toSatoshi(val string) (interface{}, error) {
idx, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, err
}
amt, err := btcutil.NewAmount(idx)
if err != nil {
return nil, err
}
return int64(amt), nil
}
*/
// toInt attempts to convert the passed string to an integer. It returns the
// integer packed into an interface so it can be used in the calls which expect
// interfaces. An error will be returned if the string can't be converted to an
// integer.
func toInt(val string) (interface{}, error) {
idx, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
return idx, nil
}
// toInt64 attempts to convert the passed string to an int64. It returns the
// integer packed into an interface so it can be used in the calls which expect
// interfaces. An error will be returned if the string can't be converted to an
// integer.
func toInt64(val string) (interface{}, error) {
idx, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, err
}
return idx, nil
}
// toBool attempts to convert the passed string to a bool. It returns the
// bool packed into the empty interface so it can be used in the calls which
// accept interfaces. An error will be returned if the string can't be
// converted to a bool.
func toBool(val string) (interface{}, error) {
return strconv.ParseBool(val)
}
// displayGeneric is a displayHandler that simply displays the passed interface
// using fmt.Println.
func displayGeneric(reply interface{}) error {
fmt.Println(reply)
return nil
}
// displayFloat64 is a displayHandler that ensures the concrete type of the
// passed interface is a float64 and displays it using fmt.Printf. An error
// is returned if a float64 is not passed.
func displayFloat64(reply interface{}) error {
if val, ok := reply.(float64); ok {
fmt.Printf("%f\n", val)
return nil
}
return fmt.Errorf("reply type is not a float64: %v", spew.Sdump(reply))
}
// displaySpewDump is a displayHandler that simply uses spew.Dump to display the
// passed interface.
func displaySpewDump(reply interface{}) error {
spew.Dump(reply)
return nil
}
// displayJSONDump is a displayHandler that uses json.Indent to display the
// passed interface.
func displayJSONDump(reply interface{}) error {
marshaledBytes, err := json.Marshal(reply)
if err != nil {
return err
}
var buf bytes.Buffer
err = json.Indent(&buf, marshaledBytes, "", "\t")
if err != nil {
return err
}
fmt.Println(buf.String())
return nil
}
// makeAddMultiSigAddress generates the cmd structure for addmultisigaddress commands.
func makeAddMultiSigAddress(args []interface{}) (btcjson.Cmd, error) {
var pubkeys []string
err := json.Unmarshal([]byte(args[1].(string)), &pubkeys)
if err != nil {
return nil, err
}
var opt string
if len(args) > 2 {
opt = args[2].(string)
}
return btcjson.NewAddMultisigAddressCmd("btcctl", args[0].(int), pubkeys, opt)
}
// makeAddNode generates the cmd structure for addnode commands.
func makeAddNode(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewAddNodeCmd("btcctl", args[0].(string),
args[1].(string))
}
// makeCreateEncryptedWallet generates the cmd structure for
// createencryptedwallet commands.
func makeCreateEncryptedWallet(args []interface{}) (btcjson.Cmd, error) {
return btcws.NewCreateEncryptedWalletCmd("btcctl", args[0].(string)), nil
}
// makeCreateNewAccount generates the cmd structure for
// createnewaccount commands.
func makeCreateNewAccount(args []interface{}) (btcjson.Cmd, error) {
return btcws.NewCreateNewAccountCmd("btcctl", args[0].(string)), nil
}
/*
// makeCreateRawTransaction generates the cmd structure for createrawtransaction
// commands.
func makeCreateRawTransaction(args []interface{}) (btcjson.Cmd, error) {
var inputs []btcjson.TransactionInput
err := json.Unmarshal([]byte(args[0].(string)), &inputs)
if err != nil {
return nil, err
}
var famounts map[string]float64
err = json.Unmarshal([]byte(args[1].(string)), &famounts)
if err != nil {
return nil, err
}
amounts := make(map[string]int64, len(famounts))
for k, v := range famounts {
amt, err := btcutil.NewAmount(v)
if err != nil {
return nil, err
}
amounts[k] = int64(amt)
}
return btcjson.NewCreateRawTransactionCmd("btcctl", inputs, amounts)
}
*/
// makeDebugLevel generates the cmd structure for debuglevel commands.
func makeDebugLevel(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewDebugLevelCmd("btcctl", args[0].(string))
}
// makeDecodeRawTransaction generates the cmd structure for
// decoderawtransaction commands.
func makeDecodeRawTransaction(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewDecodeRawTransactionCmd("btcctl", args[0].(string))
}
// makeDecodeScript generates the cmd structure for decodescript commands.
func makeDecodeScript(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewDecodeScriptCmd("btcctl", args[0].(string))
}
// makeDumpPrivKey generates the cmd structure for
// dumpprivkey commands.
func makeDumpPrivKey(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewDumpPrivKeyCmd("btcctl", args[0].(string))
}
// makeEstimateFee generates the cmd structure for estimatefee commands.
func makeEstimateFee(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewEstimateFeeCmd("btcctl", args[0].(int64))
}
// makeEstimatePriority generates the cmd structure for estimatepriority commands.
func makeEstimatePriority(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewEstimatePriorityCmd("btcctl", args[0].(int64))
}
// makeGetAccount generates the cmd structure for
// getaccount commands.
func makeGetAccount(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetAccountCmd("btcctl", args[0].(string))
}
// makeGetAccountAddress generates the cmd structure for
// getaccountaddress commands.
func makeGetAccountAddress(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetAccountAddressCmd("btcctl", args[0].(string))
}
// makeGetAddedNodeInfo generates the cmd structure for
// getaccountaddress commands.
func makeGetAddedNodeInfo(args []interface{}) (btcjson.Cmd, error) {
// Create the getaddednodeinfo command with defaults for the optional
// parameters.
cmd, err := btcjson.NewGetAddedNodeInfoCmd("btcctl", args[0].(bool))
if err != nil {
return nil, err
}
// Override the optional parameter if it was specified.
if len(args) > 1 {
cmd.Node = args[1].(string)
}
return cmd, nil
}
// makeGetAddressesByAccount generates the cmd structure for
// getaddressesbyaccount commands.
func makeGetAddressesByAccount(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetAddressesByAccountCmd("btcctl", args[0].(string))
}
// makeGetBalance generates the cmd structure for
// getbalance commands.
func makeGetBalance(args []interface{}) (btcjson.Cmd, error) {
optargs := make([]interface{}, 0, 2)
if len(args) > 0 {
optargs = append(optargs, args[0].(string))
}
if len(args) > 1 {
optargs = append(optargs, args[1].(int))
}
return btcjson.NewGetBalanceCmd("btcctl", optargs...)
}
// makeGetBestBlockHash generates the cmd structure for
// makebestblockhash commands.
func makeGetBestBlockHash(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBestBlockHashCmd("btcctl")
}
// makeGetBlock generates the cmd structure for getblock commands.
func makeGetBlock(args []interface{}) (btcjson.Cmd, error) {
// Create the getblock command with defaults for the optional
// parameters.
getBlockCmd, err := btcjson.NewGetBlockCmd("btcctl", args[0].(string))
if err != nil {
return nil, err
}
// Override the optional parameters if they were specified.
if len(args) > 1 {
getBlockCmd.Verbose = args[1].(bool)
}
if len(args) > 2 {
getBlockCmd.VerboseTx = args[2].(bool)
}
return getBlockCmd, nil
}
// makeGetBlockChainInfo generates the cmd structure for getblockchaininfo commands.
func makeGetBlockChainInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBlockChainInfoCmd("btcctl")
}
// makeGetBlockCount generates the cmd structure for getblockcount commands.
func makeGetBlockCount(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBlockCountCmd("btcctl")
}
// makeGetBlockHash generates the cmd structure for getblockhash commands.
func makeGetBlockHash(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBlockHashCmd("btcctl", args[0].(int64))
}
// makeGetBlockTemplate generates the cmd structure for getblocktemplate commands.
func makeGetBlockTemplate(args []interface{}) (btcjson.Cmd, error) {
cmd, err := btcjson.NewGetBlockTemplateCmd("btcctl")
if err != nil {
return nil, err
}
if len(args) == 1 {
err = cmd.UnmarshalJSON([]byte(args[0].(string)))
if err != nil {
return nil, err
}
}
return cmd, nil
}
// makeGetConnectionCount generates the cmd structure for
// getconnectioncount commands.
func makeGetConnectionCount(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetConnectionCountCmd("btcctl")
}
// makeGetDifficulty generates the cmd structure for
// getdifficulty commands.
func makeGetDifficulty(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetDifficultyCmd("btcctl")
}
// makeGetGenerate generates the cmd structure for
// getgenerate commands.
func makeGetGenerate(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetGenerateCmd("btcctl")
}
// makeGetHashesPerSec generates the cmd structure for gethashespersec commands.
func makeGetHashesPerSec(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetHashesPerSecCmd("btcctl")
}
// makeGetInfo generates the cmd structure for getinfo commands.
func makeGetInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetInfoCmd("btcctl")
}
// makeGetMiningInfo generates the cmd structure for getmininginfo commands.
func makeGetMiningInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetMiningInfoCmd("btcctl")
}
// makeGetNetworkHashPS generates the cmd structure for getnetworkhashps
// commands.
func makeGetNetworkHashPS(args []interface{}) (btcjson.Cmd, error) {
// Create the getnetworkhashps command with defaults for the optional
// parameters.
cmd, err := btcjson.NewGetNetworkHashPSCmd("btcctl")
if err != nil {
return nil, err
}
// Override the optional blocks if specified.
if len(args) > 0 {
cmd.Blocks = args[0].(int)
}
// Override the optional height if specified.
if len(args) > 1 {
cmd.Height = args[1].(int)
}
return cmd, nil
}
// makeGetNetworkInfo generates the cmd structure for getnetworkinfo commands.
func makeGetNetworkInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetNetworkInfoCmd("btcctl")
}
// makeGetNetTotals generates the cmd structure for getnettotals commands.
func makeGetNetTotals(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetNetTotalsCmd("btcctl")
}
// makeGetNewAddress generates the cmd structure for getnewaddress commands.
func makeGetNewAddress(args []interface{}) (btcjson.Cmd, error) {
var account string
if len(args) > 0 {
account = args[0].(string)
}
return btcjson.NewGetNewAddressCmd("btcctl", account)
}
// makePeerInfo generates the cmd structure for
// getpeerinfo commands.
func makeGetPeerInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetPeerInfoCmd("btcctl")
}
// makeGetRawChangeAddress generates the cmd structure for getrawchangeaddress commands.
func makeGetRawChangeAddress(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetRawChangeAddressCmd("btcctl")
}
// makeRawMempool generates the cmd structure for
// getrawmempool commands.
func makeGetRawMempool(args []interface{}) (btcjson.Cmd, error) {
opt := make([]bool, 0, 1)
if len(args) > 0 {
opt = append(opt, args[0].(bool))
}
return btcjson.NewGetRawMempoolCmd("btcctl", opt...)
}
// makeGetReceivedByAccount generates the cmd structure for
// getreceivedbyaccount commands.
func makeGetReceivedByAccount(args []interface{}) (btcjson.Cmd, error) {
opt := make([]int, 0, 1)
if len(args) > 1 {
opt = append(opt, args[1].(int))
}
return btcjson.NewGetReceivedByAccountCmd("btcctl", args[0].(string), opt...)
}
// makeGetReceivedByAddress generates the cmd structure for
// getreceivedbyaddress commands.
func makeGetReceivedByAddress(args []interface{}) (btcjson.Cmd, error) {
opt := make([]int, 0, 1)
if len(args) > 1 {
opt = append(opt, args[1].(int))
}
return btcjson.NewGetReceivedByAddressCmd("btcctl", args[0].(string), opt...)
}
// makeGetTransaction generates the cmd structure for gettransaction commands.
func makeGetTransaction(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetTransactionCmd("btcctl", args[0].(string))
}
// makeGetTxOut generates the cmd structure for gettxout commands.
func makeGetTxOut(args []interface{}) (btcjson.Cmd, error) {
opt := make([]bool, 0, 1)
if len(args) > 2 {
opt = append(opt, args[2].(bool))
}
return btcjson.NewGetTxOutCmd("btcctl", args[0].(string), args[1].(int), opt...)
}
// makeGetTxOutSetInfo generates the cmd structure for gettxoutsetinfo commands.
func makeGetTxOutSetInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetTxOutSetInfoCmd("btcctl")
}
func makeGetWork(args []interface{}) (btcjson.Cmd, error) {
cmd, err := btcjson.NewGetWorkCmd("btcctl")
if err != nil {
return nil, err
}
if len(args) == 1 {
cmd.Data = args[0].(string)
}
return cmd, nil
}
func makeHelp(args []interface{}) (btcjson.Cmd, error) {
opt := make([]string, 0, 1)
if len(args) > 0 {
opt = append(opt, args[0].(string))
}
return btcjson.NewHelpCmd("btcctl", opt...)
}
// makeRawTransaction generates the cmd structure for
// getrawtransaction commands.
func makeGetRawTransaction(args []interface{}) (btcjson.Cmd, error) {
opt := make([]int, 0, 1)
if len(args) > 1 {
opt = append(opt, args[1].(int))
}
return btcjson.NewGetRawTransactionCmd("btcctl", args[0].(string), opt...)
}
// makeImportPrivKey generates the cmd structure for
// importprivkey commands.
func makeImportPrivKey(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 2)
if len(args) > 1 {
optargs = append(optargs, args[1].(string))
}
if len(args) > 2 {
optargs = append(optargs, args[2].(bool))
}
return btcjson.NewImportPrivKeyCmd("btcctl", args[0].(string), optargs...)
}
// makeImportWallet generates the cmd structure for
// importwallet commands.
func makeImportWallet(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewImportWalletCmd("btcctl", args[0].(string))
}
// makeKeyPoolRefill generates the cmd structure for keypoolrefill commands.
func makeKeyPoolRefill(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]uint, 0, 1)
if len(args) > 0 {
optargs = append(optargs, uint(args[0].(int)))
}
return btcjson.NewKeyPoolRefillCmd("btcctl", optargs...)
}
// makeListAccounts generates the cmd structure for listaccounts commands.
func makeListAccounts(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]int, 0, 1)
if len(args) > 0 {
optargs = append(optargs, args[0].(int))
}
return btcjson.NewListAccountsCmd("btcctl", optargs...)
}
// makeListAddressGroupings generates the cmd structure for listaddressgroupings commands.
func makeListAddressGroupings(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewListAddressGroupingsCmd("btcctl")
}
// makeListReceivedByAccount generates the cmd structure for listreceivedbyaccount commands.
func makeListReceivedByAccount(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 2)
if len(args) > 0 {
optargs = append(optargs, args[0].(int))
}
if len(args) > 1 {
optargs = append(optargs, args[1].(bool))
}
return btcjson.NewListReceivedByAccountCmd("btcctl", optargs...)
}
// makeListReceivedByAddress generates the cmd structure for listreceivedbyaddress commands.
func makeListReceivedByAddress(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 2)
if len(args) > 0 {
optargs = append(optargs, args[0].(int))
}
if len(args) > 1 {
optargs = append(optargs, args[1].(bool))
}
return btcjson.NewListReceivedByAddressCmd("btcctl", optargs...)
}
// makeListLockUnspent generates the cmd structure for listlockunspent commands.
func makeListLockUnspent(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewListLockUnspentCmd("btcctl")
}
// makeListSinceBlock generates the cmd structure for
// listsinceblock commands.
func makeListSinceBlock(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 2)
if len(args) > 0 {
optargs = append(optargs, args[0].(string))
}
if len(args) > 1 {
optargs = append(optargs, args[1].(int))
}
return btcjson.NewListSinceBlockCmd("btcctl", optargs...)
}
// makeListTransactions generates the cmd structure for
// listtransactions commands.
func makeListTransactions(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 3)
if len(args) > 0 {
optargs = append(optargs, args[0].(string))
}
if len(args) > 1 {
optargs = append(optargs, args[1].(int))
}
if len(args) > 2 {
optargs = append(optargs, args[2].(int))
}
return btcjson.NewListTransactionsCmd("btcctl", optargs...)
}
// makeListUnspent generates the cmd structure for listunspent commands.
func makeListUnspent(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 3)
if len(args) > 0 {
optargs = append(optargs, args[0].(int))
}
if len(args) > 1 {
optargs = append(optargs, args[1].(int))
}
if len(args) > 2 {
var addrs []string
err := json.Unmarshal([]byte(args[2].(string)), &addrs)
if err != nil {
return nil, err
}
optargs = append(optargs, addrs)
}
return btcjson.NewListUnspentCmd("btcctl", optargs...)
}
// makeLockUnspent generates the cmd structure for lockunspent commands.
func makeLockUnspent(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([][]btcjson.TransactionInput, 0, 1)
if len(args) > 1 {
var inputs []btcjson.TransactionInput
err := json.Unmarshal([]byte(args[1].(string)), &inputs)
if err != nil {
return nil, err
}
optargs = append(optargs, inputs)
}
return btcjson.NewLockUnspentCmd("btcctl", args[0].(bool), optargs...)
}
// makePing generates the cmd structure for ping commands.
func makePing(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewPingCmd("btcctl")
}
// makeRenameAccount generates the cmd structure for
// renameaccount commands.
func makeRenameAccount(args []interface{}) (btcjson.Cmd, error) {
return btcws.NewRenameAccountCmd("btcctl", args[0].(string),
args[1].(string)), nil
}
// makeSearchRawTransactions generates the cmd strucutre for
// searchrawtransactions commands.
func makeSearchRawTransactions(args []interface{}) (btcjson.Cmd, error) {
optArgs := make([]interface{}, 0, 3)
if len(args) > 1 {
optArgs = append(optArgs, args[1].(int))
}
if len(args) > 2 {
optArgs = append(optArgs, args[2].(int))
}
if len(args) > 3 {
optArgs = append(optArgs, args[3].(int))
}
return btcjson.NewSearchRawTransactionsCmd("btcctl", args[0].(string),
optArgs...)
}
// makeSendFrom generates the cmd structure for sendfrom commands.
func makeSendFrom(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]interface{}, 0, 3)
if len(args) > 3 {
optargs = append(optargs, args[3].(int))
}
if len(args) > 4 {
optargs = append(optargs, args[4].(string))
}
if len(args) > 5 {
optargs = append(optargs, args[5].(string))
}
return btcjson.NewSendFromCmd("btcctl", args[0].(string),
args[1].(string), args[2].(int64), optargs...)
}
/*
// makeSendMany generates the cmd structure for sendmany commands.
func makeSendMany(args []interface{}) (btcjson.Cmd, error) {
origPairs := make(map[string]float64)
err := json.Unmarshal([]byte(args[1].(string)), &origPairs)
if err != nil {
return nil, err
}
pairs := make(map[string]int64)
for addr, value := range origPairs {
pairs[addr] = int64(btcutil.SatoshiPerBitcoin * value)
}
var optargs = make([]interface{}, 0, 2)
if len(args) > 2 {
optargs = append(optargs, args[2].(int))
}
if len(args) > 3 {
optargs = append(optargs, args[3].(string))
}
return btcjson.NewSendManyCmd("btcctl", args[0].(string), pairs, optargs...)
}
*/
// makeSendRawTransaction generates the cmd structure for sendrawtransaction
// commands.
func makeSendRawTransaction(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewSendRawTransactionCmd("btcctl", args[0].(string))
}
// makeSendToAddress generates the cmd struture for sendtoaddress commands.
func makeSendToAddress(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewSendToAddressCmd("btcctl", args[0].(string), args[1].(int64), args[2:]...)
}
// makeSetGenerate generates the cmd structure for setgenerate commands.
func makeSetGenerate(args []interface{}) (btcjson.Cmd, error) {
var optargs = make([]int, 0, 1)
if len(args) > 1 {
optargs = append(optargs, args[1].(int))
}
return btcjson.NewSetGenerateCmd("btcctl", args[0].(bool), optargs...)
}
// makeSetTxFee generates the cmd structure for settxfee commands.
func makeSetTxFee(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewSetTxFeeCmd("btcctl", args[0].(int64))
}
// makeSignMessage generates the cmd structure for signmessage commands.
func makeSignMessage(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewSignMessageCmd("btcctl", args[0].(string),
args[1].(string))
}
// makeSignRawTransaction generates the cmd structure for signrawtransaction commands.
func makeSignRawTransaction(args []interface{}) (btcjson.Cmd, error) {
optArgs := make([]interface{}, 0, 3)
if len(args) > 1 {
var inputs []btcjson.RawTxInput
err := json.Unmarshal([]byte(args[1].(string)), &inputs)
if err != nil {
return nil, err
}
optArgs = append(optArgs, inputs)
}
if len(args) > 2 {
var inputs []string
err := json.Unmarshal([]byte(args[2].(string)), &inputs)
if err != nil {
return nil, err
}
optArgs = append(optArgs, inputs)
}
if len(args) > 3 {
optArgs = append(optArgs, args[3])
}
return btcjson.NewSignRawTransactionCmd("btcctl", args[0].(string), optArgs...)
}
// makeStop generates the cmd structure for stop commands.
func makeStop(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewStopCmd("btcctl")
}
// makeSubmitBlock generates the cmd structure for submitblock commands.
func makeSubmitBlock(args []interface{}) (btcjson.Cmd, error) {
opts := &btcjson.SubmitBlockOptions{}
if len(args) == 2 {
opts.WorkID = args[1].(string)
}
return btcjson.NewSubmitBlockCmd("btcctl", args[0].(string), opts)
}
// makeValidateAddress generates the cmd structure for validateaddress commands.
func makeValidateAddress(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewValidateAddressCmd("btcctl", args[0].(string))
}
// makeVerifyChain generates the cmd structure for verifychain commands.
func makeVerifyChain(args []interface{}) (btcjson.Cmd, error) {
iargs := make([]int32, 0, 2)
for _, i := range args {
iargs = append(iargs, int32(i.(int)))
}
return btcjson.NewVerifyChainCmd("btcctl", iargs...)
}
func makeVerifyMessage(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewVerifyMessageCmd("btcctl", args[0].(string),
args[1].(string), args[2].(string))
}
// makeWalletLock generates the cmd structure for walletlock commands.
func makeWalletLock(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewWalletLockCmd("btcctl")
}
// makeWalletPassphrase generates the cmd structure for walletpassphrase commands.
func makeWalletPassphrase(args []interface{}) (btcjson.Cmd, error) {
timeout := int64(60)
if len(args) > 1 {
timeout = args[1].(int64)
}
return btcjson.NewWalletPassphraseCmd("btcctl", args[0].(string), timeout)
}
// makeWalletPassphraseChange generates the cmd structure for
// walletpassphrasechange commands.
func makeWalletPassphraseChange(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewWalletPassphraseChangeCmd("btcctl", args[0].(string),
args[1].(string))
}
// send sends a JSON-RPC command to the specified RPC server and examines the
// results for various error conditions. It either returns a valid result or
// an appropriate error.
func send(cfg *config, msg []byte) (interface{}, error) {
var reply btcjson.Reply
var err error
if cfg.NoTLS || (cfg.RPCCert == "" && !cfg.TLSSkipVerify) {
reply, err = btcjson.RpcCommand(cfg.RPCUser, cfg.RPCPassword,
cfg.RPCServer, msg)
} else {
var pem []byte
if cfg.RPCCert != "" {
pem, err = ioutil.ReadFile(cfg.RPCCert)
if err != nil {
return nil, err
}
}
reply, err = btcjson.TlsRpcCommand(cfg.RPCUser,
cfg.RPCPassword, cfg.RPCServer, msg, pem,
cfg.TLSSkipVerify)
}
if err != nil {
return nil, err
}
if reply.Error != nil {
return nil, reply.Error
}
return reply.Result, nil
}
// sendCommand creates a JSON-RPC command using the passed command and arguments
// and then sends it. A prefix is added to any errors that occur indicating
// what step failed.
func sendCommand(cfg *config, command btcjson.Cmd) (interface{}, error) {
msg, err := json.Marshal(command)
if err != nil {
return nil, fmt.Errorf("createMessage: %v", err.Error())
}
reply, err := send(cfg, msg)
if err != nil {
return nil, fmt.Errorf("rpcCommand: %v", err.Error())
}
return reply, nil
}
// commandHandler handles commands provided via the cli using the specific
// handler data to instruct the handler what to do.
func commandHandler(cfg *config, command string, data *handlerData, args []string) error {
// Ensure the number of arguments are the expected value.
if len(args) < data.requiredArgs {
return ErrUsage
}
if len(args) > data.requiredArgs+data.optionalArgs {
return ErrUsage
}
// Ensure there is a display handler.
if data.displayHandler == nil {
return ErrNoDisplayHandler
}
// Ensure the number of conversion handlers is valid if any are
// specified.
convHandlers := data.conversionHandlers
if convHandlers != nil && len(convHandlers) < len(args) {
return fmt.Errorf("the number of conversion handlers is invalid")
}
// Convert input parameters per the conversion handlers.
iargs := make([]interface{}, len(args))
for i, arg := range args {
iargs[i] = arg
}
for i := range iargs {
if convHandlers != nil {
converter := convHandlers[i]
if converter != nil {
convertedArg, err := converter(args[i])
if err != nil {
return err
}
iargs[i] = convertedArg
}
}
}
cmd, err := data.makeCmd(iargs)
if err != nil {