|
@ -1044,6 +1044,10 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WalletService.scanConfig = { |
|
|
|
|
|
SCAN_WINDOW: 10, |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Scan the blockchain looking for addresses having some activity |
|
|
* Scan the blockchain looking for addresses having some activity |
|
|
* |
|
|
* |
|
@ -1051,6 +1055,78 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
* @param {Boolean} opts.includeCopayerBranches (defaults to false) |
|
|
* @param {Boolean} opts.includeCopayerBranches (defaults to false) |
|
|
*/ |
|
|
*/ |
|
|
WalletService.prototype.scan = function(opts, cb) { |
|
|
WalletService.prototype.scan = function(opts, cb) { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
|
|
|
|
opts = opts || {}; |
|
|
|
|
|
|
|
|
|
|
|
var allAddresses = []; |
|
|
|
|
|
|
|
|
|
|
|
function deriveAddresses(size, isChange, derivator, cb) { |
|
|
|
|
|
async.map(_.range(size), function(i, next) { |
|
|
|
|
|
next(null, derivator(isChange)); |
|
|
|
|
|
}, cb); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function checkActivity(addresses, cb) { |
|
|
|
|
|
var bc = self._getBlockchainExplorer(); |
|
|
|
|
|
bc.getAddressActivity(addresses, cb); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function scanBranch(isChange, derivator, cb) { |
|
|
|
|
|
var activity = true; |
|
|
|
|
|
async.whilst(function() { |
|
|
|
|
|
return activity; |
|
|
|
|
|
}, function(next) { |
|
|
|
|
|
deriveAddresses(WalletService.scanConfig.SCAN_WINDOW, isChange, derivator, function(err, addresses) { |
|
|
|
|
|
if (err) return next(err); |
|
|
|
|
|
allAddresses.push(addresses); |
|
|
|
|
|
checkActivity(_.pluck(addresses, 'address'), function(err, thereIsActivity) { |
|
|
|
|
|
if (err) return next(err); |
|
|
|
|
|
activity = thereIsActivity; |
|
|
|
|
|
next(); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, cb); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Utils.runLocked(self.walletId, cb, function(cb) { |
|
|
|
|
|
self.getWallet({}, function(err, wallet) { |
|
|
|
|
|
if (err) return cb(err); |
|
|
|
|
|
if (!wallet.isComplete()) |
|
|
|
|
|
return cb(new ClientError('Wallet is not complete')); |
|
|
|
|
|
|
|
|
|
|
|
var derivators = []; |
|
|
|
|
|
derivators.push(_.bind(wallet.createAddress, wallet)); |
|
|
|
|
|
if (opts.includeCopayerBranches) { |
|
|
|
|
|
_.each(wallet.copayers, function(copayer) { |
|
|
|
|
|
derivators.push(_.bind(copayer.createAddress, copayer, wallet)); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var branches = _.flatten( |
|
|
|
|
|
_.map(derivators, function(derivator) { |
|
|
|
|
|
return _.map([false, true], function(isChange) { |
|
|
|
|
|
return { |
|
|
|
|
|
derivator: derivator, |
|
|
|
|
|
isChange: isChange |
|
|
|
|
|
}; |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
async.each(branches, function(branch, next) { |
|
|
|
|
|
scanBranch(branch.isChange, branch.derivator, function(err) { |
|
|
|
|
|
next(err); |
|
|
|
|
|
}); |
|
|
|
|
|
}, function(err) { |
|
|
|
|
|
if (err) return cb(err); |
|
|
|
|
|
self.storage.storeAddressAndWallet(wallet, _.flatten(allAddresses), function(err) { |
|
|
|
|
|
return cb(err); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|