From 540605a0652f1c158c3ca693265d9560d4f92730 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 30 Nov 2024 12:49:12 -0800 Subject: [PATCH] Use ArrayBuffer to transfer data to and from worker. --- src/lib/workers/ckdBip44.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/workers/ckdBip44.ts b/src/lib/workers/ckdBip44.ts index 0aa91d4..bb7f380 100644 --- a/src/lib/workers/ckdBip44.ts +++ b/src/lib/workers/ckdBip44.ts @@ -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 { + 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. -- 2.34.1