From f7af487597b04eadcb0004d8ff7b8e72e6d21a87 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Mon, 23 Jun 2014 08:30:57 +0800 Subject: [PATCH] wallet: reintroduce output.to to track pending spent utxo --- src/wallet.js | 11 ++++++----- test/wallet.js | 10 +++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 65651f0..da14080 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -65,7 +65,7 @@ function Wallet(seed, network) { for(var key in this.outputs){ var output = this.outputs[key] - utxo.push(outputToUnspentOutput(output)) + if(!output.to) utxo.push(outputToUnspentOutput(output)) } return utxo @@ -167,7 +167,7 @@ function Wallet(seed, network) { } }) - tx.ins.forEach(function(txIn) { + tx.ins.forEach(function(txIn, i) { // copy and convert to big-endian hex var txinId = new Buffer(txIn.hash) Array.prototype.reverse.call(txinId) @@ -178,10 +178,11 @@ function Wallet(seed, network) { if (!(output in me.outputs)) return if (isPending) { - return me.outputs[output].pending = true + me.outputs[output].to = txid + ':' + i + me.outputs[output].pending = true + } else { + delete me.outputs[output] } - - delete me.outputs[output] }) } diff --git a/test/wallet.js b/test/wallet.js index 456838e..76d830f 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -215,6 +215,13 @@ describe('Wallet', function() { it('parses wallet outputs to the expect format', function(){ assert.deepEqual(wallet.getUnspentOutputs(), [expectedUtxo]) }) + + it("ignores pending spending outputs (outputs with 'to' property)", function(){ + var output = wallet.outputs[expectedOutputKey] + output.to = fakeTxId(0) + ':' + 0 + output.pending = true + assert.deepEqual(wallet.getUnspentOutputs(), []) + }) }) describe('setUnspentOutputs', function(){ @@ -280,7 +287,7 @@ describe('Wallet', function() { spendTx = Transaction.fromHex(fixtureTx2Hex) }) - it("outgoing: sets the pending flag on output instead of deleting it", function(){ + it("outgoing: sets the pending flag and 'to' on output", function(){ var txIn = spendTx.ins[0] var txInId = new Buffer(txIn.hash) Array.prototype.reverse.call(txInId) @@ -291,6 +298,7 @@ describe('Wallet', function() { wallet.processPendingTx(spendTx) assert(wallet.outputs[key].pending) + assert.equal(wallet.outputs[key].to, spendTx.getId() + ':' + 0) }) }) })