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

Commit

Permalink
refactor(experimental): a function that tells you whether a `CryptoKe…
Browse files Browse the repository at this point in the history
…y` belongs to the polyfill or not

## Summary

Only keys produced with the polyfill's `generateKey` method are usable with its other methods. Having been produced with the polyfill keygen is the same as ‘being present in the key store,’ hence this simple test.

## Test Plan


```
cd packages/webcrypto-ed25519-polyfill
pnpm test:unit:browser
pnpm test:unit:node
```
  • Loading branch information
steveluscher committed Jul 18, 2023
1 parent 457d032 commit 7520bc4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateKeyPolyfill } from '../secrets';
import { generateKeyPolyfill, isPolyfilledKey } from '../secrets';

describe('generateKeyPolyfill', () => {
it('stores secret key bytes in an internal cache', () => {
Expand Down Expand Up @@ -88,3 +88,30 @@ describe('generateKeyPolyfill', () => {
});
});
});

describe('isPolyfilledKey', () => {
it('returns true when given a public key produced with generateKeyPolyfill()', () => {
const key = generateKeyPolyfill(/* extractable */ false, ['sign', 'verify']);
expect(isPolyfilledKey(key.publicKey)).toBe(true);
});
it('returns true when given a private key produced with generateKeyPolyfill()', () => {
const key = generateKeyPolyfill(/* extractable */ false, ['sign', 'verify']);
expect(isPolyfilledKey(key.privateKey)).toBe(true);
});
it('returns false when given a public key produced with the native keygen', async () => {
expect.assertions(1);
const key = (await crypto.subtle.generateKey('Ed25519', /* extractable */ false, [
'sign',
'verify',
])) as CryptoKeyPair;
expect(isPolyfilledKey(key.publicKey)).toBe(false);
});
it('returns false when given a private key produced with the native keygen', async () => {
expect.assertions(1);
const key = (await crypto.subtle.generateKey('Ed25519', /* extractable */ false, [
'sign',
'verify',
])) as CryptoKeyPair;
expect(isPolyfilledKey(key.privateKey)).toBe(false);
});
});
4 changes: 4 additions & 0 deletions packages/webcrypto-ed25519-polyfill/src/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ export function generateKeyPolyfill(extractable: boolean, keyUsages: readonly Ke
const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
return keyPair;
}

export function isPolyfilledKey(key: CryptoKey): boolean {
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT?.has(key);
}

0 comments on commit 7520bc4

Please sign in to comment.