Skip to content

Commit

Permalink
merge bitcoin#22363: use script_util helpers for creating P2{PKH,SH…
Browse files Browse the repository at this point in the history
…} scripts

excludes:
- 61b6a01 (Dash doesn't support SegWit)
- 905d672 (see above)
  • Loading branch information
kwvg committed Jan 4, 2025
1 parent bbb6599 commit 24eb44c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
17 changes: 12 additions & 5 deletions test/functional/data/invalid_txs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@
CTxOut,
MAX_MONEY,
)
from test_framework import script as sc
from test_framework.blocktools import create_tx_with_script, MAX_BLOCK_SIGOPS

basic_p2sh = sc.CScript([sc.OP_HASH160, sc.hash160(sc.CScript([sc.OP_0])), sc.OP_EQUAL])
from test_framework.script import (
CScript,
OP_0,
OP_CHECKSIG,
OP_TRUE,
)
from test_framework.script_util import (
script_to_p2sh_script,
)
basic_p2sh = script_to_p2sh_script(CScript([OP_0]))


class BadTxTemplate:
Expand Down Expand Up @@ -95,7 +102,7 @@ class SizeTooSmall(BadTxTemplate):
def get_tx(self):
tx = CTransaction()
tx.vin.append(self.valid_txin)
tx.vout.append(CTxOut(0, sc.CScript([sc.OP_TRUE])))
tx.vout.append(CTxOut(0, CScript([OP_TRUE])))
tx.calc_sha256()
return tx

Expand Down Expand Up @@ -209,7 +216,7 @@ class TooManySigops(BadTxTemplate):
expect_disconnect = False

