|
9 | 9 | from enum import Enum
|
10 | 10 | from random import choice
|
11 | 11 | from typing import Optional
|
12 |
| -from test_framework.address import create_deterministic_address_bcrt1_p2tr_op_true |
| 12 | +from test_framework.address import ( |
| 13 | + create_deterministic_address_bcrt1_p2tr_op_true, |
| 14 | + key_to_p2pkh, |
| 15 | + key_to_p2sh_p2wpkh, |
| 16 | + key_to_p2wpkh, |
| 17 | +) |
13 | 18 | from test_framework.descriptors import descsum_create
|
14 | 19 | from test_framework.key import ECKey
|
15 | 20 | from test_framework.messages import (
|
|
31 | 36 | )
|
32 | 37 | from test_framework.script_util import (
|
33 | 38 | key_to_p2pk_script,
|
| 39 | + key_to_p2pkh_script, |
| 40 | + key_to_p2sh_p2wpkh_script, |
34 | 41 | key_to_p2wpkh_script,
|
35 | 42 | )
|
36 | 43 | from test_framework.util import (
|
@@ -209,12 +216,28 @@ def sendrawtransaction(self, *, from_node, tx_hex):
|
209 | 216 | return txid
|
210 | 217 |
|
211 | 218 |
|
212 |
| -def random_p2wpkh(): |
213 |
| - """Generate a random P2WPKH scriptPubKey. Can be used when a random destination is needed, |
214 |
| - but no compiled wallet is available (e.g. as replacement to the getnewaddress RPC).""" |
| 219 | +def getnewdestination(address_type='bech32'): |
| 220 | + """Generate a random destination of the specified type and return the |
| 221 | + corresponding public key, scriptPubKey and address. Supported types are |
| 222 | + 'legacy', 'p2sh-segwit' and 'bech32'. Can be used when a random |
| 223 | + destination is needed, but no compiled wallet is available (e.g. as |
| 224 | + replacement to the getnewaddress/getaddressinfo RPCs).""" |
215 | 225 | key = ECKey()
|
216 | 226 | key.generate()
|
217 |
| - return key_to_p2wpkh_script(key.get_pubkey().get_bytes()) |
| 227 | + pubkey = key.get_pubkey().get_bytes() |
| 228 | + if address_type == 'legacy': |
| 229 | + scriptpubkey = key_to_p2pkh_script(pubkey) |
| 230 | + address = key_to_p2pkh(pubkey) |
| 231 | + elif address_type == 'p2sh-segwit': |
| 232 | + scriptpubkey = key_to_p2sh_p2wpkh_script(pubkey) |
| 233 | + address = key_to_p2sh_p2wpkh(pubkey) |
| 234 | + elif address_type == 'bech32': |
| 235 | + scriptpubkey = key_to_p2wpkh_script(pubkey) |
| 236 | + address = key_to_p2wpkh(pubkey) |
| 237 | + # TODO: also support bech32m (need to generate x-only-pubkey) |
| 238 | + else: |
| 239 | + assert False |
| 240 | + return pubkey, scriptpubkey, address |
218 | 241 |
|
219 | 242 |
|
220 | 243 | def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):
|
|
0 commit comments