forex.pm forex forum binary options trade - Binance - Сryptocurrency exchanges - Creating a P2SH-P2WPKH using Bitcoin Functional Test Framework
  • Welcome to forex.pm forex forum binary options trade. Please login or sign up.
 

Creating a P2SH-P2WPKH using Bitcoin Functional Test Framework

Started by Bitcoin, Mar 17, 2022, 04:54 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bitcoin

Creating a P2SH-P2WPKH using Bitcoin Functional Test Framework

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

Source: Creating a P2SH-P2WPKH using Bitcoin Functional Test Framework