def get_tx(self):
lotsa_checksigs = sc.CScript([sc.OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
lotsa_checksigs = CScript([OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
return create_tx_with_script(
self.spend_tx, 0,
script_pub_key=lotsa_checksigs,
Expand Down
23 changes: 16 additions & 7 deletions test/functional/feature_addressindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
# Test addressindex generation and fetching
#

from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
from test_framework.messages import (
COutPoint,
CTransaction,
CTxIn,
CTxOut,
COIN,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_node import ErrorMatch
from test_framework.script import CScript, OP_CHECKSIG, OP_DUP, OP_EQUAL, OP_EQUALVERIFY, OP_HASH160
from test_framework.script_util import (
keyhash_to_p2pkh_script,
scripthash_to_p2sh_script,
)
from test_framework.util import assert_equal

class AddressIndexTest(BitcoinTestFramework):
Expand Down Expand Up @@ -127,7 +136,7 @@ def run_test(self):
# Check that outputs with the same address will only return one txid
self.log.info("Testing for txid uniqueness...")
addressHash = bytes.fromhex("FE30B718DCF0BF8A2A686BF1820C073F8B2C3B37")
scriptPubKey = CScript([OP_HASH160, addressHash, OP_EQUAL])
scriptPubKey = scripthash_to_p2sh_script(addressHash)
unspent = self.nodes[0].listunspent()
tx = CTransaction()
tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))]
Expand All @@ -153,7 +162,7 @@ def run_test(self):
privkey2 = "cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc"
address2 = "yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3"
addressHash2 = bytes.fromhex("C5E4FB9171C22409809A3E8047A29C83886E325D")
scriptPubKey2 = CScript([OP_DUP, OP_HASH160, addressHash2, OP_EQUALVERIFY, OP_CHECKSIG])
scriptPubKey2 = keyhash_to_p2pkh_script(addressHash2)
self.nodes[0].importprivkey(privkey2)

unspent = self.nodes[0].listunspent()
Expand Down Expand Up @@ -245,9 +254,9 @@ def run_test(self):
privKey3 = "cRyrMvvqi1dmpiCmjmmATqjAwo6Wu7QTjKu1ABMYW5aFG4VXW99K"
address3 = "yWB15aAdpeKuSaQHFVJpBDPbNSLZJSnDLA"
addressHash3 = bytes.fromhex("6C186B3A308A77C779A9BB71C3B5A7EC28232A13")
scriptPubKey3 = CScript([OP_DUP, OP_HASH160, addressHash3, OP_EQUALVERIFY, OP_CHECKSIG])
scriptPubKey3 = keyhash_to_p2pkh_script(addressHash3)
# address4 = "2N8oFVB2vThAKury4vnLquW2zVjsYjjAkYQ"
scriptPubKey4 = CScript([OP_HASH160, addressHash3, OP_EQUAL])
scriptPubKey4 = scripthash_to_p2sh_script(addressHash3)
unspent = self.nodes[2].listunspent()

tx = CTransaction()
Expand Down Expand Up @@ -310,7 +319,7 @@ def run_test(self):
privkey1 = "cMvZn1pVWntTEcsK36ZteGQXRAcZ8CoTbMXF1QasxBLdnTwyVQCc"
address1 = "yM9Eed1bxjy7tYxD3yZDHxjcVT48WdRoB1"
address1hash = bytes.fromhex("0909C84A817651502E020AAD0FBCAE5F656E7D8A")
address1script = CScript([OP_DUP, OP_HASH160, address1hash, OP_EQUALVERIFY, OP_CHECKSIG])
address1script = keyhash_to_p2pkh_script(address1hash)

self.nodes[0].sendtoaddress(address1, 10)
self.generate(self.nodes[0], 1)
Expand Down
9 changes: 4 additions & 5 deletions test/functional/feature_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@
OP_CHECKSIGVERIFY,
OP_ELSE,
OP_ENDIF,
OP_EQUAL,
OP_DROP,
OP_FALSE,
OP_HASH160,
OP_IF,
OP_INVALIDOPCODE,
OP_RETURN,
OP_TRUE,
SIGHASH_ALL,
SignatureHash,
hash160,
)
from test_framework.script_util import (
script_to_p2sh_script,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
Expand Down Expand Up @@ -484,8 +484,7 @@ def run_test(self):

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

# Create a transaction that spends one satoshi to the p2sh_script, the rest to OP_TRUE
# This must be signed because it is spending a coinbase
Expand Down
10 changes: 5 additions & 5 deletions test/functional/feature_fee_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
OP_1,
OP_2,
OP_DROP,
OP_EQUAL,
OP_HASH160,
OP_TRUE,
hash160,
)
from test_framework.script_util import (
script_to_p2sh_script,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
Expand All @@ -37,8 +37,8 @@
# time signing.
REDEEM_SCRIPT_1 = CScript([OP_1, OP_DROP])
REDEEM_SCRIPT_2 = CScript([OP_2, OP_DROP])
P2SH_1 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_1), OP_EQUAL])
P2SH_2 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_2), OP_EQUAL])
P2SH_1 = script_to_p2sh_script(REDEEM_SCRIPT_1)
P2SH_2 = script_to_p2sh_script(REDEEM_SCRIPT_2)

# Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
SCRIPT_SIG = [CScript([OP_TRUE, REDEEM_SCRIPT_1]), CScript([OP_TRUE, REDEEM_SCRIPT_2])]
Expand Down
16 changes: 12 additions & 4 deletions test/functional/feature_spentindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@

from decimal import Decimal

from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
from test_framework.script import CScript, OP_CHECKSIG, OP_DUP, OP_EQUALVERIFY, OP_HASH160
from test_framework.messages import (
COutPoint,
CTransaction,
CTxIn,
CTxOut,
COIN,
)
from test_framework.script_util import (
keyhash_to_p2pkh_script,
)
from test_framework.test_node import ErrorMatch
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
Expand Down Expand Up @@ -62,7 +70,7 @@ def run_test(self):

privkey = "cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc"
addressHash = bytes.fromhex("C5E4FB9171C22409809A3E8047A29C83886E325D")
scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG])
scriptPubKey = keyhash_to_p2pkh_script(addressHash)
unspent = self.nodes[0].listunspent()
tx = CTransaction()
tx_fee = Decimal('0.00001')
Expand Down Expand Up @@ -100,7 +108,7 @@ def run_test(self):
# Check that verbose raw transaction includes address values and input values
address2 = "yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3"
addressHash2 = bytes.fromhex("C5E4FB9171C22409809A3E8047A29C83886E325D")
scriptPubKey2 = CScript([OP_DUP, OP_HASH160, addressHash2, OP_EQUALVERIFY, OP_CHECKSIG])
scriptPubKey2 = keyhash_to_p2pkh_script(addressHash2)
tx2 = CTransaction()
tx2.vin = [CTxIn(COutPoint(int(txid, 16), 0))]
tx2.vout = [CTxOut(amount - int(COIN / 10), scriptPubKey2)]
Expand Down
13 changes: 10 additions & 3 deletions test/functional/feature_txindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
# Test txindex generation and fetching
#

