@@ -6,6 +6,11 @@ const utils = @import("utils.zig");
6
6
const script = @import ("script.zig" );
7
7
const tx = @import ("tx.zig" );
8
8
const deriveP2WPKHAddress = @import ("address.zig" ).deriveP2WPKHAddress ;
9
+ const Network = @import ("const.zig" ).Network ;
10
+
11
+ fn showHelp () void {
12
+ std .debug .print ("Valid commands:\n scriptdecode\n txdecode\n epknew\n epktopublic\n hdderivation\n addr\n For more information use wbx <cmd> help" , .{});
13
+ }
9
14
10
15
pub fn main () ! void {
11
16
var gpa = std .heap .GeneralPurposeAllocator (.{}){};
@@ -16,7 +21,7 @@ pub fn main() !void {
16
21
txdecode ,
17
22
epknew ,
18
23
epktopublic ,
19
- hdderivation ,
24
+ derivation ,
20
25
addr ,
21
26
};
22
27
@@ -25,15 +30,23 @@ pub fn main() !void {
25
30
return ;
26
31
};
27
32
defer std .process .argsFree (allocator , args );
33
+ if (args .len < 2 ) {
34
+ showHelp ();
35
+ return ;
36
+ }
28
37
29
38
const cmd = std .meta .stringToEnum (Commands , args [1 ]);
30
39
if (cmd == null ) {
31
- std . debug . print ( "Invalid argument \n " , .{} );
40
+ showHelp ( );
32
41
return ;
33
42
}
34
43
35
44
switch (cmd .? ) {
36
45
.scriptdecode = > {
46
+ if (args .len < 3 or std .mem .eql (u8 , args [2 ], "help" )) {
47
+ std .debug .print ("Decode hex script\n wbx scriptdecode <hex script>\n " , .{});
48
+ return ;
49
+ }
37
50
const s = script .Script .decode (allocator , args [2 ]) catch {
38
51
std .debug .print ("Invalid script provided\n " , .{});
39
52
return ;
@@ -43,6 +56,10 @@ pub fn main() !void {
43
56
std .debug .print ("{}\n " , .{s });
44
57
},
45
58
.txdecode = > {
59
+ if (args .len < 3 or std .mem .eql (u8 , args [2 ], "help" )) {
60
+ std .debug .print ("Decode raw transaction\n wbx txdecode <raw transaction>\n " , .{});
61
+ return ;
62
+ }
46
63
const transaction = tx .decodeRawTx (allocator , args [2 ]) catch {
47
64
std .debug .print ("Invalid rawtx provided\n " , .{});
48
65
return ;
@@ -51,16 +68,21 @@ pub fn main() !void {
51
68
std .debug .print ("{}\n " , .{transaction });
52
69
},
53
70
.epknew = > {
54
- if (args .len > 2 ) {
55
- const seed = args [2 ][0.. args [2 ].len ];
71
+ if (args .len < 3 or std .mem .eql (u8 , args [2 ], "help" )) {
72
+ std .debug .print ("Generate new private key. You can specify an existing seed or a random one will be used\n wbx epknew <mainnet/testnet> <?existing seed>\n " , .{});
73
+ return ;
74
+ }
75
+ const addr_version : bip32.SerializedPrivateKeyVersion = if (std .mem .eql (u8 , args [2 ], "mainnet" )) .segwit_mainnet else .segwit_testnet ;
76
+ if (args .len > 3 ) {
77
+ const seed = args [3 ][0.. args [3 ].len ];
56
78
const bytes : []u8 = allocator .alloc (u8 , seed .len / 2 ) catch {
57
79
std .debug .print ("Error while allocating memory" , .{});
58
80
return ;
59
81
};
60
82
defer allocator .free (bytes );
61
83
_ = try std .fmt .hexToBytes (bytes , seed );
62
84
const epk = bip32 .generateExtendedMasterPrivateKey (bytes );
63
- const addr = epk .address (.segwit_mainnet , 0 , [4 ]u8 { 0 , 0 , 0 , 0 }, 0 ) catch {
85
+ const addr = epk .address (addr_version , 0 , [4 ]u8 { 0 , 0 , 0 , 0 }, 0 ) catch {
64
86
std .debug .print ("Error while generating address" , .{});
65
87
return ;
66
88
};
@@ -84,14 +106,18 @@ pub fn main() !void {
84
106
try bip39 .mnemonicToSeed (allocator , & mnemonic , "" , & seed );
85
107
86
108
const epk = bip32 .generateExtendedMasterPrivateKey (& seed );
87
- const addr = epk .address (.segwit_mainnet , 0 , [4 ]u8 { 0 , 0 , 0 , 0 }, 0 ) catch {
109
+ const addr = epk .address (addr_version , 0 , [4 ]u8 { 0 , 0 , 0 , 0 }, 0 ) catch {
88
110
std .debug .print ("Error while generating address" , .{});
89
111
return ;
90
112
};
91
113
std .debug .print ("Master private key: {s}\n " , .{addr });
92
114
}
93
115
},
94
116
.epktopublic = > {
117
+ if (args .len < 3 or std .mem .eql (u8 , args [2 ], "help" )) {
118
+ std .debug .print ("Derive public key from a given private key in address form.\n wbx epktopublic <private key addr>\n " , .{});
119
+ return ;
120
+ }
95
121
const epk = bip32 .ExtendedPrivateKey .fromAddress (args [2 ][0.. 111].* ) catch {
96
122
std .debug .print ("Invalid extended private key address" , .{});
97
123
return ;
@@ -104,7 +130,12 @@ pub fn main() !void {
104
130
};
105
131
std .debug .print ("Compressed public key {s}\n " , .{compressed });
106
132
},
107
- .hdderivation = > {
133
+ .derivation = > {
134
+ if (args .len < 5 or std .mem .eql (u8 , args [2 ], "help" )) {
135
+ std .debug .print ("Bip32 derivation. Use ' to indicate hardened derivation.\n wbx hdderivation <private key addr> <path> <mainnet/testnet>\n " , .{});
136
+ return ;
137
+ }
138
+ const addr_version : bip32.SerializedPrivateKeyVersion = if (std .mem .eql (u8 , args [2 ], "mainnet" )) .segwit_mainnet else .segwit_testnet ;
108
139
const epk = bip32 .ExtendedPrivateKey .fromAddress (args [2 ][0.. 111].* ) catch {
109
140
std .debug .print ("Invalid extended private key address" , .{});
110
141
return ;
@@ -154,14 +185,19 @@ pub fn main() !void {
154
185
var bytes : [33 ]u8 = undefined ;
155
186
_ = try std .fmt .hexToBytes (& bytes , & compressedpublic );
156
187
const fingerprint = utils .hash160 (& bytes )[0.. 4].* ;
157
- const addr = current .address (.segwit_mainnet , depth , fingerprint , lastindex ) catch {
188
+ const addr = current .address (addr_version , depth , fingerprint , lastindex ) catch {
158
189
std .debug .print ("Error while converting to address\n " , .{});
159
190
return ;
160
191
};
161
192
std .debug .print ("private key: {s}\n " , .{strprivate });
162
193
std .debug .print ("addr: {s}\n " , .{addr });
163
194
},
164
195
.addr = > {
196
+ if (args .len < 3 or std .mem .eql (u8 , args [2 ], "help" )) {
197
+ std .debug .print ("Generate Pay To Witness Public Key Address.\n wbx addr <compressed public key> <mainnet/testnet>\n " , .{});
198
+ return ;
199
+ }
200
+ const network : Network = if (std .mem .eql (u8 , args [3 ], "mainnet" )) .mainnet else .testnet ;
165
201
const compressed = args [2 ][0.. 66].* ;
166
202
const public = bip32 .PublicKey .fromStrCompressed (compressed ) catch {
167
203
std .debug .print ("Invalid compressed public key\n " , .{});
@@ -178,7 +214,7 @@ pub fn main() !void {
178
214
};
179
215
defer s .deinit ();
180
216
181
- const addr = deriveP2WPKHAddress (allocator , s , .mainnet ) catch {
217
+ const addr = deriveP2WPKHAddress (allocator , s , network ) catch {
182
218
std .debug .print ("Error while generating address\n " , .{});
183
219
return ;
184
220
};
0 commit comments