vault.ts

81.6 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 TextSize = "small" | "medium" | "large";9192export interface VaultSettings {93  accentColor?: AccentColorName;94  theme?: "dark" | "light";95  autoLockMinutes?: number;96  clipboardClearSeconds?: number;97  lockOnClose?: boolean;98  breachMonitoring?: boolean;99  passkeyProviderEnabled?: boolean;100  trashRetentionDays?: number;101  manualSummonDomains?: Record<string, ManualSummonRecord>;102  declinedCardFingerprints?: string[];103  /**104   * Text size scale for body copy and entry titles. Stops at "large" by105   * design — XL clips dense screens (Vault, Health) inside the fixed106   * 330×480 popup surface.107   */108  textSize?: TextSize;109  /**110   * When true, all entrance/exit transitions and ambient animations are111   * collapsed to instant. Honoured alongside the user's OS-level112   * `prefers-reduced-motion`.113   */114  reduceMotion?: boolean;115  /**116   * Android-only: when true, the master key is escrowed in AndroidKeyStore117   * and the lock screen offers biometric unlock. Toggled from Settings →118   * Security → Biometrics. Has no effect on desktop builds.119   */120  biometricsEnabled?: boolean;121  /**122   * User overrides for keyboard shortcuts. Keys are ShortcutAction names123   * (see src/shared/shortcuts.ts). Missing actions fall through to124   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the125   * canonical lookup. Storing only the deltas keeps the vault payload126   * small and lets default updates land for users who never rebind.127   */128  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;129}130131let settings: VaultSettings = {};132133export function getSettings(): VaultSettings {134  return settings;135}136137export async function ensureSettingsLoaded(): Promise<VaultSettings> {138  await loadSettings();139  return settings;140}141142export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {143  await loadSettings();144  settings = { ...settings, ...patch };145  await persistSettings();146}147148async function persistSettings(): Promise<void> {149  try {150    await chrome.storage.local.set({ peach_settings: settings });151  } catch (err) {152    throw mapStorageError(err);153  }154}155156function mapStorageError(err: unknown): Error {157  const message = err instanceof Error ? err.message : String(err);158  if (/QUOTA_BYTES|quota/i.test(message)) {159    return new Error("STORAGE_QUOTA_EXCEEDED");160  }161  return err instanceof Error ? err : new Error(message);162}163164async function loadSettings(): Promise<void> {165  const result = await chrome.storage.local.get("peach_settings");166  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};167}168169// ---------------------------------------------------------------------------170// HIBP breach check (k-anonymity model)171// ---------------------------------------------------------------------------172173export async function checkBreach(password: string): Promise<number> {174  // HIBP k-anonymity API is keyed on SHA-1 prefixes — this is dictated by175  // the public API, not a security choice. Do not migrate to SHA-256 here.176  const encoder = new TextEncoder();177  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));178  const hashHex = Array.from(new Uint8Array(hashBuffer))179    .map((b) => b.toString(16).padStart(2, "0"))180    .join("")181    .toUpperCase();182183  const prefix = hashHex.slice(0, 5);184  const suffix = hashHex.slice(5);185186  try {187    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);188    if (!res.ok) return 0;189    const text = await res.text();190    const match = text.split("\r\n").find((l) => l.startsWith(suffix));191    return match ? parseInt(match.split(":")[1], 10) : 0;192  } catch {193    return 0; // Network unavailable — don't block194  }195}196197export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {198  const fingerprint = await cardFingerprint(last4, expMonth, expYear);199  await loadSettings();200  settings = {201    ...settings,202    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],203  };204  await persistSettings();205}206207export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {208  await loadSettings();209  const fingerprint = await cardFingerprint(last4, expMonth, expYear);210  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);211}212213async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {214  const encoder = new TextEncoder();215  const buf = await crypto.subtle.digest("SHA-256", encoder.encode(`${last4}|${expMonth}|${expYear}`));216  return Array.from(new Uint8Array(buf).slice(0, 8))217    .map((b) => b.toString(16).padStart(2, "0"))218    .join("");219}220221function hex(buf: Uint8Array): string {222  return Array.from(buf)223    .map((b) => b.toString(16).padStart(2, "0"))224    .join("");225}226227function unhex(s: string): Uint8Array {228  const out = new Uint8Array(s.length / 2);229  for (let i = 0; i < s.length; i += 2) {230    out[i / 2] = parseInt(s.slice(i, i + 2), 16);231  }232  return out;233}234235// ---------------------------------------------------------------------------236// Root key generation and recovery word wrapping237// ---------------------------------------------------------------------------238239/**240 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key241 * derived from the recovery word material via HKDF-SHA256. The wrapped blob242 * fields are stored in slot A alongside the password-wrapped ciphertext. The243 * root key itself is kept in memory for the session so getRecoveryWords() can244 * encode it without re-deriving.245 */246async function generateAndWrapRootKey(): Promise<{247  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey248  recoverySalt: string;249  wrappedRootKey: string;250  wrappedNonce: string;251  wrappedTag: string;252}> {253  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));254255  // Derive the recovery key from the BIP39 words encoding of the root key.256  const words = await rootKeyToWords(rootKeyBytes);257  const wordMaterial = await wordsToRootKeyMaterial(words);258  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));259  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);260261  // Wrap the root key with the recovery key using AES-256-GCM directly.262  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.263  const nonce = crypto.getRandomValues(new Uint8Array(12));264  const encoded = new TextEncoder().encode(hex(rootKeyBytes));265  const raw = await crypto.subtle.encrypt(266    {267      name: "AES-GCM",268      iv: nonce,269      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,270      tagLength: 128,271    },272    recoveryKey,273    encoded274  );275  // WebCrypto returns ciphertext || auth tag concatenated; split them.276  const rawU8 = new Uint8Array(raw);277  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));278  const tag = hex(rawU8.slice(rawU8.length - 16));279280  return {281    rawRootKey: hex(rootKeyBytes),282    recoverySalt: hex(recoverySalt),283    wrappedRootKey: ciphertext,284    wrappedNonce: hex(nonce),285    wrappedTag: tag,286  };287}288289/**290 * Unwrap the root key using recovery words. Called during recovery flow when291 * the user provides their 12-word phrase and a new password.292 * Exported for use by the RECOVER_WITH_WORDS message handler.293 */294export async function unwrapRootKeyForRecovery(295  wrappedRootKey: string,296  wrappedNonce: string,297  wrappedTag: string,298  recoverySaltHex: string,299  words: string[]300): Promise<void> {301  const wordMaterial = await wordsToRootKeyMaterial(words);302  const recoverySalt = unhex(recoverySaltHex);303  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);304305  // Decrypt and cache the root key. The root key bytes were hex-encoded before306  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt307  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.308  // Each component is stored separately in the vault slot; decode them individually309  // so the nonce is always exactly the 12 bytes from wrappedNonce.310  const nonceRaw = unhex(wrappedNonce);311  const ciphertextRaw = unhex(wrappedRootKey);312  const tagRaw = unhex(wrappedTag);313  // Combine ciphertext and tag for AES-GCM (expects ciphertext||tag).314  // Use explicit ArrayBuffer allocations to satisfy TS BufferSource constraints.315  const nonceU8 = nonceRaw as unknown as Uint8Array<ArrayBuffer>;316  const combined = new Uint8Array(ciphertextRaw.length + tagRaw.length);317  combined.set(ciphertextRaw, 0);318  combined.set(tagRaw, ciphertextRaw.length);319  const raw = await crypto.subtle.decrypt(320    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },321    recoveryKey,322    combined323  );324  const decryptedHex = new TextDecoder().decode(raw);325  rootKey = unhex(decryptedHex);326}327328/**329 * Re-wrap the cached root key under a new derived key (from the user's new330 * password). Called after password change and after recovery re-key so the331 * same recovery words continue to work with the updated password — and so332 * subsequent unlocks can rehydrate rootKey via deriveRootKeyFromDerivedKey.333 *334 * recovery_* fields are intentionally not touched: those wrap rootKey under335 * the recovery words, which do not change when the password changes.336 */337export async function rewrapRootKeyForPassword(338  newDerivedKey: Uint8Array,339  _newSalt: Uint8Array340): Promise<void> {341  if (!rootKey) throw new Error("root key not set");342  const slotA = await loadSlot(SLOT_A);343  if (!slotA) throw new Error("missing slot A");344  const pwRoot = await wrapRootKeyForDerivedKey(newDerivedKey, rootKey);345  await writeSlot(SLOT_A, { ...slotA, ...pwRoot });346}347348/**349 * Change the vault encryption to a new derived key (already derived from350 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.351 * Caller is responsible for key swap and snapshot.352 */353async function reencryptVaultForNewKey(354  newKey: Uint8Array,355  slotA: StoredVault356): Promise<void> {357  const plaintext = encodePayload(entries, duressConfigured);358  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");359  await writeSlot(SLOT_A, {360    ...slotA,361    nonce,362    ciphertext,363    tag,364  });365}366367/**368 * Full password change with an already-derived key (used after recovery).369 * Unlike changePassword(), this skips re-verification of current password370 * since recovery flow has already validated the user's identity via words.371 */372export async function changePasswordWithNewKey(373  newSalt: Uint8Array,374  newKey: Uint8Array375): Promise<void> {376  const slotA = await loadSlot(SLOT_A);377  if (!slotA) throw new Error("missing slot A");378  // reencryptVaultForNewKey must receive the updated salt so that subsequent379  // UNLOCK calls with the new password can re-derive the correct key.380  await reencryptVaultForNewKey(newKey, { ...slotA, salt: hex(newSalt) });381  if (derivedKey) zeroKey(derivedKey);382  derivedKey = newKey;383  // Refresh pw_root_* under the new key so deriveRootKeyFromDerivedKey works384  // on the next unlock (H2/COR-006). If rootKey is missing — recovery flow385  // should have populated it — fall back silently rather than blowing up the386  // recovery path; migrateToRecovery will rehydrate on the next unlock.387  if (rootKey) {388    const slotAfter = await loadSlot(SLOT_A);389    if (slotAfter) {390      const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);391      await writeSlot(SLOT_A, { ...slotAfter, ...pwRoot });392    }393  }394  if (!duressConfigured) {395    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));396  }397  await stampLocalVaultMutation();398}399400// ---------------------------------------------------------------------------401// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive402// a new key after password reset without re-importing the crypto module.403// ---------------------------------------------------------------------------404405export { deriveKey, generateSalt };406407export async function loadSlotA(): Promise<StoredVault | null> {408  return loadSlot(SLOT_A);409}410411/**412 * Recover the session root key by AES-GCM-decrypting the pw_root_* fields413 * (a second wrapping of the root key under the password-derived key). This414 * is what makes GET_RECOVERY_WORDS work across lock/unlock cycles without415 * the user re-entering their recovery phrase: the same root key bytes —416 * and therefore the same 12 words — survive every unlock.417 *418 * Returns null when the pw_root fields are absent (pre-v2.12 vault that419 * hasn't been migrated yet) or when decryption fails (wrong derived key,420 * tampered ciphertext). On null the caller should fall through to421 * migrateToRecovery() to populate the fields.422 */423async function deriveRootKeyFromDerivedKey(424  derivedKeyBytes: Uint8Array,425  slotA: StoredVault,426): Promise<Uint8Array | null> {427  if (!slotA.pw_root_ciphertext || !slotA.pw_root_nonce || !slotA.pw_root_tag) {428    return null;429  }430  try {431    const rootHex = await decrypt(432      derivedKeyBytes,433      slotA.pw_root_nonce,434      slotA.pw_root_ciphertext,435      slotA.pw_root_tag,436      "pw_root_v1",437    );438    return unhex(rootHex);439  } catch {440    return null;441  }442}443444// AES-GCM-wrap rootKey under derivedKey for the pw_root_* fields. Returns the445// hex-encoded ciphertext/nonce/tag triple ready to embed in StoredVault.446async function wrapRootKeyForDerivedKey(447  derivedKeyBytes: Uint8Array,448  rootKeyBytes: Uint8Array,449): Promise<{ pw_root_ciphertext: string; pw_root_nonce: string; pw_root_tag: string }> {450  const { nonce, ciphertext, tag } = await encrypt(derivedKeyBytes, hex(rootKeyBytes), "pw_root_v1");451  return {452    pw_root_ciphertext: ciphertext,453    pw_root_nonce: nonce,454    pw_root_tag: tag,455  };456}457458/**459 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),460 * generate the root key, wrap it under the recovery words, and persist the461 * recovery escrow into slot A. The caller must show the recovery words to the462 * user before calling this — once called, those words are the vault's recovery463 * path. This runs once, on the first unlock of an upgraded vault.464 */465export async function migrateToRecovery(): Promise<void> {466  if (!derivedKey || vaultMode !== "real") {467    throw new Error("vault must be unlocked in real mode");468  }469  const slotA = await loadSlot(SLOT_A);470  if (!slotA) throw new Error("missing slot A");471  const hasRecoveryEscrow =472    !!slotA.recovery_salt && !!slotA.recovery_wrapped_root_key;473  const hasPwRoot = !!slotA.pw_root_ciphertext && !!slotA.pw_root_nonce;474  if (hasRecoveryEscrow && hasPwRoot) return; // already done475476  // If recovery escrow already exists but pw_root doesn't, we cannot recover477  // the original rootKey without the user's words. The least-surprising path478  // is to rotate to a fresh root key — the user's old recovery words become479  // invalid but we surface peach_recovery_pending so the popup re-presents480  // the words. The unlock-path callers all set that flag.481  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =482    await generateAndWrapRootKey();483  rootKey = unhex(rawRootKey);484485  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);486  const nextSlotA: StoredVault = {487    ...slotA,488    recovery_salt: recoverySalt,489    recovery_wrapped_root_key: wrappedRootKey,490    recovery_nonce: wrappedNonce,491    recovery_tag: wrappedTag,492    ...pwRoot,493  };494  await writeSlot(SLOT_A, nextSlotA);495  await stampLocalVaultMutation();496}497498/**499 * Returns the 12 recovery words for the current session's root key.500 * Must be called while the vault is unlocked. Throws if recovery has not501 * been set up (pre-v2.12 vault that hasn't migrated yet).502 */503export async function getRecoveryWords(): Promise<string[]> {504  if (!rootKey) {505    throw new Error("recovery not set up — vault may need migration");506  }507  return rootKeyToWords(rootKey);508}509510async function loadSlot(key: string): Promise<StoredVault | null> {511  const result = await chrome.storage.local.get(key);512  return (result as Record<string, StoredVault | undefined>)[key] ?? null;513}514515async function writeSlot(key: string, vault: StoredVault): Promise<void> {516  try {517    await chrome.storage.local.set({ [key]: vault });518  } catch (err) {519    throw mapStorageError(err);520  }521}522523async function stampLocalVaultMutation(): Promise<void> {524  const raw = await chrome.storage.local.get("peach_last_modified_at");525  const previous = Number(526    (raw as Record<string, unknown>).peach_last_modified_at,527  );528  const next = Math.max(529    Date.now(),530    Number.isFinite(previous) ? previous + 1 : 0,531  );532  await chrome.storage.local.set({533    peach_last_modified_at: String(next),534  });535}536537/**538 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault539 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +540 * peach_vault_b). Runs at most once per session; safe to call repeatedly.541 */542async function ensureMigrated(): Promise<void> {543  if (migrationRan) return;544  migrationRan = true;545546  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];547  const raw = await chrome.storage.local.get(keys);548  const slotAExists = raw[SLOT_A] !== undefined;549  const slotBExists = raw[SLOT_B] !== undefined;550  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;551  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;552553  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.554  if (!slotAExists && !legacyMain) return;555556  // Already migrated.557  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;558559  let didMigrate = false;560  if (legacyMain && !slotAExists) {561    await writeSlot(SLOT_A, legacyMain);562    didMigrate = true;563  }564565  // If slot B isn't set, populate it: use legacy decoy when present, else noise.566  if (!slotBExists) {567    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;568    if (legacyDecoy) {569      await writeSlot(SLOT_B, legacyDecoy);570      didMigrate = true;571    } else if (target) {572      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));573      didMigrate = true;574    }575  }576577  const toRemove: string[] = [];578  if (legacyMain) toRemove.push(LEGACY_MAIN);579  if (legacyDecoy) toRemove.push(LEGACY_DECOY);580  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);581  if (toRemove.length > 0) {582    await chrome.storage.local.remove(toRemove);583    didMigrate = true;584  }585  if (didMigrate) {586    await stampLocalVaultMutation();587  }588}589590// ---------------------------------------------------------------------------591// Padding + noise helpers592// ---------------------------------------------------------------------------593594/**595 * Pads the plaintext string with trailing spaces so that the encoded byte596 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing597 * whitespace after a complete document, so the padding is transparent on598 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.599 */600function padPlaintext(json: string): string {601  const encoded = new TextEncoder().encode(json);602  const target = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);603  const padLen = target - encoded.length;604  return json + " ".repeat(padLen);605}606607/**608 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The609 * ciphertext length matches the given byte count. Because GCM ciphertext is610 * pseudo-random, this blob is indistinguishable at rest from a real611 * encrypted vault without the key.612 *613 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto614 * spec hard limit). Chunk the fill so vaults larger than that — common once615 * the user has dozens of entries or any passkey — don't throw.616 */617const CSPRNG_CHUNK = 65_536;618619function fillRandom(buf: Uint8Array): Uint8Array {620  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {621    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);622    const chunk = new Uint8Array(end - offset);623    crypto.getRandomValues(chunk);624    buf.set(chunk, offset);625  }626  return buf;627}628629function generateNoiseBlob(ciphertextBytes: number): StoredVault {630  const salt = crypto.getRandomValues(new Uint8Array(16));631  const nonce = crypto.getRandomValues(new Uint8Array(12));632  const cipher = fillRandom(new Uint8Array(ciphertextBytes));633  const tag = crypto.getRandomValues(new Uint8Array(16));634  return {635    version: 1,636    salt: hex(salt),637    nonce: hex(nonce),638    ciphertext: hex(cipher),639    tag: hex(tag),640  };641}642643// ---------------------------------------------------------------------------644// Encrypted plaintext envelope645// ---------------------------------------------------------------------------646647type VaultPayload = {648  entries: VaultEntry[];649  duressConfigured?: boolean;650};651652type DecryptedPayload = {653  entries: VaultEntry[];654  duressConfigured: boolean;655  /**656   * True when at least one entry in the on-disk payload had a legacy shape657   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)658   * and got rewritten by normalizeEntry. The unlock path uses this to659   * persist the new shape immediately — without it, the migration would660   * re-run on every unlock and old shape never disappears from disk.661   */662  migratedFromLegacy: boolean;663};664665function encodePayload(entries: VaultEntry[], duress: boolean): string {666  const envelope: VaultPayload = { entries, duressConfigured: duress };667  return padPlaintext(JSON.stringify(envelope));668}669670function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {671  if ("created" in raw || "updated" in raw) return true;672  const flat = raw as Partial<VaultEntry>;673  if (flat.type === "passkey") return true;674  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.675  if (flat.rpId !== undefined && flat.passkey === undefined) return true;676  return false;677}678679function decodePayload(plain: string): DecryptedPayload {680  const parsed = JSON.parse(plain) as681    | Array<VaultEntry | LegacyVaultEntry>682    | VaultPayload;683684  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];685  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);686687  const normalized = rawEntries.map(normalizeEntry);688  const { merged } = mergePasskeyOnlyIntoLogins(normalized);689690  if (Array.isArray(parsed)) {691    return {692      entries: normalized,693      duressConfigured: false,694      migratedFromLegacy: true, // pre-A-2 array shape always needs save695    };696  }697698  return {699    entries: normalized,700    duressConfigured: parsed.duressConfigured === true,701    migratedFromLegacy: flatMigrationNeeded || merged,702  };703}704705/**706 * One-time consolidation pass run alongside the legacy passkey migration.707 *708 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`709 * entry per registration — so users who tested registration multiple times710 * accumulated several passkey-bearing entries for the same RP, and the711 * migration to the unified shape carried that duplication forward. This712 * pass collapses the duplicates:713 *714 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.715 *   2. Within each group, the most recently-active credential wins (prefer716 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).717 *   3. If a NON-passkey login entry exists for that RP (matched by url or718 *      additionalUrls hostname), attach the winning passkey to it and719 *      soft-delete every passkey-bearing entry in the group.720 *   4. Otherwise, soft-delete every loser in the group, keeping the winner721 *      as a passkey-only entry.722 *723 * Soft delete (deletedAt) rather than splice — the trash flow keeps the724 * record around for retention and the user can restore if the heuristic725 * picked wrong. The `merged` flag goes back to decodePayload so the726 * unlock path persists the consolidated shape immediately.727 */728function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {729  let merged = false;730  const now = Date.now();731732  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);733  const byRpId = new Map<string, VaultEntry[]>();734  for (const e of candidates) {735    const rpId = e.passkey!.rpId;736    const list = byRpId.get(rpId);737    if (list) list.push(e);738    else byRpId.set(rpId, [e]);739  }740741  for (const [rpId, group] of byRpId) {742    if (group.length === 0) continue;743    group.sort((a, b) => {744      const aT =745        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;746      const bT =747        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;748      return bT - aT;749    });750    const winner = group[0]!;751752    // Look for a NON-passkey login entry matching this RP. Use the URL753    // hostname rather than a substring search so a "login.foo.com" entry754    // doesn't accidentally absorb a passkey for "foo.com".755    const loginTarget = entries.find((e) => {756      if (e.deletedAt || e.passkey || e.type !== "login") return false;757      if (e.url && extractDomain(e.url) === rpId) return true;758      for (const extra of e.additionalUrls ?? []) {759        if (extractDomain(extra) === rpId) return true;760      }761      return false;762    });763764    if (loginTarget) {765      loginTarget.passkey = winner.passkey;766      loginTarget.modifiedAt = now;767      for (const dup of group) {768        if (!dup.deletedAt) {769          dup.deletedAt = now;770          dup.modifiedAt = now;771        }772      }773      merged = true;774    } else if (group.length > 1) {775      for (let i = 1; i < group.length; i += 1) {776        const dup = group[i]!;777        if (!dup.deletedAt) {778          dup.deletedAt = now;779          dup.modifiedAt = now;780        }781      }782      merged = true;783    }784  }785786  return { merged };787}788789// Validates that a `StoredVault` record on disk has the field shape we expect790// before treating an AES-GCM decrypt success as a clean unlock. AES-GCM only791// authenticates the static VERSION_BYTE AAD, not the `version` field on the792// outer record — a corrupted `version: 42` slot still decrypts cleanly. This793// check surfaces that as `corrupted` (M7/COR-007) so the popup can offer a794// history-restore path instead of falsely reporting "wrong password".795function hasValidVaultSchema(v: unknown): v is StoredVault {796  if (typeof v !== "object" || v === null) return false;797  const r = v as Record<string, unknown>;798  return (799    r.version === 1 &&800    typeof r.salt === "string" &&801    typeof r.nonce === "string" &&802    typeof r.ciphertext === "string" &&803    typeof r.tag === "string"804  );805}806807export type TryDecryptResult =808  | { status: "ok"; payload: DecryptedPayload }809  | { status: "wrong_key" }810  | { status: "corrupted"; reason: string };811812// Exported solely for `unlockFromCodex` below, which needs to verify a813// supplied passphrase against a slotA blob *without* committing anything to814// storage. Keep this internal otherwise — callers crossing the messages.ts815// boundary should always go through `unlock` / `verifyMainPassword` so the816// constant-time invariants in Projects.md §3.4 are not bypassed.817export async function tryDecryptSlot(818  vault: StoredVault,819  key: Uint8Array,820  aadTag?: string,821): Promise<DecryptedPayload | null> {822  const res = await tryDecryptSlotResult(vault, key, aadTag);823  return res.status === "ok" ? res.payload : null;824}825826export async function tryDecryptSlotResult(827  vault: StoredVault,828  key: Uint8Array,829  aadTag?: string,830): Promise<TryDecryptResult> {831  if (!hasValidVaultSchema(vault)) {832    return { status: "corrupted", reason: "invalid_slot_schema" };833  }834  let plain: string;835  try {836    plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag, aadTag);837  } catch {838    return { status: "wrong_key" };839  }840  try {841    const parsed = JSON.parse(plain) as unknown;842    if (typeof parsed !== "object" || parsed === null) {843      return { status: "corrupted", reason: "invalid_payload_shape" };844    }845    if (!Array.isArray(parsed) && !Array.isArray((parsed as VaultPayload).entries)) {846      return { status: "corrupted", reason: "invalid_payload_shape" };847    }848    return { status: "ok", payload: decodePayload(plain) };849  } catch {850    return { status: "corrupted", reason: "invalid_payload_json" };851  }852}853854// ---------------------------------------------------------------------------855// Core operations856// ---------------------------------------------------------------------------857858function buildDomainIndexSync(all: VaultEntry[]): void {859  domainIndex = new Map();860861  for (const entry of all) {862    if (entry.deletedAt) continue;863    if (entry.type === "note" || entry.type === "card") continue;864865    // An entry can match multiple sites via `additionalUrls`. Index each866    // hostname so `queryDomain` returns the entry regardless of which alias867    // the user is currently visiting. Dedupe per-entry so a single entry868    // never appears twice in one bucket.869    const domains = new Set<string>();870    const primary = extractDomain(entry.url ?? "");871    if (primary) domains.add(primary);872    for (const extra of entry.additionalUrls ?? []) {873      const d = extractDomain(extra);874      if (d) domains.add(d);875    }876    if (domains.size === 0) continue;877878    for (const domain of domains) {879      const bucket = domainIndex.get(domain) ?? [];880      bucket.push(entry);881      domainIndex.set(domain, bucket);882    }883  }884}885886export async function createVault(password: string): Promise<void> {887  await ensureMigrated();888889  const salt = generateSalt();890  derivedKey = await deriveKey(password, salt);891  entries = [];892  vaultMode = "real";893  duressConfigured = false;894  buildDomainIndexSync(entries);895896  // Generate and wrap the root key for recovery word escrow.897  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =898    await generateAndWrapRootKey();899  rootKey = unhex(rawRootKey);900901  const plaintext = encodePayload(entries, duressConfigured);902  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");903904  // Second wrapping of rootKey under derivedKey so future unlocks can rehydrate905  // it without recovery words (H2/COR-006).906  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);907908  const slotA: StoredVault = {909    version: 1,910    salt: hex(salt),911    nonce,912    ciphertext,913    tag,914    recovery_salt: recoverySalt,915    recovery_wrapped_root_key: wrappedRootKey,916    recovery_nonce: wrappedNonce,917    recovery_tag: wrappedTag,918    ...pwRoot,919  };920  await writeSlot(SLOT_A, slotA);921922  // Matching-size noise blob for slot B, so a fresh install shows two923  // similarly-shaped records regardless of whether duress is ever set up.924  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));925  await stampLocalVaultMutation();926927  touchAutoLock();928}929930export async function hasVault(): Promise<boolean> {931  await ensureMigrated();932  return (await loadSlot(SLOT_A)) !== null;933}934935/**936 * Returns whether the real vault has recorded that a duress passphrase is937 * configured. Reliable only after a real-mode unlock — before unlock or in938 * decoy mode, returns false because we cannot read slot A's plaintext.939 */940export async function isDuressEnabled(): Promise<boolean> {941  return vaultMode === "real" && duressConfigured;942}943944export async function setupDuress(duressPassword: string): Promise<void> {945  if (!derivedKey || vaultMode !== "real") {946    throw new Error("vault must be unlocked in real mode");947  }948  await ensureMigrated();949950  // Encrypt the decoy payload (empty entries) under the duress key. Pad to951  // at least the current slot A ciphertext size so the two slots look the952  // same at rest at setup time.953  const slotA = await loadSlot(SLOT_A);954  if (!slotA) throw new Error("missing slot A");955956  const decoySalt = generateSalt();957  const decoyKey = await deriveKey(duressPassword, decoySalt);958  try {959    // Pad the decoy plaintext to match slot A's ciphertext length (minus the960    // 16-byte tag) so encrypted sizes align exactly.961    const targetBytes = slotA.ciphertext.length / 2;962    let plaintext = encodePayload([], false);963    const encoded = new TextEncoder().encode(plaintext);964    if (encoded.length < targetBytes) {965      plaintext = plaintext + " ".repeat(targetBytes - encoded.length);966    }967    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext, "slot_b_v1");968    await writeSlot(SLOT_B, {969      version: 1,970      salt: hex(decoySalt),971      nonce,972      ciphertext,973      tag,974    });975  } finally {976    zeroKey(decoyKey);977  }978979  // Re-encrypt slot A with the duressConfigured flag set to true. No external980  // flag is written — the bit lives inside the encrypted envelope.981  duressConfigured = true;982  await persistSlotA();983}984985export async function disableDuress(): Promise<void> {986  if (!derivedKey || vaultMode !== "real") {987    throw new Error("vault must be unlocked in real mode");988  }989  await ensureMigrated();990991  // Overwrite slot B with fresh noise that matches slot A's size. The slot992  // remains occupied so its mere existence does not signal that duress was993  // ever configured.994  const slotA = await loadSlot(SLOT_A);995  if (slotA) {996    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));997  }998999  duressConfigured = false;1000  await persistSlotA();1001}10021003// L14/COR-015: this function intentionally checks slot A (real vault) only.1004// A duress-mode user attempting changePassword is rejected earlier by the1005// vaultMode === 'decoy' check at changePassword. The real-vs-decoy1006// distinction here is by design — duress users cannot rotate the real1007// password by guessing it, and the asymmetry in error timing is acceptable1008// because duress mode is opt-in and the user knows their own decoy key.1009export async function verifyMainPassword(password: string): Promise<boolean> {1010  await ensureMigrated();1011  const slotA = await loadSlot(SLOT_A);1012  if (!slotA) return false;1013  const key = await deriveKey(password, unhex(slotA.salt));1014  try {1015    return (await tryDecryptSlot(slotA, key, "slot_a_v1")) !== null;1016  } finally {1017    zeroKey(key);1018  }1019}10201021export async function unlock(password: string): Promise<boolean> {1022  await ensureMigrated();10231024  // Load both slots in parallel. Both always exist post-migration; the1025  // parallel load and the parallel derivation below keep wall-clock time1026  // constant regardless of which slot's passphrase (real or decoy) the1027  // caller supplied, so a timing side channel cannot distinguish the1028  // duress path from the real-unlock path (DUR-002).1029  const [slotA, slotB] = await Promise.all([1030    loadSlot(SLOT_A),1031    loadSlot(SLOT_B),1032  ]);10331034  // Derive both keys in parallel, unconditionally.1035  const [keyA, keyB] = await Promise.all([1036    slotA ? deriveKey(password, unhex(slotA.salt)) : Promise.resolve(null),1037    slotB ? deriveKey(password, unhex(slotB.salt)) : Promise.resolve(null),1038  ]);10391040  // Attempt both decrypts in parallel. The Result variant lets us distinguish1041  // 'wrong_key' from 'corrupted' so callers can offer a history-restore path1042  // when a slot's schema is invalid even though AES-GCM authenticated (M7).1043  const [resultA, resultB] = await Promise.all([1044    keyA && slotA1045      ? tryDecryptSlotResult(slotA, keyA, "slot_a_v1")1046      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1047    keyB && slotB1048      ? tryDecryptSlotResult(slotB, keyB, "slot_b_v1")1049      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1050  ]);1051  const payloadA = resultA.status === "ok" ? resultA.payload : null;1052  const payloadB = resultB.status === "ok" ? resultB.payload : null;10531054  if (payloadA && keyA) {1055    derivedKey = keyA;1056    void saveSessionKey(keyA, "real");1057    vaultMode = "real";1058    entries = payloadA.entries;1059    duressConfigured = payloadA.duressConfigured;1060    buildDomainIndexSync(entries);1061    await loadSettings();1062    touchAutoLock();1063    if (keyB) zeroKey(keyB);10641065    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).1066    if (slotA) {1067      const needsRecoverySetup =1068        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;1069      if (!needsRecoverySetup) {1070        rootKey = await deriveRootKeyFromDerivedKey(keyA, slotA);1071      }1072      // Pre-v2.12 vault, or a vault whose pw_root_* fields haven't been written1073      // yet: generate the recovery escrow and seed pw_root_* so subsequent1074      // unlocks can rehydrate rootKey without the user's recovery words.1075      // Best-effort — a failure here must not block unlock.1076      if (needsRecoverySetup || !rootKey) {1077        try {1078          await migrateToRecovery();1079          if (needsRecoverySetup) {1080            // String "1" matches the popup's existing peach_recovery_pending1081            // check; surfaced so the user re-confirms recovery words after a1082            // legacy-vault unlock (H2/COR-006).1083            await chrome.storage.local.set({ peach_recovery_pending: '1' });1084          }1085        } catch (migrationErr) {1086          console.warn('[Peach] recovery migration failed:', migrationErr);1087        }1088      }1089    }10901091    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat1092    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so1093    // the next save uses the new format and migration doesn't re-run forever.1094    if (payloadA.migratedFromLegacy) {1095      await persistSlotA();1096    }1097    return true;1098  }10991100  if (payloadB && keyB) {1101    derivedKey = keyB;1102    void saveSessionKey(keyB, "decoy");1103    vaultMode = "decoy";1104    entries = payloadB.entries;1105    duressConfigured = false;1106    buildDomainIndexSync(entries);1107    await loadSettings();1108    touchAutoLock();1109    if (keyA) zeroKey(keyA);1110    return true;1111  }11121113  if (keyA) zeroKey(keyA);1114  if (keyB) zeroKey(keyB);1115  // Surface corruption (good AES tag, broken schema) distinctly from1116  // wrong-password so the popup can offer history-restore (M7/COR-007).1117  if (resultA.status === "corrupted") {1118    lastUnlockFailureReason = resultA.reason;1119  } else if (resultB.status === "corrupted") {1120    lastUnlockFailureReason = resultB.reason;1121  }1122  return false;1123}11241125// ---------------------------------------------------------------------------1126// MV3 service worker session key — survives SW restarts within a browser session1127// ---------------------------------------------------------------------------11281129// chrome.storage.session is cleared on browser close; survives SW restart.1130// Firefox does not implement it (persistent background script, no restart risk).1131const SESSION_KEY_STORAGE = "peach_session_dk";11321133async function saveSessionKey(key: Uint8Array, mode: "real" | "decoy"): Promise<void> {1134  if (!chrome.storage.session) return;1135  await chrome.storage.session.set({ [SESSION_KEY_STORAGE]: { keyHex: hex(key), mode } });1136}11371138async function clearSessionKey(): Promise<void> {1139  if (!chrome.storage.session) return;1140  await chrome.storage.session.remove(SESSION_KEY_STORAGE);1141}11421143/**1144 * On SW restart, re-derive all in-memory vault state from the session key1145 * stored in chrome.storage.session (set at unlock time, cleared on lock).1146 * Returns true if the vault was restored, false if no session key was found.1147 * No-ops if the vault is already unlocked.1148 */1149export async function restoreFromSession(): Promise<boolean> {1150  if (derivedKey !== null) return true;1151  if (!chrome.storage.session) return false;1152  const stored = await chrome.storage.session.get(SESSION_KEY_STORAGE);1153  const entry = stored[SESSION_KEY_STORAGE] as { keyHex: string; mode: "real" | "decoy" } | undefined;1154  if (!entry?.keyHex) return false;11551156  const key = unhex(entry.keyHex);1157  const slotKey = entry.mode === "real" ? SLOT_A : SLOT_B;1158  const aadTag = entry.mode === "real" ? "slot_a_v1" : "slot_b_v1";1159  const slot = await loadSlot(slotKey);1160  if (!slot) { await clearSessionKey(); return false; }11611162  const result = await tryDecryptSlotResult(slot, key, aadTag);1163  if (result.status !== "ok") { await clearSessionKey(); return false; }11641165  derivedKey = key;1166  vaultMode = entry.mode;1167  entries = result.payload.entries;1168  duressConfigured = entry.mode === "real" ? result.payload.duressConfigured : false;1169  buildDomainIndexSync(entries);1170  await loadSettings();1171  touchAutoLock();11721173  if (entry.mode === "real" && slot.recovery_salt && slot.recovery_wrapped_root_key) {1174    try { rootKey = await deriveRootKeyFromDerivedKey(key, slot); } catch { /* non-fatal */ }1175  }11761177  return true;1178}11791180export function lock(): void {1181  void clearSessionKey();1182  if (derivedKey) {1183    zeroKey(derivedKey);1184    derivedKey = null;1185  }1186  if (rootKey) {1187    rootKey.fill(0);1188    rootKey = null;1189  }11901191  entries = [];1192  domainIndex = new Map();1193  vaultMode = "real";1194  duressConfigured = false;1195  clearAutoLock();1196}11971198export function isUnlocked(): boolean {1199  return derivedKey !== null;1200}12011202// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).1203// Used by Android biometric unlock — the key was derived on the native side.1204export async function unlockWithKey(1205  masterKeyHex: string,1206): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {1207  try {1208    const masterKeyBytes = unhex(masterKeyHex);1209    const stored = await loadSlot(SLOT_A);1210    if (!stored) return { ok: false, error: "invalid_key" };1211    const payload = await tryDecryptSlot(stored, masterKeyBytes, "slot_a_v1");1212    if (!payload) return { ok: false, error: "invalid_key" };12131214    derivedKey = masterKeyBytes;1215    vaultMode = "real";1216    entries = payload.entries;1217    duressConfigured = payload.duressConfigured;1218    buildDomainIndexSync(entries);1219    await loadSettings();1220    touchAutoLock();1221    return { ok: true, entries };1222  } catch {1223    return { ok: false, error: "invalid_key" };1224  }1225}12261227/**1228 * Restore the vault from a parsed Peach Codex payload.1229 *1230 * The supplied passphrase is verified against the Codex's slotA *before* any1231 * storage mutation — wrong passphrases return without touching disk. On1232 * success, the current vault state is snapshotted (reason: "restore") so the1233 * user can revert via Settings -> Vault History, then slotA/slotB are written1234 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1235 * regular `unlock()` follows so the rest of the SW sees the standard1236 * post-unlock state.1237 *1238 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1239 * printed by a Pro user must always be restorable, even on a free device.1240 *1241 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1242 * is an explicit user action with a known passphrase, not a passive lock1243 * screen, so the duress-vs-real timing side channel does not apply.1244 */1245export async function unlockFromCodex(params: {1246  slotA: string; // JSON string of StoredVault for slot A1247  slotB: string | null; // JSON string of StoredVault for slot B, or null1248  timestamp: number;1249  masterPassphrase: string;1250}): Promise<1251  | { ok: true; entryCount: number }1252  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1253> {1254  let parsedSlotA: StoredVault;1255  let parsedSlotB: StoredVault | null = null;1256  try {1257    const rawA = JSON.parse(params.slotA) as unknown;1258    if (!hasValidVaultSchema(rawA)) {1259      return { ok: false, reason: "invalid_slot_format" };1260    }1261    parsedSlotA = rawA;1262    if (params.slotB !== null) {1263      const rawB = JSON.parse(params.slotB) as unknown;1264      if (!hasValidVaultSchema(rawB)) {1265        return { ok: false, reason: "invalid_slot_format" };1266      }1267      parsedSlotB = rawB;1268    }1269  } catch {1270    return { ok: false, reason: "invalid_slot_format" };1271  }12721273  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1274  // If this fails the on-disk vault is left completely untouched.1275  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1276  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey, "slot_a_v1");1277  if (!trialPayload) {1278    zeroKey(trialKey);1279    return { ok: false, reason: "wrong_passphrase" };1280  }1281  // Drop the trial key; `unlock()` below derives its own copy. Holding two1282  // copies of the master key in memory at once would be a needless surface.1283  zeroKey(trialKey);12841285  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1286  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1287  await takeVaultSnapshot("restore");12881289  await writeSlot(SLOT_A, parsedSlotA);1290  if (parsedSlotB) {1291    await writeSlot(SLOT_B, parsedSlotB);1292  } else {1293    // Slot B must always exist with ciphertext length matching slot A1294    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1295    // configured); regenerate noise to satisfy the invariant.1296    await writeSlot(1297      SLOT_B,1298      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1299    );1300  }1301  await chrome.storage.local.set({1302    peach_last_modified_at: String(params.timestamp),1303  });13041305  // Standard unlock path so domain index, settings, auto-lock, and1306  // recovery-key state all populate exactly as a normal unlock would.1307  const ok = await unlock(params.masterPassphrase);1308  if (!ok) {1309    // Should be unreachable — we just verified the passphrase against the1310    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1311    // simple if some race or storage anomaly intrudes.1312    return { ok: false, reason: "wrong_passphrase" };1313  }1314  return { ok: true, entryCount: getEntries().length };1315}13161317// Returns the current in-memory derived key as a hex string.1318// Used by the biometric enable flow in Settings to store the key in Android Keystore.1319export function getDerivedKeyHex(): string {1320  if (!derivedKey) throw new Error("vault_not_unlocked");1321  return hex(derivedKey);1322}13231324export function getEntries(): VaultEntry[] {1325  if (!derivedKey) throw new Error("vault_locked");1326  return entries.filter((entry) => !entry.deletedAt);1327}13281329const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;13301331export function getIdentities(): SavedIdentity[] {1332  if (!derivedKey) throw new Error("vault_locked");1333  const active = entries.filter((e) => !e.deletedAt && e.type === "login");13341335  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1336  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();13371338  for (const entry of active) {1339    const domain = extractDomain(entry.url ?? "") ?? "";1340    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;13411342    function tally(map: Map<string, { lastUsed: number; count: number; domain: string }>, v: string) {1343      const existing = map.get(v);1344      if (!existing || modifiedAt > existing.lastUsed) {1345        map.set(v, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1346      } else {1347        map.set(v, { ...existing, count: existing.count + 1 });1348      }1349    }13501351    const uVal = entry.username?.trim();1352    if (uVal) tally(EMAIL_RE.test(uVal) ? emailMap : usernameMap, uVal);13531354    // Also count the dedicated email field — entries created via the inline1355    // popup new-entry form store email here rather than in username.1356    const eVal = entry.email?.trim();1357    if (eVal) tally(emailMap, eVal);1358  }13591360  const toIdentity = (type: "email" | "username") =>1361    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1362      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1363    });13641365  return [1366    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1367    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1368  ];1369}13701371export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1372  if (!derivedKey) throw new Error("vault_locked");13731374  const now = Date.now();1375  const added: VaultEntry[] = [];1376  for (const draft of drafts) {1377    try {1378      const sanitized = sanitizeEntryDraft(draft);1379      const nextEntry: VaultEntry = {1380        ...sanitized,1381        id: crypto.randomUUID(),1382        createdAt: now,1383        modifiedAt: now,1384      };1385      entries.push(nextEntry);1386      added.push(nextEntry);1387    } catch {1388      // Skip invalid drafts silently — caller can see diff between input and output length1389    }1390  }13911392  if (added.length > 0) {1393    await persistEntries();1394  }13951396  return added;1397}13981399/**1400 * Restore a set of entries in full fidelity — preserves id, createdAt,1401 * modifiedAt, and every other field unlike `batchAddEntries` which1402 * assigns fresh identity. Used by restore paths where the backup is the1403 * authoritative source of truth. `mode` controls how entries already in1404 * the vault are treated.1405 */1406export async function restoreEntries(1407  incoming: VaultEntry[],1408  mode: "replace" | "merge"1409): Promise<{ added: number; updated: number; skipped: number }> {1410  if (!derivedKey) throw new Error("vault_locked");14111412  // A replace with zero incoming entries is almost certainly a caller bug1413  // (e.g. an import that silently dropped everything). Refuse rather than1414  // wipe the vault.1415  if (mode === "replace" && incoming.length === 0) {1416    throw new Error("refuse to replace with empty entry set");1417  }14181419  let added = 0;1420  let updated = 0;1421  let skipped = 0;14221423  if (mode === "replace") {1424    entries = [];1425    for (const entry of incoming) {1426      const normalized = sanitizeRestoredEntry(entry);1427      if (!normalized) {1428        skipped += 1;1429        continue;1430      }1431      entries.push(normalized);1432      added += 1;1433    }1434  } else {1435    const byId = new Map<string, VaultEntry>();1436    for (const e of entries) byId.set(e.id, e);14371438    for (const entry of incoming) {1439      const normalized = sanitizeRestoredEntry(entry);1440      if (!normalized) {1441        skipped += 1;1442        continue;1443      }1444      const existing = byId.get(normalized.id);1445      if (!existing) {1446        entries.push(normalized);1447        added += 1;1448      } else if (normalized.modifiedAt > existing.modifiedAt) {1449        const idx = entries.findIndex((e) => e.id === existing.id);1450        if (idx !== -1) {1451          entries[idx] = normalized;1452          updated += 1;1453        }1454      } else {1455        skipped += 1;1456      }1457    }1458  }14591460  await persistEntries("import");1461  return { added, updated, skipped };1462}14631464/**1465 * Validate + normalise a VaultEntry coming from a backup. Accepts1466 * anything structurally sound; keeps all metadata verbatim. Returns1467 * null for values the live vault shouldn't accept (e.g., missing title).1468 */1469function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1470  if (!entry || typeof entry !== "object") return null;1471  const title = typeof entry.title === "string" ? entry.title.trim() : "";1472  if (!title) return null;1473  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1474  const now = Date.now();1475  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1476  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;14771478  // Migrate legacy passkey shape on import (same logic as on-disk read).1479  // Imports from pre-v2.11.0 vault backups still carry type:"passkey" with1480  // flat fields; bring them into the new shape immediately.1481  const migrated: VaultEntry = migrateLegacyPasskey(1482    {1483      ...entry,1484      id,1485      title,1486      type: entry.type ?? "login",1487      createdAt,1488      modifiedAt,1489    },1490    entry1491  );14921493  // Login entries from backup must carry a password (or be passkey-only,1494  // in which case the passkey sub-object replaces the password requirement).1495  if (1496    migrated.type === "login" &&1497    !migrated.passkey &&1498    typeof migrated.password !== "string"1499  ) {1500    return null;1501  }1502  if (migrated.passkey && !migrated.passkey.credentialId) {1503    return null;1504  }15051506  return migrated;1507}15081509// Soft-deletes every non-deleted entry in one call. The literal string1510// argument is a tripwire against accidental calls — any caller that would1511// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1512// to a boolean.1513export async function clearVaultEntries(1514  confirmation: "REPLACE_ALL"1515): Promise<void> {1516  if (confirmation !== "REPLACE_ALL") {1517    throw new Error("clearVaultEntries requires explicit confirmation");1518  }1519  if (!derivedKey) throw new Error("vault_locked");1520  for (const entry of entries) {1521    if (!entry.deletedAt) {1522      entry.deletedAt = Date.now();1523      entry.modifiedAt = Date.now();1524    }1525  }1526  await persistEntries();1527}15281529export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1530  if (!derivedKey) throw new Error("vault_locked");15311532  const sanitized = sanitizeEntryDraft(entry);1533  const now = Date.now();1534  const nextEntry: VaultEntry = {1535    ...sanitized,1536    id: crypto.randomUUID(),1537    createdAt: now,1538    modifiedAt: now,1539    // Stamp passwordChangedAt at creation so age tracking is accurate from1540    // day one. Non-login entries carry the same stamp harmlessly.1541    passwordChangedAt: sanitized.password ? now : undefined,1542  };15431544  // Clone-and-replace so a failed persist rolls back the in-memory state1545  // (COR-008). Without this, a quota-full or storage error leaves the1546  // module-level `entries` ahead of what's actually on disk.1547  const rollback = entries;1548  entries = [...entries, nextEntry];1549  try {1550    await persistEntries();1551  } catch (err) {1552    entries = rollback;1553    throw err;1554  }15551556  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1557  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1558    checkBreach(nextEntry.password).then(async (count) => {1559      if (count > 0) {1560        const idx = entries.findIndex((e) => e.id === nextEntry.id);1561        if (idx !== -1) {1562          entries[idx] = { ...entries[idx], breach: count, modifiedAt: Date.now() };1563          await persistEntries();1564        }1565      }1566    });1567  }15681569  return nextEntry;1570}15711572export async function updateEntry(1573  id: string,1574  patch: Partial<VaultEntry>1575): Promise<void> {1576  if (!derivedKey) throw new Error("vault_locked");15771578  const idx = entries.findIndex((entry) => entry.id === id);1579  if (idx === -1 || entries[idx].deletedAt) {1580    throw new Error("entry_not_found");1581  }15821583  const current = entries[idx];1584  const sanitizedPatch = sanitizeEntryPatch(patch);1585  // Refresh passwordChangedAt only when the password value actually changes —1586  // a metadata-only update (title, tags, etc.) must not reset the age clock1587  // that drives the Pro health rotation recommendation.1588  const passwordChanged =1589    "password" in patch &&1590    typeof sanitizedPatch.password === "string" &&1591    sanitizedPatch.password !== current.password;1592  entries[idx] = {1593    ...current,1594    ...sanitizedPatch,1595    modifiedAt: Date.now(),1596    ...(passwordChanged ? { passwordChangedAt: Date.now() } : {}),1597  };15981599  // A login entry must carry SOMETHING fillable — either a password or a1600  // passkey credential. An empty-password login with no passkey is dead1601  // state. Permit the password-clear when a passkey is attached (that's1602  // the legitimate "drop the password, keep the passkey" path).1603  if (1604    entries[idx].type === "login" &&1605    !entries[idx].password &&1606    !entries[idx].passkey1607  ) {1608    entries[idx] = current;1609    throw new Error("password or passkey required");1610  }16111612  // Card entries must retain their card number1613  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1614    entries[idx] = current;1615    throw new Error("card number is required for card entries");1616  }16171618  await persistEntries();16191620  // Breach check when password changes: clear immediately so old breach doesn't linger,1621  // then async-check the new password and only re-set if it's also found.1622  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1623    entries[idx] = { ...entries[idx], breach: undefined };1624    await persistEntries();1625    const password = entries[idx].password;1626    if (password) {1627      checkBreach(password).then(async (count) => {1628        const i = entries.findIndex((e) => e.id === id);1629        if (i !== -1 && count > 0) {1630          entries[i] = { ...entries[i], breach: count, modifiedAt: Date.now() };1631          await persistEntries();1632        }1633      });1634    }1635  }1636}16371638export async function deleteEntry(id: string): Promise<void> {1639  if (!derivedKey) throw new Error("vault_locked");16401641  const entry = entries.find((item) => item.id === id);1642  if (!entry || entry.deletedAt) {1643    throw new Error("entry_not_found");1644  }16451646  entry.deletedAt = Date.now();1647  entry.modifiedAt = Date.now();1648  await persistEntries();1649}16501651export function getDeletedEntries(): VaultEntry[] {1652  if (!derivedKey) return [];1653  return entries.filter((e) => !!e.deletedAt);1654}16551656export async function restoreEntry(id: string): Promise<void> {1657  if (!derivedKey) throw new Error("vault_locked");1658  const entry = entries.find((e) => e.id === id && e.deletedAt);1659  if (!entry) throw new Error("deleted entry not found");1660  delete entry.deletedAt;1661  entry.modifiedAt = Date.now();1662  await persistEntries();1663  buildDomainIndexSync(entries);1664}16651666export async function purgeEntry(id: string): Promise<void> {1667  if (!derivedKey) throw new Error("vault_locked");1668  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);1669  if (idx === -1) throw new Error("deleted entry not found");1670  entries.splice(idx, 1);1671  await persistEntries();1672}16731674/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */1675export async function purgeExpiredTrash(retentionMs: number): Promise<number> {1676  if (!derivedKey || retentionMs === 0) return 0;1677  const cutoff = Date.now() - retentionMs;1678  const before = entries.length;1679  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));1680  const purged = before - entries.length;1681  if (purged > 0) await persistEntries();1682  return purged;1683}16841685export function queryDomain(domain: string): VaultEntry[] {1686  return domainIndex.get(domain) ?? [];1687}16881689export function getEntryById(id: string): VaultEntry | undefined {1690  return entries.find((entry) => entry.id === id && !entry.deletedAt);1691}16921693async function persistSlotA(1694  reason: VaultSnapshot["reason"] = "local_save",1695): Promise<StoredVault> {1696  if (!derivedKey) throw new Error("vault_locked");16971698  const slotA = await loadSlot(SLOT_A);1699  if (!slotA) throw new Error("missing slot A");17001701  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures1702  // are swallowed by takeVaultSnapshot — history must never block a save.1703  await takeVaultSnapshot(reason);17041705  const plaintext = encodePayload(entries, duressConfigured);1706  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");17071708  // Preserve existing recovery escrow fields.1709  const nextSlotA: StoredVault = {1710    ...slotA,1711    nonce,1712    ciphertext,1713    tag,1714    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot1715  };1716  await writeSlot(SLOT_A, nextSlotA);17171718  // When duress is not configured, keep slot B size-matched with slot A by1719  // regenerating its noise. When duress IS configured, leave slot B alone —1720  // we don't have the decoy key to re-encrypt it. This is a known trade-off.1721  if (!duressConfigured) {1722    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1723  }1724  await stampLocalVaultMutation();17251726  return nextSlotA;1727}17281729async function persistSlotB(): Promise<void> {1730  if (!derivedKey) throw new Error("vault_locked");17311732  const slotB = await loadSlot(SLOT_B);1733  if (!slotB) throw new Error("missing slot B");17341735  // Decoy plaintext always reports duressConfigured=false — the decoy user1736  // sees a clean slate.1737  const plaintext = encodePayload(entries, false);1738  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_b_v1");17391740  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });1741  await stampLocalVaultMutation();1742}17431744async function persistEntries(1745  reason: VaultSnapshot["reason"] = "local_save",1746): Promise<void> {1747  if (!derivedKey) throw new Error("vault_locked");17481749  if (vaultMode === "decoy") {1750    await persistSlotB();1751  } else {1752    await persistSlotA(reason);1753  }1754  buildDomainIndexSync(entries);1755}17561757function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {1758  if (!Array.isArray(values)) return undefined;1759  const seen = new Set<string>();1760  const cleaned: string[] = [];1761  for (const value of values) {1762    const trimmed = typeof value === "string" ? value.trim() : "";1763    if (!trimmed) continue;1764    const key = trimmed.toLowerCase();1765    if (seen.has(key)) continue;1766    seen.add(key);1767    cleaned.push(trimmed);1768  }1769  return cleaned.length > 0 ? cleaned : undefined;1770}17711772function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {1773  const title = entry.title.trim();1774  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new1775  // model is "any entry, optionally with a passkey sub-object." Imports and1776  // any in-flight callers from before v2.11.0 keep working without throwing.1777  let type: VaultEntry["type"] = entry.type ?? "login";1778  if (type === "passkey") type = "login";17791780  if (!title) throw new Error("title is required");1781  // A login entry needs either a password or a passkey credential to be useful.1782  // Passkey-only entries (passkey present, no password) are valid.1783  if (type === "login" && !entry.password?.trim() && !entry.passkey) {1784    throw new Error("password or passkey required");1785  }1786  if (entry.passkey) {1787    const p = entry.passkey;1788    if (1789      !p.rpId ||1790      !p.credentialId ||1791      !p.publicKeyCose ||1792      !p.userHandle ||1793      !p.userName ||1794      (p.algorithm !== -7 && p.algorithm !== -8)1795    ) {1796      throw new Error("invalid passkey credential");1797    }1798  }1799  if (type === "card") {1800    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {1801      throw new Error("cardholder name and card number are required");1802    }1803  }18041805  return {1806    type,1807    title,1808    username: type === "login" ? entry.username?.trim() ?? "" : undefined,1809    email: type === "login" ? entry.email?.trim() || undefined : undefined,1810    password: type === "login" ? entry.password : undefined,1811    url: type === "login" ? entry.url?.trim() ?? "" : undefined,1812    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,1813    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,1814    notes: entry.notes?.trim() || undefined,1815    breach: type === "login" ? entry.breach : undefined,1816    canary: type === "login" ? entry.canary : undefined,1817    tags: entry.tags,1818    passkey: entry.passkey,1819    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,1820    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,1821    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,1822    expYear: type === "card" ? entry.expYear?.trim() : undefined,1823    cvc: type === "card" ? entry.cvc?.trim() : undefined,1824    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,1825    network: type === "card" ? entry.network : undefined,1826    last4: type === "card" ? entry.last4?.trim() : undefined,1827  };1828}18291830function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {1831  const nextPatch: Partial<VaultEntry> = {};18321833  if ("type" in patch) {1834    nextPatch.type = patch.type;1835  }18361837  if ("title" in patch) {1838    const title = patch.title?.trim() ?? "";1839    if (!title) throw new Error("title is required");1840    nextPatch.title = title;1841  }18421843  if ("url" in patch) {1844    nextPatch.url = patch.url?.trim() ?? "";1845  }18461847  if ("additionalUrls" in patch) {1848    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];1849    const seen = new Set<string>();1850    const cleaned: string[] = [];1851    for (const value of incoming) {1852      const trimmed = typeof value === "string" ? value.trim() : "";1853      if (!trimmed) continue;1854      const key = trimmed.toLowerCase();1855      if (seen.has(key)) continue;1856      seen.add(key);1857      cleaned.push(trimmed);1858    }1859    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;1860  }18611862  if ("username" in patch) {1863    nextPatch.username = patch.username?.trim() ?? "";1864  }18651866  if ("email" in patch) {1867    nextPatch.email = patch.email?.trim() || undefined;1868  }18691870  if ("password" in patch) {1871    const password = patch.password ?? "";1872    // Allow clearing a password (e.g. on passkey-only entries)1873    nextPatch.password = password || undefined;1874  }18751876  if ("totp" in patch) {1877    nextPatch.totp = patch.totp?.trim() || undefined;1878  }18791880  if ("notes" in patch) {1881    nextPatch.notes = patch.notes?.trim() || undefined;1882  }18831884  if ("breach" in patch) {1885    nextPatch.breach = patch.breach;1886  }18871888  if ("canary" in patch) {1889    nextPatch.canary = patch.canary;1890  }18911892  if ("passkey" in patch) {1893    // Passkey sub-object replaces all the legacy flat passkey fields. A1894    // patch with `passkey: undefined` removes the credential from the entry1895    // (used by "remove passkey" without deleting the whole entry).1896    nextPatch.passkey = patch.passkey;1897  }18981899  if ("tags" in patch) {1900    nextPatch.tags = patch.tags;1901  }19021903  if ("cardholderName" in patch) {1904    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;1905  }1906  if ("cardNumber" in patch) {1907    const n = patch.cardNumber ?? "";1908    nextPatch.cardNumber = n || undefined;1909  }1910  if ("expMonth" in patch) {1911    nextPatch.expMonth = patch.expMonth?.trim() || undefined;1912  }1913  if ("expYear" in patch) {1914    nextPatch.expYear = patch.expYear?.trim() || undefined;1915  }1916  if ("cvc" in patch) {1917    nextPatch.cvc = patch.cvc?.trim() || undefined;1918  }1919  if ("cardNickname" in patch) {1920    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;1921  }1922  if ("network" in patch) {1923    nextPatch.network = patch.network;1924  }1925  if ("last4" in patch) {1926    nextPatch.last4 = patch.last4?.trim() || undefined;1927  }19281929  return nextPatch;1930}19311932function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {1933  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;1934  const modifiedAt =1935    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;19361937  const base: VaultEntry = {1938    id: entry.id,1939    type: "type" in entry ? entry.type : "login",1940    title: entry.title,1941    username: entry.username,1942    password: entry.password,1943    url: entry.url,1944    totp: entry.totp,1945    notes: entry.notes,1946    breach: entry.breach,1947    canary: "canary" in entry ? entry.canary : undefined,1948    passkey: "passkey" in entry ? entry.passkey : undefined,1949    createdAt,1950    modifiedAt,1951    deletedAt: entry.deletedAt,1952    tags: "tags" in entry ? entry.tags : undefined,1953    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,1954    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,1955    expMonth: "expMonth" in entry ? entry.expMonth : undefined,1956    expYear: "expYear" in entry ? entry.expYear : undefined,1957    cvc: "cvc" in entry ? entry.cvc : undefined,1958    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,1959    network: "network" in entry ? entry.network : undefined,1960    last4: "last4" in entry ? entry.last4 : undefined,1961  };19621963  return migrateLegacyPasskey(base, entry);1964}19651966/**1967 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`1968 * with flat top-level fields (rpId, credentialId, etc.). This converts them1969 * to the new shape — `type:"login"` with a `passkey` sub-object — so all1970 * downstream consumers can rely on the new model. The result is rewritten1971 * to disk on next vault save (hasLegacyShape detects the un-migrated case).1972 */1973function migrateLegacyPasskey(1974  base: VaultEntry,1975  raw: VaultEntry | LegacyVaultEntry1976): VaultEntry {1977  if (base.passkey) return base;1978  const flat = raw as Partial<VaultEntry>;1979  const isPasskeyType = base.type === "passkey";1980  const hasFlatPasskeyFields =1981    typeof flat.rpId === "string" &&1982    typeof flat.credentialId === "string" &&1983    typeof flat.publicKeyCose === "string" &&1984    typeof flat.userHandle === "string" &&1985    typeof flat.userName === "string";1986  if (!isPasskeyType && !hasFlatPasskeyFields) return base;1987  if (!hasFlatPasskeyFields) return base;19881989  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;1990  base.passkey = {1991    rpId: flat.rpId!,1992    rpName: flat.rpName ?? flat.rpId!,1993    credentialId: flat.credentialId!,1994    publicKeyCose: flat.publicKeyCose!,1995    privateKeyJwk: flat.privateKeyJwk,1996    userHandle: flat.userHandle!,1997    userName: flat.userName!,1998    userDisplayName: flat.userDisplayName ?? flat.userName!,1999    algorithm,2000    signCount: flat.signCount ?? 0,2001    aaguid: flat.aaguid ?? "",2002    backupEligible: flat.backupEligible === true,2003    backupState: flat.backupState === true,2004    lastUsedAt: flat.lastUsedAt,2005    browser: flat.browser,2006    transports: flat.transports,2007    createdAt: base.createdAt,2008  };2009  // Promote to "login" so the unified Entry view renders. Username gets2010  // populated from the passkey's userName so the entry is searchable in the2011  // existing autofill flow without a separate code path.2012  if (isPasskeyType) base.type = "login";2013  if (!base.username) base.username = flat.userName;2014  if (!base.url) base.url = `https://${flat.rpId}`;2015  return base;2016}201720182019// ---------------------------------------------------------------------------2020// Deduplication2021// ---------------------------------------------------------------------------20222023function entryDupeKey(e: VaultEntry): string {2024  if (e.type === "note") return `note|${e.title.toLowerCase()}`;2025  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins2026  // on different sites that happen to share a username are still distinct;2027  // two passkey credentials for the same RP+user are duplicates regardless2028  // of any login fields they may also carry.2029  if (e.passkey) {2030    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;2031  }2032  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;2033}20342035/** Returns the number of duplicate entries (those that would be removed). */2036export function countDuplicates(): number {2037  const best = new Map<string, VaultEntry>();2038  let dupeCount = 0;2039  for (const e of entries.filter((e) => !e.deletedAt)) {2040    const key = entryDupeKey(e);2041    const existing = best.get(key);2042    if (!existing) {2043      best.set(key, e);2044    } else {2045      dupeCount++;2046      // Keep the more recently modified one in the map2047      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2048        best.set(key, e);2049      }2050    }2051  }2052  return dupeCount;2053}20542055/**2056 * Soft-deletes duplicate entries, keeping the most recently modified copy of2057 * each group. Returns the number of entries moved to trash.2058 */2059export async function deduplicateEntries(): Promise<number> {2060  if (!derivedKey) throw new Error("vault_locked");2061  // Dedup reshapes slot B permanently under the decoy key, which is2062  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.2063  if (vaultMode === "decoy") {2064    throw new Error("deduplicate disabled in decoy mode");2065  }20662067  const best = new Map<string, VaultEntry>();2068  for (const e of entries.filter((en) => !en.deletedAt)) {2069    const key = entryDupeKey(e);2070    const existing = best.get(key);2071    if (!existing) {2072      best.set(key, e);2073    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2074      best.set(key, e);2075    }2076  }20772078  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));2079  const now = Date.now();2080  let removed = 0;2081  entries = entries.map((e) => {2082    if (!e.deletedAt && !keepIds.has(e.id)) {2083      removed++;2084      return { ...e, deletedAt: now };2085    }2086    return e;2087  });20882089  if (removed > 0) await persistEntries();2090  return removed;2091}20922093// ---------------------------------------------------------------------------2094// Vault history — snapshot BEFORE every vault write so the saved blob is2095// what is about to be lost, not what was just written. Capped at 10. The2096// stored blobs are AES-256-GCM ciphertext under the master password, so2097// history adds zero security surface.2098// ---------------------------------------------------------------------------20992100const HISTORY_KEY = "peach_vault_history";2101const HISTORY_MAX = 10;21022103/**2104 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE2105 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost2106 * state. Identical-to-most-recent blobs are skipped to avoid filling history2107 * with no-op writes.2108 */2109export async function takeVaultSnapshot(2110  reason: VaultSnapshot["reason"],2111): Promise<void> {2112  try {2113    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);2114    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];2115    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];21162117    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,2118    // pre-createVault). createVault skips snapshots intentionally — there is2119    // no prior state to preserve.2120    if (!slotA) return;21212122    const vault_a = JSON.stringify(slotA);2123    const vault_b = JSON.stringify(slotB ?? slotA);21242125    // Best-effort entry count: only meaningful when the in-memory state is2126    // unlocked AND in real mode. Decoy mode would leak the decoy entry2127    // count; locked mode has no entries to count. Both fall through to null.2128    let entryCount: number | null = null;2129    if (derivedKey && vaultMode === "real") {2130      try {2131        entryCount = entries.filter((e) => !e.deletedAt).length;2132      } catch {2133        entryCount = null;2134      }2135    }21362137    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2138    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)2139      ? existingRaw2140      : { snapshots: [] };21412142    // Deduplication: if the most recent snapshot has byte-identical blobs,2143    // skip — saves the same vault N times in a row would otherwise burn the2144    // entire history window with no recovery value.2145    const latest = history.snapshots[0];2146    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {2147      return;2148    }21492150    const snapshot: VaultSnapshot = {2151      id: crypto.randomUUID(),2152      savedAt: Date.now(),2153      reason,2154      vault_a,2155      vault_b,2156      entryCount,2157    };21582159    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);2160    await chrome.storage.local.set({ [HISTORY_KEY]: history });2161  } catch (err) {2162    // Non-fatal — history must never block a save. A failed snapshot just2163    // means the user loses one rung of the safety ladder for this write.2164    // M4/QUA-006: log so the failure is visible; if it looks like a quota2165    // problem, notify the popup so the user can prune history themselves.2166    console.warn('[Peach] takeVaultSnapshot failed:', err);2167    if (String(err).includes('QUOTA') || String(err).includes('quota')) {2168      try {2169        await chrome.runtime.sendMessage({ type: 'VAULT_STORAGE_QUOTA_WARNING' });2170      } catch {2171        // popup may be closed; surfaced via console.warn above2172      }2173    }2174  }2175}21762177/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */2178export async function clearVaultHistory(): Promise<void> {2179  try {2180    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });2181  } catch {2182    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().2183  }2184}21852186/** Read all snapshots, newest first, for the Settings vault history view. */2187export async function getVaultHistory(): Promise<VaultSnapshot[]> {2188  const raw = await chrome.storage.local.get(HISTORY_KEY);2189  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2190  if (!history || !Array.isArray(history.snapshots)) return [];2191  return history.snapshots;2192}21932194/**2195 * Replace the on-disk vault with the chosen snapshot's blobs and lock the2196 * vault. The current state is itself snapshotted with reason 'restore' first2197 * so the restore is reversible. After this returns, the user must unlock2198 * with the password that matches the restored blob.2199 */2200export async function restoreVaultSnapshot(2201  snapshotId: string,2202): Promise<{ ok: true } | { ok: false; error: string }> {2203  const history = await getVaultHistory();2204  const snapshot = history.find((s) => s.id === snapshotId);2205  if (!snapshot) return { ok: false, error: "snapshot_not_found" };22062207  // Snapshot the about-to-be-replaced current state so the restore is2208  // reversible. takeVaultSnapshot is no-op when the most recent entry is2209  // byte-identical, so a restore-of-a-restore stays clean.2210  await takeVaultSnapshot("restore");22112212  let restoredA: StoredVault;2213  let restoredB: StoredVault;2214  try {2215    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;2216    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;2217  } catch {2218    return { ok: false, error: "snapshot_corrupt" };2219  }22202221  await writeSlot(SLOT_A, restoredA);2222  await writeSlot(SLOT_B, restoredB);2223  await stampLocalVaultMutation();22242225  // Lock so the user must prove they can decrypt the restored blob. If they2226  // restored a pre-password-change snapshot they discover it immediately at2227  // the lock screen rather than silently holding an inaccessible vault.2228  lock();22292230  return { ok: true };2231}22322233/**2234 * Change the master password. Re-encrypts slot A with a fresh salt + new2235 * derived key. Slot B (duress or noise) is left untouched when duress is2236 * configured — it uses an independent key derived from the duress password —2237 * and regenerated as noise when duress is not configured.2238 */2239export async function changePassword(2240  currentPassword: string,2241  newPassword: string2242): Promise<boolean> {2243  if (!derivedKey || vaultMode !== "real") return false;22442245  // Verify current password before allowing any change2246  const valid = await verifyMainPassword(currentPassword);2247  if (!valid) return false;22482249  const slotA = await loadSlot(SLOT_A);2250  if (!slotA) return false;2251  if (!rootKey) return false;22522253  // Derive fresh key from new password + new random salt2254  const newSalt = generateSalt();2255  const newKey = await deriveKey(newPassword, newSalt);22562257  // Re-encrypt the in-memory entries with the new key2258  const plaintext = encodePayload(entries, duressConfigured);2259  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");22602261  // Pre-wrap root key under the new derived key. Recovery fields wrap rootKey2262  // under recovery words and remain untouched — recovery phrase keeps working2263  // across password changes (H4/COR-002).2264  const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);22652266  const finalSlotA: StoredVault = {2267    ...slotA,2268    salt: hex(newSalt),2269    nonce,2270    ciphertext,2271    tag,2272    ...pwRoot,2273  };22742275  // Snapshot the OLD-key blob before overwriting it. If the user later2276  // restores from this snapshot they will need the OLD password — the2277  // post-restore lock surfaces that immediately.2278  await takeVaultSnapshot("local_save");22792280  // Single atomic write: ciphertext, salt, and pw_root_* fields all land in2281  // one storage transaction. An SW kill before this point leaves the old2282  // slot intact; after this point everything tied to the new password is2283  // already on disk together (M6/COR-004).2284  await writeSlot(SLOT_A, finalSlotA);22852286  // Swap the in-memory key synchronously before any further await. If we2287  // threw between the Slot A write and this swap, the next persistEntries()2288  // would overwrite Slot A with old-key ciphertext, locking the user out.2289  zeroKey(derivedKey);2290  derivedKey = newKey;22912292  // Regenerate noise for slot B when duress is not in use. Runs after the2293  // key swap so a failure here is a disposable noise regeneration, not a2294  // lockout.2295  if (!duressConfigured) {2296    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2297  }2298  await stampLocalVaultMutation();22992300  return true;2301}