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