diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index dbf842d..a5e7b1e 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 36900a0..2ea1d51 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); });