bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Error: Input #0 has witnessUtxo but non-segwit script

Open aniketdivekar opened this issue 7 years ago • 3 comments

Hi Guys, I'm trying to sign a transaction using the library but I'm getting below error:

Error: Input #0 has witnessUtxo but non-segwit script: a914d82952189f437226406ec17c8c397a34af177cd187

Could someone please help me understand where am I going wrong?

Below is my code:

  const network = bitcoin.networks.testnet
  const psbt = new bitcoin.Psbt({ network })

  const amount0 = UNSPENT_OUTPUTS[0].amount * 100000000
  const amount1 = UNSPENT_OUTPUTS[1].amount * 100000000
 
  // input 1
  psbt.addInput({
    hash: UNSPENT_OUTPUTS[0].txid,
    index: UNSPENT_OUTPUTS[0].vout,
    witnessUtxo: {
      script: Buffer.from(UNSPENT_OUTPUTS[0].scriptPubKey, 'hex'),
      value: amount0
    }
  })

  // input 2
  psbt.addInput({
    hash: UNSPENT_OUTPUTS[1].txid,
    index: UNSPENT_OUTPUTS[1].vout,
    witnessUtxo: {
      script: Buffer.from(UNSPENT_OUTPUTS[1].scriptPubKey, 'hex'),
      value: amount1
    }
  })

  const totalAmount = amount0 + amount1
  const withdrawAmount = 0.0001 * 100000000
  const fee = 0.00001 * 100000000
  const change = totalAmount - (withdrawAmount + fee)

  psbt.addOutput({
    address: '2NF3WNhdXJzgChaAZgdYjHWaAvYG25Nhz58',
    value: withdrawAmount
  })

  psbt.addOutput({
    address: CHANGE_ADDRESSES[0],
    value: change
  })

  await psbt.signInputAsync(0, bitcoin.ECPair.fromWIF(PRIVATE_KEYS[UNSPENT_OUTPUTS[0].address], network))
  await psbt.signInputAsync(1, bitcoin.ECPair.fromWIF(PRIVATE_KEYS[UNSPENT_OUTPUTS[1].address], network))

  console.log('valid signature: ', psbt.validateSignaturesOfAllInputs()) // This prints true

  psbt.finalizeAllInputs()
  const rawHex = psbt.extractTransaction().toHex()
  console.log(rawHex)

Below is the list of Unspent transactions:

[
  {
      "txid": "a919fa237493c3479bd940adf73678293ed5dad673d00f465e7c76d5eeef2063",
      "vout": 1,
      "address": "2NCxBSjMaVeBFyxmGcD2X428v6k5n3pCQKN",
      "account": "payment",
      "redeemScript": "001495a994a417c45f97c87d03efb21997452d4c782f",
      "scriptPubKey": "a914d82952189f437226406ec17c8c397a34af177cd187",
      "amount": 0.00150000,
      "confirmations": 93,
      "spendable": true,
      "solvable": true,
      "safe": true
  },
  {
      "txid": "932e90d17b7d0db8a058bf8714e2db3dde5e74d5d3a6d1ea1d7efba39bd218a5",
      "vout": 1,
      "address": "2MzaZzn4cuAByJrNRpDHEgE8Z55Y7dsi3Gq",
      "account": "payment",
      "redeemScript": "00143c40cfa9281ac0f01b248cdaa33afe46fca00a4a",
      "scriptPubKey": "a91450717c6c3e59b94312fe5effe58e5d50edb8f69e87",
      "amount": 0.00150000,
      "confirmations": 90,
      "spendable": true,
      "solvable": true,
      "safe": true
  }
]

Thanks in advance :)

aniketdivekar avatar Nov 07 '19 10:11 aniketdivekar

I had to add redeemScript in input.. It works now!

aniketdivekar avatar Nov 07 '19 13:11 aniketdivekar

@aniketdivekar How did you get the list of Unspent transactions?

mikbolshakov avatar Dec 01 '23 06:12 mikbolshakov

https://mempool.space/docs/api/rest#get-address-utxo

junderw avatar Dec 01 '23 06:12 junderw