// 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.
*
* @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 }
}
})
async work (data: object): Promise<any> {
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
})
\r
// WARNING: Do not send any funds to the test vectors below\r
// Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere\r
-describe('derive child accounts from the same seed', async () => {\r
+describe('derive child accounts from the same seed', async function () {\r
const wallet = await Bip44Wallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.BIP39_SEED)\r
await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
\r
- it('should derive the first account from the given BIP-44 seed', async function () {\r
+ await it('should derive the first account from the given BIP-44 seed', async function () {\r
const accounts = await wallet.accounts()\r
\r
assert.equal(accounts.length, 1)\r
assert.equal(accounts[0].address, NANO_TEST_VECTORS.ADDRESS_0)\r
})\r
\r
- it('should derive low indexed accounts from the given BIP-44 seed', async function () {\r
+ await it('should derive low indexed accounts from the given BIP-44 seed', async function () {\r
const accounts = await wallet.accounts(1, 2)\r
\r
assert.equal(accounts.length, 2)\r
assert.equal(accounts[1].address, NANO_TEST_VECTORS.ADDRESS_2)\r
})\r
\r
- it('should derive high indexed accounts from the given seed', async function () {\r
+ await it('should derive high indexed accounts from the given seed', async function () {\r
const accounts = await wallet.accounts(0x70000000, 0x700000ff)\r
\r
assert.equal(accounts.length, 0x100)\r
}\r
})\r
\r
- it('should derive accounts for a BLAKE2b wallet', async function () {\r
+ await it('should derive accounts for a BLAKE2b wallet', async function () {\r
const bwallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)\r
await bwallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
const lowAccounts = await bwallet.accounts(0, 2)\r
})\r
\r
describe('child key derivation performance', { skip }, async () => {\r
- it('performance test of BIP-44 ckd', async function () {\r
+ await it('performance test of BIP-44 ckd', async function () {\r
const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)\r
await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
\r
assert.equal(accounts.length, 0x8000)\r
})\r
\r
- it('performance test of BLAKE2b ckd', async function () {\r
+ await it('performance test of BLAKE2b ckd', async function () {\r
const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)\r
await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
\r