-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01_wallet_creation.zig
More file actions
164 lines (130 loc) Β· 6.67 KB
/
Copy path01_wallet_creation.zig
File metadata and controls
164 lines (130 loc) Β· 6.67 KB
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
/// Example: Wallet Creation and Management
/// This example demonstrates how to:
/// - Create a new random wallet
/// - Import a wallet from private key
/// - Export private keys
/// - Generate and use mnemonic phrases
/// - Create HD wallets
/// - Encrypt and store wallets
const std = @import("std");
const zigeth = @import("zigeth");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.debug.print("\nπ Zigeth Wallet Creation Examples\n", .{});
std.debug.print("βββββββββββββββββββββββββββββββββββ\n\n", .{});
// Example 1: Generate a new random wallet
std.debug.print("Example 1: Generate Random Wallet\n", .{});
std.debug.print("βββββββββββββββββββββββββββββββββ\n", .{});
{
var wallet = try zigeth.signer.Wallet.generate(allocator);
const address = try wallet.getAddress();
const addr_hex = try address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print("β
Generated new wallet\n", .{});
std.debug.print(" Address: {s}\n\n", .{addr_hex});
}
// Example 2: Import wallet from private key hex
std.debug.print("Example 2: Import from Private Key\n", .{});
std.debug.print("βββββββββββββββββββββββββββββββββββ\n", .{});
{
const private_key_hex = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
var wallet = try zigeth.signer.Wallet.fromPrivateKeyHex(allocator, private_key_hex);
const address = try wallet.getAddress();
const addr_hex = try address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print("β
Imported wallet from private key\n", .{});
std.debug.print(" Address: {s}\n\n", .{addr_hex});
}
// Example 3: Export private key
std.debug.print("Example 3: Export Private Key\n", .{});
std.debug.print("ββββββββββββββββββββββββββββββ\n", .{});
{
const private_key = try zigeth.crypto.PrivateKey.fromBytes([_]u8{0x42} ** 32);
const wallet = try zigeth.signer.Wallet.init(allocator, private_key);
const exported_key = try wallet.exportPrivateKey();
defer allocator.free(exported_key);
std.debug.print("β
Exported private key\n", .{});
std.debug.print(" Private Key: {s}\n", .{exported_key});
std.debug.print(" β οΈ Keep this secret!\n\n", .{});
}
// Example 4: Create wallet from mnemonic phrase
std.debug.print("Example 4: Mnemonic Phrase (BIP-39)\n", .{});
std.debug.print("ββββββββββββββββββββββββββββββββββββ\n", .{});
{
// In production, use a real BIP-39 phrase
const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
var mnemonic = try zigeth.signer.Mnemonic.fromPhrase(allocator, phrase);
defer mnemonic.deinit();
// Convert to seed with optional passphrase
const seed = try mnemonic.toSeed(""); // Empty passphrase
defer allocator.free(seed);
std.debug.print("β
Created mnemonic wallet\n", .{});
std.debug.print(" Phrase: {s}\n", .{phrase});
std.debug.print(" Seed length: {} bytes\n\n", .{seed.len});
}
// Example 5: HD Wallet - Multiple accounts from one seed
std.debug.print("Example 5: HD Wallet (BIP-32/BIP-44)\n", .{});
std.debug.print("βββββββββββββββββββββββββββββββββββββ\n", .{});
{
// Generate seed (in production, use mnemonic)
const seed = [_]u8{0xAB} ** 64;
const hd_wallet = try zigeth.signer.HDWallet.fromSeed(allocator, &seed);
std.debug.print("β
Created HD wallet\n", .{});
// Derive multiple accounts
var i: u32 = 0;
while (i < 3) : (i += 1) {
var account_wallet = try hd_wallet.getWallet(i);
const address = try account_wallet.getAddress();
const addr_hex = try address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print(" Account {d}: {s}\n", .{ i, addr_hex });
}
std.debug.print("\n", .{});
}
// Example 6: Encrypted Keystore (JSON V3)
std.debug.print("Example 6: Encrypted Keystore\n", .{});
std.debug.print("ββββββββββββββββββββββββββββββ\n", .{});
{
const private_key = try zigeth.crypto.PrivateKey.fromBytes([_]u8{0x99} ** 32);
const password = "SecurePassword123!";
// Encrypt and create keystore
var keystore = try zigeth.signer.Keystore.encrypt(
allocator,
private_key,
password,
.pbkdf2, // or .scrypt
);
defer keystore.deinit();
std.debug.print("β
Created encrypted keystore\n", .{});
std.debug.print(" KDF: PBKDF2\n", .{});
std.debug.print(" Cipher: AES-128-CTR\n", .{});
// Export to JSON
const json = try keystore.toJSON();
defer allocator.free(json);
std.debug.print(" JSON length: {} bytes\n", .{json.len});
// Decrypt and recover wallet
var recovered_wallet = try keystore.toWallet(password);
const address = try recovered_wallet.getAddress();
const addr_hex = try address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print(" Recovered address: {s}\n", .{addr_hex});
std.debug.print(" β
Password verified!\n\n", .{});
}
// Example 7: Sign a message
std.debug.print("Example 7: Message Signing\n", .{});
std.debug.print("ββββββββββββββββββββββββββ\n", .{});
{
const private_key = try zigeth.crypto.PrivateKey.fromBytes([_]u8{0x88} ** 32);
var wallet = try zigeth.signer.Wallet.init(allocator, private_key);
const message = "Hello, Ethereum!";
const signature = try wallet.signMessage(message);
std.debug.print("β
Signed message: \"{s}\"\n", .{message});
std.debug.print(" Signature valid: {}\n", .{signature.isValid()});
std.debug.print(" v: {}\n", .{signature.v});
std.debug.print(" r: {any}\n", .{signature.r});
std.debug.print(" s: {any}\n\n", .{signature.s});
}
std.debug.print("π All wallet examples completed!\n\n", .{});
}