//! 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
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 {
//! 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
* 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>
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
}
if (b) {
return bytesSet(a, b, o)
}
- const t = new Bytes(getByteLength(a))
+ const t = allocate(getByteLength(a))
bytesSet(t, a)
return t
}
}
/** 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)
}
/**
*/
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)
}
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++) {
}
if (d >> 4) {
fill(out, 0)
- throw new TypeError('Invalid hexadecimal character')
+ throw new Nano25519TypeError('Invalid hexadecimal character')
}
}
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)
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>`)
}
//! 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
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 {
//! 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
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()
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)
//@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
}
}
-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
/**
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