From 2669a196f41cc415040c2c260f92d56a6a13493c Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 7 Sep 2026 00:24:41 -0700 Subject: [PATCH] Add Bytes type alias for legibility. --- src/index.ts | 52 ++++++++++++++++++++++++++----------------------- src/lib/wasm.ts | 2 +- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/index.ts b/src/index.ts index 38cb81c..2e90dd3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,70 +5,74 @@ import * as nano25519 from './lib' export { constants } from './lib' +declare global { + type Bytes = Uint8Array +} + /** * Nano public key derivation using WebAssembly. If byte output is required, * pass a target buffer as a second argument. - * @param {(Uint8Array | string)} prv - 32-byte private key + * @param {(Bytes | string)} prv - 32-byte private key * @returns 32-byte public key */ -export function derive (prv: Uint8Array | string): string +export function derive (prv: Bytes | string): string /** * Nano public key derivation using WebAssembly. Instead of allocating an output * buffer internally for the return value, public key bytes are written to the * the user-supplied output buffer. - * @param {(Uint8Array | string)} prv - 32-byte private key - * @param {Uint8Array} out - buffer to receive 32-byte public key + * @param {(Bytes | string)} prv - 32-byte private key + * @param {Bytes} out - buffer to receive 32-byte public key */ -export function derive (prv: Uint8Array | string, out: Uint8Array): void -export function derive (prv: Uint8Array | string, out?: Uint8Array): string | void { +export function derive (prv: Bytes | string, out: Bytes): void +export function derive (prv: Bytes | string, out?: Bytes): string | void { return nano25519.derive(prv, out) } /** * Signing using WebAssembly. To sign Nano blocks, the message should be a * 32-byte block hash. - * @param {(Uint8Array | string)} msg - Variable-byte-length message up to 64 KiB - * @param {(Uint8Array | string)} prv - 32-byte private key - * @param {(Uint8Array | string)} pub - 32-byte public key + * @param {(Bytes | string)} msg - Variable-byte-length message up to 64 KiB + * @param {(Bytes | string)} prv - 32-byte private key + * @param {(Bytes | string)} pub - 32-byte public key * @returns 64-byte detached signature */ -export function sign (msg: Uint8Array | string, prv: Uint8Array | string, pub: Uint8Array | string): string +export function sign (msg: Bytes | string, prv: Bytes | string, pub: Bytes | string): string /** * Signing using WebAssembly. To sign Nano blocks, the message should be a * 32-byte block hash. Instead of allocating an output buffer internally for the * return value, signature bytes are written to the the user-supplied output * buffer. - * @param {(Uint8Array | string)} msg - Variable-byte-length message up to 64 KiB - * @param {(Uint8Array | string)} prv - 32-byte private key - * @param {(Uint8Array | string)} pub - 32-byte public key - * @param {(Uint8Array | string)} out - buffer to receive 64-byte detached signature + * @param {(Bytes | string)} msg - Variable-byte-length message up to 64 KiB + * @param {(Bytes | string)} prv - 32-byte private key + * @param {(Bytes | string)} pub - 32-byte public key + * @param {(Bytes | string)} out - buffer to receive 64-byte detached signature */ -export function sign (msg: Uint8Array | string, prv: Uint8Array | string, pub: Uint8Array | string, out: Uint8Array): void -export function sign (msg: Uint8Array | string, prv: Uint8Array | string, pub: Uint8Array | string, out?: Uint8Array): string | void { +export function sign (msg: Bytes | string, prv: Bytes | string, pub: Bytes | string, out: Bytes): void +export function sign (msg: Bytes | string, prv: Bytes | string, pub: Bytes | string, out?: Bytes): string | void { return nano25519.sign(msg, prv, pub, out) } /** * Signature verification using WebAssembly. To verify Nano block signatures, * use `verify_blocks()` instead. - * @param {(Uint8Array | string)} sig - 64-byte detached signature - * @param {(Uint8Array | string)} msg - Variable-byte-length message up to 64 KiB - * @param {(Uint8Array | string)} pub - 32-byte public key + * @param {(Bytes | string)} sig - 64-byte detached signature + * @param {(Bytes | string)} msg - Variable-byte-length message up to 64 KiB + * @param {(Bytes | string)} pub - 32-byte public key * @returns true if signature matches block hash and public key, else false */ -export function verify (sig: Uint8Array | string, msg: Uint8Array | string, pub: Uint8Array | string): boolean { +export function verify (sig: Bytes | string, msg: Bytes | string, pub: Bytes | string): boolean { return nano25519.verify(sig, msg, pub) } /** * Nano block signature bulk verification using WebAssembly. - * @param {(Uint8Array | string)} publicKey - 32-byte public key + * @param {(Bytes | string)} publicKey - 32-byte public key * @param {object[]} blocks - Array of up to 64 block signature/hash pairs - * @param {(Uint8Array | string)} blocks.signature - 64-byte block signature - * @param {(Uint8Array | string)} blocks.hash - 32-byte block hash + * @param {(Bytes | string)} blocks.signature - 64-byte block signature + * @param {(Bytes | string)} blocks.hash - 32-byte block hash * @returns Array of results with, for each case, true if signature matches block hash and public key, else false */ -export function verify_blocks (publicKey: Uint8Array | string, blocks: { signature: Uint8Array | string, hash: Uint8Array | string }[]): boolean[] { +export function verify_blocks (publicKey: Bytes | string, blocks: { signature: Bytes | string, hash: Bytes | string }[]): boolean[] { return nano25519.verify_blocks(publicKey, blocks) } diff --git a/src/lib/wasm.ts b/src/lib/wasm.ts index 4de9567..38e6ae1 100644 --- a/src/lib/wasm.ts +++ b/src/lib/wasm.ts @@ -45,7 +45,7 @@ export const Mutex = { */ export const memory = new WebAssembly.Memory({ initial: 4 }) -const wasm: Uint8Array = Uint8Array.from(nano25519_wasm) +const wasm: Bytes = Uint8Array.from(nano25519_wasm) const module = new WebAssembly.Module(wasm) export const { exports } = new WebAssembly.Instance(module, { -- 2.52.0