Skip to content

Commit 7503593

Browse files
MarcoFalkeknst
authored andcommitted
Merge bitcoin#23305: test: refactor: add script_util helper for creating bare multisig scripts
4718897 test: add script_util helper for creating bare multisig scripts (Sebastian Falbesoner) Pull request description: This PR is a follow-up to bitcoin#22363 and bitcoin#23118 and introduces a helper `keys_to_multisig_script` for creating bare multisig outputs in the form of ``` OP_K PubKey1 PubKey2 ... PubKeyN OP_N OP_CHECKMULTISIG ``` The function takes a list of pubkeys (both hex- and byte-strings are accepted due to the `script_util.check_key` helper being used internally) and optionally a threshold _k_. If no threshold is passed, a n-of-n multisig output is created, with _n_ being the number of passed pubkeys. ACKs for top commit: shaavan: utACK 4718897 rajarshimaitra: tACK bitcoin@4718897 Tree-SHA512: b452d8a75b0d17316b66ac4ed4c6893fe59c7c417719931d4cd3955161f59afca43503cd09b83a35b5a252a122eb3f0fbb9da9f0e7c944cf8da572a02219ed9d
1 parent 6d1b1a3 commit 7503593

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

test/functional/mempool_accept.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
from test_framework.script import (
2525
CScript,
2626
OP_0,
27-
OP_2,
28-
OP_3,
29-
OP_CHECKMULTISIG,
3027
OP_HASH160,
3128
OP_RETURN,
3229
)
3330
from test_framework.script_util import (
31+
keys_to_multisig_script,
3432
script_to_p2sh_script,
3533
)
3634
from test_framework.util import (
@@ -275,7 +273,7 @@ def run_test(self):
275273
key = ECKey()
276274
key.generate()
277275
pubkey = key.get_pubkey().get_bytes()
278-
tx.vout[0].scriptPubKey = CScript([OP_2, pubkey, pubkey, pubkey, OP_3, OP_CHECKMULTISIG]) # Some bare multisig script (2-of-3)
276+
tx.vout[0].scriptPubKey = keys_to_multisig_script([pubkey] * 3, k=2) # Some bare multisig script (2-of-3)
279277
self.check_mempool_result(
280278
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bare-multisig'}],
281279
rawtxs=[tx.serialize().hex()],

test/functional/test_framework/script_util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"""Useful Script constants and utils."""
66
from test_framework.script import (
77
CScript,
8+
CScriptOp,
9+
OP_CHECKMULTISIG,
810
OP_CHECKSIG,
911
OP_DUP,
1012
OP_EQUAL,
@@ -39,6 +41,17 @@ def key_to_p2pk_script(key):
3941
return CScript([key, OP_CHECKSIG])
4042

4143

44+
def keys_to_multisig_script(keys, *, k=None):
45+
n = len(keys)
46+
if k is None: # n-of-n multisig by default
47+
k = n
48+
assert k <= n
49+
op_k = CScriptOp.encode_op_n(k)
50+
op_n = CScriptOp.encode_op_n(n)
51+
checked_keys = [check_key(key) for key in keys]
52+
return CScript([op_k] + checked_keys + [op_n, OP_CHECKMULTISIG])
53+
54+
4255
def keyhash_to_p2pkh_script(hash):
4356
assert len(hash) == 20
4457
return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG])

test/functional/test_framework/wallet_util.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111
script_to_p2sh,
1212
)
1313
from test_framework.key import ECKey
14-
from test_framework.script import (
15-
CScript,
16-
OP_2,
17-
OP_3,
18-
OP_CHECKMULTISIG,
19-
)
2014
from test_framework.script_util import (
2115
key_to_p2pkh_script,
16+
keys_to_multisig_script,
2217
script_to_p2sh_script,
2318
)
2419

@@ -67,7 +62,7 @@ def get_multisig(node):
6762
addr = node.getaddressinfo(node.getnewaddress())
6863
addrs.append(addr['address'])
6964
pubkeys.append(addr['pubkey'])
70-
script_code = CScript([OP_2] + [bytes.fromhex(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
65+
script_code = keys_to_multisig_script(pubkeys, k=2)
7166
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
7267
pubkeys=pubkeys,
7368
p2sh_script=script_to_p2sh_script(script_code).hex(),

0 commit comments

Comments
 (0)