|
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 |
|
28 |
| -def keyhash_to_p2pkh_script(hash, main = False): |
| 38 | + |
| 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 |
|
32 |
| -def scripthash_to_p2sh_script(hash, main = False): |
| 43 | + |
| 44 | +def scripthash_to_p2sh_script(hash): |
33 | 45 | assert len(hash) == 20
|
34 | 46 | return CScript([OP_HASH160, hash, OP_EQUAL])
|
35 | 47 |
|
36 |
| -def key_to_p2pkh_script(key, main = False): |
| 48 | + |
| 49 | +def key_to_p2pkh_script(key): |
37 | 50 | key = check_key(key)
|
38 |
| - return keyhash_to_p2pkh_script(hash160(key), main) |
| 51 | + return keyhash_to_p2pkh_script(hash160(key)) |
39 | 52 |
|
40 |
| -def script_to_p2sh_script(script, main = False): |
| 53 | + |
| 54 | +def script_to_p2sh_script(script): |
41 | 55 | script = check_script(script)
|
42 |
| - return scripthash_to_p2sh_script(hash160(script), main) |
| 56 | + return scripthash_to_p2sh_script(hash160(script)) |
| 57 | + |
43 | 58 |
|
44 |
| -def key_to_p2sh_p2wpkh_script(key, main = False): |
| 59 | +def key_to_p2sh_p2wpkh_script(key): |
45 | 60 | key = check_key(key)
|
46 | 61 | p2shscript = CScript([OP_0, hash160(key)])
|
47 |
| - return script_to_p2sh_script(p2shscript, main) |
| 62 | + return script_to_p2sh_script(p2shscript) |
| 63 | + |
48 | 64 |
|
49 |
| -def program_to_witness_script(version, program, main = False): |
| 65 | +def program_to_witness_script(version, program): |
50 | 66 | if isinstance(program, str):
|
51 | 67 | program = bytes.fromhex(program)
|
52 | 68 | assert 0 <= version <= 16
|
53 | 69 | assert 2 <= len(program) <= 40
|
54 | 70 | assert version > 0 or len(program) in [20, 32]
|
55 | 71 | return CScript([version, program])
|
56 | 72 |
|
57 |
| -def script_to_p2wsh_script(script, main = False): |
| 73 | + |
| 74 | +def script_to_p2wsh_script(script): |
58 | 75 | script = check_script(script)
|
59 |
| - return program_to_witness_script(0, sha256(script), main) |
| 76 | + return program_to_witness_script(0, sha256(script)) |
| 77 | + |
60 | 78 |
|
61 |
| -def key_to_p2wpkh_script(key, main = False): |
| 79 | +def key_to_p2wpkh_script(key): |
62 | 80 | key = check_key(key)
|
63 |
| - return program_to_witness_script(0, hash160(key), main) |
| 81 | + return program_to_witness_script(0, hash160(key)) |
| 82 | + |
64 | 83 |
|
65 |
| -def script_to_p2sh_p2wsh_script(script, main = False): |
| 84 | +def script_to_p2sh_p2wsh_script(script): |
66 | 85 | script = check_script(script)
|
67 | 86 | p2shscript = CScript([OP_0, sha256(script)])
|
68 |
| - return script_to_p2sh_script(p2shscript, main) |
| 87 | + return script_to_p2sh_script(p2shscript) |
| 88 | + |
69 | 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