From: Chris Duncan Date: Sun, 6 Jul 2025 04:46:19 +0000 (-0700) Subject: Initialize all workers as static members. X-Git-Tag: v0.10.5~112 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=f8f8ca802f4dd9b8ea106e666ce90e1827c2d6ae;p=libnemo.git Initialize all workers as static members. --- diff --git a/src/lib/wallets/bip44-wallet.ts b/src/lib/wallets/bip44-wallet.ts index 20b6dbd..0b4865a 100644 --- a/src/lib/wallets/bip44-wallet.ts +++ b/src/lib/wallets/bip44-wallet.ts @@ -32,7 +32,7 @@ import { Bip44CkdWorker } from '#workers' */ export class Bip44Wallet extends Wallet { static #isInternal: boolean = false - #poolBip44Ckd: Pool + static #poolBip44Ckd: Pool constructor (id: Entropy, seed: string, mnemonic?: Bip39Mnemonic) { if (!Bip44Wallet.#isInternal) { @@ -40,16 +40,7 @@ export class Bip44Wallet extends Wallet { } Bip44Wallet.#isInternal = false super(id, seed, mnemonic) - this.#poolBip44Ckd = new Pool(Bip44CkdWorker) - } - - /** - * Removes encrypted secrets in storage and releases variable references to - * allow garbage collection. - */ - async destroy (): Promise { - await super.destroy() - this.#poolBip44Ckd.terminate() + Bip44Wallet.#poolBip44Ckd ??= new Pool(Bip44CkdWorker) } /** @@ -220,7 +211,7 @@ export class Bip44Wallet extends Wallet { async ckd (indexes: number[]): Promise { const data: any = [] indexes.forEach(i => data.push({ seed: this.seed, index: i })) - const privateKeys: KeyPair[] = await this.#poolBip44Ckd.assign(data) + const privateKeys: KeyPair[] = await Bip44Wallet.#poolBip44Ckd.assign(data) for (let i = 0; i < privateKeys.length; i++) { if (privateKeys[i].privateKey == null) { throw new Error('Failed to derive private keys') diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index df110db..ed56b54 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -24,12 +24,12 @@ export type KeyPair = { * Blake2bWallet, LedgerWallet. */ export abstract class Wallet { + static #poolNanoNacl: Pool + static #poolSafe: Pool #accounts: AccountList #id: Entropy #locked: boolean = true #mnemonic: Bip39Mnemonic | null - #poolNanoNacl: Pool - #poolSafe: Pool #seed: string | null get id () { return this.#id.hex } get isLocked () { return this.#locked } @@ -56,9 +56,9 @@ export abstract class Wallet { this.#accounts = new AccountList() this.#id = id this.#mnemonic = mnemonic ?? null - this.#poolNanoNacl = new Pool(NanoNaClWorker) - this.#poolSafe = new Pool(SafeWorker) this.#seed = seed ?? null + Wallet.#poolNanoNacl ??= new Pool(NanoNaClWorker) + Wallet.#poolSafe ??= new Pool(SafeWorker) } /** @@ -74,12 +74,10 @@ export abstract class Wallet { } this.#mnemonic = null this.#seed = null - this.#poolNanoNacl.terminate() - await this.#poolSafe.assign({ + await Wallet.#poolSafe.assign({ method: 'destroy', name: this.id }) - this.#poolSafe.terminate() } /** @@ -148,7 +146,7 @@ export abstract class Wallet { privateKey: r.privateKey, index: r.index })) - const keypairs: KeyPair[] = await this.#poolNanoNacl.assign(data) + const keypairs: KeyPair[] = await Wallet.#poolNanoNacl.assign(data) for (const keypair of keypairs) { if (keypair.privateKey == null) throw new RangeError('Account private key missing') if (keypair.publicKey == null) throw new RangeError('Account public key missing') @@ -244,7 +242,7 @@ export abstract class Wallet { if (typeof this.#seed === 'string') { data.seed = this.#seed } - const response = (await this.#poolSafe.assign({ + const response = (await Wallet.#poolSafe.assign({ method: 'put', name: this.id, password, @@ -284,7 +282,7 @@ export abstract class Wallet { throw new Error('Failed to unlock wallet') } try { - const response = (await this.#poolSafe.assign({ + const response = (await Wallet.#poolSafe.assign({ method: 'get', name: this.id, password