diff --git a/lib/bip32.js b/lib/bip32.js index 4e84fff86..ffac16be9 100644 --- a/lib/bip32.js +++ b/lib/bip32.js @@ -8,15 +8,9 @@ var Random = require('./random'); var bn = require('./bn'); var constants = require('./constants'); -var BIP32 = function BIP32(str) { +var BIP32 = function BIP32() { if (!(this instanceof BIP32)) - return new BIP32(str); - if (str === 'testnet' || str === 'mainnet') { - this.version = constants[str].bip32privkey; - this.fromRandom(); - } - else if (str) - this.fromString(str); + return new BIP32(); } BIP32.prototype.fromRandom = function(network) { @@ -48,6 +42,8 @@ BIP32.prototype.fromString = function(str) { if (bytes !== undefined && bytes !== null) this.initFromBytes(bytes); + + return this; }; BIP32.prototype.fromSeed = function(bytes, network) { diff --git a/test/test.bip32.js b/test/test.bip32.js index 971318f1d..82643b7e7 100644 --- a/test/test.bip32.js +++ b/test/test.bip32.js @@ -43,224 +43,224 @@ describe('BIP32', function() { }); it('should initialize test vector 1 from the extended public key', function() { - var bip32 = new BIP32(vector1_m_public); + var bip32 = new BIP32().fromString(vector1_m_public); should.exist(bip32); }); it('should initialize test vector 1 from the extended private key', function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); should.exist(bip32); }); it('should get the extended public key from the extended private key for test vector 1', function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); bip32.extendedPublicKeyString().should.equal(vector1_m_public); }); it("should get m/0' ext. private key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector1_m0h_private); }); it("should get m/0' ext. public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'"); should.exist(child); child.extendedPublicKeyString().should.equal(vector1_m0h_public); }); it("should get m/0'/1 ext. private key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector1_m0h1_private); }); it("should get m/0'/1 ext. public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1"); should.exist(child); child.extendedPublicKeyString().should.equal(vector1_m0h1_public); }); it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'"); - var child_pub = new BIP32(child.extendedPublicKeyString()); + var child_pub = new BIP32().fromString(child.extendedPublicKeyString()); var child2 = child_pub.derive("m/1"); should.exist(child2); child2.extendedPublicKeyString().should.equal(vector1_m0h1_public); }); it("should get m/0'/1/2h ext. private key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector1_m0h12h_private); }); it("should get m/0'/1/2h ext. public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'"); should.exist(child); child.extendedPublicKeyString().should.equal(vector1_m0h12h_public); }); it("should get m/0'/1/2h/2 ext. private key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'/2"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector1_m0h12h2_private); }); it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'"); - var child_pub = new BIP32(child.extendedPublicKeyString()); + var child_pub = new BIP32().fromString(child.extendedPublicKeyString()); var child2 = child_pub.derive("m/2"); should.exist(child2); child2.extendedPublicKeyString().should.equal(vector1_m0h12h2_public); }); it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'/2"); should.exist(child); child.extendedPublicKeyString().should.equal(vector1_m0h12h2_public); }); it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'/2/1000000000"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector1_m0h12h21000000000_private); }); it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'/2/1000000000"); should.exist(child); child.extendedPublicKeyString().should.equal(vector1_m0h12h21000000000_public); }); it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() { - var bip32 = new BIP32(vector1_m_private); + var bip32 = new BIP32().fromString(vector1_m_private); var child = bip32.derive("m/0'/1/2'/2"); - var child_pub = new BIP32(child.extendedPublicKeyString()); + var child_pub = new BIP32().fromString(child.extendedPublicKeyString()); var child2 = child_pub.derive("m/1000000000"); should.exist(child2); child2.extendedPublicKeyString().should.equal(vector1_m0h12h21000000000_public); }); it('should initialize test vector 2 from the extended public key', function() { - var bip32 = new BIP32(vector2_m_public); + var bip32 = new BIP32().fromString(vector2_m_public); should.exist(bip32); }); it('should initialize test vector 2 from the extended private key', function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); should.exist(bip32); }); it('should get the extended public key from the extended private key for test vector 2', function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); bip32.extendedPublicKeyString().should.equal(vector2_m_public); }); it("should get m/0 ext. private key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector2_m0_private); }); it("should get m/0 ext. public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0"); should.exist(child); child.extendedPublicKeyString().should.equal(vector2_m0_public); }); it("should get m/0 ext. public key from m public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m"); - var child_pub = new BIP32(child.extendedPublicKeyString()); + var child_pub = new BIP32().fromString(child.extendedPublicKeyString()); var child2 = child_pub.derive("m/0"); should.exist(child2); child2.extendedPublicKeyString().should.equal(vector2_m0_public); }); it("should get m/0/2147483647h ext. private key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector2_m02147483647h_private); }); it("should get m/0/2147483647h ext. public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'"); should.exist(child); child.extendedPublicKeyString().should.equal(vector2_m02147483647h_public); }); it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector2_m02147483647h1_private); }); it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1"); should.exist(child); child.extendedPublicKeyString().should.equal(vector2_m02147483647h1_public); }); it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'"); - var child_pub = new BIP32(child.extendedPublicKeyString()); + var child_pub = new BIP32().fromString(child.extendedPublicKeyString()); var child2 = child_pub.derive("m/1"); should.exist(child2); child2.extendedPublicKeyString().should.equal(vector2_m02147483647h1_public); }); it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1/2147483646'"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector2_m02147483647h12147483646h_private); }); it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1/2147483646'"); should.exist(child); child.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h_public); }); it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1/2147483646'/2"); should.exist(child); child.extendedPrivateKeyString().should.equal(vector2_m02147483647h12147483646h2_private); }); it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1/2147483646'/2"); should.exist(child); child.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public); }); it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() { - var bip32 = new BIP32(vector2_m_private); + var bip32 = new BIP32().fromString(vector2_m_private); var child = bip32.derive("m/0/2147483647'/1/2147483646'"); - var child_pub = new BIP32(child.extendedPublicKeyString()); + var child_pub = new BIP32().fromString(child.extendedPublicKeyString()); var child2 = child_pub.derive("m/2"); should.exist(child2); child2.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public); @@ -289,7 +289,7 @@ describe('BIP32', function() { it('should initialize a new BIP32 correctly from a random BIP32', function() { var b1 = new BIP32(); b1.fromRandom('testnet'); - var b2 = new BIP32(b1.extendedPublicKeyString()); + var b2 = new BIP32().fromString(b1.extendedPublicKeyString()); b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString()); }); @@ -311,7 +311,7 @@ describe('BIP32', function() { }); it('should return an xpub string', function() { - var bip32b = new BIP32(bip32.extendedPublicKeyString()); + var bip32b = new BIP32().fromString(bip32.extendedPublicKeyString()); bip32b.toString().slice(0, 4).should.equal('xpub'); }); @@ -320,7 +320,7 @@ describe('BIP32', function() { }); it('should return a tpub string', function() { - var tip32b = new BIP32(tip32.extendedPublicKeyString()); + var tip32b = new BIP32().fromString(tip32.extendedPublicKeyString()); tip32b.toString().slice(0, 4).should.equal('tpub'); });