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

Add getPublicKeyFromAddress to derive public keys from addresses #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

beeman
Copy link
Contributor

@beeman beeman commented Feb 12, 2025

This commit introduces getPublicKeyFromAddress, which allows deriving a CryptoKey from a given Solana address. This is useful for verifying signatures when only the address is available.

Problem

There is no convenient way to derive a CryptoKey from an Address.

Summary of Changes

  • Adds getPublicKeyFromAddress to public-key.ts
  • Ensures the function properly asserts valid addresses before decoding
  • Updates tests to check valid and invalid address handling
  • Uses crypto.subtle.importKey to create an Ed25519 verification key

Fixes #

https://solana.stackexchange.com/questions/18940/how-to-convert-address-to-pubkey-in-solana-web3-js-v2

This commit introduces getPublicKeyFromAddress, which allows deriving a CryptoKey from a given Solana address. This is useful for verifying signatures when only the address is available.

 - Adds getPublicKeyFromAddress to public-key.ts
 - Ensures the function properly asserts valid addresses before decoding
 - Updates tests to check valid and invalid address handling
 - Uses crypto.subtle.importKey to create an Ed25519 verification key
Copy link

changeset-bot bot commented Feb 12, 2025

⚠️ No Changeset found

Latest commit: 0cf052d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Collaborator

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

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

I wonder if the fact that your key operations were so far away from the Firefox fix (ie. ED25519_ALGORITHM_IDENTIFIER) is an indication that these functions should actually live in @solana/keys.

It's a bit late for that, but… maybe not too late?

cc/ @lorisleiva

assertIsAddress(address);
const addressBytes = getAddressEncoder().encode(address);

return await crypto.subtle.importKey('raw', addressBytes, 'Ed25519', extractable, ['verify']);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will break in Firefox. See #60.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants