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
});
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;
};

View File

@ -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);
});