Bitcoin

Unable to generate p2sh-p2wpkh/ (Uncaught TypeError TypeError: Cannot read property of undefined (reading ‘length’))

I’m trying to create a transaction using an external script. If I use each input separately and create a transaction with one input everything will work in all scripts.

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

const ECPair = ECPairFactory(ecc);

const network = bitcoin.networks.testnet;

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


function addP2wpkhInput(psbt, tx) 
    psbt.addInput(
        hash: tx.hash,
        index: tx.index,
        witnessUtxo: 
            script: tx.witnessUtxo.script,
            value: tx.value,
        
    );


function addP2shP2wpkhInput(psbt, tx, network, pubkey) 
    const payment = bitcoin.payments.p2sh(
        redeem: bitcoin.payments.p2wpkh(
                pubkey,
                network
            
        ),
        network
    );

    psbt.addInput(
        hash: tx.hash,
        index: tx.index,
        redeemScript: payment.redeem.output,
        witnessUtxo: 
            script: payment.output,
            value: tx.value,
        
    );


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

function addP2trInput(psbt, inputIndex, tx, network, pubkey) 
    const  output  = bitcoin.payments.p2tr(
        internalPubkey: toXOnly(pubkey),
        network
    );

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


const psbt = new bitcoin.Psbt( network );

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,
    value: 1155399,
    nonWitnessUtxo: "0200000001e02bdcc56cdf90e71299f697c4fca0e968a8e1d415b58f947d2337754cc18046000000006b483045022100995eab8d8d13a500b768d866c84636840de85a4880f6d49157b3fb424bc7c846022052dcf9bac0f12aa28e0c862c753ae4e0dc7dab023da6d4fd9c4033382935e1740121026ed517acfe148b1d012af4f99134fa24582e4018dfbfe7df1b1b748e2ee7a83bffffffff0147a11100000000001976a9145b63c66657bf13babcbfa9425fb39cdbd28d38de88ac00000000"


const tx2 = 
    hash: "91305d2e311c4c2eb4eaee825abd4c6da91b71b0f1a98124fea116b5835283d1",
    index: 0,
    value: 1284858,
    witnessUtxo: 
        script: bitcoin.address.toOutputScript('tb1qs3hcrf2ls2cgsh0xvnlnsk546flrkugpaseuwf', network)
    


const tx3 = 
    hash: "4db77e68c7cbf9a5b10f2d9e8c9ca0a9c5612abb9f9d2a62e72bb5f2d5a9dc26",
    index: 0,
    value: 1265769


const tx4 = 
    hash: "138f2d616c36cf86c2500f308de198c7ce99ed3b0b1626f454caffb1bd266374",
    index: 0,
    value: 50000


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

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

psbt.addOutput(
    address: 'mggZoTM2Qb2Zdw4ADspJM51Tj8hzPuzKb4',
    value: totalAmount - 1000,
);

for (let i = 0; i < keyPairs.length; ++i) 
    psbt.signInput(i, keyPairs(i));


psbt.finalizeAllInputs();

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

Related Articles

Back to top button