From 7975c59ba7895173b653c6398bf7371512bbcfb6 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Thu, 5 Feb 2015 11:58:00 -0300 Subject: [PATCH] UnspentOutput: Fix issue when 0 being feed as amount --- lib/transaction/unspentoutput.js | 8 +++++--- test/transaction/unspentoutput.js | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/transaction/unspentoutput.js b/lib/transaction/unspentoutput.js index e5e71f5..f80c487 100644 --- a/lib/transaction/unspentoutput.js +++ b/lib/transaction/unspentoutput.js @@ -41,10 +41,12 @@ function UnspentOutput(data) { if (!_.isNumber(outputIndex)) { throw new Error('Invalid outputIndex, received ' + outputIndex); } - $.checkArgument(!_.isUndefined(data.scriptPubKey || data.script), 'Must provide the scriptPubKey for that output!'); + $.checkArgument(!_.isUndefined(data.scriptPubKey) || !_.isUndefined(data.script), + 'Must provide the scriptPubKey for that output!'); var script = new Script(data.scriptPubKey || data.script); - $.checkArgument(!_.isUndefined(data.amount || data.satoshis), 'Must provide an amount for the output'); - var amount = data.amount ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis; + $.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis), + 'Must provide an amount for the output'); + var amount = !_.isUndefined(data.amount) ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis; $.checkArgument(_.isNumber(amount), 'Amount must be a number'); JSUtil.defineImmutable(this, { address: address, diff --git a/test/transaction/unspentoutput.js b/test/transaction/unspentoutput.js index fde2881..8d9da46 100644 --- a/test/transaction/unspentoutput.js +++ b/test/transaction/unspentoutput.js @@ -53,6 +53,30 @@ describe('UnspentOutput', function() { expect(new UnspentOutput(sampleData1).inspect()).to.equal(expected); }); + describe('checking the constructor parameters', function() { + var notDefined = { + 'txId': 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458', + 'outputIndex': 0, + 'script': 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG', + }; + var zero = { + 'txId': 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458', + 'outputIndex': 0, + 'script': 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG', + 'amount': 0 + }; + it('fails when no amount is defined', function() { + expect(function() { + return new UnspentOutput(notDefined); + }).to.throw('Must provide an amount for the output'); + }); + it('does not fail when amount is zero', function() { + expect(function() { + return new UnspentOutput(zero); + }).to.not.throw(); + }); + }); + it('toString returns txid:vout', function() { var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0'; expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);