vault.ts

84.8 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// Set to a corruption reason during the last unlock() attempt when both slots70// decrypted but failed schema validation. Consumed and cleared by71// consumeLastUnlockFailureReason() so messages.ts can surface vault_corrupted72// distinctly from wrong-password (M7/COR-007).73//74// NOTE: module-level state — if vault.ts is ever moved to a worker context,75// this must be replaced with a structured return value from unlock() or a76// postMessage channel. The signal is invisible across module-instance77// boundaries.78let lastUnlockFailureReason: string | null = null;7980export function consumeLastUnlockFailureReason(): string | null {81  const r = lastUnlockFailureReason;82  lastUnlockFailureReason = null;83  return r;84}8586// ---------------------------------------------------------------------------87// Settings (persisted inside the encrypted vault store as a sidecar key)88// ---------------------------------------------------------------------------8990export type DisplayFont = "cormorant-italic" | "cormorant" | "inter" | "system";91export type TextSize = "small" | "medium" | "large";9293export interface VaultSettings {94  accentColor?: AccentColorName;95  theme?: "dark" | "light";96  autoLockMinutes?: number;97  clipboardClearSeconds?: number;98  lockOnClose?: boolean;99  breachMonitoring?: boolean;100  passkeyProviderEnabled?: boolean;101  trashRetentionDays?: number;102  manualSummonDomains?: Record<string, ManualSummonRecord>;103  declinedCardFingerprints?: string[];104  /**105   * Display font for headlines, brand wordmark, and narrative copy. Default106   * is "cormorant-italic" — the bold italic serif that ships as Peach's107   * voice. Non-italic options exist for users who prefer upright type.108   */109  displayFont?: DisplayFont;110  /**111   * Text size scale for body copy and entry titles. Stops at "large" by112   * design — XL clips dense screens (Vault, Health) inside the fixed113   * 330×480 popup surface.114   */115  textSize?: TextSize;116  /**117   * When true, all entrance/exit transitions and ambient animations are118   * collapsed to instant. Honoured alongside the user's OS-level119   * `prefers-reduced-motion`.120   */121  reduceMotion?: boolean;122  /**123   * Android-only: when true, the master key is escrowed in AndroidKeyStore124   * and the lock screen offers biometric unlock. Toggled from Settings →125   * Security → Biometrics. Has no effect on desktop builds.126   */127  biometricsEnabled?: boolean;128  /**129   * User overrides for keyboard shortcuts. Keys are ShortcutAction names130   * (see src/shared/shortcuts.ts). Missing actions fall through to131   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the132   * canonical lookup. Storing only the deltas keeps the vault payload133   * small and lets default updates land for users who never rebind.134   */135  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;136}137138let settings: VaultSettings = {};139140export function getSettings(): VaultSettings {141  return settings;142}143144export async function ensureSettingsLoaded(): Promise<VaultSettings> {145  await loadSettings();146  return settings;147}148149export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {150  await loadSettings();151  settings = { ...settings, ...patch };152  await persistSettings();153}154155async function persistSettings(): Promise<void> {156  try {157    await chrome.storage.local.set({ peach_settings: settings });158  } catch (err) {159    throw mapStorageError(err);160  }161}162163function mapStorageError(err: unknown): Error {164  const message = err instanceof Error ? err.message : String(err);165  if (/QUOTA_BYTES|quota/i.test(message)) {166    return new Error("STORAGE_QUOTA_EXCEEDED");167  }168  return err instanceof Error ? err : new Error(message);169}170171async function loadSettings(): Promise<void> {172  const result = await chrome.storage.local.get("peach_settings");173  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};174}175176// ---------------------------------------------------------------------------177// HIBP breach check (k-anonymity model)178// ---------------------------------------------------------------------------179180export async function checkBreach(password: string): Promise<number> {181  // HIBP k-anonymity API is keyed on SHA-1 prefixes — this is dictated by182  // the public API, not a security choice. Do not migrate to SHA-256 here.183  const encoder = new TextEncoder();184  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));185  const hashHex = Array.from(new Uint8Array(hashBuffer))186    .map((b) => b.toString(16).padStart(2, "0"))187    .join("")188    .toUpperCase();189190  const prefix = hashHex.slice(0, 5);191  const suffix = hashHex.slice(5);192193  try {194    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);195    if (!res.ok) return 0;196    const text = await res.text();197    const match = text.split("\r\n").find((l) => l.startsWith(suffix));198    return match ? parseInt(match.split(":")[1], 10) : 0;199  } catch {200    return 0; // Network unavailable — don't block201  }202}203204export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {205  const fingerprint = await cardFingerprint(last4, expMonth, expYear);206  await loadSettings();207  settings = {208    ...settings,209    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],210  };211  await persistSettings();212}213214export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {215  await loadSettings();216  const fingerprint = await cardFingerprint(last4, expMonth, expYear);217  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);218}219220async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {221  const encoder = new TextEncoder();222  const buf = await crypto.subtle.digest("SHA-256", encoder.encode(`${last4}|${expMonth}|${expYear}`));223  return Array.from(new Uint8Array(buf).slice(0, 8))224    .map((b) => b.toString(16).padStart(2, "0"))225    .join("");226}227228function hex(buf: Uint8Array): string {229  return Array.from(buf)230    .map((b) => b.toString(16).padStart(2, "0"))231    .join("");232}233234function unhex(s: string): Uint8Array {235  const out = new Uint8Array(s.length / 2);236  for (let i = 0; i < s.length; i += 2) {237    out[i / 2] = parseInt(s.slice(i, i + 2), 16);238  }239  return out;240}241242// ---------------------------------------------------------------------------243// Root key generation and recovery word wrapping244// ---------------------------------------------------------------------------245246/**247 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key248 * derived from the recovery word material via HKDF-SHA256. The wrapped blob249 * fields are stored in slot A alongside the password-wrapped ciphertext. The250 * root key itself is kept in memory for the session so getRecoveryWords() can251 * encode it without re-deriving.252 */253async function generateAndWrapRootKey(): Promise<{254  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey255  recoverySalt: string;256  wrappedRootKey: string;257  wrappedNonce: string;258  wrappedTag: string;259}> {260  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));261262  // Derive the recovery key from the BIP39 words encoding of the root key.263  const words = await rootKeyToWords(rootKeyBytes);264  const wordMaterial = await wordsToRootKeyMaterial(words);265  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));266  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);267268  // Wrap the root key with the recovery key using AES-256-GCM directly.269  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.270  const nonce = crypto.getRandomValues(new Uint8Array(12));271  const encoded = new TextEncoder().encode(hex(rootKeyBytes));272  const raw = await crypto.subtle.encrypt(273    {274      name: "AES-GCM",275      iv: nonce,276      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,277      tagLength: 128,278    },279    recoveryKey,280    encoded281  );282  // WebCrypto returns ciphertext || auth tag concatenated; split them.283  const rawU8 = new Uint8Array(raw);284  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));285  const tag = hex(rawU8.slice(rawU8.length - 16));286287  return {288    rawRootKey: hex(rootKeyBytes),289    recoverySalt: hex(recoverySalt),290    wrappedRootKey: ciphertext,291    wrappedNonce: hex(nonce),292    wrappedTag: tag,293  };294}295296/**297 * Unwrap the root key using recovery words. Called during recovery flow when298 * the user provides their 12-word phrase and a new password.299 * Exported for use by the RECOVER_WITH_WORDS message handler.300 */301export async function unwrapRootKeyForRecovery(302  wrappedRootKey: string,303  wrappedNonce: string,304  wrappedTag: string,305  recoverySaltHex: string,306  words: string[]307): Promise<void> {308  const wordMaterial = await wordsToRootKeyMaterial(words);309  const recoverySalt = unhex(recoverySaltHex);310  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);311312  // Decrypt and cache the root key. The root key bytes were hex-encoded before313  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt314  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.315  // Each component is stored separately in the vault slot; decode them individually316  // so the nonce is always exactly the 12 bytes from wrappedNonce.317  const nonceRaw = unhex(wrappedNonce);318  const ciphertextRaw = unhex(wrappedRootKey);319  const tagRaw = unhex(wrappedTag);320  // Combine ciphertext and tag for AES-GCM (expects ciphertext||tag).321  // Use explicit ArrayBuffer allocations to satisfy TS BufferSource constraints.322  const nonceU8 = nonceRaw as unknown as Uint8Array<ArrayBuffer>;323  const combined = new Uint8Array(ciphertextRaw.length + tagRaw.length);324  combined.set(ciphertextRaw, 0);325  combined.set(tagRaw, ciphertextRaw.length);326  const raw = await crypto.subtle.decrypt(327    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },328    recoveryKey,329    combined330  );331  const decryptedHex = new TextDecoder().decode(raw);332  rootKey = unhex(decryptedHex);333}334335/**336 * Re-wrap the cached root key under a new derived key (from the user's new337 * password). Called after password change and after recovery re-key so the338 * same recovery words continue to work with the updated password — and so339 * subsequent unlocks can rehydrate rootKey via deriveRootKeyFromDerivedKey.340 *341 * recovery_* fields are intentionally not touched: those wrap rootKey under342 * the recovery words, which do not change when the password changes.343 */344export async function rewrapRootKeyForPassword(345  newDerivedKey: Uint8Array,346  _newSalt: Uint8Array347): Promise<void> {348  if (!rootKey) throw new Error("root key not set");349  const slotA = await loadSlot(SLOT_A);350  if (!slotA) throw new Error("missing slot A");351  const pwRoot = await wrapRootKeyForDerivedKey(newDerivedKey, rootKey);352  await writeSlot(SLOT_A, { ...slotA, ...pwRoot });353}354355/**356 * Change the vault encryption to a new derived key (already derived from357 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.358 * Caller is responsible for key swap and snapshot.359 */360async function reencryptVaultForNewKey(361  newKey: Uint8Array,362  slotA: StoredVault363): Promise<void> {364  const plaintext = encodePayload(entries, duressConfigured);365  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);366  await writeSlot(SLOT_A, {367    ...slotA,368    nonce,369    ciphertext,370    tag,371  });372}373374/**375 * Full password change with an already-derived key (used after recovery).376 * Unlike changePassword(), this skips re-verification of current password377 * since recovery flow has already validated the user's identity via words.378 */379export async function changePasswordWithNewKey(380  newSalt: Uint8Array,381  newKey: Uint8Array382): Promise<void> {383  const slotA = await loadSlot(SLOT_A);384  if (!slotA) throw new Error("missing slot A");385  // reencryptVaultForNewKey must receive the updated salt so that subsequent386  // UNLOCK calls with the new password can re-derive the correct key.387  await reencryptVaultForNewKey(newKey, { ...slotA, salt: hex(newSalt) });388  if (derivedKey) zeroKey(derivedKey);389  derivedKey = newKey;390  // Refresh pw_root_* under the new key so deriveRootKeyFromDerivedKey works391  // on the next unlock (H2/COR-006). If rootKey is missing — recovery flow392  // should have populated it — fall back silently rather than blowing up the393  // recovery path; migrateToRecovery will rehydrate on the next unlock.394  if (rootKey) {395    const slotAfter = await loadSlot(SLOT_A);396    if (slotAfter) {397      const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);398      await writeSlot(SLOT_A, { ...slotAfter, ...pwRoot });399    }400  }401  if (!duressConfigured) {402    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));403  }404  await stampLocalVaultMutation();405}406407// ---------------------------------------------------------------------------408// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive409// a new key after password reset without re-importing the crypto module.410// ---------------------------------------------------------------------------411412export { deriveKey, generateSalt };413414export async function loadSlotA(): Promise<StoredVault | null> {415  return loadSlot(SLOT_A);416}417418/**419 * Recover the session root key by AES-GCM-decrypting the pw_root_* fields420 * (a second wrapping of the root key under the password-derived key). This421 * is what makes GET_RECOVERY_WORDS work across lock/unlock cycles without422 * the user re-entering their recovery phrase: the same root key bytes —423 * and therefore the same 12 words — survive every unlock.424 *425 * Returns null when the pw_root fields are absent (pre-v2.12 vault that426 * hasn't been migrated yet) or when decryption fails (wrong derived key,427 * tampered ciphertext). On null the caller should fall through to428 * migrateToRecovery() to populate the fields.429 */430async function deriveRootKeyFromDerivedKey(431  derivedKeyBytes: Uint8Array,432  slotA: StoredVault,433): Promise<Uint8Array | null> {434  if (!slotA.pw_root_ciphertext || !slotA.pw_root_nonce || !slotA.pw_root_tag) {435    return null;436  }437  try {438    const rootHex = await decrypt(439      derivedKeyBytes,440      slotA.pw_root_nonce,441      slotA.pw_root_ciphertext,442      slotA.pw_root_tag,443    );444    return unhex(rootHex);445  } catch {446    return null;447  }448}449450// AES-GCM-wrap rootKey under derivedKey for the pw_root_* fields. Returns the451// hex-encoded ciphertext/nonce/tag triple ready to embed in StoredVault.452async function wrapRootKeyForDerivedKey(453  derivedKeyBytes: Uint8Array,454  rootKeyBytes: Uint8Array,455): Promise<{ pw_root_ciphertext: string; pw_root_nonce: string; pw_root_tag: string }> {456  const { nonce, ciphertext, tag } = await encrypt(derivedKeyBytes, hex(rootKeyBytes));457  return {458    pw_root_ciphertext: ciphertext,459    pw_root_nonce: nonce,460    pw_root_tag: tag,461  };462}463464/**465 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),466 * generate the root key, wrap it under the recovery words, and persist the467 * recovery escrow into slot A. The caller must show the recovery words to the468 * user before calling this — once called, those words are the vault's recovery469 * path. This runs once, on the first unlock of an upgraded vault.470 */471export async function migrateToRecovery(): Promise<void> {472  if (!derivedKey || vaultMode !== "real") {473    throw new Error("vault must be unlocked in real mode");474  }475  const slotA = await loadSlot(SLOT_A);476  if (!slotA) throw new Error("missing slot A");477  const hasRecoveryEscrow =478    !!slotA.recovery_salt && !!slotA.recovery_wrapped_root_key;479  const hasPwRoot = !!slotA.pw_root_ciphertext && !!slotA.pw_root_nonce;480  if (hasRecoveryEscrow && hasPwRoot) return; // already done481482  // If recovery escrow already exists but pw_root doesn't, we cannot recover483  // the original rootKey without the user's words. The least-surprising path484  // is to rotate to a fresh root key — the user's old recovery words become485  // invalid but we surface peach_recovery_pending so the popup re-presents486  // the words. The unlock-path callers all set that flag.487  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =488    await generateAndWrapRootKey();489  rootKey = unhex(rawRootKey);490491  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);492  const nextSlotA: StoredVault = {493    ...slotA,494    recovery_salt: recoverySalt,495    recovery_wrapped_root_key: wrappedRootKey,496    recovery_nonce: wrappedNonce,497    recovery_tag: wrappedTag,498    ...pwRoot,499  };500  await writeSlot(SLOT_A, nextSlotA);501  await stampLocalVaultMutation();502}503504/**505 * Returns the 12 recovery words for the current session's root key.506 * Must be called while the vault is unlocked. Throws if recovery has not507 * been set up (pre-v2.12 vault that hasn't migrated yet).508 */509export async function getRecoveryWords(): Promise<string[]> {510  if (!rootKey) {511    throw new Error("recovery not set up — vault may need migration");512  }513  return rootKeyToWords(rootKey);514}515516async function loadSlot(key: string): Promise<StoredVault | null> {517  const result = await chrome.storage.local.get(key);518  return (result as Record<string, StoredVault | undefined>)[key] ?? null;519}520521async function writeSlot(key: string, vault: StoredVault): Promise<void> {522  try {523    await chrome.storage.local.set({ [key]: vault });524  } catch (err) {525    throw mapStorageError(err);526  }527}528529async function stampLocalVaultMutation(): Promise<void> {530  await chrome.storage.local.set({531    peach_last_modified_at: String(Date.now()),532  });533}534535/**536 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault537 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +538 * peach_vault_b). Runs at most once per session; safe to call repeatedly.539 */540async function ensureMigrated(): Promise<void> {541  if (migrationRan) return;542  migrationRan = true;543544  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];545  const raw = await chrome.storage.local.get(keys);546  const slotAExists = raw[SLOT_A] !== undefined;547  const slotBExists = raw[SLOT_B] !== undefined;548  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;549  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;550551  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.552  if (!slotAExists && !legacyMain) return;553554  // Already migrated.555  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;556557  let didMigrate = false;558  if (legacyMain && !slotAExists) {559    await writeSlot(SLOT_A, legacyMain);560    didMigrate = true;561  }562563  // If slot B isn't set, populate it: use legacy decoy when present, else noise.564  if (!slotBExists) {565    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;566    if (legacyDecoy) {567      await writeSlot(SLOT_B, legacyDecoy);568      didMigrate = true;569    } else if (target) {570      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));571      didMigrate = true;572    }573  }574575  const toRemove: string[] = [];576  if (legacyMain) toRemove.push(LEGACY_MAIN);577  if (legacyDecoy) toRemove.push(LEGACY_DECOY);578  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);579  if (toRemove.length > 0) {580    await chrome.storage.local.remove(toRemove);581    didMigrate = true;582  }583  if (didMigrate) {584    await stampLocalVaultMutation();585  }586}587588// ---------------------------------------------------------------------------589// Padding + noise helpers590// ---------------------------------------------------------------------------591592/**593 * Pads the plaintext string with trailing spaces so that the encoded byte594 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing595 * whitespace after a complete document, so the padding is transparent on596 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.597 */598function padPlaintext(json: string): string {599  const encoded = new TextEncoder().encode(json);600  const target = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);601  const padLen = target - encoded.length;602  return json + " ".repeat(padLen);603}604605/**606 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The607 * ciphertext length matches the given byte count. Because GCM ciphertext is608 * pseudo-random, this blob is indistinguishable at rest from a real609 * encrypted vault without the key.610 *611 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto612 * spec hard limit). Chunk the fill so vaults larger than that — common once613 * the user has dozens of entries or any passkey — don't throw.614 */615const CSPRNG_CHUNK = 65_536;616617function fillRandom(buf: Uint8Array): Uint8Array {618  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {619    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);620    const chunk = new Uint8Array(end - offset);621    crypto.getRandomValues(chunk);622    buf.set(chunk, offset);623  }624  return buf;625}626627function generateNoiseBlob(ciphertextBytes: number): StoredVault {628  const salt = crypto.getRandomValues(new Uint8Array(16));629  const nonce = crypto.getRandomValues(new Uint8Array(12));630  const cipher = fillRandom(new Uint8Array(ciphertextBytes));631  const tag = crypto.getRandomValues(new Uint8Array(16));632  return {633    version: 1,634    salt: hex(salt),635    nonce: hex(nonce),636    ciphertext: hex(cipher),637    tag: hex(tag),638  };639}640641// ---------------------------------------------------------------------------642// Encrypted plaintext envelope643// ---------------------------------------------------------------------------644645type VaultPayload = {646  entries: VaultEntry[];647  duressConfigured?: boolean;648};649650type DecryptedPayload = {651  entries: VaultEntry[];652  duressConfigured: boolean;653  /**654   * True when at least one entry in the on-disk payload had a legacy shape655   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)656   * and got rewritten by normalizeEntry. The unlock path uses this to657   * persist the new shape immediately — without it, the migration would658   * re-run on every unlock and old shape never disappears from disk.659   */660  migratedFromLegacy: boolean;661};662663function encodePayload(entries: VaultEntry[], duress: boolean): string {664  const envelope: VaultPayload = { entries, duressConfigured: duress };665  return padPlaintext(JSON.stringify(envelope));666}667668function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {669  if ("created" in raw || "updated" in raw) return true;670  const flat = raw as Partial<VaultEntry>;671  if (flat.type === "passkey") return true;672  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.673  if (flat.rpId !== undefined && flat.passkey === undefined) return true;674  return false;675}676677function decodePayload(plain: string): DecryptedPayload {678  const parsed = JSON.parse(plain) as679    | Array<VaultEntry | LegacyVaultEntry>680    | VaultPayload;681682  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];683  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);684685  const normalized = rawEntries.map(normalizeEntry);686  const { merged } = mergePasskeyOnlyIntoLogins(normalized);687688  if (Array.isArray(parsed)) {689    return {690      entries: normalized,691      duressConfigured: false,692      migratedFromLegacy: true, // pre-A-2 array shape always needs save693    };694  }695696  return {697    entries: normalized,698    duressConfigured: parsed.duressConfigured === true,699    migratedFromLegacy: flatMigrationNeeded || merged,700  };701}702703/**704 * One-time consolidation pass run alongside the legacy passkey migration.705 *706 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`707 * entry per registration — so users who tested registration multiple times708 * accumulated several passkey-bearing entries for the same RP, and the709 * migration to the unified shape carried that duplication forward. This710 * pass collapses the duplicates:711 *712 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.713 *   2. Within each group, the most recently-active credential wins (prefer714 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).715 *   3. If a NON-passkey login entry exists for that RP (matched by url or716 *      additionalUrls hostname), attach the winning passkey to it and717 *      soft-delete every passkey-bearing entry in the group.718 *   4. Otherwise, soft-delete every loser in the group, keeping the winner719 *      as a passkey-only entry.720 *721 * Soft delete (deletedAt) rather than splice — the trash flow keeps the722 * record around for retention and the user can restore if the heuristic723 * picked wrong. The `merged` flag goes back to decodePayload so the724 * unlock path persists the consolidated shape immediately.725 */726function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {727  let merged = false;728  const now = Date.now();729730  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);731  const byRpId = new Map<string, VaultEntry[]>();732  for (const e of candidates) {733    const rpId = e.passkey!.rpId;734    const list = byRpId.get(rpId);735    if (list) list.push(e);736    else byRpId.set(rpId, [e]);737  }738739  for (const [rpId, group] of byRpId) {740    if (group.length === 0) continue;741    group.sort((a, b) => {742      const aT =743        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;744      const bT =745        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;746      return bT - aT;747    });748    const winner = group[0]!;749750    // Look for a NON-passkey login entry matching this RP. Use the URL751    // hostname rather than a substring search so a "login.foo.com" entry752    // doesn't accidentally absorb a passkey for "foo.com".753    const loginTarget = entries.find((e) => {754      if (e.deletedAt || e.passkey || e.type !== "login") return false;755      if (e.url && extractDomain(e.url) === rpId) return true;756      for (const extra of e.additionalUrls ?? []) {757        if (extractDomain(extra) === rpId) return true;758      }759      return false;760    });761762    if (loginTarget) {763      loginTarget.passkey = winner.passkey;764      loginTarget.modifiedAt = now;765      for (const dup of group) {766        if (!dup.deletedAt) {767          dup.deletedAt = now;768          dup.modifiedAt = now;769        }770      }771      merged = true;772    } else if (group.length > 1) {773      for (let i = 1; i < group.length; i += 1) {774        const dup = group[i]!;775        if (!dup.deletedAt) {776          dup.deletedAt = now;777          dup.modifiedAt = now;778        }779      }780      merged = true;781    }782  }783784  return { merged };785}786787// Validates that a `StoredVault` record on disk has the field shape we expect788// before treating an AES-GCM decrypt success as a clean unlock. AES-GCM only789// authenticates the static VERSION_BYTE AAD, not the `version` field on the790// outer record — a corrupted `version: 42` slot still decrypts cleanly. This791// check surfaces that as `corrupted` (M7/COR-007) so the popup can offer a792// history-restore path instead of falsely reporting "wrong password".793function hasValidVaultSchema(v: unknown): v is StoredVault {794  if (typeof v !== "object" || v === null) return false;795  const r = v as Record<string, unknown>;796  return (797    r.version === 1 &&798    typeof r.salt === "string" &&799    typeof r.nonce === "string" &&800    typeof r.ciphertext === "string" &&801    typeof r.tag === "string"802  );803}804805export type TryDecryptResult =806  | { status: "ok"; payload: DecryptedPayload }807  | { status: "wrong_key" }808  | { status: "corrupted"; reason: string };809810// Exported solely for `unlockFromCodex` below, which needs to verify a811// supplied passphrase against a slotA blob *without* committing anything to812// storage. Keep this internal otherwise — callers crossing the messages.ts813// boundary should always go through `unlock` / `verifyMainPassword` so the814// constant-time invariants in Projects.md §3.4 are not bypassed.815export async function tryDecryptSlot(816  vault: StoredVault,817  key: Uint8Array818): Promise<DecryptedPayload | null> {819  const res = await tryDecryptSlotResult(vault, key);820  return res.status === "ok" ? res.payload : null;821}822823export async function tryDecryptSlotResult(824  vault: StoredVault,825  key: Uint8Array826): Promise<TryDecryptResult> {827  if (!hasValidVaultSchema(vault)) {828    return { status: "corrupted", reason: "invalid_slot_schema" };829  }830  let plain: string;831  try {832    plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag);833  } catch {834    return { status: "wrong_key" };835  }836  try {837    const parsed = JSON.parse(plain) as unknown;838    if (typeof parsed !== "object" || parsed === null) {839      return { status: "corrupted", reason: "invalid_payload_shape" };840    }841    if (!Array.isArray(parsed) && !Array.isArray((parsed as VaultPayload).entries)) {842      return { status: "corrupted", reason: "invalid_payload_shape" };843    }844    return { status: "ok", payload: decodePayload(plain) };845  } catch {846    return { status: "corrupted", reason: "invalid_payload_json" };847  }848}849850// ---------------------------------------------------------------------------851// Core operations852// ---------------------------------------------------------------------------853854function buildDomainIndexSync(all: VaultEntry[]): void {855  domainIndex = new Map();856857  for (const entry of all) {858    if (entry.deletedAt) continue;859    if (entry.type === "note" || entry.type === "card") continue;860861    // An entry can match multiple sites via `additionalUrls`. Index each862    // hostname so `queryDomain` returns the entry regardless of which alias863    // the user is currently visiting. Dedupe per-entry so a single entry864    // never appears twice in one bucket.865    const domains = new Set<string>();866    const primary = extractDomain(entry.url ?? "");867    if (primary) domains.add(primary);868    for (const extra of entry.additionalUrls ?? []) {869      const d = extractDomain(extra);870      if (d) domains.add(d);871    }872    if (domains.size === 0) continue;873874    for (const domain of domains) {875      const bucket = domainIndex.get(domain) ?? [];876      bucket.push(entry);877      domainIndex.set(domain, bucket);878    }879  }880}881882export async function createVault(password: string): Promise<void> {883  await ensureMigrated();884885  const salt = generateSalt();886  derivedKey = await deriveKey(password, salt);887  entries = [];888  vaultMode = "real";889  duressConfigured = false;890  buildDomainIndexSync(entries);891892  // Generate and wrap the root key for recovery word escrow.893  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =894    await generateAndWrapRootKey();895  rootKey = unhex(rawRootKey);896897  const plaintext = encodePayload(entries, duressConfigured);898  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);899900  // Second wrapping of rootKey under derivedKey so future unlocks can rehydrate901  // it without recovery words (H2/COR-006).902  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);903904  const slotA: StoredVault = {905    version: 1,906    salt: hex(salt),907    nonce,908    ciphertext,909    tag,910    recovery_salt: recoverySalt,911    recovery_wrapped_root_key: wrappedRootKey,912    recovery_nonce: wrappedNonce,913    recovery_tag: wrappedTag,914    ...pwRoot,915  };916  await writeSlot(SLOT_A, slotA);917918  // Matching-size noise blob for slot B, so a fresh install shows two919  // similarly-shaped records regardless of whether duress is ever set up.920  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));921  await stampLocalVaultMutation();922923  touchAutoLock();924}925926export async function hasVault(): Promise<boolean> {927  await ensureMigrated();928  return (await loadSlot(SLOT_A)) !== null;929}930931/**932 * Returns whether the real vault has recorded that a duress passphrase is933 * configured. Reliable only after a real-mode unlock — before unlock or in934 * decoy mode, returns false because we cannot read slot A's plaintext.935 */936export async function isDuressEnabled(): Promise<boolean> {937  return vaultMode === "real" && duressConfigured;938}939940export async function setupDuress(duressPassword: string): Promise<void> {941  if (!derivedKey || vaultMode !== "real") {942    throw new Error("vault must be unlocked in real mode");943  }944  await ensureMigrated();945946  // Encrypt the decoy payload (empty entries) under the duress key. Pad to947  // at least the current slot A ciphertext size so the two slots look the948  // same at rest at setup time.949  const slotA = await loadSlot(SLOT_A);950  if (!slotA) throw new Error("missing slot A");951952  const decoySalt = generateSalt();953  const decoyKey = await deriveKey(duressPassword, decoySalt);954  try {955    // Pad the decoy plaintext to match slot A's ciphertext length (minus the956    // 16-byte tag) so encrypted sizes align exactly.957    const targetBytes = slotA.ciphertext.length / 2;958    let plaintext = encodePayload([], false);959    const encoded = new TextEncoder().encode(plaintext);960    if (encoded.length < targetBytes) {961      plaintext = plaintext + " ".repeat(targetBytes - encoded.length);962    }963    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext);964    await writeSlot(SLOT_B, {965      version: 1,966      salt: hex(decoySalt),967      nonce,968      ciphertext,969      tag,970    });971  } finally {972    zeroKey(decoyKey);973  }974975  // Re-encrypt slot A with the duressConfigured flag set to true. No external976  // flag is written — the bit lives inside the encrypted envelope.977  duressConfigured = true;978  await persistSlotA();979}980981export async function disableDuress(): Promise<void> {982  if (!derivedKey || vaultMode !== "real") {983    throw new Error("vault must be unlocked in real mode");984  }985  await ensureMigrated();986987  // Overwrite slot B with fresh noise that matches slot A's size. The slot988  // remains occupied so its mere existence does not signal that duress was989  // ever configured.990  const slotA = await loadSlot(SLOT_A);991  if (slotA) {992    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));993  }994995  duressConfigured = false;996  await persistSlotA();997}998999export async function verifyMainPassword(password: string): Promise<boolean> {1000  await ensureMigrated();1001  const slotA = await loadSlot(SLOT_A);1002  if (!slotA) return false;1003  const key = await deriveKey(password, unhex(slotA.salt));1004  try {1005    return (await tryDecryptSlot(slotA, key)) !== null;1006  } finally {1007    zeroKey(key);1008  }1009}10101011export async function unlock(password: string): Promise<boolean> {1012  await ensureMigrated();10131014  // Load both slots in parallel. Both always exist post-migration; the1015  // parallel load and the parallel derivation below keep wall-clock time1016  // constant regardless of which slot's passphrase (real or decoy) the1017  // caller supplied, so a timing side channel cannot distinguish the1018  // duress path from the real-unlock path (DUR-002).1019  const [slotA, slotB] = await Promise.all([1020    loadSlot(SLOT_A),1021    loadSlot(SLOT_B),1022  ]);10231024  // Derive both keys in parallel, unconditionally.1025  const [keyA, keyB] = await Promise.all([1026    slotA ? deriveKey(password, unhex(slotA.salt)) : Promise.resolve(null),1027    slotB ? deriveKey(password, unhex(slotB.salt)) : Promise.resolve(null),1028  ]);10291030  // Attempt both decrypts in parallel. The Result variant lets us distinguish1031  // 'wrong_key' from 'corrupted' so callers can offer a history-restore path1032  // when a slot's schema is invalid even though AES-GCM authenticated (M7).1033  const [resultA, resultB] = await Promise.all([1034    keyA && slotA1035      ? tryDecryptSlotResult(slotA, keyA)1036      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1037    keyB && slotB1038      ? tryDecryptSlotResult(slotB, keyB)1039      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1040  ]);1041  const payloadA = resultA.status === "ok" ? resultA.payload : null;1042  const payloadB = resultB.status === "ok" ? resultB.payload : null;10431044  if (payloadA && keyA) {1045    derivedKey = keyA;1046    vaultMode = "real";1047    entries = payloadA.entries;1048    duressConfigured = payloadA.duressConfigured;1049    buildDomainIndexSync(entries);1050    await loadSettings();1051    touchAutoLock();1052    if (keyB) zeroKey(keyB);10531054    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).1055    if (slotA) {1056      const needsRecoverySetup =1057        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;1058      if (!needsRecoverySetup) {1059        rootKey = await deriveRootKeyFromDerivedKey(keyA, slotA);1060      }1061      // Pre-v2.12 vault, or a vault whose pw_root_* fields haven't been written1062      // yet: generate the recovery escrow and seed pw_root_* so subsequent1063      // unlocks can rehydrate rootKey without the user's recovery words.1064      // Best-effort — a failure here must not block unlock.1065      if (needsRecoverySetup || !rootKey) {1066        try {1067          await migrateToRecovery();1068          if (needsRecoverySetup) {1069            // String "1" matches the popup's existing peach_recovery_pending1070            // check; surfaced so the user re-confirms recovery words after a1071            // legacy-vault unlock (H2/COR-006).1072            await chrome.storage.local.set({ peach_recovery_pending: '1' });1073          }1074        } catch (migrationErr) {1075          console.warn('[Peach] recovery migration failed:', migrationErr);1076        }1077      }1078    }10791080    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat1081    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so1082    // the next save uses the new format and migration doesn't re-run forever.1083    if (payloadA.migratedFromLegacy) {1084      await persistSlotA();1085    }1086    return true;1087  }10881089  if (payloadB && keyB) {1090    derivedKey = keyB;1091    vaultMode = "decoy";1092    entries = payloadB.entries;1093    duressConfigured = false;1094    buildDomainIndexSync(entries);1095    await loadSettings();1096    touchAutoLock();1097    if (keyA) zeroKey(keyA);1098    return true;1099  }11001101  if (keyA) zeroKey(keyA);1102  if (keyB) zeroKey(keyB);1103  // Surface corruption (good AES tag, broken schema) distinctly from1104  // wrong-password so the popup can offer history-restore (M7/COR-007).1105  if (resultA.status === "corrupted") {1106    lastUnlockFailureReason = resultA.reason;1107  } else if (resultB.status === "corrupted") {1108    lastUnlockFailureReason = resultB.reason;1109  }1110  return false;1111}11121113export function lock(): void {1114  if (derivedKey) {1115    zeroKey(derivedKey);1116    derivedKey = null;1117  }1118  if (rootKey) {1119    rootKey.fill(0);1120    rootKey = null;1121  }11221123  entries = [];1124  domainIndex = new Map();1125  vaultMode = "real";1126  duressConfigured = false;1127  clearAutoLock();1128}11291130export function isUnlocked(): boolean {1131  return derivedKey !== null;1132}11331134// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).1135// Used by Android biometric unlock — the key was derived on the native side.1136export async function unlockWithKey(1137  masterKeyHex: string,1138): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {1139  try {1140    const masterKeyBytes = unhex(masterKeyHex);1141    const stored = await loadSlot(SLOT_A);1142    if (!stored) return { ok: false, error: "invalid_key" };1143    const payload = await tryDecryptSlot(stored, masterKeyBytes);1144    if (!payload) return { ok: false, error: "invalid_key" };11451146    derivedKey = masterKeyBytes;1147    vaultMode = "real";1148    entries = payload.entries;1149    duressConfigured = payload.duressConfigured;1150    buildDomainIndexSync(entries);1151    await loadSettings();1152    touchAutoLock();1153    return { ok: true, entries };1154  } catch {1155    return { ok: false, error: "invalid_key" };1156  }1157}11581159/**1160 * Restore the vault from a parsed Peach Codex payload.1161 *1162 * The supplied passphrase is verified against the Codex's slotA *before* any1163 * storage mutation — wrong passphrases return without touching disk. On1164 * success, the current vault state is snapshotted (reason: "restore") so the1165 * user can revert via Settings -> Vault History, then slotA/slotB are written1166 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1167 * regular `unlock()` follows so the rest of the SW sees the standard1168 * post-unlock state.1169 *1170 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1171 * printed by a Pro user must always be restorable, even on a free device.1172 *1173 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1174 * is an explicit user action with a known passphrase, not a passive lock1175 * screen, so the duress-vs-real timing side channel does not apply.1176 */1177export async function unlockFromCodex(params: {1178  slotA: string; // JSON string of StoredVault for slot A1179  slotB: string | null; // JSON string of StoredVault for slot B, or null1180  timestamp: number;1181  masterPassphrase: string;1182}): Promise<1183  | { ok: true; entryCount: number }1184  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1185> {1186  let parsedSlotA: StoredVault;1187  let parsedSlotB: StoredVault | null = null;1188  try {1189    const rawA = JSON.parse(params.slotA) as unknown;1190    if (!hasValidVaultSchema(rawA)) {1191      return { ok: false, reason: "invalid_slot_format" };1192    }1193    parsedSlotA = rawA;1194    if (params.slotB !== null) {1195      const rawB = JSON.parse(params.slotB) as unknown;1196      if (!hasValidVaultSchema(rawB)) {1197        return { ok: false, reason: "invalid_slot_format" };1198      }1199      parsedSlotB = rawB;1200    }1201  } catch {1202    return { ok: false, reason: "invalid_slot_format" };1203  }12041205  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1206  // If this fails the on-disk vault is left completely untouched.1207  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1208  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey);1209  if (!trialPayload) {1210    zeroKey(trialKey);1211    return { ok: false, reason: "wrong_passphrase" };1212  }1213  // Drop the trial key; `unlock()` below derives its own copy. Holding two1214  // copies of the master key in memory at once would be a needless surface.1215  zeroKey(trialKey);12161217  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1218  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1219  await takeVaultSnapshot("restore");12201221  await writeSlot(SLOT_A, parsedSlotA);1222  if (parsedSlotB) {1223    await writeSlot(SLOT_B, parsedSlotB);1224  } else {1225    // Slot B must always exist with ciphertext length matching slot A1226    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1227    // configured); regenerate noise to satisfy the invariant.1228    await writeSlot(1229      SLOT_B,1230      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1231    );1232  }1233  await chrome.storage.local.set({1234    peach_last_modified_at: String(params.timestamp),1235  });12361237  // Standard unlock path so domain index, settings, auto-lock, and1238  // recovery-key state all populate exactly as a normal unlock would.1239  const ok = await unlock(params.masterPassphrase);1240  if (!ok) {1241    // Should be unreachable — we just verified the passphrase against the1242    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1243    // simple if some race or storage anomaly intrudes.1244    return { ok: false, reason: "wrong_passphrase" };1245  }1246  return { ok: true, entryCount: getEntries().length };1247}12481249// Returns the current in-memory derived key as a hex string.1250// Used by the biometric enable flow in Settings to store the key in Android Keystore.1251export function getDerivedKeyHex(): string {1252  if (!derivedKey) throw new Error("vault_not_unlocked");1253  return hex(derivedKey);1254}12551256export function getEntries(): VaultEntry[] {1257  if (!derivedKey) throw new Error("vault locked");1258  return entries.filter((entry) => !entry.deletedAt);1259}12601261const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;12621263export function getIdentities(): SavedIdentity[] {1264  if (!derivedKey) throw new Error("vault locked");1265  const active = entries.filter((e) => !e.deletedAt && e.type === "login");12661267  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1268  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();12691270  for (const entry of active) {1271    const value = entry.username?.trim();1272    if (!value) continue;1273    const map = EMAIL_RE.test(value) ? emailMap : usernameMap;1274    const existing = map.get(value);1275    const domain = extractDomain(entry.url ?? "") ?? "";1276    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;1277    if (!existing || modifiedAt > existing.lastUsed) {1278      map.set(value, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1279    } else {1280      map.set(value, { ...existing, count: existing.count + 1 });1281    }1282  }12831284  const toIdentity = (type: "email" | "username") =>1285    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1286      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1287    });12881289  return [1290    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1291    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1292  ];1293}12941295export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1296  if (!derivedKey) throw new Error("vault locked");12971298  const now = Date.now();1299  const added: VaultEntry[] = [];1300  for (const draft of drafts) {1301    try {1302      const sanitized = sanitizeEntryDraft(draft);1303      const nextEntry: VaultEntry = {1304        ...sanitized,1305        id: crypto.randomUUID(),1306        createdAt: now,1307        modifiedAt: now,1308      };1309      entries.push(nextEntry);1310      added.push(nextEntry);1311    } catch {1312      // Skip invalid drafts silently — caller can see diff between input and output length1313    }1314  }13151316  if (added.length > 0) {1317    await persistEntries();1318  }13191320  return added;1321}13221323/**1324 * Restore a set of entries in full fidelity — preserves id, createdAt,1325 * modifiedAt, and every other field unlike `batchAddEntries` which1326 * assigns fresh identity. Used by restore paths where the backup is the1327 * authoritative source of truth. `mode` controls how entries already in1328 * the vault are treated.1329 */1330export async function restoreEntries(1331  incoming: VaultEntry[],1332  mode: "replace" | "merge"1333): Promise<{ added: number; updated: number; skipped: number }> {1334  if (!derivedKey) throw new Error("vault locked");13351336  // A replace with zero incoming entries is almost certainly a caller bug1337  // (e.g. an import that silently dropped everything). Refuse rather than1338  // wipe the vault.1339  if (mode === "replace" && incoming.length === 0) {1340    throw new Error("refuse to replace with empty entry set");1341  }13421343  let added = 0;1344  let updated = 0;1345  let skipped = 0;13461347  if (mode === "replace") {1348    entries = [];1349    for (const entry of incoming) {1350      const normalized = sanitizeRestoredEntry(entry);1351      if (!normalized) {1352        skipped += 1;1353        continue;1354      }1355      entries.push(normalized);1356      added += 1;1357    }1358  } else {1359    const byId = new Map<string, VaultEntry>();1360    for (const e of entries) byId.set(e.id, e);13611362    for (const entry of incoming) {1363      const normalized = sanitizeRestoredEntry(entry);1364      if (!normalized) {1365        skipped += 1;1366        continue;1367      }1368      const existing = byId.get(normalized.id);1369      if (!existing) {1370        entries.push(normalized);1371        added += 1;1372      } else if (normalized.modifiedAt > existing.modifiedAt) {1373        const idx = entries.findIndex((e) => e.id === existing.id);1374        if (idx !== -1) {1375          entries[idx] = normalized;1376          updated += 1;1377        }1378      } else {1379        skipped += 1;1380      }1381    }1382  }13831384  await persistEntries("import");1385  return { added, updated, skipped };1386}13871388/**1389 * Validate + normalise a VaultEntry coming from a backup. Accepts1390 * anything structurally sound; keeps all metadata verbatim. Returns1391 * null for values the live vault shouldn't accept (e.g., missing title).1392 */1393function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1394  if (!entry || typeof entry !== "object") return null;1395  const title = typeof entry.title === "string" ? entry.title.trim() : "";1396  if (!title) return null;1397  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1398  const now = Date.now();1399  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1400  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;14011402  // Migrate legacy passkey shape on import (same logic as on-disk read).1403  // Imports from pre-v2.11.0 vault backups still carry type:"passkey" with1404  // flat fields; bring them into the new shape immediately.1405  const migrated: VaultEntry = migrateLegacyPasskey(1406    {1407      ...entry,1408      id,1409      title,1410      type: entry.type ?? "login",1411      createdAt,1412      modifiedAt,1413    },1414    entry1415  );14161417  // Login entries from backup must carry a password (or be passkey-only,1418  // in which case the passkey sub-object replaces the password requirement).1419  if (1420    migrated.type === "login" &&1421    !migrated.passkey &&1422    typeof migrated.password !== "string"1423  ) {1424    return null;1425  }1426  if (migrated.passkey && !migrated.passkey.credentialId) {1427    return null;1428  }14291430  return migrated;1431}14321433// Soft-deletes every non-deleted entry in one call. The literal string1434// argument is a tripwire against accidental calls — any caller that would1435// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1436// to a boolean.1437export async function clearVaultEntries(1438  confirmation: "REPLACE_ALL"1439): Promise<void> {1440  if (confirmation !== "REPLACE_ALL") {1441    throw new Error("clearVaultEntries requires explicit confirmation");1442  }1443  if (!derivedKey) throw new Error("vault locked");1444  for (const entry of entries) {1445    if (!entry.deletedAt) {1446      entry.deletedAt = Date.now();1447      entry.modifiedAt = Date.now();1448    }1449  }1450  await persistEntries();1451}14521453export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1454  if (!derivedKey) throw new Error("vault locked");14551456  const sanitized = sanitizeEntryDraft(entry);1457  const now = Date.now();1458  const nextEntry: VaultEntry = {1459    ...sanitized,1460    id: crypto.randomUUID(),1461    createdAt: now,1462    modifiedAt: now,1463    // Stamp passwordChangedAt at creation so age tracking is accurate from1464    // day one. Non-login entries carry the same stamp harmlessly.1465    passwordChangedAt: sanitized.password ? now : undefined,1466  };14671468  entries.push(nextEntry);1469  await persistEntries();14701471  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1472  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1473    checkBreach(nextEntry.password).then(async (count) => {1474      if (count > 0) {1475        const idx = entries.findIndex((e) => e.id === nextEntry.id);1476        if (idx !== -1) {1477          entries[idx] = { ...entries[idx], breach: count, modifiedAt: Date.now() };1478          await persistEntries();1479        }1480      }1481    });1482  }14831484  return nextEntry;1485}14861487export async function updateEntry(1488  id: string,1489  patch: Partial<VaultEntry>1490): Promise<void> {1491  if (!derivedKey) throw new Error("vault locked");14921493  const idx = entries.findIndex((entry) => entry.id === id);1494  if (idx === -1 || entries[idx].deletedAt) {1495    throw new Error("entry not found");1496  }14971498  const current = entries[idx];1499  const sanitizedPatch = sanitizeEntryPatch(patch);1500  // Refresh passwordChangedAt only when the password value actually changes —1501  // a metadata-only update (title, tags, etc.) must not reset the age clock1502  // that drives the Pro health rotation recommendation.1503  const passwordChanged =1504    "password" in patch &&1505    typeof sanitizedPatch.password === "string" &&1506    sanitizedPatch.password !== current.password;1507  entries[idx] = {1508    ...current,1509    ...sanitizedPatch,1510    modifiedAt: Date.now(),1511    ...(passwordChanged ? { passwordChangedAt: Date.now() } : {}),1512  };15131514  // A login entry must carry SOMETHING fillable — either a password or a1515  // passkey credential. An empty-password login with no passkey is dead1516  // state. Permit the password-clear when a passkey is attached (that's1517  // the legitimate "drop the password, keep the passkey" path).1518  if (1519    entries[idx].type === "login" &&1520    !entries[idx].password &&1521    !entries[idx].passkey1522  ) {1523    entries[idx] = current;1524    throw new Error("password or passkey required");1525  }15261527  // Card entries must retain their card number1528  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1529    entries[idx] = current;1530    throw new Error("card number is required for card entries");1531  }15321533  await persistEntries();15341535  // Breach check when password changes: clear immediately so old breach doesn't linger,1536  // then async-check the new password and only re-set if it's also found.1537  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1538    entries[idx] = { ...entries[idx], breach: undefined };1539    await persistEntries();1540    const password = entries[idx].password;1541    if (password) {1542      checkBreach(password).then(async (count) => {1543        const i = entries.findIndex((e) => e.id === id);1544        if (i !== -1 && count > 0) {1545          entries[i] = { ...entries[i], breach: count, modifiedAt: Date.now() };1546          await persistEntries();1547        }1548      });1549    }1550  }1551}15521553export async function deleteEntry(id: string): Promise<void> {1554  if (!derivedKey) throw new Error("vault locked");15551556  const entry = entries.find((item) => item.id === id);1557  if (!entry || entry.deletedAt) {1558    throw new Error("entry not found");1559  }15601561  entry.deletedAt = Date.now();1562  entry.modifiedAt = Date.now();1563  await persistEntries();1564}15651566export function getDeletedEntries(): VaultEntry[] {1567  if (!derivedKey) return [];1568  return entries.filter((e) => !!e.deletedAt);1569}15701571export async function restoreEntry(id: string): Promise<void> {1572  if (!derivedKey) throw new Error("vault locked");1573  const entry = entries.find((e) => e.id === id && e.deletedAt);1574  if (!entry) throw new Error("deleted entry not found");1575  delete entry.deletedAt;1576  entry.modifiedAt = Date.now();1577  await persistEntries();1578  buildDomainIndexSync(entries);1579}15801581export async function purgeEntry(id: string): Promise<void> {1582  if (!derivedKey) throw new Error("vault locked");1583  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);1584  if (idx === -1) throw new Error("deleted entry not found");1585  entries.splice(idx, 1);1586  await persistEntries();1587}15881589/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */1590export async function purgeExpiredTrash(retentionMs: number): Promise<number> {1591  if (!derivedKey || retentionMs === 0) return 0;1592  const cutoff = Date.now() - retentionMs;1593  const before = entries.length;1594  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));1595  const purged = before - entries.length;1596  if (purged > 0) await persistEntries();1597  return purged;1598}15991600export function queryDomain(domain: string): VaultEntry[] {1601  return domainIndex.get(domain) ?? [];1602}16031604export function getEntryById(id: string): VaultEntry | undefined {1605  return entries.find((entry) => entry.id === id && !entry.deletedAt);1606}16071608async function persistSlotA(1609  reason: VaultSnapshot["reason"] = "local_save",1610): Promise<StoredVault> {1611  if (!derivedKey) throw new Error("vault locked");16121613  const slotA = await loadSlot(SLOT_A);1614  if (!slotA) throw new Error("missing slot A");16151616  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures1617  // are swallowed by takeVaultSnapshot — history must never block a save.1618  await takeVaultSnapshot(reason);16191620  const plaintext = encodePayload(entries, duressConfigured);1621  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);16221623  // Preserve existing recovery escrow fields.1624  const nextSlotA: StoredVault = {1625    ...slotA,1626    nonce,1627    ciphertext,1628    tag,1629    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot1630  };1631  await writeSlot(SLOT_A, nextSlotA);16321633  // When duress is not configured, keep slot B size-matched with slot A by1634  // regenerating its noise. When duress IS configured, leave slot B alone —1635  // we don't have the decoy key to re-encrypt it. This is a known trade-off.1636  if (!duressConfigured) {1637    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1638  }1639  await stampLocalVaultMutation();16401641  return nextSlotA;1642}16431644async function persistSlotB(): Promise<void> {1645  if (!derivedKey) throw new Error("vault locked");16461647  const slotB = await loadSlot(SLOT_B);1648  if (!slotB) throw new Error("missing slot B");16491650  // Decoy plaintext always reports duressConfigured=false — the decoy user1651  // sees a clean slate.1652  const plaintext = encodePayload(entries, false);1653  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);16541655  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });1656  await stampLocalVaultMutation();1657}16581659async function persistEntries(1660  reason: VaultSnapshot["reason"] = "local_save",1661): Promise<void> {1662  if (!derivedKey) throw new Error("vault locked");16631664  if (vaultMode === "decoy") {1665    await persistSlotB();1666  } else {1667    await persistSlotA(reason);1668  }1669  buildDomainIndexSync(entries);1670}16711672function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {1673  if (!Array.isArray(values)) return undefined;1674  const seen = new Set<string>();1675  const cleaned: string[] = [];1676  for (const value of values) {1677    const trimmed = typeof value === "string" ? value.trim() : "";1678    if (!trimmed) continue;1679    const key = trimmed.toLowerCase();1680    if (seen.has(key)) continue;1681    seen.add(key);1682    cleaned.push(trimmed);1683  }1684  return cleaned.length > 0 ? cleaned : undefined;1685}16861687function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {1688  const title = entry.title.trim();1689  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new1690  // model is "any entry, optionally with a passkey sub-object." Imports and1691  // any in-flight callers from before v2.11.0 keep working without throwing.1692  let type: VaultEntry["type"] = entry.type ?? "login";1693  if (type === "passkey") type = "login";16941695  if (!title) throw new Error("title is required");1696  // A login entry needs either a password or a passkey credential to be useful.1697  // Passkey-only entries (passkey present, no password) are valid.1698  if (type === "login" && !entry.password?.trim() && !entry.passkey) {1699    throw new Error("password or passkey required");1700  }1701  if (entry.passkey) {1702    const p = entry.passkey;1703    if (1704      !p.rpId ||1705      !p.credentialId ||1706      !p.publicKeyCose ||1707      !p.userHandle ||1708      !p.userName ||1709      (p.algorithm !== -7 && p.algorithm !== -8)1710    ) {1711      throw new Error("invalid passkey credential");1712    }1713  }1714  if (type === "card") {1715    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {1716      throw new Error("cardholder name and card number are required");1717    }1718  }17191720  return {1721    type,1722    title,1723    username: type === "login" ? entry.username?.trim() ?? "" : undefined,1724    password: type === "login" ? entry.password : undefined,1725    url: type === "login" ? entry.url?.trim() ?? "" : undefined,1726    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,1727    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,1728    notes: entry.notes?.trim() || undefined,1729    breach: type === "login" ? entry.breach : undefined,1730    canary: type === "login" ? entry.canary : undefined,1731    tags: entry.tags,1732    passkey: entry.passkey,1733    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,1734    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,1735    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,1736    expYear: type === "card" ? entry.expYear?.trim() : undefined,1737    cvc: type === "card" ? entry.cvc?.trim() : undefined,1738    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,1739    network: type === "card" ? entry.network : undefined,1740    last4: type === "card" ? entry.last4?.trim() : undefined,1741  };1742}17431744function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {1745  const nextPatch: Partial<VaultEntry> = {};17461747  if ("type" in patch) {1748    nextPatch.type = patch.type;1749  }17501751  if ("title" in patch) {1752    const title = patch.title?.trim() ?? "";1753    if (!title) throw new Error("title is required");1754    nextPatch.title = title;1755  }17561757  if ("url" in patch) {1758    nextPatch.url = patch.url?.trim() ?? "";1759  }17601761  if ("additionalUrls" in patch) {1762    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];1763    const seen = new Set<string>();1764    const cleaned: string[] = [];1765    for (const value of incoming) {1766      const trimmed = typeof value === "string" ? value.trim() : "";1767      if (!trimmed) continue;1768      const key = trimmed.toLowerCase();1769      if (seen.has(key)) continue;1770      seen.add(key);1771      cleaned.push(trimmed);1772    }1773    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;1774  }17751776  if ("username" in patch) {1777    nextPatch.username = patch.username?.trim() ?? "";1778  }17791780  if ("email" in patch) {1781    nextPatch.email = patch.email?.trim() || undefined;1782  }17831784  if ("password" in patch) {1785    const password = patch.password ?? "";1786    // Allow clearing a password (e.g. on passkey-only entries)1787    nextPatch.password = password || undefined;1788  }17891790  if ("totp" in patch) {1791    nextPatch.totp = patch.totp?.trim() || undefined;1792  }17931794  if ("notes" in patch) {1795    nextPatch.notes = patch.notes?.trim() || undefined;1796  }17971798  if ("breach" in patch) {1799    nextPatch.breach = patch.breach;1800  }18011802  if ("canary" in patch) {1803    nextPatch.canary = patch.canary;1804  }18051806  if ("passkey" in patch) {1807    // Passkey sub-object replaces all the legacy flat passkey fields. A1808    // patch with `passkey: undefined` removes the credential from the entry1809    // (used by "remove passkey" without deleting the whole entry).1810    nextPatch.passkey = patch.passkey;1811  }18121813  if ("tags" in patch) {1814    nextPatch.tags = patch.tags;1815  }18161817  if ("cardholderName" in patch) {1818    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;1819  }1820  if ("cardNumber" in patch) {1821    const n = patch.cardNumber ?? "";1822    nextPatch.cardNumber = n || undefined;1823  }1824  if ("expMonth" in patch) {1825    nextPatch.expMonth = patch.expMonth?.trim() || undefined;1826  }1827  if ("expYear" in patch) {1828    nextPatch.expYear = patch.expYear?.trim() || undefined;1829  }1830  if ("cvc" in patch) {1831    nextPatch.cvc = patch.cvc?.trim() || undefined;1832  }1833  if ("cardNickname" in patch) {1834    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;1835  }1836  if ("network" in patch) {1837    nextPatch.network = patch.network;1838  }1839  if ("last4" in patch) {1840    nextPatch.last4 = patch.last4?.trim() || undefined;1841  }18421843  return nextPatch;1844}18451846function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {1847  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;1848  const modifiedAt =1849    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;18501851  const base: VaultEntry = {1852    id: entry.id,1853    type: "type" in entry ? entry.type : "login",1854    title: entry.title,1855    username: entry.username,1856    password: entry.password,1857    url: entry.url,1858    totp: entry.totp,1859    notes: entry.notes,1860    breach: entry.breach,1861    canary: "canary" in entry ? entry.canary : undefined,1862    passkey: "passkey" in entry ? entry.passkey : undefined,1863    createdAt,1864    modifiedAt,1865    deletedAt: entry.deletedAt,1866    tags: "tags" in entry ? entry.tags : undefined,1867    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,1868    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,1869    expMonth: "expMonth" in entry ? entry.expMonth : undefined,1870    expYear: "expYear" in entry ? entry.expYear : undefined,1871    cvc: "cvc" in entry ? entry.cvc : undefined,1872    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,1873    network: "network" in entry ? entry.network : undefined,1874    last4: "last4" in entry ? entry.last4 : undefined,1875  };18761877  return migrateLegacyPasskey(base, entry);1878}18791880/**1881 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`1882 * with flat top-level fields (rpId, credentialId, etc.). This converts them1883 * to the new shape — `type:"login"` with a `passkey` sub-object — so all1884 * downstream consumers can rely on the new model. The result is rewritten1885 * to disk on next vault save (hasLegacyShape detects the un-migrated case).1886 */1887function migrateLegacyPasskey(1888  base: VaultEntry,1889  raw: VaultEntry | LegacyVaultEntry1890): VaultEntry {1891  if (base.passkey) return base;1892  const flat = raw as Partial<VaultEntry>;1893  const isPasskeyType = base.type === "passkey";1894  const hasFlatPasskeyFields =1895    typeof flat.rpId === "string" &&1896    typeof flat.credentialId === "string" &&1897    typeof flat.publicKeyCose === "string" &&1898    typeof flat.userHandle === "string" &&1899    typeof flat.userName === "string";1900  if (!isPasskeyType && !hasFlatPasskeyFields) return base;1901  if (!hasFlatPasskeyFields) return base;19021903  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;1904  base.passkey = {1905    rpId: flat.rpId!,1906    rpName: flat.rpName ?? flat.rpId!,1907    credentialId: flat.credentialId!,1908    publicKeyCose: flat.publicKeyCose!,1909    privateKeyJwk: flat.privateKeyJwk,1910    userHandle: flat.userHandle!,1911    userName: flat.userName!,1912    userDisplayName: flat.userDisplayName ?? flat.userName!,1913    algorithm,1914    signCount: flat.signCount ?? 0,1915    aaguid: flat.aaguid ?? "",1916    backupEligible: flat.backupEligible === true,1917    backupState: flat.backupState === true,1918    lastUsedAt: flat.lastUsedAt,1919    browser: flat.browser,1920    transports: flat.transports,1921    createdAt: base.createdAt,1922  };1923  // Promote to "login" so the unified Entry view renders. Username gets1924  // populated from the passkey's userName so the entry is searchable in the1925  // existing autofill flow without a separate code path.1926  if (isPasskeyType) base.type = "login";1927  if (!base.username) base.username = flat.userName;1928  if (!base.url) base.url = `https://${flat.rpId}`;1929  return base;1930}193119321933// ---------------------------------------------------------------------------1934// Deduplication1935// ---------------------------------------------------------------------------19361937function entryDupeKey(e: VaultEntry): string {1938  if (e.type === "note") return `note|${e.title.toLowerCase()}`;1939  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins1940  // on different sites that happen to share a username are still distinct;1941  // two passkey credentials for the same RP+user are duplicates regardless1942  // of any login fields they may also carry.1943  if (e.passkey) {1944    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;1945  }1946  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;1947}19481949/** Returns the number of duplicate entries (those that would be removed). */1950export function countDuplicates(): number {1951  const best = new Map<string, VaultEntry>();1952  let dupeCount = 0;1953  for (const e of entries.filter((e) => !e.deletedAt)) {1954    const key = entryDupeKey(e);1955    const existing = best.get(key);1956    if (!existing) {1957      best.set(key, e);1958    } else {1959      dupeCount++;1960      // Keep the more recently modified one in the map1961      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1962        best.set(key, e);1963      }1964    }1965  }1966  return dupeCount;1967}19681969/**1970 * Soft-deletes duplicate entries, keeping the most recently modified copy of1971 * each group. Returns the number of entries moved to trash.1972 */1973export async function deduplicateEntries(): Promise<number> {1974  if (!derivedKey) throw new Error("vault locked");1975  // Dedup reshapes slot B permanently under the decoy key, which is1976  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.1977  if (vaultMode === "decoy") {1978    throw new Error("deduplicate disabled in decoy mode");1979  }19801981  const best = new Map<string, VaultEntry>();1982  for (const e of entries.filter((en) => !en.deletedAt)) {1983    const key = entryDupeKey(e);1984    const existing = best.get(key);1985    if (!existing) {1986      best.set(key, e);1987    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1988      best.set(key, e);1989    }1990  }19911992  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));1993  const now = Date.now();1994  let removed = 0;1995  entries = entries.map((e) => {1996    if (!e.deletedAt && !keepIds.has(e.id)) {1997      removed++;1998      return { ...e, deletedAt: now };1999    }2000    return e;2001  });20022003  if (removed > 0) await persistEntries();2004  return removed;2005}20062007// ---------------------------------------------------------------------------2008// Vault history — snapshot BEFORE every vault write so the saved blob is2009// what is about to be lost, not what was just written. Capped at 10. The2010// stored blobs are AES-256-GCM ciphertext under the master password, so2011// history adds zero security surface.2012// ---------------------------------------------------------------------------20132014const HISTORY_KEY = "peach_vault_history";2015const HISTORY_MAX = 10;20162017/**2018 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE2019 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost2020 * state. Identical-to-most-recent blobs are skipped to avoid filling history2021 * with no-op writes.2022 */2023export async function takeVaultSnapshot(2024  reason: VaultSnapshot["reason"],2025): Promise<void> {2026  try {2027    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);2028    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];2029    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];20302031    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,2032    // pre-createVault). createVault skips snapshots intentionally — there is2033    // no prior state to preserve.2034    if (!slotA) return;20352036    const vault_a = JSON.stringify(slotA);2037    const vault_b = JSON.stringify(slotB ?? slotA);20382039    // Best-effort entry count: only meaningful when the in-memory state is2040    // unlocked AND in real mode. Decoy mode would leak the decoy entry2041    // count; locked mode has no entries to count. Both fall through to null.2042    let entryCount: number | null = null;2043    if (derivedKey && vaultMode === "real") {2044      try {2045        entryCount = entries.filter((e) => !e.deletedAt).length;2046      } catch {2047        entryCount = null;2048      }2049    }20502051    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2052    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)2053      ? existingRaw2054      : { snapshots: [] };20552056    // Deduplication: if the most recent snapshot has byte-identical blobs,2057    // skip — saves the same vault N times in a row would otherwise burn the2058    // entire history window with no recovery value.2059    const latest = history.snapshots[0];2060    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {2061      return;2062    }20632064    const snapshot: VaultSnapshot = {2065      id: crypto.randomUUID(),2066      savedAt: Date.now(),2067      reason,2068      vault_a,2069      vault_b,2070      entryCount,2071    };20722073    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);2074    await chrome.storage.local.set({ [HISTORY_KEY]: history });2075  } catch (err) {2076    // Non-fatal — history must never block a save. A failed snapshot just2077    // means the user loses one rung of the safety ladder for this write.2078    // M4/QUA-006: log so the failure is visible; if it looks like a quota2079    // problem, notify the popup so the user can prune history themselves.2080    console.warn('[Peach] takeVaultSnapshot failed:', err);2081    if (String(err).includes('QUOTA') || String(err).includes('quota')) {2082      try {2083        await chrome.runtime.sendMessage({ type: 'VAULT_STORAGE_QUOTA_WARNING' });2084      } catch {2085        // popup may be closed; surfaced via console.warn above2086      }2087    }2088  }2089}20902091/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */2092export async function clearVaultHistory(): Promise<void> {2093  try {2094    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });2095  } catch {2096    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().2097  }2098}20992100/** Read all snapshots, newest first, for the Settings vault history view. */2101export async function getVaultHistory(): Promise<VaultSnapshot[]> {2102  const raw = await chrome.storage.local.get(HISTORY_KEY);2103  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2104  if (!history || !Array.isArray(history.snapshots)) return [];2105  return history.snapshots;2106}21072108/**2109 * Replace the on-disk vault with the chosen snapshot's blobs and lock the2110 * vault. The current state is itself snapshotted with reason 'restore' first2111 * so the restore is reversible. After this returns, the user must unlock2112 * with the password that matches the restored blob.2113 */2114export async function restoreVaultSnapshot(2115  snapshotId: string,2116): Promise<{ ok: true } | { ok: false; error: string }> {2117  const history = await getVaultHistory();2118  const snapshot = history.find((s) => s.id === snapshotId);2119  if (!snapshot) return { ok: false, error: "snapshot_not_found" };21202121  // Snapshot the about-to-be-replaced current state so the restore is2122  // reversible. takeVaultSnapshot is no-op when the most recent entry is2123  // byte-identical, so a restore-of-a-restore stays clean.2124  await takeVaultSnapshot("restore");21252126  let restoredA: StoredVault;2127  let restoredB: StoredVault;2128  try {2129    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;2130    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;2131  } catch {2132    return { ok: false, error: "snapshot_corrupt" };2133  }21342135  await writeSlot(SLOT_A, restoredA);2136  await writeSlot(SLOT_B, restoredB);2137  await stampLocalVaultMutation();21382139  // Lock so the user must prove they can decrypt the restored blob. If they2140  // restored a pre-password-change snapshot they discover it immediately at2141  // the lock screen rather than silently holding an inaccessible vault.2142  lock();21432144  return { ok: true };2145}21462147/**2148 * Change the master password. Re-encrypts slot A with a fresh salt + new2149 * derived key. Slot B (duress or noise) is left untouched when duress is2150 * configured — it uses an independent key derived from the duress password —2151 * and regenerated as noise when duress is not configured.2152 */2153export async function changePassword(2154  currentPassword: string,2155  newPassword: string2156): Promise<boolean> {2157  if (!derivedKey || vaultMode !== "real") return false;21582159  // Verify current password before allowing any change2160  const valid = await verifyMainPassword(currentPassword);2161  if (!valid) return false;21622163  const slotA = await loadSlot(SLOT_A);2164  if (!slotA) return false;2165  if (!rootKey) return false;21662167  // Derive fresh key from new password + new random salt2168  const newSalt = generateSalt();2169  const newKey = await deriveKey(newPassword, newSalt);21702171  // Re-encrypt the in-memory entries with the new key2172  const plaintext = encodePayload(entries, duressConfigured);2173  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);21742175  // Pre-wrap root key under the new derived key. Recovery fields wrap rootKey2176  // under recovery words and remain untouched — recovery phrase keeps working2177  // across password changes (H4/COR-002).2178  const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);21792180  const finalSlotA: StoredVault = {2181    ...slotA,2182    salt: hex(newSalt),2183    nonce,2184    ciphertext,2185    tag,2186    ...pwRoot,2187  };21882189  // Snapshot the OLD-key blob before overwriting it. If the user later2190  // restores from this snapshot they will need the OLD password — the2191  // post-restore lock surfaces that immediately.2192  await takeVaultSnapshot("local_save");21932194  // Single atomic write: ciphertext, salt, and pw_root_* fields all land in2195  // one storage transaction. An SW kill before this point leaves the old2196  // slot intact; after this point everything tied to the new password is2197  // already on disk together (M6/COR-004).2198  await writeSlot(SLOT_A, finalSlotA);21992200  // Swap the in-memory key synchronously before any further await. If we2201  // threw between the Slot A write and this swap, the next persistEntries()2202  // would overwrite Slot A with old-key ciphertext, locking the user out.2203  zeroKey(derivedKey);2204  derivedKey = newKey;22052206  // Regenerate noise for slot B when duress is not in use. Runs after the2207  // key swap so a failure here is a disposable noise regeneration, not a2208  // lockout.2209  if (!duressConfigured) {2210    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2211  }2212  await stampLocalVaultMutation();22132214  return true;2215}22162217// ---------------------------------------------------------------------------2218// Sync helpers2219// ---------------------------------------------------------------------------22202221export async function getVaultBlobForSync(): Promise<{2222  blob: string;2223  timestamp: number;2224  license?: string;2225} | null> {2226  const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, 'peach_last_modified_at', 'peach_license']);2227  const slotA = (raw as Record<string, unknown>)[SLOT_A];2228  if (!slotA) return null;2229  const slotB = (raw as Record<string, unknown>)[SLOT_B];2230  const blob = JSON.stringify({ a: slotA, b: slotB });2231  const rawTs = (raw as Record<string, unknown>)['peach_last_modified_at'];2232  const timestamp = typeof rawTs === 'string' ? parseInt(rawTs) : 0;2233  const license = (raw as Record<string, unknown>)['peach_license'];2234  return {2235    blob,2236    timestamp,2237    license: typeof license === 'string' ? license : undefined,2238  };2239}22402241export async function applyIncomingVault(2242  blobJson: string,2243  remoteTimestamp: number,2244  remoteLicense?: string,2245): Promise<boolean> {2246  const raw = await chrome.storage.local.get('peach_last_modified_at');2247  const rawTs = (raw as Record<string, unknown>)['peach_last_modified_at'];2248  const localTimestamp = typeof rawTs === 'string' ? parseInt(rawTs) : 0;22492250  if (remoteLicense) {2251    const licenseRaw = await chrome.storage.local.get('peach_license');2252    const existing = (licenseRaw as Record<string, unknown>)['peach_license'];2253    if (!existing) {2254      await chrome.storage.local.set({ peach_license: remoteLicense });2255    }2256  }22572258  if (remoteTimestamp <= localTimestamp) return false;22592260  // Parse + shape-validate before any storage mutation. A malformed blob2261  // (non-JSON, non-object, missing slot fields) must NOT touch slot A —2262  // otherwise an unauthenticated remote could poison the local vault by2263  // sending '[]' or '42' (M10/COR-012). Mirror the structural check2264  // already used in unlockFromCodex.2265  let parsed: unknown;2266  try {2267    parsed = JSON.parse(blobJson);2268  } catch {2269    throw new Error('invalid_incoming_vault_blob');2270  }2271  if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {2272    throw new Error('invalid_incoming_vault_blob');2273  }2274  const { a, b } = parsed as { a?: unknown; b?: unknown };2275  if (!hasValidVaultSchema(a)) {2276    throw new Error('invalid_incoming_vault_blob');2277  }22782279  // Snapshot before overwriting — non-fatal but user-visible.2280  // M4/QUA-006: a silent snapshot failure leaves the user without their2281  // safety net for an incoming sync write; surface it.2282  try {2283    await takeVaultSnapshot('sync_received');2284  } catch (snapshotErr) {2285    console.warn('[Peach] vault snapshot failed during sync:', snapshotErr);2286    if (String(snapshotErr).includes('QUOTA') || String(snapshotErr).includes('quota')) {2287      try {2288        await chrome.runtime.sendMessage({ type: 'VAULT_STORAGE_QUOTA_WARNING' });2289      } catch {2290        // popup may be closed; surfaced via console.warn above2291      }2292    }2293  }22942295  await writeSlot(SLOT_A, a);2296  if (hasValidVaultSchema(b)) await writeSlot(SLOT_B, b);2297  await chrome.storage.local.set({2298    peach_last_sync_at: String(Date.now()),2299    peach_last_modified_at: String(remoteTimestamp),2300  });23012302  return true;2303}23042305/**2306 * Returns the X25519 sync identity, encrypting the private key on first call2307 * and on any unencrypted-shape migration. Requires the vault to be unlocked2308 * — the private key is wrapped under the same derived key as the vault, so2309 * a locked browser cannot leak it from storage alone (M12/SEC-003).2310 *2311 * Persistence shapes:2312 *   v0 (pre-M12, migrated on first read):2313 *     { priv: base64, pub: base64 }2314 *   v1 (encrypted):2315 *     { pub: base64, enc_priv: hex, enc_nonce: hex, enc_tag: hex }2316 */2317export async function getSyncIdentity(): Promise<{2318  privateKeyBytes: Uint8Array;2319  publicKeyBytes: Uint8Array;2320  publicKeyHex: string;2321}> {2322  if (!derivedKey) throw new Error('vault_locked');2323  const raw = await chrome.storage.local.get('peach_sync_identity');2324  const stored = (raw as Record<string, unknown>)['peach_sync_identity'];23252326  if (typeof stored === 'string') {2327    const parsed = JSON.parse(stored) as {2328      pub: string;2329      priv?: string;2330      enc_priv?: string;2331      enc_nonce?: string;2332      enc_tag?: string;2333    };2334    const publicKeyBytes = base64DecodeBytes(parsed.pub);23352336    if (parsed.enc_priv && parsed.enc_nonce && parsed.enc_tag) {2337      const privHex = await decrypt(2338        derivedKey,2339        parsed.enc_nonce,2340        parsed.enc_priv,2341        parsed.enc_tag,2342      );2343      const privateKeyBytes = unhex(privHex);2344      return { privateKeyBytes, publicKeyBytes, publicKeyHex: bytesToHex(publicKeyBytes) };2345    }23462347    if (parsed.priv) {2348      // Migrate v0 (unencrypted private key) → v1.2349      const privateKeyBytes = base64DecodeBytes(parsed.priv);2350      await persistEncryptedSyncIdentity(privateKeyBytes, publicKeyBytes);2351      return { privateKeyBytes, publicKeyBytes, publicKeyHex: bytesToHex(publicKeyBytes) };2352    }2353  }23542355  // Generate a fresh X25519 keypair and persist it encrypted.2356  const { generateKeyPair } = await import('@stablelib/x25519');2357  const keypair = generateKeyPair();2358  await persistEncryptedSyncIdentity(keypair.secretKey, keypair.publicKey);2359  return {2360    privateKeyBytes: keypair.secretKey,2361    publicKeyBytes: keypair.publicKey,2362    publicKeyHex: bytesToHex(keypair.publicKey),2363  };2364}23652366async function persistEncryptedSyncIdentity(2367  privateKeyBytes: Uint8Array,2368  publicKeyBytes: Uint8Array,2369): Promise<void> {2370  if (!derivedKey) throw new Error('vault_locked');2371  const enc = await encrypt(derivedKey, hex(privateKeyBytes));2372  await chrome.storage.local.set({2373    peach_sync_identity: JSON.stringify({2374      pub: base64EncodeBytes(publicKeyBytes),2375      enc_priv: enc.ciphertext,2376      enc_nonce: enc.nonce,2377      enc_tag: enc.tag,2378    }),2379  });2380}23812382// Re-decrypt vault entries from storage using the current in-memory key.2383// Called after applyIncomingVault so the in-memory state stays consistent2384// with what was just written to storage.2385export async function reloadEntriesAfterSync(): Promise<boolean> {2386  if (!derivedKey) return false;2387  const slotA = await loadSlot(SLOT_A);2388  if (!slotA) return false;2389  try {2390    const payload = await tryDecryptSlot(slotA, derivedKey);2391    if (!payload) return false;2392    entries = payload.entries;2393    buildDomainIndexSync(entries);2394    return true;2395  } catch {2396    return false;2397  }2398}23992400function bytesToHex(bytes: Uint8Array): string {2401  return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');2402}24032404function base64EncodeBytes(bytes: Uint8Array): string {2405  let binary = '';2406  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);2407  return btoa(binary);2408}24092410function base64DecodeBytes(b64: string): Uint8Array {2411  const binary = atob(b64);2412  const bytes = new Uint8Array(binary.length);2413  for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);2414  return bytes;2415}