forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bip32.spec.ts
151 lines (128 loc) · 4.53 KB
/
bip32.spec.ts
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
import * as assert from 'assert';
import BIP32Factory from 'bip32grs';
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('groestlcoinjs-lib (BIP32)', () => {
it('can import a BIP32 testnet xpriv and export to WIF', () => {
const xpriv =
'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWzcfJHa';
const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet);
assert.strictEqual(
node.toWIF(),
'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV74UKj4',
);
});
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 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,
'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bK8i4rc',
);
});
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),
'FnTgcvXm6QMPVrYsYptMCCAVk8hVnEzS4Y',
);
assert.strictEqual(
getAddress(child1b),
'FnTgcvXm6QMPVrYsYptMCCAVk8hVnEzS4Y',
);
});
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),
'FWdhNkjqgejN6E87pTDwXrvwcCXzkN8RTM',
);
assert.strictEqual(
getAddress(child1b),
'FWdhNkjqgejN6E87pTDwXrvwcCXzkN8RTM',
);
});
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, '2Mww8dCYPUpKHofjgcXcBCEGmniw9E6pgYR');
});
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")),
'Fef7jW1GnvkfNp7F3Qhaj8bvGChXSoUswW',
);
assert.strictEqual(
getAddress(root.derivePath("m/0'/0/1")),
'FenpEnWCnVGxEgbEu8Bd4GiVL1psohgiNG',
);
// change addresses
assert.strictEqual(
getAddress(root.derivePath("m/0'/1/0")),
'FXDrmQLSwBLB1v8Lp1CYQj4GzHNMwgbQfV',
);
assert.strictEqual(
getAddress(root.derivePath("m/0'/1/1")),
'FiLeAyP1PNC9tFsAX1y4d8reNaLYrvVQmS',
);
});
});