From fa2170a5f31248659d45ce44e532b28f5d910f10 Mon Sep 17 00:00:00 2001 From: Gregg Zigler Date: Mon, 22 Jun 2015 14:00:33 -0400 Subject: [PATCH] avoid badFoo flags, avoid _.each() bug, use for-loop to check each output --- lib/server.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/server.js b/lib/server.js index 4cf3db7..b33acea 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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);