Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor: introduce generate_keypair helper with WIF support #27733

Merged
merged 1 commit into from
Jun 21, 2023

Conversation

theStack
Copy link
Contributor

@theStack theStack commented May 24, 2023

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).

@DrahtBot
Copy link
Contributor

DrahtBot commented May 24, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK stratospher, instagibbs, kevkevinpal

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

Copy link
Contributor

@kevkevinpal kevkevinpal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code review ACK 25f19c0

added one question

also ran grep for generate_wif_key in ./test on this commit 25f19c0 and seems like all them are gone

test/functional/test_framework/wallet_util.py Outdated Show resolved Hide resolved
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).
@theStack
Copy link
Contributor Author

Addressed review comment #27733 (comment) and rebased on master -- with the merge of #25634 there was another ECKey instance in wallet_blank.py that could be replaced with the new generate_keypair helper.

Changes since the initial version can be reviewed via $ git range-diff 25f19c01...1a572ce7.

Copy link
Contributor

@stratospher stratospher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 1a572ce. neat to have this since keypair generation is done in lots of places.

@fanquake fanquake requested review from instagibbs and removed request for kevkevinpal June 20, 2023 08:57
@instagibbs
Copy link
Member

ACK 1a572ce

There are 3 remaining uses of ECKey outside of this new utility function, each of which are deterministic uses.

@DrahtBot DrahtBot requested review from kevkevinpal and removed request for instagibbs June 20, 2023 13:38
@kevkevinpal
Copy link
Contributor

reACK 1a572ce

@DrahtBot DrahtBot removed the request for review from kevkevinpal June 20, 2023 15:27
@fanquake fanquake merged commit 7d65e33 into bitcoin:master Jun 21, 2023
@theStack theStack deleted the generate_wif_bytes_keypair branch June 21, 2023 10:11
k = ECKey()
k.generate()
return bytes_to_wif(k.get_bytes(), k.is_compressed)
def generate_keypair(compressed=True, wif=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit to force named args:

def generate_keypair(*, compressed=True, wif=False):

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 21, 2023
joostjager pushed a commit to joostjager/bitcoin that referenced this pull request Jun 26, 2023
…per with WIF support

1a572ce test: refactor: introduce `generate_keypair` helper with WIF support (Sebastian Falbesoner)

Pull request description:

  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).

ACKs for top commit:
  instagibbs:
    ACK bitcoin@1a572ce
  kevkevinpal:
    reACK [1a572ce](bitcoin@1a572ce)
  stratospher:
    ACK 1a572ce. neat to have this since keypair generation is done in lots of places.

Tree-SHA512: ceb695ba7b34dc9f65357b55be03e67609e7e13a178083d405284eff4d8d3c5cea4fb0b6632658604a533f38ebfefc33e0c375995cc21ebc7843442ad764287b
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Oct 20, 2023
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)

Github-Pull: bitcoin#27733
Rebased-From: 1a572ce [partial]
@bitcoin bitcoin locked and limited conversation to collaborators Jun 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants