From 7390b15f8946850367dc10dc5a1d7e7a74691f92 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 17 Sep 2014 15:11:16 -0700 Subject: [PATCH] add set function to bip32 --- lib/bip32.js | 16 ++++++++++++++++ test/bip32.js | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/bip32.js b/lib/bip32.js index a7f03a6..1046317 100644 --- a/lib/bip32.js +++ b/lib/bip32.js @@ -14,9 +14,25 @@ var BIP32 = function BIP32(obj) { if (typeof obj === 'string') { var str = obj; this.fromString(str); + } else if (obj ) { + this.set(obj); } } +BIP32.prototype.set = function(obj) { + this.version = typeof obj.version !== 'undefined' ? obj.version : this.version; + this.depth = typeof obj.depth !== 'undefined' ? obj.depth : this.depth; + this.parentFingerprint = obj.parentFingerprint || this.parentFingerprint; + this.childIndex = obj.childIndex || this.childIndex; + this.chainCode = obj.chainCode || this.chainCode; + this.key = obj.key || this.key; + this.hasPrivateKey = typeof obj.hasPrivateKey !== 'undefined' ? obj.hasPrivateKey : this.hasPrivateKey; + this.pubKeyHash = obj.pubKeyHash || this.pubKeyHash; + this.extendedPublicKey = obj.extendedPublicKey || this.extendedPublicKey; + this.extendedPrivateKey = obj.extendedPrivateKey || this.extendedPrivateKey; + return this; +}; + BIP32.prototype.fromRandom = function(networkstr) { if (!networkstr) networkstr = 'mainnet'; diff --git a/test/bip32.js b/test/bip32.js index 72e4fa9..7a87f19 100644 --- a/test/bip32.js +++ b/test/bip32.js @@ -40,6 +40,7 @@ describe('BIP32', function() { should.exist(bip32); new BIP32(vector1_m_private).toString().should.equal(vector1_m_private); BIP32(vector1_m_private).toString().should.equal(vector1_m_private); + BIP32(BIP32(vector1_m_private)).toString().should.equal(vector1_m_private); }); it('should initialize test vector 1 from the extended public key', function() { @@ -281,6 +282,24 @@ describe('BIP32', function() { }); }); + describe('#set', function() { + var bip32 = BIP32(vector1_m_private); + var bip322 = BIP32().set({ + version: bip32.version, + depth: bip32.depth, + parentFingerprint: bip32.parentFingerprint, + childIndex: bip32.childIndex, + chainCode: bip32.chainCode, + key: bip32.key, + hasPrivateKey: bip32.hasPrivateKey, + pubKeyHash: bip32.pubKeyhash, + extendedPublicKey: bip32.extendedPublicKey, + extendedPrivateKey: bip32.extendedPrivateKey + }); + bip322.toString().should.equal(bip32.toString()); + bip322.set({}).toString().should.equal(bip32.toString()); + }); + describe('#seed', function() { it('should initialize a new BIP32 correctly from test vector 1 seed', function() {