Browse Source

check activity of previous addresses before generating a new one

activeAddress
Ivan Socolsky 9 years ago
parent
commit
a8a6962c74
  1. 20
      lib/blockchainexplorers/insight.js
  2. 23
      lib/server.js
  3. 2
      test/integration/server.js

20
lib/blockchainexplorers/insight.js

@ -113,7 +113,8 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
});
};
Insight.prototype.getAddressActivity = function(address, cb) {
Insight.prototype.getAddressActivity = function(addresses, cb) {
function getOneAddressActivity(address, cb) {
var url = this.url + this.apiPrefix + '/addr/' + address;
var args = {
method: 'GET',
@ -131,6 +132,23 @@ Insight.prototype.getAddressActivity = function(address, cb) {
});
};
addresses = [].concat(addresses);
var activityFound = false;
var i = 0;
async.until(function() {
return activityFound;
}, function(next) {
getOneAddressActivity(addresses[i++], function(err, res) {
if (err) return next(err);
activityFound = !!res;
return next();
});
}, function(err) {
return cb(err, activityFound);
});
};
Insight.prototype.estimateFee = function(nbBlocks, cb) {
var url = this.url + this.apiPrefix + '/utils/estimatefee';
if (nbBlocks) {

23
lib/server.js

@ -692,20 +692,42 @@ WalletService.prototype.getPreferences = function(opts, cb) {
});
};
WalletService.prototype._canCreateAddress = function(ignoreMaxGap, cb) {
var self = this;
if (ignoreMaxGap) return cb(null, true);
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err);
var latestAddresses = _.takeRight(_.reject(addresses, {
isChange: true
}), WalletService.MAX_MAIN_ADDRESS_GAP);
if (latestAddresses.length < WalletService.MAX_MAIN_ADDRESS_GAP) return cb(null, true);
var bc = self._getBlockchainExplorer(latestAddresses[0].network);
bc.getAddressActivity(latestAddresses, cb);
});
};
/**
* Creates a new address.
* @param {Object} opts
* @param {Boolean} [opts.ignoreMaxGap=false] - Ignore constraint of maximum number of consecutive addresses without activity
* @returns {Address} address
*/
WalletService.prototype.createAddress = function(opts, cb) {
var self = this;
opts = opts || {};
self._runLocked(cb, function(cb) {
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
if (!wallet.isComplete()) return cb(Errors.WALLET_NOT_COMPLETE);
self._canCreateAddress(opts.ignoreMaxGap, function(err, canCreate) {
if (err) return cb(err);
if (!canCreate) return cb(Errors.MAIN_ADDRESS_GAP_REACHED);
var address = wallet.createAddress(false);
self.storage.storeAddressAndWallet(wallet, address, function(err) {
@ -719,6 +741,7 @@ WalletService.prototype.createAddress = function(opts, cb) {
});
});
});
});
};
/**

2
test/integration/server.js

@ -1668,7 +1668,7 @@ describe('Wallet service', function() {
});
});
it.only('should fail to create more consecutive addresses with no activity than allowed', function(done) {
it('should fail to create more consecutive addresses with no activity than allowed', function(done) {
var MAX_MAIN_ADDRESS_GAP_old = WalletService.MAX_MAIN_ADDRESS_GAP;
var n = WalletService.MAX_MAIN_ADDRESS_GAP = 2;
helpers.stubAddressActivity([]);

Loading…
Cancel
Save