From: Chris Duncan Date: Sat, 21 Dec 2024 03:16:25 +0000 (-0800) Subject: Fix listener not being inherited properly by classes extending WorkerInterface. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8870fd3b1229a55c39b733f97f112a1745d16d48;p=libnemo.git Fix listener not being inherited properly by classes extending WorkerInterface. --- diff --git a/src/lib/pool.ts b/src/lib/pool.ts index 030c75a..d763dff 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -77,7 +77,6 @@ export class Pool { this.#threads.push(thread) Pool.#cores = Math.max(1, Pool.#cores - this.#threads.length) } - console.log(this.#url) } #assign (thread: Thread, job: Job): void { @@ -125,16 +124,29 @@ export class Pool { } } +/** +* Provides basic worker event messaging to extending classes. +* +* In order to be properly bundled in a format that can be used to create an +* inline Web Worker, the extending classes must export WorkerInterface and +* themselves as a string: +*``` +* export default ` +* const WorkerInterface = ${WorkerInterface} +* const Pow = ${Pow} +* ` +* ``` +* They must also initialize the event listener by calling their inherited +* `listen()` function. Finally, they must override the implementation of the +* `work()` function. See the documentation of those functions for details. +*/ export class WorkerInterface { /** * Processes data through a worker. * - * Worker classes should override this template by implementing the same - * function signature and providing their own processing method in the - * try-catch block. In order to be properly bundled in a format that can be The classes also need to export WorkerInterface and - * themselves as a string. For example: - * - * `export default `const WorkerInterface + * Extending classes must override this template by implementing the same + * function signature and providing their own processing call in the try-catch + * block. * * @param {any[]} data - Array of data to process * @returns Promise for that data after being processed @@ -165,9 +177,16 @@ export class WorkerInterface { } /** - * Listens for messages from a calling function. + * Listens for messages from the main thread. + * + * Extending classes must call this in a static initialization block: + * ``` + * static { + * Pow.listen() + * } + * ``` */ - static { + static listen (): void { addEventListener('message', (message: any): void => { const { name, buffer } = message.data if (name === 'STOP') { diff --git a/src/lib/workers/bip44-ckd.ts b/src/lib/workers/bip44-ckd.ts index 09c19a3..7764399 100644 --- a/src/lib/workers/bip44-ckd.ts +++ b/src/lib/workers/bip44-ckd.ts @@ -13,6 +13,10 @@ export class Bip44Ckd extends WorkerInterface { static HARDENED_OFFSET = 0x80000000 static SLIP10_ED25519 = 'ed25519 seed' + static { + Bip44Ckd.listen() + } + static async work (data: any[]): Promise { for (const d of data) { if (d.coin != null && d.coin !== this.BIP44_PURPOSE) { diff --git a/src/lib/workers/nano-nacl.ts b/src/lib/workers/nano-nacl.ts index 0e66f10..3bbaf69 100644 --- a/src/lib/workers/nano-nacl.ts +++ b/src/lib/workers/nano-nacl.ts @@ -19,6 +19,23 @@ import { WorkerInterface } from '../pool.js' // Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js export class NanoNaCl extends WorkerInterface { + static { + NanoNaCl.listen() + } + + static async work (data: any[]): Promise { + return new Promise(async (resolve, reject): Promise => { + for (let d of data) { + try { + d.publicKey = await this.convert(d.privateKey) + } catch (err) { + reject(err) + } + } + resolve(data) + }) + } + static gf = function (init?: number[]): Float64Array { const r = new Float64Array(16) if (init) for (let i = 0; i < init.length; i++) r[i] = init[i] @@ -839,19 +856,6 @@ export class NanoNaCl extends WorkerInterface { return this.hexify(pk).toUpperCase() } - - static async work (data: any[]): Promise { - return new Promise(async (resolve, reject): Promise => { - for (let d of data) { - try { - d.publicKey = await this.convert(d.privateKey) - } catch (err) { - reject(err) - } - } - resolve(data) - }) - } } export default ` diff --git a/src/lib/workers/powgl.ts b/src/lib/workers/powgl.ts index ca0bf9e..973213d 100644 --- a/src/lib/workers/powgl.ts +++ b/src/lib/workers/powgl.ts @@ -5,6 +5,9 @@ import { WorkerInterface } from '../pool.js' export class Pow extends WorkerInterface { + static { + Pow.listen() + } /** * Calculates proof-of-work as described by the Nano cryptocurrency protocol. * diff --git a/test/create-wallet.test.mjs b/test/create-wallet.test.mjs index 8fc6765..f0feaab 100644 --- a/test/create-wallet.test.mjs +++ b/test/create-wallet.test.mjs @@ -7,7 +7,7 @@ import { assert, skip, suite, test } from '#GLOBALS.mjs' import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '#dist/main.js' -await skip('Create wallets', async () => { +await suite('Create wallets', async () => { await test('BIP-44 wallet with random entropy', async () => { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index adb533f..562635a 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -19,7 +19,7 @@ await suite('Account derivation', async () => { assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) }) - await skip('should derive low indexed accounts from the given BIP-44 seed', async () => { + await test('should derive low indexed accounts from the given BIP-44 seed', async () => { const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(1, 2) @@ -33,7 +33,7 @@ await suite('Account derivation', async () => { assert.equals(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2) }) - await skip('should derive high indexed accounts from the given seed', async () => { + await test('should derive high indexed accounts from the given seed', async () => { const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const accounts = await wallet.accounts(0x70000000, 0x700000ff) @@ -50,7 +50,7 @@ await suite('Account derivation', async () => { } }) - await skip('should derive accounts for a BLAKE2b wallet', async () => { + await test('should derive accounts for a BLAKE2b wallet', async () => { const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) const lowAccounts = await wallet.accounts(0, 2)