]> git.codecow.com Git - libnemo.git/commitdiff
Fix signature verification.
authorChris Duncan <chris@zoso.dev>
Tue, 15 Jul 2025 21:39:00 +0000 (14:39 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 15 Jul 2025 21:39:00 +0000 (14:39 -0700)
src/lib/block.ts
src/lib/tools.ts
src/lib/workers/nano-nacl.ts
test/test.main.mjs

index 6adb3f08673cf45a000c8be6f8eb10399de4df2c..208958fa42334e53ec10a8fa978e121b8e006ad7 100644 (file)
@@ -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 })
                }
index de9a98b00707900d874b0e0b5695760e8d639ff3..2669e5b801660d87ae644a4b0934aa59fc35dfb5 100644 (file)
@@ -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<ArrayBuffer>, ...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<ArrayBuffer>).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<ArrayBuffer>, 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<ArrayBuffer>).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
index 8edf1f933cd5a71af907d0c29393a82dbafb6d72..2ab492047828ae8057d3842f0e2f77f841ff84e8 100644 (file)
@@ -27,8 +27,11 @@ export class NanoNaCl extends WorkerInterface {
        }\r
 \r
        static async work (headers: Headers, data: Data): Promise<boolean | string> {\r
-               const { method, msg, signature, publicKey } = headers\r
+               const { method } = headers\r
+               const msg = new Uint8Array(data.msg)\r
                const privateKey = new Uint8Array(data.privateKey)\r
+               const publicKey = new Uint8Array(data.publicKey)\r
+               const signature = new Uint8Array(data.signature)\r
                switch (method) {\r
                        case 'convert': {\r
                                return bytes.toHex(await this.convert(privateKey))\r
index c179f9c266d1a479270918662aedc2679163defb..bca4fc5fd52bffd19b9bc0ca9994839a0936ba32 100644 (file)
@@ -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'