diff --git a/lib/bip32.js b/lib/bip32.js index 3264e76..a7f03a6 100644 --- a/lib/bip32.js +++ b/lib/bip32.js @@ -8,9 +8,13 @@ var Random = require('./random'); var bn = require('./bn'); var constants = require('./constants'); -var BIP32 = function BIP32() { +var BIP32 = function BIP32(obj) { if (!(this instanceof BIP32)) - return new BIP32(); + return new BIP32(obj); + if (typeof obj === 'string') { + var str = obj; + this.fromString(str); + } } BIP32.prototype.fromRandom = function(networkstr) { diff --git a/test/bip32.js b/test/bip32.js index 96f0d33..72e4fa9 100644 --- a/test/bip32.js +++ b/test/bip32.js @@ -32,14 +32,14 @@ describe('BIP32', function() { var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt'; var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j'; - - it('should initialize the class', function() { - should.exist(BIP32); - }); - - it('should create a bip32', function() { - var bip32 = new BIP32(); + it('should make a new a bip32', function() { + var bip32; + bip32 = new BIP32(); + should.exist(bip32); + bip32 = BIP32(); should.exist(bip32); + new BIP32(vector1_m_private).toString().should.equal(vector1_m_private); + BIP32(vector1_m_private).toString().should.equal(vector1_m_private); }); it('should initialize test vector 1 from the extended public key', function() { @@ -266,6 +266,21 @@ describe('BIP32', function() { child2.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public); }); + describe('testnet', function() { + it('should initialize a new BIP32 correctly from a random BIP32', function() { + var b1 = new BIP32(); + b1.fromRandom('testnet'); + var b2 = new BIP32().fromString(b1.extendedPublicKeyString()); + b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString()); + }); + + it('should generate valid ext pub key for testnet', function() { + var b = new BIP32(); + b.fromRandom('testnet'); + b.extendedPublicKeyString().substring(0,4).should.equal('tpub'); + }); + }); + describe('#seed', function() { it('should initialize a new BIP32 correctly from test vector 1 seed', function() { @@ -285,21 +300,6 @@ describe('BIP32', function() { }); }); - describe('testnet', function() { - it('should initialize a new BIP32 correctly from a random BIP32', function() { - var b1 = new BIP32(); - b1.fromRandom('testnet'); - var b2 = new BIP32().fromString(b1.extendedPublicKeyString()); - b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString()); - }); - - it('should generate valid ext pub key for testnet', function() { - var b = new BIP32(); - b.fromRandom('testnet'); - b.extendedPublicKeyString().substring(0,4).should.equal('tpub'); - }); - }); - describe('#toString', function() { var bip32 = new BIP32(); bip32.fromRandom('mainnet');