MCPcopy Create free account
hub / github.com/ElementsProject/elements / taproot_tree_helper

Function taproot_tree_helper

test/functional/test_framework/script.py:1003–1040  ·  view source on GitHub ↗
(scripts)

Source from the content-addressed store, hash-verified

1001 return TaggedHash("TapSighash/elements", TaprootSignatureMsg(*args, **kwargs))
1002
1003def taproot_tree_helper(scripts):
1004 if len(scripts) == 0:
1005 return ([], bytes())
1006 if len(scripts) == 1:
1007 # One entry: treat as a leaf
1008 script = scripts[0]
1009 assert(not callable(script))
1010 if isinstance(script, list):
1011 return taproot_tree_helper(script)
1012 assert(isinstance(script, tuple))
1013 version = LEAF_VERSION_TAPSCRIPT
1014 name = script[0]
1015 code = script[1]
1016 if len(script) == 3:
1017 version = script[2]
1018 assert version & 1 == 0
1019 assert isinstance(code, bytes)
1020 h = TaggedHash("TapLeaf/elements", bytes([version]) + ser_string(code))
1021 if name is None:
1022 return ([], h)
1023 return ([(name, version, code, bytes(), h)], h)
1024 elif len(scripts) == 2 and callable(scripts[1]):
1025 # Two entries, and the right one is a function
1026 left, left_h = taproot_tree_helper(scripts[0:1])
1027 right_h = scripts[1](left_h)
1028 left = [(name, version, script, control + right_h, leaf) for name, version, script, control, leaf in left]
1029 right = []
1030 else:
1031 # Two or more entries: descend into each side
1032 split_pos = len(scripts) // 2
1033 left, left_h = taproot_tree_helper(scripts[0:split_pos])
1034 right, right_h = taproot_tree_helper(scripts[split_pos:])
1035 left = [(name, version, script, control + right_h, leaf) for name, version, script, control, leaf in left]
1036 right = [(name, version, script, control + left_h, leaf) for name, version, script, control, leaf in right]
1037 if right_h < left_h:
1038 right_h, left_h = left_h, right_h
1039 h = TaggedHash("TapBranch/elements", left_h + right_h)
1040 return (left + right, h)
1041
1042# A TaprootInfo object has the following fields:
1043# - scriptPubKey: the scriptPubKey (witness v1 CScript)

Callers 1

taproot_constructFunction · 0.85

Calls 2

TaggedHashFunction · 0.70
ser_stringFunction · 0.70

Tested by

no test coverage detected