Skip to content

Commit 285a65c

Browse files
committed
test: use script_util helpers for creating P2SH scripts
1 parent b57b633 commit 285a65c

File tree

8 files changed

+57
-64
lines changed

8 files changed

+57
-64
lines changed

test/functional/data/invalid_txs.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,32 @@
2929
CTxOut,
3030
MAX_MONEY,
3131
)
32-
from test_framework import script as sc
3332
from test_framework.blocktools import create_tx_with_script, MAX_BLOCK_SIGOPS
3433
from test_framework.script import (
3534
CScript,
35+
OP_0,
36+
OP_2DIV,
37+
OP_2MUL,
38+
OP_AND,
3639
OP_CAT,
37-
OP_SUBSTR,
38-
OP_LEFT,
39-
OP_RIGHT,
40+
OP_CHECKSIG,
41+
OP_DIV,
4042
OP_INVERT,
41-
OP_AND,
43+
OP_LEFT,
44+
OP_LSHIFT,
45+
OP_MOD,
46+
OP_MUL,
4247
OP_OR,
48+
OP_RIGHT,
49+
OP_RSHIFT,
50+
OP_SUBSTR,
51+
OP_TRUE,
4352
OP_XOR,
44-
OP_2MUL,
45-
OP_2DIV,
46-
OP_MUL,
47-
OP_DIV,
48-
OP_MOD,
49-
OP_LSHIFT,
50-
OP_RSHIFT
5153
)
52-
basic_p2sh = sc.CScript([sc.OP_HASH160, sc.hash160(sc.CScript([sc.OP_0])), sc.OP_EQUAL])
54+
from test_framework.script_util import (
55+
script_to_p2sh_script,
56+
)
57+
basic_p2sh = script_to_p2sh_script(CScript([OP_0]))
5358

5459

5560
class BadTxTemplate:
@@ -116,7 +121,7 @@ class SizeTooSmall(BadTxTemplate):
116121
def get_tx(self):
117122
tx = CTransaction()
118123
tx.vin.append(self.valid_txin)
119-
tx.vout.append(CTxOut(0, sc.CScript([sc.OP_TRUE])))
124+
tx.vout.append(CTxOut(0, CScript([OP_TRUE])))
120125
tx.calc_sha256()
121126
return tx
122127

@@ -217,7 +222,7 @@ class TooManySigops(BadTxTemplate):
217222
expect_disconnect = False
218223

219224
def get_tx(self):
220-
lotsa_checksigs = sc.CScript([sc.OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
225+
lotsa_checksigs = CScript([OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
221226
return create_tx_with_script(
222227
self.spend_tx, 0,
223228
script_pub_key=lotsa_checksigs,

test/functional/feature_block.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@
3737
OP_CHECKSIGVERIFY,
3838
OP_ELSE,
3939
OP_ENDIF,
40-
OP_EQUAL,
4140
OP_DROP,
4241
OP_FALSE,
43-
OP_HASH160,
4442
OP_IF,
4543
OP_INVALIDOPCODE,
4644
OP_RETURN,
4745
OP_TRUE,
4846
SIGHASH_ALL,
4947
LegacySignatureHash,
50-
hash160,
48+
)
49+
from test_framework.script_util import (
50+
script_to_p2sh_script,
5151
)
5252
from test_framework.test_framework import BitcoinTestFramework
5353
from test_framework.util import assert_equal
@@ -469,8 +469,7 @@ def run_test(self):
469469

470470
# Build the redeem script, hash it, use hash to create the p2sh script
471471
redeem_script = CScript([self.coinbase_pubkey] + [OP_2DUP, OP_CHECKSIGVERIFY] * 5 + [OP_CHECKSIG])
472-
redeem_script_hash = hash160(redeem_script)
473-
p2sh_script = CScript([OP_HASH160, redeem_script_hash, OP_EQUAL])
472+
p2sh_script = script_to_p2sh_script(redeem_script)
474473

475474
# Create a transaction that spends one satoshi to the p2sh_script, the rest to OP_TRUE
476475
# This must be signed because it is spending a coinbase

test/functional/feature_fee_estimation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
OP_1,
1919
OP_2,
2020
OP_DROP,
21-
OP_EQUAL,
22-
OP_HASH160,
2321
OP_TRUE,
24-
hash160,
22+
)
23+
from test_framework.script_util import (
24+
script_to_p2sh_script,
2525
)
2626
from test_framework.test_framework import BitcoinTestFramework
2727
from test_framework.util import (
@@ -37,8 +37,8 @@
3737
# time signing.
3838
REDEEM_SCRIPT_1 = CScript([OP_1, OP_DROP])
3939
REDEEM_SCRIPT_2 = CScript([OP_2, OP_DROP])
40-
P2SH_1 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_1), OP_EQUAL])
41-
P2SH_2 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_2), OP_EQUAL])
40+
P2SH_1 = script_to_p2sh_script(REDEEM_SCRIPT_1)
41+
P2SH_2 = script_to_p2sh_script(REDEEM_SCRIPT_2)
4242

