MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / taproot_tree_helper

Function taproot_tree_helper

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

Source from the content-addressed store, hash-verified

861 return TaggedHash("TapSighash", TaprootSignatureMsg(*args, **kwargs))
862
863def taproot_tree_helper(scripts):
864 if len(scripts) == 0:
865 return ([], bytes())
866 if len(scripts) == 1:
867 # One entry: treat as a leaf
868 script = scripts[0]
869 assert not callable(script)
870 if isinstance(script, list):
871 return taproot_tree_helper(script)
872 assert isinstance(script, tuple)
873 version = LEAF_VERSION_TAPSCRIPT
874 name = script[0]
875 code = script[1]
876 if len(script) == 3:
877 version = script[2]
878 assert_equal(version & 1, 0)
879 assert isinstance(code, bytes)
880 h = TaggedHash("TapLeaf", bytes([version]) + ser_string(code))
881 if name is None:
882 return ([], h)
883 return ([(name, version, code, bytes(), h)], h)
884 elif len(scripts) == 2 and callable(scripts[1]):
885 # Two entries, and the right one is a function
886 left, left_h = taproot_tree_helper(scripts[0:1])
887 right_h = scripts[1](left_h)
888 left = [(name, version, script, control + right_h, leaf) for name, version, script, control, leaf in left]
889 right = []
890 else:
891 # Two or more entries: descend into each side
892 split_pos = len(scripts) // 2
893 left, left_h = taproot_tree_helper(scripts[0:split_pos])
894 right, right_h = taproot_tree_helper(scripts[split_pos:])
895 left = [(name, version, script, control + right_h, leaf) for name, version, script, control, leaf in left]
896 right = [(name, version, script, control + left_h, leaf) for name, version, script, control, leaf in right]
897 if right_h < left_h:
898 right_h, left_h = left_h, right_h
899 h = TaggedHash("TapBranch", left_h + right_h)
900 return (left + right, h)
901
902# A TaprootInfo object has the following fields:
903# - scriptPubKey: the scriptPubKey (witness v1 CScript)

Callers 1

taproot_constructFunction · 0.85

Calls 3

assert_equalFunction · 0.85
ser_stringFunction · 0.85
TaggedHashFunction · 0.70

Tested by

no test coverage detected