vault.ts

66.7 KB · raw

1/**2 * vault.ts — sole owner of vault state and chrome.storage vault keys.3 *4 * Every read/write of `peach_vault_a`, `peach_vault_b`, and `peach_settings`5 * goes through this module. No other file accesses those keys directly.6 * External callers reach vault operations via `messages.ts` — never by7 * importing from here cross-boundary (popup/content must not). Invariants8 * for the two-slot schema, unlock timing, and key/disk atomicity are in9 * Projects.md §3.2–§3.6.10 */1112import { deriveKey, generateSalt } from "../crypto/argon2";13import { extractDomain } from "../shared/domain";14import {15  rootKeyToWords,16  wordsToRootKeyMaterial,17  deriveRecoveryKey,18} from "../shared/recovery-words";19import type {20  AccentColorName,21  LegacyVaultEntry,22  ManualSummonRecord,23  SavedIdentity,24  StoredVault,25  VaultEntry,26  VaultHistory,27  VaultSnapshot,28} from "../shared/types";29import { decrypt, encrypt, zeroKey } from "./aes";30import { clearAutoLock, touchAutoLock } from "./alarms";3132type EntryDraft = Omit<33  VaultEntry,34  "id" | "createdAt" | "modifiedAt" | "deletedAt"35>;3637// Slot-neutral storage keys (v1.4.24+). Both slots always exist; slot B38// either holds the decoy vault (when duress is configured) or random noise39// that looks identical at rest. No external indicator is stored — duress40// configuration is recorded only inside the encrypted payload of slot A.41const SLOT_A = "peach_vault_a";42const SLOT_B = "peach_vault_b";4344// Legacy keys, migrated away on first access.45const LEGACY_MAIN = "peach_vault";46const LEGACY_DECOY = "peach_decoy_vault";47const LEGACY_DURESS_FLAG = "peach_duress";4849// Plaintext is padded to the next multiple of this size before encryption.50// This coarsens ciphertext-length leakage into 1 KiB buckets.51const PAD_BLOCK = 1024;5253let derivedKey: Uint8Array | null = null;54// 32-byte random root key, separate from the derivedKey used for vault encryption.55// The root key is wrapped (encrypted) under the recovery key derived from the56// 12-word phrase. When the user changes their password, only the wrapping changes;57// the root key and recovery words remain stable.58let rootKey: Uint8Array | null = null;59let entries: VaultEntry[] = [];60let domainIndex: Map<string, VaultEntry[]> = new Map();61let vaultMode: "real" | "decoy" = "real";62// Cached post-unlock view of whether the real vault records that duress is63// configured. Only meaningful after a real-mode unlock; for decoy-mode unlock64// this stays false because we never see slot A's plaintext.65let duressConfigured = false;6667let migrationRan = false;6869// ---------------------------------------------------------------------------70// Settings (persisted inside the encrypted vault store as a sidecar key)71// ---------------------------------------------------------------------------7273export type DisplayFont = "cormorant-italic" | "cormorant" | "inter" | "system";74export type TextSize = "small" | "medium" | "large";7576export interface VaultSettings {77  accentColor?: AccentColorName;78  theme?: "dark" | "light";79  autoLockMinutes?: number;80  clipboardClearSeconds?: number;81  lockOnClose?: boolean;82  breachMonitoring?: boolean;83  passkeyProviderEnabled?: boolean;84  trashRetentionDays?: number;85  manualSummonDomains?: Record<string, ManualSummonRecord>;86  declinedCardFingerprints?: string[];87  /**88   * Display font for headlines, brand wordmark, and narrative copy. Default89   * is "cormorant-italic" — the bold italic serif that ships as Peach's90   * voice. Non-italic options exist for users who prefer upright type.91   */92  displayFont?: DisplayFont;93  /**94   * Text size scale for body copy and entry titles. Stops at "large" by95   * design — XL clips dense screens (Vault, Health) inside the fixed96   * 330×480 popup surface.97   */98  textSize?: TextSize;99  /**100   * When true, all entrance/exit transitions and ambient animations are101   * collapsed to instant. Honoured alongside the user's OS-level102   * `prefers-reduced-motion`.103   */104  reduceMotion?: boolean;105  /**106   * User overrides for keyboard shortcuts. Keys are ShortcutAction names107   * (see src/shared/shortcuts.ts). Missing actions fall through to108   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the109   * canonical lookup. Storing only the deltas keeps the vault payload110   * small and lets default updates land for users who never rebind.111   */112  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;113}114115let settings: VaultSettings = {};116117export function getSettings(): VaultSettings {118  return settings;119}120121export async function ensureSettingsLoaded(): Promise<VaultSettings> {122  await loadSettings();123  return settings;124}125126export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {127  await loadSettings();128  settings = { ...settings, ...patch };129  await persistSettings();130}131132async function persistSettings(): Promise<void> {133  try {134    await chrome.storage.local.set({ peach_settings: settings });135  } catch (err) {136    throw mapStorageError(err);137  }138}139140function mapStorageError(err: unknown): Error {141  const message = err instanceof Error ? err.message : String(err);142  if (/QUOTA_BYTES|quota/i.test(message)) {143    return new Error("STORAGE_QUOTA_EXCEEDED");144  }145  return err instanceof Error ? err : new Error(message);146}147148async function loadSettings(): Promise<void> {149  const result = await chrome.storage.local.get("peach_settings");150  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};151}152153// ---------------------------------------------------------------------------154// HIBP breach check (k-anonymity model)155// ---------------------------------------------------------------------------156157export async function checkBreach(password: string): Promise<number> {158  const encoder = new TextEncoder();159  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));160  const hashHex = Array.from(new Uint8Array(hashBuffer))161    .map((b) => b.toString(16).padStart(2, "0"))162    .join("")163    .toUpperCase();164165  const prefix = hashHex.slice(0, 5);166  const suffix = hashHex.slice(5);167168  try {169    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);170    if (!res.ok) return 0;171    const text = await res.text();172    const match = text.split("\r\n").find((l) => l.startsWith(suffix));173    return match ? parseInt(match.split(":")[1], 10) : 0;174  } catch {175    return 0; // Network unavailable — don't block176  }177}178179export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {180  const fingerprint = await cardFingerprint(last4, expMonth, expYear);181  await loadSettings();182  settings = {183    ...settings,184    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],185  };186  await persistSettings();187}188189export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {190  await loadSettings();191  const fingerprint = await cardFingerprint(last4, expMonth, expYear);192  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);193}194195async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {196  const encoder = new TextEncoder();197  const buf = await crypto.subtle.digest("SHA-1", encoder.encode(`${last4}${expMonth}${expYear}`));198  return Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, "0")).join("");199}200201function hex(buf: Uint8Array): string {202  return Array.from(buf)203    .map((b) => b.toString(16).padStart(2, "0"))204    .join("");205}206207function unhex(s: string): Uint8Array {208  const out = new Uint8Array(s.length / 2);209  for (let i = 0; i < s.length; i += 2) {210    out[i / 2] = parseInt(s.slice(i, i + 2), 16);211  }212  return out;213}214215// ---------------------------------------------------------------------------216// Root key generation and recovery word wrapping217// ---------------------------------------------------------------------------218219/**220 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key221 * derived from the recovery word material via HKDF-SHA256. The wrapped blob222 * fields are stored in slot A alongside the password-wrapped ciphertext. The223 * root key itself is kept in memory for the session so getRecoveryWords() can224 * encode it without re-deriving.225 */226async function generateAndWrapRootKey(): Promise<{227  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey228  recoverySalt: string;229  wrappedRootKey: string;230  wrappedNonce: string;231  wrappedTag: string;232}> {233  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));234235  // Derive the recovery key from the BIP39 words encoding of the root key.236  const words = await rootKeyToWords(rootKeyBytes);237  const wordMaterial = await wordsToRootKeyMaterial(words);238  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));239  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);240241  // Wrap the root key with the recovery key using AES-256-GCM directly.242  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.243  const nonce = crypto.getRandomValues(new Uint8Array(12));244  const encoded = new TextEncoder().encode(hex(rootKeyBytes));245  const raw = await crypto.subtle.encrypt(246    {247      name: "AES-GCM",248      iv: nonce,249      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,250      tagLength: 128,251    },252    recoveryKey,253    encoded254  );255  // WebCrypto returns ciphertext || auth tag concatenated; split them.256  const rawU8 = new Uint8Array(raw);257  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));258  const tag = hex(rawU8.slice(rawU8.length - 16));259260  return {261    rawRootKey: hex(rootKeyBytes),262    recoverySalt: hex(recoverySalt),263    wrappedRootKey: ciphertext,264    wrappedNonce: hex(nonce),265    wrappedTag: tag,266  };267}268269/**270 * Unwrap the root key using recovery words. Called during recovery flow when271 * the user provides their 12-word phrase and a new password.272 * Exported for use by the RECOVER_WITH_WORDS message handler.273 */274export async function unwrapRootKeyForRecovery(275  wrappedRootKey: string,276  wrappedNonce: string,277  wrappedTag: string,278  recoverySaltHex: string,279  words: string[]280): Promise<void> {281  const wordMaterial = await wordsToRootKeyMaterial(words);282  const recoverySalt = unhex(recoverySaltHex);283  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);284285  // Decrypt and cache the root key. The root key bytes were hex-encoded before286  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt287  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.288  const combined = unhex(wrappedRootKey + wrappedNonce + wrappedTag);289  const nonceU8 = combined.slice(0, 12);290  const ciphertext = combined.slice(12, combined.length - 16);291  const storedTag = combined.slice(combined.length - 16);292  const raw = await crypto.subtle.decrypt(293    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },294    recoveryKey,295    new Uint8Array([...ciphertext, ...storedTag])296  );297  const decryptedHex = new TextDecoder().decode(raw);298  rootKey = unhex(decryptedHex);299}300301/**302 * Re-wrap the cached root key under a new derived key (from the user's new303 * password). Called after password change and after recovery re-key so the304 * same recovery words continue to work with the updated password.305 */306export async function rewrapRootKeyForPassword(307  newDerivedKey: Uint8Array,308  _newSalt: Uint8Array309): Promise<void> {310  if (!rootKey) throw new Error("root key not set");311  const { nonce, ciphertext, tag } = await encrypt(312    newDerivedKey,313    hex(rootKey)314  );315  const slotA = await loadSlot(SLOT_A);316  if (!slotA) throw new Error("missing slot A");317  await writeSlot(SLOT_A, {318    ...slotA,319    recovery_wrapped_root_key: ciphertext,320    recovery_nonce: nonce,321    recovery_tag: tag,322    // recovery_salt does NOT change when password changes — the recovery323    // words always derive the same key; only the wrapping key changes.324  });325}326327/**328 * Change the vault encryption to a new derived key (already derived from329 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.330 * Caller is responsible for key swap and snapshot.331 */332async function reencryptVaultForNewKey(333  newKey: Uint8Array,334  slotA: StoredVault335): Promise<void> {336  const plaintext = encodePayload(entries, duressConfigured);337  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);338  await writeSlot(SLOT_A, {339    ...slotA,340    nonce,341    ciphertext,342    tag,343  });344}345346/**347 * Full password change with an already-derived key (used after recovery).348 * Unlike changePassword(), this skips re-verification of current password349 * since recovery flow has already validated the user's identity via words.350 */351export async function changePasswordWithNewKey(352  newSalt: Uint8Array,353  newKey: Uint8Array354): Promise<void> {355  const slotA = await loadSlot(SLOT_A);356  if (!slotA) throw new Error("missing slot A");357  await reencryptVaultForNewKey(newKey, slotA);358  zeroKey(derivedKey!);359  derivedKey = newKey;360  if (!duressConfigured) {361    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));362  }363}364365// ---------------------------------------------------------------------------366// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive367// a new key after password reset without re-importing the crypto module.368// ---------------------------------------------------------------------------369370export { deriveKey, generateSalt };371372export async function loadSlotA(): Promise<StoredVault | null> {373  return loadSlot(SLOT_A);374}375376/**377 * Re-derive the session root key from the already-derived AES key (keyA from378 * unlock) and the recovery salt stored in slot A. This is the inverse of379 * generateAndWrapRootKey() — both operations use the same HKDF with the same380 * salt and info string, so they cancel out.381 *382 * The derivedKey and rootKey are related by:383 *   derivedKey = Argon2id(password, slotA.salt)   → AES key for vault payload384 *   rootKey    = HKDF-SHA256(recovery_words, recovery_salt)  → wrapped by HKDF385 *386 * Since we only store the wrapped root key and the recovery salt, we can387 * only re-derive rootKey during an active (unlocked) session where we have388 * the derivedKey already. On lock, rootKey is zeroed.389 *390 * For pre-v2.12 vaults that have no recovery_salt, this returns null and391 * the caller must invoke migrateToRecovery() to generate the recovery escrow.392 */393async function deriveRootKeyFromDerivedKey(394  _derivedKey: Uint8Array,395  _recoverySaltHex: string396): Promise<Uint8Array | null> {397  // The root key is stored wrapped under the recovery key derived from the398  // 12-word phrase. It cannot be re-derived from derivedKey alone — that would399  // require knowing the original recovery words. The in-memory rootKey is set400  // during unlock if recovery data is present.401  // This function exists as a potential future path if we ever store an402  // additional derived-key → root-key relationship. Currently rootKey is403  // cached directly during unlock.404  return null;405}406407/**408 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),409 * generate the root key, wrap it under the recovery words, and persist the410 * recovery escrow into slot A. The caller must show the recovery words to the411 * user before calling this — once called, those words are the vault's recovery412 * path. This runs once, on the first unlock of an upgraded vault.413 */414export async function migrateToRecovery(): Promise<void> {415  if (!derivedKey || vaultMode !== "real") {416    throw new Error("vault must be unlocked in real mode");417  }418  const slotA = await loadSlot(SLOT_A);419  if (!slotA) throw new Error("missing slot A");420  if (slotA.recovery_salt && slotA.recovery_wrapped_root_key) return; // already done421422  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =423    await generateAndWrapRootKey();424425  rootKey = unhex(rawRootKey);426427  const nextSlotA: StoredVault = {428    ...slotA,429    recovery_salt: recoverySalt,430    recovery_wrapped_root_key: wrappedRootKey,431    recovery_nonce: wrappedNonce,432    recovery_tag: wrappedTag,433  };434  await writeSlot(SLOT_A, nextSlotA);435}436437/**438 * Returns the 12 recovery words for the current session's root key.439 * Must be called while the vault is unlocked. Throws if recovery has not440 * been set up (pre-v2.12 vault that hasn't migrated yet).441 */442export async function getRecoveryWords(): Promise<string[]> {443  if (!rootKey) {444    throw new Error("recovery not set up — vault may need migration");445  }446  return rootKeyToWords(rootKey);447}448449async function loadSlot(key: string): Promise<StoredVault | null> {450  const result = await chrome.storage.local.get(key);451  return (result as Record<string, StoredVault | undefined>)[key] ?? null;452}453454async function writeSlot(key: string, vault: StoredVault): Promise<void> {455  try {456    await chrome.storage.local.set({ [key]: vault });457  } catch (err) {458    throw mapStorageError(err);459  }460}461462/**463 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault464 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +465 * peach_vault_b). Runs at most once per session; safe to call repeatedly.466 */467async function ensureMigrated(): Promise<void> {468  if (migrationRan) return;469  migrationRan = true;470471  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];472  const raw = await chrome.storage.local.get(keys);473  const slotAExists = raw[SLOT_A] !== undefined;474  const slotBExists = raw[SLOT_B] !== undefined;475  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;476  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;477478  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.479  if (!slotAExists && !legacyMain) return;480481  // Already migrated.482  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;483484  if (legacyMain && !slotAExists) {485    await writeSlot(SLOT_A, legacyMain);486  }487488  // If slot B isn't set, populate it: use legacy decoy when present, else noise.489  if (!slotBExists) {490    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;491    if (legacyDecoy) {492      await writeSlot(SLOT_B, legacyDecoy);493    } else if (target) {494      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));495    }496  }497498  const toRemove: string[] = [];499  if (legacyMain) toRemove.push(LEGACY_MAIN);500  if (legacyDecoy) toRemove.push(LEGACY_DECOY);501  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);502  if (toRemove.length > 0) {503    await chrome.storage.local.remove(toRemove);504  }505}506507// ---------------------------------------------------------------------------508// Padding + noise helpers509// ---------------------------------------------------------------------------510511/**512 * Pads the plaintext string with trailing spaces so that the encoded byte513 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing514 * whitespace after a complete document, so the padding is transparent on515 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.516 */517function padPlaintext(json: string): string {518  const encoded = new TextEncoder().encode(json);519  const target = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);520  const padLen = target - encoded.length;521  return json + " ".repeat(padLen);522}523524/**525 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The526 * ciphertext length matches the given byte count. Because GCM ciphertext is527 * pseudo-random, this blob is indistinguishable at rest from a real528 * encrypted vault without the key.529 *530 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto531 * spec hard limit). Chunk the fill so vaults larger than that — common once532 * the user has dozens of entries or any passkey — don't throw.533 */534const CSPRNG_CHUNK = 65_536;535536function fillRandom(buf: Uint8Array): Uint8Array {537  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {538    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);539    const chunk = new Uint8Array(end - offset);540    crypto.getRandomValues(chunk);541    buf.set(chunk, offset);542  }543  return buf;544}545546function generateNoiseBlob(ciphertextBytes: number): StoredVault {547  const salt = crypto.getRandomValues(new Uint8Array(16));548  const nonce = crypto.getRandomValues(new Uint8Array(12));549  const cipher = fillRandom(new Uint8Array(ciphertextBytes));550  const tag = crypto.getRandomValues(new Uint8Array(16));551  return {552    version: 1,553    salt: hex(salt),554    nonce: hex(nonce),555    ciphertext: hex(cipher),556    tag: hex(tag),557  };558}559560// ---------------------------------------------------------------------------561// Encrypted plaintext envelope562// ---------------------------------------------------------------------------563564type VaultPayload = {565  entries: VaultEntry[];566  duressConfigured?: boolean;567};568569type DecryptedPayload = {570  entries: VaultEntry[];571  duressConfigured: boolean;572  /**573   * True when at least one entry in the on-disk payload had a legacy shape574   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)575   * and got rewritten by normalizeEntry. The unlock path uses this to576   * persist the new shape immediately — without it, the migration would577   * re-run on every unlock and old shape never disappears from disk.578   */579  migratedFromLegacy: boolean;580};581582function encodePayload(entries: VaultEntry[], duress: boolean): string {583  const envelope: VaultPayload = { entries, duressConfigured: duress };584  return padPlaintext(JSON.stringify(envelope));585}586587function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {588  if ("created" in raw || "updated" in raw) return true;589  const flat = raw as Partial<VaultEntry>;590  if (flat.type === "passkey") return true;591  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.592  if (flat.rpId !== undefined && flat.passkey === undefined) return true;593  return false;594}595596function decodePayload(plain: string): DecryptedPayload {597  const parsed = JSON.parse(plain) as598    | Array<VaultEntry | LegacyVaultEntry>599    | VaultPayload;600601  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];602  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);603604  const normalized = rawEntries.map(normalizeEntry);605  const { merged } = mergePasskeyOnlyIntoLogins(normalized);606607  if (Array.isArray(parsed)) {608    return {609      entries: normalized,610      duressConfigured: false,611      migratedFromLegacy: true, // pre-A-2 array shape always needs save612    };613  }614615  return {616    entries: normalized,617    duressConfigured: parsed.duressConfigured === true,618    migratedFromLegacy: flatMigrationNeeded || merged,619  };620}621622/**623 * One-time consolidation pass run alongside the legacy passkey migration.624 *625 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`626 * entry per registration — so users who tested registration multiple times627 * accumulated several passkey-bearing entries for the same RP, and the628 * migration to the unified shape carried that duplication forward. This629 * pass collapses the duplicates:630 *631 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.632 *   2. Within each group, the most recently-active credential wins (prefer633 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).634 *   3. If a NON-passkey login entry exists for that RP (matched by url or635 *      additionalUrls hostname), attach the winning passkey to it and636 *      soft-delete every passkey-bearing entry in the group.637 *   4. Otherwise, soft-delete every loser in the group, keeping the winner638 *      as a passkey-only entry.639 *640 * Soft delete (deletedAt) rather than splice — the trash flow keeps the641 * record around for retention and the user can restore if the heuristic642 * picked wrong. The `merged` flag goes back to decodePayload so the643 * unlock path persists the consolidated shape immediately.644 */645function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {646  let merged = false;647  const now = Date.now();648649  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);650  const byRpId = new Map<string, VaultEntry[]>();651  for (const e of candidates) {652    const rpId = e.passkey!.rpId;653    const list = byRpId.get(rpId);654    if (list) list.push(e);655    else byRpId.set(rpId, [e]);656  }657658  for (const [rpId, group] of byRpId) {659    if (group.length === 0) continue;660    group.sort((a, b) => {661      const aT =662        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;663      const bT =664        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;665      return bT - aT;666    });667    const winner = group[0]!;668669    // Look for a NON-passkey login entry matching this RP. Use the URL670    // hostname rather than a substring search so a "login.foo.com" entry671    // doesn't accidentally absorb a passkey for "foo.com".672    const loginTarget = entries.find((e) => {673      if (e.deletedAt || e.passkey || e.type !== "login") return false;674      if (e.url && extractDomain(e.url) === rpId) return true;675      for (const extra of e.additionalUrls ?? []) {676        if (extractDomain(extra) === rpId) return true;677      }678      return false;679    });680681    if (loginTarget) {682      loginTarget.passkey = winner.passkey;683      loginTarget.modifiedAt = now;684      for (const dup of group) {685        if (!dup.deletedAt) {686          dup.deletedAt = now;687          dup.modifiedAt = now;688        }689      }690      merged = true;691    } else if (group.length > 1) {692      for (let i = 1; i < group.length; i += 1) {693        const dup = group[i]!;694        if (!dup.deletedAt) {695          dup.deletedAt = now;696          dup.modifiedAt = now;697        }698      }699      merged = true;700    }701  }702703  return { merged };704}705706async function tryDecryptSlot(707  vault: StoredVault,708  key: Uint8Array709): Promise<DecryptedPayload | null> {710  try {711    const plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag);712    return decodePayload(plain);713  } catch {714    return null;715  }716}717718// ---------------------------------------------------------------------------719// Core operations720// ---------------------------------------------------------------------------721722function buildDomainIndexSync(all: VaultEntry[]): void {723  domainIndex = new Map();724725  for (const entry of all) {726    if (entry.deletedAt) continue;727    if (entry.type === "note" || entry.type === "card") continue;728729    // An entry can match multiple sites via `additionalUrls`. Index each730    // hostname so `queryDomain` returns the entry regardless of which alias731    // the user is currently visiting. Dedupe per-entry so a single entry732    // never appears twice in one bucket.733    const domains = new Set<string>();734    const primary = extractDomain(entry.url ?? "");735    if (primary) domains.add(primary);736    for (const extra of entry.additionalUrls ?? []) {737      const d = extractDomain(extra);738      if (d) domains.add(d);739    }740    if (domains.size === 0) continue;741742    for (const domain of domains) {743      const bucket = domainIndex.get(domain) ?? [];744      bucket.push(entry);745      domainIndex.set(domain, bucket);746    }747  }748}749750export async function createVault(password: string): Promise<void> {751  await ensureMigrated();752753  const salt = generateSalt();754  derivedKey = await deriveKey(password, salt);755  entries = [];756  vaultMode = "real";757  duressConfigured = false;758  buildDomainIndexSync(entries);759760  // Generate and wrap the root key for recovery word escrow.761  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =762    await generateAndWrapRootKey();763  rootKey = unhex(rawRootKey);764765  const plaintext = encodePayload(entries, duressConfigured);766  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);767768  const slotA: StoredVault = {769    version: 1,770    salt: hex(salt),771    nonce,772    ciphertext,773    tag,774    recovery_salt: recoverySalt,775    recovery_wrapped_root_key: wrappedRootKey,776    recovery_nonce: wrappedNonce,777    recovery_tag: wrappedTag,778  };779  await writeSlot(SLOT_A, slotA);780781  // Matching-size noise blob for slot B, so a fresh install shows two782  // similarly-shaped records regardless of whether duress is ever set up.783  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));784785  touchAutoLock();786}787788export async function hasVault(): Promise<boolean> {789  await ensureMigrated();790  return (await loadSlot(SLOT_A)) !== null;791}792793/**794 * Returns whether the real vault has recorded that a duress passphrase is795 * configured. Reliable only after a real-mode unlock — before unlock or in796 * decoy mode, returns false because we cannot read slot A's plaintext.797 */798export async function isDuressEnabled(): Promise<boolean> {799  return vaultMode === "real" && duressConfigured;800}801802export async function hasDecoyVault(): Promise<boolean> {803  return isDuressEnabled();804}805806export async function setupDuress(duressPassword: string): Promise<void> {807  if (!derivedKey || vaultMode !== "real") {808    throw new Error("vault must be unlocked in real mode");809  }810  await ensureMigrated();811812  // Encrypt the decoy payload (empty entries) under the duress key. Pad to813  // at least the current slot A ciphertext size so the two slots look the814  // same at rest at setup time.815  const slotA = await loadSlot(SLOT_A);816  if (!slotA) throw new Error("missing slot A");817818  const decoySalt = generateSalt();819  const decoyKey = await deriveKey(duressPassword, decoySalt);820  try {821    // Pad the decoy plaintext to match slot A's ciphertext length (minus the822    // 16-byte tag) so encrypted sizes align exactly.823    const targetBytes = slotA.ciphertext.length / 2;824    let plaintext = encodePayload([], false);825    const encoded = new TextEncoder().encode(plaintext);826    if (encoded.length < targetBytes) {827      plaintext = plaintext + " ".repeat(targetBytes - encoded.length);828    }829    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext);830    await writeSlot(SLOT_B, {831      version: 1,832      salt: hex(decoySalt),833      nonce,834      ciphertext,835      tag,836    });837  } finally {838    zeroKey(decoyKey);839  }840841  // Re-encrypt slot A with the duressConfigured flag set to true. No external842  // flag is written — the bit lives inside the encrypted envelope.843  duressConfigured = true;844  await persistSlotA();845}846847export async function disableDuress(): Promise<void> {848  if (!derivedKey || vaultMode !== "real") {849    throw new Error("vault must be unlocked in real mode");850  }851  await ensureMigrated();852853  // Overwrite slot B with fresh noise that matches slot A's size. The slot854  // remains occupied so its mere existence does not signal that duress was855  // ever configured.856  const slotA = await loadSlot(SLOT_A);857  if (slotA) {858    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));859  }860861  duressConfigured = false;862  await persistSlotA();863}864865export async function verifyMainPassword(password: string): Promise<boolean> {866  await ensureMigrated();867  const slotA = await loadSlot(SLOT_A);868  if (!slotA) return false;869  const key = await deriveKey(password, unhex(slotA.salt));870  try {871    return (await tryDecryptSlot(slotA, key)) !== null;872  } finally {873    zeroKey(key);874  }875}876877export async function unlock(password: string): Promise<boolean> {878  await ensureMigrated();879880  // Load both slots in parallel. Both always exist post-migration; the881  // parallel load and the parallel derivation below keep wall-clock time882  // constant regardless of which slot's passphrase (real or decoy) the883  // caller supplied, so a timing side channel cannot distinguish the884  // duress path from the real-unlock path (DUR-002).885  const [slotA, slotB] = await Promise.all([886    loadSlot(SLOT_A),887    loadSlot(SLOT_B),888  ]);889890  // Derive both keys in parallel, unconditionally.891  const [keyA, keyB] = await Promise.all([892    slotA ? deriveKey(password, unhex(slotA.salt)) : Promise.resolve(null),893    slotB ? deriveKey(password, unhex(slotB.salt)) : Promise.resolve(null),894  ]);895896  // Attempt both decrypts in parallel.897  const [payloadA, payloadB] = await Promise.all([898    keyA && slotA ? tryDecryptSlot(slotA, keyA) : Promise.resolve(null),899    keyB && slotB ? tryDecryptSlot(slotB, keyB) : Promise.resolve(null),900  ]);901902  if (payloadA && keyA) {903    derivedKey = keyA;904    vaultMode = "real";905    entries = payloadA.entries;906    duressConfigured = payloadA.duressConfigured;907    buildDomainIndexSync(entries);908    await loadSettings();909    touchAutoLock();910    if (keyB) zeroKey(keyB);911912    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).913    if (slotA) {914      const needsRecoverySetup =915        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;916      if (!needsRecoverySetup) {917        rootKey = await deriveRootKeyFromDerivedKey(918          keyA,919          slotA.recovery_salt!920        );921      }922    }923924    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat925    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so926    // the next save uses the new format and migration doesn't re-run forever.927    if (payloadA.migratedFromLegacy) {928      await persistSlotA();929    }930    return true;931  }932933  if (payloadB && keyB) {934    derivedKey = keyB;935    vaultMode = "decoy";936    entries = payloadB.entries;937    duressConfigured = false;938    buildDomainIndexSync(entries);939    await loadSettings();940    touchAutoLock();941    if (keyA) zeroKey(keyA);942    return true;943  }944945  if (keyA) zeroKey(keyA);946  if (keyB) zeroKey(keyB);947  return false;948}949950export function lock(): void {951  if (derivedKey) {952    zeroKey(derivedKey);953    derivedKey = null;954  }955  if (rootKey) {956    rootKey.fill(0);957    rootKey = null;958  }959960  entries = [];961  domainIndex = new Map();962  vaultMode = "real";963  duressConfigured = false;964  clearAutoLock();965}966967export function isUnlocked(): boolean {968  return derivedKey !== null;969}970971// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).972// Used by Android biometric unlock — the key was derived on the native side.973export async function unlockWithKey(974  masterKeyHex: string,975): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {976  try {977    const masterKeyBytes = unhex(masterKeyHex);978    const stored = await loadSlot(SLOT_A);979    if (!stored) return { ok: false, error: "invalid_key" };980    const payload = await tryDecryptSlot(stored, masterKeyBytes);981    if (!payload) return { ok: false, error: "invalid_key" };982983    derivedKey = masterKeyBytes;984    vaultMode = "real";985    entries = payload.entries;986    duressConfigured = payload.duressConfigured;987    buildDomainIndexSync(entries);988    await loadSettings();989    touchAutoLock();990    return { ok: true, entries };991  } catch {992    return { ok: false, error: "invalid_key" };993  }994}995996// Returns the current in-memory derived key as a hex string.997// Used by the biometric enable flow in Settings to store the key in Android Keystore.998export function getDerivedKeyHex(): string {999  if (!derivedKey) throw new Error("vault_not_unlocked");1000  return hex(derivedKey);1001}10021003export function getEntries(): VaultEntry[] {1004  if (!derivedKey) throw new Error("vault locked");1005  return entries.filter((entry) => !entry.deletedAt);1006}10071008const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;10091010export function getIdentities(): SavedIdentity[] {1011  if (!derivedKey) throw new Error("vault locked");1012  const active = entries.filter((e) => !e.deletedAt && e.type === "login");10131014  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1015  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();10161017  for (const entry of active) {1018    const value = entry.username?.trim();1019    if (!value) continue;1020    const map = EMAIL_RE.test(value) ? emailMap : usernameMap;1021    const existing = map.get(value);1022    const domain = extractDomain(entry.url ?? "") ?? "";1023    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;1024    if (!existing || modifiedAt > existing.lastUsed) {1025      map.set(value, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1026    } else {1027      map.set(value, { ...existing, count: existing.count + 1 });1028    }1029  }10301031  const toIdentity = (type: "email" | "username") =>1032    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1033      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1034    });10351036  return [1037    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1038    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1039  ];1040}10411042export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1043  if (!derivedKey) throw new Error("vault locked");10441045  const now = Date.now();1046  const added: VaultEntry[] = [];1047  for (const draft of drafts) {1048    try {1049      const sanitized = sanitizeEntryDraft(draft);1050      const nextEntry: VaultEntry = {1051        ...sanitized,1052        id: crypto.randomUUID(),1053        createdAt: now,1054        modifiedAt: now,1055      };1056      entries.push(nextEntry);1057      added.push(nextEntry);1058    } catch {1059      // Skip invalid drafts silently — caller can see diff between input and output length1060    }1061  }10621063  if (added.length > 0) {1064    await persistEntries();1065  }10661067  return added;1068}10691070/**1071 * Restore a set of entries in full fidelity — preserves id, createdAt,1072 * modifiedAt, and every other field unlike `batchAddEntries` which1073 * assigns fresh identity. Used by restore paths where the backup is the1074 * authoritative source of truth. `mode` controls how entries already in1075 * the vault are treated.1076 */1077export async function restoreEntries(1078  incoming: VaultEntry[],1079  mode: "replace" | "merge"1080): Promise<{ added: number; updated: number; skipped: number }> {1081  if (!derivedKey) throw new Error("vault locked");10821083  // A replace with zero incoming entries is almost certainly a caller bug1084  // (e.g. an import that silently dropped everything). Refuse rather than1085  // wipe the vault.1086  if (mode === "replace" && incoming.length === 0) {1087    throw new Error("refuse to replace with empty entry set");1088  }10891090  let added = 0;1091  let updated = 0;1092  let skipped = 0;10931094  if (mode === "replace") {1095    entries = [];1096    for (const entry of incoming) {1097      const normalized = sanitizeRestoredEntry(entry);1098      if (!normalized) {1099        skipped += 1;1100        continue;1101      }1102      entries.push(normalized);1103      added += 1;1104    }1105  } else {1106    const byId = new Map<string, VaultEntry>();1107    for (const e of entries) byId.set(e.id, e);11081109    for (const entry of incoming) {1110      const normalized = sanitizeRestoredEntry(entry);1111      if (!normalized) {1112        skipped += 1;1113        continue;1114      }1115      const existing = byId.get(normalized.id);1116      if (!existing) {1117        entries.push(normalized);1118        added += 1;1119      } else if (normalized.modifiedAt > existing.modifiedAt) {1120        const idx = entries.findIndex((e) => e.id === existing.id);1121        if (idx !== -1) {1122          entries[idx] = normalized;1123          updated += 1;1124        }1125      } else {1126        skipped += 1;1127      }1128    }1129  }11301131  await persistEntries("import");1132  return { added, updated, skipped };1133}11341135/**1136 * Validate + normalise a VaultEntry coming from a backup. Accepts1137 * anything structurally sound; keeps all metadata verbatim. Returns1138 * null for values the live vault shouldn't accept (e.g., missing title).1139 */1140function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1141  if (!entry || typeof entry !== "object") return null;1142  const title = typeof entry.title === "string" ? entry.title.trim() : "";1143  if (!title) return null;1144  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1145  const now = Date.now();1146  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1147  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;11481149  // Migrate legacy passkey shape on import (same logic as on-disk read).1150  // Imports from pre-v2.11.0 vault backups still carry type:"passkey" with1151  // flat fields; bring them into the new shape immediately.1152  const migrated: VaultEntry = migrateLegacyPasskey(1153    {1154      ...entry,1155      id,1156      title,1157      type: entry.type ?? "login",1158      createdAt,1159      modifiedAt,1160    },1161    entry1162  );11631164  // Login entries from backup must carry a password (or be passkey-only,1165  // in which case the passkey sub-object replaces the password requirement).1166  if (1167    migrated.type === "login" &&1168    !migrated.passkey &&1169    typeof migrated.password !== "string"1170  ) {1171    return null;1172  }1173  if (migrated.passkey && !migrated.passkey.credentialId) {1174    return null;1175  }11761177  return migrated;1178}11791180// Soft-deletes every non-deleted entry in one call. The literal string1181// argument is a tripwire against accidental calls — any caller that would1182// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1183// to a boolean.1184export async function clearVaultEntries(1185  confirmation: "REPLACE_ALL"1186): Promise<void> {1187  if (confirmation !== "REPLACE_ALL") {1188    throw new Error("clearVaultEntries requires explicit confirmation");1189  }1190  if (!derivedKey) throw new Error("vault locked");1191  for (const entry of entries) {1192    if (!entry.deletedAt) {1193      entry.deletedAt = Date.now();1194      entry.modifiedAt = Date.now();1195    }1196  }1197  await persistEntries();1198}11991200export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1201  if (!derivedKey) throw new Error("vault locked");12021203  const sanitized = sanitizeEntryDraft(entry);1204  const now = Date.now();1205  const nextEntry: VaultEntry = {1206    ...sanitized,1207    id: crypto.randomUUID(),1208    createdAt: now,1209    modifiedAt: now,1210    // Stamp passwordChangedAt at creation so age tracking is accurate from1211    // day one. Non-login entries carry the same stamp harmlessly.1212    passwordChangedAt: sanitized.password ? now : undefined,1213  };12141215  entries.push(nextEntry);1216  await persistEntries();12171218  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1219  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1220    checkBreach(nextEntry.password).then(async (count) => {1221      if (count > 0) {1222        const idx = entries.findIndex((e) => e.id === nextEntry.id);1223        if (idx !== -1) {1224          entries[idx] = { ...entries[idx], breach: count, modifiedAt: Date.now() };1225          await persistEntries();1226        }1227      }1228    });1229  }12301231  return nextEntry;1232}12331234export async function updateEntry(1235  id: string,1236  patch: Partial<VaultEntry>1237): Promise<void> {1238  if (!derivedKey) throw new Error("vault locked");12391240  const idx = entries.findIndex((entry) => entry.id === id);1241  if (idx === -1 || entries[idx].deletedAt) {1242    throw new Error("entry not found");1243  }12441245  const current = entries[idx];1246  const sanitizedPatch = sanitizeEntryPatch(patch);1247  // Refresh passwordChangedAt only when the password value actually changes —1248  // a metadata-only update (title, tags, etc.) must not reset the age clock1249  // that drives the Pro health rotation recommendation.1250  const passwordChanged =1251    "password" in patch &&1252    typeof sanitizedPatch.password === "string" &&1253    sanitizedPatch.password !== current.password;1254  entries[idx] = {1255    ...current,1256    ...sanitizedPatch,1257    modifiedAt: Date.now(),1258    ...(passwordChanged ? { passwordChangedAt: Date.now() } : {}),1259  };12601261  // A login entry must carry SOMETHING fillable — either a password or a1262  // passkey credential. An empty-password login with no passkey is dead1263  // state. Permit the password-clear when a passkey is attached (that's1264  // the legitimate "drop the password, keep the passkey" path).1265  if (1266    entries[idx].type === "login" &&1267    !entries[idx].password &&1268    !entries[idx].passkey1269  ) {1270    entries[idx] = current;1271    throw new Error("password or passkey required");1272  }12731274  // Card entries must retain their card number1275  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1276    entries[idx] = current;1277    throw new Error("card number is required for card entries");1278  }12791280  await persistEntries();12811282  // Breach check when password changes: clear immediately so old breach doesn't linger,1283  // then async-check the new password and only re-set if it's also found.1284  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1285    entries[idx] = { ...entries[idx], breach: undefined };1286    await persistEntries();1287    const password = entries[idx].password;1288    if (password) {1289      checkBreach(password).then(async (count) => {1290        const i = entries.findIndex((e) => e.id === id);1291        if (i !== -1 && count > 0) {1292          entries[i] = { ...entries[i], breach: count, modifiedAt: Date.now() };1293          await persistEntries();1294        }1295      });1296    }1297  }1298}12991300export async function deleteEntry(id: string): Promise<void> {1301  if (!derivedKey) throw new Error("vault locked");13021303  const entry = entries.find((item) => item.id === id);1304  if (!entry || entry.deletedAt) {1305    throw new Error("entry not found");1306  }13071308  entry.deletedAt = Date.now();1309  entry.modifiedAt = Date.now();1310  await persistEntries();1311}13121313export function getDeletedEntries(): VaultEntry[] {1314  if (!derivedKey) return [];1315  return entries.filter((e) => !!e.deletedAt);1316}13171318export async function restoreEntry(id: string): Promise<void> {1319  if (!derivedKey) throw new Error("vault locked");1320  const entry = entries.find((e) => e.id === id && e.deletedAt);1321  if (!entry) throw new Error("deleted entry not found");1322  delete entry.deletedAt;1323  entry.modifiedAt = Date.now();1324  await persistEntries();1325  buildDomainIndexSync(entries);1326}13271328export async function purgeEntry(id: string): Promise<void> {1329  if (!derivedKey) throw new Error("vault locked");1330  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);1331  if (idx === -1) throw new Error("deleted entry not found");1332  entries.splice(idx, 1);1333  await persistEntries();1334}13351336/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */1337export async function purgeExpiredTrash(retentionMs: number): Promise<number> {1338  if (!derivedKey || retentionMs === 0) return 0;1339  const cutoff = Date.now() - retentionMs;1340  const before = entries.length;1341  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));1342  const purged = before - entries.length;1343  if (purged > 0) await persistEntries();1344  return purged;1345}13461347export function queryDomain(domain: string): VaultEntry[] {1348  return domainIndex.get(domain) ?? [];1349}13501351export function getEntryById(id: string): VaultEntry | undefined {1352  return entries.find((entry) => entry.id === id && !entry.deletedAt);1353}13541355async function persistSlotA(1356  reason: VaultSnapshot["reason"] = "local_save",1357): Promise<StoredVault> {1358  if (!derivedKey) throw new Error("vault locked");13591360  const slotA = await loadSlot(SLOT_A);1361  if (!slotA) throw new Error("missing slot A");13621363  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures1364  // are swallowed by takeVaultSnapshot — history must never block a save.1365  await takeVaultSnapshot(reason);13661367  const plaintext = encodePayload(entries, duressConfigured);1368  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);13691370  // Preserve existing recovery escrow fields.1371  const nextSlotA: StoredVault = {1372    ...slotA,1373    nonce,1374    ciphertext,1375    tag,1376    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot1377  };1378  await writeSlot(SLOT_A, nextSlotA);13791380  // When duress is not configured, keep slot B size-matched with slot A by1381  // regenerating its noise. When duress IS configured, leave slot B alone —1382  // we don't have the decoy key to re-encrypt it. This is a known trade-off.1383  if (!duressConfigured) {1384    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1385  }13861387  return nextSlotA;1388}13891390async function persistSlotB(): Promise<void> {1391  if (!derivedKey) throw new Error("vault locked");13921393  const slotB = await loadSlot(SLOT_B);1394  if (!slotB) throw new Error("missing slot B");13951396  // Decoy plaintext always reports duressConfigured=false — the decoy user1397  // sees a clean slate.1398  const plaintext = encodePayload(entries, false);1399  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);14001401  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });1402}14031404async function persistEntries(1405  reason: VaultSnapshot["reason"] = "local_save",1406): Promise<void> {1407  if (!derivedKey) throw new Error("vault locked");14081409  if (vaultMode === "decoy") {1410    await persistSlotB();1411  } else {1412    await persistSlotA(reason);1413  }1414  buildDomainIndexSync(entries);1415}14161417function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {1418  if (!Array.isArray(values)) return undefined;1419  const seen = new Set<string>();1420  const cleaned: string[] = [];1421  for (const value of values) {1422    const trimmed = typeof value === "string" ? value.trim() : "";1423    if (!trimmed) continue;1424    const key = trimmed.toLowerCase();1425    if (seen.has(key)) continue;1426    seen.add(key);1427    cleaned.push(trimmed);1428  }1429  return cleaned.length > 0 ? cleaned : undefined;1430}14311432function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {1433  const title = entry.title.trim();1434  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new1435  // model is "any entry, optionally with a passkey sub-object." Imports and1436  // any in-flight callers from before v2.11.0 keep working without throwing.1437  let type: VaultEntry["type"] = entry.type ?? "login";1438  if (type === "passkey") type = "login";14391440  if (!title) throw new Error("title is required");1441  // A login entry needs either a password or a passkey credential to be useful.1442  // Passkey-only entries (passkey present, no password) are valid.1443  if (type === "login" && !entry.password?.trim() && !entry.passkey) {1444    throw new Error("password or passkey required");1445  }1446  if (entry.passkey) {1447    const p = entry.passkey;1448    if (1449      !p.rpId ||1450      !p.credentialId ||1451      !p.publicKeyCose ||1452      !p.userHandle ||1453      !p.userName ||1454      (p.algorithm !== -7 && p.algorithm !== -8)1455    ) {1456      throw new Error("invalid passkey credential");1457    }1458  }1459  if (type === "card") {1460    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {1461      throw new Error("cardholder name and card number are required");1462    }1463  }14641465  return {1466    type,1467    title,1468    username: type === "login" ? entry.username?.trim() ?? "" : undefined,1469    password: type === "login" ? entry.password : undefined,1470    url: type === "login" ? entry.url?.trim() ?? "" : undefined,1471    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,1472    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,1473    notes: entry.notes?.trim() || undefined,1474    breach: type === "login" ? entry.breach : undefined,1475    canary: type === "login" ? entry.canary : undefined,1476    tags: entry.tags,1477    passkey: entry.passkey,1478    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,1479    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,1480    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,1481    expYear: type === "card" ? entry.expYear?.trim() : undefined,1482    cvc: type === "card" ? entry.cvc?.trim() : undefined,1483    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,1484    network: type === "card" ? entry.network : undefined,1485    last4: type === "card" ? entry.last4?.trim() : undefined,1486  };1487}14881489function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {1490  const nextPatch: Partial<VaultEntry> = {};14911492  if ("type" in patch) {1493    nextPatch.type = patch.type;1494  }14951496  if ("title" in patch) {1497    const title = patch.title?.trim() ?? "";1498    if (!title) throw new Error("title is required");1499    nextPatch.title = title;1500  }15011502  if ("url" in patch) {1503    nextPatch.url = patch.url?.trim() ?? "";1504  }15051506  if ("additionalUrls" in patch) {1507    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];1508    const seen = new Set<string>();1509    const cleaned: string[] = [];1510    for (const value of incoming) {1511      const trimmed = typeof value === "string" ? value.trim() : "";1512      if (!trimmed) continue;1513      const key = trimmed.toLowerCase();1514      if (seen.has(key)) continue;1515      seen.add(key);1516      cleaned.push(trimmed);1517    }1518    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;1519  }15201521  if ("username" in patch) {1522    nextPatch.username = patch.username?.trim() ?? "";1523  }15241525  if ("email" in patch) {1526    nextPatch.email = patch.email?.trim() || undefined;1527  }15281529  if ("password" in patch) {1530    const password = patch.password ?? "";1531    // Allow clearing a password (e.g. on passkey-only entries)1532    nextPatch.password = password || undefined;1533  }15341535  if ("totp" in patch) {1536    nextPatch.totp = patch.totp?.trim() || undefined;1537  }15381539  if ("notes" in patch) {1540    nextPatch.notes = patch.notes?.trim() || undefined;1541  }15421543  if ("breach" in patch) {1544    nextPatch.breach = patch.breach;1545  }15461547  if ("canary" in patch) {1548    nextPatch.canary = patch.canary;1549  }15501551  if ("passkey" in patch) {1552    // Passkey sub-object replaces all the legacy flat passkey fields. A1553    // patch with `passkey: undefined` removes the credential from the entry1554    // (used by "remove passkey" without deleting the whole entry).1555    nextPatch.passkey = patch.passkey;1556  }15571558  if ("tags" in patch) {1559    nextPatch.tags = patch.tags;1560  }15611562  if ("cardholderName" in patch) {1563    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;1564  }1565  if ("cardNumber" in patch) {1566    const n = patch.cardNumber ?? "";1567    nextPatch.cardNumber = n || undefined;1568  }1569  if ("expMonth" in patch) {1570    nextPatch.expMonth = patch.expMonth?.trim() || undefined;1571  }1572  if ("expYear" in patch) {1573    nextPatch.expYear = patch.expYear?.trim() || undefined;1574  }1575  if ("cvc" in patch) {1576    nextPatch.cvc = patch.cvc?.trim() || undefined;1577  }1578  if ("cardNickname" in patch) {1579    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;1580  }1581  if ("network" in patch) {1582    nextPatch.network = patch.network;1583  }1584  if ("last4" in patch) {1585    nextPatch.last4 = patch.last4?.trim() || undefined;1586  }15871588  return nextPatch;1589}15901591function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {1592  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;1593  const modifiedAt =1594    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;15951596  const base: VaultEntry = {1597    id: entry.id,1598    type: "type" in entry ? entry.type : "login",1599    title: entry.title,1600    username: entry.username,1601    password: entry.password,1602    url: entry.url,1603    totp: entry.totp,1604    notes: entry.notes,1605    breach: entry.breach,1606    canary: "canary" in entry ? entry.canary : undefined,1607    passkey: "passkey" in entry ? entry.passkey : undefined,1608    createdAt,1609    modifiedAt,1610    deletedAt: entry.deletedAt,1611    tags: "tags" in entry ? entry.tags : undefined,1612    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,1613    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,1614    expMonth: "expMonth" in entry ? entry.expMonth : undefined,1615    expYear: "expYear" in entry ? entry.expYear : undefined,1616    cvc: "cvc" in entry ? entry.cvc : undefined,1617    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,1618    network: "network" in entry ? entry.network : undefined,1619    last4: "last4" in entry ? entry.last4 : undefined,1620  };16211622  return migrateLegacyPasskey(base, entry);1623}16241625/**1626 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`1627 * with flat top-level fields (rpId, credentialId, etc.). This converts them1628 * to the new shape — `type:"login"` with a `passkey` sub-object — so all1629 * downstream consumers can rely on the new model. The result is rewritten1630 * to disk on next vault save (hasLegacyShape detects the un-migrated case).1631 */1632function migrateLegacyPasskey(1633  base: VaultEntry,1634  raw: VaultEntry | LegacyVaultEntry1635): VaultEntry {1636  if (base.passkey) return base;1637  const flat = raw as Partial<VaultEntry>;1638  const isPasskeyType = base.type === "passkey";1639  const hasFlatPasskeyFields =1640    typeof flat.rpId === "string" &&1641    typeof flat.credentialId === "string" &&1642    typeof flat.publicKeyCose === "string" &&1643    typeof flat.userHandle === "string" &&1644    typeof flat.userName === "string";1645  if (!isPasskeyType && !hasFlatPasskeyFields) return base;1646  if (!hasFlatPasskeyFields) return base;16471648  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;1649  base.passkey = {1650    rpId: flat.rpId!,1651    rpName: flat.rpName ?? flat.rpId!,1652    credentialId: flat.credentialId!,1653    publicKeyCose: flat.publicKeyCose!,1654    privateKeyJwk: flat.privateKeyJwk,1655    userHandle: flat.userHandle!,1656    userName: flat.userName!,1657    userDisplayName: flat.userDisplayName ?? flat.userName!,1658    algorithm,1659    signCount: flat.signCount ?? 0,1660    aaguid: flat.aaguid ?? "",1661    backupEligible: flat.backupEligible === true,1662    backupState: flat.backupState === true,1663    lastUsedAt: flat.lastUsedAt,1664    browser: flat.browser,1665    transports: flat.transports,1666    createdAt: base.createdAt,1667  };1668  // Promote to "login" so the unified Entry view renders. Username gets1669  // populated from the passkey's userName so the entry is searchable in the1670  // existing autofill flow without a separate code path.1671  if (isPasskeyType) base.type = "login";1672  if (!base.username) base.username = flat.userName;1673  if (!base.url) base.url = `https://${flat.rpId}`;1674  return base;1675}167616771678// ---------------------------------------------------------------------------1679// Deduplication1680// ---------------------------------------------------------------------------16811682function entryDupeKey(e: VaultEntry): string {1683  if (e.type === "note") return `note|${e.title.toLowerCase()}`;1684  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins1685  // on different sites that happen to share a username are still distinct;1686  // two passkey credentials for the same RP+user are duplicates regardless1687  // of any login fields they may also carry.1688  if (e.passkey) {1689    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;1690  }1691  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;1692}16931694/** Returns the number of duplicate entries (those that would be removed). */1695export function countDuplicates(): number {1696  const best = new Map<string, VaultEntry>();1697  let dupeCount = 0;1698  for (const e of entries.filter((e) => !e.deletedAt)) {1699    const key = entryDupeKey(e);1700    const existing = best.get(key);1701    if (!existing) {1702      best.set(key, e);1703    } else {1704      dupeCount++;1705      // Keep the more recently modified one in the map1706      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1707        best.set(key, e);1708      }1709    }1710  }1711  return dupeCount;1712}17131714/**1715 * Soft-deletes duplicate entries, keeping the most recently modified copy of1716 * each group. Returns the number of entries moved to trash.1717 */1718export async function deduplicateEntries(): Promise<number> {1719  if (!derivedKey) throw new Error("vault locked");1720  // Dedup reshapes slot B permanently under the decoy key, which is1721  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.1722  if (vaultMode === "decoy") {1723    throw new Error("deduplicate disabled in decoy mode");1724  }17251726  const best = new Map<string, VaultEntry>();1727  for (const e of entries.filter((en) => !en.deletedAt)) {1728    const key = entryDupeKey(e);1729    const existing = best.get(key);1730    if (!existing) {1731      best.set(key, e);1732    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1733      best.set(key, e);1734    }1735  }17361737  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));1738  const now = Date.now();1739  let removed = 0;1740  entries = entries.map((e) => {1741    if (!e.deletedAt && !keepIds.has(e.id)) {1742      removed++;1743      return { ...e, deletedAt: now };1744    }1745    return e;1746  });17471748  if (removed > 0) await persistEntries();1749  return removed;1750}17511752// ---------------------------------------------------------------------------1753// Vault history — snapshot BEFORE every vault write so the saved blob is1754// what is about to be lost, not what was just written. Capped at 10. The1755// stored blobs are AES-256-GCM ciphertext under the master password, so1756// history adds zero security surface.1757// ---------------------------------------------------------------------------17581759const HISTORY_KEY = "peach_vault_history";1760const HISTORY_MAX = 10;17611762/**1763 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE1764 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost1765 * state. Identical-to-most-recent blobs are skipped to avoid filling history1766 * with no-op writes.1767 */1768export async function takeVaultSnapshot(1769  reason: VaultSnapshot["reason"],1770): Promise<void> {1771  try {1772    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);1773    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];1774    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];17751776    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,1777    // pre-createVault). createVault skips snapshots intentionally — there is1778    // no prior state to preserve.1779    if (!slotA) return;17801781    const vault_a = JSON.stringify(slotA);1782    const vault_b = JSON.stringify(slotB ?? slotA);17831784    // Best-effort entry count: only meaningful when the in-memory state is1785    // unlocked AND in real mode. Decoy mode would leak the decoy entry1786    // count; locked mode has no entries to count. Both fall through to null.1787    let entryCount: number | null = null;1788    if (derivedKey && vaultMode === "real") {1789      try {1790        entryCount = entries.filter((e) => !e.deletedAt).length;1791      } catch {1792        entryCount = null;1793      }1794    }17951796    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];1797    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)1798      ? existingRaw1799      : { snapshots: [] };18001801    // Deduplication: if the most recent snapshot has byte-identical blobs,1802    // skip — saves the same vault N times in a row would otherwise burn the1803    // entire history window with no recovery value.1804    const latest = history.snapshots[0];1805    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {1806      return;1807    }18081809    const snapshot: VaultSnapshot = {1810      id: crypto.randomUUID(),1811      savedAt: Date.now(),1812      reason,1813      vault_a,1814      vault_b,1815      entryCount,1816    };18171818    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);1819    await chrome.storage.local.set({ [HISTORY_KEY]: history });1820  } catch {1821    // Non-fatal — history must never block a save. A failed snapshot just1822    // means the user loses one rung of the safety ladder for this write.1823  }1824}18251826/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */1827export async function clearVaultHistory(): Promise<void> {1828  try {1829    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });1830  } catch {1831    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().1832  }1833}18341835/** Read all snapshots, newest first, for the Settings vault history view. */1836export async function getVaultHistory(): Promise<VaultSnapshot[]> {1837  const raw = await chrome.storage.local.get(HISTORY_KEY);1838  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];1839  if (!history || !Array.isArray(history.snapshots)) return [];1840  return history.snapshots;1841}18421843/**1844 * Replace the on-disk vault with the chosen snapshot's blobs and lock the1845 * vault. The current state is itself snapshotted with reason 'restore' first1846 * so the restore is reversible. After this returns, the user must unlock1847 * with the password that matches the restored blob.1848 */1849export async function restoreVaultSnapshot(1850  snapshotId: string,1851): Promise<{ ok: true } | { ok: false; error: string }> {1852  const history = await getVaultHistory();1853  const snapshot = history.find((s) => s.id === snapshotId);1854  if (!snapshot) return { ok: false, error: "snapshot_not_found" };18551856  // Snapshot the about-to-be-replaced current state so the restore is1857  // reversible. takeVaultSnapshot is no-op when the most recent entry is1858  // byte-identical, so a restore-of-a-restore stays clean.1859  await takeVaultSnapshot("restore");18601861  let restoredA: StoredVault;1862  let restoredB: StoredVault;1863  try {1864    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;1865    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;1866  } catch {1867    return { ok: false, error: "snapshot_corrupt" };1868  }18691870  await writeSlot(SLOT_A, restoredA);1871  await writeSlot(SLOT_B, restoredB);18721873  // Lock so the user must prove they can decrypt the restored blob. If they1874  // restored a pre-password-change snapshot they discover it immediately at1875  // the lock screen rather than silently holding an inaccessible vault.1876  lock();18771878  return { ok: true };1879}18801881/**1882 * Change the master password. Re-encrypts slot A with a fresh salt + new1883 * derived key. Slot B (duress or noise) is left untouched when duress is1884 * configured — it uses an independent key derived from the duress password —1885 * and regenerated as noise when duress is not configured.1886 */1887export async function changePassword(1888  currentPassword: string,1889  newPassword: string1890): Promise<boolean> {1891  if (!derivedKey || vaultMode !== "real") return false;18921893  // Verify current password before allowing any change1894  const valid = await verifyMainPassword(currentPassword);1895  if (!valid) return false;18961897  const slotA = await loadSlot(SLOT_A);1898  if (!slotA) return false;18991900  // Derive fresh key from new password + new random salt1901  const newSalt = generateSalt();1902  const newKey = await deriveKey(newPassword, newSalt);19031904  // Re-encrypt the in-memory entries with the new key1905  const plaintext = encodePayload(entries, duressConfigured);1906  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);19071908  const freshSlotA: StoredVault = {1909    ...slotA,1910    salt: hex(newSalt),1911    nonce,1912    ciphertext,1913    tag,1914  };1915  // Snapshot the OLD-key blob before overwriting it. If the user later1916  // restores from this snapshot they will need the OLD password — the1917  // post-restore lock surfaces that immediately.1918  await takeVaultSnapshot("local_save");1919  await writeSlot(SLOT_A, freshSlotA);19201921  // Swap the in-memory key synchronously before any further await. If we1922  // threw between the Slot A write and this swap, the next persistEntries()1923  // would overwrite Slot A with old-key ciphertext, locking the user out.1924  zeroKey(derivedKey);1925  derivedKey = newKey;19261927  // Rotate the root key so the old recovery words are immediately invalidated.1928  // After this point getRecoveryWords() returns a fresh 12-word set.1929  const {1930    rawRootKey: freshRawKey,1931    recoverySalt: freshSalt,1932    wrappedRootKey: freshWrapped,1933    wrappedNonce: freshNonce,1934    wrappedTag: freshTag,1935  } = await generateAndWrapRootKey();1936  rootKey = unhex(freshRawKey);1937  const slotAfterRekey = await loadSlot(SLOT_A);1938  if (slotAfterRekey) {1939    await writeSlot(SLOT_A, {1940      ...slotAfterRekey,1941      recovery_salt: freshSalt,1942      recovery_wrapped_root_key: freshWrapped,1943      recovery_nonce: freshNonce,1944      recovery_tag: freshTag,1945    });1946  }19471948  // Regenerate noise for slot B when duress is not in use. Runs after the1949  // key swap so a failure here is a disposable noise regeneration, not a1950  // lockout.1951  if (!duressConfigured) {1952    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1953  }19541955  return true;1956}