This repository was archived by the owner on Apr 11, 2024. It is now read-only.
forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbip32.spec.ts
More file actions
282 lines (240 loc) · 8.8 KB
/
Copy pathbip32.spec.ts
File metadata and controls
282 lines (240 loc) · 8.8 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
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
import * as assert from 'assert';
import BIP32Factory from 'bip32';
import * as ecc from 'tiny-secp256k1';
import * as bip39 from 'bip39';
import { describe, it } from 'mocha';
import * as bitcoin from '../..';
const bip32 = BIP32Factory(ecc);
function getAddress(node: any, network?: any): string {
return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address!;
}
describe('nakamotojs-lib (BIP32)', () => {
it('can import a BIP32 testnet xpriv and export to WIF', () => {
const xpriv =
'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK';
const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet);
assert.strictEqual(
node.toWIF(),
'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7',
);
});
it('can export a BIP32 xpriv, then import it', () => {
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const node = bip32.fromSeed(seed);
const strng = node.toBase58();
const restored = bip32.fromBase58(strng);
assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
});
it('can export a BIP32 xpriv, then import it using Litecoin network', () => {
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const node = bip32.fromSeed(seed, bitcoin.networks.litecoin);
const strng = node.toBase58();
const restored = bip32.fromBase58(strng, bitcoin.networks.litecoin);
assert.strictEqual(
getAddress(node, bitcoin.networks.litecoin),
getAddress(restored, bitcoin.networks.litecoin),
); // same public key
assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
});
it('can export a BIP32 xpub', () => {
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const node = bip32.fromSeed(seed);
const strng = node.neutered().toBase58();
assert.strictEqual(
strng,
'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n',
);
});
it('can export a BIP32 xpub using Litecoin network', () => {
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const node = bip32.fromSeed(seed, bitcoin.networks.litecoin);
const strng = node.neutered().toBase58();
assert.strictEqual(
strng,
'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n',
);
});
it('can create a BIP32, bitcoin, account 0, external address', () => {
const path = "m/0'/0/0";
const root = bip32.fromSeed(
Buffer.from(
'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
'hex',
),
);
const child1 = root.derivePath(path);
// option 2, manually
const child1b = root.deriveHardened(0).derive(0).derive(0);
assert.strictEqual(
getAddress(child1),
'1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7',
);
assert.strictEqual(
getAddress(child1b),
'1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7',
);
});
it('can create a BIP32, litecoin, account 0, external address', () => {
const path = "m/0'/0/0";
const root = bip32.fromSeed(
Buffer.from(
'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
'hex',
),
bitcoin.networks.litecoin,
);
const child1 = root.derivePath(path);
// option 2, manually
const child1b = root.deriveHardened(0).derive(0).derive(0);
assert.strictEqual(
getAddress(child1, bitcoin.networks.litecoin),
'LcWvSE7DcZuuK4DuqrtB1hRwJBnpLPHUuT',
);
assert.strictEqual(
getAddress(child1b, bitcoin.networks.litecoin),
'LcWvSE7DcZuuK4DuqrtB1hRwJBnpLPHUuT',
);
});
it('can create a BIP44, bitcoin, account 0, external address', () => {
const root = bip32.fromSeed(
Buffer.from(
'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
'hex',
),
);
const child1 = root.derivePath("m/44'/0'/0'/0/0");
// option 2, manually
const child1b = root
.deriveHardened(44)
.deriveHardened(0)
.deriveHardened(0)
.derive(0)
.derive(0);
assert.strictEqual(
getAddress(child1),
'12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au',
);
assert.strictEqual(
getAddress(child1b),
'12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au',
);
});
it('can create a BIP44, litecoin, account 0, external address', () => {
const root = bip32.fromSeed(
Buffer.from(
'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
'hex',
),
bitcoin.networks.litecoin,
);
const child1 = root.derivePath("m/44'/0'/0'/0/0");
// option 2, manually
const child1b = root
.deriveHardened(44)
.deriveHardened(0)
.deriveHardened(0)
.derive(0)
.derive(0);
assert.strictEqual(
getAddress(child1, bitcoin.networks.litecoin),
'LLgwC4KJCpHsuRoA7VDmMNCPAFdKBNU5Ni',
);
assert.strictEqual(
getAddress(child1b, bitcoin.networks.litecoin),
'LLgwC4KJCpHsuRoA7VDmMNCPAFdKBNU5Ni',
);
});
it('can create a BIP49, bitcoin testnet, account 0, external address', () => {
const mnemonic =
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed);
const path = "m/49'/1'/0'/0/0";
const child = root.derivePath(path);
const { address } = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({
pubkey: child.publicKey,
network: bitcoin.networks.testnet,
}),
network: bitcoin.networks.testnet,
});
assert.strictEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2');
});
it('can create a BIP49, litecoin testnet, account 0, external address', () => {
const mnemonic =
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed, bitcoin.networks.litecointestnet);
const path = "m/49'/1'/0'/0/0";
const child = root.derivePath(path);
const { address } = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({
pubkey: child.publicKey,
network: bitcoin.networks.litecointestnet,
}),
network: bitcoin.networks.litecointestnet,
});
assert.strictEqual(address, 'QRHtkDQdVvNNwrVjEdeCGviCw7Ny3SNNiA');
});
it('can use BIP39 to generate BIP32 addresses', () => {
// var mnemonic = bip39.generateMnemonic()
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
assert(bip39.validateMnemonic(mnemonic));
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed);
// receive addresses
assert.strictEqual(
getAddress(root.derivePath("m/0'/0/0")),
'1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC',
);
assert.strictEqual(
getAddress(root.derivePath("m/0'/0/1")),
'1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL',
);
// change addresses
assert.strictEqual(
getAddress(root.derivePath("m/0'/1/0")),
'1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn',
);
assert.strictEqual(
getAddress(root.derivePath("m/0'/1/1")),
'1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6',
);
});
it('can use BIP39 to generate a Litecoin BIP32 addresses', () => {
// var mnemonic = bip39.generateMnemonic()
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
assert(bip39.validateMnemonic(mnemonic));
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed, bitcoin.networks.litecoin);
// receive addresses
assert.strictEqual(
getAddress(root.derivePath("m/0'/0/0"), bitcoin.networks.litecoin),
'LUiMYoajK6KBC1nHLShQYdsMpFnqzMd59C',
);
assert.strictEqual(
getAddress(root.derivePath("m/0'/0/1"), bitcoin.networks.litecoin),
'LUr4465fJeqU3tGHCABSsmyvt4vCLcjrjW',
);
// change addresses
assert.strictEqual(
getAddress(root.derivePath("m/0'/1/0"), bitcoin.networks.litecoin),
'LMH6ahuuTLtgq7oP73CNEEKiYLTgVnwckC',
);
assert.strictEqual(
getAddress(root.derivePath("m/0'/1/1"), bitcoin.networks.litecoin),
'LYPszGxTuXkfhTYCp3xtSe85vdRsMytLkv',
);
});
});