Sunday, December 22, 2024

Unable to spend a script path utilizing buidl python lib

I have been struggling to determine how one can spend a selected script path utilizing buidl python lib. At the moment I assume the issue lies in how I assemble p2tr deal with or there is a bug (impossible) in how the management block is calculated.

The error I am receiving when broadcasting a spending transaction is: Witness program hash mismatch

You’ll find my code within the gist or beneath:

from buidl.hd import HDPrivateKey
from buidl.ecc import PrivateKey
from buidl.taproot import TapLeaf, TapBranch, TapScript
from buidl.script import Script
from buidl.op import *
from buidl.tx import Tx, TxIn, TxOut
from buidl.taproot import ControlBlock
from buidl.witness import Witness
from buidl.script import P2WPKHScriptPubKey, ScriptPubKey, decode_bech32

pvt_key = PrivateKey(12345, community="regtest")
internal_pvtkey = PrivateKey(3333, community="regtest")
internal_pubkey = internal_pvtkey.level

# leaf_a = Script([op_1, op_add, op_2, op_equal])
# leaf_b = Script([op_2, op_add, op_4, op_equal])
# leaf_b = Script([op_1, op_add, op_5, op_equal])
script_a = TapScript([81, 147, 82, 135])
script_b = TapScript([82, 147, 84, 135])
script_c = TapScript([81, 147, 85, 135])

leaf_a = TapLeaf(script_a)
leaf_b = TapLeaf(script_b)
leaf_c = TapLeaf(script_c)

branch_ab = TapBranch(leaf_a, leaf_b)
branch_abc = TapBranch(branch_ab, leaf_c)

deal with = branch_abc.external_pubkey(internal_pubkey).p2tr_address(community="regtest")
print("Deposit funds to:", deal with)

# SPEND PATH B
quantity = 1_000_000
cb = branch_abc.control_block(internal_pubkey, leaf_b)

tx_in = TxIn(bytes.fromhex("07fdfaf3541c26d5fc18f644f338d8ac4e47f7bcc83c41192741990d150b8d19"), 1)
tx_in.witness.gadgets = [encode_num(2), leaf_b.tap_script.raw_serialize(), cb.serialize()]
tx_ins = [tx_in]
tx_outs = [TxOut(998000, P2TRScriptPubKey(decode_bech32("bcrt1py7e8e0q7pdkxwyp2tjn65k4kd5kgch0aqe03jzjczn96xat6nwjswsu892")[2]))]
tx = Tx(2, tx_ins, tx_outs, community="regtest", segwit=True)
tx.serialize().hex()
# 02000000000101941ea2c9c9236e92e0d94530d0de3694012f0d3902399dc680294ef2e82a0e780100000000ffffffff01301b0f0000000000160014904946363de1f2cb5ce439e5ed038492051b2785030101045293538841c04cecb33a915e2c3b189eb6ee45fa4eb3eddeff09729945c600f893bd4381b294c95c39c9307263577e8ed45b4d6ad91cf6f47f42476e0e40676b1f028be1360600000000
# I all the time get: `non-mandatory-script-verify-flag (Witness program hash mismatch)`

Please not that I used to be in a position to efficiently create and spend a p2tr deal with (utilizing similar tapscripts) with bitcointx library, therefore why I assume I am probably not utilizing the buidl library accurately or am misunderstanding one thing.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles