From 213780fef9e97dd6d1aae030b90710666f0a9301 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 15 Jul 2025 14:39:00 -0700 Subject: [PATCH] Fix signature verification. --- src/lib/block.ts | 22 ++++++++++++---------- src/lib/tools.ts | 25 ++++++++++++++----------- src/lib/workers/nano-nacl.ts | 5 ++++- test/test.main.mjs | 2 +- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 6adb3f0..208958f 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -146,14 +146,14 @@ abstract class Block { const account = await Account.fromPrivateKey(key) try { const headers = { - method: 'detached', - msg: this.hash + method: 'detached' } const data = { - privateKey: hex.toBytes(account.privateKey).buffer + privateKey: hex.toBytes(account.privateKey).buffer, + msg: hex.toBytes(this.hash).buffer } const result = await NanoNaClWorker.add(headers, data) - this.signature = result[0].signature + this.signature = result[0] } catch (err) { throw new Error(`Failed to sign block`, { cause: err }) } @@ -202,13 +202,15 @@ abstract class Block { } try { const headers = { - method: 'verify', - msg: this.hash, - signature: this.signature ?? '', - publicKey: key + method: 'verify' } - const result = await NanoNaClWorker.add(headers) - return result.isVerified[0] + const data = { + msg: hex.toBytes(this.hash).buffer, + signature: hex.toBytes(this.signature ?? '').buffer, + publicKey: hex.toBytes(key).buffer + } + const result = await NanoNaClWorker.add(headers, data) + return result[0] } catch (err) { throw new Error(`Failed to derive public key from private key`, { cause: err }) } diff --git a/src/lib/tools.ts b/src/lib/tools.ts index de9a98b..2669e5b 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -16,7 +16,7 @@ type SweepResult = { message: string } -function hash (data: string | string[], encoding?: 'hex'): string { +function hash (data: string | string[], encoding?: 'hex', format?: 'hex'): string | Uint8Array { if (!Array.isArray(data)) data = [data] const hash = new Blake2b(32) if (encoding === 'hex') { @@ -25,7 +25,9 @@ function hash (data: string | string[], encoding?: 'hex'): string { const enc = new TextEncoder() data.forEach(str => hash.update(enc.encode(str))) } - return hash.digest('hex').toUpperCase() + return format === 'hex' + ? hash.digest('hex').toUpperCase() + : hash.digest() } /** @@ -90,14 +92,14 @@ export async function sign (key: string | Uint8Array, ...input: str let signature: string try { const headers = { - method: 'detached', - msg: hash(input) + method: 'detached' } const data = { - privateKey: key.buffer + privateKey: key.buffer, + msg: (hash(input) as Uint8Array).buffer } const result = await NanoNaClWorker.add(headers, data) - signature = result.publicKey[0] + signature = result[0] } catch (err) { throw new Error(`Failed to sign message with private key`, { cause: err }) } finally { @@ -181,14 +183,15 @@ export async function verify (key: string | Uint8Array, signature: let isVerified: boolean try { const headers = { - method: 'verify', - msg: hash(input), - signature + method: 'verify' } const data = { - privateKey: key.buffer + msg: (hash(input) as Uint8Array).buffer, + signature: hex.toBytes(signature).buffer, + publicKey: new Uint8Array(key).buffer } - isVerified = await NanoNaClWorker.add(headers, data) + const result = await NanoNaClWorker.add(headers, data) + isVerified = result[0] } catch (err) { console.log(err) isVerified = false diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index 8edf1f9..2ab4920 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -27,8 +27,11 @@ export class NanoNaCl extends WorkerInterface { } static async work (headers: Headers, data: Data): Promise { - const { method, msg, signature, publicKey } = headers + const { method } = headers + const msg = new Uint8Array(data.msg) const privateKey = new Uint8Array(data.privateKey) + const publicKey = new Uint8Array(data.publicKey) + const signature = new Uint8Array(data.signature) switch (method) { case 'convert': { return bytes.toHex(await this.convert(privateKey)) diff --git a/test/test.main.mjs b/test/test.main.mjs index c179f9c..bca4fc5 100644 --- a/test/test.main.mjs +++ b/test/test.main.mjs @@ -5,7 +5,7 @@ // import './test.calculate-pow.mjs' // import './test.create-wallet.mjs' // import './test.derive-accounts.mjs' -import './test.import-wallet.mjs' +// import './test.import-wallet.mjs' import './test.ledger.mjs' // import './test.lock-unlock.mjs' // import './test.manage-rolodex.mjs' -- 2.47.3