From: Chris Duncan Date: Sat, 9 Nov 2024 08:53:33 +0000 (-0800) Subject: w X-Git-Tag: v0.0.20~36 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=27d36ad63045fa14b0ce85929273d44a8e7b552a;p=libnemo.git w --- diff --git a/src/lib/ckd.ts b/src/lib/ckd.ts index f7e2d05..67e1a28 100644 --- a/src/lib/ckd.ts +++ b/src/lib/ckd.ts @@ -13,7 +13,7 @@ import type { Ledger } from './ledger.js' * @param {number} index - Index of the account * @returns {Promise} */ -onmessage = async (event) => { +globalThis.onmessage = async (event) => { let result = null const { type, seed, index } = event.data switch (type) { diff --git a/src/lib/pool.ts b/src/lib/pool.ts index a07087c..e5afa1b 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -2,6 +2,12 @@ // SPDX-License-Identifier: GPL-3.0-or-later const Worker = globalThis.Worker ?? (await import('node:worker_threads')).Worker + +type Thread = { + worker: Worker, + tasks: any[], + isAvailable: boolean +} /** * Assigns a Web Worker to process data. Creates a new one if none are available. * @@ -11,16 +17,16 @@ const Worker = globalThis.Worker ?? (await import('node:worker_threads')).Worker * @returns {boolean} True if the data was signed by the public key's matching private key */ export class Pool { - #threads + #threads: Thread[] = new Array() #url constructor (url: string | URL) { this.#url = new URL(url, import.meta.url) this.#threads = [...Array(navigator.hardwareConcurrency)] - this.#threads.forEach(slot => { - slot = { + this.#threads = this.#threads.map(() => { + return { worker: new Worker(this.#url), - tasks: [], + tasks: new Array(), get isAvailable () { return this.tasks.length === 0 } } }) @@ -29,7 +35,7 @@ export class Pool { async work (data: object): Promise { return new Promise((resolve) => { const thread = this.#threads.reduce((curr, next) => { - next.tasks.length < curr.tasks.length + return (next.tasks.length < curr.tasks.length) ? next : curr }) diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index 1b0864e..0765017 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -12,11 +12,11 @@ const skip = true // WARNING: Do not send any funds to the test vectors below // Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere -describe('derive child accounts from the same seed', async () => { +describe('derive child accounts from the same seed', async function () { const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) - it('should derive the first account from the given BIP-44 seed', async function () { + await it('should derive the first account from the given BIP-44 seed', async function () { const accounts = await wallet.accounts() assert.equal(accounts.length, 1) @@ -25,7 +25,7 @@ describe('derive child accounts from the same seed', async () => { assert.equal(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0) }) - it('should derive low indexed accounts from the given BIP-44 seed', async function () { + await it('should derive low indexed accounts from the given BIP-44 seed', async function () { const accounts = await wallet.accounts(1, 2) assert.equal(accounts.length, 2) @@ -37,7 +37,7 @@ describe('derive child accounts from the same seed', async () => { assert.equal(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2) }) - it('should derive high indexed accounts from the given seed', async function () { + await it('should derive high indexed accounts from the given seed', async function () { const accounts = await wallet.accounts(0x70000000, 0x700000ff) assert.equal(accounts.length, 0x100) @@ -50,7 +50,7 @@ describe('derive child accounts from the same seed', async () => { } }) - it('should derive accounts for a BLAKE2b wallet', async function () { + await it('should derive accounts for a BLAKE2b wallet', async function () { const bwallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) await bwallet.unlock(NANO_TEST_VECTORS.PASSWORD) const lowAccounts = await bwallet.accounts(0, 2) @@ -90,7 +90,7 @@ describe('Ledger device accounts', { skip: true }, async () => { }) describe('child key derivation performance', { skip }, async () => { - it('performance test of BIP-44 ckd', async function () { + await it('performance test of BIP-44 ckd', async function () { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) @@ -99,7 +99,7 @@ describe('child key derivation performance', { skip }, async () => { assert.equal(accounts.length, 0x8000) }) - it('performance test of BLAKE2b ckd', async function () { + await it('performance test of BLAKE2b ckd', async function () { const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)