Skip to content

Commit b57b633

Browse files
committed
test: use script_util helpers for creating P2PKH scripts
1 parent 61b6a01 commit b57b633

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

test/functional/feature_segwit.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
OP_CHECKMULTISIG,
3535
OP_CHECKSIG,
3636
OP_DROP,
37-
OP_DUP,
3837
OP_EQUAL,
39-
OP_EQUALVERIFY,
4038
OP_HASH160,
4139
OP_TRUE,
4240
hash160,
4341
)
42+
from test_framework.script_util import (
43+
key_to_p2pkh_script,
44+
)
4445
from test_framework.test_framework import BitcoinTestFramework
4546
from test_framework.util import (
4647
assert_equal,
@@ -427,7 +428,7 @@ def run_test(self):
427428
op0 = CScript([OP_0])
428429
# 2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe is the P2SH(P2PKH) version of mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V
429430
unsolvable_address_key = hex_str_to_bytes("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
430-
unsolvablep2pkh = CScript([OP_DUP, OP_HASH160, hash160(unsolvable_address_key), OP_EQUALVERIFY, OP_CHECKSIG])
431+
unsolvablep2pkh = key_to_p2pkh_script(unsolvable_address_key)
431432
unsolvablep2wshp2pkh = CScript([OP_0, sha256(unsolvablep2pkh)])
432433
p2shop0 = CScript([OP_HASH160, hash160(op0), OP_EQUAL])
433434
p2wshop1 = CScript([OP_0, sha256(op1)])
@@ -454,7 +455,7 @@ def run_test(self):
454455
else:
455456
pubkey = hex_str_to_bytes(v['pubkey'])
456457
p2pk = CScript([pubkey, OP_CHECKSIG])
457-
p2pkh = CScript([OP_DUP, OP_HASH160, hash160(pubkey), OP_EQUALVERIFY, OP_CHECKSIG])
458+
p2pkh = key_to_p2pkh_script(pubkey)
458459
importlist.append(p2pk.hex())
459460
importlist.append(p2pkh.hex())
460461
importlist.append(CScript([OP_0, hash160(pubkey)]).hex())

test/functional/feature_taproot.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
is_op_success,
7777
taproot_construct,
7878
)
79+
from test_framework.script_util import (
80+
keyhash_to_p2pkh_script,
81+
)
7982
from test_framework.test_framework import BitcoinTestFramework
8083
from test_framework.util import assert_raises_rpc_error, assert_equal
8184
from test_framework.key import generate_privkey, compute_xonly_pubkey, sign_schnorr, tweak_add_privkey, ECKey
@@ -459,7 +462,7 @@ def make_spender(comment, *, tap=None, witv0=False, script=None, pkh=None, p2sh=
459462
assert script is None
460463
pubkeyhash = hash160(pkh)
461464
spk = CScript([OP_0, pubkeyhash])
462-
conf["scriptcode"] = CScript([OP_DUP, OP_HASH160, pubkeyhash, OP_EQUALVERIFY, OP_CHECKSIG])
465+
conf["scriptcode"] = keyhash_to_p2pkh_script(pubkeyhash)
463466
conf["script_witv0"] = None
464467
conf["inputs"] = [getter("sign"), pkh]
465468
elif script is not None:
@@ -475,7 +478,7 @@ def make_spender(comment, *, tap=None, witv0=False, script=None, pkh=None, p2sh=
475478
# P2PKH
476479
assert script is None
477480
pubkeyhash = hash160(pkh)
478-
spk = CScript([OP_DUP, OP_HASH160, pubkeyhash, OP_EQUALVERIFY, OP_CHECKSIG])
481+
spk = keyhash_to_p2pkh_script(pubkeyhash)
479482
conf["scriptcode"] = spk
480483
conf["inputs"] = [getter("sign"), pkh]
481484
elif script is not None:

test/functional/p2p_segwit.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@
6060
OP_CHECKMULTISIG,
6161
OP_CHECKSIG,
6262
OP_DROP,
63-
OP_DUP,
6463
OP_ELSE,
6564
OP_ENDIF,
6665
OP_EQUAL,
67-
OP_EQUALVERIFY,
6866
OP_HASH160,
6967
OP_IF,
7068
OP_RETURN,
@@ -77,6 +75,9 @@
7775
LegacySignatureHash,
7876
hash160,
7977
)
78+
from test_framework.script_util import (
79+
keyhash_to_p2pkh_script,
80+
)
8081
from test_framework.test_framework import BitcoinTestFramework
8182
from test_framework.util import (
8283
assert_equal,
@@ -100,10 +101,6 @@ def __init__(self, sha256, n, value):
100101
self.n = n
101102
self.nValue = value
102103

103-
def get_p2pkh_script(pubkeyhash):
104-
"""Get the script associated with a P2PKH."""
105-
return CScript([CScriptOp(OP_DUP), CScriptOp(OP_HASH160), pubkeyhash, CScriptOp(OP_EQUALVERIFY), CScriptOp(OP_CHECKSIG)])
106-
107104
def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
108105
"""Add signature for a P2PK witness program."""
109106
tx_hash = SegwitV0SignatureHash(script, tx_to, in_idx, hashtype, value)
@@ -1550,7 +1547,7 @@ def test_uncompressed_pubkey(self):
15501547
tx2 = CTransaction()
15511548
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
15521549
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_wsh))
1553-
script = get_p2pkh_script(pubkeyhash)
1550+
script = keyhash_to_p2pkh_script(pubkeyhash)
15541551
sig_hash = SegwitV0SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
15551552
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
15561553
tx2.wit.vtxinwit.append(CTxInWitness())
@@ -1587,7 +1584,7 @@ def test_uncompressed_pubkey(self):
15871584
# Test 3: P2SH(P2WSH)
15881585
# Try to spend the P2SH output created in the last test.
15891586
# Send it to a P2PKH output, which we'll use in the next test.
1590-
script_pubkey = get_p2pkh_script(pubkeyhash)
1587+
script_pubkey = keyhash_to_p2pkh_script(pubkeyhash)
15911588
tx4 = CTransaction()
15921589
tx4.vin.append(CTxIn(COutPoint(tx3.sha256, 0), script_sig))
15931590
tx4.vout.append(CTxOut(tx3.vout[0].nValue - 1000, script_pubkey))
@@ -1754,7 +1751,7 @@ def test_signature_version_1(self):
17541751
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
17551752
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
17561753

1757-
script = get_p2pkh_script(pubkeyhash)
1754+
script = keyhash_to_p2pkh_script(pubkeyhash)
17581755
sig_hash = SegwitV0SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
17591756
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
17601757

test/functional/test_framework/wallet_util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
OP_2,
2222
OP_3,
2323
OP_CHECKMULTISIG,
24-
OP_CHECKSIG,
25-
OP_DUP,
2624
OP_EQUAL,
27-
OP_EQUALVERIFY,
2825
OP_HASH160,
2926
hash160,
3027
sha256,
3128
)
29+
from test_framework.script_util import (
30+
key_to_p2pkh_script,
31+
)
3232
from test_framework.util import hex_str_to_bytes
3333

3434
Key = namedtuple('Key', ['privkey',
@@ -60,7 +60,7 @@ def get_key(node):
6060
pkh = hash160(hex_str_to_bytes(pubkey))
6161
return Key(privkey=node.dumpprivkey(addr),
6262
pubkey=pubkey,
63-
p2pkh_script=CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(),
63+
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
6464
p2pkh_addr=key_to_p2pkh(pubkey),
6565
p2wpkh_script=CScript([OP_0, pkh]).hex(),
6666
p2wpkh_addr=key_to_p2wpkh(pubkey),
@@ -79,7 +79,7 @@ def get_generate_key():
7979
pkh = hash160(hex_str_to_bytes(pubkey))
8080
return Key(privkey=privkey,
8181
pubkey=pubkey,
82-
p2pkh_script=CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(),
82+
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
8383
p2pkh_addr=key_to_p2pkh(pubkey),
8484
p2wpkh_script=CScript([OP_0, pkh]).hex(),
8585
p2wpkh_addr=key_to_p2wpkh(pubkey),

0 commit comments

Comments
 (0)