Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Add getPublicKeyFromPrivateKey helper #3049

Merged
merged 1 commit into from
Aug 7, 2024
Merged

Conversation

lorisleiva
Copy link
Contributor

@lorisleiva lorisleiva commented Aug 2, 2024

This PR adds a getPublicKeyFromPrivateKey helper method that can be used to retrieve the CryptoKey public key from an extractable CryptoKey private key.

import { createPrivateKeyFromBytes, getPublicKeyFromPrivateKey } from '@solana/keys';

const privateKey = await createPrivateKeyFromBytes(new Uint8Array([...]), true);

const publicKey = await getPublicKeyFromPrivateKey(privateKey);
const extractablePublicKey = await getPublicKeyFromPrivateKey(privateKey, true);

Copy link

changeset-bot bot commented Aug 2, 2024

🦋 Changeset detected

Latest commit: 0a4c8ae

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@lorisleiva lorisleiva changed the title Add getPublicKeyFromPrivateKey helper Add getPublicKeyFromPrivateKey helper Aug 2, 2024
@lorisleiva lorisleiva force-pushed the loris/public-key-helper branch 2 times, most recently from 806a29c to 7153986 Compare August 2, 2024 13:42
@lorisleiva lorisleiva marked this pull request as ready for review August 2, 2024 13:43
@lorisleiva lorisleiva force-pushed the loris/public-key-helper branch from 7153986 to 981ad42 Compare August 2, 2024 14:01
beforeEach(async () => {
privateKey = await createPrivateKeyFromBytes(MOCK_PRIVATE_KEY_BYTES, false);
});
it('cannot get the associated public key', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for us to check if it's extractable? I'm wondering if we should add a Solana error for this case because I'm skeptical that subtle crypto will give a nice error message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good shout! I've added a new SOLANA_ERROR__SUBTLE_CRYPTO__CANNOT_EXPORT_NON_EXTRACTABLE_KEY error code.

Copy link
Contributor

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

Wow! I didn't think this was possible! Amazing.

This can't go in without this PR also including an update to the polyfill. The only reason the tests pass here is that the tests run in a haz-ed25519 environment. This will puke in a browser that doesn't.

To be clear, you need to add a polyfill that implements at least jwk export for private keys and import for public keys. Hint: the x and d coordinates are base64URL encoded.

packages/keys/src/public-key.ts Outdated Show resolved Hide resolved
packages/keys/src/public-key.ts Outdated Show resolved Hide resolved
@lorisleiva
Copy link
Contributor Author

Thanks @steveluscher, I'll apply your suggestions and prepend PRs to the stack for the polyfill.

@lorisleiva lorisleiva force-pushed the loris/public-key-helper branch from 981ad42 to 06734a1 Compare August 5, 2024 11:31
@lorisleiva lorisleiva changed the base branch from master to loris/crypto-polyfill-import-jwk August 5, 2024 11:31
@lorisleiva lorisleiva force-pushed the loris/crypto-polyfill-import-jwk branch from f4af5f1 to b4a8fbf Compare August 5, 2024 11:36
@lorisleiva lorisleiva force-pushed the loris/public-key-helper branch 3 times, most recently from 5a9de85 to 526072b Compare August 5, 2024 11:51
Copy link
Contributor

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

Added via Giphy

@lorisleiva lorisleiva force-pushed the loris/crypto-polyfill-import-jwk branch from b4a8fbf to 4f3be6a Compare August 7, 2024 08:09
@lorisleiva lorisleiva force-pushed the loris/public-key-helper branch from 526072b to 99f14b3 Compare August 7, 2024 08:09
Copy link
Contributor Author

lorisleiva commented Aug 7, 2024

Merge activity

  • Aug 7, 4:34 AM EDT: @lorisleiva started a stack merge that includes this pull request via Graphite.
  • Aug 7, 4:39 AM EDT: Graphite rebased this pull request as part of a merge.
  • Aug 7, 4:40 AM EDT: @lorisleiva merged this pull request with Graphite.

@lorisleiva lorisleiva changed the base branch from loris/crypto-polyfill-import-jwk to graphite-base/3049 August 7, 2024 08:35
@lorisleiva lorisleiva changed the base branch from graphite-base/3049 to master August 7, 2024 08:37
@lorisleiva lorisleiva force-pushed the loris/public-key-helper branch from 99f14b3 to 0a4c8ae Compare August 7, 2024 08:38
@lorisleiva lorisleiva merged commit f9a8446 into master Aug 7, 2024
8 checks passed
@lorisleiva lorisleiva deleted the loris/public-key-helper branch August 7, 2024 08:40
@github-actions github-actions bot mentioned this pull request Aug 7, 2024
Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2024
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