From fa48b02d968789b4beee9a8b93190a84f78f00b6 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Tue, 6 May 2014 14:06:17 +0400 Subject: [PATCH] tx: remove .inputTx() method --- lib/bcoin/tx.js | 31 ++++++++++++++++++++----------- test/tx-test.js | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 7f0de14d..0139a556 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -58,27 +58,36 @@ TX.prototype.input = function input(i, index) { else hash = i.hash; - this.inputs.push({ + if (typeof hash !== 'string') + hash = utils.toHex(hash); + + var input = { out: { - tx: i.tx, + tx: i.tx || null, hash: hash, index: i.out ? i.out.index : i.index, }, script: i.script ? i.script.slice() : [], seq: i.seq === undefined ? 0xffffffff : i.seq - }); + }; + + // Try modifying existing input first + for (var i = 0; i < this.inputs.length; i++) { + var ex = this.inputs[i]; + if (ex.out.hash !== hash && ex.out.index !== index) + continue; + + ex.out.tx = input.out.tx || ex.out.tx; + ex.seq = input.seq || ex.seq; + ex.script = input.script.length ? input.script : ex.script; + break; + } + if (i === this.inputs.length) + this.inputs.push(input); return this; }; -TX.prototype.inputTx = function inputTx(i, tx) { - if (!(tx instanceof TX)) - tx = new TX(tx); - - assert(i <= this.inputs.length); - this.inputs[i].out.tx = tx; -}; - TX.prototype.out = function out(output, value) { if (typeof output === 'string') { output = { diff --git a/test/tx-test.js b/test/tx-test.js index e8312a77..df5776e9 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -48,7 +48,7 @@ describe('TX', function() { it('should be verifiable', function() { var tx = bcoin.tx(parser.parseTx(bcoin.utils.toArray(raw, 'hex'))); - tx.inputTx(0, bcoin.tx(parser.parseTx(bcoin.utils.toArray(inp, 'hex')))); + tx.input(bcoin.tx(parser.parseTx(bcoin.utils.toArray(inp, 'hex'))), 0); assert(tx.verify()); });