-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexamples.js
212 lines (169 loc) · 6.34 KB
/
examples.js
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
const HdAddGen = require("./hdAddressGenerator");
const mnemonic =
"brand improve symbol strike say focus ginger imitate ginger appear wheel brand swear relief zero";
const seed =
"09a0a3b58d10bbc456254f1915c2627d8f0e9968922505b39dbb08f6a5dc9dafdee40cff16aa488add7ee4e2ec6eaf40425d3f2aea81c18c2c314d58885e923a";
const xpub =
"xpub6CfhGffKu2dhKxhxVS7gmTwtfK6NKA6pVAjgTgManuJoTVqLjiN6RTydgE13GPGCShaEQk1BjbHZ7Lps7eE5ECTk48vJuYjcf2FsPN1n9av";
(async () => {
/**
* Generate cryptographically random Mnemonic and seed buffer.
* This is a pass through for the Bip39 libraries generateMnemomic function.
* Please review the bip39 implementation to make sure you are comfortable with
* the implementation before using this for anything critical.
*/
const sampleMnemonic = await HdAddGen.generateMnemonic(
"english",
128 /* 128=12words, 256=24words */
);
console.log(`\nSample Mnemonic and Seed Generation`);
console.log(`Mnemonic: ${sampleMnemonic.mnemonic}`);
console.log(`Seed: ${sampleMnemonic.seed.toString("hex")}`);
/**
* Generate BIP 44 ( Legacy '1' ) addresses for bitcoin.
*/
const bip44 = HdAddGen.withMnemonic(mnemonic, false, "BTC");
console.log(`\nBIP 44 ( Legacy '1' ) Key Generation`);
/**
* HD information and keys are available directly from the object.
*/
console.log(`BIP 44 ( Legacy '1' ) Key Generation`);
console.log(`BIP 32 Algo: ${bip44.hashAlgo}`);
console.log(`BIP 32 Path: ${bip44.bip32Path}`);
console.log(`BIP 32 Seed: ${bip44.bip32Seed}`);
console.log(`BIP 32 Root Key: ${bip44.bip32RootKey}`);
console.log(`Account X Priv Key: ${bip44.accountXprivKey}`);
console.log(`Account X Pub Key: ${bip44.accountXpubKey}`);
console.log(`BIP 32 X Priv Key: ${bip44.bip32XprivKey}`);
console.log(`BIP 32 X Pub Key: ${bip44.bip32XpubKey}\n`);
// Generate 3 address pairs starting from index 0,
const bip44Addresses = await bip44.generate(3, 0);
bip44Addresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate BIP 44 ( Legacy '1' ) addresses for bitcoin with seed instead of mnemonic.
*/
const bip44withSeed = HdAddGen.withSeed(seed, "BTC");
console.log(`\nBIP 44 ( Legacy '1' ) Key Generation with seed`);
// Generate 3 address pairs starting from index 0,
const bip44withSeedAddresses = await bip44withSeed.generate(3, 0);
bip44withSeedAddresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate BIP 44 ( Legacy '1' ) encrypted addresses.
*/
const bip44withEnc = HdAddGen.withSeed(
seed,
"BTC",
false,
44,
0,
0,
"password"
);
bip44withEnc.showEncryptProgress = true; // You can enable encryption progress to have the status logged to console.
console.log(
`\nBIP 44 ( Legacy '1' ) encrypted addresses. This can be quite slow...`
);
// Generate 3 address pairs starting from index 0,
const bip44withEncAddresses = await bip44withEnc.generate(2, 0);
bip44withEncAddresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate BIP 49 ( Segwit Compatible '3' ) addresses for bitcoin with mnemonic passphrase.
*/
const bip49 = HdAddGen.withMnemonic(mnemonic, "test", "BTC", false, 49);
console.log(`\nBIP 49 ( Segwit Compatible '3' ) Key Generation`);
// Generate 3 address pairs starting from index 0,
const bip49Addresses = await bip49.generate(3, 0);
bip49Addresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate hardened BIP 84 ( Segwit bech32 'bc1' ) addresses for bitcoin.
*/
const bip84 = HdAddGen.withMnemonic(mnemonic, false, "BTC", true, 49);
console.log(`\nBIP 84 ( Segwit bech32 'bc1' ) hardened Key Generation`);
// Generate 3 address pairs starting from index 0,
const bip84Addresses = await bip84.generate(3, 0);
bip84Addresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate BIP 32 addresses with custom path.
*/
const bip32 = HdAddGen.withMnemonicBIP32(
mnemonic,
false,
"BTC",
"m/44'/0'/0'",
false
);
console.log(`\nBIP 32 Key Generation`);
// Generate 3 address pairs starting from index 0,
const bip32Addresses = await bip32.generate(3, 0);
bip32Addresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate BIP 144 addresses with custom path and algo.
*/
const bip141 = HdAddGen.withMnemonicBIP141(
mnemonic,
false,
"BTC",
"m/44'/0'/0'",
"p2wpkh"
);
console.log(`\nBIP 141 Key Generation`);
// Generate 3 address pairs starting from index 0,
const bip141Addresses = await bip141.generate(3, 0, "m/0'/0", "p2wpkh");
bip141Addresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* Generate Neutered BIP 44 ( Legacy '1' ) addresses for bitcoin with XPUB.
*/
let bip44xpub = {};
bip44xpub = HdAddGen.withExtPub(xpub, "BTC", 44);
// Generate 3 address pairs starting from index 0,
const bip44xpubAddresses = await bip44xpub.generate(3);
bip44xpubAddresses.forEach((address) => {
console.log(address.path, address.address, address.pubKey, address.privKey);
});
/**
* BCH addresses conversion.
*/
const bip44BCH = HdAddGen.withSeed(seed, "BCH");
console.log(`\nBCH Address Conversion`);
// Generate 3 address pairs starting from index 0,
const bip44BCHAddresses = await bip44BCH.generate(3, 0);
bip44BCHAddresses.forEach((address) => {
console.log(
address.path,
bip44BCH.convertAddress(address.address, "cashAddress"),
address.pubKey,
address.privKey
);
});
/**
* Using promises
*/
const bip44promise = HdAddGen.withSeed(seed, "BTC");
// Generate 3 address pairs starting from index 0,
// eslint-disable-next-line no-shadow
bip44promise.generate(3, 0).then((bip44withSeedAddresses) => {
console.log(`\nBIP 44 ( Legacy '1' ) Key Using Promises`);
bip44withSeedAddresses.forEach((address) => {
console.log(
address.path,
address.address,
address.pubKey,
address.privKey
);
});
});
})();