Bitcoinjs-lib: Transaction is not complete

Created on 7 Sep 2018  路  4Comments  路  Source: bitcoinjs/bitcoinjs-lib

"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?

how to / question / docs

Most helpful comment

You have N inputs.
You are only signing input 0.

You need to sign N inputs.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

askucher picture askucher  路  3Comments

tuyennvtb picture tuyennvtb  路  3Comments

panpan2 picture panpan2  路  3Comments

itsMikeLowrey picture itsMikeLowrey  路  3Comments

Mr-Mondragon picture Mr-Mondragon  路  3Comments