REF: cached HD addresses generation (speeded up)

This commit is contained in:
Overtorment 2019-05-20 23:57:46 +01:00
parent 3391793f8c
commit fb8cbe0fd8
4 changed files with 41 additions and 9 deletions

View file

@ -20,6 +20,12 @@ export class AbstractHDWallet extends LegacyWallet {
this.gap_limit = 3;
}
prepareForSerialization() {
// deleting structures that cant be serialized
delete this._node0;
delete this._node1;
}
generate() {
throw new Error('Not implemented');
}

View file

@ -250,6 +250,7 @@ export class AppStorage {
let walletsToSave = [];
for (let key of this.wallets) {
if (typeof key === 'boolean') continue;
if (key.prepareForSerialization) key.prepareForSerialization();
walletsToSave.push(JSON.stringify({ ...key, type: key.type }));
}

View file

@ -52,6 +52,8 @@ export class HDSegwitBech32Wallet extends AbstractHDWallet {
this._txs_by_external_index = {};
this._txs_by_internal_index = {};
this.gap_limit = 20;
}
/**
@ -141,9 +143,26 @@ export class HDSegwitBech32Wallet extends AbstractHDWallet {
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
}
const xpub = _zpubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
const address = _nodeToBech32SegwitAddress(hdNode.derive(node).derive(index));
if (node === 0 && !this._node0) {
const xpub = _zpubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
this._node0 = hdNode.derive(node);
}
if (node === 1 && !this._node1) {
const xpub = _zpubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
this._node1 = hdNode.derive(node);
}
let address;
if (node === 0) {
address = _nodeToBech32SegwitAddress(this._node0.derive(index));
}
if (node === 1) {
address = _nodeToBech32SegwitAddress(this._node1.derive(index));
}
if (node === 0) {
return (this.external_addresses_cache[index] = address);

View file

@ -102,9 +102,12 @@ export class HDSegwitP2SHWallet extends AbstractHDWallet {
index = index * 1; // cast to int
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
const xpub = ypubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
const address = nodeToP2shSegwitAddress(hdNode.derive(0).derive(index));
if (!this._node0) {
const xpub = ypubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
this._node0 = hdNode.derive(0);
}
const address = nodeToP2shSegwitAddress(this._node0.derive(index));
return (this.external_addresses_cache[index] = address);
}
@ -113,9 +116,12 @@ export class HDSegwitP2SHWallet extends AbstractHDWallet {
index = index * 1; // cast to int
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
const xpub = ypubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
const address = nodeToP2shSegwitAddress(hdNode.derive(1).derive(index));
if (!this._node1) {
const xpub = ypubToXpub(this.getXpub());
const hdNode = bitcoin.HDNode.fromBase58(xpub);
this._node1 = hdNode.derive(1);
}
const address = nodeToP2shSegwitAddress(this._node1.derive(index));
return (this.internal_addresses_cache[index] = address);
}