I'm using the Bitcoin Functional Test framework to understand how SegWit works by analyzing and building a P2SH-P2WPKH transaction.
When I run my code the transaction gets created, but when I inspect the Tx Hex, I cannot see the SegWit marker 00 on the 5th byte nor the segWitFlag, so probably I'm doing something wrong. Can anyone point out what could be the correct way to do it? Thanks so much in advance
Here is my Python Code:
key = ECKey()
key.generate() # generate private key
pubkey = key.get_pubkey().get_bytes()
pubkey_hash = hash160(pubkey)
witness_script = keyhash_to_p2pkh_script(pubkey_hash)
hashed_script = hash160(witness_script)
script_sig = CScript([ OP_0, pubkey_hash ])
script_pubkey = scripthash_to_p2sh_script(hashed_script)
destination_address = byte_to_base58(hashed_script, 196) # 196, Bitcoin testnet script hash
self.log.info("Witness script: {}".format(repr(witness_script)))
self.log.info("scriptSig): {}".format(repr(script_sig)))
self.log.info("scriptPubKey: {}".format(repr(script_pubkey)))
self.log.info("Destination Address: {}".format(repr(destination_address)))
# Create transaction
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int(utxo['txid'],16), int(utxo['vout'])), scriptSig=script_sig))
tx.vout.append(CTxOut((int(utxo['amount']) * COIN) - 1000, script_pubkey))
tx.rehash()
self.log.info("Transacción: {}".format(tx))
# Add witnessscript
tx.wit.vtxinwit.append(CTxInWitness())
tx.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
self.log.info("Sign transaction")
tx_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())["hex"]
self.log.info("Transaction HEX: {}".format(tx_hex))
decrawtx = self.nodes[0].decoderawtransaction(tx_hex, True)
descriptor = decrawtx['vout'][0]['scriptPubKey']['desc']
# Get descriptor
self.log.info("descriptor: {}".format(descriptor))