From: Chris Duncan Date: Wed, 4 Dec 2024 20:24:01 +0000 (-0800) Subject: Fix public key aspect of signing function, specific to Nano. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=55b831b5fd1961f7b6d00e6a14814e59e7ac2a9d;p=libnemo.git Fix public key aspect of signing function, specific to Nano. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index ccb7cfd..58ee6fd 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -143,7 +143,7 @@ abstract class Block { try { const signature = NanoNaCl.detached( hex.toBytes(await this.hash()), - hex.toBytes(`${account.privateKey}${account.publicKey}`) + hex.toBytes(`${account.privateKey}`) ) this.signature = signature } catch (err) { diff --git a/src/lib/nano-nacl.ts b/src/lib/nano-nacl.ts index c395974..dffd50e 100644 --- a/src/lib/nano-nacl.ts +++ b/src/lib/nano-nacl.ts @@ -659,7 +659,7 @@ const n = () => { } // Note: difference from C - smlen returned, not passed as argument. - function crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array): number { + function crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array, pk: Uint8Array): number { const d = new Uint8Array(64) const h = new Uint8Array(64) const r = new Uint8Array(64) @@ -680,7 +680,7 @@ const n = () => { scalarbase(p, r) pack(sm, p) - for (let i = 32; i < 64; i++) sm[i] = sk[i] + for (let i = 0; i < 32; i++) sm[i + 32] = pk[i] crypto_hash(h, sm, n + 64) reduce(h) @@ -770,7 +770,7 @@ const n = () => { const crypto_sign_BYTES = 64 const crypto_sign_PUBLICKEYBYTES = 32 - const crypto_sign_SECRETKEYBYTES = 64 + const crypto_sign_SECRETKEYBYTES = 32 const crypto_sign_SEEDBYTES = 32 /* High-level API */ @@ -805,7 +805,8 @@ const n = () => { if (secretKey.length !== crypto_sign_SECRETKEYBYTES) throw new Error('bad secret key size') var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length) - crypto_sign(signedMsg, msg, msg.length, secretKey) + const publicKey = parseHex(convert(secretKey)) + crypto_sign(signedMsg, msg, msg.length, secretKey, publicKey) return signedMsg } diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 5db2619..0fa8a7a 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -82,9 +82,10 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu export async function sign (key: string, ...input: string[]): Promise { const account = await Account.fromPrivateKey(key) const data = hash(input) - return NanoNaCl.detached( + const signature = NanoNaCl.detached( hex.toBytes(data), - hex.toBytes(`${account.privateKey}${account.publicKey}`)) + hex.toBytes(`${account.privateKey}`)) + return signature } /**