Skip to content

Commit 905d672

Browse files
committed
test: use script_util helpers for creating P2W{PKH,SH} scripts
1 parent 285a65c commit 905d672

File tree

6 files changed

+58
-80
lines changed

6 files changed

+58
-80
lines changed

test/functional/feature_segwit.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
CTransaction,
2424
CTxIn,
2525
CTxOut,
26-
sha256,
2726
tx_from_hex,
2827
)
2928
from test_framework.script import (
@@ -35,11 +34,12 @@
3534
OP_CHECKSIG,
3635
OP_DROP,
3736
OP_TRUE,
38-
hash160,
3937
)
4038
from test_framework.script_util import (
4139
key_to_p2pkh_script,
40+
key_to_p2wpkh_script,
4241
script_to_p2sh_script,
42+
script_to_p2wsh_script,
4343
)
4444
from test_framework.test_framework import BitcoinTestFramework
4545
from test_framework.util import (
@@ -428,9 +428,9 @@ def run_test(self):
428428
# 2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe is the P2SH(P2PKH) version of mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V
429429
unsolvable_address_key = hex_str_to_bytes("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
430430
unsolvablep2pkh = key_to_p2pkh_script(unsolvable_address_key)
431-
unsolvablep2wshp2pkh = CScript([OP_0, sha256(unsolvablep2pkh)])
431+
unsolvablep2wshp2pkh = script_to_p2wsh_script(unsolvablep2pkh)
432432
p2shop0 = script_to_p2sh_script(op0)
433-
p2wshop1 = CScript([OP_0, sha256(op1)])
433+
p2wshop1 = script_to_p2wsh_script(op1)
434434
unsolvable_after_importaddress.append(unsolvablep2pkh)
435435
unsolvable_after_importaddress.append(unsolvablep2wshp2pkh)
436436
unsolvable_after_importaddress.append(op1) # OP_1 will be imported as script
@@ -450,16 +450,16 @@ def run_test(self):
450450
if (v['isscript']):
451451
bare = hex_str_to_bytes(v['hex'])
452452
importlist.append(bare.hex())
453-
importlist.append(CScript([OP_0, sha256(bare)]).hex())
453+
importlist.append(script_to_p2wsh_script(bare).hex())
454454
else:
455455
pubkey = hex_str_to_bytes(v['pubkey'])
456456
p2pk = CScript([pubkey, OP_CHECKSIG])
457457
p2pkh = key_to_p2pkh_script(pubkey)
458458
importlist.append(p2pk.hex())
459459
importlist.append(p2pkh.hex())
460-
importlist.append(CScript([OP_0, hash160(pubkey)]).hex())
461-
importlist.append(CScript([OP_0, sha256(p2pk)]).hex())
462-
importlist.append(CScript([OP_0, sha256(p2pkh)]).hex())
460+
importlist.append(key_to_p2wpkh_script(pubkey).hex())
461+
importlist.append(script_to_p2wsh_script(p2pk).hex())
462+
importlist.append(script_to_p2wsh_script(p2pkh).hex())
463463

464464
importlist.append(unsolvablep2pkh.hex())
465465
importlist.append(unsolvablep2wshp2pkh.hex())
@@ -614,20 +614,20 @@ def mine_and_test_listunspent(self, script_list, ismine):
614614
def p2sh_address_to_script(self, v):
615615
bare = CScript(hex_str_to_bytes(v['hex']))
616616
p2sh = CScript(hex_str_to_bytes(v['scriptPubKey']))
617-
p2wsh = CScript([OP_0, sha256(bare)])
617+
p2wsh = script_to_p2wsh_script(bare)
618618
p2sh_p2wsh = script_to_p2sh_script(p2wsh)
619619
return([bare, p2sh, p2wsh, p2sh_p2wsh])
620620

621621
def p2pkh_address_to_script(self, v):
622622
pubkey = hex_str_to_bytes(v['pubkey'])
623-
p2wpkh = CScript([OP_0, hash160(pubkey)])
623+
p2wpkh = key_to_p2wpkh_script(pubkey)
624624
p2sh_p2wpkh = script_to_p2sh_script(p2wpkh)
625625
p2pk = CScript([pubkey, OP_CHECKSIG])
626626
p2pkh = CScript(hex_str_to_bytes(v['scriptPubKey']))
627627
p2sh_p2pk = script_to_p2sh_script(p2pk)
628628
p2sh_p2pkh = script_to_p2sh_script(p2pkh)
629-
p2wsh_p2pk = CScript([OP_0, sha256(p2pk)])
630-
p2wsh_p2pkh = CScript([OP_0, sha256(p2pkh)])
629+
p2wsh_p2pk = script_to_p2wsh_script(p2pk)
630+
p2wsh_p2pkh = script_to_p2wsh_script(p2pkh)
631631
p2sh_p2wsh_p2pk = script_to_p2sh_script(p2wsh_p2pk)
632632
p2sh_p2wsh_p2pkh = script_to_p2sh_script(p2wsh_p2pkh)
633633
return [p2wpkh, p2sh_p2wpkh, p2pk, p2pkh, p2sh_p2pk, p2sh_p2pkh, p2wsh_p2pk, p2wsh_p2pkh, p2sh_p2wsh_p2pk, p2sh_p2wsh_p2pkh]

test/functional/feature_taproot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@
7676
taproot_construct,
7777
)
7878
from test_framework.script_util import (
79+
key_to_p2wpkh_script,
7980
keyhash_to_p2pkh_script,
8081
script_to_p2sh_script,
82+
script_to_p2wsh_script,
8183
)
8284
from test_framework.test_framework import BitcoinTestFramework
8385
from test_framework.util import assert_raises_rpc_error, assert_equal
8486
from test_framework.key import generate_privkey, compute_xonly_pubkey, sign_schnorr, tweak_add_privkey, ECKey
8587
from test_framework.address import (
8688
hash160,
87-
sha256,
8889
)
8990
from collections import OrderedDict, namedtuple
9091
from io import BytesIO
@@ -461,13 +462,13 @@ def make_spender(comment, *, tap=None, witv0=False, script=None, pkh=None, p2sh=
461462
# P2WPKH
462463
assert script is None
463464
pubkeyhash = hash160(pkh)
464-
spk = CScript([OP_0, pubkeyhash])
465+
spk = key_to_p2wpkh_script(pkh)
465466
conf["scriptcode"] = keyhash_to_p2pkh_script(pubkeyhash)
466467
conf["script_witv0"] = None
467468
conf["inputs"] = [getter("sign"), pkh]
468469
elif script is not None:
469470
# P2WSH
470-
spk = CScript([OP_0, sha256(script)])
471+
spk = script_to_p2wsh_script(script)
471472
conf["scriptcode"] = script
472473
conf["script_witv0"] = script
473474
else:

test/functional/p2p_segwit.py

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
ser_vector,
4242
sha256,
4343
tx_from_hex,
44-
uint256_from_str,
4544
)
4645
from test_framework.p2p import (
4746
P2PInterface,
@@ -74,8 +73,10 @@
7473
hash160,
7574
)
7675
from test_framework.script_util import (
76+
key_to_p2wpkh_script,
7777
keyhash_to_p2pkh_script,
7878
script_to_p2sh_script,
79+
script_to_p2wsh_script,
7980
)
8081
from test_framework.test_framework import BitcoinTestFramework
8182
from test_framework.util import (
@@ -488,8 +489,7 @@ def test_v0_outputs_arent_spendable(self):
488489

489490
# Create two outputs, a p2wsh and p2sh-p2wsh
490491
witness_program = CScript([OP_TRUE])
491-
witness_hash = sha256(witness_program)
492-
script_pubkey = CScript([OP_0, witness_hash])
492+
script_pubkey = script_to_p2wsh_script(witness_program)
493493
p2sh_script_pubkey = script_to_p2sh_script(script_pubkey)
494494

495495
value = self.utxo[0].nValue // 3
@@ -625,8 +625,7 @@ def test_standardness_v0(self):
625625
V0 segwit inputs may only be mined after activation, but not before."""
626626

627627
witness_program = CScript([OP_TRUE])
628-
witness_hash = sha256(witness_program)
629-
script_pubkey = CScript([OP_0, witness_hash])
628+
script_pubkey = script_to_p2wsh_script(witness_program)
630629
p2sh_script_pubkey = script_to_p2sh_script(witness_program)
631630

632631
# First prepare a p2sh output (so that spending it will pass standardness)
@@ -654,6 +653,7 @@ def test_standardness_v0(self):
654653
test_transaction_acceptance(self.nodes[1], self.std_node, tx, with_witness=True, accepted=True)
655654

656655
# Now create something that looks like a P2PKH output. This won't be spendable.
656+
witness_hash = sha256(witness_program)
657657
script_pubkey = CScript([OP_0, hash160(witness_hash)])
658658
tx2 = CTransaction()
659659
# tx was accepted, so we spend the second output.
@@ -732,8 +732,7 @@ def test_p2sh_witness(self):
732732

733733
# Prepare the p2sh-wrapped witness output
734734
witness_program = CScript([OP_DROP, OP_TRUE])
735-
witness_hash = sha256(witness_program)
736-
p2wsh_pubkey = CScript([OP_0, witness_hash])
735+
p2wsh_pubkey = script_to_p2wsh_script(witness_program)
737736
script_pubkey = script_to_p2sh_script(p2wsh_pubkey)
738737
script_sig = CScript([p2wsh_pubkey]) # a push of the redeem script
739738

@@ -828,8 +827,7 @@ def test_witness_commitments(self):
828827

829828
# Let's construct a witness program
830829
witness_program = CScript([OP_TRUE])
831-
witness_hash = sha256(witness_program)
832-
script_pubkey = CScript([OP_0, witness_hash])
830+
script_pubkey = script_to_p2wsh_script(witness_program)
833831
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
834832
tx.rehash()
835833

@@ -942,8 +940,7 @@ def test_witness_block_size(self):
942940
NUM_OUTPUTS = 50
943941

944942
witness_program = CScript([OP_2DROP] * NUM_DROPS + [OP_TRUE])
945-
witness_hash = uint256_from_str(sha256(witness_program))
946-
script_pubkey = CScript([OP_0, ser_uint256(witness_hash)])
943+
script_pubkey = script_to_p2wsh_script(witness_program)
947944

948945
prevout = COutPoint(self.utxo[0].sha256, self.utxo[0].n)
949946
value = self.utxo[0].nValue
@@ -1045,8 +1042,7 @@ def test_extra_witness_data(self):
10451042
block = self.build_next_block()
10461043

10471044
witness_program = CScript([OP_DROP, OP_TRUE])
1048-
witness_hash = sha256(witness_program)
1049-
script_pubkey = CScript([OP_0, witness_hash])
1045+
script_pubkey = script_to_p2wsh_script(witness_program)
10501046

10511047
# First try extra witness data on a tx that doesn't require a witness
10521048
tx = CTransaction()
@@ -1118,8 +1114,7 @@ def test_max_witness_push_length(self):
11181114
block = self.build_next_block()
11191115

11201116
witness_program = CScript([OP_DROP, OP_TRUE])
1121-
witness_hash = sha256(witness_program)
1122-
script_pubkey = CScript([OP_0, witness_hash])
1117+
script_pubkey = script_to_p2wsh_script(witness_program)
11231118

11241119
tx = CTransaction()
11251120
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
@@ -1157,8 +1152,7 @@ def test_max_witness_program_length(self):
11571152
# This program is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
11581153
long_witness_program = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 63 + [OP_TRUE])
11591154
assert len(long_witness_program) == MAX_PROGRAM_LENGTH + 1
1160-
long_witness_hash = sha256(long_witness_program)
1161-
long_script_pubkey = CScript([OP_0, long_witness_hash])
1155+
long_script_pubkey = script_to_p2wsh_script(long_witness_program)
11621156

11631157
block = self.build_next_block()
11641158

@@ -1181,8 +1175,7 @@ def test_max_witness_program_length(self):
11811175
# Try again with one less byte in the witness program
11821176
witness_program = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 62 + [OP_TRUE])
11831177
assert len(witness_program) == MAX_PROGRAM_LENGTH
1184-
witness_hash = sha256(witness_program)
1185-
script_pubkey = CScript([OP_0, witness_hash])
1178+
script_pubkey = script_to_p2wsh_script(witness_program)
11861179

11871180
tx.vout[0] = CTxOut(tx.vout[0].nValue, script_pubkey)
11881181
tx.rehash()
@@ -1201,8 +1194,7 @@ def test_witness_input_length(self):
12011194
"""Test that vin length must match vtxinwit length."""
12021195

12031196
witness_program = CScript([OP_DROP, OP_TRUE])
1204-
witness_hash = sha256(witness_program)
1205-
script_pubkey = CScript([OP_0, witness_hash])
1197+
script_pubkey = script_to_p2wsh_script(witness_program)
12061198

12071199
# Create a transaction that splits our utxo into many outputs
12081200
tx = CTransaction()
@@ -1309,8 +1301,7 @@ def test_tx_relay_after_segwit_activation(self):
13091301

13101302
# Now try to add extra witness data to a valid witness tx.
13111303
witness_program = CScript([OP_TRUE])
1312-
witness_hash = sha256(witness_program)
1313-
script_pubkey = CScript([OP_0, witness_hash])
1304+
script_pubkey = script_to_p2wsh_script(witness_program)
13141305
tx2 = CTransaction()
13151306
tx2.vin.append(CTxIn(COutPoint(tx_hash, 0), b""))
13161307
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
@@ -1472,8 +1463,7 @@ def test_premature_coinbase_witness_spend(self):
14721463
block = self.build_next_block()
14731464
# Change the output of the block to be a witness output.
14741465
witness_program = CScript([OP_TRUE])
1475-
witness_hash = sha256(witness_program)
1476-
script_pubkey = CScript([OP_0, witness_hash])
1466+
script_pubkey = script_to_p2wsh_script(witness_program)
14771467
block.vtx[0].vout[0].scriptPubKey = script_pubkey
14781468
# This next line will rehash the coinbase and update the merkle
14791469
# root, and solve.
@@ -1520,7 +1510,7 @@ def test_uncompressed_pubkey(self):
15201510
# Test 1: P2WPKH
15211511
# First create a P2WPKH output that uses an uncompressed pubkey
15221512
pubkeyhash = hash160(pubkey)
1523-
script_pkh = CScript([OP_0, pubkeyhash])
1513+
script_pkh = key_to_p2wpkh_script(pubkey)
15241514
tx = CTransaction()
15251515
tx.vin.append(CTxIn(COutPoint(utxo.sha256, utxo.n), b""))
15261516
tx.vout.append(CTxOut(utxo.nValue - 1000, script_pkh))
@@ -1534,8 +1524,7 @@ def test_uncompressed_pubkey(self):
15341524
# Now try to spend it. Send it to a P2WSH output, which we'll
15351525
# use in the next test.
15361526
witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
1537-
witness_hash = sha256(witness_program)
1538-
script_wsh = CScript([OP_0, witness_hash])
1527+
script_wsh = script_to_p2wsh_script(witness_program)
15391528

15401529
tx2 = CTransaction()
15411530
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
@@ -1613,8 +1602,7 @@ def test_signature_version_1(self):
16131602
pubkey = key.get_pubkey().get_bytes()
16141603

16151604
witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
1616-
witness_hash = sha256(witness_program)
1617-
script_pubkey = CScript([OP_0, witness_hash])
1605+
script_pubkey = script_to_p2wsh_script(witness_program)
16181606

16191607
# First create a witness output for use in the tests.
16201608
tx = CTransaction()
@@ -1733,7 +1721,7 @@ def test_signature_version_1(self):
17331721

17341722
# Now test witness version 0 P2PKH transactions
17351723
pubkeyhash = hash160(pubkey)
1736-
script_pkh = CScript([OP_0, pubkeyhash])
1724+
script_pkh = key_to_p2wpkh_script(pubkey)
17371725
tx = CTransaction()
17381726
tx.vin.append(CTxIn(COutPoint(temp_utxos[0].sha256, temp_utxos[0].n), b""))
17391727
tx.vout.append(CTxOut(temp_utxos[0].nValue, script_pkh))
@@ -1860,7 +1848,7 @@ def test_non_standard_witness(self):
18601848
# For each script, generate a pair of P2WSH and P2SH-P2WSH output.
18611849
outputvalue = (self.utxo[0].nValue - 1000) // (len(scripts) * 2)
18621850
for i in scripts:
1863-
p2wsh = CScript([OP_0, sha256(i)])
1851+
p2wsh = script_to_p2wsh_script(i)
18641852
p2wsh_scripts.append(p2wsh)
18651853
tx.vout.append(CTxOut(outputvalue, p2wsh))
18661854
tx.vout.append(CTxOut(outputvalue, script_to_p2sh_script(p2wsh)))
@@ -1877,13 +1865,13 @@ def test_non_standard_witness(self):
18771865
for i in range(len(scripts)):
18781866
p2wsh_tx = CTransaction()
18791867
p2wsh_tx.vin.append(CTxIn(COutPoint(txid, i * 2)))
1880-
p2wsh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(hex_str_to_bytes(""))])))
1868+
p2wsh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(b"")])))
18811869
p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
18821870
p2wsh_tx.rehash()
18831871
p2wsh_txs.append(p2wsh_tx)
18841872
p2sh_tx = CTransaction()
18851873
p2sh_tx.vin.append(CTxIn(COutPoint(txid, i * 2 + 1), CScript([p2wsh_scripts[i]])))
1886-
p2sh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(hex_str_to_bytes(""))])))
1874+
p2sh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(b"")])))
18871875
p2sh_tx.wit.vtxinwit.append(CTxInWitness())
18881876
p2sh_tx.rehash()
18891877
p2sh_txs.append(p2sh_tx)
@@ -1978,8 +1966,7 @@ def test_witness_sigops(self):
19781966

19791967
# Keep this under MAX_OPS_PER_SCRIPT (201)
19801968
witness_program = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKMULTISIG] * 5 + [OP_CHECKSIG] * 193 + [OP_ENDIF])
1981-
witness_hash = sha256(witness_program)
1982-
script_pubkey = CScript([OP_0, witness_hash])
1969+
script_pubkey = script_to_p2wsh_script(witness_program)
19831970

19841971
sigops_per_script = 20 * 5 + 193 * 1
19851972
# We'll produce 2 extra outputs, one with a program that would take us
@@ -1995,14 +1982,12 @@ def test_witness_sigops(self):
19951982
# N(=MAX_SIGOP_COST//sigops_per_script) outputs of our transaction,
19961983
# would push us just over the block sigop limit.
19971984
witness_program_toomany = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available + 1) + [OP_ENDIF])
1998-
witness_hash_toomany = sha256(witness_program_toomany)
1999-
script_pubkey_toomany = CScript([OP_0, witness_hash_toomany])
1985+
script_pubkey_toomany = script_to_p2wsh_script(witness_program_toomany)
20001986

20011987
# If we spend this script instead, we would exactly reach our sigop
20021988
# limit (for witness sigops).
20031989
witness_program_justright = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available) + [OP_ENDIF])
2004-
witness_hash_justright = sha256(witness_program_justright)
2005-
script_pubkey_justright = CScript([OP_0, witness_hash_justright])
1990+
script_pubkey_justright = script_to_p2wsh_script(witness_program_justright)
20061991

20071992
# First split our available utxo into a bunch of outputs
20081993
split_value = self.utxo[0].nValue // outputs
@@ -2135,8 +2120,7 @@ def received_wtxidrelay():
21352120
# Create a Segwit output from the latest UTXO
21362121
# and announce it to the network
21372122
witness_program = CScript([OP_TRUE])
2138-
witness_hash = sha256(witness_program)
2139-
script_pubkey = CScript([OP_0, witness_hash])
2123+
script_pubkey = script_to_p2wsh_script(witness_program)
21402124

21412125
tx = CTransaction()
21422126
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))

test/functional/rpc_signrawtransaction.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from test_framework.blocktools import COINBASE_MATURITY
88
from test_framework.address import (
9-
check_script,
109
script_to_p2sh,
1110
script_to_p2wsh,
1211
)
@@ -20,12 +19,10 @@
2019
)
2120
from test_framework.messages import (
2221
CTxInWitness,
23-
sha256,
2422
tx_from_hex,
2523
)
2624
from test_framework.script import (
2725
CScript,
28-
OP_0,
2926
OP_CHECKLOCKTIMEVERIFY,
3027
OP_CHECKSIG,
3128
OP_CHECKSEQUENCEVERIFY,
@@ -233,7 +230,7 @@ def verify_txn_with_witness_script(self, tx_type):
233230
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
234231
'P2PK': CScript([hex_str_to_bytes(embedded_pubkey), OP_CHECKSIG]).hex()
235232
}.get(tx_type, "Invalid tx_type")
236-
redeem_script = CScript([OP_0, sha256(check_script(witness_script))]).hex()
233+
redeem_script = script_to_p2wsh_script(witness_script).hex()
237234
addr = script_to_p2sh(redeem_script)
238235
script_pub_key = self.nodes[1].validateaddress(addr)['scriptPubKey']
239236
# Fund that address

0 commit comments

Comments
 (0)