]> zoso.dev Git - libnemo.git/commitdiff
Fix listener not being inherited properly by classes extending WorkerInterface.
authorChris Duncan <chris@zoso.dev>
Sat, 21 Dec 2024 03:16:25 +0000 (19:16 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 21 Dec 2024 03:16:25 +0000 (19:16 -0800)
src/lib/pool.ts
src/lib/workers/bip44-ckd.ts
src/lib/workers/nano-nacl.ts
src/lib/workers/powgl.ts
test/create-wallet.test.mjs
test/derive-accounts.test.mjs

index 030c75a05bdaa04c0e7fbdc1c26795762bb732cf..d763dff0718dd0e7b73b93ebb079d0827506289e 100644 (file)
@@ -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') {
index 09c19a3c9410895b13d316119b338994dffc166a..7764399f3c641884fec676bd2a28081b82f1ad72 100644 (file)
@@ -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<any[]> {
                for (const d of data) {
                        if (d.coin != null && d.coin !== this.BIP44_PURPOSE) {
index 0e66f104a7f12df1d8b7e454ad12ce75b8fe3054..3bbaf69a6e022e9b651151d79903a39afe96f026 100644 (file)
@@ -19,6 +19,23 @@ import { WorkerInterface } from '../pool.js'
 // Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
 \r
 export class NanoNaCl extends WorkerInterface {\r
+       static {\r
+               NanoNaCl.listen()\r
+       }\r
+\r
+       static async work (data: any[]): Promise<any[]> {\r
+               return new Promise(async (resolve, reject): Promise<void> => {\r
+                       for (let d of data) {\r
+                               try {\r
+                                       d.publicKey = await this.convert(d.privateKey)\r
+                               } catch (err) {\r
+                                       reject(err)\r
+                               }\r
+                       }\r
+                       resolve(data)\r
+               })\r
+       }\r
+\r
        static gf = function (init?: number[]): Float64Array {\r
                const r = new Float64Array(16)\r
                if (init) for (let i = 0; i < init.length; i++) r[i] = init[i]\r
@@ -839,19 +856,6 @@ export class NanoNaCl extends WorkerInterface {
 \r
                return this.hexify(pk).toUpperCase()\r
        }\r
-\r
-       static async work (data: any[]): Promise<any[]> {\r
-               return new Promise(async (resolve, reject): Promise<void> => {\r
-                       for (let d of data) {\r
-                               try {\r
-                                       d.publicKey = await this.convert(d.privateKey)\r
-                               } catch (err) {\r
-                                       reject(err)\r
-                               }\r
-                       }\r
-                       resolve(data)\r
-               })\r
-       }\r
 }\r
 \r
 export default `\r
index ca0bf9efd6b0a22bf6703e8a81b02fffeaf30fce..973213d99a74989a4bb80594ac080aa9d6b6a939 100644 (file)
@@ -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.
        *
index 8fc6765905091cfbee129a41886a52064955af23..f0feaabd86e8f42f4205dd0cbf8e851c53c1e8f4 100644 (file)
@@ -7,7 +7,7 @@ import { assert, skip, suite, test } from '#GLOBALS.mjs'
 import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js'\r
 import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '#dist/main.js'\r
 \r
-await skip('Create wallets', async () => {\r
+await suite('Create wallets', async () => {\r
        await test('BIP-44 wallet with random entropy', async () => {\r
                const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
index adb533f40388a5d27f4bb71db91137d9d98ec0b2..562635ab1a9b9dab5c95b975f172ca2a1ae112c0 100644 (file)
@@ -19,7 +19,7 @@ await suite('Account derivation', async () => {
                assert.equals(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0)\r
        })\r
 \r
-       await skip('should derive low indexed accounts from the given BIP-44 seed', async () => {\r
+       await test('should derive low indexed accounts from the given BIP-44 seed', async () => {\r
                const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                const accounts = await wallet.accounts(1, 2)\r
@@ -33,7 +33,7 @@ await suite('Account derivation', async () => {
                assert.equals(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2)\r
        })\r
 \r
-       await skip('should derive high indexed accounts from the given seed', async () => {\r
+       await test('should derive high indexed accounts from the given seed', async () => {\r
                const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                const accounts = await wallet.accounts(0x70000000, 0x700000ff)\r
@@ -50,7 +50,7 @@ await suite('Account derivation', async () => {
                }\r
        })\r
 \r
-       await skip('should derive accounts for a BLAKE2b wallet', async () => {\r
+       await test('should derive accounts for a BLAKE2b wallet', async () => {\r
                const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
                const lowAccounts = await wallet.accounts(0, 2)\r