forked from Qitmeer/qx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qx.go
1074 lines (983 loc) · 31.9 KB
/
qx.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
// Copyright 2017-2018 The qitmeer developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"github.com/Qitmeer/qitmeer-lib/crypto/seed"
"github.com/Qitmeer/qitmeer-lib/wallet"
"github.com/Qitmeer/qx/core"
"io/ioutil"
"os"
"strings"
)
const (
QX_VERSION = "0.0.1"
TX_VERION = 1 //default version is 1
)
func usage() {
fmt.Fprintf(os.Stderr,"Usage: qx [--version] [--help] <command> [<args>]\n")
fmt.Fprintf(os.Stderr,`
encode and decode :
base58-encode encode a base16 string to a base58 string
base58-decode decode a base58 string to a base16 string
base58check-encode encode a base58check string
base58check-decode decode a base58check string
base64-encode encode a base16 string to a base64 string
base64-decode encode a base64 string to a base16 string
rlp-encode encode a string to a rlp encoded base16 string
rlp-decode decode a rlp base16 string to a human-readble representation
hash :
blake2b256 calculate Blake2b 256 hash of a base16 data.
blake2b512 calculate Blake2b 512 hash of a base16 data.
sha256 calculate SHA256 hash of a base16 data.
sha3-256 calculate SHA3 256 hash of a base16 data.
keccak-256 calculate legacy keccak 256 hash of a bash16 data.
blake256 calculate blake256 hash of a base16 data.
ripemd160 calculate ripemd160 hash of a base16 data.
bitcoin160 calculate ripemd160(sha256(data))
hash160 calculate ripemd160(blake2b256(data))
entropy (seed) & mnemoic & hd & ec
entropy generate a cryptographically secure pseudorandom entropy (seed)
hd-new create a new HD(BIP32) private key from an entropy (seed)
hd-to-ec convert the HD (BIP32) format private/public key to a EC private/public key
hd-to-public derive the HD (BIP32) public key from a HD private key
hd-decode decode a HD (BIP32) private/public key serialization format
hd-derive Derive a child HD (BIP32) key from another HD public or private key.
mnemonic-new create a mnemonic world-list (BIP39) from an entropy
mnemonic-to-entropy return back to the entropy (the random seed) from a mnemonic world list (BIP39)
mnemonic-to-seed convert a mnemonic world-list (BIP39) to its 512 bits seed
ec-new create a new EC private key from an entropy (seed).
ec-to-public derive the EC public key from an EC private key (the compressed format by default )
ec-to-wif convert an EC private key to a WIF, associates with the compressed public key by default.
wif-to-ec convert a WIF private key to an EC private key.
wif-to-public derive the EC public key from a WIF private key.
addr & tx & sign
ec-to-addr convert an EC public key to a paymant address. default is qx address
tx-encode encode a unsigned transaction.
tx-decode decode a transaction in base16 to json format.
tx-sign sign a transactions using a private key.
msg-sign create a message signature
msg-verify validate a message signature
signature-decode decode a ECDSA signature
`)
os.Exit(1)
}
func cmdUsage (cmd *flag.FlagSet, usage string){
fmt.Fprintf(os.Stderr, usage)
cmd.PrintDefaults()
}
func version() {
fmt.Fprintf(os.Stderr,"Qx Version : %q\n",QX_VERSION)
os.Exit(1)
}
func errExit(err error){
fmt.Fprintf(os.Stderr, "Qx Error : %q\n",err)
os.Exit(1)
}
var base58checkVersion core.QitmeerBase58checkVersionFlag
var base58checkVersionSize int
var base58checkMode string
var showDetails bool
var base58checkHasher string
var base58checkCksumSize int
var seedSize uint
var hdVer core.Bip32VersionFlag
var hdHarden bool
var hdIndex uint
var derivePath core.DerivePathFlag
var mnemoicSeedPassphrase string
var curve string
var uncompressedPKFormat bool
var network string
var txInputs core.TxInputsFlag
var txOutputs core.TxOutputsFlag
var txVersion core.TxVersionFlag
var txLockTime core.TxLockTimeFlag
var privateKey string
var msgSignatureMode string
func main() {
// ----------------------------
// cmd for encoding & decoding
// ----------------------------
base58CheckEncodeCommand := flag.NewFlagSet("base58check-encode", flag.ExitOnError)
base58checkVersion = core.QitmeerBase58checkVersionFlag{}
base58checkVersion.Set("testnet")
base58CheckEncodeCommand.Var(&base58checkVersion, "v","base58check `version` [mainnet|testnet|privnet")
base58CheckEncodeCommand.StringVar(&base58checkMode, "m", "qitmeer", "base58check encode mode : [qitmeer|btc]")
base58CheckEncodeCommand.StringVar(&base58checkHasher,"a","", "base58check hasher")
base58CheckEncodeCommand.IntVar(&base58checkCksumSize,"c",4, "base58check checksum size")
base58CheckEncodeCommand.Usage = func() {
cmdUsage(base58CheckEncodeCommand,"Usage: qx base58check-encode [-v <ver>] [hexstring]\n")
}
base58CheckDecodeCommand := flag.NewFlagSet("base58check-decode", flag.ExitOnError)
base58CheckDecodeCommand.BoolVar(&showDetails,"d",false, "show decode details")
base58CheckDecodeCommand.StringVar(&base58checkMode,"m","qitmeer", "base58check decode `mode`: [qitmeer|btc]")
base58CheckDecodeCommand.StringVar(&base58checkHasher,"a","", "base58check `hasher`")
base58CheckDecodeCommand.IntVar(&base58checkVersionSize,"vs",2, "base58check version `size`")
base58CheckDecodeCommand.IntVar(&base58checkCksumSize,"cs",4, "base58check checksum `size`")
base58CheckDecodeCommand.Usage = func() {
cmdUsage(base58CheckDecodeCommand,"Usage: qx base58check-decode [hexstring]\n")
}
base58EncodeCmd := flag.NewFlagSet("base58-encode",flag.ExitOnError)
base58EncodeCmd.Usage = func() {
cmdUsage(base58EncodeCmd ,"Usage: qx base58-encode [hexstring]\n")
}
base58DecodeCmd := flag.NewFlagSet("base58-decode",flag.ExitOnError)
base58DecodeCmd.Usage = func() {
cmdUsage(base58DecodeCmd, "Usage: qx base58-decode [hexstring]\n")
}
base64EncodeCmd := flag.NewFlagSet("base64-encode",flag.ExitOnError)
base64EncodeCmd.Usage = func() {
cmdUsage(base64EncodeCmd ,"Usage: qx base64-encode [hexstring]\n")
}
base64DecodeCmd := flag.NewFlagSet("base64-decode",flag.ExitOnError)
base64DecodeCmd.Usage = func() {
cmdUsage(base64DecodeCmd, "Usage: qx base64-decode [hexstring]\n")
}
rlpEncodeCmd := flag.NewFlagSet("rlp-encode",flag.ExitOnError)
rlpEncodeCmd.Usage = func() {
cmdUsage(rlpEncodeCmd, "Usage: qx rlp-encode [string]\n")
}
rlpDecodeCmd := flag.NewFlagSet("rlp-decode",flag.ExitOnError)
rlpDecodeCmd.Usage = func() {
cmdUsage(rlpDecodeCmd, "Usage: qx rlp-decode [hexstring]\n")
}
// ----------------------------
// cmd for hashing
// ----------------------------
sha256cmd := flag.NewFlagSet("sha256",flag.ExitOnError)
sha256cmd.Usage = func() {
cmdUsage(sha256cmd, "Usage: qx sha256 [hexstring]\n")
}
blake2b256cmd := flag.NewFlagSet("blake2b256",flag.ExitOnError)
blake2b256cmd.Usage = func() {
cmdUsage(blake2b256cmd, "Usage: qx blak2b256 [hexstring]\n")
}
blake2b512cmd := flag.NewFlagSet("blake2b512",flag.ExitOnError)
blake2b512cmd.Usage = func() {
cmdUsage(blake2b512cmd, "Usage: qx blak2b512 [hexstring]\n")
}
blake256cmd := flag.NewFlagSet("blake256",flag.ExitOnError)
blake256cmd.Usage = func() {
cmdUsage(blake256cmd, "Usage: qx blake256 [hexstring]\n")
}
sha3_256cmd := flag.NewFlagSet("sha3-256",flag.ExitOnError)
sha3_256cmd.Usage = func() {
cmdUsage(sha3_256cmd, "Usage: qx sha3-256 [hexstring]\n")
}
keccak256cmd := flag.NewFlagSet("keccak-256",flag.ExitOnError)
keccak256cmd.Usage = func() {
cmdUsage(keccak256cmd, "Usage: qx keccak-256 [hexstring]\n")
}
ripemd160Cmd := flag.NewFlagSet("ripemd160",flag.ExitOnError)
ripemd160Cmd.Usage = func() {
cmdUsage(ripemd160Cmd, "Usage: qx ripemd160 [hexstring]\n")
}
bitcion160Cmd := flag.NewFlagSet("bitcoin160",flag.ExitOnError)
bitcion160Cmd.Usage = func() {
cmdUsage(bitcion160Cmd, "Usage: qx bitcoin160 [hexstring]\n")
}
hash160Cmd := flag.NewFlagSet("hash160",flag.ExitOnError)
hash160Cmd.Usage = func() {
cmdUsage(bitcion160Cmd, "Usage: qx hash160 [hexstring]\n")
}
// ----------------------------
// cmd for crypto
// ----------------------------
// Entropy (Seed)
entropyCmd := flag.NewFlagSet("entropy",flag.ExitOnError)
entropyCmd.Usage = func() {
cmdUsage(entropyCmd, "Usage: qx entropy [-s size] \n")
}
entropyCmd.UintVar(&seedSize,"s",seed.DefaultSeedBytes*8,"The length in bits for a seed (entropy)")
// HD (BIP32)
hdNewCmd := flag.NewFlagSet("hd-new",flag.ExitOnError)
hdNewCmd.Usage = func() {
cmdUsage(hdNewCmd, "Usage: qx hd-new [-v version] [entropy] \n")
}
hdVer.Set("testnet")
hdNewCmd.Var(&hdVer, "v","The HD(BIP32) `version` [mainnet|testnet|privnet|bip32]")
hdToPubCmd := flag.NewFlagSet("hd-to-public",flag.ExitOnError)
hdToPubCmd.Usage = func() {
cmdUsage(hdToPubCmd, "Usage: qx hd-to-public [hd_private_key] \n")
}
hdToPubCmd.Var(&hdVer, "v","The HD(BIP32) `version` [mainnet|testnet|privnet|bip32]")
hdToEcCmd := flag.NewFlagSet("hd-to-ec",flag.ExitOnError)
hdToEcCmd.Usage = func() {
cmdUsage(hdToEcCmd, "Usage: qx hd-to-ec [hd_private_key or hd_public_key] \n")
}
hdToEcCmd.Var(&hdVer, "v","The HD(BIP32) `version` [mainnet|testnet|privnet|bip32]")
hdDecodeCmd := flag.NewFlagSet("hd-decode",flag.ExitOnError)
hdDecodeCmd.Usage = func() {
cmdUsage(hdDecodeCmd, "Usage: qx hd-decode [hd_private_key or hd_public_key] \n")
}
hdDeriveCmd := flag.NewFlagSet("hd-derive",flag.ExitOnError)
hdDeriveCmd.Usage = func() {
cmdUsage(hdDeriveCmd, "Usage: qx hd-derive [hd_private_key or hd_public_key] \n")
}
hdDeriveCmd.UintVar(&hdIndex,"i",0,"The HD `index`")
hdDeriveCmd.BoolVar(&hdHarden,"d",false,"create a hardened key")
derivePath = core.DerivePathFlag{Path: wallet.DerivationPath{}}
hdDeriveCmd.Var(&derivePath,"p","hd derive `path`. ex: m/44'/0'/0'/0")
hdDeriveCmd.Var(&hdVer, "v","The HD(BIP32) `version` [mainnet|testnet|privnet|bip32]")
// Mnemonic (BIP39)
mnemonicNewCmd := flag.NewFlagSet("mnemonic-new",flag.ExitOnError)
mnemonicNewCmd.Usage = func() {
cmdUsage(mnemonicNewCmd, "Usage: qx mnemonic-new [entropy] \n")
}
mnemonicToEntropyCmd := flag.NewFlagSet("mnemonic-to-entropy",flag.ExitOnError)
mnemonicToEntropyCmd.Usage = func() {
cmdUsage(mnemonicToEntropyCmd, "Usage: qx mnemonic-to-entropy [mnemonic] \n")
}
mnemonicToSeedCmd := flag.NewFlagSet("mnemonic-to-seed",flag.ExitOnError)
mnemonicToSeedCmd.Usage = func() {
cmdUsage(mnemonicToSeedCmd, "Usage: qx mnemonic-to-seed [mnemonic] \n")
}
mnemonicToSeedCmd.StringVar(&mnemoicSeedPassphrase,"p","","An optional passphrase for converting the mnemonic to a seed")
// EC
ecNewCmd := flag.NewFlagSet("ec-new",flag.ExitOnError)
ecNewCmd.Usage = func() {
cmdUsage(ecNewCmd, "Usage: qx ec-new [entropy] \n")
}
ecNewCmd.StringVar(&curve,"c","secp256k1", "the elliptic curve is using")
ecToPubCmd := flag.NewFlagSet("ec-to-public",flag.ExitOnError)
ecToPubCmd.Usage = func() {
cmdUsage(ecToPubCmd, "Usage: qx ec-to-public [ec_private_key] \n")
}
ecToPubCmd.BoolVar(&uncompressedPKFormat,"u", false,"using the uncompressed public key format")
// Wif
ecToWifCmd := flag.NewFlagSet("ec-to-wif", flag.ExitOnError)
ecToWifCmd.Usage = func() {
cmdUsage(ecToWifCmd, "Usage: qx ec-to-wif [ec_private_key] \n")
}
ecToWifCmd.BoolVar(&uncompressedPKFormat,"u", false,"using the uncompressed public key format")
wifToEcCmd := flag.NewFlagSet("wif-to-ec", flag.ExitOnError)
wifToEcCmd.Usage = func() {
cmdUsage(wifToEcCmd, "Usage: qx wif-to-ec [WIF] \n")
}
wifToPubCmd:= flag.NewFlagSet("wif-to-public", flag.ExitOnError)
wifToPubCmd.Usage = func() {
cmdUsage(wifToPubCmd, "Usage: qx wif-to-public [WIF] \n")
}
wifToPubCmd.BoolVar(&uncompressedPKFormat,"u", false,"using the uncompressed public key format")
// Address
ecToAddrCmd := flag.NewFlagSet("ec-to-addr",flag.ExitOnError)
ecToAddrCmd.Usage = func() {
cmdUsage(ecToAddrCmd, "Usage: qx ec-to-addr [ec_public_key] \n")
}
ecToAddrCmd.Var(&base58checkVersion, "v","base58check `version` [mainnet|testnet|privnet]")
// Transaction
txDecodeCmd := flag.NewFlagSet("tx-decode",flag.ExitOnError)
txDecodeCmd.Usage = func() {
cmdUsage(txDecodeCmd, "Usage: qx tx-decode [base16_string] \n")
}
txDecodeCmd.StringVar(&network,"n","privnet", "decode rawtx for the target network. (mainnet, testnet, privnet)")
txEncodeCmd := flag.NewFlagSet("tx-encode",flag.ExitOnError)
txEncodeCmd.Usage = func() {
cmdUsage(txEncodeCmd, "Usage: qx tx-encode [-i tx-input] [-l tx-lock-time] [-o tx-output] [-v tx-version] \n")
}
txVersion = core.TxVersionFlag(TX_VERION) //set default tx version
txEncodeCmd.Var(&txVersion,"v","the transaction version")
txEncodeCmd.Var(&txLockTime,"l","the transaction lock time")
txEncodeCmd.Var(&txInputs,"i",`The set of transaction input points encoded as TXHASH:INDEX:SEQUENCE.
TXHASH is a Base16 transaction hash. INDEX is the 32 bit input index
in the context of the transaction. SEQUENCE is the optional 32 bit
input sequence and defaults to the maximum value.`)
txEncodeCmd.Var(&txOutputs,"o",`The set of transaction output data encoded as TARGET:MEER.
TARGET is an address (pay-to-pubkey-hash or pay-to-script-hash).
MEER is the 64 bit spend amount in qitmeer.`)
txSignCmd := flag.NewFlagSet("tx-sign",flag.ExitOnError)
txSignCmd.Usage = func() {
cmdUsage(txSignCmd, "Usage: qx tx-sign [raw_tx_base16_string] \n")
}
txSignCmd.StringVar(&privateKey,"k","", "the ec private key to sign the raw transaction")
msgSignCmd := flag.NewFlagSet("msg-sign",flag.ExitOnError)
msgSignCmd.Usage = func() {
cmdUsage(msgSignCmd, "Usage: msg-sign [wif] [message] \n")
}
msgSignCmd.StringVar(&msgSignatureMode, "m","qx", "the msg signature mode")
msgSignCmd.BoolVar(&showDetails,"d",false, "show signature details")
msgVerifyCmd := flag.NewFlagSet("msg-verify",flag.ExitOnError)
msgVerifyCmd.Usage = func() {
cmdUsage(msgVerifyCmd, "Usage: msg-verify [addr] [signature] [message] \n")
}
msgVerifyCmd.StringVar(&msgSignatureMode, "m","qx", "the msg signature mode")
flagSet :=[]*flag.FlagSet{
base58CheckEncodeCommand,
base58CheckDecodeCommand,
base58EncodeCmd,
base58DecodeCmd,
base64EncodeCmd,
base64DecodeCmd,
rlpEncodeCmd,
rlpDecodeCmd,
sha256cmd,
blake2b256cmd,
blake2b512cmd,
blake256cmd,
sha3_256cmd,
keccak256cmd,
ripemd160Cmd,
bitcion160Cmd,
hash160Cmd,
entropyCmd,
hdNewCmd,
hdToPubCmd,
hdToEcCmd,
hdDecodeCmd,
hdDeriveCmd,
mnemonicNewCmd,
mnemonicToEntropyCmd,
mnemonicToSeedCmd,
ecNewCmd,
ecToPubCmd,
ecToWifCmd,
wifToEcCmd,
wifToPubCmd,
ecToAddrCmd,
txEncodeCmd,
txDecodeCmd,
txSignCmd,
msgSignCmd,
msgVerifyCmd,
}
if len(os.Args) == 1 {
usage()
}
switch os.Args[1]{
case "help","--help" :
usage()
case "version","--version":
version()
default:
valid := false
for _, cmd := range flagSet{
if os.Args[1] == cmd.Name() {
cmd.Parse(os.Args[2:])
valid = true
break
}
}
if !valid {
invalid := os.Args[1]
if invalid[0] == '-' {
fmt.Fprintf(os.Stderr, "unknown option: %q \n", invalid)
} else {
fmt.Fprintf(os.Stderr, "%q is not valid command\n", invalid)
}
os.Exit(1)
}
}
// Handle base58check-encode
if base58CheckEncodeCommand.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
base58CheckEncodeCommand.Usage()
}else{
core.Base58CheckEncode(base58checkVersion.Ver,base58checkMode,base58checkHasher,base58checkCksumSize,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Base58CheckEncode(base58checkVersion.Ver,base58checkMode,base58checkHasher,base58checkCksumSize,str)
}
}
// Handle base58check-decode
if base58CheckDecodeCommand.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
base58CheckDecodeCommand.Usage()
}else{
core.Base58CheckDecode(base58checkMode,base58checkHasher,base58checkVersionSize,base58checkCksumSize,os.Args[len(os.Args)-1],showDetails)
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Base58CheckDecode(base58checkMode,base58checkHasher,base58checkVersionSize,base58checkCksumSize,str,showDetails)
}
}
// Handle base58-encode
if base58EncodeCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
base58EncodeCmd.Usage()
}else{
core.Base58Encode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Base58Encode(str)
}
}
// Handle base58-decode
if base58DecodeCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
base58DecodeCmd.Usage()
}else{
core.Base58Decode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Base58Decode(str)
}
}
if base64EncodeCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
base64EncodeCmd.Usage()
}else{
core.Base64Encode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Base64Encode(str)
}
}
if base64DecodeCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
base64DecodeCmd.Usage()
}else{
core.Base64Decode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Base64Decode(str)
}
}
if rlpEncodeCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
rlpEncodeCmd.Usage()
}else{
core.RlpEncode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.RlpEncode(str)
}
}
if rlpDecodeCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
rlpDecodeCmd.Usage()
}else{
core.RlpDecode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.RlpDecode(str)
}
}
if sha256cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
sha256cmd.Usage()
}else{
core.Sha256(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Sha256(str)
}
}
if blake256cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
blake256cmd.Usage()
}else{
core.Blake256(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Blake256(str)
}
}
if blake2b256cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
blake2b256cmd.Usage()
}else{
core.Blake2b256(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Blake2b256(str)
}
}
if blake2b512cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
blake2b512cmd.Usage()
}else{
core.Blake2b512(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Blake2b512(str)
}
}
if sha3_256cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
sha3_256cmd.Usage()
}else{
core.Sha3_256(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Sha3_256(str)
}
}
if keccak256cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
keccak256cmd.Usage()
}else{
core.Keccak256(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Keccak256(str)
}
}
if ripemd160Cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
ripemd160Cmd.Usage()
}else{
core.Ripemd160(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Ripemd160(str)
}
}
if bitcion160Cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
bitcion160Cmd.Usage()
}else{
core.Bitcoin160(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Bitcoin160(str)
}
}
if hash160Cmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
hash160Cmd.Usage()
}else{
core.Hash160(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.Hash160(str)
}
}
if entropyCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) > 2 && (os.Args[2] == "help" || os.Args[2] == "--help" ){
entropyCmd.Usage()
}else{
if seedSize % 8 > 0 {
errExit(fmt.Errorf("seed (entropy) length must be Must be divisible by 8"))
}
core.NewEntropy(seedSize/8)
}
}else {
entropyCmd.Usage()
}
}
if hdNewCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
hdNewCmd.Usage()
}else{
core.HdNewMasterPrivateKey(hdVer.Version,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.HdNewMasterPrivateKey(hdVer.Version,str)
}
}
if hdToPubCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
hdToPubCmd.Usage()
}else{
core.HdPrivateKeyToHdPublicKey(hdVer.Version,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.HdPrivateKeyToHdPublicKey(hdVer.Version,str)
}
}
if hdToEcCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
hdToEcCmd.Usage()
}else{
core.HdKeyToEcKey(hdVer.Version,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.HdKeyToEcKey(hdVer.Version,str)
}
}
if hdDecodeCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
hdDecodeCmd.Usage()
}else{
core.HdDecode(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.HdDecode(str)
}
}
if hdDeriveCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
hdDeriveCmd.Usage()
}else{
core.HdDerive(hdHarden,uint32(hdIndex),derivePath.Path,hdVer.Version,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.HdDerive(hdHarden,uint32(hdIndex),derivePath.Path,hdVer.Version,str)
}
}
if mnemonicNewCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
mnemonicNewCmd.Usage()
}else{
core.MnemonicNew(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.MnemonicNew(str)
}
}
if mnemonicToEntropyCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
mnemonicToEntropyCmd.Usage()
}else{
core.MnemonicToEntropy(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.MnemonicToEntropy(str)
}
}
if mnemonicToSeedCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
mnemonicToSeedCmd.Usage()
}else{
core.MnemonicToSeed(mnemoicSeedPassphrase, os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.MnemonicToSeed(mnemoicSeedPassphrase,str)
}
}
if ecNewCmd.Parsed(){
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
ecNewCmd.Usage()
}else{
core.EcNew(curve,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.EcNew(curve, str)
}
}
if ecToPubCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
ecToPubCmd.Usage()
}else{
core.EcPrivateKeyToEcPublicKey(uncompressedPKFormat,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.EcPrivateKeyToEcPublicKey(uncompressedPKFormat,str)
}
}
if ecToWifCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
ecToWifCmd.Usage()
}else{
core.EcPrivateKeyToWif(uncompressedPKFormat,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.EcPrivateKeyToWif(uncompressedPKFormat,str)
}
}
if wifToEcCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
wifToEcCmd.Usage()
}else{
core.WifToEcPrivateKey(os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.WifToEcPrivateKey(str)
}
}
if wifToPubCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
wifToPubCmd.Usage()
}else{
core.WifToEcPubkey(uncompressedPKFormat,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))
core.WifToEcPubkey(uncompressedPKFormat,str)
}
}
if ecToAddrCmd.Parsed() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" {
ecToAddrCmd.Usage()
}else{
core.EcPubKeyToAddress(base58checkVersion.Ver,os.Args[len(os.Args)-1])
}
}else { //try from STDIN
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errExit(err)
}
str := strings.TrimSpace(string(src))