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).
This commit is contained in:
Sebastian Falbesoner
2021-12-25 21:31:49 +01:00
parent 9bec5b80a0
commit e704d4d26f
2 changed files with 33 additions and 10 deletions

View File

@ -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):