@ -4,11 +4,11 @@ var sec = require('../src/sec')
var ecparams = sec ( "secp256k1" )
var BigInteger = require ( 'bigi' )
var HDWallet = require ( '../src/hdwallet ' )
var HDNode = require ( '../src/hdnode ' )
var fixtures = require ( './fixtures/hdwallet .json' )
var fixtures = require ( './fixtures/hdnode .json' )
describe ( 'HDWallet ' , function ( ) {
describe ( 'HDNode ' , function ( ) {
describe ( 'Constructor' , function ( ) {
var D = BigInteger . ONE
var Q = ecparams . getG ( ) . multiply ( D )
@ -16,41 +16,41 @@ describe('HDWallet', function() {
chainCode . fill ( 1 )
it ( 'calculates the publicKey from a BigInteger' , function ( ) {
var hd = new HDWallet ( D , chainCode )
var hd = new HDNode ( D , chainCode )
assert ( hd . pub . Q . equals ( Q ) )
} )
it ( 'only uses compressed points' , function ( ) {
var hd = new HDWallet ( Q , chainCode )
var hdP = new HDWallet ( D , chainCode )
var hd = new HDNode ( Q , chainCode )
var hdP = new HDNode ( D , chainCode )
assert . strictEqual ( hd . pub . compressed , true )
assert . strictEqual ( hdP . pub . compressed , true )
} )
it ( 'has a default depth/index of 0' , function ( ) {
var hd = new HDWallet ( Q , chainCode )
var hd = new HDNode ( Q , chainCode )
assert . strictEqual ( hd . depth , 0 )
assert . strictEqual ( hd . index , 0 )
} )
it ( 'defaults to the bitcoin network' , function ( ) {
var hd = new HDWallet ( Q , chainCode )
var hd = new HDNode ( Q , chainCode )
assert . equal ( hd . network , networks . bitcoin )
} )
it ( 'supports alternative networks' , function ( ) {
var hd = new HDWallet ( Q , chainCode , networks . testnet )
var hd = new HDNode ( Q , chainCode , networks . testnet )
assert . equal ( hd . network , networks . testnet )
} )
it ( 'throws an exception when an unknown network is given' , function ( ) {
assert . throws ( function ( ) {
new HDWallet ( D , chainCode , { } )
new HDNode ( D , chainCode , { } )
} , /Unknown BIP32 constants for network/ )
} )
} )
@ -58,7 +58,7 @@ describe('HDWallet', function() {
describe ( 'fromSeed*' , function ( ) {
fixtures . valid . forEach ( function ( f ) {
it ( 'calculates privKey and chainCode for ' + f . master . fingerprint , function ( ) {
var hd = HDWallet . fromSeedHex ( f . master . seed )
var hd = HDNode . fromSeedHex ( f . master . seed )
assert . equal ( hd . priv . toWIF ( ) , f . master . wif )
assert . equal ( hd . chainCode . toString ( 'hex' ) , f . master . chainCode )
@ -69,7 +69,7 @@ describe('HDWallet', function() {
describe ( 'toBase58' , function ( ) {
fixtures . valid . forEach ( function ( f ) {
it ( 'exports ' + f . master . base58 + ' (public) correctly' , function ( ) {
var hd = HDWallet . fromSeedHex ( f . master . seed )
var hd = HDNode . fromSeedHex ( f . master . seed )
assert . equal ( hd . toBase58 ( false ) , f . master . base58 )
} )
@ -77,14 +77,14 @@ describe('HDWallet', function() {
fixtures . valid . forEach ( function ( f ) {
it ( 'exports ' + f . master . base58Priv + ' (private) correctly' , function ( ) {
var hd = HDWallet . fromSeedHex ( f . master . seed )
var hd = HDNode . fromSeedHex ( f . master . seed )
assert . equal ( hd . toBase58 ( true ) , f . master . base58Priv )
} )
} )
it ( 'fails when there is no private key' , function ( ) {
var hd = HDWallet . fromBase58 ( fixtures . valid [ 0 ] . master . base58 )
var hd = HDNode . fromBase58 ( fixtures . valid [ 0 ] . master . base58 )
assert . throws ( function ( ) {
hd . toBase58 ( true )
@ -95,7 +95,7 @@ describe('HDWallet', function() {
describe ( 'fromBase58' , function ( ) {
fixtures . valid . forEach ( function ( f ) {
it ( 'imports ' + f . master . base58 + ' (public) correctly' , function ( ) {
var hd = HDWallet . fromBase58 ( f . master . base58 )
var hd = HDNode . fromBase58 ( f . master . base58 )
assert . equal ( hd . toBase58 ( ) , f . master . base58 )
} )
@ -103,7 +103,7 @@ describe('HDWallet', function() {
fixtures . valid . forEach ( function ( f ) {
it ( 'imports ' + f . master . base58Priv + ' (private) correctly' , function ( ) {
var hd = HDWallet . fromBase58 ( f . master . base58Priv )
var hd = HDNode . fromBase58 ( f . master . base58Priv )
assert . equal ( hd . toBase58 ( ) , f . master . base58Priv )
} )
@ -112,7 +112,7 @@ describe('HDWallet', function() {
fixtures . invalid . fromBase58 . forEach ( function ( f ) {
it ( 'throws on ' + f . string , function ( ) {
assert . throws ( function ( ) {
HDWallet . fromBase58 ( f . string )
HDNode . fromBase58 ( f . string )
} , new RegExp ( f . exception ) )
} )
} )
@ -121,7 +121,7 @@ describe('HDWallet', function() {
describe ( 'fromBuffer/fromHex' , function ( ) {
fixtures . valid . forEach ( function ( f ) {
it ( 'imports ' + f . master . hex + ' (public) correctly' , function ( ) {
var hd = HDWallet . fromHex ( f . master . hex )
var hd = HDNode . fromHex ( f . master . hex )
assert . equal ( hd . toBuffer ( ) . toString ( 'hex' ) , f . master . hex )
} )
@ -129,7 +129,7 @@ describe('HDWallet', function() {
fixtures . valid . forEach ( function ( f ) {
it ( 'imports ' + f . master . hexPriv + ' (private) correctly' , function ( ) {
var hd = HDWallet . fromHex ( f . master . hexPriv )
var hd = HDNode . fromHex ( f . master . hexPriv )
assert . equal ( hd . toBuffer ( ) . toString ( 'hex' ) , f . master . hexPriv )
} )
@ -138,7 +138,7 @@ describe('HDWallet', function() {
fixtures . invalid . fromBuffer . forEach ( function ( f ) {
it ( 'throws on ' + f . string , function ( ) {
assert . throws ( function ( ) {
HDWallet . fromHex ( f . hex )
HDNode . fromHex ( f . hex )
} , new RegExp ( f . exception ) )
} )
} )
@ -147,7 +147,7 @@ describe('HDWallet', function() {
describe ( 'toBuffer/toHex' , function ( ) {
fixtures . valid . forEach ( function ( f ) {
it ( 'exports ' + f . master . hex + ' (public) correctly' , function ( ) {
var hd = HDWallet . fromSeedHex ( f . master . seed )
var hd = HDNode . fromSeedHex ( f . master . seed )
assert . equal ( hd . toHex ( false ) , f . master . hex )
} )
@ -155,14 +155,14 @@ describe('HDWallet', function() {
fixtures . valid . forEach ( function ( f ) {
it ( 'exports ' + f . master . hexPriv + ' (private) correctly' , function ( ) {
var hd = HDWallet . fromSeedHex ( f . master . seed )
var hd = HDNode . fromSeedHex ( f . master . seed )
assert . equal ( hd . toHex ( true ) , f . master . hexPriv )
} )
} )
it ( 'fails when there is no private key' , function ( ) {
var hd = HDWallet . fromHex ( fixtures . valid [ 0 ] . master . hex )
var hd = HDNode . fromHex ( fixtures . valid [ 0 ] . master . hex )
assert . throws ( function ( ) {
hd . toHex ( true )
@ -174,7 +174,7 @@ describe('HDWallet', function() {
var f = fixtures . valid [ 0 ]
it ( 'returns the identifier for ' + f . master . fingerprint , function ( ) {
var hd = HDWallet . fromBase58 ( f . master . base58 )
var hd = HDNode . fromBase58 ( f . master . base58 )
assert . equal ( hd . getIdentifier ( ) . toString ( 'hex' ) , f . master . identifier )
} )
@ -184,7 +184,7 @@ describe('HDWallet', function() {
var f = fixtures . valid [ 0 ]
it ( 'returns the fingerprint for ' + f . master . fingerprint , function ( ) {
var hd = HDWallet . fromBase58 ( f . master . base58 )
var hd = HDNode . fromBase58 ( f . master . base58 )
assert . equal ( hd . getFingerprint ( ) . toString ( 'hex' ) , f . master . fingerprint )
} )
@ -194,13 +194,13 @@ describe('HDWallet', function() {
var f = fixtures . valid [ 0 ]
it ( 'returns the Address (pubKeyHash) for ' + f . master . fingerprint , function ( ) {
var hd = HDWallet . fromBase58 ( f . master . base58 )
var hd = HDNode . fromBase58 ( f . master . base58 )
assert . equal ( hd . getAddress ( ) . toString ( ) , f . master . address )
} )
it ( 'supports alternative networks' , function ( ) {
var hd = HDWallet . fromBase58 ( f . master . base58 )
var hd = HDNode . fromBase58 ( f . master . base58 )
hd . network = networks . testnet
assert . equal ( hd . getAddress ( ) . version , networks . testnet . pubKeyHash )
@ -215,14 +215,14 @@ describe('HDWallet', function() {
assert . equal ( hd . depth , depth || 0 )
if ( v . hardened ) {
assert . equal ( hd . index , v . m + HDWallet . HIGHEST_BIT )
assert . equal ( hd . index , v . m + HDNode . HIGHEST_BIT )
} else {
assert . equal ( hd . index , v . m )
}
}
fixtures . valid . forEach ( function ( f , j ) {
var hd = HDWallet . fromSeedHex ( f . master . seed )
var hd = HDNode . fromSeedHex ( f . master . seed )
f . children . forEach ( function ( c , i ) {
it ( c . description + ' from ' + f . master . fingerprint , function ( ) {
@ -242,8 +242,8 @@ describe('HDWallet', function() {
var f = fixtures . valid [ 1 ]
var c = f . children [ 0 ]
var parent = HDWallet . fromBase58 ( f . master . base58 )
var child = parent . derive ( c . m )
var parentNode = HDNode . fromBase58 ( f . master . base58 )
var child = parentNode . derive ( c . m )
assert . equal ( child . toBase58 ( ) , c . base58 )
} )
@ -252,10 +252,10 @@ describe('HDWallet', function() {
var f = fixtures . valid [ 1 ]
var c = f . children [ 0 ]
var parent = HDWallet . fromBase58 ( f . master . base58 )
var parentNode = HDNode . fromBase58 ( f . master . base58 )
assert . throws ( function ( ) {
parent . deriveHardened ( c . m )
parentNode . deriveHardened ( c . m )
} , /Could not derive hardened child key/ )
} )
} )