Skip to content

Commit 429b493

Browse files
committed
test: introduce script_util helper for creating P2PK scripts
1 parent d648bbb commit 429b493

7 files changed

+26
-17
lines changed

test/functional/feature_segwit.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
OP_1,
3232
OP_2,
3333
OP_CHECKMULTISIG,
34-
OP_CHECKSIG,
3534
OP_DROP,
3635
OP_TRUE,
3736
)
3837
from test_framework.script_util import (
38+
key_to_p2pk_script,
3939
key_to_p2pkh_script,
4040
key_to_p2wpkh_script,
4141
script_to_p2sh_script,
@@ -459,7 +459,7 @@ def run_test(self):
459459
importlist.append(script_to_p2wsh_script(bare).hex())
460460
else:
461461
pubkey = bytes.fromhex(v['pubkey'])
462-
p2pk = CScript([pubkey, OP_CHECKSIG])
462+
p2pk = key_to_p2pk_script(pubkey)
463463
p2pkh = key_to_p2pkh_script(pubkey)
464464
importlist.append(p2pk.hex())
465465
importlist.append(p2pkh.hex())
@@ -628,7 +628,7 @@ def p2pkh_address_to_script(self, v):
628628
pubkey = bytes.fromhex(v['pubkey'])
629629
p2wpkh = key_to_p2wpkh_script(pubkey)
630630
p2sh_p2wpkh = script_to_p2sh_script(p2wpkh)
631-
p2pk = CScript([pubkey, OP_CHECKSIG])
631+
p2pk = key_to_p2pk_script(pubkey)
632632
p2pkh = CScript(bytes.fromhex(v['scriptPubKey']))
633633
p2sh_p2pk = script_to_p2sh_script(p2pk)
634634
p2sh_p2pkh = script_to_p2sh_script(p2pkh)

test/functional/feature_taproot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
taproot_construct,
7777
)
7878
from test_framework.script_util import (
79+
key_to_p2pk_script,
7980
key_to_p2wpkh_script,
8081
keyhash_to_p2pkh_script,
8182
script_to_p2sh_script,
@@ -1109,7 +1110,7 @@ def predict_sigops_ratio(n, dummy_size):
11091110
for witv0 in [False, True]:
11101111
for hashtype in VALID_SIGHASHES_ECDSA + [random.randrange(0x04, 0x80), random.randrange(0x84, 0x100)]:
11111112
standard = (hashtype in VALID_SIGHASHES_ECDSA) and (compressed or not witv0)
1112-
add_spender(spenders, "legacy/pk-wrongkey", hashtype=hashtype, p2sh=p2sh, witv0=witv0, standard=standard, script=CScript([pubkey1, OP_CHECKSIG]), **SINGLE_SIG, key=eckey1, failure={"key": eckey2}, sigops_weight=4-3*witv0, **ERR_NO_SUCCESS)
1113+
add_spender(spenders, "legacy/pk-wrongkey", hashtype=hashtype, p2sh=p2sh, witv0=witv0, standard=standard, script=key_to_p2pk_script(pubkey1), **SINGLE_SIG, key=eckey1, failure={"key": eckey2}, sigops_weight=4-3*witv0, **ERR_NO_SUCCESS)
11131114
add_spender(spenders, "legacy/pkh-sighashflip", hashtype=hashtype, p2sh=p2sh, witv0=witv0, standard=standard, pkh=pubkey1, key=eckey1, **SIGHASH_BITFLIP, sigops_weight=4-3*witv0, **ERR_NO_SUCCESS)
11141115

11151116
# Verify that OP_CHECKSIGADD wasn't accidentally added to pre-taproot validation logic.

test/functional/p2p_segwit.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
hash160,
7373
)
7474
from test_framework.script_util import (
75+
key_to_p2pk_script,
7576
key_to_p2wpkh_script,
7677
keyhash_to_p2pkh_script,
7778
script_to_p2sh_script,
@@ -1455,7 +1456,7 @@ def test_uncompressed_pubkey(self):
14551456

14561457
# Now try to spend it. Send it to a P2WSH output, which we'll
14571458
# use in the next test.
1458-
witness_script = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
1459+
witness_script = key_to_p2pk_script(pubkey)
14591460
script_wsh = script_to_p2wsh_script(witness_script)
14601461

14611462
tx2 = CTransaction()
@@ -1533,7 +1534,7 @@ def test_signature_version_1(self):
15331534
key.generate()
15341535
pubkey = key.get_pubkey().get_bytes()
15351536

1536-
witness_script = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
1537+
witness_script = key_to_p2pk_script(pubkey)
15371538
script_pubkey = script_to_p2wsh_script(witness_script)
15381539

15391540
# First create a witness output for use in the tests.

test/functional/rpc_signrawtransaction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
from test_framework.script import (
2626
CScript,
2727
OP_CHECKLOCKTIMEVERIFY,
28-
OP_CHECKSIG,
2928
OP_CHECKSEQUENCEVERIFY,
3029
OP_DROP,
3130
OP_TRUE,
3231
)
3332
from test_framework.script_util import (
33+
key_to_p2pk_script,
3434
key_to_p2pkh_script,
3535
script_to_p2sh_p2wsh_script,
3636
script_to_p2wsh_script,
@@ -229,7 +229,7 @@ def verify_txn_with_witness_script(self, tx_type):
229229
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
230230
witness_script = {
231231
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
232-
'P2PK': CScript([bytes.fromhex(embedded_pubkey), OP_CHECKSIG]).hex()
232+
'P2PK': key_to_p2pk_script(embedded_pubkey).hex()
233233
}.get(tx_type, "Invalid tx_type")
234234
redeem_script = script_to_p2wsh_script(witness_script).hex()
235235
addr = script_to_p2sh(redeem_script)

test/functional/test_framework/blocktools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
CScriptOp,
3434
OP_1,
3535
OP_CHECKMULTISIG,
36-
OP_CHECKSIG,
3736
OP_RETURN,
3837
OP_TRUE,
3938
)
4039
from .script_util import (
40+
key_to_p2pk_script,
4141
key_to_p2wpkh_script,
4242
script_to_p2wsh_script,
4343
)
@@ -134,7 +134,7 @@ def create_coinbase(height, pubkey=None, extra_output_script=None, fees=0, nValu
134134
coinbaseoutput.nValue >>= halvings
135135
coinbaseoutput.nValue += fees
136136
if pubkey is not None:
137-
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
137+
coinbaseoutput.scriptPubKey = key_to_p2pk_script(pubkey)
138138
else:
139139
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
140140
coinbase.vout = [coinbaseoutput]

test/functional/test_framework/script_util.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"""Useful Script constants and utils."""
66
from test_framework.script import (
77
CScript,
8-
hash160,
9-
sha256,
108
OP_0,
11-
OP_DUP,
12-
OP_HASH160,
139
OP_CHECKSIG,
10+
OP_DUP,
1411
OP_EQUAL,
1512
OP_EQUALVERIFY,
13+
OP_HASH160,
14+
hash160,
15+
sha256,
1616
)
1717

1818
# To prevent a "tx-size-small" policy rule error, a transaction has to have a
@@ -36,6 +36,11 @@
3636
DUMMY_2_P2WPKH_SCRIPT = CScript([b'b' * 21])
3737

3838

39+
def key_to_p2pk_script(key):
40+
key = check_key(key)
41+
return CScript([key, OP_CHECKSIG])
42+
43+
3944
def keyhash_to_p2pkh_script(hash):
4045
assert len(hash) == 20
4146
return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG])

test/functional/test_framework/wallet.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
from test_framework.script import (
2424
CScript,
2525
LegacySignatureHash,
26-
OP_CHECKSIG,
2726
OP_TRUE,
2827
OP_NOP,
2928
SIGHASH_ALL,
3029
)
31-
from test_framework.script_util import key_to_p2wpkh_script
30+
from test_framework.script_util import (
31+
key_to_p2pk_script,
32+
key_to_p2wpkh_script,
33+
)
3234
from test_framework.util import (
3335
assert_equal,
3436
assert_greater_than_or_equal,
@@ -75,7 +77,7 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
7577
self._priv_key = ECKey()
7678
self._priv_key.set((1).to_bytes(32, 'big'), True)
7779
pub_key = self._priv_key.get_pubkey()
78-
self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG]))
80+
self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes())
7981
elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
8082
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
8183
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])

0 commit comments

Comments
 (0)