Skip to content

Commit 4bcef32

Browse files
committed
Merge bitcoin/bitcoin#28312: test: fix keys_to_multisig_script (P2MS) helper for n/k > 16
5cf0a1f test: add `createmultisig` P2MS encoding test for all n (1..20) (Sebastian Falbesoner) 0570d2c test: add unit test for `keys_to_multisig_script` (Sebastian Falbesoner) 0c41fc3 test: fix `keys_to_multisig_script` (P2MS) helper for n/k > 16 (Sebastian Falbesoner) Pull request description: While reviewing #28307, I noticed that the test framework's `key_to_multisig_script` helper (introduced in #23305) is broken for pubkey count (n) and threshold (k) values larger than 16. This is due to the implementation currently enforcing a direct single-byte data push (using `CScriptOp.encode_op_n`), which obviously fails for values 17+. Fix that by passing the numbers directly to the CScript list, where it's automatically converted to minimally-encoded pushes (see class method `CScript.__coerce_instance`, branch `isinstance(other, int)`). The second commit adds a unit test to ensure that the encoding is correct. ACKs for top commit: achow101: ACK 5cf0a1f tdb3: ACK 5cf0a1f rkrux: reACK [5cf0a1f](bitcoin/bitcoin@5cf0a1f) Tree-SHA512: 4168a165c3f483ec8e37a27dba1628a7ea0063545a2b7e74d9e20d753fddd7e33d37e1a190434fa6dca39adf9eef5d0211f7a0c1c7b44979f0a3bb350e267562
2 parents 808898f + 5cf0a1f commit 4bcef32

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

test/functional/feature_framework_unit_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"crypto.ripemd160",
2828
"crypto.secp256k1",
2929
"script",
30+
"script_util",
3031
"segwit_addr",
3132
"wallet_util",
3233
]

test/functional/rpc_createmultisig.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from test_framework.descriptors import descsum_create, drop_origins
1313
from test_framework.key import ECPubKey
1414
from test_framework.messages import COIN
15+
from test_framework.script_util import keys_to_multisig_script
1516
from test_framework.test_framework import BitcoinTestFramework
1617
from test_framework.util import (
1718
assert_raises_rpc_error,
@@ -69,6 +70,16 @@ def run_test(self):
6970
# Check that bech32m is currently not allowed
7071
assert_raises_rpc_error(-5, "createmultisig cannot create bech32m multisig addresses", self.nodes[0].createmultisig, 2, self.pub, "bech32m")
7172

73+
self.log.info('Check correct encoding of multisig script for all n (1..20)')
74+
for nkeys in range(1, 20+1):
75+
keys = [self.pub[0]]*nkeys
76+
expected_ms_script = keys_to_multisig_script(keys, k=nkeys) # simply use n-of-n
77+
# note that the 'legacy' address type fails for n values larger than 15
78+
# due to exceeding the P2SH size limit (520 bytes), so we use 'bech32' instead
79+
# (for the purpose of this encoding test, we don't care about the resulting address)
80+
res = self.nodes[0].createmultisig(nrequired=nkeys, keys=keys, address_type='bech32')
81+
assert_equal(res['redeemScript'], expected_ms_script.hex())
82+
7283
def check_addmultisigaddress_errors(self):
7384
if self.options.descriptors:
7485
return

test/functional/test_framework/script_util.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
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+
import unittest
7+
68
from test_framework.script import (
79
CScript,
8-
CScriptOp,
910
OP_0,
11+
OP_15,
12+
OP_16,
1013
OP_CHECKMULTISIG,
1114
OP_CHECKSIG,
1215
OP_DUP,
@@ -49,10 +52,8 @@ def keys_to_multisig_script(keys, *, k=None):
4952
if k is None: # n-of-n multisig by default
5053
k = n
5154
assert k <= n
52-
op_k = CScriptOp.encode_op_n(k)
53-
op_n = CScriptOp.encode_op_n(n)
5455
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])
5657

5758

5859
def keyhash_to_p2pkh_script(hash):
@@ -125,3 +126,19 @@ def check_script(script):
125126
if isinstance(script, bytes) or isinstance(script, CScript):
126127
return script
127128
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

Comments
 (0)