From 8ea1eaac2d01b1632c32231d1b3bbd719710f66f Mon Sep 17 00:00:00 2001 From: notmike Date: Fri, 26 Jun 2026 19:58:28 -0600 Subject: [PATCH] bip360: move collect_leaf_hashes() to the test code section --- bip-0360/ref-impl/python/p2mr.py | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/bip-0360/ref-impl/python/p2mr.py b/bip-0360/ref-impl/python/p2mr.py index a8dc64f1..bd793234 100644 --- a/bip-0360/ref-impl/python/p2mr.py +++ b/bip-0360/ref-impl/python/p2mr.py @@ -88,23 +88,6 @@ def tapbranch_hash(left: str, right: str) -> bytes: return tagged_hash("TapBranch", b"".join(sorted((h2b(right) + h2b(left))))) -def collect_leaf_hashes(tree: ScriptTree) -> List[str]: - """Recursively collect leaf hashes in order (for verification)""" - if isinstance(tree, dict): # Leaf - version = f"{tree['leafVersion']:x}" - script = tree["script"] - return [tapleaf_hash(script=script, tapleaf_ver=version)] - - elif isinstance(tree, list): # Branch: recurse on children - hashes: List[str] = [] - for sub in tree: - hashes.extend(collect_leaf_hashes(sub)) - return hashes - - else: - raise ValueError("Invalid tree node") - - def compute_merkle_root(tree: ScriptTree) -> str: """Recursively compute script tree merkle root""" if isinstance(tree, dict): # Leaf @@ -280,6 +263,23 @@ def encode(hrp, witver, witprog): # # BIP-360 Test Code # +def collect_leaf_hashes(tree: ScriptTree) -> List[str]: + """Recursively collect leaf hashes in order (for verification)""" + if isinstance(tree, dict): # Leaf + version = f"{tree['leafVersion']:x}" + script = tree["script"] + return [tapleaf_hash(script=script, tapleaf_ver=version)] + + elif isinstance(tree, list): # Branch: recurse on children + hashes: List[str] = [] + for sub in tree: + hashes.extend(collect_leaf_hashes(sub)) + return hashes + + else: + raise ValueError("Invalid tree node") + + def walk_script_tree_paths( script_tree: ScriptTree, path: int = 0, depth: int = 0 ) -> List[int]: