]> zoso.dev Git - libnemo.git/commitdiff
Use ArrayBuffer to transfer data to and from worker.
authorChris Duncan <chris@zoso.dev>
Sat, 30 Nov 2024 20:49:12 +0000 (12:49 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 30 Nov 2024 20:49:12 +0000 (12:49 -0800)
src/lib/workers/ckdBip44.ts

index 0aa91d489eda4c8359db25c8286b6c55e7924b5d..bb7f380b402115c6b425a4d3ce034eaa4b47e0f8 100644 (file)
@@ -16,10 +16,23 @@ const Bip44Ckd = () => {
        * Listens for messages from a calling function.
        */
        addEventListener('message', (message) => {
-               const { seed, index } = message.data ?? message
-               nanoCKD(seed, index).then(privateKey => postMessage({ privateKey, index }))
+               const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
+               process(data).then(results => {
+                       const buf = new TextEncoder().encode(JSON.stringify(data)).buffer
+                       //@ts-expect-error
+                       postMessage(buf, [buf])
+               })
        })
 
+       async function process (data: any[]): Promise<any[]> {
+               return new Promise(async (resolve) => {
+                       for (const d of data) {
+                               d.privateKey = await nanoCKD(d.seed, d.index)
+                       }
+                       resolve(data)
+               })
+       }
+
        /**
        * Derives a private child key following the BIP-32 and BIP-44 derivation path
        * registered to the Nano block lattice. Only hardened child keys are defined.