Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

conversion of a public key, regardless of compression, to an address #2632

Closed
wants to merge 3 commits into from

Conversation

nanaknihal
Copy link
Contributor

@nanaknihal nanaknihal commented Oct 10, 2023

This commit exposes a method to convert public keys to addresses. It is tested against the public key examples on https://docs.ethers.org/v5/api/utils/address/#utils-computeAddress. It does not convert private keys to address because Wallet already does that.

Motivation

Often, it is helpful to compute the address from the public key, especially in threshold signature schemes, but as far is I can tell there is no convenient function to do so. ethers.js exposes a computeAddress, but I could not find an ethers-rs function to do the same. Those unfamiliar with elliptic curves typically would rather avoid dealing with the question of how to hash a public key, as they would likely have to study point encoding to do it.

Solution

This provides a simple function to convert a public key to an Ethereum address, regardless of compression.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@mattsse
Copy link
Collaborator

mattsse commented Oct 10, 2023

defer to @prestwich here

Copy link
Collaborator

@prestwich prestwich left a comment

Choose a reason for hiding this comment

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

method is good, thanks for adding a test :)

nits:

  • needs clippy and fmt to pass CI
  • move the function impl up around L390, it should be just before or just after secret_key_to_address
  • refactor secret_key_to_address to use this method
  • rename to public_key_to_address to match the other method

:)

@nanaknihal
Copy link
Contributor Author

Thanks. I made the above fixes, hopefully it is good to go :) I also changed the return type to Address instead of slice. It should be noted the new version of secret_key_to_address now has to do a bit of extra computation: it must convert a public key from/to bytes an extra time now in order to use secret_key_to_address. But I imagine that's a worthwhile tradeoff for modular code.

@prestwich
Copy link
Collaborator

I was gonna push a small refactor to your address, but your branch protections prevented it. so I have #2635

@prestwich
Copy link
Collaborator

closing this as the commits are now included in main. Thank you for your contribution!

@prestwich prestwich closed this Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants