python – Failing to construct transaction that unlocks P2WSH

on

|

views

and

comments


I am trying to unlock the P2WSH output of the following transaction:

https://blockstream.info/testnet/tx/b435b180c936564cc902ce3d7011643836a74d671159d494f59536e70d977cd2

The script that I’m paying to is a non-standard script, which can be unlocked by pushing OP_5 OP_0 before evaluation.

Here’s the Python code I use to assemble the unlocking transaction:

from bitcoinutils.setup import setup
from bitcoinutils.utils import to_satoshis
from bitcoinutils.transactions import Transaction, TxInput, TxOutput
from bitcoinutils.keys import PrivateKey, P2wshAddress, P2wpkhAddress
from bitcoinutils.script import Script


def main():
    # always remember to setup the network
    setup('testnet')

    p2wsh_witness_script = Script.from_raw('0000018257615179547a75537a537a537a0079537a75527a527a7575615279008763537952795279615179517993517a75517a75619c77777777675279518763537952795279949c7777777767006868')

    fromAddress = P2wshAddress.from_script(p2wsh_witness_script)
    #print(fromAddress.to_string())

    toAddress = P2wpkhAddress.from_address("tb1qelplgcx5q7lj4mgk4sg8tlnq6njvamurvntwha")

    # set values
    txid = 'b435b180c936564cc902ce3d7011643836a74d671159d494f59536e70d977cd2'
    vout = 0
    amount = 0.00001400
    fee = 0.00000200


    # create transaction input from tx id of UTXO
    txin = TxInput(txid, vout)

    txOut1 = TxOutput(to_satoshis(amount - fee), toAddress.to_script_pub_key())

    tx = Transaction([txin], [txOut1], has_segwit=True)

    tx.witnesses.append(Script(['OP_5', 'OP_0', p2wsh_witness_script.to_hex()]))

    # print raw signed transaction ready to be broadcasted
    print("\nRaw signed transaction:\n" + tx.serialize())
    print("\nTxId:", tx.get_txid())


if __name__ == "__main__":
    main()

It produces the following serialized transaction:

02000000000101d27c970de73695f594d45911674da736386411703dce02c94c5636c980b135b40000000000ffffffff01b004000000000000160014cfc3f460d407bf2aed16ac1075fe60d4e4ceef83035500500000018257615179547a75537a537a537a0079537a75527a527a7575615279008763537952795279615179517993517a75517a75619c77777777675279518763537952795279949c777777776700686800000000

Unfortunately, while broadcasting, I get the following error:

sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}

I am not sure what I’m missing. Does the library serialize something wrong? Is the redeeming script too large?

Share this
Tags

Must-read

The Great Bitcoin Crash of 2024

Bitcoin Crash The cryptocurrency world faced the hell of early 2024 when the most popular Bitcoin crashed by over 80% in a matter of weeks,...

Bitcoin Gambling: A comprehensive guide in 2024

Bitcoin Gambling With online currencies rapidly gaining traditional acceptance, the intriguing convergence of the crypto-trek and gambling industries is taking place. Cryptocurrency gambling, which started...

The Rise of Bitcoin Extractor: A comprehensive guide 2024

Bitcoin Extractor  Crypto mining is resources-thirsty with investors in mining hardware and those investing in the resources needed as the main beneficiaries. In this sense,...

Recent articles

More like this