Browse Source

getCandidateOuputs ignores spent outputs

hk-custom-address
Wei Lu 11 years ago
parent
commit
77b5d1ee21
  1. 2
      src/wallet.js
  2. 37
      test/wallet.js

2
src/wallet.js

@ -207,7 +207,7 @@ var Wallet = function (seed, options) {
var unspent = [] var unspent = []
for (var key in me.outputs){ for (var key in me.outputs){
var output = me.outputs[key] 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){ var sortByValueDesc = unspent.sort(function(o1, o2){

37
test/wallet.js

@ -356,15 +356,6 @@ describe('Wallet', function() {
} }
] ]
wallet.setUnspentOutputs(utxo) wallet.setUnspentOutputs(utxo)
function scriptPubKeyFor(address, value){
var txOut = new TransactionOut({
value: value,
script: Script.createOutputScript(address)
})
return txOut.scriptPubKey()
}
}) })
describe('choosing utxo', function(){ 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[0].outpoint, { hash: fakeTxHash(3), index: 0 })
assert.deepEqual(tx.ins[1].outpoint, { hash: fakeTxHash(2), index: 1 }) 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(){ describe('transaction outputs', function(){
@ -467,6 +477,15 @@ describe('Wallet', function() {
function fakeTxHash(i) { function fakeTxHash(i) {
return "txtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtx" + 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){ function assertEqual(obj1, obj2){

Loading…
Cancel
Save