Monday, November 25, 2024

Can’t create p2sh-p2wpkh/ (Uncaught TypeError TypeError: Can’t learn properties of undefined (studying ‘size’))

I am attempting to create transactions with exterior scripts. Individually, utilizing every enter individually and making a transaction with one enter, every part works out with all of the scripts.

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

const ECPair = ECPairFactory(ecc);

const community = bitcoin.networks.testnet;

perform addP2pkhInput(psbt, tx) {
    psbt.addInput({
        hash: tx.hash,
        index: tx.index,
        nonWitnessUtxo: Buffer.from(tx.nonWitnessUtxo, 'hex')
    });
}

perform addP2wpkhInput(psbt, tx) {
    psbt.addInput({
        hash: tx.hash,
        index: tx.index,
        witnessUtxo: {
            script: tx.witnessUtxo.script,
            worth: tx.worth,
        }
    });
}

perform addP2shP2wpkhInput(psbt, tx, community, pubkey) {
    const fee = bitcoin.funds.p2sh({
        redeem: bitcoin.funds.p2wpkh({
                pubkey,
                community
            }
        ),
        community
    });

    psbt.addInput({
        hash: tx.hash,
        index: tx.index,
        redeemScript: fee.redeem.output,
        witnessUtxo: {
            script: fee.output,
            worth: tx.worth,
        }
    });
}

const toXOnly = pubKey => (pubKey.size === 32 ? pubKey : pubKey.slice(1, 33));

perform addP2trInput(psbt, inputIndex, tx, community, pubkey) {
    const { output } = bitcoin.funds.p2tr({
        internalPubkey: toXOnly(pubkey),
        community
    });

    psbt.addInput({
        hash: tx.hash,
        index: inputIndex,
        witnessUtxo: {
            script: output,
            worth: tx.worth, 
        },
        tapInternalKey: toXOnly(pubkey),
    });
}

const psbt = new bitcoin.Psbt({ community });

let totalAmount = 0;

const keyPairs = [
    ECPair.fromWIF('cVgXWJcMy82StLBxuSv7GBTGoLy7voaqMgtPtv7vjQsooMuBWGrH', network),
    ECPair.fromWIF('cVFHShSXiPNp7txS5pcwszm2iQT2tX1wKsXy5a4i1f9PHn1fH6MU', network),
    ECPair.fromWIF('cUpqvCmmnSKt2iAMyzVtADyecsSQiZLkvKojidbpneu8GKt4GHFG', network),
    ECPair.fromWIF('cP2ujbEBrMUf8EiunVx4CX662j8W13SMPVveezR1dR2yKXE6jE35', network).tweak(
        bitcoin.crypto.taggedHash('TapTweak', toXOnly(ECPair.fromWIF('cP2ujbEBrMUf8EiunVx4CX662j8W13SMPVveezR1dR2yKXE6jE35', network).publicKey)),
    )
];

const tx1 = {
    hash: "3399dfd038762c7f37d45a2db55568101b350472099e9a7b640aa917174c7e11",
    index: 0,
    worth: 1155399,
    nonWitnessUtxo: "0200000001e02bdcc56cdf90e71299f697c4fca0e968a8e1d415b58f947d2337754cc18046000000006b483045022100995eab8d8d13a500b768d866c84636840de85a4880f6d49157b3fb424bc7c846022052dcf9bac0f12aa28e0c862c753ae4e0dc7dab023da6d4fd9c4033382935e1740121026ed517acfe148b1d012af4f99134fa24582e4018dfbfe7df1b1b748e2ee7a83bffffffff0147a11100000000001976a9145b63c66657bf13babcbfa9425fb39cdbd28d38de88ac00000000"
}

const tx2 = {
    hash: "91305d2e311c4c2eb4eaee825abd4c6da91b71b0f1a98124fea116b5835283d1",
    index: 0,
    worth: 1284858,
    witnessUtxo: {
        script: bitcoin.handle.toOutputScript('tb1qs3hcrf2ls2cgsh0xvnlnsk546flrkugpaseuwf', community)
    }
}

const tx3 = {
    hash: "4db77e68c7cbf9a5b10f2d9e8c9ca0a9c5612abb9f9d2a62e72bb5f2d5a9dc26",
    index: 0,
    worth: 1265769
}

const tx4 = {
    hash: "138f2d616c36cf86c2500f308de198c7ce99ed3b0b1626f454caffb1bd266374",
    index: 0,
    worth: 50000
}

addP2pkhInput(psbt, tx1, community);
addP2wpkhInput(psbt, tx2, community);
addP2shP2wpkhInput(psbt, tx3, community, keyPairs[2].publicKey);
addP2trInput(psbt, tx4, community);

totalAmount += tx1.worth;
totalAmount += tx2.worth;
totalAmount += tx3.worth;
totalAmount += tx4.worth;

psbt.addOutput({
    handle: 'mggZoTM2Qb2Zdw4ADspJM51Tj8hzPuzKb4',
    worth: totalAmount - 1000,
});

for (let i = 0; i < keyPairs.size; ++i) {
    psbt.signInput(i, keyPairs[i]);
}

psbt.finalizeAllInputs();

console.log(psbt.extractTransaction().toHex());

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles