From 3e8ca41a0a6bcf19c67d46de8126f8ed6d75b7bd Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 17 Apr 2014 18:06:55 -0300 Subject: [PATCH] optimize public key generation for BIP32 with cache --- js/models/core/PublicKeyRing.js | 47 ++++++++++++++-------------- test/test.performance.js | 54 +++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 37 deletions(-) diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index 25a31e6b9..9a52cb9cf 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -25,10 +25,12 @@ function PublicKeyRing(opts) { this.requiredCopayers = opts.requiredCopayers || 3; this.totalCopayers = opts.totalCopayers || 5; - this.copayersBIP32 = []; + this.copayersBIP32 = opts.copayersBIP32 || []; - this.changeAddressIndex=0; - this.addressIndex=0; + this.changeAddressIndex= opts.changeAddressIndex || 0; + this.addressIndex= opts.addressIndex || 0; + + this.publicKeysCache = opts.publicKeysCache || {}; } /* @@ -45,24 +47,13 @@ PublicKeyRing.Branch = function (index, isChange) { }; PublicKeyRing.fromObj = function (data) { - if (!data.ts) { + if (data instanceof PublicKeyRing) { throw new Error('bad data format: Did you use .toObj()?'); } - var config = { networkName: data.networkName || 'livenet' }; - - var w = new PublicKeyRing(config); - - w.walletId = data.walletId; - w.requiredCopayers = data.requiredCopayers; - w.totalCopayers = data.totalCopayers; - w.addressIndex = data.addressIndex; - w.changeAddressIndex = data.changeAddressIndex; - w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) { + data.copayersBIP32 = data.copayersExtPubKeys.map(function(pk) { return new BIP32(pk); }); - - w.ts = data.ts; - return w; + return new PublicKeyRing(data); }; PublicKeyRing.prototype.toObj = function() { @@ -77,7 +68,7 @@ PublicKeyRing.prototype.toObj = function() { copayersExtPubKeys: this.copayersBIP32.map( function (b) { return b.extendedPublicKeyString(); }), - ts: parseInt(Date.now() / 1000), + publicKeysCache: this.publicKeysCache }; }; @@ -91,7 +82,6 @@ PublicKeyRing.prototype.registeredCopayers = function () { }; - PublicKeyRing.prototype.isComplete = function () { return this.registeredCopayers() >= this.totalCopayers; }; @@ -130,12 +120,19 @@ PublicKeyRing.prototype.addCopayer = function (newEpk) { PublicKeyRing.prototype.getPubKeys = function (index, isChange) { this._checkKeys(); - var pubKeys = []; - var l = this.copayersBIP32.length; - for(var i=0; i