"bitcoinjs-lib": "^4.0.1"
NodeJS 8.11.4
I have a method to create a transaction:
_createTransaction ({ fromAddr, toAddr, amount, wif, fee }) {
return this.info({address: fromAddr}).then((info) => {
try {
const txb = new bitcoin.TransactionBuilder(this._network)
let total = 0
for (let utx of info.utxos) {
txb.addInput(utx.tx_hash_big_endian, utx.tx_output_n)
total += utx.value
}
txb.addOutput(toAddr, amount)
const change = total - (amount + fee)
if (change) txb.addOutput(fromAddr, change)
txb.sign(0, bitcoin.ECPair.fromWIF(wif, this._network))
return txb.build().toHex()
} catch (err) {
throw new Error(this._errMessage('bad implementation', err))
}
}).catch((err) => {
throw new Error(this._errMessage('create transaction', err))
})
}
And I get
Error: Transaction is not complete
What did I miss in my method?
You have N inputs.
You are only signing input 0.
You need to sign N inputs.
Yes, this is it.
I didn't have this problem before. It happened after I sent coins from address A to address A (to the same address), I got 2 inputs. As you advised, I signed these 2 inputs.
info.utxos.forEach((v,i) => {
txb.sign(i, bitcoin.ECPair.fromWIF(wif, this._network))
})
The transaction executed successfully. And I don't have 2 inputs anymore, only 1.
Why did I get 2 inputs and now it is again 1?
What do you mean now it is 1 input?
What's changing?
Why did you get inputs?
Because someone sent you Bitcoins N times.
Most helpful comment
You have N inputs.
You are only signing input 0.
You need to sign N inputs.