|
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 |
| 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 |
7 | 8 |
|
8 | 9 | # To prevent a "tx-size-small" policy rule error, a transaction has to have a
|
9 | 10 | # non-witness size of at least 82 bytes (MIN_STANDARD_TX_NONWITNESS_SIZE in
|
|
24 | 25 | # met.
|
25 | 26 | DUMMY_P2WPKH_SCRIPT = CScript([b'a' * 21])
|
26 | 27 | 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