PR #18788 (commit 08067aebfd7e838e6ce6b030c31a69422260fc6f) introduced functions to generate output scripts for various types. This PR replaces all manual CScript creations in the P2PKH, P2SH, P2WPKH, P2WSH formats with those helpers in order to increase readability and maintainability over the functional test codebase. The first commit fixes a bug in the wallet_util helper module w.r.t. to P2SH-P2WSH script creation (the result is not used in any test so far, hence it can still be seen as refactoring).
The following table shows a summary of the output script patterns tackled in this PR:
| Type | master branch | PR branch |
|---|---|---|
| P2PKH | CScript([OP_DUP, OP_HASH160, hash160(key), OP_EQUALVERIFY, OP_CHECKSIG]) |
key_to_p2pkh_script(key) |
CScript([OP_DUP, OP_HASH160, keyhash, OP_EQUALVERIFY, OP_CHECKSIG]) |
keyhash_to_p2pkh_script(keyhash) |
|
| P2SH | CScript([OP_HASH160, hash160(script), OP_EQUAL]) |
script_to_p2sh_script(script) |
| P2WPKH | CScript([OP_0, hash160(key)]) |
key_to_p2wpkh_script(key) |
| P2WSH | CScript([OP_0, sha256(script)]) |
script_to_p2wsh_script(script) |
Note that the key_to_... helpers can't be used if an invalid key size (not 33 or 65 bytes) is passed, which is the case in some rare instances where the scripts still have to be created manually.
Possible follow-up ideas:
- further simplify by identifying P2SH-wrapped scripts and using
key_to_p2sh_p2wpkh_script()andscript_to_p2sh_p2wsh_script()helpers - introduce and use
key_to_p2pk_script()helper for P2PK scripts