diff --git a/lib/server.js b/lib/server.js index 38ba428..7501019 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1420,28 +1420,37 @@ WalletService.prototype.getTxHistory = function(opts, cb) { var now = Math.floor(Date.now() / 1000); return _.map(txs, function(tx) { - var inputs = classify(tx.inputs); - var outputs = classify(tx.outputs); - var amountIn = sum(inputs, true); - var amountOut = sum(outputs, true, false); - var amountOutChange = sum(outputs, true, true); + var amountIn, amountOut, amountOutChange; var amount, action, addressTo; - if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? tx.fees : 0))) { - amount = amountOut; - action = 'moved'; - } else { - amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? tx.fees : 0); - action = amount > 0 ? 'sent' : 'received'; - } - amount = Math.abs(amount); - if (action == 'sent' || action == 'moved') { - var firstExternalOutput = _.find(outputs, { - isMine: false - }); - addressTo = firstExternalOutput ? firstExternalOutput.address : 'N/A'; - }; + if (tx.outputs.length || tx.inputs.length) { + + var inputs = classify(tx.inputs); + var outputs = classify(tx.outputs); + + amountIn = sum(inputs, true); + amountOut = sum(outputs, true, false); + amountOutChange = sum(outputs, true, true); + if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? tx.fees : 0))) { + amount = amountOut; + action = 'moved'; + } else { + amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? tx.fees : 0); + action = amount > 0 ? 'sent' : 'received'; + } + + amount = Math.abs(amount); + if (action == 'sent' || action == 'moved') { + var firstExternalOutput = _.find(outputs, { + isMine: false + }); + addressTo = firstExternalOutput ? firstExternalOutput.address : 'N/A'; + }; + } else { + action = 'invalid'; + amount = 0; + } var newTx = { txid: tx.txid, diff --git a/test/integration/server.js b/test/integration/server.js index 2d7abce..8d14f2c 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -3592,6 +3592,19 @@ describe('Wallet service', function() { done(); }); }); + it('should handle invalid tx in history ', function(done) { + var h = _.clone(TestData.history); + h.push({txid:'xx'}) + helpers.stubHistory(h); + + server.getTxHistory({}, function(err, txs) { + should.not.exist(err); + should.exist(txs); + txs.length.should.equal(3); + txs[2].action.should.equal('invalid'); + done(); + }); + }); }); describe('#scan', function() {