4343
# Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
4444
SCRIPT_SIG = [CScript([OP_TRUE, REDEEM_SCRIPT_1]), CScript([OP_TRUE, REDEEM_SCRIPT_2])]

test/functional/feature_segwit.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@
3434
OP_CHECKMULTISIG,
3535
OP_CHECKSIG,
3636
OP_DROP,
37-
OP_EQUAL,
38-
OP_HASH160,
3937
OP_TRUE,
4038
hash160,
4139
)
4240
from test_framework.script_util import (
4341
key_to_p2pkh_script,
42+
script_to_p2sh_script,
4443
)
4544
from test_framework.test_framework import BitcoinTestFramework
4645
from test_framework.util import (
@@ -354,7 +353,7 @@ def run_test(self):
354353

355354
multisig_without_privkey_address = self.nodes[0].addmultisigaddress(2, [pubkeys[3], pubkeys[4]])['address']
356355
script = CScript([OP_2, hex_str_to_bytes(pubkeys[3]), hex_str_to_bytes(pubkeys[4]), OP_2, OP_CHECKMULTISIG])
357-
solvable_after_importaddress.append(CScript([OP_HASH160, hash160(script), OP_EQUAL]))
356+
solvable_after_importaddress.append(script_to_p2sh_script(script))
358357

359358
for i in compressed_spendable_address:
360359
v = self.nodes[0].getaddressinfo(i)
@@ -430,7 +429,7 @@ def run_test(self):
430429
unsolvable_address_key = hex_str_to_bytes("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
431430
unsolvablep2pkh = key_to_p2pkh_script(unsolvable_address_key)
432431
unsolvablep2wshp2pkh = CScript([OP_0, sha256(unsolvablep2pkh)])
433-
p2shop0 = CScript([OP_HASH160, hash160(op0), OP_EQUAL])
432+
p2shop0 = script_to_p2sh_script(op0)
434433
p2wshop1 = CScript([OP_0, sha256(op1)])
435434
unsolvable_after_importaddress.append(unsolvablep2pkh)
436435
unsolvable_after_importaddress.append(unsolvablep2wshp2pkh)
@@ -616,21 +615,21 @@ def p2sh_address_to_script(self, v):
616615
bare = CScript(hex_str_to_bytes(v['hex']))
617616
p2sh = CScript(hex_str_to_bytes(v['scriptPubKey']))
618617
p2wsh = CScript([OP_0, sha256(bare)])
619-
p2sh_p2wsh = CScript([OP_HASH160, hash160(p2wsh), OP_EQUAL])
618+
p2sh_p2wsh = script_to_p2sh_script(p2wsh)
620619
return([bare, p2sh, p2wsh, p2sh_p2wsh])
621620

622621
def p2pkh_address_to_script(self, v):
623622
pubkey = hex_str_to_bytes(v['pubkey'])
624623
p2wpkh = CScript([OP_0, hash160(pubkey)])
625-
p2sh_p2wpkh = CScript([OP_HASH160, hash160(p2wpkh), OP_EQUAL])
624+
p2sh_p2wpkh = script_to_p2sh_script(p2wpkh)
626625
p2pk = CScript([pubkey, OP_CHECKSIG])
627626
p2pkh = CScript(hex_str_to_bytes(v['scriptPubKey']))
628-
p2sh_p2pk = CScript([OP_HASH160, hash160(p2pk), OP_EQUAL])
629-
p2sh_p2pkh = CScript([OP_HASH160, hash160(p2pkh), OP_EQUAL])
627+
p2sh_p2pk = script_to_p2sh_script(p2pk)
628+
p2sh_p2pkh = script_to_p2sh_script(p2pkh)
630629
p2wsh_p2pk = CScript([OP_0, sha256(p2pk)])
631630
p2wsh_p2pkh = CScript([OP_0, sha256(p2pkh)])
632-
p2sh_p2wsh_p2pk = CScript([OP_HASH160, hash160(p2wsh_p2pk), OP_EQUAL])
633-
p2sh_p2wsh_p2pkh = CScript([OP_HASH160, hash160(p2wsh_p2pkh), OP_EQUAL])
631+
p2sh_p2wsh_p2pk = script_to_p2sh_script(p2wsh_p2pk)
632+
p2sh_p2wsh_p2pkh = script_to_p2sh_script(p2wsh_p2pkh)
634633
return [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh]
635634

636635
def create_and_mine_tx_from_txids(self, txids, success=True):

test/functional/feature_taproot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
OP_ENDIF,
5858
OP_EQUAL,
5959
OP_EQUALVERIFY,
60-
OP_HASH160,
6160
OP_IF,
6261
OP_NOP,
6362
OP_NOT,
@@ -78,6 +77,7 @@
7877
)
7978
from test_framework.script_util import (
8079
keyhash_to_p2pkh_script,
80+
script_to_p2sh_script,
8181
)
8282
from test_framework.test_framework import BitcoinTestFramework
8383
from test_framework.util import assert_raises_rpc_error, assert_equal
@@ -499,7 +499,7 @@ def make_spender(comment, *, tap=None, witv0=False, script=None, pkh=None, p2sh=
499499
if p2sh:
500500
# P2SH wrapper can be combined with anything else
501501
conf["script_p2sh"] = spk
502-
spk = CScript([OP_HASH160, hash160(spk), OP_EQUAL])
502+
spk = script_to_p2sh_script(spk)
503503

504504
conf = {**conf, **kwargs}
505505

test/functional/mempool_accept.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
tx_from_hex,
2020
)
2121
from test_framework.script import (
22-
hash160,
2322
CScript,
2423
OP_0,
2524
OP_2,
2625
OP_3,
2726
OP_CHECKMULTISIG,
28-
OP_EQUAL,
2927
OP_HASH160,
3028
OP_RETURN,
3129
)
30+
from test_framework.script_util import (
31+
script_to_p2sh_script,
32+
)
3233
from test_framework.util import (
3334
assert_equal,
3435
assert_raises_rpc_error,
@@ -291,7 +292,7 @@ def run_test(self):
291292
rawtxs=[tx.serialize().hex()],
292293
)
293294
tx = tx_from_hex(raw_tx_reference)
294-
output_p2sh_burn = CTxOut(nValue=540, scriptPubKey=CScript([OP_HASH160, hash160(b'burn'), OP_EQUAL]))
295+
output_p2sh_burn = CTxOut(nValue=540, scriptPubKey=script_to_p2sh_script(b'burn'))
295296
num_scripts = 100000 // len(output_p2sh_burn.serialize()) # Use enough outputs to make the tx too large for our policy
296297
tx.vout = [output_p2sh_burn] * num_scripts
297298
self.check_mempool_result(

test/functional/p2p_segwit.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@
6262
OP_DROP,
6363
OP_ELSE,
6464
OP_ENDIF,
65-
OP_EQUAL,
66-
OP_HASH160,
6765
OP_IF,
6866
OP_RETURN,
6967
OP_TRUE,
@@ -77,6 +75,7 @@
7775
)
7876
from test_framework.script_util import (
7977
keyhash_to_p2pkh_script,
78+
script_to_p2sh_script,
8079
)
8180
from test_framework.test_framework import BitcoinTestFramework
8281
from test_framework.util import (
@@ -491,9 +490,7 @@ def test_v0_outputs_arent_spendable(self):
491490
witness_program = CScript([OP_TRUE])
492491
witness_hash = sha256(witness_program)
493492
script_pubkey = CScript([OP_0, witness_hash])
494-
495-
p2sh_pubkey = hash160(script_pubkey)
496-
p2sh_script_pubkey = CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])
493+
p2sh_script_pubkey = script_to_p2sh_script(script_pubkey)
497494

