diff --git a/lib/wallet/account.js b/lib/wallet/account.js index e6c0cb90..9d74ab5b 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -67,7 +67,7 @@ function Account(db, options) { this.receiveDepth = 0; this.changeDepth = 0; this.nestedDepth = 0; - this.lookahead = 5; + this.lookahead = 10; this.accountKey = null; this.keys = []; @@ -441,9 +441,9 @@ Account.prototype.createKey = co(function* createKey(branch) { this.receive = key; break; case 1: - // Note: no lookahead here. key = this.deriveChange(this.changeDepth); - yield this.saveKey(key); + lookahead = this.deriveReceive(this.changeDepth + this.lookahead); + yield this.saveKey(lookahead); this.changeDepth++; this.change = key; break; @@ -621,12 +621,18 @@ Account.prototype.initDepth = co(function* initDepth() { yield this.saveKey(key); } - // Change Address (no lookahead) + // Change Address this.change = this.deriveChange(0); this.changeDepth = 1; yield this.saveKey(this.change); + // Lookahead + for (i = 0; i < this.lookahead; i++) { + key = this.deriveChange(i + 1); + yield this.saveKey(key); + } + // Nested Address if (this.witness) { this.nested = this.deriveNested(0); @@ -660,7 +666,7 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { if (receive > this.receiveDepth) { depth = this.receiveDepth + this.lookahead; - assert(this.lookahead === 0 || receive <= depth); + assert(receive <= depth + 1); for (i = depth; i < receive + this.lookahead; i++) { key = this.deriveReceive(i); @@ -675,12 +681,16 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { } if (change > this.changeDepth) { - assert(change === this.changeDepth + 1); + depth = this.changeDepth + this.lookahead; - key = this.deriveChange(change - 1); - yield this.saveKey(key); + assert(change <= depth + 1); - this.change = key; + for (i = depth; i < change + this.lookahead; i++) { + key = this.deriveChange(i); + yield this.saveKey(key); + } + + this.change = this.deriveChange(change - 1); this.changeDepth = change; derived = true; @@ -689,7 +699,7 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { if (this.witness && nested > this.nestedDepth) { depth = this.nestedDepth + this.lookahead; - assert(this.lookahead === 0 || nested <= depth); + assert(nested <= depth + 1); for (i = depth; i < nested + this.lookahead; i++) { key = this.deriveNested(i); diff --git a/migrate/walletdb5to6.js b/migrate/walletdb5to6.js index 9d688b30..cb36f202 100644 --- a/migrate/walletdb5to6.js +++ b/migrate/walletdb5to6.js @@ -194,10 +194,10 @@ function accountFromRaw(data) { account.m = p.readU8(); account.n = p.readU8(); account.accountIndex = p.readU32(); - account.receiveDepth = p.readU32() + 5; - account.changeDepth = p.readU32() + 10; - account.nestedDepth = p.readU32() + (account.witness ? 5 : 0); - account.lookahead = 5; + account.receiveDepth = p.readU32(); + account.changeDepth = p.readU32(); + account.nestedDepth = p.readU32(); + account.lookahead = 10; account.accountKey = p.readBytes(82); account.keys = [];