|
|
@ -692,29 +692,52 @@ 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); |
|
|
|
|
|
|
|
var address = wallet.createAddress(false); |
|
|
|
|
|
|
|
self.storage.storeAddressAndWallet(wallet, address, function(err) { |
|
|
|
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._notify('NewAddress', { |
|
|
|
address: address.address, |
|
|
|
}, function() { |
|
|
|
return cb(null, address); |
|
|
|
self.storage.storeAddressAndWallet(wallet, address, function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self._notify('NewAddress', { |
|
|
|
address: address.address, |
|
|
|
}, function() { |
|
|
|
return cb(null, address); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|