4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 1 | import type { EncryptionState } from './types'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 2 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 3 | export type RecoveryPrompt = 'recover' | 'setup'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 4 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 5 | export interface InstallContext { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 6 | device: boolean; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 7 | standalone: boolean; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 8 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 9 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 10 | export interface PersistResult { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 11 | persisted: boolean; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 12 | usage: number | null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 13 | quota: number | null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 14 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 15 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 16 | export interface StorageLike { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 17 | getItem(key: string): string | null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 18 | setItem(key: string, value: string): void; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 19 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 20 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 21 | export interface PersistManager { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 22 | persisted?(): Promise<boolean>; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 23 | persist?(): Promise<boolean>; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 24 | estimate?(): Promise<{ usage?: number; quota?: number }>; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 25 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 26 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 27 | export function canDismissRecovery(ctx: InstallContext): boolean { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 28 | return ctx.device && !ctx.standalone; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 29 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 30 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 31 | export function recoveryPrompt(state: EncryptionState | null, ctx: InstallContext, dismissed: boolean): RecoveryPrompt | null { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 32 | if (!state) return null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 33 | if (state.recovery === 'Incomplete') return 'recover'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 34 | if (state.recovery === 'Disabled') return dismissed && canDismissRecovery(ctx) ? null : 'setup'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 35 | return null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 36 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 37 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 38 | export function dismissalKey(kind: 'recovery' | 'storage', account: string): string { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 39 | return `malice.dismissed.${kind}.${account}`; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 40 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 41 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 42 | export function isDismissed(store: StorageLike, kind: 'recovery' | 'storage', account: string): boolean { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 43 | try { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 44 | return store.getItem(dismissalKey(kind, account)) === '1'; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 45 | } catch { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 46 | return false; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 47 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 48 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 49 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 50 | export function setDismissed(store: StorageLike, kind: 'recovery' | 'storage', account: string): void { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 51 | try { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 52 | store.setItem(dismissalKey(kind, account), '1'); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 53 | } catch { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 54 | // dismissal then lasts only for this page load |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 55 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 56 | } |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 57 | |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 58 | export async function requestPersistence(manager: PersistManager | undefined): Promise<PersistResult | null> { |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 59 | if (!manager?.persist || !manager.persisted) return null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 60 | let persisted = await manager.persisted(); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 61 | if (!persisted) persisted = await manager.persist(); |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 62 | const estimate = manager.estimate ? await manager.estimate().catch(() => null) : null; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 63 | return { persisted, usage: estimate?.usage ?? null, quota: estimate?.quota ?? null }; |
4929dcd34937 | atagen | 2026-10-26 04:43 UTC | 64 | } |