Skip to content

Commit 08067ae

Browse files
committed
Add script equivalent of functions in address.py
1 parent 8696888 commit 08067ae

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

test/functional/test_framework/script_util.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Useful Script constants and utils."""
6-
from test_framework.script import CScript
6+
from test_framework.script import CScript, hash160, sha256, OP_0, OP_DUP, OP_HASH160, OP_CHECKSIG, OP_EQUAL, OP_EQUALVERIFY
7+
from test_framework.util import hex_str_to_bytes
78

89
# To prevent a "tx-size-small" policy rule error, a transaction has to have a
910
# non-witness size of at least 82 bytes (MIN_STANDARD_TX_NONWITNESS_SIZE in
@@ -24,3 +25,59 @@
2425
# met.
2526
DUMMY_P2WPKH_SCRIPT = CScript([b'a' * 21])
2627
DUMMY_2_P2WPKH_SCRIPT = CScript([b'b' * 21])
28+
29+
def keyhash_to_p2pkh_script(hash, main = False):
30+
assert len(hash) == 20
31+
return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG])
32+
33+
def scripthash_to_p2sh_script(hash, main = False):
34+
assert len(hash) == 20
35+
return CScript([OP_HASH160, hash, OP_EQUAL])
36+
37+
def key_to_p2pkh_script(key, main = False):
38+
key = check_key(key)
39+
return keyhash_to_p2pkh_script(hash160(key), main)
40+
41+
def script_to_p2sh_script(script, main = False):
42+
script = check_script(script)
43+
return scripthash_to_p2sh_script(hash160(script), main)
44+
45+
def key_to_p2sh_p2wpkh_script(key, main = False):
46+
key = check_key(key)
47+
p2shscript = CScript([OP_0, hash160(key)])
48+
return script_to_p2sh_script(p2shscript, main)
49+
50+
def program_to_witness_script(version, program, main = False):
51+
if isinstance(program, str):
52+
program = hex_str_to_bytes(program)
53+
assert 0 <= version <= 16
54+
assert 2 <= len(program) <= 40
55+
assert version > 0 or len(program) in [20, 32]
56+
return CScript([version, program])
57+
58+
def script_to_p2wsh_script(script, main = False):
59+
script = check_script(script)
60+
return program_to_witness_script(0, sha256(script), main)
61+
62+
def key_to_p2wpkh_script(key, main = False):
63+
key = check_key(key)
64+
return program_to_witness_script(0, hash160(key), main)
65+
66+
def script_to_p2sh_p2wsh_script(script, main = False):
67+
script = check_script(script)
68+
p2shscript = CScript([OP_0, sha256(script)])
69+
return script_to_p2sh_script(p2shscript, main)
70+
71+
def check_key(key):
72+
if isinstance(key, str):
73+
key = hex_str_to_bytes(key) # Assuming this is hex string
74+
if isinstance(key, bytes) and (len(key) == 33 or len(key) == 65):
75+
return key
76+
assert False
77+
78+
def check_script(script):
79+
if isinstance(script, str):
80+
script = hex_str_to_bytes(script) # Assuming this is hex string
81+
if isinstance(script, bytes) or isinstance(script, CScript):
82+
return script
83+
assert False

0 commit comments

Comments
 (0)