]> zoso.dev Git - libnemo.git/commitdiff
Worker imports using strings does not work with an async function, so revert that...
authorChris Duncan <chris@zoso.dev>
Wed, 4 Dec 2024 07:12:38 +0000 (23:12 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 4 Dec 2024 07:12:38 +0000 (23:12 -0800)
src/lib/bip44-ckd.ts
src/lib/nano-nacl.ts
src/lib/pool.ts
src/lib/pow.ts

index 3aa91efbfb48970369b0b1c070399164555c8630..b6ed3a55f8d0024f8c5686f5aa17ef852a2118ae 100644 (file)
@@ -6,16 +6,20 @@ type ExtendedKey = {
        chainCode: DataView
 }
 
-const b = async () => {
+const b = () => {
        /**
        * Polyfill for window methods which do not exist when executing Node.js tests.
        */
-       if (typeof Window === 'undefined') {
-               const { isMainThread, parentPort } = await import('node:worker_threads')
-               if (!isMainThread && parentPort) {
-                       var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
-                       var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
-               }
+       if (typeof window === 'undefined' && typeof self === 'undefined') {
+               try {
+                       import('node:worker_threads').then(worker_threads => {
+                               const { isMainThread, parentPort } = worker_threads
+                               if (!isMainThread && parentPort) {
+                                       var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+                                       var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+                               }
+                       })
+               } catch { }
        }
 
        const BIP44_COIN_NANO = 165
@@ -26,17 +30,18 @@ const b = async () => {
        /**
        * Listens for messages from a calling function.
        */
-       if (addEventListener != null) {
+       if (typeof addEventListener === 'function') {
                addEventListener('message', (message: any): void => {
                        const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
-                       process(data).then(results => {
+                       calculate(data).then(results => {
                                const buf = new TextEncoder().encode(JSON.stringify(results)).buffer
+                               //@ts-expect-error
                                postMessage(buf, [buf])
                        })
                })
        }
 
-       async function process (data: any[]): Promise<any[]> {
+       async function calculate (data: any[]): Promise<any[]> {
                return new Promise(async (resolve) => {
                        for (const d of data) {
                                d.privateKey = await nanoCKD(d.seed, d.index)
@@ -65,7 +70,11 @@ const b = async () => {
                const coinKey = await CKDpriv(purposeKey, BIP44_COIN_NANO + HARDENED_OFFSET)
                const accountKey = await CKDpriv(coinKey, index + HARDENED_OFFSET)
                const privateKey = new Uint8Array(accountKey.privateKey.buffer)
-               return privateKey.reduce((key, byte) => key.concat(byte.toString(16).padStart(2, '0')), '')
+               let hex = ''
+               for (let i = 0; i < privateKey.length; i++) {
+                       hex += privateKey[i].toString(16).padStart(2, '0')
+               }
+               return hex
        }
 
        async function slip10 (curve: string, S: string): Promise<ExtendedKey> {
@@ -122,7 +131,7 @@ const b = async () => {
        return { nanoCKD }
 }
 
-export const Bip44Ckd = await b()
+export const Bip44Ckd = b()
 
 const start = b.toString().indexOf('{') + 1
 const end = b.toString().lastIndexOf('return')
index e57e8362e140c74411348eab427f27efd3da1821..d6f9fada4bcf9f78fbbd4751f77b79441c9f6661 100644 (file)
@@ -17,28 +17,33 @@ import { Blake2b } from './blake2b.js'
 // See for details: https://docs.nano.org/integration-guides/the-basics/\r
 // Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
 \r
-const n = async () => {\r
+const n = () => {\r
        /**\r
        * Polyfill for window methods which do not exist when executing Node.js tests.\r
        */\r
-       if (typeof Window === 'undefined') {\r
-               const { isMainThread, parentPort } = await import('node:worker_threads')\r
-               if (!isMainThread && parentPort) {\r
-                       var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)\r
-                       var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)\r
-               }\r
+       if (typeof window === 'undefined' && typeof self === 'undefined') {\r
+               try {\r
+                       import('node:worker_threads').then(worker_threads => {\r
+                               const { isMainThread, parentPort } = worker_threads\r
+                               if (!isMainThread && parentPort) {\r
+                                       var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)\r
+                                       var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)\r
+                               }\r
+                       })\r
+               } catch { }\r
        }\r
 \r
        /**\r
        * Listens for messages from a calling function.\r
        */\r
-       if (addEventListener != null) {\r
+       if (typeof addEventListener === 'function') {\r
                addEventListener('message', (message: any): void => {\r
                        const data = JSON.parse(new TextDecoder().decode(message.data ?? message))\r
                        for (const d of data) {\r
                                d.publicKey = convert(d.privateKey)\r
                        }\r
                        const buf = new TextEncoder().encode(JSON.stringify(data)).buffer\r
+                       //@ts-expect-error\r
                        postMessage(buf, [buf])\r
                })\r
        }\r
@@ -862,7 +867,7 @@ const n = async () => {
        return { sign, open, detached, verify, convert }\r
 }\r
 \r
-export const NanoNaCl = await n()\r
+export const NanoNaCl = n()\r
 \r
 const start = n.toString().indexOf('{') + 1\r
 const end = n.toString().lastIndexOf('return')\r
index 245b6ee805fe8f6c02a0dc28b635171041882bfe..6360beeb2edb467fdf972f931a7e6ccaef7c0b25 100644 (file)
@@ -36,7 +36,7 @@ export class Pool {
        }
 
        constructor (fn: string) {
-               if (typeof Window === 'undefined') {
+               if (typeof window === 'undefined' && typeof self === 'undefined') {
                        this.#url = fn
                } else {
                        this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' }))
index 9065386b5dfa27604d1fa11dc02543f7a148aa09..eb5571151c327ab9c0c4ba5cc47bdaa03e7285e8 100644 (file)
@@ -5,19 +5,23 @@ const p = async () => {
        /**
        * Polyfill for window methods which do not exist when executing Node.js tests.
        */
-       if (typeof Window === 'undefined') {
-               const { isMainThread, parentPort } = await import('node:worker_threads')
-               if (!isMainThread && parentPort) {
-                       var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
-                       var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
-               }
+       if (typeof window === 'undefined' && typeof self === 'undefined') {
+               try {
+                       import('node:worker_threads').then(worker_threads => {
+                               const { isMainThread, parentPort } = worker_threads
+                               if (!isMainThread && parentPort) {
+                                       var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+                                       var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+                               }
+                       })
+               } catch (err) { }
        }
 
        const SEND_THRESHOLD = '0xfffffff8'
        /**
        * Listens for messages from a calling function.
        */
-       if (addEventListener != null) {
+       if (typeof addEventListener === 'function') {
                addEventListener('message', (message: any): void => {
                        const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
                        for (const d of data) {
@@ -28,6 +32,7 @@ const p = async () => {
                                        find(d.hash, d.threshold ?? SEND_THRESHOLD).then(nonce => {
                                                d.work = nonce
                                                const buf = new TextEncoder().encode(JSON.stringify(data)).buffer
+                                               //@ts-expect-error
                                                postMessage(buf, [buf])
                                        })
                                }