Skip to content

Commit e704d4d

Browse files
committed
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).
1 parent 9bec5b8 commit e704d4d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

test/functional/p2p_filter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from test_framework.test_framework import BitcoinTestFramework
3232
from test_framework.wallet import (
3333
MiniWallet,
34-
random_p2wpkh,
34+
getnewdestination,
3535
)
3636

3737

@@ -169,14 +169,14 @@ def test_filter(self, filter_peer):
169169

170170
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
171171
filter_peer.tx_received = False
172-
block_hash = self.generatetoscriptpubkey(random_p2wpkh())
172+
block_hash = self.generatetoscriptpubkey(getnewdestination()[1])
173173
filter_peer.wait_for_merkleblock(block_hash)
174174
assert not filter_peer.tx_received
175175

176176
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')
177177
filter_peer.merkleblock_received = False
178178
filter_peer.tx_received = False
179-
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=random_p2wpkh(), amount=7 * COIN)
179+
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=7 * COIN)
180180
filter_peer.sync_send_with_ping()
181181
assert not filter_peer.merkleblock_received
182182
assert not filter_peer.tx_received
@@ -190,14 +190,14 @@ def test_filter(self, filter_peer):
190190
self.log.info('Check that after deleting filter all txs get relayed again')
191191
filter_peer.send_and_ping(msg_filterclear())
192192
for _ in range(5):
193-
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=random_p2wpkh(), amount=7 * COIN)
193+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=7 * COIN)
194194
filter_peer.wait_for_tx(txid)
195195

196196
self.log.info('Check that request for filtered blocks is ignored if no filter is set')
197197
filter_peer.merkleblock_received = False
198198
filter_peer.tx_received = False
199199
with self.nodes[0].assert_debug_log(expected_msgs=['received getdata']):
200-
block_hash = self.generatetoscriptpubkey(random_p2wpkh())
200+
block_hash = self.generatetoscriptpubkey(getnewdestination()[1])
201201
filter_peer.wait_for_inv([CInv(MSG_BLOCK, int(block_hash, 16))])
202202
filter_peer.sync_with_ping()
203203
assert not filter_peer.merkleblock_received

test/functional/test_framework/wallet.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
from enum import Enum
1010
from random import choice
1111
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+
)
1318
from test_framework.descriptors import descsum_create
1419
from test_framework.key import ECKey
1520
from test_framework.messages import (
@@ -31,6 +36,8 @@
3136
)
3237
from test_framework.script_util import (
3338
key_to_p2pk_script,
39+
key_to_p2pkh_script,
40+
key_to_p2sh_p2wpkh_script,
3441
key_to_p2wpkh_script,
3542
)
3643
from test_framework.util import (
@@ -209,12 +216,28 @@ def sendrawtransaction(self, *, from_node, tx_hex):
209216
return txid
210217

211218

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)."""
215225
key = ECKey()
216226
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
218241

219242

220243
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):

0 commit comments

Comments
 (0)