Merge pull request #1149 from maraoz/lazy/xpubkey
lazy calc for xpubkey in HDPrivateKey
This commit is contained in:
commit
56f3e9a7da
|
@ -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
|
* Receives a object with buffers in all the properties and populates the
|
||||||
* internal structure
|
* internal structure
|
||||||
|
@ -423,14 +432,24 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
|
||||||
fingerPrint: fingerPrint
|
fingerPrint: fingerPrint
|
||||||
});
|
});
|
||||||
|
|
||||||
var HDPublicKey = require('./hdpublickey');
|
this._hdPublicKey = null;
|
||||||
var hdPublicKey = new HDPublicKey(this);
|
|
||||||
|
|
||||||
JSUtil.defineImmutable(this, {
|
Object.defineProperty(this, 'hdPublicKey', {
|
||||||
hdPublicKey: hdPublicKey,
|
configurable: false,
|
||||||
xpubkey: hdPublicKey.xpubkey
|
enumerable: true,
|
||||||
|
get: function() {
|
||||||
|
this._calcHDPublicKey();
|
||||||
|
return this._hdPublicKey;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Object.defineProperty(this, 'xpubkey', {
|
||||||
|
configurable: false,
|
||||||
|
enumerable: true,
|
||||||
|
get: function() {
|
||||||
|
this._calcHDPublicKey();
|
||||||
|
return this._hdPublicKey.xpubkey;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -100,6 +100,14 @@ describe('HDPrivate key interface', function() {
|
||||||
testnetKey.publicKey.network.should.equal(Networks.testnet);
|
testnetKey.publicKey.network.should.equal(Networks.testnet);
|
||||||
livenetKey.publicKey.network.should.equal(Networks.livenet);
|
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() {
|
it('inspect() displays correctly', function() {
|
||||||
|
|
Loading…
Reference in New Issue