// Background-owned state set while the user has just created or rotated a // vault identity and is confirming their recovery words. It intentionally // never lives in chrome.storage.local: content scripts can read/write local // storage unless the browser supports and has applied a trusted-context gate. // Chrome's session storage is in-memory and trusted-context-only by default; // Firefox falls back to this background module's memory for the same process. const KEY = "peach_recovery_pending"; const RECOVERY_CONFIRM_WINDOW_MS = 10 * 60 * 1000; let inMemoryDeadline = 0; function sessionStorage(): chrome.storage.StorageArea | undefined { return chrome.storage?.session; } export async function isRecoveryConfirmPending(): Promise { try { const session = sessionStorage(); if (!session) return inMemoryDeadline > Date.now(); const out = await session.get(KEY); const deadline = (out as Record)[KEY]; if (typeof deadline === "number" && deadline > Date.now()) return true; await clearRecoveryConfirmPending(); return false; } catch { return inMemoryDeadline > Date.now(); } } export async function setRecoveryConfirmPending(): Promise { const deadline = Date.now() + RECOVERY_CONFIRM_WINDOW_MS; try { const session = sessionStorage(); if (session) { await session.set({ [KEY]: deadline }); return; } } catch { /* use the in-memory fallback */ } inMemoryDeadline = deadline; } export async function clearRecoveryConfirmPending(): Promise { try { const session = sessionStorage(); if (session) { await session.remove(KEY); } } catch { /* best-effort */ } inMemoryDeadline = 0; }