41
41
ser_vector ,
42
42
sha256 ,
43
43
tx_from_hex ,
44
- uint256_from_str ,
45
44
)
46
45
from test_framework .p2p import (
47
46
P2PInterface ,
74
73
hash160 ,
75
74
)
76
75
from test_framework .script_util import (
76
+ key_to_p2wpkh_script ,
77
77
keyhash_to_p2pkh_script ,
78
78
script_to_p2sh_script ,
79
+ script_to_p2wsh_script ,
79
80
)
80
81
from test_framework .test_framework import BitcoinTestFramework
81
82
from test_framework .util import (
@@ -488,8 +489,7 @@ def test_v0_outputs_arent_spendable(self):
488
489
489
490
# Create two outputs, a p2wsh and p2sh-p2wsh
490
491
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 )
493
493
p2sh_script_pubkey = script_to_p2sh_script (script_pubkey )
494
494
495
495
value = self .utxo [0 ].nValue // 3
@@ -625,8 +625,7 @@ def test_standardness_v0(self):
625
625
V0 segwit inputs may only be mined after activation, but not before."""
626
626
627
627
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 )
630
629
p2sh_script_pubkey = script_to_p2sh_script (witness_program )
631
630
632
631
# First prepare a p2sh output (so that spending it will pass standardness)
@@ -654,6 +653,7 @@ def test_standardness_v0(self):
654
653
test_transaction_acceptance (self .nodes [1 ], self .std_node , tx , with_witness = True , accepted = True )
655
654
656
655
# Now create something that looks like a P2PKH output. This won't be spendable.
656
+ witness_hash = sha256 (witness_program )
657
657
script_pubkey = CScript ([OP_0 , hash160 (witness_hash )])
658
658
tx2 = CTransaction ()
659
659
# tx was accepted, so we spend the second output.
@@ -732,8 +732,7 @@ def test_p2sh_witness(self):
732
732
733
733
# Prepare the p2sh-wrapped witness output
734
734
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 )
737
736
script_pubkey = script_to_p2sh_script (p2wsh_pubkey )
738
737
script_sig = CScript ([p2wsh_pubkey ]) # a push of the redeem script
739
738
@@ -828,8 +827,7 @@ def test_witness_commitments(self):
828
827
829
828
# Let's construct a witness program
830
829
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 )
833
831
tx .vout .append (CTxOut (self .utxo [0 ].nValue - 1000 , script_pubkey ))
834
832
tx .rehash ()
835
833
@@ -942,8 +940,7 @@ def test_witness_block_size(self):
942
940
NUM_OUTPUTS = 50
943
941
944
942
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 )
947
944
948
945
prevout = COutPoint (self .utxo [0 ].sha256 , self .utxo [0 ].n )
949
946
value = self .utxo [0 ].nValue
@@ -1045,8 +1042,7 @@ def test_extra_witness_data(self):
1045
1042
block = self .build_next_block ()
1046
1043
1047
1044
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 )
1050
1046
1051
1047
# First try extra witness data on a tx that doesn't require a witness
1052
1048
tx = CTransaction ()
@@ -1118,8 +1114,7 @@ def test_max_witness_push_length(self):
1118
1114
block = self .build_next_block ()
1119
1115
1120
1116
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 )
1123
1118
1124
1119
tx = CTransaction ()
1125
1120
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):
1157
1152
# This program is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
1158
1153
long_witness_program = CScript ([b'a' * MAX_SCRIPT_ELEMENT_SIZE ] * 19 + [OP_DROP ] * 63 + [OP_TRUE ])
1159
1154
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 )
1162
1156
1163
1157
block = self .build_next_block ()
1164
1158
@@ -1181,8 +1175,7 @@ def test_max_witness_program_length(self):
1181
1175
# Try again with one less byte in the witness program
1182
1176
witness_program = CScript ([b'a' * MAX_SCRIPT_ELEMENT_SIZE ] * 19 + [OP_DROP ] * 62 + [OP_TRUE ])
1183
1177
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 )
1186
1179
1187
1180
tx .vout [0 ] = CTxOut (tx .vout [0 ].nValue , script_pubkey )
1188
1181
tx .rehash ()
@@ -1201,8 +1194,7 @@ def test_witness_input_length(self):
1201
1194
"""Test that vin length must match vtxinwit length."""
1202
1195
1203
1196
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 )
1206
1198
1207
1199
# Create a transaction that splits our utxo into many outputs
1208
1200
tx = CTransaction ()
@@ -1309,8 +1301,7 @@ def test_tx_relay_after_segwit_activation(self):
1309
1301
1310
1302
# Now try to add extra witness data to a valid witness tx.
1311
1303
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 )
1314
1305
tx2 = CTransaction ()
1315
1306
tx2 .vin .append (CTxIn (COutPoint (tx_hash , 0 ), b"" ))
1316
1307
tx2 .vout .append (CTxOut (tx .vout [0 ].nValue - 1000 , script_pubkey ))
@@ -1472,8 +1463,7 @@ def test_premature_coinbase_witness_spend(self):
1472
1463
block = self .build_next_block ()
1473
1464
# Change the output of the block to be a witness output.
1474
1465
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 )
1477
1467
block .vtx [0 ].vout [0 ].scriptPubKey = script_pubkey
1478
1468
# This next line will rehash the coinbase and update the merkle
1479
1469
# root, and solve.
@@ -1520,7 +1510,7 @@ def test_uncompressed_pubkey(self):
1520
1510
# Test 1: P2WPKH
1521
1511
# First create a P2WPKH output that uses an uncompressed pubkey
1522
1512
pubkeyhash = hash160 (pubkey )
1523
- script_pkh = CScript ([ OP_0 , pubkeyhash ] )
1513
+ script_pkh = key_to_p2wpkh_script ( pubkey )
1524
1514
tx = CTransaction ()
1525
1515
tx .vin .append (CTxIn (COutPoint (utxo .sha256 , utxo .n ), b"" ))
1526
1516
tx .vout .append (CTxOut (utxo .nValue - 1000 , script_pkh ))
@@ -1534,8 +1524,7 @@ def test_uncompressed_pubkey(self):
1534
1524
# Now try to spend it. Send it to a P2WSH output, which we'll
1535
1525
# use in the next test.
1536
1526
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 )
1539
1528
1540
1529
tx2 = CTransaction ()
1541
1530
tx2 .vin .append (CTxIn (COutPoint (tx .sha256 , 0 ), b"" ))
@@ -1613,8 +1602,7 @@ def test_signature_version_1(self):
1613
1602
pubkey = key .get_pubkey ().get_bytes ()
1614
1603
1615
1604
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 )
1618
1606
1619
1607
# First create a witness output for use in the tests.
1620
1608
tx = CTransaction ()
@@ -1733,7 +1721,7 @@ def test_signature_version_1(self):
1733
1721
1734
1722
# Now test witness version 0 P2PKH transactions
1735
1723
pubkeyhash = hash160 (pubkey )
1736
- script_pkh = CScript ([ OP_0 , pubkeyhash ] )
1724
+ script_pkh = key_to_p2wpkh_script ( pubkey )
1737
1725
tx = CTransaction ()
1738
1726
tx .vin .append (CTxIn (COutPoint (temp_utxos [0 ].sha256 , temp_utxos [0 ].n ), b"" ))
1739
1727
tx .vout .append (CTxOut (temp_utxos [0 ].nValue , script_pkh ))
@@ -1860,7 +1848,7 @@ def test_non_standard_witness(self):
1860
1848
# For each script, generate a pair of P2WSH and P2SH-P2WSH output.
1861
1849
outputvalue = (self .utxo [0 ].nValue - 1000 ) // (len (scripts ) * 2 )
1862
1850
for i in scripts :
1863
- p2wsh = CScript ([ OP_0 , sha256 ( i )] )
1851
+ p2wsh = script_to_p2wsh_script ( i )
1864
1852
p2wsh_scripts .append (p2wsh )
1865
1853
tx .vout .append (CTxOut (outputvalue , p2wsh ))
1866
1854
tx .vout .append (CTxOut (outputvalue , script_to_p2sh_script (p2wsh )))
@@ -1877,13 +1865,13 @@ def test_non_standard_witness(self):
1877
1865
for i in range (len (scripts )):
1878
1866
p2wsh_tx = CTransaction ()
1879
1867
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"" )])))
1881
1869
p2wsh_tx .wit .vtxinwit .append (CTxInWitness ())
1882
1870
p2wsh_tx .rehash ()
1883
1871
p2wsh_txs .append (p2wsh_tx )
1884
1872
p2sh_tx = CTransaction ()
1885
1873
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"" )])))
1887
1875
p2sh_tx .wit .vtxinwit .append (CTxInWitness ())
1888
1876
p2sh_tx .rehash ()
1889
1877
p2sh_txs .append (p2sh_tx )
@@ -1978,8 +1966,7 @@ def test_witness_sigops(self):
1978
1966
1979
1967
# Keep this under MAX_OPS_PER_SCRIPT (201)
1980
1968
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 )
1983
1970
1984
1971
sigops_per_script = 20 * 5 + 193 * 1
1985
1972
# We'll produce 2 extra outputs, one with a program that would take us
@@ -1995,14 +1982,12 @@ def test_witness_sigops(self):
1995
1982
# N(=MAX_SIGOP_COST//sigops_per_script) outputs of our transaction,
1996
1983
# would push us just over the block sigop limit.
1997
1984
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 )
2000
1986
2001
1987
# If we spend this script instead, we would exactly reach our sigop
2002
1988
# limit (for witness sigops).
2003
1989
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 )
2006
1991
2007
1992
# First split our available utxo into a bunch of outputs
2008
1993
split_value = self .utxo [0 ].nValue // outputs
@@ -2135,8 +2120,7 @@ def received_wtxidrelay():
2135
2120
# Create a Segwit output from the latest UTXO
2136
2121
# and announce it to the network
2137
2122
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 )
2140
2124
2141
2125
tx = CTransaction ()
2142
2126
tx .vin .append (CTxIn (COutPoint (self .utxo [0 ].sha256 , self .utxo [0 ].n ), b"" ))
0 commit comments