fix backup restore + tests

This commit is contained in:
Matias Alejo Garcia 2014-08-18 18:11:30 -04:00
parent 411ebbe2d7
commit c7d04418b0
2 changed files with 15 additions and 7 deletions

View File

@ -1611,12 +1611,12 @@ Wallet.prototype.updateIndexes = function(callback) {
Wallet.prototype.updateIndex = function(index, callback) {
var self = this;
var SCANN_WINDOW = 20;
self.indexDiscovery(index.changeIndex, true, index.cosigner, SCANN_WINDOW, function(err, changeIndex) {
self.indexDiscovery(index.changeIndex, true, index.copayerIndex, SCANN_WINDOW, function(err, changeIndex) {
if (err) return callback(err);
if (changeIndex != -1)
index.changeIndex = changeIndex + 1;
self.indexDiscovery(index.receiveIndex, false, index.cosigner, SCANN_WINDOW, function(err, receiveIndex) {
self.indexDiscovery(index.receiveIndex, false, index.copayerIndex, SCANN_WINDOW, function(err, receiveIndex) {
if (err) return callback(err);
if (receiveIndex != -1)
index.receiveIndex = receiveIndex + 1;
@ -1625,12 +1625,13 @@ Wallet.prototype.updateIndex = function(index, callback) {
});
}
Wallet.prototype.deriveAddresses = function(index, amount, isChange, cosigner) {
preconditions.checkArgument(cosigner);
Wallet.prototype.deriveAddresses = function(index, amount, isChange, copayerIndex) {
preconditions.checkArgument(amount);
preconditions.shouldBeDefined(copayerIndex);
var ret = new Array(amount);
for (var i = 0; i < amount; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange, cosigner).toString();
ret[i] = this.publicKeyRing.getAddress(index + i, isChange, copayerIndex).toString();
}
return ret;
}
@ -1638,7 +1639,9 @@ Wallet.prototype.deriveAddresses = function(index, amount, isChange, cosigner) {
// This function scans the publicKeyRing branch starting at index @start and reports the index with last activity,
// using a scan window of @gap. The argument @change defines the branch to scan: internal or external.
// Returns -1 if no activity is found in range.
Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) {
Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb) {
preconditions.shouldBeDefined(copayerIndex);
preconditions.checkArgument(gap);
var scanIndex = start;
var lastActive = -1;
var hasActivity = false;
@ -1648,7 +1651,7 @@ Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) {
function _do(next) {
// Optimize window to minimize the derivations.
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, cosigner);
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, copayerIndex);
self.blockchain.checkActivity(addresses, function(err, actives) {
if (err) throw err;

View File

@ -814,6 +814,9 @@ describe('Wallet model', function() {
});
sinon.assert.callCount(updateIndex, 4);
sinon.assert.calledWith(updateIndex, w.publicKeyRing.indexes[0] );
sinon.assert.calledWith(updateIndex, w.publicKeyRing.indexes[1] );
sinon.assert.calledWith(updateIndex, w.publicKeyRing.indexes[2] );
w.updateIndex.restore();
done();
});
@ -837,6 +840,8 @@ describe('Wallet model', function() {
index.receiveIndex.should.equal(9);
index.changeIndex.should.equal(9);
indexDiscovery.callCount.should.equal(2);
sinon.assert.calledWith(indexDiscovery, 1, true, 2, 20 );
sinon.assert.calledWith(indexDiscovery, 2, false, 2, 20 );
w.indexDiscovery.restore();
done();
});