Skip to content

Commit 00b40e2

Browse files
committed
help wbx cli
1 parent cb26d21 commit 00b40e2

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

src/wbx.zig

+45-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const utils = @import("utils.zig");
66
const script = @import("script.zig");
77
const tx = @import("tx.zig");
88
const deriveP2WPKHAddress = @import("address.zig").deriveP2WPKHAddress;
9+
const Network = @import("const.zig").Network;
10+
11+
fn showHelp() void {
12+
std.debug.print("Valid commands:\nscriptdecode\ntxdecode\nepknew\nepktopublic\nhdderivation\naddr\nFor more information use wbx <cmd> help", .{});
13+
}
914

1015
pub fn main() !void {
1116
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@@ -16,7 +21,7 @@ pub fn main() !void {
1621
txdecode,
1722
epknew,
1823
epktopublic,
19-
hdderivation,
24+
derivation,
2025
addr,
2126
};
2227

@@ -25,15 +30,23 @@ pub fn main() !void {
2530
return;
2631
};
2732
defer std.process.argsFree(allocator, args);
33+
if (args.len < 2) {
34+
showHelp();
35+
return;
36+
}
2837

2938
const cmd = std.meta.stringToEnum(Commands, args[1]);
3039
if (cmd == null) {
31-
std.debug.print("Invalid argument\n", .{});
40+
showHelp();
3241
return;
3342
}
3443

3544
switch (cmd.?) {
3645
.scriptdecode => {
46+
if (args.len < 3 or std.mem.eql(u8, args[2], "help")) {
47+
std.debug.print("Decode hex script\nwbx scriptdecode <hex script>\n", .{});
48+
return;
49+
}
3750
const s = script.Script.decode(allocator, args[2]) catch {
3851
std.debug.print("Invalid script provided\n", .{});
3952
return;
@@ -43,6 +56,10 @@ pub fn main() !void {
4356
std.debug.print("{}\n", .{s});
4457
},
4558
.txdecode => {
59+
if (args.len < 3 or std.mem.eql(u8, args[2], "help")) {
60+
std.debug.print("Decode raw transaction\nwbx txdecode <raw transaction>\n", .{});
61+
return;
62+
}
4663
const transaction = tx.decodeRawTx(allocator, args[2]) catch {
4764
std.debug.print("Invalid rawtx provided\n", .{});
4865
return;
@@ -51,16 +68,21 @@ pub fn main() !void {
5168
std.debug.print("{}\n", .{transaction});
5269
},
5370
.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\nwbx 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];
5678
const bytes: []u8 = allocator.alloc(u8, seed.len / 2) catch {
5779
std.debug.print("Error while allocating memory", .{});
5880
return;
5981
};
6082
defer allocator.free(bytes);
6183
_ = try std.fmt.hexToBytes(bytes, seed);
6284
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 {
6486
std.debug.print("Error while generating address", .{});
6587
return;
6688
};
@@ -84,14 +106,18 @@ pub fn main() !void {
84106
try bip39.mnemonicToSeed(allocator, &mnemonic, "", &seed);
85107

86108
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 {
88110
std.debug.print("Error while generating address", .{});
89111
return;
90112
};
91113
std.debug.print("Master private key: {s}\n", .{addr});
92114
}
93115
},
94116
.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.\nwbx epktopublic <private key addr>\n", .{});
119+
return;
120+
}
95121
const epk = bip32.ExtendedPrivateKey.fromAddress(args[2][0..111].*) catch {
96122
std.debug.print("Invalid extended private key address", .{});
97123
return;
@@ -104,7 +130,12 @@ pub fn main() !void {
104130
};
105131
std.debug.print("Compressed public key {s}\n", .{compressed});
106132
},
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.\nwbx 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;
108139
const epk = bip32.ExtendedPrivateKey.fromAddress(args[2][0..111].*) catch {
109140
std.debug.print("Invalid extended private key address", .{});
110141
return;
@@ -154,14 +185,19 @@ pub fn main() !void {
154185
var bytes: [33]u8 = undefined;
155186
_ = try std.fmt.hexToBytes(&bytes, &compressedpublic);
156187
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 {
158189
std.debug.print("Error while converting to address\n", .{});
159190
return;
160191
};
161192
std.debug.print("private key: {s}\n", .{strprivate});
162193
std.debug.print("addr: {s}\n", .{addr});
163194
},
164195
.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.\nwbx 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;
165201
const compressed = args[2][0..66].*;
166202
const public = bip32.PublicKey.fromStrCompressed(compressed) catch {
167203
std.debug.print("Invalid compressed public key\n", .{});
@@ -178,7 +214,7 @@ pub fn main() !void {
178214
};
179215
defer s.deinit();
180216

181-
const addr = deriveP2WPKHAddress(allocator, s, .mainnet) catch {
217+
const addr = deriveP2WPKHAddress(allocator, s, network) catch {
182218
std.debug.print("Error while generating address\n", .{});
183219
return;
184220
};

0 commit comments

Comments
 (0)