From: Chris Duncan Date: Sun, 1 Dec 2024 05:57:49 +0000 (-0800) Subject: Add bytes-to-hex function in nanonacl and return keys as strings instead of bytes. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=bc239507551022f76b34f34fa84d419a8f9747c4;p=libnemo.git Add bytes-to-hex function in nanonacl and return keys as strings instead of bytes. --- diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 36997e0..398c477 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -108,7 +108,7 @@ abstract class Wallet { if (keypair.privateKey == null) throw new RangeError('Account private key missing') if (keypair.index == null) throw new RangeError('Account keys derived but index missing') const { publicKey, privateKey, index } = keypair - this.#accounts[keypair.index] = Account.fromKeypair(bytes.toHex(publicKey as unknown as Uint8Array), bytes.toHex(privateKey as unknown as Uint8Array), index) + this.#accounts[keypair.index] = Account.fromKeypair(publicKey, privateKey, index) } console.log(`accounts: ${-now + (now = performance.now())} ms`) } diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index 744a8cd..3cea4a0 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -22,7 +22,7 @@ const NanoNaCl = () => { addEventListener('message', (message) => { const data = JSON.parse(new TextDecoder().decode(message.data ?? message)) for (const d of data) { - d.publicKey = keyPair(d.privateKey).publicKey + d.publicKey = hexify(keyPair(d.privateKey).publicKey) } const buf = new TextEncoder().encode(JSON.stringify(data)).buffer //@ts-expect-error @@ -777,6 +777,16 @@ const NanoNaCl = () => { return Uint8Array.from(arr) } + function hexify (buf: Uint8Array) { + return buf.reduce((curr, next) => { + if (typeof next !== 'number') + throw new TypeError(`expected number to convert to hex; received ${typeof next}`) + if (next < 0 || next > 255) + throw new RangeError(`expected byte value 0-255; received ${next}`) + return curr + next.toString(16).padStart(2, '0') + }, '') + } + const sign = function (msg: Uint8Array, secretKey: Uint8Array) { checkArrayTypes(msg, secretKey) if (secretKey.length !== crypto_sign_SECRETKEYBYTES)