From 9a35cac74f7a876ce43567019786216c540f53ad Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 23 Feb 2016 19:16:39 -0800 Subject: [PATCH] lazy load publicKey. --- lib/bcoin/keypair.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/bcoin/keypair.js b/lib/bcoin/keypair.js index 2456caf8..efa51da7 100644 --- a/lib/bcoin/keypair.js +++ b/lib/bcoin/keypair.js @@ -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);