Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Luu committed Mar 23, 2024
1 parent 99ff817 commit 9292acf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
15 changes: 9 additions & 6 deletions packages/assertions/src/__tests__/crypto-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { SOLANA_ERROR__CRYPTO__RANDOM_VALUES_FUNCTION_UNIMPLEMENTED, SolanaError
import { assertPRNGIsAvailable } from '../crypto';

describe('assertPRNGIsAvailable()', () => {
it('resolves to `undefined` without throwing', async () => {
expect.assertions(1);
await expect(assertPRNGIsAvailable()).resolves.toBeUndefined();
describe('when getRandomValues is available', () => {
it('does not throw', () => {
expect(assertPRNGIsAvailable).not.toThrow();
});
it('returns `undefined`', () => {
expect(assertPRNGIsAvailable()).toBeUndefined();
});
});
describe('when getRandomValues is not available', () => {
let oldCrypto: InstanceType<typeof Crypto>['getRandomValues'];
Expand All @@ -18,9 +22,8 @@ describe('assertPRNGIsAvailable()', () => {
afterEach(() => {
globalThis.crypto.getRandomValues = oldCrypto;
});
it('rejects', async () => {
expect.assertions(1);
await expect(() => assertPRNGIsAvailable()).rejects.toThrow(
it('throws', () => {
expect(assertPRNGIsAvailable).toThrow(
new SolanaError(SOLANA_ERROR__CRYPTO__RANDOM_VALUES_FUNCTION_UNIMPLEMENTED),
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/assertions/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SOLANA_ERROR__CRYPTO__RANDOM_VALUES_FUNCTION_UNIMPLEMENTED, SolanaError } from '@solana/errors';

export async function assertPRNGIsAvailable() {
export function assertPRNGIsAvailable() {
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.getRandomValues !== 'function') {
throw new SolanaError(SOLANA_ERROR__CRYPTO__RANDOM_VALUES_FUNCTION_UNIMPLEMENTED);
}
Expand Down
1 change: 1 addition & 0 deletions packages/assertions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './subtle-crypto';
export * from './crypto';
4 changes: 2 additions & 2 deletions packages/keys/src/key-pair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertKeyGenerationIsAvailable, assertPRNGIsAvailable, assert } from '@solana/assertions';
import { assertKeyGenerationIsAvailable, assertPRNGIsAvailable } from '@solana/assertions';
import {
SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH,
SOLANA_ERROR__KEYS__PUBLIC_KEY_MUST_MATCH_PRIVATE_KEY,
Expand All @@ -19,7 +19,7 @@ export async function generateKeyPair(): Promise<CryptoKeyPair> {
}

export async function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair> {
await assertPRNGIsAvailable();
assertPRNGIsAvailable();

if (bytes.byteLength !== 64) {
throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH, { byteLength: bytes.byteLength });
Expand Down

0 comments on commit 9292acf

Please sign in to comment.