mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-29 18:20:58 +02:00
test: refactor: introduce generate_keypair
helper with WIF support
In functional tests it is a quite common scenario to generate fresh elliptic curve keypairs, which is currently a bit cumbersome as it involves multiple steps, e.g.: privkey = ECKey() privkey.generate() privkey_wif = bytes_to_wif(privkey.get_bytes()) pubkey = privkey.get_pubkey().get_bytes() Simplify this by providing a new `generate_keypair` helper function that returns the private key either as `ECKey` object or as WIF-string (depending on the boolean `wif` parameter) and the public key as byte-string; these formats are what we mostly need (currently we don't use `ECPubKey` objects from generated keypairs anywhere). With this, most of the affected code blocks following the pattern above can be replaced by one-liners, e.g.: privkey, pubkey = generate_keypair(wif=True) Note that after this commit, the only direct uses of `ECKey` remain in situations where we want to set the private key explicitly, e.g. in MiniWallet (test/functional/test_framework/wallet.py) or the test for the signet miner script (test/functional/tool_signet_miner.py).
This commit is contained in:
@ -20,6 +20,7 @@ from test_framework.address import (
|
||||
key_to_p2wpkh,
|
||||
output_key_to_p2tr,
|
||||
)
|
||||
from test_framework.blocktools import COINBASE_MATURITY
|
||||
from test_framework.descriptors import descsum_create
|
||||
from test_framework.key import (
|
||||
ECKey,
|
||||
@ -53,7 +54,7 @@ from test_framework.util import (
|
||||
assert_equal,
|
||||
assert_greater_than_or_equal,
|
||||
)
|
||||
from test_framework.blocktools import COINBASE_MATURITY
|
||||
from test_framework.wallet_util import generate_keypair
|
||||
|
||||
DEFAULT_FEE = Decimal("0.0001")
|
||||
|
||||
@ -395,9 +396,7 @@ def getnewdestination(address_type='bech32m'):
|
||||
'legacy', 'p2sh-segwit', 'bech32' and 'bech32m'. 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()
|
||||
pubkey = key.get_pubkey().get_bytes()
|
||||
key, pubkey = generate_keypair()
|
||||
if address_type == 'legacy':
|
||||
scriptpubkey = key_to_p2pkh_script(pubkey)
|
||||
address = key_to_p2pkh(pubkey)
|
||||
|
@ -63,12 +63,9 @@ def get_generate_key():
|
||||
"""Generate a fresh key
|
||||
|
||||
Returns a named tuple of privkey, pubkey and all address and scripts."""
|
||||
eckey = ECKey()
|
||||
eckey.generate()
|
||||
privkey = bytes_to_wif(eckey.get_bytes())
|
||||
pubkey = eckey.get_pubkey().get_bytes().hex()
|
||||
privkey, pubkey = generate_keypair(wif=True)
|
||||
return Key(privkey=privkey,
|
||||
pubkey=pubkey,
|
||||
pubkey=pubkey.hex(),
|
||||
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
|
||||
p2pkh_addr=key_to_p2pkh(pubkey),
|
||||
p2wpkh_script=key_to_p2wpkh_script(pubkey).hex(),
|
||||
@ -114,8 +111,14 @@ def bytes_to_wif(b, compressed=True):
|
||||
b += b'\x01'
|
||||
return byte_to_base58(b, 239)
|
||||
|
||||
def generate_wif_key():
|
||||
# Makes a WIF privkey for imports
|
||||
k = ECKey()
|
||||
k.generate()
|
||||
return bytes_to_wif(k.get_bytes(), k.is_compressed)
|
||||
def generate_keypair(compressed=True, wif=False):
|
||||
"""Generate a new random keypair and return the corresponding ECKey /
|
||||
bytes objects. The private key can also be provided as WIF (wallet
|
||||
import format) string instead, which is often useful for wallet RPC
|
||||
interaction."""
|
||||
privkey = ECKey()
|
||||
privkey.generate(compressed)
|
||||
pubkey = privkey.get_pubkey().get_bytes()
|
||||
if wif:
|
||||
privkey = bytes_to_wif(privkey.get_bytes(), compressed)
|
||||
return privkey, pubkey
|
||||
|
Reference in New Issue
Block a user