karask / python-bitcoin-utils

Library to interact with the Bitcoin network. Ideal for low-level learning and experimenting.
MIT License
262 stars 99 forks source link

[BUG] Bad computation of taproot hash #105

Open ramonamela opened 5 days ago

ramonamela commented 5 days ago

When hashing two branches in the taproot tree, the one containing the script that is going to be executed should always go first (we cannot just concatenate the two branches in order). Then, the current implementation for the example [ [A, B], C ] works for A and B. This can be seen in this code snippet: for leaf in level: if isinstance(leaf, list): traverse_level(leaf) else: if traversed == target_leaf_index: traversed += 1 continue tagged_hash = tapleaf_tagged_hash(leaf) merkle_path.append(tagged_hash) We should keep track of the branch containing the current index and put it first. Considering this, it is no longer practical to have a list, it's better to compute the hash on the fly and change the concatenation order depending on the case. I opened a PR to solve this problem: https://github.com/karask/python-bitcoin-utils/pull/104/files