From 00eba20c4e7f7e2ae263ddd6c1311e558510c1d4 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 23 Feb 2016 13:14:58 -0800 Subject: [PATCH] hd key optimization --- lib/bcoin/hd.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index abdb8436..08ce5178 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -707,24 +707,33 @@ HDPrivateKey.prototype._build = function _build(data) { this.fingerPrint = fingerPrint; this.publicKey = publicKey; - this.hdPublicKey = new HDPublicKey({ - network: this.network, - data: { - version: network[this.network].prefixes.xpubkey, - depth: this.depth, - parentFingerPrint: this.parentFingerPrint, - childIndex: this.childIndex, - chainCode: this.chainCode, - checksum: this.checksum, - publicKey: this.publicKey - } - }); this.hdPrivateKey = this; - - this.xpubkey = this.hdPublicKey.xpubkey; this.key = key; }; +HDPrivateKey.prototype.__defineGetter__('xpubkey', function() { + return this.hdPublicKey.xpubkey; +}); + +HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() { + if (!this._hdPublicKey) { + this._hdPublicKey = new HDPublicKey({ + network: this.network, + data: { + key: this.key, + version: network[this.network].prefixes.xpubkey, + depth: this.depth, + parentFingerPrint: this.parentFingerPrint, + childIndex: this.childIndex, + chainCode: this.chainCode, + checksum: this.checksum, + publicKey: this.publicKey + } + }); + } + return this._hdPublicKey; +}); + HDPrivateKey.prototype.derive = function derive(index, hardened) { var cached, data, hash, leftPart, chainCode, privateKey, child; var off = 0; @@ -1080,8 +1089,7 @@ HDPublicKey.prototype._build = function _build(data) { this.xpubkey = xpubkey; this.fingerPrint = fingerPrint; - this.xprivkey = data.xprivkey; - this.key = bcoin.keypair({ publicKey: this.publicKey }); + this.key = data.key || bcoin.keypair({ publicKey: this.publicKey }); }; HDPublicKey.prototype.derive = function derive(index, hardened) {