498495
value = self.utxo[0].nValue // 3
499496

@@ -630,9 +627,7 @@ def test_standardness_v0(self):
630627
witness_program = CScript([OP_TRUE])
631628
witness_hash = sha256(witness_program)
632629
script_pubkey = CScript([OP_0, witness_hash])
633-
634-
p2sh_pubkey = hash160(witness_program)
635-
p2sh_script_pubkey = CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])
630+
p2sh_script_pubkey = script_to_p2sh_script(witness_program)
636631

637632
# First prepare a p2sh output (so that spending it will pass standardness)
638633
p2sh_tx = CTransaction()
@@ -739,8 +734,7 @@ def test_p2sh_witness(self):
739734
witness_program = CScript([OP_DROP, OP_TRUE])
740735
witness_hash = sha256(witness_program)
741736
p2wsh_pubkey = CScript([OP_0, witness_hash])
742-
p2sh_witness_hash = hash160(p2wsh_pubkey)
743-
script_pubkey = CScript([OP_HASH160, p2sh_witness_hash, OP_EQUAL])
737+
script_pubkey = script_to_p2sh_script(p2wsh_pubkey)
744738
script_sig = CScript([p2wsh_pubkey]) # a push of the redeem script
745739

746740
# Fund the P2SH output
@@ -1328,9 +1322,8 @@ def test_tx_relay_after_segwit_activation(self):
13281322

