bitcoinjs/bitcoinjs-lib

Error: Redeem script for input #0 doesn't match the scriptPubKey in the prevout

lavi023 opened this issue · 6 comments

Dear community

I am new here to work with bitcoin library, know only basic programming, i wants to create a bitcoin transaction with bitcoinjs-lib and i have WIF pvt key. i don't know how to create redeemscript? i try this code from here

https://bitcoin.stackexchange.com/questions/118945/how-to-build-a-transaction-using-bitcoinjs-lib

but not able to create transaction hex successfully, if it will ok then i will broadcast here
https://live.blockcypher.com/btc-testnet/pushtx/

So please look out my code and give me suggest why i am getting this error "Error: Redeem script for input #0 doesn't match the scriptPubKey in the prevout"

import * as bitcoin from 'bitcoinjs-lib';
import * as ECPairFactory from 'ecpair';
import * as ecc from 'tiny-secp256k1';

const ECPair = ECPairFactory.ECPairFactory(ecc);
const network = bitcoin.networks.testnet;

const privateKeyWIF = 'cUE5pQBLsEXUh9H4A9XQ4gHRG19Le5sfPG34QupUaKDHqS5dwZVJ';
const previousTxid = '155009fe07a45086066843faf90dcb5d541bf7d77d7e3dd0db7707e13947d314';
const receiverAddress = '2N2Vf2ezL4FgHVhi1tUo77ZGF1m7ykdzjm4';

async function createTransaction()
{

const keyPair = ECPair.fromWIF(privateKeyWIF, network);
const psbt = new bitcoin.Psbt({ network });

psbt.addInput({
hash: "155009fe07a45086066843faf90dcb5d541bf7d77d7e3dd0db7707e13947d314",
index: 1,
witnessUtxo: {
script: Buffer.from("a91473b7786d0ada73c9f0d90c42605c03ba3a76a88687", 'hex'), // this buffer is correct??
value: 86141,
},
redeemScript: Buffer.from("001473b7786d0ada73c9f0d90c42605c03ba3a76a886", 'hex'), // redeemscript is correct or not?, how to get redeemscript here?
});

psbt.addOutput({
address: receiverAddress,
value: 70000,
});

psbt.signInput(0, keyPair);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
console.log(tx.toHex());
//return tx.toHex();
}

createTransaction();

The redeemScript is incorrect.

This is how you would create the redeemScript properly.

const p2shP2wpkh = bitcoin.payments.p2sh({
    redeem: bitcoin.payments.p2wpkh({
        pubkey: keyPair.publicKey,
        network,
    }),
    network,
});

const redeemScript = p2shP2wpkh.redeem.output;
const previousScriptPubkey = p2shP2wpkh.output; // This is the witnessUtxo.script data. You hard coded it, but this is how you would calculate it.

Thanks @junderw for supporting me.

now i have put those two value here and my whole code is:-

async function createTransaction()
{

const keyPair = ECPair.fromWIF(privateKeyWIF, network);
const psbt = new bitcoin.Psbt({ network });

const p2shP2wpkh = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({
pubkey: keyPair.publicKey,
network,
}),
network,
});

const redeemScript = p2shP2wpkh.redeem.output;
const previousScriptPubkey = p2shP2wpkh.output;
psbt.addInput({
hash: "155009fe07a45086066843faf90dcb5d541bf7d77d7e3dd0db7707e13947d314",
index: 1,
witnessUtxo: {
script: Buffer.from(previousScriptPubkey, 'hex'),
value: 86141,
},
redeemScript: Buffer.from(redeemScript, 'hex'),
});

psbt.addOutput({
address: receiverAddress,
value: 70000,
});

psbt.signInput(0, keyPair);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
console.log(tx.toHex());
//return tx.toHex();
}

After compling it i have get below hex

0200000000010114d34739e10777dbd03d7e7dd7f71b545dcb0df9fa4368068650a407fe095015010000001716001452d9f4cf06defccfacfa0307cd7bab1ac47d4bd5ffffffff01701101000000000017a9146573dbcd80b8b054eca401147c4c4bc19b00b9558702483045022100a76c902171a9ae53b06f0723d0e9ea689fac031b61f5075d2ccaa52956230fe502203000cf23b3633c33ba1c236e54e2d2558c2af263dcf898fbf4ab5d6c7f4ff95601210209ccdaa6f3f34ffeeef4365bdc64c30b3c33887cecf1ac429172565d02885d0500000000

i try to broadcast it and facing a error that is-

Error validating transaction: Error running script for input 0 referencing 155009fe07a45086066843faf90dcb5d541bf7d77d7e3dd0db7707e13947d314 at 1: Script was NOT verified successfully..

Please tell me i am doing any mistake here ?

Don’t pass Buffers into Buffer.from

they are already Buffers

Dear @junderw

I have pass Buffers direct like it

witnessUtxo: {
script: previousScriptPubkey,
value: 86141,
},
redeemScript: redeemScript,

But still facing same issue, please provide me reference for any basic transaction example where i could put WIF pvt key, TxID, Vout to get transaction Hex.. or suggest any updation for it so it could be error free.

thanks

Then the issue is using the wrong key.

Here's an example. It seems you are doing everything correctly. So the only thing that can be wrong is that you are using the wrong WIF key.

it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input', async () => {
const p2sh = createPayment('p2sh-p2wpkh');
const inputData = await getInputData(5e4, p2sh.payment, true, 'p2sh');
const inputData2 = await getInputData(5e4, p2sh.payment, true, 'p2sh');
{
const {
hash,
index,
witnessUtxo, // NEW: this is an object of the output being spent { script: Buffer; value: Satoshis; }
redeemScript,
} = inputData;
assert.deepStrictEqual(
{ hash, index, witnessUtxo, redeemScript },
inputData,
);
}
const keyPair = p2sh.keys[0];
const outputData = {
script: p2sh.payment.output, // sending to myself for fun
value: 2e4,
};
const outputData2 = {
script: p2sh.payment.output, // sending to myself for fun
value: 7e4,
};
const tx = new bitcoin.Psbt()
.addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2])
.signAllInputs(keyPair)
.finalizeAllInputs()
.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex());
await regtestUtils.verify({
txId: tx.getId(),
address: p2sh.payment.address,
vout: 0,
value: 2e4,
});
});

Dear @junderw

Many many thanks my issue has solved :)