3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Useful Script constants and utils."""
6
- from test_framework .script import CScript , hash160 , sha256 , OP_0 , OP_DUP , OP_HASH160 , OP_CHECKSIG , OP_EQUAL , OP_EQUALVERIFY
6
+ from test_framework .script import (
7
+ CScript ,
8
+ hash160 ,
9
+ sha256 ,
10
+ OP_0 ,
11
+ OP_DUP ,
12
+ OP_HASH160 ,
13
+ OP_CHECKSIG ,
14
+ OP_EQUAL ,
15
+ OP_EQUALVERIFY ,
16
+ )
7
17
8
18
# To prevent a "tx-size-small" policy rule error, a transaction has to have a
9
19
# non-witness size of at least 82 bytes (MIN_STANDARD_TX_NONWITNESS_SIZE in
25
35
DUMMY_P2WPKH_SCRIPT = CScript ([b'a' * 21 ])
26
36
DUMMY_2_P2WPKH_SCRIPT = CScript ([b'b' * 21 ])
27
37
38
+
28
39
def keyhash_to_p2pkh_script (hash ):
29
40
assert len (hash ) == 20
30
41
return CScript ([OP_DUP , OP_HASH160 , hash , OP_EQUALVERIFY , OP_CHECKSIG ])
31
42
43
+
32
44
def scripthash_to_p2sh_script (hash ):
33
45
assert len (hash ) == 20
34
46
return CScript ([OP_HASH160 , hash , OP_EQUAL ])
35
47
48
+
36
49
def key_to_p2pkh_script (key ):
37
50
key = check_key (key )
38
51
return keyhash_to_p2pkh_script (hash160 (key ))
39
52
53
+
40
54
def script_to_p2sh_script (script ):
41
55
script = check_script (script )
42
56
return scripthash_to_p2sh_script (hash160 (script ))
43
57
58
+
44
59
def key_to_p2sh_p2wpkh_script (key ):
45
60
key = check_key (key )
46
61
p2shscript = CScript ([OP_0 , hash160 (key )])
47
62
return script_to_p2sh_script (p2shscript )
48
63
64
+
49
65
def program_to_witness_script (version , program ):
50
66
if isinstance (program , str ):
51
67
program = bytes .fromhex (program )
@@ -54,29 +70,34 @@ def program_to_witness_script(version, program):
54
70
assert version > 0 or len (program ) in [20 , 32 ]
55
71
return CScript ([version , program ])
56
72
73
+
57
74
def script_to_p2wsh_script (script ):
58
75
script = check_script (script )
59
76
return program_to_witness_script (0 , sha256 (script ))
60
77
78
+
61
79
def key_to_p2wpkh_script (key ):
62
80
key = check_key (key )
63
81
return program_to_witness_script (0 , hash160 (key ))
64
82
83
+
65
84
def script_to_p2sh_p2wsh_script (script ):
66
85
script = check_script (script )
67
86
p2shscript = CScript ([OP_0 , sha256 (script )])
68
87
return script_to_p2sh_script (p2shscript )
69
88
89
+
70
90
def check_key (key ):
71
91
if isinstance (key , str ):
72
- key = bytes .fromhex (key ) # Assuming this is hex string
92
+ key = bytes .fromhex (key ) # Assuming this is hex string
73
93
if isinstance (key , bytes ) and (len (key ) == 33 or len (key ) == 65 ):
74
94
return key
75
95
assert False
76
96
97
+
77
98
def check_script (script ):
78
99
if isinstance (script , str ):
79
- script = bytes .fromhex (script ) # Assuming this is hex string
100
+ script = bytes .fromhex (script ) # Assuming this is hex string
80
101
if isinstance (script , bytes ) or isinstance (script , CScript ):
81
102
return script
82
103
assert False
0 commit comments