13291323
# Add too-large for IsStandard witness and check that it does not enter reject filter
13301324
p2sh_program = CScript([OP_TRUE])
1331-
p2sh_pubkey = hash160(p2sh_program)
13321325
witness_program2 = CScript([b'a' * 400000])
1333-
tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])))
1326+
tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, script_to_p2sh_script(p2sh_program)))
13341327
tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program2]
13351328
tx3.rehash()
13361329

@@ -1564,8 +1557,7 @@ def test_uncompressed_pubkey(self):
15641557
# Test 2: P2WSH
15651558
# Try to spend the P2WSH output created in last test.
15661559
# Send it to a P2SH(P2WSH) output, which we'll use in the next test.
1567-
p2sh_witness_hash = hash160(script_wsh)
1568-
script_p2sh = CScript([OP_HASH160, p2sh_witness_hash, OP_EQUAL])
1560+
script_p2sh = script_to_p2sh_script(script_wsh)
15691561
script_sig = CScript([script_wsh])
15701562

15711563
tx3 = CTransaction()
@@ -1803,8 +1795,7 @@ def test_non_standard_witness_blinding(self):
18031795
# rules (an anyone-can-spend OP_TRUE would be rejected, if not wrapped
18041796
# in P2SH).
18051797
p2sh_program = CScript([OP_TRUE])
1806-
p2sh_pubkey = hash160(p2sh_program)
1807-
script_pubkey = CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])
1798+
script_pubkey = script_to_p2sh_script(p2sh_program)
18081799

