From: Chris Duncan Date: Sun, 17 Aug 2025 23:00:11 +0000 (-0700) Subject: Offload type checking to called function. X-Git-Tag: v0.10.5~41^2~82 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=fb06f39c3431dcdeb51d68fc9948e11d00cce0bc;p=libnemo.git Offload type checking to called function. --- diff --git a/src/lib/crypto/nano-nacl.ts b/src/lib/crypto/nano-nacl.ts index cd6ecc7..35e57ab 100644 --- a/src/lib/crypto/nano-nacl.ts +++ b/src/lib/crypto/nano-nacl.ts @@ -574,31 +574,18 @@ export class NanoNaCl { static verify (signedMessage: unknown, signature: unknown, publicKey: unknown): boolean { try { if (!(signedMessage instanceof Uint8Array)) { - throw new TypeError('Message must be Uint8Array') + throw new TypeError('Signed message must be Uint8Array') } if (!(signature instanceof Uint8Array)) { throw new TypeError('Signature must be Uint8Array') } - if (!(publicKey instanceof Uint8Array)) { - throw new TypeError('Public key must be Uint8Array') - } if (signature.byteLength !== this.crypto_sign_BYTES) { throw new Error(`Signature must be ${this.crypto_sign_BYTES} bytes`) } - if (publicKey.byteLength !== this.crypto_sign_PUBLICKEYBYTES) { - throw new Error(`Public key must be ${this.crypto_sign_PUBLICKEYBYTES} bytes`) - } - const msg = new Uint8Array(signedMessage) - const sig = new Uint8Array(signature) - const pub = new Uint8Array(publicKey) - signedMessage = undefined - signature = undefined - publicKey = undefined - const sm = new Uint8Array(this.crypto_sign_BYTES + msg.length) - const m = new Uint8Array(this.crypto_sign_BYTES + msg.length) - sm.set(sig, 0) - sm.set(msg, this.crypto_sign_BYTES) - return (this.crypto_sign_open(m, sm, sm.length, pub) >= 0) + const sm = new Uint8Array(this.crypto_sign_BYTES + signedMessage.length) + sm.set(signature, 0) + sm.set(signedMessage, this.crypto_sign_BYTES) + return (this.open(sm, publicKey as Uint8Array).byteLength >= 0) } catch (err) { throw new Error('Failed to verify signature on message with the given public key', { cause: err }) }