lazy load publicKey.

This commit is contained in:
Christopher Jeffrey 2016-02-23 19:16:39 -08:00
parent 30db808930
commit 9a35cac74f

View File

@ -33,7 +33,7 @@ function KeyPair(options) {
assert(!options.publicKey || Buffer.isBuffer(options.publicKey));
this.privateKey = options.privateKey;
this.publicKey = options.publicKey;
this._publicKey = options.publicKey;
}
KeyPair.generate = function() {
@ -48,6 +48,19 @@ KeyPair.prototype.verify = function verify(msg, sig) {
return bcoin.ec.verify(msg, sig, this.getPublicKey());
};
KeyPair.prototype.__defineGetter__('publicKey', function() {
if (!this._publicKey) {
if (!this.privateKey)
return;
this._publicKey = bcoin.ec.publicKeyCreate(
this.privateKey, this.compressed
);
}
return this._publicKey;
});
KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) {
if (!this.privateKey)
return;
@ -62,15 +75,6 @@ KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) {
};
KeyPair.prototype.getPublicKey = function getPublicKey(enc) {
if (!this.publicKey) {
if (!this.privateKey)
return;
this.publicKey = bcoin.ec.publicKeyCreate(
this.privateKey, this.compressed
);
}
if (enc === 'base58')
return utils.toBase58(this.publicKey);