18091800
# Now check that unnecessary witnesses can't be used to blind a node
18101801
# to a transaction, eg by violating standardness checks.
@@ -1870,10 +1861,9 @@ def test_non_standard_witness(self):
18701861
outputvalue = (self.utxo[0].nValue - 1000) // (len(scripts) * 2)
18711862
for i in scripts:
18721863
p2wsh = CScript([OP_0, sha256(i)])
1873-
p2sh = hash160(p2wsh)
18741864
p2wsh_scripts.append(p2wsh)
18751865
tx.vout.append(CTxOut(outputvalue, p2wsh))
1876-
tx.vout.append(CTxOut(outputvalue, CScript([OP_HASH160, p2sh, OP_EQUAL])))
1866+
tx.vout.append(CTxOut(outputvalue, script_to_p2sh_script(p2wsh)))
18771867
tx.rehash()
18781868
txid = tx.sha256
18791869
test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=False, accepted=True)

test/functional/test_framework/wallet_util.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
OP_2,
2222
OP_3,
2323
OP_CHECKMULTISIG,
24-
OP_EQUAL,
25-
OP_HASH160,
2624
hash160,
2725
sha256,
2826
)
2927
from test_framework.script_util import (
3028
key_to_p2pkh_script,
29+
script_to_p2sh_script,
3130
)
3231
from test_framework.util import hex_str_to_bytes
3332

@@ -64,7 +63,7 @@ def get_key(node):
6463
p2pkh_addr=key_to_p2pkh(pubkey),
6564
p2wpkh_script=CScript([OP_0, pkh]).hex(),
6665
p2wpkh_addr=key_to_p2wpkh(pubkey),
67-
p2sh_p2wpkh_script=CScript([OP_HASH160, hash160(CScript([OP_0, pkh])), OP_EQUAL]).hex(),
66+
p2sh_p2wpkh_script=script_to_p2sh_script(CScript([OP_0, pkh])).hex(),
6867
p2sh_p2wpkh_redeem_script=CScript([OP_0, pkh]).hex(),
6968
p2sh_p2wpkh_addr=key_to_p2sh_p2wpkh(pubkey))
7069

@@ -83,7 +82,7 @@ def get_generate_key():
8382
p2pkh_addr=key_to_p2pkh(pubkey),
8483
p2wpkh_script=CScript([OP_0, pkh]).hex(),
8584
p2wpkh_addr=key_to_p2wpkh(pubkey),
86-
p2sh_p2wpkh_script=CScript([OP_HASH160, hash160(CScript([OP_0, pkh])), OP_EQUAL]).hex(),
85+
p2sh_p2wpkh_script=script_to_p2sh_script(CScript([OP_0, pkh])).hex(),
8786
p2sh_p2wpkh_redeem_script=CScript([OP_0, pkh]).hex(),
8887
p2sh_p2wpkh_addr=key_to_p2sh_p2wpkh(pubkey))
8988

@@ -101,12 +100,12 @@ def get_multisig(node):
101100
witness_script = CScript([OP_0, sha256(script_code)])
102101
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
103102
pubkeys=pubkeys,
104-
p2sh_script=CScript([OP_HASH160, hash160(script_code), OP_EQUAL]).hex(),
103+
p2sh_script=script_to_p2sh_script(script_code).hex(),
105104
p2sh_addr=script_to_p2sh(script_code),
106105
redeem_script=script_code.hex(),
107106
p2wsh_script=witness_script.hex(),
108107
p2wsh_addr=script_to_p2wsh(script_code),
109-
p2sh_p2wsh_script=CScript([OP_HASH160, hash160(witness_script), OP_EQUAL]).hex(),
108+
p2sh_p2wsh_script=script_to_p2sh_script(witness_script).hex(),
110109
p2sh_p2wsh_addr=script_to_p2sh_p2wsh(script_code))
111110

112111
def test_address(node, address, **kwargs):

0 commit comments

Comments
 (0)