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 = {
}
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 }
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
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
? 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)
? 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)
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)
}
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)
}
}
-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}`