avoid badFoo flags, avoid _.each() bug, use for-loop to check each output
This commit is contained in:
parent
40dc1c095b
commit
fa2170a5f3
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue