test: introduce script_util helper for creating P2PK scripts

This commit is contained in:
Sebastian Falbesoner
2021-09-28 13:37:46 +02:00
parent d648bbb0a7
commit 429b49378e
7 changed files with 26 additions and 17 deletions

View File

@@ -33,11 +33,11 @@ from .script import (
CScriptOp,
OP_1,
OP_CHECKMULTISIG,
OP_CHECKSIG,
OP_RETURN,
OP_TRUE,
)
from .script_util import (
key_to_p2pk_script,
key_to_p2wpkh_script,
script_to_p2wsh_script,
)
@@ -134,7 +134,7 @@ def create_coinbase(height, pubkey=None, extra_output_script=None, fees=0, nValu
coinbaseoutput.nValue >>= halvings
coinbaseoutput.nValue += fees
if pubkey is not None:
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
coinbaseoutput.scriptPubKey = key_to_p2pk_script(pubkey)
else:
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
coinbase.vout = [coinbaseoutput]

View File

@@ -5,14 +5,14 @@
"""Useful Script constants and utils."""
from test_framework.script import (
CScript,
hash160,
sha256,
OP_0,
OP_DUP,
OP_HASH160,
OP_CHECKSIG,
OP_DUP,
OP_EQUAL,
OP_EQUALVERIFY,
OP_HASH160,
hash160,
sha256,
)
# To prevent a "tx-size-small" policy rule error, a transaction has to have a
@@ -36,6 +36,11 @@ DUMMY_P2WPKH_SCRIPT = CScript([b'a' * 21])
DUMMY_2_P2WPKH_SCRIPT = CScript([b'b' * 21])
def key_to_p2pk_script(key):
key = check_key(key)
return CScript([key, OP_CHECKSIG])
def keyhash_to_p2pkh_script(hash):
assert len(hash) == 20
return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG])

View File

@@ -23,12 +23,14 @@ from test_framework.messages import (
from test_framework.script import (
CScript,
LegacySignatureHash,
OP_CHECKSIG,
OP_TRUE,
OP_NOP,
SIGHASH_ALL,
)
from test_framework.script_util import key_to_p2wpkh_script
from test_framework.script_util import (
key_to_p2pk_script,
key_to_p2wpkh_script,
)
from test_framework.util import (
assert_equal,
assert_greater_than_or_equal,
@@ -75,7 +77,7 @@ class MiniWallet:
self._priv_key = ECKey()
self._priv_key.set((1).to_bytes(32, 'big'), True)
pub_key = self._priv_key.get_pubkey()
self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG]))
self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes())
elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])