]> zoso.dev Git - libnemo.git/commitdiff
Add bytes-to-hex function in nanonacl and return keys as strings instead of bytes.
authorChris Duncan <chris@zoso.dev>
Sun, 1 Dec 2024 05:57:49 +0000 (21:57 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 1 Dec 2024 05:57:49 +0000 (21:57 -0800)
src/lib/wallet.ts
src/lib/workers/nano-nacl.ts

index 36997e07c48973a14eca4ca30c9c5b84f4cc3533..398c47717dd91ce542c1158003bf0505a5e27303 100644 (file)
@@ -108,7 +108,7 @@ abstract class Wallet {
                                if (keypair.privateKey == null) throw new RangeError('Account private key missing')\r
                                if (keypair.index == null) throw new RangeError('Account keys derived but index missing')\r
                                const { publicKey, privateKey, index } = keypair\r
-                               this.#accounts[keypair.index] = Account.fromKeypair(bytes.toHex(publicKey as unknown as Uint8Array), bytes.toHex(privateKey as unknown as Uint8Array), index)\r
+                               this.#accounts[keypair.index] = Account.fromKeypair(publicKey, privateKey, index)\r
                        }\r
                        console.log(`accounts: ${-now + (now = performance.now())} ms`)\r
                }\r
index 744a8cda3d910e1553e906b668e86649971fe372..3cea4a085759b00114e8f3fe0477ebddb894ebe5 100644 (file)
@@ -22,7 +22,7 @@ const NanoNaCl = () => {
        addEventListener('message', (message) => {\r
                const data = JSON.parse(new TextDecoder().decode(message.data ?? message))\r
                for (const d of data) {\r
-                       d.publicKey = keyPair(d.privateKey).publicKey\r
+                       d.publicKey = hexify(keyPair(d.privateKey).publicKey)\r
                }\r
                const buf = new TextEncoder().encode(JSON.stringify(data)).buffer\r
                //@ts-expect-error\r
@@ -777,6 +777,16 @@ const NanoNaCl = () => {
                return Uint8Array.from(arr)\r
        }\r
 \r
+       function hexify (buf: Uint8Array) {\r
+               return buf.reduce((curr, next) => {\r
+                       if (typeof next !== 'number')\r
+                               throw new TypeError(`expected number to convert to hex; received ${typeof next}`)\r
+                       if (next < 0 || next > 255)\r
+                               throw new RangeError(`expected byte value 0-255; received ${next}`)\r
+                       return curr + next.toString(16).padStart(2, '0')\r
+               }, '')\r
+       }\r
+\r
        const sign = function (msg: Uint8Array, secretKey: Uint8Array) {\r
                checkArrayTypes(msg, secretKey)\r
                if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r