python-bitcoin-utils icon indicating copy to clipboard operation
python-bitcoin-utils copied to clipboard

Fix taproot hash computation

Open ramonamela opened this issue 1 year ago • 1 comments

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. The problem is that for a huge tree with an arbitrary amount of leaves, the implementation only works for the two most leftmost leaves. The tests were correct since they checked spending from the first leaves. They'd failed when spending from C.

ramonamela avatar Jul 02 '24 13:07 ramonamela

Hi @ramonamela , I will check this when I am back from holidays. Thanks for the contribution.

karask avatar Jul 03 '24 12:07 karask

Just an update here. I've tested with a taproot tree with more than 500 scripts and it seems to work on testnet. Enjoy you holidays :)

ramonamela avatar Jul 19 '24 10:07 ramonamela

I was solving two problems in the same PR but I think one has been solved by an other PR. I rebased my master so there is no longer conflicts. The key is in this piece of code: if len(level) == 1: return traverse_level(level[0]) if len(level) == 2: a, a1 = traverse_level(level[0]) b, b1 = traverse_level(level[1]) if a1: return (a + b), True if b1: return (b + a), True return tapbranch_tagged_hash(a, b), False As explained in this issue: https://github.com/karask/python-bitcoin-utils/issues/105

ramonamela avatar Oct 18 '24 09:10 ramonamela