test: implement 'bech32m' mode for getnewdestination() helper

This commit is contained in:
Sebastian Falbesoner 2022-06-07 01:47:54 +02:00
parent 1999dcfa40
commit dcf36fe8e3
2 changed files with 15 additions and 8 deletions

View File

@ -43,7 +43,7 @@ class RpcCreateMultiSigTest(BitcoinTestFramework):
if self.is_bdb_compiled(): if self.is_bdb_compiled():
self.final = node2.getnewaddress() self.final = node2.getnewaddress()
else: else:
self.final = getnewdestination()[2] self.final = getnewdestination('bech32')[2]
def run_test(self): def run_test(self):
node0, node1, node2 = self.nodes node0, node1, node2 = self.nodes
@ -66,9 +66,7 @@ class RpcCreateMultiSigTest(BitcoinTestFramework):
# Test mixed compressed and uncompressed pubkeys # Test mixed compressed and uncompressed pubkeys
self.log.info('Mixed compressed and uncompressed multisigs are not allowed') self.log.info('Mixed compressed and uncompressed multisigs are not allowed')
pk0 = getnewdestination()[0].hex() pk0, pk1, pk2 = [getnewdestination('bech32')[0].hex() for _ in range(3)]
pk1 = getnewdestination()[0].hex()
pk2 = getnewdestination()[0].hex()
# decompress pk2 # decompress pk2
pk_obj = ECPubKey() pk_obj = ECPubKey()

View File

@ -19,9 +19,13 @@ from test_framework.address import (
key_to_p2pkh, key_to_p2pkh,
key_to_p2sh_p2wpkh, key_to_p2sh_p2wpkh,
key_to_p2wpkh, key_to_p2wpkh,
output_key_to_p2tr,
) )
from test_framework.descriptors import descsum_create from test_framework.descriptors import descsum_create
from test_framework.key import ECKey from test_framework.key import (
ECKey,
compute_xonly_pubkey,
)
from test_framework.messages import ( from test_framework.messages import (
COIN, COIN,
COutPoint, COutPoint,
@ -38,6 +42,7 @@ from test_framework.script import (
OP_NOP, OP_NOP,
OP_TRUE, OP_TRUE,
SIGHASH_ALL, SIGHASH_ALL,
taproot_construct,
) )
from test_framework.script_util import ( from test_framework.script_util import (
key_to_p2pk_script, key_to_p2pk_script,
@ -286,10 +291,10 @@ class MiniWallet:
return txid return txid
def getnewdestination(address_type='bech32'): def getnewdestination(address_type='bech32m'):
"""Generate a random destination of the specified type and return the """Generate a random destination of the specified type and return the
corresponding public key, scriptPubKey and address. Supported types are corresponding public key, scriptPubKey and address. Supported types are
'legacy', 'p2sh-segwit' and 'bech32'. Can be used when a random 'legacy', 'p2sh-segwit', 'bech32' and 'bech32m'. Can be used when a random
destination is needed, but no compiled wallet is available (e.g. as destination is needed, but no compiled wallet is available (e.g. as
replacement to the getnewaddress/getaddressinfo RPCs).""" replacement to the getnewaddress/getaddressinfo RPCs)."""
key = ECKey() key = ECKey()
@ -304,7 +309,11 @@ def getnewdestination(address_type='bech32'):
elif address_type == 'bech32': elif address_type == 'bech32':
scriptpubkey = key_to_p2wpkh_script(pubkey) scriptpubkey = key_to_p2wpkh_script(pubkey)
address = key_to_p2wpkh(pubkey) address = key_to_p2wpkh(pubkey)
# TODO: also support bech32m (need to generate x-only-pubkey) elif address_type == 'bech32m':
tap = taproot_construct(compute_xonly_pubkey(key.get_bytes())[0])
pubkey = tap.output_pubkey
scriptpubkey = tap.scriptPubKey
address = output_key_to_p2tr(pubkey)
else: else:
assert False assert False
return pubkey, scriptpubkey, address return pubkey, scriptpubkey, address