@@ -12,36 +12,55 @@ describe('bitcoinjs-lib (BIP32)', function () {
1212 assert . equal ( node . keyPair . toWIF ( ) , 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7' )
1313 } )
1414
15- it ( 'can create a BIP32 wallet external address' , function ( ) {
15+ it ( 'can export a BIP32 xpriv, then import it' , function ( ) {
16+ var mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
17+ var seed = bip39 . mnemonicToSeed ( mnemonic )
18+ var node = bitcoin . HDNode . fromSeedBuffer ( seed )
19+ var string = node . toBase58 ( )
20+ var restored = bitcoin . HDNode . fromBase58 ( string )
21+
22+ assert . equal ( node . getAddress ( ) , restored . getAddress ( ) ) // same public key
23+ assert . equal ( node . keyPair . toWIF ( ) , restored . keyPair . toWIF ( ) ) // same private key
24+ } )
25+
26+ it ( 'can export a BIP32 xpub' , function ( ) {
27+ var mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
28+ var seed = bip39 . mnemonicToSeed ( mnemonic )
29+ var node = bitcoin . HDNode . fromSeedBuffer ( seed )
30+ var string = node . neutered ( ) . toBase58 ( )
31+
32+ assert . equal ( string , 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n' )
33+ } )
34+
35+ it ( 'can create a BIP32, bitcoin, account 0, external address' , function ( ) {
1636 var path = "m/0'/0/0"
1737 var root = bitcoin . HDNode . fromSeedHex ( 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' )
1838
1939 var child1 = root . derivePath ( path )
2040
2141 // option 2, manually
22- var child2 = root . deriveHardened ( 0 )
42+ var child1b = root . deriveHardened ( 0 )
2343 . derive ( 0 )
2444 . derive ( 0 )
2545
2646 assert . equal ( child1 . getAddress ( ) , '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7' )
27- assert . equal ( child2 . getAddress ( ) , '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7' )
47+ assert . equal ( child1b . getAddress ( ) , '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7' )
2848 } )
2949
3050 it ( 'can create a BIP44, bitcoin, account 0, external address' , function ( ) {
31- var path = "m/44'/0'/0'/0/0"
3251 var root = bitcoin . HDNode . fromSeedHex ( 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' )
3352
34- var child1 = root . derivePath ( path )
53+ var child1 = root . derivePath ( "m/44'/0'/0'/0/0" )
3554
3655 // option 2, manually
37- var child2 = root . deriveHardened ( 44 )
56+ var child1b = root . deriveHardened ( 44 )
3857 . deriveHardened ( 0 )
3958 . deriveHardened ( 0 )
4059 . derive ( 0 )
4160 . derive ( 0 )
4261
4362 assert . equal ( child1 . getAddress ( ) , '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au' )
44- assert . equal ( child2 . getAddress ( ) , '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au' )
63+ assert . equal ( child1b . getAddress ( ) , '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au' )
4564 } )
4665
4766 it ( 'can create a BIP49, bitcoin testnet, account 0, external address' , function ( ) {
@@ -61,18 +80,20 @@ describe('bitcoinjs-lib (BIP32)', function () {
6180 assert . equal ( address , '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2' )
6281 } )
6382
64- it ( 'can use BIP39 to generate BIP32 wallet address ' , function ( ) {
83+ it ( 'can use BIP39 to generate BIP32 addresses ' , function ( ) {
6584// var mnemonic = bip39.generateMnemonic()
6685 var mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
6786 assert ( bip39 . validateMnemonic ( mnemonic ) )
6887
6988 var seed = bip39 . mnemonicToSeed ( mnemonic )
7089 var root = bitcoin . HDNode . fromSeedBuffer ( seed )
7190
72- // 1st receive address
91+ // receive addresses
7392 assert . strictEqual ( root . derivePath ( "m/0'/0/0" ) . getAddress ( ) , '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC' )
93+ assert . strictEqual ( root . derivePath ( "m/0'/0/1" ) . getAddress ( ) , '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL' )
7494
75- // 1st change address
95+ // change addresses
7696 assert . strictEqual ( root . derivePath ( "m/0'/1/0" ) . getAddress ( ) , '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn' )
97+ assert . strictEqual ( root . derivePath ( "m/0'/1/1" ) . getAddress ( ) , '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6' )
7798 } )
7899} )
0 commit comments