0

I am writing some code implementing taproot address creation where a script path is present and I have a quick question surrounding the reference code.

The BIP-341 reference method taproot_tree_helper(script_tree) will recursively call itself if the parameter is not a tuple (leaf node). When it calls itself, it passes in it's child nodes by:

left, left_h = taproot_tree_helper(script_tree[0])
right, right_h = taproot_tree_helper(script_tree[1])

What should happen if a node only contains a single child (where script_tree[1] does not exist)? Should it pass in the same node twice for both left and right or should the tree be constructed in a way that this cannot happen?

Thanks

Keijyu
  • 85
  • 4
  • 1
    Does this answer your question? [Single-leaf script-path example](https://bitcoin.stackexchange.com/questions/110240/single-leaf-script-path-example) – Pieter Wuille Oct 30 '21 at 01:51

1 Answers1

2

Unlike transaction Merkle trees, the taproot script tree does not need to be balanced; i.e., not every leaf needs to be at the same distance from the root.

So there is no need for a "node" with just a single child, and such nodes are in fact not possible. Instead, just construct a tree where the would-be single child is placed one level higher in the tree.

Pieter Wuille
  • 98,249
  • 9
  • 183
  • 287
  • I see, I was working under the assumption that the tree was balanced and arbitrary data was used to fill in left over leaves but this makes more sense. Thanks – Keijyu Oct 30 '21 at 13:14