0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

Merge bitcoin/bitcoin#23305: test: refactor: add script_util helper for creating bare multisig scripts

4718897ce3 test: add script_util helper for creating bare multisig scripts (Sebastian Falbesoner)

Pull request description:

  This PR is a follow-up to #22363 and #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 4718897ce3
  rajarshimaitra:
    tACK 4718897ce3

Tree-SHA512: b452d8a75b0d17316b66ac4ed4c6893fe59c7c417719931d4cd3955161f59afca43503cd09b83a35b5a252a122eb3f0fbb9da9f0e7c944cf8da572a02219ed9d
This commit is contained in:
MarcoFalke 2021-10-27 11:48:57 +01:00
commit ab25ef8c7f
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
5 changed files with 23 additions and 18 deletions

View file

@ -30,8 +30,6 @@ from test_framework.script import (
CScript,
OP_0,
OP_1,
OP_2,
OP_CHECKMULTISIG,
OP_DROP,
OP_TRUE,
)
@ -39,6 +37,7 @@ from test_framework.script_util import (
key_to_p2pk_script,
key_to_p2pkh_script,
key_to_p2wpkh_script,
keys_to_multisig_script,
script_to_p2sh_script,
script_to_p2wsh_script,
)
@ -149,7 +148,7 @@ class SegWitTest(BitcoinTestFramework):
key = get_generate_key()
self.pubkey.append(key.pubkey)
multiscript = CScript([OP_1, bytes.fromhex(self.pubkey[-1]), OP_1, OP_CHECKMULTISIG])
multiscript = keys_to_multisig_script([self.pubkey[-1]])
p2sh_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'p2sh-segwit')['address']
bip173_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'bech32')['address']
assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript))
@ -389,7 +388,7 @@ class SegWitTest(BitcoinTestFramework):
# Money sent to P2SH of multisig of this should only be seen after importaddress with the BASE58 P2SH address.
multisig_without_privkey_address = self.nodes[0].addmultisigaddress(2, [pubkeys[3], pubkeys[4]])['address']
script = CScript([OP_2, bytes.fromhex(pubkeys[3]), bytes.fromhex(pubkeys[4]), OP_2, OP_CHECKMULTISIG])
script = keys_to_multisig_script([pubkeys[3], pubkeys[4]])
solvable_after_importaddress.append(script_to_p2sh_script(script))
for i in compressed_spendable_address:

View file

@ -22,13 +22,11 @@ from test_framework.messages import (
from test_framework.script import (
CScript,
OP_0,
OP_2,
OP_3,
OP_CHECKMULTISIG,
OP_HASH160,
OP_RETURN,
)
from test_framework.script_util import (
keys_to_multisig_script,
script_to_p2sh_script,
)
from test_framework.util import (
@ -283,7 +281,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
key = ECKey()
key.generate()
pubkey = key.get_pubkey().get_bytes()
tx.vout[0].scriptPubKey = CScript([OP_2, pubkey, pubkey, pubkey, OP_3, OP_CHECKMULTISIG]) # Some bare multisig script (2-of-3)
tx.vout[0].scriptPubKey = keys_to_multisig_script([pubkey] * 3, k=2) # Some bare multisig script (2-of-3)
self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bare-multisig'}],
rawtxs=[tx.serialize().hex()],

View file

@ -32,13 +32,13 @@ from .script import (
CScriptNum,
CScriptOp,
OP_1,
OP_CHECKMULTISIG,
OP_RETURN,
OP_TRUE,
)
from .script_util import (
key_to_p2pk_script,
key_to_p2wpkh_script,
keys_to_multisig_script,
script_to_p2wsh_script,
)
from .util import assert_equal
@ -209,7 +209,7 @@ def witness_script(use_p2wsh, pubkey):
pkscript = key_to_p2wpkh_script(pubkey)
else:
# 1-of-1 multisig
witness_script = CScript([OP_1, bytes.fromhex(pubkey), OP_1, OP_CHECKMULTISIG])
witness_script = keys_to_multisig_script([pubkey])
pkscript = script_to_p2wsh_script(witness_script)
return pkscript.hex()
@ -218,7 +218,7 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
Optionally wrap the segwit output using P2SH."""
if use_p2wsh:
program = CScript([OP_1, bytes.fromhex(pubkey), OP_1, OP_CHECKMULTISIG])
program = keys_to_multisig_script([pubkey])
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
else:
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)

View file

@ -5,7 +5,9 @@
"""Useful Script constants and utils."""
from test_framework.script import (
CScript,
CScriptOp,
OP_0,
OP_CHECKMULTISIG,
OP_CHECKSIG,
OP_DUP,
OP_EQUAL,
@ -41,6 +43,17 @@ def key_to_p2pk_script(key):
return CScript([key, OP_CHECKSIG])
def keys_to_multisig_script(keys, *, k=None):
n = len(keys)
if k is None: # n-of-n multisig by default
k = n
assert k <= n
op_k = CScriptOp.encode_op_n(k)
op_n = CScriptOp.encode_op_n(n)
checked_keys = [check_key(key) for key in keys]
return CScript([op_k] + checked_keys + [op_n, OP_CHECKMULTISIG])
def keyhash_to_p2pkh_script(hash):
assert len(hash) == 20
return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG])

View file

@ -15,15 +15,10 @@ from test_framework.address import (
script_to_p2wsh,
)
from test_framework.key import ECKey
from test_framework.script import (
CScript,
OP_2,
OP_3,
OP_CHECKMULTISIG,
)
from test_framework.script_util import (
key_to_p2pkh_script,
key_to_p2wpkh_script,
keys_to_multisig_script,
script_to_p2sh_script,
script_to_p2wsh_script,
)
@ -92,7 +87,7 @@ def get_multisig(node):
addr = node.getaddressinfo(node.getnewaddress())
addrs.append(addr['address'])
pubkeys.append(addr['pubkey'])
script_code = CScript([OP_2] + [bytes.fromhex(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
script_code = keys_to_multisig_script(pubkeys, k=2)
witness_script = script_to_p2wsh_script(script_code)
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
pubkeys=pubkeys,