Skip to content

Commit

Permalink
Document @solana/assertions with TypeDoc (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher authored Jan 20, 2025
1 parent fabec8f commit cd07559
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/assertions/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SOLANA_ERROR__CRYPTO__RANDOM_VALUES_FUNCTION_UNIMPLEMENTED, SolanaError } from '@solana/errors';

/**
* Throws an exception unless {@link Crypto#getRandomValues | `crypto.getRandomValues()`} is
* available in the current JavaScript environment.
*/
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
9 changes: 7 additions & 2 deletions packages/assertions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export * from './subtle-crypto';

/**
* This package contains utilities for asserting that a JavaScript environment supports certain
* features necessary for the operation of the Solana JavaScript SDK.
*
* @packageDocumentation
*/
export * from './crypto';
export * from './subtle-crypto';
21 changes: 20 additions & 1 deletion packages/assertions/src/subtle-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ async function isEd25519CurveSupported(subtle: SubtleCrypto): Promise<boolean> {
}
}

/**
* Throws an exception unless {@link SubtleCrypto#digest | `crypto.subtle.digest()`} is available in
* the current JavaScript environment.
*/
export function assertDigestCapabilityIsAvailable() {
assertIsSecureContext();
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.digest !== 'function') {
throw new SolanaError(SOLANA_ERROR__SUBTLE_CRYPTO__DIGEST_UNIMPLEMENTED);
}
}

/**
* Throws an exception unless {@link SubtleCrypto#generateKey | `crypto.subtle.generateKey()`} is
* available in the current JavaScript environment and has support for the Ed25519 curve.
*/
export async function assertKeyGenerationIsAvailable() {
assertIsSecureContext();
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.generateKey !== 'function') {
Expand All @@ -53,20 +61,31 @@ export async function assertKeyGenerationIsAvailable() {
}
}

/**
* Throws an exception unless {@link SubtleCrypto#exportKey | `crypto.subtle.exportKey()`} is
* available in the current JavaScript environment.
*/
export function assertKeyExporterIsAvailable() {
assertIsSecureContext();
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.exportKey !== 'function') {
throw new SolanaError(SOLANA_ERROR__SUBTLE_CRYPTO__EXPORT_FUNCTION_UNIMPLEMENTED);
}
}

/**
* Throws an exception unless {@link SubtleCrypto#sign | `crypto.subtle.sign()`} is available in the
* current JavaScript environment.
*/
export function assertSigningCapabilityIsAvailable() {
assertIsSecureContext();
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.sign !== 'function') {
throw new SolanaError(SOLANA_ERROR__SUBTLE_CRYPTO__SIGN_FUNCTION_UNIMPLEMENTED);
}
}

/**
* Throws an exception unless {@link SubtleCrypto#verify | `crypto.subtle.verify()`} is available in
* the current JavaScript environment.
*/
export function assertVerificationCapabilityIsAvailable() {
assertIsSecureContext();
if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle?.verify !== 'function') {
Expand Down
3 changes: 2 additions & 1 deletion packages/assertions/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../typedoc.base.json"],
"entryPoints": ["src/index.ts"]
"entryPoints": ["src/index.ts"],
"readme": "none"
}

0 comments on commit cd07559

Please sign in to comment.