Impossible de créer p2sh-p2wpkh/ (Uncaught TypeError TypeError : impossible de lire les propriétés d'undéfini (lecture de 'longueur'))

J'essaie de créer des transactions avec des scripts externes. Séparément, en utilisant chaque entrée séparément et en créant une transaction avec une seule entrée, tout fonctionne avec tous les scripts.

importer * en tant que bitcoin depuis 'bitcoinjs-lib' ; importer * comme ecc depuis 'tiny-secp256k1' ; importer { ECPairFactory } depuis 'ecpair' ; const ECPair = ECPairFactory(ecc); const réseau = 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, valeur : tx.value, } }); } function addP2shP2wpkhInput(psbt, tx, réseau, pubkey) { const payment = bitcoin.payments.p2sh({ racheter : bitcoin.payments.p2wpkh({ pubkey, réseau } ), réseau }); psbt.addInput({ hash : tx.hash, index : tx.index, redirectScript : 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, réseau, pubkey) { const { sortie } = bitcoin.payments.p2tr({ internalPubkey : toXOnly(pubkey), réseau }); psbt.addInput({ hash : tx.hash, index : inputIndex, WitnessUtxo : { script : output, value : tx.value, }, tapInternalKey : toXOnly(pubkey), }); } const psbt = new bitcoin.Psbt({ réseau }); soit totalAmount = 0 ; const paires de clés = [
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 = { hachage : "3399dfd038762c7f37d45a2db55568101b350472099e9a7b640aa917174c7e11", index : 0, valeur : 1155399, nonWitnessUtxo : "0200000001e02bdcc56cdf90e 71299f697c4fca0e968a8e1d415b58f947d2337754cc18046000000006b483045022100995eab8d8d13a500b768d866c84636840de85a4880f6d49157b3fb424bc 7c846022052dcf9bac0f12aa28e0c862c753ae4e0dc7dab023da6d4fd9c4033382935e1740121026ed517acfe148b1d012af4f99134fa24582e4018dfbfe7df1b1b748e2 ee7a83bffffffff0147a11100000000001976a9145b63c66657bf13babcbfa9425fb39cdbd28d38de88ac00000000" } const tx2 = { hachage : "91305d2e311c4c2eb4eaee825abd4c6da91b 71b0f1a98124fea116b5835283d1", index : 0, valeur : 1284858, témoinUtxo : { script : bitcoin.address .toOutputScript('tb1qs3hcrf2ls2cgsh0xvnlnsk546flrkugpaseuwf', réseau) } } const tx3 = { hash : "4db77e68c7cbf9a5b10f2d9e8c9ca0a9c5612abb9f9d2a62e72bb5f2d5a9dc26", index : 0, valeur : 1265769 } const tx4 = { hash : "138f2d616c36cf86c2500f308de198c7ce99ed3b0b1626f454caffb1bd266374", index : 0, valeur : 50000 } addP2pkhInput(psbt, tx1, réseau); addP2wpkhInput(psbt, tx2, réseau); addP2shP2wpkhInput (psbt, tx3, réseau, paires de clés.Clé publique); addP2trInput (psbt, tx4, réseau); totalAmount += tx1.value ; totalAmount += tx2.value ; totalAmount += tx3.value ; totalAmount += tx4.value ; psbt.addOutput({ adresse : 'mggZoTM2Qb2Zdw4ADspJM51Tj8hzPuzKb4', valeur : totalAmount - 1000, }); pour (soit i = 0; i