Get the scriptPubKey from a Taproot tackle

0
69


I’ve this operate that converts a Bitcoin non-public key as an integer to a public key and an tackle.

def get_seg_v1_from_pkey(imported_key):
    imported_key_toint = int(imported_key)
    privKeytoHex = imported_key_toint.to_bytes(32, 'large').hex()
    prv = HDKey(privKeytoHex)
    prv_tnet = TNetHDKey(privKeytoHex)
    pubkey = prv.public_hex
    pub_tnet = prv_tnet.public_hex
    model = 0x01
    tackle = program_to_witness(model, pubkey[2:], primary=True)
    address_testnet = pubkeyhash_to_addr_bech32(pubkeyhash=pub_tnet[2:], prefix='tb', witver=1, separator="1", checksum_xor=0x2bc830a3)
    return pubkey, tackle, address_testnet  

HDKey imports the integer into a correct non-public key object like what’s utilized in https://github.com/bitcoinops/taproot-workshop. TNetHDKey is a customized operate I wrote that does the identical factor as HDKey however it shops the non-public key in a means that makes it simply convertible to a testnet tackle.

I wish to get the scriptPubKey from an tackle like on this operate in https://github.com/bitcoinops/taproot-workshop:

def create_spending_transaction(self, txid, model=1, nSequence=0):
    """Assemble a CTransaction object that spends the primary ouput from txid."""
    # Assemble transaction
    spending_tx = CTransaction()

    # Populate the transaction model
    spending_tx.nVersion = model

    # Populate the locktime
    spending_tx.nLockTime = 0

    # Populate the transaction inputs
    outpoint = COutPoint(int(txid, 16), 0)
    spending_tx_in = CTxIn(outpoint=outpoint, nSequence=nSequence)
    spending_tx.vin = [spending_tx_in]

    # Generate new Bitcoin Core pockets tackle
    dest_addr = self.nodes[0].getnewaddress(address_type="bech32")
    scriptpubkey = bytes.fromhex(self.nodes[0].getaddressinfo(dest_addr)['scriptPubKey'])

    # Full output which returns 0.5 BTC to Bitcoin Core pockets
    amount_sat = int(0.5 * 100_000_000)
    dest_output = CTxOut(nValue=amount_sat, scriptPubKey=scriptpubkey)
    spending_tx.vout = [dest_output]

    return spending_tx  

Discover these strains within the above operate:

# Generate new Bitcoin Core pockets tackle
dest_addr = self.nodes[0].getnewaddress(address_type="bech32")
scriptpubkey = bytes.fromhex(self.nodes[0].getaddressinfo(dest_addr)['scriptPubKey'])  

self.nodes[0] refers to a operating occasion of bitcoind in your pc. I wish to replicate this operate with out requiring a operating occasion of bitcoind. In my model of this operate I plan on passing in dest_addr as a result of I will not have a bitcoind occasion to generate it. Is there a method to get a scriptPubKey from a uncooked segwit v1 tackle?

LEAVE A REPLY

Please enter your comment!
Please enter your name here