From: Chris Duncan Date: Fri, 29 Aug 2025 19:44:25 +0000 (-0700) Subject: Rename vault timer and move blob construction to main thread class. X-Git-Tag: v0.10.5~39^2~2 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=b250728b85ae7a38f56690574f1327a44a928b92;p=libnemo.git Rename vault timer and move blob construction to main thread class. --- diff --git a/src/lib/vault/index.ts b/src/lib/vault/index.ts index c4a1c06..9868052 100644 --- a/src/lib/vault/index.ts +++ b/src/lib/vault/index.ts @@ -3,6 +3,10 @@ import { Worker as NodeWorker } from 'node:worker_threads' import { Data, NamedData } from '#types' +import { default as Constants } from '../constants' +import { default as Convert } from '../convert' +import { Bip39, Bip44, Blake2b, NanoNaCl } from '../crypto' +import { default as VaultTimer } from './vault-timer' import { default as VaultWorker } from './vault-worker' type Task = { @@ -13,6 +17,21 @@ type Task = { } export class Vault { + static get #blob () { + let importWorkerThreads = '' + NODE: importWorkerThreads = `import { parentPort } from 'node:worker_threads'` + return ` + ${importWorkerThreads} + ${Convert} + ${Constants} + const Bip39 = ${Bip39} + const Bip44 = ${Bip44} + const Blake2b = ${Blake2b} + const NanoNaCl = ${NanoNaCl} + ${VaultTimer} + ${VaultWorker} + ` + } static #instances: Vault[] = [] static get instances (): Vault[] { return this.#instances } @@ -27,12 +46,12 @@ export class Vault { this.#isIdle = true this.#isTerminated = false this.#queue = [] - this.#url = URL.createObjectURL(new Blob([VaultWorker], { type: 'text/javascript' })) + this.#url = URL.createObjectURL(new Blob([Vault.#blob], { type: 'text/javascript' })) BROWSER: this.#worker = new Worker(this.#url, { type: 'module' }) BROWSER: this.#worker.addEventListener('message', message => { this.#report(message.data) }) - NODE: this.#worker = new NodeWorker(VaultWorker, { + NODE: this.#worker = new NodeWorker(Vault.#blob, { eval: true, stderr: false, stdout: false diff --git a/src/lib/vault/timer.ts b/src/lib/vault/vault-timer.ts similarity index 89% rename from src/lib/vault/timer.ts rename to src/lib/vault/vault-timer.ts index d788ad4..9bc2d3c 100644 --- a/src/lib/vault/timer.ts +++ b/src/lib/vault/vault-timer.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -export class Timer { +export class VaultTimer { #f: () => any #elapsed: number = 0 #isPaused: boolean = false @@ -30,3 +30,5 @@ export class Timer { } } } + +export default `const VaultTimer = ${VaultTimer}` diff --git a/src/lib/vault/vault-worker.ts b/src/lib/vault/vault-worker.ts index 5c269e1..2f4efb8 100644 --- a/src/lib/vault/vault-worker.ts +++ b/src/lib/vault/vault-worker.ts @@ -3,17 +3,17 @@ import { parentPort } from 'node:worker_threads' import { NamedData } from '#types' -import { default as Constants, BIP44_COIN_NANO } from '../constants' -import { default as Convert, utf8 } from '../convert' +import { BIP44_COIN_NANO } from '../constants' +import { utf8 } from '../convert' import { Bip39, Bip44, Blake2b, NanoNaCl } from '../crypto' -import { Timer } from './timer' +import { VaultTimer } from './vault-timer' /** * Cross-platform worker for managing wallet secrets. */ -class VaultWorker { +export class VaultWorker { static #locked: boolean = true - static #timeout: Timer + static #timeout: VaultTimer static #type?: 'BIP-44' | 'BLAKE2b' static #seed?: ArrayBuffer static #mnemonic?: ArrayBuffer @@ -152,7 +152,7 @@ class VaultWorker { ? await Bip44.ckd(this.#seed, BIP44_COIN_NANO, index) : await this.#deriveBlake2bPrivateKey(this.#seed, index) const pub = await NanoNaCl.convert(new Uint8Array(prv)) - this.#timeout = new Timer(() => this.lock(), 120000) + this.#timeout = new VaultTimer(() => this.lock(), 120000) return { index, publicKey: pub.buffer } } catch (err) { console.error(err) @@ -211,7 +211,7 @@ class VaultWorker { ? await Bip44.ckd(this.#seed, BIP44_COIN_NANO, index) : await this.#deriveBlake2bPrivateKey(this.#seed, index) const sig = await NanoNaCl.detached(new Uint8Array(data), new Uint8Array(prv)) - this.#timeout = new Timer(() => this.lock(), 120000) + this.#timeout = new VaultTimer(() => this.lock(), 120000) return { signature: sig.buffer } } catch (err) { console.error(err) @@ -246,7 +246,7 @@ class VaultWorker { throw new TypeError('Invalid mnemonic') } this.#locked = false - this.#timeout = new Timer(() => this.lock(), 120000) + this.#timeout = new VaultTimer(() => this.lock(), 120000) return { isUnlocked: !this.#locked } } catch (err) { console.error(err) @@ -271,7 +271,7 @@ class VaultWorker { } this.#timeout.pause() const { iv, encrypted } = await this.#encryptWallet(key) - this.#timeout = new Timer(() => this.lock(), 120000) + this.#timeout = new VaultTimer(() => this.lock(), 120000) return { iv, salt: keySalt, encrypted } } catch (err) { console.error(err) @@ -591,16 +591,4 @@ class VaultWorker { } } -let importWorkerThreads = '' -NODE: importWorkerThreads = `import { parentPort } from 'node:worker_threads'` -export default ` - ${importWorkerThreads} - ${Convert} - ${Constants} - const Bip39 = ${Bip39} - const Bip44 = ${Bip44} - const Blake2b = ${Blake2b} - const NanoNaCl = ${NanoNaCl} - const Timer = ${Timer} - const Vault = ${VaultWorker} -` +export default `const VaultWorker = ${VaultWorker}`