]> git.codecow.com Git - libnemo.git/commitdiff
Initialize all workers as static members.
authorChris Duncan <chris@zoso.dev>
Sun, 6 Jul 2025 04:46:19 +0000 (21:46 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 6 Jul 2025 04:46:19 +0000 (21:46 -0700)
src/lib/wallets/bip44-wallet.ts
src/lib/wallets/wallet.ts

index 20b6dbdef2b68c1028ac370a6bb1b56186616999..0b4865a181bd7528d3d0cba2a012c37e28a285fd 100644 (file)
@@ -32,7 +32,7 @@ import { Bip44CkdWorker } from '#workers'
 */\r
 export class Bip44Wallet extends Wallet {\r
        static #isInternal: boolean = false\r
-       #poolBip44Ckd: Pool\r
+       static #poolBip44Ckd: Pool\r
 \r
        constructor (id: Entropy, seed: string, mnemonic?: Bip39Mnemonic) {\r
                if (!Bip44Wallet.#isInternal) {\r
@@ -40,16 +40,7 @@ export class Bip44Wallet extends Wallet {
                }\r
                Bip44Wallet.#isInternal = false\r
                super(id, seed, mnemonic)\r
-               this.#poolBip44Ckd = new Pool(Bip44CkdWorker)\r
-       }\r
-\r
-       /**\r
-       * Removes encrypted secrets in storage and releases variable references to\r
-       * allow garbage collection.\r
-       */\r
-       async destroy (): Promise<void> {\r
-               await super.destroy()\r
-               this.#poolBip44Ckd.terminate()\r
+               Bip44Wallet.#poolBip44Ckd ??= new Pool(Bip44CkdWorker)\r
        }\r
 \r
        /**\r
@@ -220,7 +211,7 @@ export class Bip44Wallet extends Wallet {
        async ckd (indexes: number[]): Promise<KeyPair[]> {\r
                const data: any = []\r
                indexes.forEach(i => data.push({ seed: this.seed, index: i }))\r
-               const privateKeys: KeyPair[] = await this.#poolBip44Ckd.assign(data)\r
+               const privateKeys: KeyPair[] = await Bip44Wallet.#poolBip44Ckd.assign(data)\r
                for (let i = 0; i < privateKeys.length; i++) {\r
                        if (privateKeys[i].privateKey == null) {\r
                                throw new Error('Failed to derive private keys')\r
index df110dbc04114f5ed4360e296ddef9973f07496d..ed56b54151f7e84565427589fb60384ae3a746bc 100644 (file)
@@ -24,12 +24,12 @@ export type KeyPair = {
 * Blake2bWallet, LedgerWallet.\r
 */\r
 export abstract class Wallet {\r
+       static #poolNanoNacl: Pool\r
+       static #poolSafe: Pool\r
        #accounts: AccountList\r
        #id: Entropy\r
        #locked: boolean = true\r
        #mnemonic: Bip39Mnemonic | null\r
-       #poolNanoNacl: Pool\r
-       #poolSafe: Pool\r
        #seed: string | null\r
        get id () { return this.#id.hex }\r
        get isLocked () { return this.#locked }\r
@@ -56,9 +56,9 @@ export abstract class Wallet {
                this.#accounts = new AccountList()\r
                this.#id = id\r
                this.#mnemonic = mnemonic ?? null\r
-               this.#poolNanoNacl = new Pool(NanoNaClWorker)\r
-               this.#poolSafe = new Pool(SafeWorker)\r
                this.#seed = seed ?? null\r
+               Wallet.#poolNanoNacl ??= new Pool(NanoNaClWorker)\r
+               Wallet.#poolSafe ??= new Pool(SafeWorker)\r
        }\r
 \r
        /**\r
@@ -74,12 +74,10 @@ export abstract class Wallet {
                }\r
                this.#mnemonic = null\r
                this.#seed = null\r
-               this.#poolNanoNacl.terminate()\r
-               await this.#poolSafe.assign({\r
+               await Wallet.#poolSafe.assign({\r
                        method: 'destroy',\r
                        name: this.id\r
                })\r
-               this.#poolSafe.terminate()\r
        }\r
 \r
        /**\r
@@ -148,7 +146,7 @@ export abstract class Wallet {
                                privateKey: r.privateKey,\r
                                index: r.index\r
                        }))\r
-                       const keypairs: KeyPair[] = await this.#poolNanoNacl.assign(data)\r
+                       const keypairs: KeyPair[] = await Wallet.#poolNanoNacl.assign(data)\r
                        for (const keypair of keypairs) {\r
                                if (keypair.privateKey == null) throw new RangeError('Account private key missing')\r
                                if (keypair.publicKey == null) throw new RangeError('Account public key missing')\r
@@ -244,7 +242,7 @@ export abstract class Wallet {
                        if (typeof this.#seed === 'string') {\r
                                data.seed = this.#seed\r
                        }\r
-                       const response = (await this.#poolSafe.assign({\r
+                       const response = (await Wallet.#poolSafe.assign({\r
                                method: 'put',\r
                                name: this.id,\r
                                password,\r
@@ -284,7 +282,7 @@ export abstract class Wallet {
                        throw new Error('Failed to unlock wallet')\r
                }\r
                try {\r
-                       const response = (await this.#poolSafe.assign({\r
+                       const response = (await Wallet.#poolSafe.assign({\r
                                method: 'get',\r
                                name: this.id,\r
                                password\r