diff options
author | W. J. van der Laan <laanwj@protonmail.com> | 2021-10-07 15:26:58 +0200 |
---|---|---|
committer | W. J. van der Laan <laanwj@protonmail.com> | 2021-10-07 15:41:57 +0200 |
commit | 991753e4d50ea5c973f4d3330e5afba797b1b1e7 (patch) | |
tree | 09fbf6bf32d81c32bbbd5c24b7710a4c8a6a6312 /test/functional/test_framework | |
parent | 6334ff7364e23e883c40a7ed234a467fcd08fd3b (diff) | |
parent | 429b49378ee3a3d73abe276cfd176c1ca08bf9b9 (diff) |
Merge bitcoin/bitcoin#23118: test: refactor: introduce `script_util` helper for creating P2PK scripts
429b49378ee3a3d73abe276cfd176c1ca08bf9b9 test: introduce script_util helper for creating P2PK scripts (Sebastian Falbesoner)
Pull request description:
This PR is a follow-up to #22363, which took use of already existing `script_util` helpers to get rid of manual CScript for the P2{PKH,SH,WPKH,WSH} output types, in order to increase readability and maintainability of the test code. Here the same is done for P2PK scripts by introducing a helper `key_to_p2pk_script` and using it. Note that the helper only accepts ECDSA pubkeys (i.e. ones with a size of 33 or 65 bytes), hence it can't be used for scripts in the form of [x-only-pubkey, OP_CHECKSIG].
ACKs for top commit:
brunoerg:
Code review ACK 429b49378ee3a3d73abe276cfd176c1ca08bf9b9
laanwj:
Code review ACK 429b49378ee3a3d73abe276cfd176c1ca08bf9b9
rajarshimaitra:
Concept + tACK https://github.com/bitcoin/bitcoin/pull/23118/commits/429b49378ee3a3d73abe276cfd176c1ca08bf9b9
Tree-SHA512: 984aea01eba5f38a328d69905d90a3a36f0a02419ca3e5baf3c8095895fb094e3780c7da16fad5851db3847bdb05ce8cda244ab09b79b8aa9602dfb3c5e0414c
Diffstat (limited to 'test/functional/test_framework')
-rw-r--r-- | test/functional/test_framework/blocktools.py | 4 | ||||
-rwxr-xr-x | test/functional/test_framework/script_util.py | 13 | ||||
-rw-r--r-- | test/functional/test_framework/wallet.py | 8 |
3 files changed, 16 insertions, 9 deletions
diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index 6de372cd8e..85e3c2a383 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -33,11 +33,11 @@ from .script import ( CScriptOp, OP_1, OP_CHECKMULTISIG, - OP_CHECKSIG, OP_RETURN, OP_TRUE, ) from .script_util import ( + key_to_p2pk_script, key_to_p2wpkh_script, script_to_p2wsh_script, ) @@ -134,7 +134,7 @@ def create_coinbase(height, pubkey=None, extra_output_script=None, fees=0, nValu coinbaseoutput.nValue >>= halvings coinbaseoutput.nValue += fees if pubkey is not None: - coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG]) + coinbaseoutput.scriptPubKey = key_to_p2pk_script(pubkey) else: coinbaseoutput.scriptPubKey = CScript([OP_TRUE]) coinbase.vout = [coinbaseoutput] diff --git a/test/functional/test_framework/script_util.py b/test/functional/test_framework/script_util.py index e0dfce4c2f..82a9067dd2 100755 --- a/test/functional/test_framework/script_util.py +++ b/test/functional/test_framework/script_util.py @@ -5,14 +5,14 @@ """Useful Script constants and utils.""" from test_framework.script import ( CScript, - hash160, - sha256, OP_0, - OP_DUP, - OP_HASH160, OP_CHECKSIG, + OP_DUP, OP_EQUAL, OP_EQUALVERIFY, + OP_HASH160, + hash160, + sha256, ) # To prevent a "tx-size-small" policy rule error, a transaction has to have a @@ -36,6 +36,11 @@ DUMMY_P2WPKH_SCRIPT = CScript([b'a' * 21]) DUMMY_2_P2WPKH_SCRIPT = CScript([b'b' * 21]) +def key_to_p2pk_script(key): + key = check_key(key) + return CScript([key, OP_CHECKSIG]) + + def keyhash_to_p2pkh_script(hash): assert len(hash) == 20 return CScript([OP_DUP, OP_HASH160, hash, OP_EQUALVERIFY, OP_CHECKSIG]) diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py index d597af5883..b0b578e544 100644 --- a/test/functional/test_framework/wallet.py +++ b/test/functional/test_framework/wallet.py @@ -24,12 +24,14 @@ from test_framework.messages import ( from test_framework.script import ( CScript, LegacySignatureHash, - OP_CHECKSIG, OP_TRUE, OP_NOP, SIGHASH_ALL, ) -from test_framework.script_util import key_to_p2wpkh_script +from test_framework.script_util import ( + key_to_p2pk_script, + key_to_p2wpkh_script, +) from test_framework.util import ( assert_equal, assert_greater_than_or_equal, @@ -75,7 +77,7 @@ class MiniWallet: self._priv_key = ECKey() self._priv_key.set((1).to_bytes(32, 'big'), True) pub_key = self._priv_key.get_pubkey() - self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG])) + self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes()) elif mode == MiniWalletMode.ADDRESS_OP_TRUE: self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey']) |