From 77b5d1ee214d50cffb30421ff9eb8c85e8c91332 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 25 Mar 2014 10:35:33 +0800 Subject: [PATCH] getCandidateOuputs ignores spent outputs --- src/wallet.js | 2 +- test/wallet.js | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 93c4471..43ad691 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -207,7 +207,7 @@ var Wallet = function (seed, options) { var unspent = [] for (var key in me.outputs){ var output = me.outputs[key] - if(!output.value.spend) unspent.push(output) + if(!output.spend) unspent.push(output) } var sortByValueDesc = unspent.sort(function(o1, o2){ diff --git a/test/wallet.js b/test/wallet.js index 4922cdb..80074e1 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -356,15 +356,6 @@ describe('Wallet', function() { } ] wallet.setUnspentOutputs(utxo) - - function scriptPubKeyFor(address, value){ - var txOut = new TransactionOut({ - value: value, - script: Script.createOutputScript(address) - }) - - return txOut.scriptPubKey() - } }) describe('choosing utxo', function(){ @@ -383,6 +374,25 @@ describe('Wallet', function() { assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 }) assert.deepEqual(tx.ins[1].outpoint, { hash: fakeTxHash(2), index: 1 }) }) + + it('ignores spent outputs', function(){ + utxo.push( + { + "hash": fakeTxHash(4), + "outputIndex": 0, + "scriptPubKey": scriptPubKeyFor(address2, 520000), + "address" : address2, + "value": 530000 // enough but spent before createTx + } + ) + wallet.setUnspentOutputs(utxo) + wallet.outputs[fakeTxHash(4) + ":" + 0].spend = fakeTxHash(5) + ":" + 0 + + var tx = wallet.createTx(to, value) + + assert.equal(tx.ins.length, 1) + assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 }) + }) }) describe('transaction outputs', function(){ @@ -467,6 +477,15 @@ describe('Wallet', function() { function fakeTxHash(i) { return "txtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtx" + i } + + function scriptPubKeyFor(address, value){ + var txOut = new TransactionOut({ + value: value, + script: Script.createOutputScript(address) + }) + + return txOut.scriptPubKey() + } }) function assertEqual(obj1, obj2){