optimize public key generation for BIP32 with cache
This commit is contained in:
parent
6d822a9153
commit
3e8ca41a0a
2 changed files with 64 additions and 37 deletions
|
|
@ -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<l; i++) {
|
||||
var path = PublicKeyRing.Branch(index, isChange);
|
||||
var bip32 = this.copayersBIP32[i].derive(path);
|
||||
pubKeys[i] = bip32.eckey.public;
|
||||
var path = PublicKeyRing.Branch(index, isChange);
|
||||
var pubKeys = this.publicKeysCache[path];
|
||||
if (!pubKeys) {
|
||||
pubKeys = [];
|
||||
var l = this.copayersBIP32.length;
|
||||
for(var i=0; i<l; i++) {
|
||||
var bip32 = this.copayersBIP32[i].derive(path);
|
||||
pubKeys[i] = bip32.eckey.public;
|
||||
}
|
||||
this.publicKeysCache[path] = pubKeys;
|
||||
//console.log('cache fill['+path+']='+pubKeys.length);
|
||||
} else {
|
||||
//console.log('cache hit!');
|
||||
}
|
||||
|
||||
return pubKeys;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue