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))
