From bc239507551022f76b34f34fa84d419a8f9747c4 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 30 Nov 2024 21:57:49 -0800 Subject: [PATCH] Add bytes-to-hex function in nanonacl and return keys as strings instead of bytes. --- src/lib/wallet.ts | 2 +- src/lib/workers/nano-nacl.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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) -- 2.34.1