Coder Social home page Coder Social logo

Comments (6)

junderw avatar junderw commented on July 17, 2024 1

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,
});
});

from bitcoinjs-lib.

junderw avatar junderw commented on July 17, 2024

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.

from bitcoinjs-lib.

lavi023 avatar lavi023 commented on July 17, 2024

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 ?

from bitcoinjs-lib.

junderw avatar junderw commented on July 17, 2024

Don’t pass Buffers into Buffer.from

they are already Buffers

from bitcoinjs-lib.

lavi023 avatar lavi023 commented on July 17, 2024

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

from bitcoinjs-lib.

lavi023 avatar lavi023 commented on July 17, 2024

Dear @junderw

Many many thanks my issue has solved :)

from bitcoinjs-lib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.