From: Chris Duncan Date: Sun, 11 Oct 2026 14:28:06 +0000 (-0700) Subject: Throw custom errors only and move their definitions so they don't need to be imported... X-Git-Tag: v2.0.0~1^2~26 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=dfba91eb87d27b567eda0aef3f0e51b927f63d83;p=nano25519.git Throw custom errors only and move their definitions so they don't need to be imported to primordials module. Freeze constants when exported. --- diff --git a/src/lib/derive.ts b/src/lib/derive.ts index 0c90b34..c09cbea 100644 --- a/src/lib/derive.ts +++ b/src/lib/derive.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from './primordials' +import { Nano25519TypeError, allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './primordials' import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm' const { KEY_BYTELENGTH, } = constants @@ -10,7 +10,7 @@ const publicKey = allocate(KEY_BYTELENGTH) export function derive (prv: unknown, out?: unknown): string | void { if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === KEY_BYTELENGTH)) { - throw new TypeError('Derive output buffer must be 32-byte Uint8Array') + throw new Nano25519TypeError('Derive output buffer must be 32-byte Uint8Array') } Mutex.lock() try { diff --git a/src/lib/errors.ts b/src/lib/errors.ts index 678d277..b417634 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -1,14 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -export class Nano25519MutexError extends Error { } -Object.defineProperty(Nano25519MutexError, 'name', { value: 'Nano25519MutexError' }) -export class Nano25519RangeError extends RangeError { } -Object.defineProperty(Nano25519RangeError, 'name', { value: 'Nano25519RangeError' }) -export class Nano25519TypeError extends TypeError { } -Object.defineProperty(Nano25519TypeError, 'name', { value: 'Nano25519TypeError' }) -export class Nano25519WasmError extends Error { } -Object.defineProperty(Nano25519WasmError, 'name', { value: 'Nano25519WasmError' }) +import { Nano25519WasmError, } from './primordials' /** * Message text for the codes in `src/assembly/errors.ts`. The numbers are the diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index 7f2029e..77b5fb0 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -38,8 +38,37 @@ * bundler will order that dependency first, and any call it makes into here * during its own evaluation will read captures that are still unassigned. */ -const ObjectCreate = Object.create -const ObjectDefineProperty = Object.defineProperty +const objectCreate = Object.create +export const objectDefineProperty = Object.defineProperty +export const objectFreeze = Object.freeze + +/** Custom errors */ +class Nano25519MutexError extends Error { } +objectDefineProperty(Nano25519MutexError, 'name', { value: 'Nano25519MutexError' }) +objectFreeze(Nano25519MutexError) +objectFreeze(Nano25519MutexError.prototype) + +class Nano25519RangeError extends RangeError { } +objectDefineProperty(Nano25519RangeError, 'name', { value: 'Nano25519RangeError' }) +objectFreeze(Nano25519RangeError) +objectFreeze(Nano25519RangeError.prototype) + +class Nano25519TypeError extends TypeError { } +objectDefineProperty(Nano25519TypeError, 'name', { value: 'Nano25519TypeError' }) +objectFreeze(Nano25519TypeError) +objectFreeze(Nano25519TypeError.prototype) + +class Nano25519WasmError extends Error { } +objectDefineProperty(Nano25519WasmError, 'name', { value: 'Nano25519WasmError' }) +objectFreeze(Nano25519WasmError) +objectFreeze(Nano25519WasmError.prototype) + +export { + Nano25519MutexError, + Nano25519RangeError, + Nano25519TypeError, + Nano25519WasmError +} /** Type aliases for legibility. */ export type Bytes = Uint8Array @@ -56,7 +85,7 @@ const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill) function get (target: object, key: PropertyKey): (this: unknown) => T { const found = Object.getOwnPropertyDescriptor(target, key) if (typeof found?.get !== 'function') { - throw new TypeError(`Missing expected accessor while capturing builtins`) + throw new Nano25519TypeError(`Missing expected accessor while capturing builtins`) } return found.get } @@ -104,7 +133,7 @@ export function copy (a: Uint8Array, b?: Uint8Array, o?: number): void | Bytes { if (b) { return bytesSet(a, b, o) } - const t = new Bytes(getByteLength(a)) + const t = allocate(getByteLength(a)) bytesSet(t, a) return t } @@ -127,16 +156,16 @@ export function isBytes (a: unknown): a is Bytes { } /** Checks system endianness. Returns true if little-endian, else false. */ -export const isLE = new Bytes(getBuffer(new Words([1])))[0] === 1 +export const isLE = allocate(getBuffer(new Words([1])))[0] === 1 /** Sets `array[index] = value`, avoiding prototypes and getters/setters. */ export function slot (array: T[], index: number, value: T): void { - const descriptor = ObjectCreate(null) + const descriptor = objectCreate(null) descriptor.value = value descriptor.writable = true descriptor.enumerable = true descriptor.configurable = true - ObjectDefineProperty(array, index, descriptor) + objectDefineProperty(array, index, descriptor) } /** @@ -150,11 +179,11 @@ export function slot (array: T[], index: number, value: T): void { */ export function words (bytes: Bytes): Words { if (!isBytes(bytes)) { - throw new TypeError('Error creating Uint32Array', { cause: bytes }) + throw new Nano25519TypeError('Error creating Uint32Array', { cause: bytes }) } const offset = getByteOffset(bytes) if (offset !== 0) { - throw new RangeError('Unexpected byte offset', { cause: offset }) + throw new Nano25519RangeError('Unexpected byte offset', { cause: offset }) } return new Words(getBuffer(bytes) as ArrayBuffer, offset, getByteLength(bytes) >> 2) } @@ -198,11 +227,11 @@ export function bytesToHex (bytes: Uint8Array): string { function hexToBytes (out: Bytes, hex: string): void { const length = hex.length if (length & 1) { - throw new TypeError(`Invalid hexadecimal length ${length}`) + throw new Nano25519TypeError(`Invalid hexadecimal length ${length}`) } const byteLength = getByteLength(out) if (byteLength !== (length >> 1)) { - throw new TypeError(`Invalid output byte length ${byteLength}`) + throw new Nano25519TypeError(`Invalid output byte length ${byteLength}`) } let d = 0 for (let i = 0, b = 0; i < length; i += 2, b++) { @@ -213,7 +242,7 @@ function hexToBytes (out: Bytes, hex: string): void { } if (d >> 4) { fill(out, 0) - throw new TypeError('Invalid hexadecimal character') + throw new Nano25519TypeError('Invalid hexadecimal character') } } @@ -233,7 +262,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n export function normalize (name: string, byteLengthMin: number, byteLengthMax: number, value: unknown, out?: Bytes): Bytes { if (typeof value === 'string') { if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) { - throw new TypeError(`Invalid hexadecimal length ${value.length} for ${name}`) + throw new Nano25519TypeError(`Invalid hexadecimal length ${value.length} for ${name}`) } if (typeof out === 'undefined') out = allocate(value.length >> 1) hexToBytes(out, value) @@ -243,15 +272,15 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n if (isBytes(value)) { const byteLength = getByteLength(value) if (byteLength < byteLengthMin) { - throw new TypeError(`${name} must be at least ${byteLengthMin} bytes`) + throw new Nano25519TypeError(`${name} must be at least ${byteLengthMin} bytes`) } if (byteLength > byteLengthMax) { - throw new TypeError(`${name} must be no more than ${byteLengthMax} bytes`) + throw new Nano25519TypeError(`${name} must be no more than ${byteLengthMax} bytes`) } if (typeof out === 'undefined') out = allocate(byteLength) copy(out, value) return out } - throw new TypeError(`${name} must be a hex string or Uint8Array`) + throw new Nano25519TypeError(`${name} must be a hex string or Uint8Array`) } diff --git a/src/lib/sign.ts b/src/lib/sign.ts index 5f638f4..e77ff93 100644 --- a/src/lib/sign.ts +++ b/src/lib/sign.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from './primordials' +import { Nano25519TypeError, allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize, } from './primordials' import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm' const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants @@ -11,7 +11,7 @@ const signature = allocate(SIGNATURE_BYTELENGTH) export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown): string | void { if (typeof out !== 'undefined' && !(isBytes(out) && byteLength(out) === SIGNATURE_BYTELENGTH)) { - throw new TypeError(`Sign output buffer must be ${SIGNATURE_BYTELENGTH}-byte Uint8Array`) + throw new Nano25519TypeError(`Sign output buffer must be ${SIGNATURE_BYTELENGTH}-byte Uint8Array`) } Mutex.lock() try { diff --git a/src/lib/verify.ts b/src/lib/verify.ts index df551d6..80a0c80 100644 --- a/src/lib/verify.ts +++ b/src/lib/verify.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { allocate, byteLength, copy, isArray, normalize, slot } from './primordials' +import { Nano25519RangeError, Nano25519TypeError, allocate, byteLength, copy, isArray, normalize, slot, } from './primordials' import { Mutex, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm' const { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, SIGNATURE_BYTELENGTH, } = constants @@ -30,10 +30,10 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { const blocks: unknown[] = isArray(data) ? data : [data] const count = blocks.length if (count < 1) { - throw new RangeError('Must pass at least one block', { cause: data }) + throw new Nano25519RangeError('Must pass at least one block', { cause: data }) } if (MAX_VERIFY_BLOCKS < count) { - throw new RangeError(`Bulk Nano block verification must be no more than ${MAX_VERIFY_BLOCKS} blocks`, { cause: count }) + throw new Nano25519RangeError(`Bulk Nano block verification must be no more than ${MAX_VERIFY_BLOCKS} blocks`, { cause: count }) } const verified: boolean[] = [] Mutex.lock() @@ -44,13 +44,13 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { for (let b = 0; b < count; b++) { const block = blocks[b] if (block == null || typeof block !== 'object') { - throw new TypeError('Invalid block', { cause: block }) + throw new Nano25519TypeError('Invalid block', { cause: block }) } if (!('signature' in block)) { - throw new TypeError('Block signature is required to verify', { cause: block }) + throw new Nano25519TypeError('Block signature is required to verify', { cause: block }) } if (!('hash' in block)) { - throw new TypeError('Block hash is required to verify', { cause: block }) + throw new Nano25519TypeError('Block hash is required to verify', { cause: block }) } normalize('signature', SIGNATURE_BYTELENGTH, SIGNATURE_BYTELENGTH, block.signature, signature) normalize('hash', BLOCKHASH_BYTELENGTH, BLOCKHASH_BYTELENGTH, block.hash, hash) diff --git a/src/lib/wasm.ts b/src/lib/wasm.ts index c7997e1..c0a7538 100644 --- a/src/lib/wasm.ts +++ b/src/lib/wasm.ts @@ -3,8 +3,8 @@ //@ts-expect-error import nano25519_wasm from '../../build/nano25519.wasm' -import { Nano25519MutexError, Nano25519TypeError, abort, errorCodes, } from './errors' -import { Bytes, Words, byteLength, copy, isLE, words } from './primordials' +import { abort, errorCodes, } from './errors' +import { Bytes, Nano25519MutexError, Nano25519TypeError, Words, byteLength, copy, isLE, objectFreeze, words, } from './primordials' type Exports = WebAssembly.Instance['exports'] & { clearMemory: () => void @@ -94,14 +94,6 @@ for (const errorCode of Object.keys(exports)) { } } -const BLOCKHASH_BYTELENGTH = exports.BLOCKHASH_BYTELENGTH.value -const KEY_BYTELENGTH = exports.KEY_BYTELENGTH.value -const SIGNATURE_BYTELENGTH = exports.SIGNATURE_BYTELENGTH.value - -const MAX_VERIFY_BLOCKS = exports.MAX_VERIFY_BLOCKS.value -const MAX_VERIFY_BLOCKS_BYTELENGTH = exports.MAX_VERIFY_BLOCKS_BYTELENGTH.value -const MAX_MESSAGE_BYTELENGTH = exports.MAX_MESSAGE_BYTELENGTH.value - const setInputView = isLE ? words : (b: Bytes): Bytes => b /** @@ -308,14 +300,14 @@ function setInputSig (sig: Bytes): void { setInputSigEndian(s) } -export const constants = { - BLOCKHASH_BYTELENGTH, - KEY_BYTELENGTH, - SIGNATURE_BYTELENGTH, - MAX_VERIFY_BLOCKS, - MAX_VERIFY_BLOCKS_BYTELENGTH, - MAX_MESSAGE_BYTELENGTH -} +export const constants = objectFreeze({ + BLOCKHASH_BYTELENGTH: exports.BLOCKHASH_BYTELENGTH.value, + KEY_BYTELENGTH: exports.KEY_BYTELENGTH.value, + SIGNATURE_BYTELENGTH: exports.SIGNATURE_BYTELENGTH.value, + MAX_VERIFY_BLOCKS: exports.MAX_VERIFY_BLOCKS.value, + MAX_VERIFY_BLOCKS_BYTELENGTH: exports.MAX_VERIFY_BLOCKS_BYTELENGTH.value, + MAX_MESSAGE_BYTELENGTH: exports.MAX_MESSAGE_BYTELENGTH.value +}) /** * Copies the bytes of a specified input parameter into the associated static