from test_framework.messages import COutPoint, CTransaction, CTxIn, CTxOut
from test_framework.script import CScript, OP_CHECKSIG, OP_DUP, OP_EQUALVERIFY, OP_HASH160
from test_framework.messages import (
COutPoint,
CTransaction,
CTxIn,
CTxOut,
)
from test_framework.script_util import (
keyhash_to_p2pkh_script,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal

Expand Down Expand Up @@ -46,7 +53,7 @@ def run_test(self):
self.log.info("Testing transaction index...")

addressHash = bytes.fromhex("C5E4FB9171C22409809A3E8047A29C83886E325D")
scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG])
scriptPubKey = keyhash_to_p2pkh_script(addressHash)
unspent = self.nodes[0].listunspent()
tx = CTransaction()
tx_fee_sat = 1000
Expand Down
7 changes: 4 additions & 3 deletions test/functional/mempool_accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
tx_from_hex,
)
from test_framework.script import (
hash160,
CScript,
OP_0,
OP_2,
OP_3,
OP_CHECKMULTISIG,
OP_EQUAL,
OP_HASH160,
OP_RETURN,
)
from test_framework.script_util import (
script_to_p2sh_script,
)
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
Expand Down Expand Up @@ -306,7 +307,7 @@ def run_test(self):
rawtxs=[tx.serialize().hex()],
)
tx = tx_from_hex(raw_tx_reference)
output_p2sh_burn = CTxOut(nValue=540, scriptPubKey=CScript([OP_HASH160, hash160(b'burn'), OP_EQUAL]))
output_p2sh_burn = CTxOut(nValue=540, scriptPubKey=script_to_p2sh_script(b'burn'))
num_scripts = 100000 // len(output_p2sh_burn.serialize()) # Use enough outputs to make the tx too large for our policy
tx.vout = [output_p2sh_burn] * num_scripts
self.check_mempool_result(
Expand Down
18 changes: 7 additions & 11 deletions test/functional/test_framework/wallet_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
OP_2,
OP_3,
OP_CHECKMULTISIG,
OP_CHECKSIG,
OP_DUP,
OP_EQUAL,
OP_EQUALVERIFY,
OP_HASH160,
hash160,
)
from test_framework.script_util import (
key_to_p2pkh_script,
script_to_p2sh_script,
)

Key = namedtuple('Key', ['privkey',
Expand All @@ -41,10 +39,9 @@ def get_key(node):
Returns a named tuple of privkey, pubkey and all address and scripts."""
addr = node.getnewaddress()
pubkey = node.getaddressinfo(addr)['pubkey']
pkh = hash160(bytes.fromhex(pubkey))
return Key(privkey=node.dumpprivkey(addr),
pubkey=pubkey,
p2pkh_script=CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(),
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
p2pkh_addr=key_to_p2pkh(pubkey))

def get_generate_key():
Expand All @@ -55,10 +52,9 @@ def get_generate_key():
eckey.generate()
privkey = bytes_to_wif(eckey.get_bytes())
pubkey = eckey.get_pubkey().get_bytes().hex()
pkh = hash160(bytes.fromhex(pubkey))
return Key(privkey=privkey,
pubkey=pubkey,
p2pkh_script=CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(),
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
p2pkh_addr=key_to_p2pkh(pubkey))

def get_multisig(node):
Expand All @@ -74,7 +70,7 @@ def get_multisig(node):
script_code = CScript([OP_2] + [bytes.fromhex(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
pubkeys=pubkeys,
p2sh_script=CScript([OP_HASH160, hash160(script_code), OP_EQUAL]).hex(),
p2sh_script=script_to_p2sh_script(script_code).hex(),
p2sh_addr=script_to_p2sh(script_code),
redeem_script=script_code.hex())

Expand Down

0 comments on commit 24eb44c

Please sign in to comment.