]> git.codecow.com Git - libnemo.git/commitdiff
Fix autolocking.
authorChris Duncan <chris@codecow.com>
Sat, 27 Jun 2026 05:37:47 +0000 (22:37 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 27 Jun 2026 05:37:47 +0000 (22:37 -0700)
src/lib/vault/index.ts
src/lib/vault/vault-worker.ts

index e020c39038133181de88bb84b61440b9f550e369..736add66ab9385070d511e7c9fbc16d43ca0550e 100644 (file)
@@ -116,6 +116,7 @@ export class Vault {
                        if (typeof isLocked === 'boolean' && this.#isLocked !== isLocked) {
                                this.#isLocked = isLocked
                                this.dispatchEvent(new Event(isLocked ? LOCKED : UNLOCKED))
+                               if (id === 'autolock') return
                        }
                        const task = this.#tasks.get(id)
                        if (task == null) {
index 3573311184d0f0d474a6cb0a4673c5d7d999a345..34e333466da815788403ba621b67f2e708101878 100644 (file)
@@ -127,7 +127,7 @@ async function config (timeout?: number): Promise<void> {
                                throw new RangeError('Timeout must be at most 10 minutes')
                        }
                        _timeout = timeout * 1000
-                       _timer = new VaultTimer(() => lock(), _timeout)
+                       _timer = new VaultTimer(_autolock, _timeout)
                }
                return Promise.resolve()
        } catch (err) {
@@ -188,7 +188,7 @@ async function derive (index?: number): Promise<Record<string, number | ArrayBuf
                        const prv = new Uint8Array(result)
                        const pub = nano25519_derive(prv)
                        prv.fill(0)
-                       _timer = new VaultTimer(() => lock(), _timeout)
+                       _timer = new VaultTimer(_autolock, _timeout)
                        return { index, publicKey: pub.buffer }
                })
        } catch (err) {
@@ -252,7 +252,7 @@ async function sign (index?: number, data?: ArrayBuffer): Promise<Record<string,
                        const pub = nano25519_derive(prv)
                        const sig = nano25519_sign(new Uint8Array(data), new Uint8Array([...prv, ...pub]))
                        prv.fill(0)
-                       _timer = new VaultTimer(() => lock(), _timeout)
+                       _timer = new VaultTimer(_autolock, _timeout)
                        return { signature: sig.buffer }
                })
        } catch (err) {
@@ -295,7 +295,7 @@ async function unlock (type?: WalletType, key?: CryptoKey, iv?: ArrayBuffer, enc
                        _seed = seed
                        _mnemonic = mnemonic
                        _locked = false
-                       _timer = new VaultTimer(lock, _timeout)
+                       _timer = new VaultTimer(_autolock, _timeout)
                        return Promise.resolve({ isLocked: false })
                })
                .catch(err => {
@@ -325,7 +325,7 @@ async function update (key?: CryptoKey, salt?: ArrayBuffer): Promise<Record<stri
                }
                return WalletAesGcm.encrypt(_type, key, _seed, _mnemonic)
                        .then(({ iv, encrypted }) => {
-                               _timer = new VaultTimer(() => lock(), _timeout)
+                               _timer = new VaultTimer(_autolock, _timeout)
                                return { iv, salt, encrypted }
                        })
        } catch (err) {
@@ -379,6 +379,13 @@ function verify (seed?: ArrayBuffer, mnemonicPhrase?: string): Promise<Record<st
        }
 }
 
+async function _autolock (): Promise<void> {
+       const { isLocked } = await lock()
+       const id = 'autolock'
+       BROWSER: self.postMessage({ url: location.href, id, isLocked })
+       NODE: parentPort?.postMessage({ data: { url: threadId.toString(), id, isLocked } })
+}
+
 function _ckd (index: number): Promise<ArrayBuffer> {
        if (_seed == null) {
                throw new Error('Wallet seed not found')