From d244624d8de881babd5912538e77717120252cf1 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 10 Oct 2026 11:46:26 -0700 Subject: [PATCH] Declare keys and signatures outside of try block so they can be properly cleared in finally block if they error out. --- src/lib/derive.ts | 5 +++-- src/lib/sign.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/derive.ts b/src/lib/derive.ts index 3a40eae..dbe7d68 100644 --- a/src/lib/derive.ts +++ b/src/lib/derive.ts @@ -5,15 +5,16 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm' const { KEY_BYTELENGTH, } = constants +let privateKey = allocate(KEY_BYTELENGTH) +let 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') } - const privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv) - const publicKey = allocate(KEY_BYTELENGTH) Mutex.lock() try { + privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv) setInput('prv', privateKey) wasm_derive() for (let i = 0; i < KEY_BYTELENGTH; i++) { diff --git a/src/lib/sign.ts b/src/lib/sign.ts index ce11676..1bc7c16 100644 --- a/src/lib/sign.ts +++ b/src/lib/sign.ts @@ -5,17 +5,19 @@ import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm' const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants +let privateKey = allocate(KEY_BYTELENGTH) +let publicKey = allocate(KEY_BYTELENGTH) +let 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`) } - const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg) - const privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv) - const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) - const signature = allocate(SIGNATURE_BYTELENGTH) Mutex.lock() try { + const message = normalize('message', 0, MAX_MESSAGE_BYTELENGTH, msg) + privateKey = normalize('private key', KEY_BYTELENGTH, KEY_BYTELENGTH, prv) + publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) setInput('msg', message) setInput('prv', privateKey) setInput('pub', publicKey) -- 2.52.0