From 6cf17c2b6de976eb7fa8e687fdae28a0231bfc25 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 23 Feb 2016 03:57:21 -0800 Subject: [PATCH] some more optimization. --- lib/bcoin/hd.js | 2 +- lib/bcoin/keypair.js | 40 ++++++++++++++++++++++++++++++---------- lib/bcoin/wallet.js | 3 +-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index c70f7803..abdb8436 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -722,7 +722,7 @@ HDPrivateKey.prototype._build = function _build(data) { this.hdPrivateKey = this; this.xpubkey = this.hdPublicKey.xpubkey; - this.key = bcoin.keypair({ privateKey: this.privateKey }); + this.key = key; }; HDPrivateKey.prototype.derive = function derive(index, hardened) { diff --git a/lib/bcoin/keypair.js b/lib/bcoin/keypair.js index 19860f71..d929d0e2 100644 --- a/lib/bcoin/keypair.js +++ b/lib/bcoin/keypair.js @@ -28,18 +28,38 @@ function KeyPair(options) { if (!options.privateKey && !options.publicKey) throw new Error('No options for keypair'); - - this.key = bcoin.ecdsa.keyPair({ - priv: options.privateKey, - pub: options.publicKey - }); - - this.privatePoint = this.key.getPrivate(); - this.publicPoint = this.key.getPublic(); - this.privateKey = this.getPrivateKey(); - this.publicKey = this.getPublicKey(); } +KeyPair.prototype.__defineGetter__('key', function() { + if (!this._key) { + this._key = bcoin.ecdsa.keyPair({ + priv: this.options.privateKey, + pub: this.options.publicKey + }); + } + return this._key; +}); + +KeyPair.prototype.__defineGetter__('privatePoint', function() { + if (!this._privatePoint) + this._privatePoint = this.key.getPrivate(); + return this._privatePoint; +}); + +KeyPair.prototype.__defineGetter__('publicPoint', function() { + if (!this._publicPoint) + this._publicPoint = this.key.getPublic(); + return this._publicPoint; +}); + +KeyPair.prototype.__defineGetter__('privateKey', function() { + return this.getPrivateKey(); +}); + +KeyPair.prototype.__defineGetter__('publicKey', function() { + return this.getPublicKey(); +}); + KeyPair.generate = function() { return new KeyPair(bcoin.ec.generate()); }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 9a15c157..a454831c 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -355,8 +355,7 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) { key = this.accountKey.derive(data.path); options = { - privateKey: key.privateKey, - publicKey: key.publicKey, + key: key.key, compressed: key.compressed, change: data.change, index: data.index,