bitcoinjs-lib: Error: Input #0 has witnessUtxo but non-segwit script:
I’m trying to sign a transaction but I’m getting below error:
Error: Input #0 has witnessUtxo but non-segwit script: a91455e9fceb916ad6cc5fc23b3a8bd2cc8955a436ed87
Below is my code:
const bitcoin = require('bitcoinjs-lib');
const request = require('request-promise-native');
// const net = bitcoin.networks.bitcoin;
const net = bitcoin.networks.testnet;
const API = net === bitcoin.networks.testnet
? 'https://test-insight.swap.online/insight-api'
: 'https://insight.bitpay.com/api'
const fetchUnspents = (address) =>
request(`${API}/addr/${address}/utxo/`).then(JSON.parse)
(async () => {
const psbt = new bitcoin.Psbt({ network: net });
const alice_pair = bitcoin.ECPair.fromWIF('cR6RdGAojE4TuzRSY16a92vPfKgdjUj3tjHmoaPRcHvxRpTMr8gy', net);
const pubkeys = [alice_pair.publicKey].map(hex => Buffer.from(hex, 'hex'));
const { address } = bitcoin.payments.p2sh({ redeem: bitcoin.payments.p2ms({ m: 1, pubkeys, network: net })});
const unspents = await fetchUnspents(address);
const totalAmount = unspents.reduce((summ, { satoshis }) => summ + satoshis, 0);
const withdrawAmount = 0.001 * 100000000;
const fee = 0.00001 * 100000000;
const change = totalAmount - (withdrawAmount + fee);
unspents.forEach(element => {
psbt.addInput({
hash: element.txid,
index: element.vout,
witnessUtxo: {
script: Buffer.from(element.scriptPubKey, 'hex'),
value: element.amount * 100000000
},
// redeemScript:Buffer.from(element.scriptPubKey)
})
});
psbt.addOutput({
address: '2NBJtjiL8kHb3vGW4b945rgSbh9zasAq7qB',
value: withdrawAmount
});
psbt.addOutput({
address: address,
value: change
});
psbt.signInput(0, alice_pair);
console.log('valid signature: ', psbt.validateSignaturesOfAllInputs());
psbt.finalizeAllInputs();
const rawHex = psbt.extractTransaction().toHex();
console.log(rawHex);
})()
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 21 (12 by maintainers)
Hello,
I’m trying to do the same thing, but I have a mistake, if you have an idea : “Redeem script for input #0 doesn’t match the scriptPubKey in the prevout”
My code:
if you have any suggestions… thanks
remove
witnessUtxo
you don’t need it.nonWitnessUtxo
has all the info you need.@rohitsahu21
.publicKey
alreadyBuffer
so above code will raise error. You can write like thisAbove utxos is not
witnessUtxo
so you should usenonWitnessUtxo
instead. Value of nonWitnessUtxo is Buffer of hex of previous raw txA sample inputData is