From 7da4c7925a0cd8006eb7f47143d47d42a03ef8ac Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 16 Mar 2015 12:55:49 -0300 Subject: [PATCH 1/3] lazy calc for xpubkey in HDPrivateKey --- lib/hdprivatekey.js | 29 ++++++++++++++++++++++------- test/hdkeys.js | 5 +++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index dbf842d90..a5e7b1e63 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -423,14 +423,29 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { fingerPrint: fingerPrint }); - var HDPublicKey = require('./hdpublickey'); - var hdPublicKey = new HDPublicKey(this); - - JSUtil.defineImmutable(this, { - hdPublicKey: hdPublicKey, - xpubkey: hdPublicKey.xpubkey + this._hdPublicKey = null; + Object.defineProperty(this, 'hdPublicKey', { + configurable: false, + enumerable: true, + get: function() { + if (!this._hdPublicKey) { + var HDPublicKey = require('./hdpublickey'); + this._hdPublicKey = new HDPublicKey(this); + } + return this._hdPublicKey; + } + }); + Object.defineProperty(this, 'xpubkey', { + configurable: false, + enumerable: true, + get: function() { + if (!this._hdPublicKey) { + var HDPublicKey = require('./hdpublickey'); + this._hdPublicKey = new HDPublicKey(this); + } + return this._hdPublicKey.xpubkey; + } }); - return this; }; diff --git a/test/hdkeys.js b/test/hdkeys.js index 36900a0d5..2ea1d5185 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -76,6 +76,11 @@ describe('BIP32 compliance', function() { publicKey.toString().should.equal(publicKey.xpubkey); }); + it('cache for xpubkey works for test vector 1', function() { + var pk = HDPrivateKey(vector1_m_private); + pk.xpubkey.should.equal(pk.xpubkey); + }); + it('should get the extended public key from the extended private key for test vector 1', function() { HDPrivateKey(vector1_m_private).xpubkey.should.equal(vector1_m_public); }); From 21266570d49851b9c40577fb4c7f5322967c2d8f Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 16 Mar 2015 13:01:00 -0300 Subject: [PATCH 2/3] refactor code --- lib/hdprivatekey.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index a5e7b1e63..08052efd3 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -365,6 +365,15 @@ HDPrivateKey.fromSeed = function(hexa, network) { }); }; + + +HDPrivateKey.prototype._calcHDPublicKey = function() { + if (!this._hdPublicKey) { + var HDPublicKey = require('./hdpublickey'); + this._hdPublicKey = new HDPublicKey(this); + } +}; + /** * Receives a object with buffers in all the properties and populates the * internal structure @@ -424,14 +433,12 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { }); this._hdPublicKey = null; + Object.defineProperty(this, 'hdPublicKey', { configurable: false, enumerable: true, get: function() { - if (!this._hdPublicKey) { - var HDPublicKey = require('./hdpublickey'); - this._hdPublicKey = new HDPublicKey(this); - } + this._calcHDPublicKey(); return this._hdPublicKey; } }); @@ -439,10 +446,7 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { configurable: false, enumerable: true, get: function() { - if (!this._hdPublicKey) { - var HDPublicKey = require('./hdpublickey'); - this._hdPublicKey = new HDPublicKey(this); - } + this._calcHDPublicKey(); return this._hdPublicKey.xpubkey; } }); From 56c1e8c00087ce371c49a68d040ba2c331d3fcbe Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Mar 2015 16:01:05 -0300 Subject: [PATCH 3/3] fix test for hdkey cache --- test/hdkeys.js | 5 ----- test/hdprivatekey.js | 8 ++++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/hdkeys.js b/test/hdkeys.js index 2ea1d5185..36900a0d5 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -76,11 +76,6 @@ describe('BIP32 compliance', function() { publicKey.toString().should.equal(publicKey.xpubkey); }); - it('cache for xpubkey works for test vector 1', function() { - var pk = HDPrivateKey(vector1_m_private); - pk.xpubkey.should.equal(pk.xpubkey); - }); - it('should get the extended public key from the extended private key for test vector 1', function() { HDPrivateKey(vector1_m_private).xpubkey.should.equal(vector1_m_public); }); diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index bd54bbc81..55782f455 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -100,6 +100,14 @@ describe('HDPrivate key interface', function() { testnetKey.publicKey.network.should.equal(Networks.testnet); livenetKey.publicKey.network.should.equal(Networks.livenet); }); + + it('cache for xpubkey works', function() { + var privateKey = new HDPrivateKey(xprivkey); + should.not.exist(privateKey._hdPublicKey); + privateKey.xpubkey.should.equal(privateKey.xpubkey); + should.exist(privateKey._hdPublicKey); + }); + }); it('inspect() displays correctly', function() {