]> git.codecow.com Git - nano25519.git/commitdiff
Throw custom errors only and move their definitions so they don't need to be imported...
authorChris Duncan <chris@codecow.com>
Sun, 11 Oct 2026 14:28:06 +0000 (07:28 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 11 Oct 2026 14:28:06 +0000 (07:28 -0700)
src/lib/derive.ts
src/lib/errors.ts
src/lib/primordials.ts
src/lib/sign.ts
src/lib/verify.ts
src/lib/wasm.ts

index 0c90b3456fc7984e305366bccd54f0048a7af7c9..c09cbeade44ceb8aa61d27fc77b92ef54e303a26 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! 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<ArrayBuffer>')
+               throw new Nano25519TypeError('Derive output buffer must be 32-byte Uint8Array<ArrayBuffer>')
        }
        Mutex.lock()
        try {
index 678d2775bf51fa72d1a4f4d99d291d2991891574..b4176347887bd6a19dba2e548ec14a52bce6b642 100644 (file)
@@ -1,14 +1,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! 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
index 7f2029e3755c3524dfbccfea5d8ff75dbd39f082..77b5fb02eeaae83a3fe9d60133b28dd8955fade1 100644 (file)
  * 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<ArrayBuffer>
@@ -56,7 +85,7 @@ const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill)
 function get<T> (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<T> (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<T> (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<ArrayBuffer>`)
+       throw new Nano25519TypeError(`${name} must be a hex string or Uint8Array<ArrayBuffer>`)
 }
index 5f638f41fa953ea3802e9307370902f68d8cd2fd..e77ff939999e755cc1c0c72508b7e5a5fad71ba9 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! 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<ArrayBuffer>`)
+               throw new Nano25519TypeError(`Sign output buffer must be ${SIGNATURE_BYTELENGTH}-byte Uint8Array<ArrayBuffer>`)
        }
        Mutex.lock()
        try {
index df551d6d3bf0c694083a00e89ecc0c4689de515d..80a0c80f4fafb48cf1fe85f5c9e983fa9e411c2b 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! 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)
index c7997e1470c0885b4cc1a44246285f2829caeba9..c0a753891808c9f4c537af231c2511875ad4f415 100644 (file)
@@ -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