This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 928
Add getPublicKeyFromPrivateKey
helper
#3049
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
'@solana/keys': patch | ||
--- | ||
|
||
Add a `getPublicKeyFromPrivateKey` helper that, given an extractable `CryptoKey` private key, gets the corresponding public key as a `CryptoKey`. | ||
|
||
```ts | ||
import { createPrivateKeyFromBytes, getPublicKeyFromPrivateKey } from '@solana/keys'; | ||
|
||
const privateKey = await createPrivateKeyFromBytes(new Uint8Array([...]), true); | ||
|
||
const publicKey = await getPublicKeyFromPrivateKey(privateKey); | ||
const extractablePublicKey = await getPublicKeyFromPrivateKey(privateKey, true); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { SOLANA_ERROR__SUBTLE_CRYPTO__CANNOT_EXPORT_NON_EXTRACTABLE_KEY, SolanaError } from '@solana/errors'; | ||
|
||
import { createPrivateKeyFromBytes } from '../private-key'; | ||
import { getPublicKeyFromPrivateKey } from '../public-key'; | ||
|
||
const MOCK_PRIVATE_KEY_BYTES = new Uint8Array([ | ||
0xeb, 0xfa, 0x65, 0xeb, 0x93, 0xdc, 0x79, 0x15, 0x7a, 0xba, 0xde, 0xa2, 0xf7, 0x94, 0x37, 0x9d, 0xfc, 0x07, 0x1d, | ||
0x68, 0x86, 0x87, 0x37, 0x6d, 0xc5, 0xd5, 0xa0, 0x54, 0x12, 0x1d, 0x34, 0x4a, | ||
]); | ||
|
||
const EXPECTED_MOCK_PUBLIC_KEY_BYTES = new Uint8Array([ | ||
0x1d, 0x0e, 0x93, 0x86, 0x4d, 0xcc, 0x81, 0x5f, 0xc3, 0xf2, 0x86, 0x18, 0x09, 0x11, 0xd0, 0x0a, 0x3f, 0xd2, 0x06, | ||
0xde, 0x31, 0xa1, 0xc9, 0x42, 0x87, 0xcb, 0x43, 0xf0, 0x5f, 0xc9, 0xf2, 0xb5, | ||
]); | ||
|
||
describe('getPublicKeyFromPrivateKey', () => { | ||
describe('given an extractable private key', () => { | ||
let privateKey: CryptoKey; | ||
beforeEach(async () => { | ||
privateKey = await createPrivateKeyFromBytes(MOCK_PRIVATE_KEY_BYTES, true); | ||
}); | ||
it('gets the associated public key', async () => { | ||
expect.assertions(1); | ||
const publicKey = await getPublicKeyFromPrivateKey(privateKey, true); | ||
const publicKeyBytes = new Uint8Array(await crypto.subtle.exportKey('raw', publicKey)); | ||
expect(publicKeyBytes).toEqual(EXPECTED_MOCK_PUBLIC_KEY_BYTES); | ||
}); | ||
it('can get an extractable public key', async () => { | ||
expect.assertions(1); | ||
const publicKey = await getPublicKeyFromPrivateKey(privateKey, true); | ||
expect(publicKey.extractable).toBe(true); | ||
}); | ||
it('can get a non-extractable public key', async () => { | ||
expect.assertions(1); | ||
const publicKey = await getPublicKeyFromPrivateKey(privateKey, false); | ||
expect(publicKey.extractable).toBe(false); | ||
}); | ||
it('returns a non-extractable public key by default', async () => { | ||
expect.assertions(1); | ||
const publicKey = await getPublicKeyFromPrivateKey(privateKey); | ||
expect(publicKey.extractable).toBe(false); | ||
}); | ||
}); | ||
describe('given a non-extractable private key', () => { | ||
let privateKey: CryptoKey; | ||
beforeEach(async () => { | ||
privateKey = await createPrivateKeyFromBytes(MOCK_PRIVATE_KEY_BYTES, false); | ||
}); | ||
it('cannot get the associated public key', async () => { | ||
expect.assertions(1); | ||
await expect(() => getPublicKeyFromPrivateKey(privateKey, true)).rejects.toThrow( | ||
new SolanaError(SOLANA_ERROR__SUBTLE_CRYPTO__CANNOT_EXPORT_NON_EXTRACTABLE_KEY, { key: privateKey }), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { getPublicKeyFromPrivateKey } from '../public-key'; | ||
|
||
getPublicKeyFromPrivateKey(new CryptoKey()) satisfies Promise<CryptoKey>; | ||
getPublicKeyFromPrivateKey(new CryptoKey(), true) satisfies Promise<CryptoKey>; | ||
getPublicKeyFromPrivateKey(new CryptoKey(), false) satisfies Promise<CryptoKey>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { assertKeyExporterIsAvailable } from '@solana/assertions'; | ||
import { SOLANA_ERROR__SUBTLE_CRYPTO__CANNOT_EXPORT_NON_EXTRACTABLE_KEY, SolanaError } from '@solana/errors'; | ||
|
||
export async function getPublicKeyFromPrivateKey( | ||
privateKey: CryptoKey, | ||
extractable: boolean = false, | ||
): Promise<CryptoKey> { | ||
assertKeyExporterIsAvailable(); | ||
|
||
if (privateKey.extractable === false) { | ||
throw new SolanaError(SOLANA_ERROR__SUBTLE_CRYPTO__CANNOT_EXPORT_NON_EXTRACTABLE_KEY, { key: privateKey }); | ||
} | ||
|
||
// Export private key. | ||
const jwk = await crypto.subtle.exportKey('jwk', privateKey); | ||
|
||
// Import public key. | ||
return await crypto.subtle.importKey( | ||
'jwk', | ||
{ | ||
crv /* curve */: 'Ed25519', | ||
ext /* extractable */: extractable, | ||
key_ops /* key operations */: ['verify'], | ||
kty /* key type */: 'OKP' /* octet key pair */, | ||
x /* public key x-coordinate */: jwk.x, | ||
}, | ||
'Ed25519', | ||
extractable, | ||
['verify'], | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.