February 2, 2026

Creating a P2SH-P2WPKH using Bitcoin Functional Test Framework

I’m using the Bitcoin Functional Test framework to understand SegWit. In particular I’m trying to build a P2SH-P2WPKH transaction. I have read Jimmy Songs book and other sources, but I’m still doubtful on how to structure the Tx to be a Segwit transaction. When I run my code the Tx gets created, but when I inspect the Tx Hex, I cannot see the SegWit marker 00 on the 5th byte. Can anyone point out what I’m doing wrong? Thanks

Here is my Python Code:

        key = ECKey()
        key.generate() 
        pubkey = key.get_pubkey().get_bytes()
        pubkey_hash = hash160(pubkey)
        # redeeem_script as per segwit rule
        redeeem_script = CScript([ OP_0, pubkey_hash ])
        hashed_script = hash160(redeeem_script)
        script_pubkey = scripthash_to_p2sh_script(hashed_script)
        destination_address = byte_to_base58(hashed_script, 196) # 196, Bitcoin testnet script hash
        
        self.log.info("redeeemScript(scriptSig): {}".format(repr(redeeem_script)))
        self.log.info("scriptPubKey: {}".format(repr(script_pubkey)))
        self.log.info("Destination Address: {}".format(repr(destination_address)))

        # First create a witness output for use in the test.
        tx = CTransaction()
        tx.vin.append(CTxIn(COutPoint(int(utxo['txid'],16), int(utxo['vout'])), redeeem_script))
        tx.vout.append(CTxOut((int(utxo['amount']) * COIN) - 1000, script_pubkey))
        tx.rehash()
        self.log.info("Transacción: {}".format(tx))

        self.log.info("Sign transaction")
        tx_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())["hex"]
        self.log.info("Transacción HEX: {}".format(tx_hex))
Previous Article

Mobius Capital Founder Explains Why Bitcoin Is Rallying Amid Russia-Ukraine War

Next Article

Which way BITCOIN is heading? A or B?

You might be interested in …

Onlyfans Founder’s Startup, Hbar Foundation Bid for Tiktok

OnlyFans founder’s startup is making waves with a bold bid for TikTok through the HBAR Foundation. This move aims to leverage blockchain technology to enhance user privacy and create new avenues for content monetization in a rapidly evolving digital landscape.