diff options
author | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-12-25 21:31:49 +0100 |
---|---|---|
committer | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-12-26 12:10:52 +0100 |
commit | e704d4d26f62f83068dc7d4082ab9b57bc33d1fb (patch) | |
tree | 33b004fa74e0d44f8111632f6995c95fa1d0e3a4 /test/functional/test_framework | |
parent | 9bec5b80a0f3b134246497b06017bce790ec4444 (diff) |
test: introduce `getnewdestination` helper for generating various address types
This serves as a replacement for the getnewaddress RPC if no wallet is
available. In addition to the address, it also returns the corresponding
public key and output script (scriptPubKey).
Diffstat (limited to 'test/functional/test_framework')
-rw-r--r-- | test/functional/test_framework/wallet.py | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py index f724cb2af3..d7c78bdeb6 100644 --- a/test/functional/test_framework/wallet.py +++ b/test/functional/test_framework/wallet.py @@ -9,7 +9,12 @@ from decimal import Decimal from enum import Enum from random import choice from typing import Optional -from test_framework.address import create_deterministic_address_bcrt1_p2tr_op_true +from test_framework.address import ( + create_deterministic_address_bcrt1_p2tr_op_true, + key_to_p2pkh, + key_to_p2sh_p2wpkh, + key_to_p2wpkh, +) from test_framework.descriptors import descsum_create from test_framework.key import ECKey from test_framework.messages import ( @@ -31,6 +36,8 @@ from test_framework.script import ( ) from test_framework.script_util import ( key_to_p2pk_script, + key_to_p2pkh_script, + key_to_p2sh_p2wpkh_script, key_to_p2wpkh_script, ) from test_framework.util import ( @@ -209,12 +216,28 @@ class MiniWallet: return txid -def random_p2wpkh(): - """Generate a random P2WPKH scriptPubKey. Can be used when a random destination is needed, - but no compiled wallet is available (e.g. as replacement to the getnewaddress RPC).""" +def getnewdestination(address_type='bech32'): + """Generate a random destination of the specified type and return the + corresponding public key, scriptPubKey and address. Supported types are + 'legacy', 'p2sh-segwit' and 'bech32'. Can be used when a random + destination is needed, but no compiled wallet is available (e.g. as + replacement to the getnewaddress/getaddressinfo RPCs).""" key = ECKey() key.generate() - return key_to_p2wpkh_script(key.get_pubkey().get_bytes()) + pubkey = key.get_pubkey().get_bytes() + if address_type == 'legacy': + scriptpubkey = key_to_p2pkh_script(pubkey) + address = key_to_p2pkh(pubkey) + elif address_type == 'p2sh-segwit': + scriptpubkey = key_to_p2sh_p2wpkh_script(pubkey) + address = key_to_p2sh_p2wpkh(pubkey) + elif address_type == 'bech32': + scriptpubkey = key_to_p2wpkh_script(pubkey) + address = key_to_p2wpkh(pubkey) + # TODO: also support bech32m (need to generate x-only-pubkey) + else: + assert False + return pubkey, scriptpubkey, address def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE): |