|
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 | +import unittest |
| 7 | + |
6 | 8 | from test_framework.script import (
|
7 | 9 | CScript,
|
8 |
| - CScriptOp, |
9 | 10 | OP_0,
|
| 11 | + OP_15, |
| 12 | + OP_16, |
10 | 13 | OP_CHECKMULTISIG,
|
11 | 14 | OP_CHECKSIG,
|
12 | 15 | OP_DUP,
|
@@ -49,10 +52,8 @@ def keys_to_multisig_script(keys, *, k=None):
|
49 | 52 | if k is None: # n-of-n multisig by default
|
50 | 53 | k = n
|
51 | 54 | assert k <= n
|
52 |
| - op_k = CScriptOp.encode_op_n(k) |
53 |
| - op_n = CScriptOp.encode_op_n(n) |
54 | 55 | checked_keys = [check_key(key) for key in keys]
|
55 |
| - return CScript([op_k] + checked_keys + [op_n, OP_CHECKMULTISIG]) |
| 56 | + return CScript([k] + checked_keys + [n, OP_CHECKMULTISIG]) |
56 | 57 |
|
57 | 58 |
|
58 | 59 | def keyhash_to_p2pkh_script(hash):
|
@@ -125,3 +126,19 @@ def check_script(script):
|
125 | 126 | if isinstance(script, bytes) or isinstance(script, CScript):
|
126 | 127 | return script
|
127 | 128 | assert False
|
| 129 | + |
| 130 | + |
| 131 | +class TestFrameworkScriptUtil(unittest.TestCase): |
| 132 | + def test_multisig(self): |
| 133 | + fake_pubkey = bytes([0]*33) |
| 134 | + # check correct encoding of P2MS script with n,k <= 16 |
| 135 | + normal_ms_script = keys_to_multisig_script([fake_pubkey]*16, k=15) |
| 136 | + self.assertEqual(len(normal_ms_script), 1 + 16*34 + 1 + 1) |
| 137 | + self.assertTrue(normal_ms_script.startswith(bytes([OP_15]))) |
| 138 | + self.assertTrue(normal_ms_script.endswith(bytes([OP_16, OP_CHECKMULTISIG]))) |
| 139 | + |
| 140 | + # check correct encoding of P2MS script with n,k > 16 |
| 141 | + max_ms_script = keys_to_multisig_script([fake_pubkey]*20, k=19) |
| 142 | + self.assertEqual(len(max_ms_script), 2 + 20*34 + 2 + 1) |
| 143 | + self.assertTrue(max_ms_script.startswith(bytes([1, 19]))) # using OP_PUSH1 |
| 144 | + self.assertTrue(max_ms_script.endswith(bytes([1, 20, OP_CHECKMULTISIG]))) |
0 commit comments