avoid badFoo flags, avoid _.each() bug, use for-loop to check each output

This commit is contained in:
Gregg Zigler 2015-06-22 14:00:33 -04:00
parent 40dc1c095b
commit fa2170a5f3
1 changed files with 10 additions and 19 deletions

View File

@ -878,32 +878,23 @@ WalletService.prototype.createTx = function(opts, cb) {
var outputs = (opts.type == Model.TxProposal.Types.MULTIPLEOUTPUTS)
? opts.outputs
: [ { toAddress: opts.toAddress, amount: opts.amount } ];
var badAddress = false,
badNetwork = false,
badAmount = false,
badDust = false;
_.each(outputs, function(output) {
var toAddress;
// _.each(outputs) fails here, causes multiple callbacks to be executed
// use for-loop instead
for (var i = 0; i < outputs.length; i++) {
var output = outputs[i];
var toAddress = {};
try {
toAddress = new Bitcore.Address(output.toAddress);
} catch (ex) {
badAddress = true;
return cb(new ClientError('INVALIDADDRESS', 'Invalid address'));
}
if (toAddress.network != wallet.getNetworkName())
badNetwork = true;
return cb(new ClientError('INVALIDADDRESS', 'Incorrect address network'));
if (output.amount <= 0)
badAmount = true;
return cb(new ClientError('Invalid amount'));
if (output.amount < Bitcore.Transaction.DUST_AMOUNT)
badDust = true;
});
if (badAddress)
return cb(new ClientError('INVALIDADDRESS', 'Invalid address'));
if (badNetwork)
return cb(new ClientError('INVALIDADDRESS', 'Incorrect address network'));
if (badAmount)
return cb(new ClientError('Invalid amount'));
if (badDust)
return cb(new ClientError('DUSTAMOUNT', 'Amount below dust threshold'));
return cb(new ClientError('DUSTAMOUNT', 'Amount below dust threshold'));
}
var changeAddress = wallet.createAddress(true);