lazy calc for xpubkey in HDPrivateKey

This commit is contained in:
Manuel Araoz 2015-03-16 12:55:49 -03:00
parent 1a0cb73add
commit 7da4c7925a
2 changed files with 27 additions and 7 deletions

View File

@ -423,14 +423,29 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
fingerPrint: fingerPrint fingerPrint: fingerPrint
}); });
var HDPublicKey = require('./hdpublickey'); this._hdPublicKey = null;
var hdPublicKey = new HDPublicKey(this); Object.defineProperty(this, 'hdPublicKey', {
configurable: false,
JSUtil.defineImmutable(this, { enumerable: true,
hdPublicKey: hdPublicKey, get: function() {
xpubkey: hdPublicKey.xpubkey 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; return this;
}; };

View File

@ -76,6 +76,11 @@ describe('BIP32 compliance', function() {
publicKey.toString().should.equal(publicKey.xpubkey); 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() { 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); HDPrivateKey(vector1_m_private).xpubkey.should.equal(vector1_m_public);
}); });