vault.ts

117.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, deriveKeyRaw, generateSalt } from "../crypto/argon2";13import { extractDomain } from "../shared/domain";14import { clampSyncTimestamp } from "../shared/sync-limits";15import { computeVaultId } from "../shared/sync-identity";16// Static, not `await import()`: runtime import() throws in an MV3 service17// worker. Neither module imports vault.ts back, so there is no cycle to break.18import { triggerSyncIfReachable } from "./sync-trigger";19import { clearPairedDevices } from "./paired-devices";20import {21  rootKeyToWords,22  wordsToRootKeyMaterial,23  deriveRecoveryKey,24} from "../shared/recovery-words";25import type {26  AccentColorName,27  Canary,28  LegacyVaultEntry,29  ManualSummonRecord,30  PasskeyData,31  SavedIdentity,32  StoredVault,33  VaultEntry,34  VaultHistory,35  VaultSnapshot,36} from "../shared/types";37import { decrypt, encrypt, zeroKey } from "./aes";38import { clearAutoLock, touchAutoLock } from "./alarms";39import { setRecoveryConfirmPending } from "./recovery-pending";4041type EntryDraft = Omit<42  VaultEntry,43  "id" | "createdAt" | "modifiedAt" | "deletedAt"44>;4546// Slot-neutral storage keys (v1.4.24+). Both slots always exist; slot B47// either holds the decoy vault (when duress is configured) or random noise48// that looks identical at rest. No external indicator is stored — duress49// configuration is recorded only inside the encrypted payload of slot A.50const SLOT_A = "peach_vault_a";51const SLOT_B = "peach_vault_b";5253// Legacy keys, migrated away on first access.54const LEGACY_MAIN = "peach_vault";55const LEGACY_DECOY = "peach_decoy_vault";56const LEGACY_DURESS_FLAG = "peach_duress";5758// Plaintext is padded to the next multiple of this size before encryption.59// This coarsens ciphertext-length leakage into 1 KiB buckets.60const PAD_BLOCK = 1024;6162// How many past passwords to retain per entry. Generous enough to be useful as63// a recovery aid while bounding the secret material we hold (and the ciphertext64// growth) for an entry whose password is rotated frequently.65const PASSWORD_HISTORY_MAX = 50;6667// When duress is configured, BOTH slots are padded to this fixed ceiling so68// their ciphertext lengths are identical and cannot reveal that a hidden decoy69// vault exists (audit MEDIUM — plausible-deniability leak). 256 KiB comfortably70// holds a very large vault (thousands of entries incl. passkeys). The decoy71// slot can't be re-encrypted on every real-vault save (the real session has no72// decoy key), so a fixed ceiling — rather than size-matching on each write — is73// what keeps the two slots equal. A real vault whose plaintext exceeds this74// would grow past the ceiling and re-expose the bit; that is an extreme,75// documented edge well beyond normal use.76const DURESS_SLOT_SIZE = 262_144;7778let derivedKey: Uint8Array | null = null;79// 32-byte random root key, separate from the derivedKey used for vault encryption.80// The root key is wrapped (encrypted) under the recovery key derived from the81// 12-word phrase. When the user changes their password, only the wrapping changes;82// the root key and recovery words remain stable.83let rootKey: Uint8Array | null = null;84let entries: VaultEntry[] = [];85let domainIndex: Map<string, VaultEntry[]> = new Map();86let vaultMode: "real" | "decoy" = "real";87// Cached post-unlock view of whether the real vault records that duress is88// configured. Only meaningful after a real-mode unlock; for decoy-mode unlock89// this stays false because we never see slot A's plaintext.90let duressConfigured = false;9192let migrationRan = false;9394// Set to a corruption reason during the last unlock() attempt when both slots95// decrypted but failed schema validation. Consumed and cleared by96// consumeLastUnlockFailureReason() so messages.ts can surface vault_corrupted97// distinctly from wrong-password (M7/COR-007).98//99// NOTE: module-level state — if vault.ts is ever moved to a worker context,100// this must be replaced with a structured return value from unlock() or a101// postMessage channel. The signal is invisible across module-instance102// boundaries.103let lastUnlockFailureReason: string | null = null;104105export function consumeLastUnlockFailureReason(): string | null {106  const r = lastUnlockFailureReason;107  lastUnlockFailureReason = null;108  return r;109}110111// ---------------------------------------------------------------------------112// Settings (persisted inside the encrypted vault store as a sidecar key)113// ---------------------------------------------------------------------------114115export type TextSize = "small" | "medium" | "large";116117export interface VaultSettings {118  accentColor?: AccentColorName;119  theme?: "dark" | "light";120  autoLockMinutes?: number;121  clipboardClearSeconds?: number;122  lockOnClose?: boolean;123  breachMonitoring?: boolean;124  passkeyProviderEnabled?: boolean;125  trashRetentionDays?: number;126  manualSummonDomains?: Record<string, ManualSummonRecord>;127  declinedCardFingerprints?: string[];128  /**129   * Text size scale for body copy and entry titles. Stops at "large" by130   * design — XL clips dense screens (Vault, Health) inside the fixed131   * 330×480 popup surface.132   */133  textSize?: TextSize;134  /**135   * When true, all entrance/exit transitions and ambient animations are136   * collapsed to instant. Honoured alongside the user's OS-level137   * `prefers-reduced-motion`.138   */139  reduceMotion?: boolean;140  /** Whether this vault has a local PIN unlock wrapper configured. */141  pinEnabled?: boolean;142  /** Whether the lock screen should open on PIN instead of the password. */143  pinPreferred?: boolean;144  /**145   * User overrides for keyboard shortcuts. Keys are ShortcutAction names146   * (see src/shared/shortcuts.ts). Missing actions fall through to147   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the148   * canonical lookup. Storing only the deltas keeps the vault payload149   * small and lets default updates land for users who never rebind.150   */151  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;152}153154interface StoredPinWrap {155  version: 1;156  salt: string;157  nonce: string;158  ciphertext: string;159  tag: string;160}161162const PIN_WRAP_STORAGE = "peach_pin_wrap";163const PIN_MIN_LENGTH = 4;164const PIN_MAX_LENGTH = 12;165166let settings: VaultSettings = {};167168export function getSettings(): VaultSettings {169  return settings;170}171172export async function ensureSettingsLoaded(): Promise<VaultSettings> {173  await loadSettings();174  return settings;175}176177export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {178  await loadSettings();179  // LV-7: cheap insurance against prototype pollution if a future caller ever180  // feeds parsed external JSON here (native spread keeps __proto__ an own-181  // property today, but Object.assign-style consumers and JSON revivers have182  // been burned before).183  const safe = Object.fromEntries(184    Object.entries(patch).filter(185      ([k]) => !["__proto__", "constructor", "prototype"].includes(k),186    ),187  ) as Partial<VaultSettings>;188  settings = { ...settings, ...safe };189  await persistSettings();190}191192function isValidPin(pin: string): boolean {193  return new RegExp(`^\\d{${PIN_MIN_LENGTH},${PIN_MAX_LENGTH}}$`).test(pin);194}195196/** Set up a local PIN wrapper after proving the current master password. */197export async function setupPinUnlock(currentPassword: string, pin: string): Promise<boolean> {198  if (!derivedKey || vaultMode !== "real" || !isValidPin(pin)) return false;199  if (!(await verifyMainPassword(currentPassword))) return false;200201  const salt = generateSalt();202  const pinKey = await deriveKey(pin, salt);203  try {204    const wrapped = await encrypt(pinKey, hex(derivedKey), "pin_wrap_v1");205    await chrome.storage.local.set({206      [PIN_WRAP_STORAGE]: { version: 1, salt: hex(salt), ...wrapped } satisfies StoredPinWrap,207    });208    await updateSettings({ pinEnabled: true });209    return true;210  } finally {211    zeroKey(pinKey);212  }213}214215async function clearPinUnlockStorage(): Promise<void> {216  await chrome.storage.local.remove(PIN_WRAP_STORAGE);217  await updateSettings({ pinEnabled: false });218}219220/** Remove the PIN wrapper after proving the current master password. */221export async function disablePinUnlock(currentPassword: string): Promise<boolean> {222  if (!derivedKey || vaultMode !== "real") return false;223  if (!(await verifyMainPassword(currentPassword))) return false;224  await clearPinUnlockStorage();225  return true;226}227228/** Unlock the real vault from its locally stored PIN-wrapped derived key. */229export async function unlockWithPin(pin: string): Promise<boolean> {230  if (!isValidPin(pin)) return false;231  await loadSettings();232  if (!settings.pinEnabled) return false;233234  const stored = (await chrome.storage.local.get(PIN_WRAP_STORAGE))[PIN_WRAP_STORAGE] as235    | StoredPinWrap236    | undefined;237  if (238    !stored ||239    stored.version !== 1 ||240    !stored.salt ||241    !stored.nonce ||242    !stored.ciphertext ||243    !stored.tag244  ) return false;245246  const pinKey = await deriveKey(pin, unhex(stored.salt));247  let unwrappedKey: Uint8Array | null = null;248  try {249    const masterKeyHex = await decrypt(250      pinKey,251      stored.nonce,252      stored.ciphertext,253      stored.tag,254      "pin_wrap_v1",255    );256    if (!/^[0-9a-f]{64}$/.test(masterKeyHex)) return false;257    unwrappedKey = unhex(masterKeyHex);258    const result = await unlockWithKey(masterKeyHex);259    if (!result.ok) return false;260    await saveSessionKey(unwrappedKey, "real");261    return true;262  } catch {263    return false;264  } finally {265    zeroKey(pinKey);266    if (unwrappedKey) zeroKey(unwrappedKey);267  }268}269270async function persistSettings(): Promise<void> {271  try {272    await chrome.storage.local.set({ peach_settings: settings });273  } catch (err) {274    throw mapStorageError(err);275  }276}277278function mapStorageError(err: unknown): Error {279  const message = err instanceof Error ? err.message : String(err);280  if (/QUOTA_BYTES|quota/i.test(message)) {281    return new Error("STORAGE_QUOTA_EXCEEDED");282  }283  return err instanceof Error ? err : new Error(message);284}285286async function loadSettings(): Promise<void> {287  const result = await chrome.storage.local.get("peach_settings");288  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};289}290291// Fire-and-forget sync trigger after a committed vault write. A failure here292// must never fail the write that caused it.293function fireSyncTrigger(reason: "vault_change"): void {294  void Promise.resolve()295    .then(() => triggerSyncIfReachable({ reason }))296    .catch(() => {297      /* no sync runner registered, or nothing reachable — non-fatal */298    });299}300301let lockedSyncRefreshHook: (() => Promise<void>) | null = null;302let lockedSyncUnlockHook: (() => Promise<void>) | null = null;303304export function setLockedSyncHooks(hooks: {305  refresh: () => Promise<void>;306  afterUnlock: () => Promise<void>;307}): void {308  lockedSyncRefreshHook = hooks.refresh;309  lockedSyncUnlockHook = hooks.afterUnlock;310}311312async function refreshLockedSyncState(): Promise<void> {313  if (vaultMode !== "real" || !rootKey || !lockedSyncRefreshHook) return;314  await lockedSyncRefreshHook().catch((error) => {315    console.warn("[Peach sync] locked spool refresh failed:", error);316  });317}318319// ---------------------------------------------------------------------------320// HIBP breach check (k-anonymity model)321// ---------------------------------------------------------------------------322323export async function checkBreach(password: string): Promise<number> {324  // HIBP k-anonymity API is keyed on SHA-1 prefixes — this is dictated by325  // the public API, not a security choice. Do not migrate to SHA-256 here.326  const encoder = new TextEncoder();327  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));328  const hashHex = Array.from(new Uint8Array(hashBuffer))329    .map((b) => b.toString(16).padStart(2, "0"))330    .join("")331    .toUpperCase();332333  const prefix = hashHex.slice(0, 5);334  const suffix = hashHex.slice(5);335336  try {337    // PCH-02-007: request HIBP response padding so response length does not338    // reveal how many suffixes a queried prefix returned.339    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`, {340      headers: { "Add-Padding": "true" },341    });342    if (!res.ok) return 0;343    const text = await res.text();344    const match = text.split("\r\n").find((l) => l.startsWith(suffix));345    return match ? parseInt(match.split(":")[1], 10) : 0;346  } catch {347    return 0; // Network unavailable — don't block348  }349}350351export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {352  const fingerprint = await cardFingerprint(last4, expMonth, expYear);353  await loadSettings();354  settings = {355    ...settings,356    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],357  };358  await persistSettings();359}360361export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {362  await loadSettings();363  const fingerprint = await cardFingerprint(last4, expMonth, expYear);364  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);365}366367async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {368  const encoder = new TextEncoder();369  const buf = await crypto.subtle.digest("SHA-256", encoder.encode(`${last4}|${expMonth}|${expYear}`));370  return Array.from(new Uint8Array(buf).slice(0, 8))371    .map((b) => b.toString(16).padStart(2, "0"))372    .join("");373}374375function hex(buf: Uint8Array): string {376  return Array.from(buf)377    .map((b) => b.toString(16).padStart(2, "0"))378    .join("");379}380381function unhex(s: string): Uint8Array {382  const out = new Uint8Array(s.length / 2);383  for (let i = 0; i < s.length; i += 2) {384    out[i / 2] = parseInt(s.slice(i, i + 2), 16);385  }386  return out;387}388389// ---------------------------------------------------------------------------390// Root key generation and recovery word wrapping391// ---------------------------------------------------------------------------392393/**394 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key395 * derived from the recovery word material via HKDF-SHA256. The wrapped blob396 * fields are stored in slot A alongside the password-wrapped ciphertext. The397 * root key itself is kept in memory for the session so getRecoveryWords() can398 * encode it without re-deriving.399 *400 * SECURITY MODEL (PCH-01-005): the recovery KEK is derived from the 12-word401 * material, which encodes only rootKey[0..16] (recovery-words.ts). The KEK is thus402 * derived from a PREFIX of the very plaintext it wraps, and effective recovery403 * security is 128-bit (standard for a 12-word phrase), NOT 256-bit. This is safe —404 * unwrapping requires the words, and the GCM ciphertext leaks nothing without them —405 * but it is intentional, not an oversight. A future escrow version wanting 256-bit406 * recovery must derive the words from INDEPENDENT entropy, not from the root key.407 */408async function generateAndWrapRootKey(): Promise<{409  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey410  recoverySalt: string;411  wrappedRootKey: string;412  wrappedNonce: string;413  wrappedTag: string;414}> {415  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));416417  // Derive the recovery key from the BIP39 words encoding of the root key.418  const words = await rootKeyToWords(rootKeyBytes);419  const wordMaterial = await wordsToRootKeyMaterial(words);420  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));421  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);422423  // Wrap the root key with the recovery key using AES-256-GCM directly.424  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.425  const nonce = crypto.getRandomValues(new Uint8Array(12));426  const encoded = new TextEncoder().encode(hex(rootKeyBytes));427  const raw = await crypto.subtle.encrypt(428    {429      name: "AES-GCM",430      iv: nonce,431      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,432      tagLength: 128,433    },434    recoveryKey,435    encoded436  );437  // WebCrypto returns ciphertext || auth tag concatenated; split them.438  const rawU8 = new Uint8Array(raw);439  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));440  const tag = hex(rawU8.slice(rawU8.length - 16));441442  return {443    rawRootKey: hex(rootKeyBytes),444    recoverySalt: hex(recoverySalt),445    wrappedRootKey: ciphertext,446    wrappedNonce: hex(nonce),447    wrappedTag: tag,448  };449}450451async function wrapExistingRootKeyForRecovery(rootKeyBytes: Uint8Array): Promise<{452  recoverySalt: string;453  wrappedRootKey: string;454  wrappedNonce: string;455  wrappedTag: string;456}> {457  if (rootKeyBytes.length !== 32) {458    throw new Error("ADOPT_INVALID_ROOT_KEY: expected 32 bytes");459  }460461  const words = await rootKeyToWords(rootKeyBytes);462  const wordMaterial = await wordsToRootKeyMaterial(words);463  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));464  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);465  const nonce = crypto.getRandomValues(new Uint8Array(12));466  const encoded = new TextEncoder().encode(hex(rootKeyBytes));467  const raw = await crypto.subtle.encrypt(468    {469      name: "AES-GCM",470      iv: nonce,471      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,472      tagLength: 128,473    },474    recoveryKey,475    encoded,476  );477  const rawU8 = new Uint8Array(raw);478  return {479    recoverySalt: hex(recoverySalt),480    wrappedRootKey: hex(rawU8.slice(0, rawU8.length - 16)),481    wrappedNonce: hex(nonce),482    wrappedTag: hex(rawU8.slice(rawU8.length - 16)),483  };484}485486/**487 * Unwrap the root key using recovery words. Called during recovery flow when488 * the user provides their 12-word phrase and a new password.489 * Exported for use by the RECOVER_WITH_WORDS message handler.490 */491export async function unwrapRootKeyForRecovery(492  wrappedRootKey: string,493  wrappedNonce: string,494  wrappedTag: string,495  recoverySaltHex: string,496  words: string[]497): Promise<void> {498  const wordMaterial = await wordsToRootKeyMaterial(words);499  const recoverySalt = unhex(recoverySaltHex);500  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);501502  // Decrypt and cache the root key. The root key bytes were hex-encoded before503  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt504  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.505  // Each component is stored separately in the vault slot; decode them individually506  // so the nonce is always exactly the 12 bytes from wrappedNonce.507  const nonceRaw = unhex(wrappedNonce);508  const ciphertextRaw = unhex(wrappedRootKey);509  const tagRaw = unhex(wrappedTag);510  // Combine ciphertext and tag for AES-GCM (expects ciphertext||tag).511  // Use explicit ArrayBuffer allocations to satisfy TS BufferSource constraints.512  const nonceU8 = nonceRaw as unknown as Uint8Array<ArrayBuffer>;513  const combined = new Uint8Array(ciphertextRaw.length + tagRaw.length);514  combined.set(ciphertextRaw, 0);515  combined.set(tagRaw, ciphertextRaw.length);516  const raw = await crypto.subtle.decrypt(517    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },518    recoveryKey,519    combined520  );521  const decryptedHex = new TextDecoder().decode(raw);522  rootKey = unhex(decryptedHex);523}524525/**526 * Re-wrap the cached root key under a new derived key (from the user's new527 * password). Called after password change and after recovery re-key so the528 * same recovery words continue to work with the updated password — and so529 * subsequent unlocks can rehydrate rootKey via deriveRootKeyFromDerivedKey.530 *531 * recovery_* fields are intentionally not touched: those wrap rootKey under532 * the recovery words, which do not change when the password changes.533 */534export async function rewrapRootKeyForPassword(535  newDerivedKey: Uint8Array,536  _newSalt: Uint8Array537): Promise<void> {538  if (!rootKey) throw new Error("root key not set");539  const slotA = await loadSlot(SLOT_A);540  if (!slotA) throw new Error("missing slot A");541  const pwRoot = await wrapRootKeyForDerivedKey(newDerivedKey, rootKey);542  await writeSlot(SLOT_A, { ...slotA, ...pwRoot });543}544545/**546 * Change the vault encryption to a new derived key (already derived from547 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.548 * Caller is responsible for key swap and snapshot.549 */550async function reencryptVaultForNewKey(551  newKey: Uint8Array,552  slotA: StoredVault553): Promise<void> {554  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);555  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");556  await writeSlot(SLOT_A, {557    ...slotA,558    nonce,559    ciphertext,560    tag,561  });562}563564/**565 * Full password change with an already-derived key (used after recovery).566 * Unlike changePassword(), this skips re-verification of current password567 * since recovery flow has already validated the user's identity via words.568 */569export async function changePasswordWithNewKey(570  newSalt: Uint8Array,571  newKey: Uint8Array572): Promise<void> {573  const slotA = await loadSlot(SLOT_A);574  if (!slotA) throw new Error("missing slot A");575  // reencryptVaultForNewKey must receive the updated salt so that subsequent576  // UNLOCK calls with the new password can re-derive the correct key.577  await reencryptVaultForNewKey(newKey, { ...slotA, salt: hex(newSalt) });578  if (derivedKey) zeroKey(derivedKey);579  derivedKey = newKey;580  // PCH-01-008: re-escrow the new key (see changePassword). The recovery flow581  // reaches this path, so a post-recovery SW restart would otherwise lock out582  // a user who just reset their password.583  void saveSessionKey(newKey, "real");584  // Refresh pw_root_* under the new key so deriveRootKeyFromDerivedKey works585  // on the next unlock (H2/COR-006). If rootKey is missing — recovery flow586  // should have populated it — fall back silently rather than blowing up the587  // recovery path; migrateToRecovery will rehydrate on the next unlock.588  if (rootKey) {589    const slotAfter = await loadSlot(SLOT_A);590    if (slotAfter) {591      const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);592      await writeSlot(SLOT_A, { ...slotAfter, ...pwRoot });593    }594  }595  if (!duressConfigured) {596    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));597  }598  await clearPinUnlockStorage();599  await stampLocalVaultMutation();600}601602// ---------------------------------------------------------------------------603// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive604// a new key after password reset without re-importing the crypto module.605// ---------------------------------------------------------------------------606607export { deriveKey, generateSalt };608609export async function loadSlotA(): Promise<StoredVault | null> {610  return loadSlot(SLOT_A);611}612613/**614 * Recover the session root key by AES-GCM-decrypting the pw_root_* fields615 * (a second wrapping of the root key under the password-derived key). This616 * is what makes GET_RECOVERY_WORDS work across lock/unlock cycles without617 * the user re-entering their recovery phrase: the same root key bytes —618 * and therefore the same 12 words — survive every unlock.619 *620 * Returns null when the pw_root fields are absent (pre-v2.12 vault that621 * hasn't been migrated yet) or when decryption fails (wrong derived key,622 * tampered ciphertext). On null the caller should fall through to623 * migrateToRecovery() to populate the fields.624 */625async function deriveRootKeyFromDerivedKey(626  derivedKeyBytes: Uint8Array,627  slotA: StoredVault,628): Promise<Uint8Array | null> {629  if (!slotA.pw_root_ciphertext || !slotA.pw_root_nonce || !slotA.pw_root_tag) {630    return null;631  }632  try {633    const rootHex = await decrypt(634      derivedKeyBytes,635      slotA.pw_root_nonce,636      slotA.pw_root_ciphertext,637      slotA.pw_root_tag,638      "pw_root_v1",639    );640    return unhex(rootHex);641  } catch {642    return null;643  }644}645646// AES-GCM-wrap rootKey under derivedKey for the pw_root_* fields. Returns the647// hex-encoded ciphertext/nonce/tag triple ready to embed in StoredVault.648async function wrapRootKeyForDerivedKey(649  derivedKeyBytes: Uint8Array,650  rootKeyBytes: Uint8Array,651): Promise<{ pw_root_ciphertext: string; pw_root_nonce: string; pw_root_tag: string }> {652  const { nonce, ciphertext, tag } = await encrypt(derivedKeyBytes, hex(rootKeyBytes), "pw_root_v1");653  return {654    pw_root_ciphertext: ciphertext,655    pw_root_nonce: nonce,656    pw_root_tag: tag,657  };658}659660/**661 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),662 * generate the root key, wrap it under the recovery words, and persist the663 * recovery escrow into slot A. The caller must show the recovery words to the664 * user before calling this — once called, those words are the vault's recovery665 * path. This runs once, on the first unlock of an upgraded vault.666 */667export async function migrateToRecovery(): Promise<void> {668  if (!derivedKey || vaultMode !== "real") {669    throw new Error("vault must be unlocked in real mode");670  }671  const slotA = await loadSlot(SLOT_A);672  if (!slotA) throw new Error("missing slot A");673  const hasRecoveryEscrow =674    !!slotA.recovery_salt && !!slotA.recovery_wrapped_root_key;675  const hasPwRoot = !!slotA.pw_root_ciphertext && !!slotA.pw_root_nonce;676  if (hasRecoveryEscrow && hasPwRoot) return; // already done677678  // If recovery escrow already exists but pw_root doesn't, we cannot recover679  // the original rootKey without the user's words. The least-surprising path680  // is to rotate to a fresh root key — the user's old recovery words become681  // invalid but we surface peach_recovery_pending so the popup re-presents682  // the words. The unlock-path callers all set that flag.683  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =684    await generateAndWrapRootKey();685  rootKey = unhex(rawRootKey);686687  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);688  const nextSlotA: StoredVault = {689    ...slotA,690    recovery_salt: recoverySalt,691    recovery_wrapped_root_key: wrappedRootKey,692    recovery_nonce: wrappedNonce,693    recovery_tag: wrappedTag,694    ...pwRoot,695  };696  await writeSlot(SLOT_A, nextSlotA);697  await stampLocalVaultMutation();698}699700/**701 * Returns the 12 recovery words for the current session's root key.702 * Must be called while the vault is unlocked. Throws if recovery has not703 * been set up (pre-v2.12 vault that hasn't migrated yet).704 */705export async function getRecoveryWords(): Promise<string[]> {706  if (!rootKey) {707    throw new Error("recovery not set up — vault may need migration");708  }709  return rootKeyToWords(rootKey);710}711712async function loadSlot(key: string): Promise<StoredVault | null> {713  const result = await chrome.storage.local.get(key);714  return (result as Record<string, StoredVault | undefined>)[key] ?? null;715}716717async function writeSlot(key: string, vault: StoredVault): Promise<void> {718  try {719    await chrome.storage.local.set({ [key]: vault });720  } catch (err) {721    throw mapStorageError(err);722  }723}724725/**726 * Roll back a vault that was created by [createVaultFromRootKey] but727 * couldn't be paired (e.g. the subsequent license activation, identity728 * derivation, or paired-device upsert failed). Without this, a failed729 * receiver session leaves behind an empty vault — the user has to730 * re-pair to get a working setup, but the empty vault sits in the way.731 *732 * The call is idempotent: removing keys that aren't there is a no-op.733 */734export async function rollbackAdoptedVault(): Promise<void> {735  lock();736  try {737    await chrome.storage.local.remove([738      SLOT_A,739      SLOT_B,740      "peach_last_modified_at",741    ]);742  } catch (err) {743    throw mapStorageError(err);744  }745}746747async function commitCreatedVaultSlots(slotA: StoredVault, slotB: StoredVault): Promise<void> {748  const raw = await chrome.storage.local.get("peach_last_modified_at");749  const previous = Number(750    (raw as Record<string, unknown>).peach_last_modified_at,751  );752  const next = Math.max(753    Date.now(),754    Number.isFinite(previous) ? previous + 1 : 0,755  );756  try {757    await chrome.storage.local.set({758      [SLOT_A]: slotA,759      [SLOT_B]: slotB,760      peach_last_modified_at: String(next),761    });762  } catch (err) {763    throw mapStorageError(err);764  }765}766767// Monotonic clock for per-entry modifiedAt / deletedAt timestamps. Guards the768// sync layer's last-write-wins merge against wall-clock regressions (NTP step,769// manual clock change, DST glitch): a backwards Date.now() must never produce770// an entry timestamp that's <= the entry's existing modifiedAt, or a genuine771// local edit would lose to a stale remote copy. The returned value is always772// strictly greater than both the entry's current modifiedAt and the previous773// call's result (SEV-2-E).774let lastEntryTimestamp = 0;775function monotonicEntryTimestamp(currentModifiedAt?: number): number {776  const base = Math.max(777    Date.now(),778    (currentModifiedAt ?? 0) + 1,779    lastEntryTimestamp + 1,780  );781  lastEntryTimestamp = base;782  return base;783}784785async function stampLocalVaultMutation(): Promise<void> {786  const raw = await chrome.storage.local.get("peach_last_modified_at");787  const previous = Number(788    (raw as Record<string, unknown>).peach_last_modified_at,789  );790  const next = Math.max(791    Date.now(),792    Number.isFinite(previous) ? previous + 1 : 0,793  );794  await chrome.storage.local.set({795    peach_last_modified_at: String(next),796  });797}798799/**800 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault801 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +802 * peach_vault_b). Runs at most once per session; safe to call repeatedly.803 */804async function ensureMigrated(): Promise<void> {805  if (migrationRan) return;806  migrationRan = true;807808  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];809  const raw = await chrome.storage.local.get(keys);810  const slotAExists = raw[SLOT_A] !== undefined;811  const slotBExists = raw[SLOT_B] !== undefined;812  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;813  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;814815  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.816  if (!slotAExists && !legacyMain) return;817818  // Already migrated.819  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;820821  let didMigrate = false;822  if (legacyMain && !slotAExists) {823    await writeSlot(SLOT_A, legacyMain);824    didMigrate = true;825  }826827  // If slot B isn't set, populate it: use legacy decoy when present, else noise.828  if (!slotBExists) {829    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;830    if (legacyDecoy) {831      await writeSlot(SLOT_B, legacyDecoy);832      didMigrate = true;833    } else if (target) {834      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));835      didMigrate = true;836    }837  }838839  const toRemove: string[] = [];840  if (legacyMain) toRemove.push(LEGACY_MAIN);841  if (legacyDecoy) toRemove.push(LEGACY_DECOY);842  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);843  if (toRemove.length > 0) {844    await chrome.storage.local.remove(toRemove);845    didMigrate = true;846  }847  if (didMigrate) {848    await stampLocalVaultMutation();849  }850}851852// ---------------------------------------------------------------------------853// Padding + noise helpers854// ---------------------------------------------------------------------------855856/**857 * Pads the plaintext string with trailing spaces so that the encoded byte858 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing859 * whitespace after a complete document, so the padding is transparent on860 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.861 */862function padPlaintext(json: string, minBytes = 0): string {863  const encoded = new TextEncoder().encode(json);864  const bucket = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);865  // minBytes lets the duress path force a fixed ceiling so both slots match.866  const target = Math.max(bucket, minBytes);867  const padLen = target - encoded.length;868  return json + " ".repeat(padLen);869}870871/**872 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The873 * ciphertext length matches the given byte count. Because GCM ciphertext is874 * pseudo-random, this blob is indistinguishable at rest from a real875 * encrypted vault without the key.876 *877 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto878 * spec hard limit). Chunk the fill so vaults larger than that — common once879 * the user has dozens of entries or any passkey — don't throw.880 */881const CSPRNG_CHUNK = 65_536;882883function fillRandom(buf: Uint8Array): Uint8Array {884  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {885    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);886    const chunk = new Uint8Array(end - offset);887    crypto.getRandomValues(chunk);888    buf.set(chunk, offset);889  }890  return buf;891}892893function generateNoiseBlob(ciphertextBytes: number): StoredVault {894  const salt = crypto.getRandomValues(new Uint8Array(16));895  const nonce = crypto.getRandomValues(new Uint8Array(12));896  const cipher = fillRandom(new Uint8Array(ciphertextBytes));897  const tag = crypto.getRandomValues(new Uint8Array(16));898  return {899    version: 1,900    salt: hex(salt),901    nonce: hex(nonce),902    ciphertext: hex(cipher),903    tag: hex(tag),904  };905}906907// ---------------------------------------------------------------------------908// Encrypted plaintext envelope909// ---------------------------------------------------------------------------910911type VaultPayload = {912  entries: VaultEntry[];913  duressConfigured?: boolean;914};915916type DecryptedPayload = {917  entries: VaultEntry[];918  duressConfigured: boolean;919  /**920   * True when at least one entry in the on-disk payload had a legacy shape921   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)922   * and got rewritten by normalizeEntry. The unlock path uses this to923   * persist the new shape immediately — without it, the migration would924   * re-run on every unlock and old shape never disappears from disk.925   */926  migratedFromLegacy: boolean;927};928929function encodePayload(entries: VaultEntry[], duress: boolean, minBytes = 0): string {930  const envelope: VaultPayload = { entries, duressConfigured: duress };931  return padPlaintext(JSON.stringify(envelope), minBytes);932}933934function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {935  if ("created" in raw || "updated" in raw) return true;936  const flat = raw as Partial<VaultEntry>;937  if (flat.type === "passkey") return true;938  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.939  if (flat.rpId !== undefined && flat.passkey === undefined) return true;940  return false;941}942943function decodePayload(plain: string): DecryptedPayload {944  const parsed = JSON.parse(plain) as945    | Array<VaultEntry | LegacyVaultEntry>946    | VaultPayload;947948  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];949  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);950951  const normalized = rawEntries.map(normalizeEntry);952  const { merged } = mergePasskeyOnlyIntoLogins(normalized);953954  if (Array.isArray(parsed)) {955    return {956      entries: normalized,957      duressConfigured: false,958      migratedFromLegacy: true, // pre-A-2 array shape always needs save959    };960  }961962  return {963    entries: normalized,964    duressConfigured: parsed.duressConfigured === true,965    migratedFromLegacy: flatMigrationNeeded || merged,966  };967}968969/**970 * One-time consolidation pass run alongside the legacy passkey migration.971 *972 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`973 * entry per registration — so users who tested registration multiple times974 * accumulated several passkey-bearing entries for the same RP, and the975 * migration to the unified shape carried that duplication forward. This976 * pass collapses the duplicates:977 *978 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.979 *   2. Within each group, the most recently-active credential wins (prefer980 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).981 *   3. If a NON-passkey login entry exists for that RP (matched by url or982 *      additionalUrls hostname), attach the winning passkey to it and983 *      soft-delete every passkey-bearing entry in the group.984 *   4. Otherwise, soft-delete every loser in the group, keeping the winner985 *      as a passkey-only entry.986 *987 * Soft delete (deletedAt) rather than splice — the trash flow keeps the988 * record around for retention and the user can restore if the heuristic989 * picked wrong. The `merged` flag goes back to decodePayload so the990 * unlock path persists the consolidated shape immediately.991 */992function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {993  let merged = false;994  const now = Date.now();995996  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);997  const byRpId = new Map<string, VaultEntry[]>();998  for (const e of candidates) {999    const rpId = e.passkey!.rpId;1000    const list = byRpId.get(rpId);1001    if (list) list.push(e);1002    else byRpId.set(rpId, [e]);1003  }10041005  for (const [rpId, group] of byRpId) {1006    if (group.length === 0) continue;1007    group.sort((a, b) => {1008      const aT =1009        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;1010      const bT =1011        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;1012      return bT - aT;1013    });1014    const winner = group[0]!;10151016    // Look for a NON-passkey login entry matching this RP. Use the URL1017    // hostname rather than a substring search so a "login.foo.com" entry1018    // doesn't accidentally absorb a passkey for "foo.com".1019    const loginTarget = entries.find((e) => {1020      if (e.deletedAt || e.passkey || e.type !== "login") return false;1021      if (e.url && extractDomain(e.url) === rpId) return true;1022      for (const extra of e.additionalUrls ?? []) {1023        if (extractDomain(extra) === rpId) return true;1024      }1025      return false;1026    });10271028    if (loginTarget) {1029      loginTarget.passkey = winner.passkey;1030      // A passkey entry may also carry the site's TOTP seed. Preserve it when1031      // the passkey is consolidated onto an existing login instead of1032      // soft-deleting the only record that held the 2FA configuration.1033      if (!loginTarget.totp && winner.totp) {1034        loginTarget.totp = winner.totp;1035        loginTarget.totpAlgorithm = winner.totpAlgorithm;1036        loginTarget.totpDigits = winner.totpDigits;1037        loginTarget.totpPeriod = winner.totpPeriod;1038      }1039      loginTarget.modifiedAt = now;1040      for (const dup of group) {1041        if (!dup.deletedAt) {1042          dup.deletedAt = now;1043          dup.modifiedAt = now;1044        }1045      }1046      merged = true;1047    } else if (group.length > 1) {1048      for (let i = 1; i < group.length; i += 1) {1049        const dup = group[i]!;1050        if (!dup.deletedAt) {1051          dup.deletedAt = now;1052          dup.modifiedAt = now;1053        }1054      }1055      merged = true;1056    }1057  }10581059  return { merged };1060}10611062// Validates that a `StoredVault` record on disk has the field shape we expect1063// before treating an AES-GCM decrypt success as a clean unlock. AES-GCM only1064// authenticates the static VERSION_BYTE AAD, not the `version` field on the1065// outer record — a corrupted `version: 42` slot still decrypts cleanly. This1066// check surfaces that as `corrupted` (M7/COR-007) so the popup can offer a1067// history-restore path instead of falsely reporting "wrong password".1068function hasValidVaultSchema(v: unknown): v is StoredVault {1069  if (typeof v !== "object" || v === null) return false;1070  const r = v as Record<string, unknown>;1071  return (1072    r.version === 1 &&1073    typeof r.salt === "string" &&1074    typeof r.nonce === "string" &&1075    typeof r.ciphertext === "string" &&1076    typeof r.tag === "string"1077  );1078}10791080export type TryDecryptResult =1081  | { status: "ok"; payload: DecryptedPayload }1082  | { status: "wrong_key" }1083  | { status: "corrupted"; reason: string };10841085// Exported solely for `unlockFromCodex` below, which needs to verify a1086// supplied passphrase against a slotA blob *without* committing anything to1087// storage. Keep this internal otherwise — callers crossing the messages.ts1088// boundary should always go through `unlock` / `verifyMainPassword` so the1089// constant-time invariants in Projects.md §3.4 are not bypassed.1090export async function tryDecryptSlot(1091  vault: StoredVault,1092  key: Uint8Array,1093  aadTag?: string,1094): Promise<DecryptedPayload | null> {1095  const res = await tryDecryptSlotResult(vault, key, aadTag);1096  return res.status === "ok" ? res.payload : null;1097}10981099export async function tryDecryptSlotResult(1100  vault: StoredVault,1101  key: Uint8Array,1102  aadTag?: string,1103): Promise<TryDecryptResult> {1104  if (!hasValidVaultSchema(vault)) {1105    return { status: "corrupted", reason: "invalid_slot_schema" };1106  }1107  let plain: string;1108  try {1109    plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag, aadTag);1110  } catch {1111    return { status: "wrong_key" };1112  }1113  try {1114    const parsed = JSON.parse(plain) as unknown;1115    if (typeof parsed !== "object" || parsed === null) {1116      return { status: "corrupted", reason: "invalid_payload_shape" };1117    }1118    if (!Array.isArray(parsed) && !Array.isArray((parsed as VaultPayload).entries)) {1119      return { status: "corrupted", reason: "invalid_payload_shape" };1120    }1121    return { status: "ok", payload: decodePayload(plain) };1122  } catch {1123    return { status: "corrupted", reason: "invalid_payload_json" };1124  }1125}11261127// ---------------------------------------------------------------------------1128// Core operations1129// ---------------------------------------------------------------------------11301131function buildDomainIndexSync(all: VaultEntry[]): void {1132  domainIndex = new Map();11331134  for (const entry of all) {1135    if (entry.deletedAt) continue;1136    if (entry.type === "note" || entry.type === "card") continue;11371138    // An entry can match multiple sites via `additionalUrls`. Index each1139    // hostname so `queryDomain` returns the entry regardless of which alias1140    // the user is currently visiting. Dedupe per-entry so a single entry1141    // never appears twice in one bucket.1142    const domains = new Set<string>();1143    const primary = extractDomain(entry.url ?? "");1144    if (primary) domains.add(primary);1145    for (const extra of entry.additionalUrls ?? []) {1146      const d = extractDomain(extra);1147      if (d) domains.add(d);1148    }1149    if (domains.size === 0) continue;11501151    for (const domain of domains) {1152      const bucket = domainIndex.get(domain) ?? [];1153      bucket.push(entry);1154      domainIndex.set(domain, bucket);1155    }1156  }1157}11581159export async function createVault(password: string): Promise<void> {1160  await ensureMigrated();11611162  const salt = generateSalt();1163  derivedKey = await deriveKey(password, salt);1164  entries = [];1165  vaultMode = "real";1166  duressConfigured = false;1167  buildDomainIndexSync(entries);11681169  // Generate and wrap the root key for recovery word escrow.1170  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =1171    await generateAndWrapRootKey();1172  rootKey = unhex(rawRootKey);11731174  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);1175  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");11761177  // Second wrapping of rootKey under derivedKey so future unlocks can rehydrate1178  // it without recovery words (H2/COR-006).1179  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);11801181  const slotA: StoredVault = {1182    version: 1,1183    salt: hex(salt),1184    nonce,1185    ciphertext,1186    tag,1187    recovery_salt: recoverySalt,1188    recovery_wrapped_root_key: wrappedRootKey,1189    recovery_nonce: wrappedNonce,1190    recovery_tag: wrappedTag,1191    ...pwRoot,1192  };1193  await writeSlot(SLOT_A, slotA);11941195  // Matching-size noise blob for slot B, so a fresh install shows two1196  // similarly-shaped records regardless of whether duress is ever set up.1197  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1198  await stampLocalVaultMutation();11991200  touchAutoLock();1201}12021203/**1204 * Creates a local vault from an existing vault root key received through the1205 * SAS-gated adoption protocol. The password is local to this device; it wraps1206 * the adopted root key at rest and does not need to match the source device.1207 */1208export async function createVaultFromRootKey(1209  password: string,1210  rootKeyHex: string,1211): Promise<{ vaultId: string }> {1212  if (typeof password !== "string" || password.length === 0) {1213    throw new Error("ADOPT_INVALID_PASSWORD");1214  }1215  if (typeof rootKeyHex !== "string" || !/^[0-9a-f]{64}$/.test(rootKeyHex)) {1216    throw new Error("ADOPT_INVALID_ROOT_KEY: expected 64 lowercase hex chars");1217  }12181219  const rootKeyBytes = unhex(rootKeyHex);1220  if (rootKeyBytes.length !== 32) {1221    rootKeyBytes.fill(0);1222    throw new Error("ADOPT_INVALID_ROOT_KEY: expected 32 bytes");1223  }12241225  let nextDerivedKey: Uint8Array | null = null;1226  let sessionRootKey: Uint8Array | null = null;1227  try {1228    await ensureMigrated();1229    if ((await loadSlot(SLOT_A)) !== null) {1230      throw new Error("ADOPT_VAULT_ALREADY_EXISTS");1231    }12321233    const nextEntries: VaultEntry[] = [];1234    const nextDuressConfigured = false;1235    const vaultId = await computeVaultId(rootKeyBytes);1236    const salt = generateSalt();1237    nextDerivedKey = await deriveKey(password, salt);1238    const recovery = await wrapExistingRootKeyForRecovery(rootKeyBytes);12391240    sessionRootKey = new Uint8Array(rootKeyBytes);1241    const plaintext = encodePayload(nextEntries, nextDuressConfigured, 0);1242    const { nonce, ciphertext, tag } = await encrypt(nextDerivedKey, plaintext, "slot_a_v1");1243    const pwRoot = await wrapRootKeyForDerivedKey(nextDerivedKey, sessionRootKey);1244    const slotA: StoredVault = {1245      version: 1,1246      salt: hex(salt),1247      nonce,1248      ciphertext,1249      tag,1250      recovery_salt: recovery.recoverySalt,1251      recovery_wrapped_root_key: recovery.wrappedRootKey,1252      recovery_nonce: recovery.wrappedNonce,1253      recovery_tag: recovery.wrappedTag,1254      ...pwRoot,1255    };1256    const slotB = generateNoiseBlob(ciphertext.length / 2);1257    await commitCreatedVaultSlots(slotA, slotB);12581259    const previousDerivedKey = derivedKey;1260    const previousRootKey = rootKey;1261    derivedKey = nextDerivedKey;1262    rootKey = sessionRootKey;1263    nextDerivedKey = null;1264    sessionRootKey = null;1265    entries = nextEntries;1266    vaultMode = "real";1267    duressConfigured = nextDuressConfigured;1268    buildDomainIndexSync(entries);1269    if (previousDerivedKey && previousDerivedKey !== derivedKey) zeroKey(previousDerivedKey);1270    if (previousRootKey && previousRootKey !== rootKey) previousRootKey.fill(0);1271    touchAutoLock();12721273    return { vaultId };1274  } finally {1275    rootKeyBytes.fill(0);1276    if (nextDerivedKey) zeroKey(nextDerivedKey);1277    if (sessionRootKey) sessionRootKey.fill(0);1278  }1279}12801281export async function hasVault(): Promise<boolean> {1282  await ensureMigrated();1283  return (await loadSlot(SLOT_A)) !== null;1284}12851286/**1287 * Returns whether the real vault has recorded that a duress passphrase is1288 * configured. Reliable only after a real-mode unlock — before unlock or in1289 * decoy mode, returns false because we cannot read slot A's plaintext.1290 */1291export async function isDuressEnabled(): Promise<boolean> {1292  return vaultMode === "real" && duressConfigured;1293}12941295export async function setupDuress(duressPassword: string): Promise<void> {1296  if (!derivedKey || vaultMode !== "real") {1297    throw new Error("vault must be unlocked in real mode");1298  }1299  await ensureMigrated();13001301  // Encrypt the decoy payload (empty entries) under the duress key. Pad to1302  // at least the current slot A ciphertext size so the two slots look the1303  // same at rest at setup time.1304  const slotA = await loadSlot(SLOT_A);1305  if (!slotA) throw new Error("missing slot A");13061307  const decoySalt = generateSalt();1308  const decoyKey = await deriveKey(duressPassword, decoySalt);1309  try {1310    // Pad the decoy to the fixed duress ceiling. Slot A is re-encrypted to the1311    // same ceiling by the persistSlotA() call below (duressConfigured is now1312    // true), so both slots are exactly DURESS_SLOT_SIZE+tag at rest and their1313    // lengths cannot reveal that duress is configured (audit MEDIUM).1314    const plaintext = encodePayload([], false, DURESS_SLOT_SIZE);1315    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext, "slot_b_v1");1316    await writeSlot(SLOT_B, {1317      version: 1,1318      salt: hex(decoySalt),1319      nonce,1320      ciphertext,1321      tag,1322    });1323  } finally {1324    zeroKey(decoyKey);1325  }13261327  // Re-encrypt slot A with the duressConfigured flag set to true. No external1328  // flag is written — the bit lives inside the encrypted envelope.1329  duressConfigured = true;1330  await persistSlotA();1331}13321333export async function disableDuress(): Promise<void> {1334  if (!derivedKey || vaultMode !== "real") {1335    throw new Error("vault must be unlocked in real mode");1336  }1337  await ensureMigrated();13381339  // Overwrite slot B with fresh noise that matches slot A's size. The slot1340  // remains occupied so its mere existence does not signal that duress was1341  // ever configured.1342  const slotA = await loadSlot(SLOT_A);1343  if (slotA) {1344    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));1345  }13461347  duressConfigured = false;1348  await persistSlotA();1349}13501351// L14/COR-015: this function intentionally checks slot A (real vault) only.1352// A duress-mode user attempting changePassword is rejected earlier by the1353// vaultMode === 'decoy' check at changePassword. The real-vs-decoy1354// distinction here is by design — duress users cannot rotate the real1355// password by guessing it, and the asymmetry in error timing is acceptable1356// because duress mode is opt-in and the user knows their own decoy key.1357export async function verifyMainPassword(password: string): Promise<boolean> {1358  await ensureMigrated();1359  const slotA = await loadSlot(SLOT_A);1360  if (!slotA) return false;1361  const key = await deriveKey(password, unhex(slotA.salt));1362  try {1363    return (await tryDecryptSlot(slotA, key, "slot_a_v1")) !== null;1364  } finally {1365    zeroKey(key);1366  }1367}13681369export async function unlock(password: string): Promise<boolean> {1370  // LV-1: the normal path NFC-normalizes inside deriveKey. If that fails and1371  // the supplied password is not already in NFC form, the vault may have been1372  // sealed (pre-LV-1) with the raw, un-normalized bytes — retry with those.1373  if (await attemptUnlockWithDerive(password, deriveKey)) return true;13741375  const nfc = password.normalize("NFC");1376  if (password === nfc) return false; // ASCII/NFC password — no legacy form to try13771378  if (await attemptUnlockWithDerive(password, deriveKeyRaw)) {1379    // Heal: re-seal the real slot under the NFC-normalized key so every1380    // future unlock takes the normal path. Only the real vault is re-sealed;1381    // a non-NFC duress password keeps working through this fallback.1382    if (vaultMode === "real") {1383      try {1384        const newSalt = generateSalt();1385        const newKey = await deriveKey(password, newSalt);1386        await changePasswordWithNewKey(newSalt, newKey);1387      } catch (healErr) {1388        console.warn("[Peach] LV-1 NFC heal failed:", healErr);1389      }1390    }1391    return true;1392  }1393  return false;1394}13951396async function attemptUnlockWithDerive(1397  password: string,1398  derive: typeof deriveKey,1399): Promise<boolean> {1400  await ensureMigrated();14011402  // Load both slots in parallel. Both always exist post-migration; the1403  // parallel load and the parallel derivation below keep wall-clock time1404  // constant regardless of which slot's passphrase (real or decoy) the1405  // caller supplied, so a timing side channel cannot distinguish the1406  // duress path from the real-unlock path (DUR-002).1407  const [slotA, slotB] = await Promise.all([1408    loadSlot(SLOT_A),1409    loadSlot(SLOT_B),1410  ]);14111412  // Derive both keys in parallel, unconditionally.1413  const [keyA, keyB] = await Promise.all([1414    slotA ? derive(password, unhex(slotA.salt)) : Promise.resolve(null),1415    slotB ? derive(password, unhex(slotB.salt)) : Promise.resolve(null),1416  ]);14171418  // Attempt both decrypts in parallel. The Result variant lets us distinguish1419  // 'wrong_key' from 'corrupted' so callers can offer a history-restore path1420  // when a slot's schema is invalid even though AES-GCM authenticated (M7).1421  const [resultA, resultB] = await Promise.all([1422    keyA && slotA1423      ? tryDecryptSlotResult(slotA, keyA, "slot_a_v1")1424      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1425    keyB && slotB1426      ? tryDecryptSlotResult(slotB, keyB, "slot_b_v1")1427      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1428  ]);1429  const payloadA = resultA.status === "ok" ? resultA.payload : null;1430  const payloadB = resultB.status === "ok" ? resultB.payload : null;14311432  if (payloadA && keyA) {1433    derivedKey = keyA;1434    void saveSessionKey(keyA, "real");1435    vaultMode = "real";1436    entries = payloadA.entries;1437    duressConfigured = payloadA.duressConfigured;1438    buildDomainIndexSync(entries);1439    await loadSettings();1440    touchAutoLock();1441    if (keyB) zeroKey(keyB);14421443    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).1444    if (slotA) {1445      const needsRecoverySetup =1446        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;1447      if (!needsRecoverySetup) {1448        rootKey = await deriveRootKeyFromDerivedKey(keyA, slotA);1449      }1450      // Pre-v2.12 vault, or a vault whose pw_root_* fields haven't been written1451      // yet: generate the recovery escrow and seed pw_root_* so subsequent1452      // unlocks can rehydrate rootKey without the user's recovery words.1453      // Best-effort — a failure here must not block unlock.1454      if (needsRecoverySetup || !rootKey) {1455        try {1456          await migrateToRecovery();1457          if (needsRecoverySetup) {1458            // String "1" matches the popup's existing peach_recovery_pending1459            // check; surfaced so the user re-confirms recovery words after a1460            // legacy-vault unlock (H2/COR-006).1461            await chrome.storage.local.set({ peach_recovery_pending: '1' });1462          }1463        } catch (migrationErr) {1464          console.warn('[Peach] recovery migration failed:', migrationErr);1465        }1466      }1467    }14681469    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat1470    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so1471    // the next save uses the new format and migration doesn't re-run forever.1472    if (payloadA.migratedFromLegacy) {1473      await persistSlotA();1474    }1475    await lockedSyncUnlockHook?.().catch((error) => {1476      console.warn("[Peach sync] locked inbox drain failed:", error);1477    });1478    return true;1479  }14801481  if (payloadB && keyB) {1482    derivedKey = keyB;1483    void saveSessionKey(keyB, "decoy");1484    vaultMode = "decoy";1485    entries = payloadB.entries;1486    duressConfigured = false;1487    buildDomainIndexSync(entries);1488    await loadSettings();1489    touchAutoLock();1490    if (keyA) zeroKey(keyA);1491    return true;1492  }14931494  if (keyA) zeroKey(keyA);1495  if (keyB) zeroKey(keyB);1496  // Surface corruption (good AES tag, broken schema) distinctly from1497  // wrong-password so the popup can offer history-restore (M7/COR-007).1498  if (resultA.status === "corrupted") {1499    lastUnlockFailureReason = resultA.reason;1500  } else if (resultB.status === "corrupted") {1501    lastUnlockFailureReason = resultB.reason;1502  }1503  return false;1504}15051506// ---------------------------------------------------------------------------1507// MV3 service worker session key — survives SW restarts within a browser session1508// ---------------------------------------------------------------------------15091510// chrome.storage.session is cleared on browser close; survives SW restart.1511// Firefox does not implement it (persistent background script, no restart risk).1512const SESSION_KEY_STORAGE = "peach_session_dk";15131514async function saveSessionKey(key: Uint8Array, mode: "real" | "decoy"): Promise<void> {1515  if (!chrome.storage.session) return;1516  await chrome.storage.session.set({ [SESSION_KEY_STORAGE]: { keyHex: hex(key), mode } });1517}15181519async function clearSessionKey(): Promise<void> {1520  if (!chrome.storage.session) return;1521  await chrome.storage.session.remove(SESSION_KEY_STORAGE);1522}15231524/**1525 * On SW restart, re-derive all in-memory vault state from the session key1526 * stored in chrome.storage.session (set at unlock time, cleared by lock();1527 * also cleared on browser close or when restoration fails).1528 * Returns true if the vault was restored, false if no session key was found.1529 * No-ops if the vault is already unlocked.1530 */1531export async function restoreFromSession(): Promise<boolean> {1532  if (derivedKey !== null) return true;1533  if (!chrome.storage.session) return false;1534  const stored = await chrome.storage.session.get(SESSION_KEY_STORAGE);1535  const entry = stored[SESSION_KEY_STORAGE] as { keyHex: string; mode: "real" | "decoy" } | undefined;1536  if (!entry?.keyHex) return false;15371538  const key = unhex(entry.keyHex);1539  const slotKey = entry.mode === "real" ? SLOT_A : SLOT_B;1540  const aadTag = entry.mode === "real" ? "slot_a_v1" : "slot_b_v1";1541  const slot = await loadSlot(slotKey);1542  if (!slot) { await clearSessionKey(); return false; }15431544  const result = await tryDecryptSlotResult(slot, key, aadTag);1545  if (result.status !== "ok") { await clearSessionKey(); return false; }15461547  derivedKey = key;1548  vaultMode = entry.mode;1549  entries = result.payload.entries;1550  duressConfigured = entry.mode === "real" ? result.payload.duressConfigured : false;1551  buildDomainIndexSync(entries);1552  await loadSettings();1553  touchAutoLock();15541555  // The rootKey is what identity/sync derivations use. deriveRootKeyFromDerivedKey1556  // pulls it from the pw_root_* fields on the slot — set on every createVault +1557  // re-set on every password change (H2/COR-006). The previous gate on1558  // recovery_salt + recovery_wrapped_root_key silently left rootKey unset for1559  // any vault that hadn't been through migrateToRecovery, which broke background1560  // sync: the SW re-woke from the alarm, isUnlocked() was true (the session key1561  // restored derivedKey + entries), but getRootKeyHex() returned null and the1562  // runner returned VAULT_LOCKED. The fix is to always try the pw_root path1563  // when the slot has those fields — the recovery fields are independent.1564  if (entry.mode === "real") {1565    try {1566      rootKey = await deriveRootKeyFromDerivedKey(key, slot);1567    } catch {1568      /* non-fatal — sync identity will still skip on the runner's null-rootKey check */1569    }1570  }15711572  await lockedSyncUnlockHook?.().catch((error) => {1573    console.warn("[Peach sync] locked inbox drain failed:", error);1574  });15751576  return true;1577}15781579export function lock(): void {1580  // A user-initiated lock must be durable across MV3 service-worker restarts.1581  // The SW is idle-killed after ~30s of inactivity, and the popup's next open1582  // wakes a cold SW whose restoreFromSession() reads chrome.storage.session.1583  // If the session key survived lock(), that restore would silently re-unlock1584  // the vault ~a minute after the user locked it — a lock that only held1585  // while the SW happened to stay alive. The background-sync convenience the1586  // session key once bought (sync runner re-waking to a restored vault) is1587  // not worth an unlocked vault the user never asked to unlock; when locked,1588  // sync-runner correctly reports VAULT_LOCKED instead.1589  void clearSessionKey();1590  if (derivedKey) {1591    zeroKey(derivedKey);1592    derivedKey = null;1593  }1594  if (rootKey) {1595    rootKey.fill(0);1596    rootKey = null;1597  }15981599  entries = [];1600  domainIndex = new Map();1601  vaultMode = "real";1602  duressConfigured = false;1603  clearAutoLock();1604}16051606export function isUnlocked(): boolean {1607  return derivedKey !== null;1608}16091610// Opens the vault using a pre-derived AES-256 key (skipping Argon2id). The1611// PIN-unlock path unwraps this key from its PIN wrap; the passphrase path1612// always derives from scratch via Argon2id.1613export async function unlockWithKey(1614  masterKeyHex: string,1615): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {1616  try {1617    const masterKeyBytes = unhex(masterKeyHex);1618    const stored = await loadSlot(SLOT_A);1619    if (!stored) return { ok: false, error: "invalid_key" };1620    const payload = await tryDecryptSlot(stored, masterKeyBytes, "slot_a_v1");1621    if (!payload) return { ok: false, error: "invalid_key" };16221623    derivedKey = masterKeyBytes;1624    vaultMode = "real";1625    entries = payload.entries;1626    duressConfigured = payload.duressConfigured;1627    buildDomainIndexSync(entries);1628    await loadSettings();1629    touchAutoLock();16301631    // PCH-01-009: the biometric path skips Argon2id but must still rehydrate the1632    // root key, exactly like attemptUnlockWithDerive and restoreFromSession.1633    // Without it, rootKey stays null after a biometric unlock and sync +1634    // GET_RECOVERY_WORDS report the open vault as locked. Guarded and non-fatal:1635    // a missing/legacy escrow must not fail the unlock itself.1636    if (stored.recovery_salt && stored.recovery_wrapped_root_key) {1637      try {1638        rootKey = await deriveRootKeyFromDerivedKey(masterKeyBytes, stored);1639      } catch { /* non-fatal — unlock still succeeds without rootKey */ }1640    }1641    return { ok: true, entries };1642  } catch {1643    return { ok: false, error: "invalid_key" };1644  }1645}16461647// --- Adoption duress-slot helpers (passthrough, no decrypt) ---16481649/**1650 * Adoption SOURCE helper: returns the sealed slot B as a JSON string when1651 * duress is configured and slot B exists, so the inline snapshot can ship it1652 * to the receiver. Returns null otherwise. Never decrypts.1653 */1654export async function exportDuressSlotForAdoption(): Promise<string | null> {1655  if (!(await isDuressEnabled())) return null;1656  await ensureMigrated();1657  const slotB = await loadSlot(SLOT_B);1658  if (!slotB) return null;1659  return JSON.stringify(slotB);1660}16611662/**1663 * Adoption RECEIVER helper: installs a duress slot received from a source1664 * device. Validates the StoredVault shape (same guard as unlockFromCodex)1665 * and writes it to slot B. Throws on invalid shape so the caller can treat1666 * it as a non-fatal snapshot failure. Never decrypts.1667 */1668export async function installAdoptedDuressSlot(json: string): Promise<void> {1669  let parsed: StoredVault;1670  try {1671    const raw = JSON.parse(json) as unknown;1672    if (!hasValidVaultSchema(raw)) {1673      throw new Error("invalid_duress_slot_shape");1674    }1675    parsed = raw;1676  } catch (err) {1677    if (err instanceof SyntaxError) {1678      throw new Error("invalid_duress_slot_shape");1679    }1680    throw err;1681  }1682  await writeSlot(SLOT_B, parsed);1683}16841685/**1686 * Restore the vault from a parsed Peach Codex payload.1687 *1688 * The supplied passphrase is verified against the Codex's slotA *before* any1689 * storage mutation — wrong passphrases return without touching disk. On1690 * success, the current vault state is snapshotted (reason: "restore") so the1691 * user can revert via Settings -> Vault History, then slotA/slotB are written1692 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1693 * regular `unlock()` follows so the rest of the SW sees the standard1694 * post-unlock state.1695 *1696 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1697 * printed by a Pro user must always be restorable, even on a free device.1698 *1699 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1700 * is an explicit user action with a known passphrase, not a passive lock1701 * screen, so the duress-vs-real timing side channel does not apply.1702 */1703export async function unlockFromCodex(params: {1704  slotA: string; // JSON string of StoredVault for slot A1705  slotB: string | null; // JSON string of StoredVault for slot B, or null1706  timestamp: number;1707  masterPassphrase: string;1708}): Promise<1709  | { ok: true; entryCount: number }1710  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1711> {1712  let parsedSlotA: StoredVault;1713  let parsedSlotB: StoredVault | null = null;1714  try {1715    const rawA = JSON.parse(params.slotA) as unknown;1716    if (!hasValidVaultSchema(rawA)) {1717      return { ok: false, reason: "invalid_slot_format" };1718    }1719    parsedSlotA = rawA;1720    if (params.slotB !== null) {1721      const rawB = JSON.parse(params.slotB) as unknown;1722      if (!hasValidVaultSchema(rawB)) {1723        return { ok: false, reason: "invalid_slot_format" };1724      }1725      parsedSlotB = rawB;1726    }1727  } catch {1728    return { ok: false, reason: "invalid_slot_format" };1729  }17301731  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1732  // If this fails the on-disk vault is left completely untouched.1733  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1734  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey, "slot_a_v1");1735  if (!trialPayload) {1736    zeroKey(trialKey);1737    return { ok: false, reason: "wrong_passphrase" };1738  }1739  // Drop the trial key; `unlock()` below derives its own copy. Holding two1740  // copies of the master key in memory at once would be a needless surface.1741  zeroKey(trialKey);17421743  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1744  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1745  await takeVaultSnapshot("restore");17461747  await writeSlot(SLOT_A, parsedSlotA);1748  if (parsedSlotB) {1749    await writeSlot(SLOT_B, parsedSlotB);1750  } else {1751    // Slot B must always exist with ciphertext length matching slot A1752    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1753    // configured); regenerate noise to satisfy the invariant.1754    await writeSlot(1755      SLOT_B,1756      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1757    );1758  }1759  await chrome.storage.local.set({1760    peach_last_modified_at: String(params.timestamp),1761  });17621763  // Standard unlock path so domain index, settings, auto-lock, and1764  // recovery-key state all populate exactly as a normal unlock would.1765  const ok = await unlock(params.masterPassphrase);1766  if (!ok) {1767    // Should be unreachable — we just verified the passphrase against the1768    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1769    // simple if some race or storage anomaly intrudes.1770    return { ok: false, reason: "wrong_passphrase" };1771  }1772  return { ok: true, entryCount: getEntries().length };1773}17741775// Returns the current in-memory derived key as a hex string. Only the PIN1776// unlock path needs this value, and it reads module state directly; this1777// accessor exists so unlockWithKey's root-key rehydration is testable.1778export /* @testable */ function getDerivedKeyHex(): string {1779  if (!derivedKey) throw new Error("vault_not_unlocked");1780  return hex(derivedKey);1781}17821783/**1784 * Returns the vault's stable root key as hex, or null when locked / not yet1785 * populated. The root key is random per-vault, generated at creation, escrowed1786 * under the recovery words, and re-wrapped (not regenerated) on every password1787 * change — so it is INVARIANT across password rotation, unlike getDerivedKeyHex.1788 *1789 * This is the correct input for sync identity derivation (vault_id, the Noise1790 * NK static key, the Ed25519 manifest signing key, and per-entry envelope1791 * keys): paired devices keep the same identity and can still decrypt each1792 * other's entries after either side rotates its master password (SEV-2-B).1793 *1794 * Returns null rather than throwing so sync callers degrade to VAULT_LOCKED1795 * instead of crashing; rootKey is populated on every real-mode unlock via1796 * deriveRootKeyFromDerivedKey / migrateToRecovery.1797 */1798export function getRootKeyHex(): string | null {1799  return rootKey ? hex(rootKey) : null;1800}18011802export function getEntries(): VaultEntry[] {1803  if (!derivedKey) throw new Error("vault_locked");1804  return entries.filter((entry) => !entry.deletedAt);1805}18061807const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;18081809export function getIdentities(): SavedIdentity[] {1810  if (!derivedKey) throw new Error("vault_locked");1811  const active = entries.filter((e) => !e.deletedAt && e.type === "login");18121813  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1814  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();18151816  for (const entry of active) {1817    const domain = extractDomain(entry.url ?? "") ?? "";1818    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;18191820    function tally(map: Map<string, { lastUsed: number; count: number; domain: string }>, v: string) {1821      const existing = map.get(v);1822      if (!existing || modifiedAt > existing.lastUsed) {1823        map.set(v, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1824      } else {1825        map.set(v, { ...existing, count: existing.count + 1 });1826      }1827    }18281829    const uVal = entry.username?.trim();1830    if (uVal) tally(EMAIL_RE.test(uVal) ? emailMap : usernameMap, uVal);18311832    // Also count the dedicated email field — entries created via the inline1833    // popup new-entry form store email here rather than in username.1834    const eVal = entry.email?.trim();1835    if (eVal) tally(emailMap, eVal);1836  }18371838  const toIdentity = (type: "email" | "username") =>1839    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1840      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1841    });18421843  return [1844    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1845    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1846  ];1847}18481849export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1850  if (!derivedKey) throw new Error("vault_locked");18511852  const now = Date.now();1853  const added: VaultEntry[] = [];1854  for (const draft of drafts) {1855    try {1856      const sanitized = sanitizeEntryDraft(draft);1857      const nextEntry: VaultEntry = {1858        ...sanitized,1859        id: crypto.randomUUID(),1860        createdAt: now,1861        modifiedAt: monotonicEntryTimestamp(),1862      };1863      entries.push(nextEntry);1864      added.push(nextEntry);1865    } catch {1866      // Skip invalid drafts silently — caller can see diff between input and output length1867    }1868  }18691870  if (added.length > 0) {1871    await persistEntries();1872  }18731874  return added;1875}18761877/**1878 * Restore a set of entries in full fidelity — preserves id, createdAt,1879 * modifiedAt, and every other field unlike `batchAddEntries` which1880 * assigns fresh identity. Used by restore paths where the backup is the1881 * authoritative source of truth. `mode` controls how entries already in1882 * the vault are treated.1883 */1884export async function restoreEntries(1885  incoming: VaultEntry[],1886  mode: "replace" | "merge"1887): Promise<{ added: number; updated: number; skipped: number }> {1888  if (!derivedKey) throw new Error("vault_locked");18891890  // A replace with zero incoming entries is almost certainly a caller bug1891  // (e.g. an import that silently dropped everything). Refuse rather than1892  // wipe the vault.1893  if (mode === "replace" && incoming.length === 0) {1894    throw new Error("refuse to replace with empty entry set");1895  }18961897  let added = 0;1898  let updated = 0;1899  let skipped = 0;19001901  if (mode === "replace") {1902    entries = [];1903    for (const entry of incoming) {1904      const normalized = sanitizeRestoredEntry(entry);1905      if (!normalized) {1906        skipped += 1;1907        continue;1908      }1909      entries.push(normalized);1910      added += 1;1911    }1912  } else {1913    const byId = new Map<string, VaultEntry>();1914    for (const e of entries) byId.set(e.id, e);19151916    for (const entry of incoming) {1917      const normalized = sanitizeRestoredEntry(entry);1918      if (!normalized) {1919        skipped += 1;1920        continue;1921      }1922      const existing = byId.get(normalized.id);1923      if (!existing) {1924        entries.push(normalized);1925        added += 1;1926      } else if (normalized.modifiedAt > existing.modifiedAt) {1927        const idx = entries.findIndex((e) => e.id === existing.id);1928        if (idx !== -1) {1929          entries[idx] = normalized;1930          updated += 1;1931        }1932      } else {1933        skipped += 1;1934      }1935    }1936  }19371938  await persistEntries("import");1939  return { added, updated, skipped };1940}19411942/**1943 * Validate + normalise a VaultEntry coming from a backup, import, or sync1944 * peer. MV-6: this is a strict field WHITELIST — `normalizeEntry` copies1945 * only known VaultEntry fields (unknown/attacker keys are dropped), and1946 * nested `passkey`/`canary` shapes are rebuilt field-by-field, never1947 * spread. Returns null for values the live vault shouldn't accept.1948 */1949function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1950  if (!entry || typeof entry !== "object") return null;1951  const title = typeof entry.title === "string" ? entry.title.trim() : "";1952  if (!title) return null;1953  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1954  const now = Date.now();1955  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1956  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;19571958  // normalizeEntry whitelists every known VaultEntry field and runs the1959  // same legacy-passkey migration as the on-disk read path — pre-v2.11.01960  // backups with flat passkey fields still import correctly because the1961  // legacy flat fields are themselves whitelisted VaultEntry members.1962  const normalized = normalizeEntry({1963    ...entry,1964    id,1965    title,1966    type: entry.type ?? "login",1967    createdAt,1968    modifiedAt,1969  });19701971  if (normalized.passkey) {1972    normalized.passkey = sanitizePasskey(normalized.passkey);1973  }1974  if (normalized.canary) {1975    normalized.canary = sanitizeCanary(normalized.canary);1976  }1977  // PCH-01-016: bound tags on the restore path too, so both import funnels agree.1978  normalized.tags = sanitizeTags(normalized.tags);19791980  // Login entries from backup must carry a password (or be passkey-only,1981  // in which case the passkey sub-object replaces the password requirement).1982  if (1983    normalized.type === "login" &&1984    !normalized.passkey &&1985    typeof normalized.password !== "string"1986  ) {1987    return null;1988  }1989  if (entry.passkey && !normalized.passkey) {1990    // Entry claimed a passkey but the shape didn't validate — refuse the1991    // whole entry rather than silently importing a credential-less husk.1992    return null;1993  }19941995  return normalized;1996}19971998/**1999 * Rebuild a PasskeyData object from untrusted input, field by field.2000 * Returns undefined when required fields are missing/mistyped — a rogue2001 * passkey credential must not activate via import (MV-6 attack scenario).2002 */2003function sanitizePasskey(pk: unknown): PasskeyData | undefined {2004  if (!pk || typeof pk !== "object") return undefined;2005  const r = pk as Record<string, unknown>;2006  if (2007    typeof r.rpId !== "string" || !r.rpId ||2008    typeof r.credentialId !== "string" || !r.credentialId ||2009    typeof r.publicKeyCose !== "string" ||2010    typeof r.userHandle !== "string" ||2011    typeof r.userName !== "string"2012  ) {2013    return undefined;2014  }2015  return {2016    rpId: r.rpId,2017    rpName: typeof r.rpName === "string" ? r.rpName : r.rpId,2018    credentialId: r.credentialId,2019    publicKeyCose: r.publicKeyCose,2020    privateKeyJwk:2021      r.privateKeyJwk && typeof r.privateKeyJwk === "object"2022        ? (r.privateKeyJwk as JsonWebKey)2023        : undefined,2024    userHandle: r.userHandle,2025    userName: r.userName,2026    userDisplayName:2027      typeof r.userDisplayName === "string" ? r.userDisplayName : r.userName,2028    algorithm: r.algorithm === -8 ? -8 : -7,2029    signCount: typeof r.signCount === "number" ? r.signCount : 0,2030    aaguid: typeof r.aaguid === "string" ? r.aaguid : "",2031    backupEligible: r.backupEligible === true,2032    backupState: r.backupState === true,2033    lastUsedAt: typeof r.lastUsedAt === "number" ? r.lastUsedAt : undefined,2034    browser: r.browser === "chrome" || r.browser === "firefox" ? r.browser : undefined,2035    transports: Array.isArray(r.transports)2036      ? r.transports.filter((t): t is string => typeof t === "string")2037      : undefined,2038    createdAt: typeof r.createdAt === "number" ? r.createdAt : Date.now(),2039  };2040}20412042/**2043 * Rebuild a Canary object from untrusted input. The webhook must be a2044 * well-formed HTTPS URL — a poisoned canary.webhookUrl is a standing2045 * exfiltration endpoint that the canary subsystem will POST entry titles2046 * to (MV-6 attack scenario). Invalid → the canary is dropped, not the2047 * entry. FIRE_CANARY_PING re-validates with the full SSRF guard at fire2048 * time (isAllowedCanaryWebhookUrl), so this import-time check is the2049 * outer of two gates.2050 */2051function sanitizeCanary(c: unknown): Canary | undefined {2052  if (!c || typeof c !== "object") return undefined;2053  const r = c as Record<string, unknown>;2054  if (typeof r.webhookUrl !== "string") return undefined;2055  let url: URL;2056  try {2057    url = new URL(r.webhookUrl);2058  } catch {2059    return undefined;2060  }2061  if (url.protocol !== "https:") return undefined;2062  return {2063    webhookUrl: url.toString(),2064    label: typeof r.label === "string" ? r.label : undefined,2065    lastPinged: typeof r.lastPinged === "number" ? r.lastPinged : undefined,2066    pingCount: typeof r.pingCount === "number" ? r.pingCount : undefined,2067  };2068}20692070/**2071 * MV-6 / PCH-01-016: tags from imported / batch-added entries are untrusted.2072 * Keep only non-empty strings, cap each at 256 chars, and cap the count at 2552073 * (the same per-entry ceiling the export codec enforces, peachPayload.ts).2074 * Non-array or all-invalid input collapses to undefined.2075 */2076function sanitizeTags(tags: unknown): string[] | undefined {2077  if (!Array.isArray(tags)) return undefined;2078  const out: string[] = [];2079  for (const t of tags) {2080    if (typeof t !== "string") continue;2081    const trimmed = t.trim();2082    if (!trimmed) continue;2083    out.push(trimmed.slice(0, 256));2084    if (out.length >= 255) break;2085  }2086  return out.length > 0 ? out : undefined;2087}20882089// Soft-deletes every non-deleted entry in one call. The literal string2090// argument is a tripwire against accidental calls — any caller that would2091// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this2092// to a boolean.2093export async function clearVaultEntries(2094  confirmation: "REPLACE_ALL"2095): Promise<void> {2096  if (confirmation !== "REPLACE_ALL") {2097    throw new Error("clearVaultEntries requires explicit confirmation");2098  }2099  if (!derivedKey) throw new Error("vault_locked");2100  for (const entry of entries) {2101    if (!entry.deletedAt) {2102      const stamp = monotonicEntryTimestamp(entry.modifiedAt);2103      entry.deletedAt = stamp;2104      entry.modifiedAt = stamp;2105    }2106  }2107  await persistEntries();2108}21092110export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {2111  if (!derivedKey) throw new Error("vault_locked");21122113  const sanitized = sanitizeEntryDraft(entry);2114  const now = Date.now();2115  const stamp = monotonicEntryTimestamp();2116  const nextEntry: VaultEntry = {2117    ...sanitized,2118    id: crypto.randomUUID(),2119    createdAt: now,2120    modifiedAt: stamp,2121    // Stamp passwordChangedAt at creation so age tracking is accurate from2122    // day one. Non-login entries carry the same stamp harmlessly.2123    passwordChangedAt: sanitized.password ? stamp : undefined,2124  };21252126  // Clone-and-replace so a failed persist rolls back the in-memory state2127  // (COR-008). Without this, a quota-full or storage error leaves the2128  // module-level `entries` ahead of what's actually on disk.2129  const rollback = entries;2130  entries = [...entries, nextEntry];2131  try {2132    await persistEntries();2133  } catch (err) {2134    entries = rollback;2135    throw err;2136  }21372138  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)2139  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {2140    checkBreach(nextEntry.password).then(async (count) => {2141      if (count > 0) {2142        const idx = entries.findIndex((e) => e.id === nextEntry.id);2143        if (idx !== -1) {2144          entries[idx] = { ...entries[idx], breach: count, modifiedAt: monotonicEntryTimestamp(entries[idx].modifiedAt) };2145          await persistEntries();2146        }2147      }2148    });2149  }21502151  fireSyncTrigger("vault_change");21522153  return nextEntry;2154}21552156/**2157 * Run the passkey-merge pass on the live in-memory entries and persist if2158 * anything changed. Called immediately after any passkey registration so that2159 * a same-domain TOTP-only entry already in the vault gets consolidated right2160 * away instead of waiting until the next lock/unlock cycle.2161 */2162export async function consolidatePasskeyEntries(): Promise<void> {2163  if (!derivedKey) return;2164  const { merged } = mergePasskeyOnlyIntoLogins(entries);2165  if (merged) {2166    await persistEntries();2167  }2168}21692170export async function updateEntry(2171  id: string,2172  patch: Partial<VaultEntry>2173): Promise<void> {2174  if (!derivedKey) throw new Error("vault_locked");21752176  const idx = entries.findIndex((entry) => entry.id === id);2177  if (idx === -1 || entries[idx].deletedAt) {2178    throw new Error("entry_not_found");2179  }21802181  const current = entries[idx];2182  const sanitizedPatch = sanitizeEntryPatch(patch);2183  // Refresh passwordChangedAt only when the password value actually changes —2184  // a metadata-only update (title, tags, etc.) must not reset the age clock2185  // that drives the Pro health rotation recommendation.2186  const passwordChanged =2187    "password" in patch &&2188    typeof sanitizedPatch.password === "string" &&2189    sanitizedPatch.password !== current.password;2190  const stamp = monotonicEntryTimestamp(current.modifiedAt);2191  // When the password actually changes, push the outgoing one onto the history2192  // (newest first) so the user can reveal/copy/restore it later. We record the2193  // age it carried (passwordChangedAt, falling back to createdAt) rather than2194  // `now`, so the list reflects when each password was last in effect. Only2195  // capture a non-empty prior value — clearing a password leaves no secret to2196  // keep, and we skip an immediate exact duplicate (e.g. restore-then-restore)2197  // to avoid noise.2198  const priorPassword = current.password;2199  const shouldCapture =2200    passwordChanged &&2201    typeof priorPassword === "string" &&2202    priorPassword.length > 0 &&2203    current.passwordHistory?.[0]?.password !== priorPassword;2204  const nextHistory = shouldCapture2205    ? [2206        { password: priorPassword!, changedAt: current.passwordChangedAt ?? current.createdAt },2207        ...(current.passwordHistory ?? []),2208      ].slice(0, PASSWORD_HISTORY_MAX)2209    : current.passwordHistory;2210  entries[idx] = {2211    ...current,2212    ...sanitizedPatch,2213    modifiedAt: stamp,2214    ...(passwordChanged ? { passwordChangedAt: stamp, passwordHistory: nextHistory } : {}),2215  };2216221722182219  // Card entries must retain their card number2220  if (entries[idx].type === "card" && !entries[idx].cardNumber) {2221    entries[idx] = current;2222    throw new Error("card number is required for card entries");2223  }22242225  // Roll the in-memory entry back to its pre-edit state if the disk write2226  // fails (e.g. storage quota). Without this the module-level `entries` array2227  // would hold an edit that never landed on disk, diverging from the vault2228  // until the next unlock silently reverts it (SEV-3-A). Mirrors addEntry.2229  try {2230    await persistEntries();2231  } catch (err) {2232    entries[idx] = current;2233    throw err;2234  }22352236  // Breach check when password changes: clear immediately so old breach doesn't linger,2237  // then async-check the new password and only re-set if it's also found.2238  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {2239    entries[idx] = { ...entries[idx], breach: undefined };2240    await persistEntries();2241    const password = entries[idx].password;2242    if (password) {2243      checkBreach(password).then(async (count) => {2244        const i = entries.findIndex((e) => e.id === id);2245        if (i !== -1 && count > 0) {2246          entries[i] = { ...entries[i], breach: count, modifiedAt: monotonicEntryTimestamp(entries[i].modifiedAt) };2247          await persistEntries();2248        }2249      });2250    }2251  }22522253  fireSyncTrigger("vault_change");2254}22552256export async function deleteEntry(id: string): Promise<void> {2257  if (!derivedKey) throw new Error("vault_locked");22582259  const entry = entries.find((item) => item.id === id);2260  if (!entry || entry.deletedAt) {2261    throw new Error("entry_not_found");2262  }22632264  // Tombstone timestamp must be strictly greater than the entry's last2265  // modification so the deletion wins LWW against any in-flight edit, even2266  // under clock skew (SEV-2-E).2267  const previousModifiedAt = entry.modifiedAt;2268  const stamp = monotonicEntryTimestamp(entry.modifiedAt);2269  entry.deletedAt = stamp;2270  entry.modifiedAt = stamp;2271  // Roll the tombstone back if the disk write fails, so a quota error doesn't2272  // leave the entry deleted in memory but alive on disk (SEV-3-A).2273  try {2274    await persistEntries();2275  } catch (err) {2276    delete entry.deletedAt;2277    entry.modifiedAt = previousModifiedAt;2278    throw err;2279  }2280  fireSyncTrigger("vault_change");2281}22822283export function getDeletedEntries(): VaultEntry[] {2284  if (!derivedKey) return [];2285  return entries.filter((e) => !!e.deletedAt);2286}22872288/**2289 * Returns the vault's monotonic version — the `peach_last_modified_at` value2290 * stamped on every user-initiated write. Remote (sync) writes do NOT advance2291 * this version so a sync round never cascades into a second outbound sync.2292 */2293export async function getVaultVersion(): Promise<number> {2294  try {2295    const raw = await chrome.storage.local.get("peach_last_modified_at");2296    const v = (raw as Record<string, unknown>).peach_last_modified_at;2297    if (typeof v === "number") return v;2298    if (typeof v === "string" && v !== "") {2299      const n = Number.parseInt(v, 10);2300      return Number.isFinite(n) ? n : 0;2301    }2302    return 0;2303  } catch {2304    return 0;2305  }2306}23072308/**2309 * Apply a batch of remote entries + tombstones from a sync peer.2310 * Last-write-wins by `modifiedAt`; a tombstone supersedes a live entry when2311 * `deletedAt > modifiedAt` on the local side. Mutates `entries` directly and2312 * persists with reason `sync_received` — does NOT fire the sync dispatcher, so2313 * applying a sync round cannot cascade into another outbound sync.2314 *2315 * `applied` counts entries that actually changed local state (new add,2316 * overwrite-by-newer, or new tombstone). `received` is the raw payload size2317 * the caller can surface in the sync result UI.2318 */2319export async function applyMergedRemoteEntries(2320  remoteEntries: VaultEntry[],2321  remoteTombstones: Record<string, number>,2322  _context: { deviceName?: string } = {},2323): Promise<{ received: number; applied: number; conflicts: number }> {2324  // DESIGN: last-write-wins is applied PER-ENTRY, not per-field. If two devices2325  // concurrently edit different fields of the same entry, the one with the2326  // newer modifiedAt wins the whole entry and the other device's field edits2327  // are dropped silently. This is the deliberate trade-off for a simple,2328  // conflict-free merge — per-field CRDT merge is out of scope. Guidance for2329  // users: edit a given entry on one device at a time.2330  if (!derivedKey) throw new Error("vault_locked");23312332  const received = remoteEntries.length + Object.keys(remoteTombstones).length;2333  let applied = 0;2334  let conflicts = 0;23352336  const byId = new Map<string, VaultEntry>();2337  for (const e of entries) byId.set(e.id, e);23382339  const mergeNow = Date.now();23402341  for (const remote of remoteEntries) {2342    const sanitized = sanitizeRestoredEntry(remote);2343    if (!sanitized) continue;2344    // HV-1a: a far-future remote modifiedAt must not permanently pin a2345    // malicious value against honest local edits. Clamp BEFORE any2346    // comparison against local timestamps.2347    sanitized.modifiedAt = clampSyncTimestamp(sanitized.modifiedAt, mergeNow);2348    if (typeof sanitized.deletedAt === "number") {2349      sanitized.deletedAt = clampSyncTimestamp(sanitized.deletedAt, mergeNow);2350    }2351    const tombstoneTs = remoteTombstones[sanitized.id];2352    if (tombstoneTs !== undefined && clampSyncTimestamp(tombstoneTs, mergeNow) >= sanitized.modifiedAt) {2353      continue;2354    }2355    const local = byId.get(sanitized.id);2356    if (!local) {2357      entries.push(sanitized);2358      byId.set(sanitized.id, sanitized);2359      applied += 1;2360    } else if (sanitized.modifiedAt > local.modifiedAt) {2361      const idx = entries.findIndex((e) => e.id === local.id);2362      if (idx !== -1) {2363        entries[idx] = sanitized;2364        byId.set(sanitized.id, sanitized);2365        applied += 1;2366        if (!local.deletedAt) conflicts += 1;2367      }2368    }2369  }23702371  for (const [id, rawDeletedAt] of Object.entries(remoteTombstones)) {2372    if (typeof rawDeletedAt !== "number" || !Number.isFinite(rawDeletedAt)) continue;2373    // HV-1a/1c: clamp the tombstone timestamp before every comparison.2374    const deletedAt = clampSyncTimestamp(rawDeletedAt, mergeNow);2375    const local = byId.get(id);2376    if (!local) continue;2377    if (local.deletedAt && local.deletedAt >= deletedAt) continue;2378    if (deletedAt < local.modifiedAt) continue;2379    const idx = entries.findIndex((e) => e.id === id);2380    if (idx !== -1) {2381      entries[idx] = { ...local, deletedAt, modifiedAt: deletedAt };2382      applied += 1;2383    }2384  }23852386  if (applied > 0) {2387    await persistEntries("sync_received");2388    buildDomainIndexSync(entries);2389  }2390  return { received, applied, conflicts };2391}23922393export async function restoreEntry(id: string): Promise<void> {2394  if (!derivedKey) throw new Error("vault_locked");2395  const entry = entries.find((e) => e.id === id && e.deletedAt);2396  if (!entry) throw new Error("deleted entry not found");2397  delete entry.deletedAt;2398  entry.modifiedAt = monotonicEntryTimestamp(entry.modifiedAt);2399  await persistEntries();2400  buildDomainIndexSync(entries);2401}24022403export async function purgeEntry(id: string): Promise<void> {2404  if (!derivedKey) throw new Error("vault_locked");2405  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);2406  if (idx === -1) throw new Error("deleted entry not found");2407  entries.splice(idx, 1);2408  await persistEntries();2409}24102411/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */2412export async function purgeExpiredTrash(retentionMs: number): Promise<number> {2413  if (!derivedKey || retentionMs === 0) return 0;2414  const cutoff = Date.now() - retentionMs;2415  const before = entries.length;2416  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));2417  const purged = before - entries.length;2418  if (purged > 0) await persistEntries();2419  return purged;2420}24212422export function queryDomain(domain: string): VaultEntry[] {2423  return domainIndex.get(domain) ?? [];2424}24252426export function getEntryById(id: string): VaultEntry | undefined {2427  return entries.find((entry) => entry.id === id && !entry.deletedAt);2428}24292430async function persistSlotA(2431  reason: VaultSnapshot["reason"] = "local_save",2432): Promise<StoredVault> {2433  if (!derivedKey) throw new Error("vault_locked");24342435  const slotA = await loadSlot(SLOT_A);2436  if (!slotA) throw new Error("missing slot A");24372438  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures2439  // are swallowed by takeVaultSnapshot — history must never block a save.2440  await takeVaultSnapshot(reason);24412442  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);2443  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");24442445  // Preserve existing recovery escrow fields.2446  const nextSlotA: StoredVault = {2447    ...slotA,2448    nonce,2449    ciphertext,2450    tag,2451    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot2452  };2453  await writeSlot(SLOT_A, nextSlotA);24542455  // Slot A (the real vault) is now committed. Wrap every later write so a2456  // mid-persist failure (e.g. a storage error after slot A landed) restores the2457  // prior slot A before rethrowing. Without this, a "failed" save that already2458  // wrote slot A leaves disk ahead of the in-memory rollback in addEntry/2459  // updateEntry/deleteEntry — the entry stays silently on disk and resurfaces2460  // as a duplicate on the next unlock (observed under storage-quota pressure).2461  try {2462    // When duress is not configured, keep slot B size-matched with slot A by2463    // regenerating its noise. When duress IS configured, leave slot B alone —2464    // we don't have the decoy key to re-encrypt it. This is a known trade-off.2465    if (!duressConfigured) {2466      await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2467    }2468    // Only advance the vault version for locally-originated writes. Entries2469    // applied from a sync peer ("sync_received") must NOT bump the counter —2470    // otherwise applying a remote round makes us look newer than the peer and2471    // triggers a redundant outbound sync, defeating the no-op optimisation2472    // and risking a ping-pong between two devices (SEV-2-C).2473    if (reason !== "sync_received") {2474      await stampLocalVaultMutation();2475    }2476  } catch (err) {2477    // Best-effort restore — the prior slot A is smaller-or-equal, so this write2478    // fits even when the failure was a quota overflow.2479    await writeSlot(SLOT_A, slotA).catch(() => {});2480    throw err;2481  }24822483  return nextSlotA;2484}24852486async function persistSlotB(): Promise<void> {2487  if (!derivedKey) throw new Error("vault_locked");24882489  const slotB = await loadSlot(SLOT_B);2490  if (!slotB) throw new Error("missing slot B");24912492  // Decoy plaintext always reports duressConfigured=false — the decoy user2493  // sees a clean slate. persistSlotB only runs in decoy mode, which only2494  // exists once duress is configured, so pad to the fixed duress ceiling to2495  // keep slot B's length equal to slot A's (audit MEDIUM — deniability).2496  const plaintext = encodePayload(entries, false, DURESS_SLOT_SIZE);2497  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_b_v1");24982499  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });2500  // Symmetric with persistSlotA: if the version stamp fails after the decoy2501  // slot committed, restore the prior slot B so a failed decoy-mode save can't2502  // leave disk ahead of the in-memory rollback.2503  try {2504    await stampLocalVaultMutation();2505  } catch (err) {2506    await writeSlot(SLOT_B, slotB).catch(() => {});2507    throw err;2508  }2509}25102511async function persistEntries(2512  reason: VaultSnapshot["reason"] = "local_save",2513): Promise<void> {2514  if (!derivedKey) throw new Error("vault_locked");25152516  if (vaultMode === "decoy") {2517    await persistSlotB();2518  } else {2519    await persistSlotA(reason);2520  }2521  buildDomainIndexSync(entries);2522  await refreshLockedSyncState();2523}25242525function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {2526  if (!Array.isArray(values)) return undefined;2527  const seen = new Set<string>();2528  const cleaned: string[] = [];2529  for (const value of values) {2530    const trimmed = typeof value === "string" ? value.trim() : "";2531    if (!trimmed) continue;2532    const key = trimmed.toLowerCase();2533    if (seen.has(key)) continue;2534    seen.add(key);2535    cleaned.push(trimmed);2536  }2537  return cleaned.length > 0 ? cleaned : undefined;2538}25392540function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {2541  const title = entry.title.trim();2542  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new2543  // model is "any entry, optionally with a passkey sub-object." Imports and2544  // any in-flight callers from before v2.11.0 keep working without throwing.2545  let type: VaultEntry["type"] = entry.type ?? "login";2546  if (type === "passkey") type = "login";25472548  if (!title) throw new Error("title is required");25492550  if (entry.passkey) {2551    const p = entry.passkey;2552    if (2553      !p.rpId ||2554      !p.credentialId ||2555      !p.publicKeyCose ||2556      !p.userHandle ||2557      !p.userName ||2558      (p.algorithm !== -7 && p.algorithm !== -8)2559    ) {2560      throw new Error("invalid passkey credential");2561    }2562  }2563  if (type === "card") {2564    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {2565      throw new Error("cardholder name and card number are required");2566    }2567  }25682569  return {2570    type,2571    title,2572    username: type === "login" ? entry.username?.trim() ?? "" : undefined,2573    email: type === "login" ? entry.email?.trim() || undefined : undefined,2574    password: type === "login" ? entry.password : undefined,2575    url: type === "login" ? entry.url?.trim() ?? "" : undefined,2576    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,2577    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,2578    // PCH-01-019: preserve non-default TOTP params through batch-add/import so2579    // they are not silently stripped (they fall back to SHA-1/6/30 when absent).2580    totpAlgorithm: type === "login" ? entry.totpAlgorithm : undefined,2581    totpDigits: type === "login" ? entry.totpDigits : undefined,2582    totpPeriod: type === "login" ? entry.totpPeriod : undefined,2583    notes: entry.notes?.trim() || undefined,2584    breach: type === "login" ? entry.breach : undefined,2585    // PCH-01-016: the batch-add path was bypassing the MV-6 canary/tags2586    // whitelist applied on the RESTORE_ENTRIES path. Run both sanitizers here.2587    canary: type === "login" ? sanitizeCanary(entry.canary) : undefined,2588    tags: sanitizeTags(entry.tags),2589    passkey: entry.passkey,2590    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,2591    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,2592    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,2593    expYear: type === "card" ? entry.expYear?.trim() : undefined,2594    cvc: type === "card" ? entry.cvc?.trim() : undefined,2595    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,2596    network: type === "card" ? entry.network : undefined,2597    last4: type === "card" ? entry.last4?.trim() : undefined,2598  };2599}26002601function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {2602  const nextPatch: Partial<VaultEntry> = {};26032604  if ("type" in patch) {2605    nextPatch.type = patch.type;2606  }26072608  if ("title" in patch) {2609    const title = patch.title?.trim() ?? "";2610    if (!title) throw new Error("title is required");2611    nextPatch.title = title;2612  }26132614  if ("url" in patch) {2615    nextPatch.url = patch.url?.trim() ?? "";2616  }26172618  if ("additionalUrls" in patch) {2619    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];2620    const seen = new Set<string>();2621    const cleaned: string[] = [];2622    for (const value of incoming) {2623      const trimmed = typeof value === "string" ? value.trim() : "";2624      if (!trimmed) continue;2625      const key = trimmed.toLowerCase();2626      if (seen.has(key)) continue;2627      seen.add(key);2628      cleaned.push(trimmed);2629    }2630    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;2631  }26322633  if ("username" in patch) {2634    nextPatch.username = patch.username?.trim() ?? "";2635  }26362637  if ("email" in patch) {2638    nextPatch.email = patch.email?.trim() || undefined;2639  }26402641  if ("password" in patch) {2642    const password = patch.password ?? "";2643    // Allow clearing a password (e.g. on passkey-only entries)2644    nextPatch.password = password || undefined;2645  }26462647  if ("totp" in patch) {2648    nextPatch.totp = patch.totp?.trim() || undefined;2649  }26502651  if ("notes" in patch) {2652    nextPatch.notes = patch.notes?.trim() || undefined;2653  }26542655  if ("breach" in patch) {2656    nextPatch.breach = patch.breach;2657  }26582659  if ("canary" in patch) {2660    // PCH-02-001: route update patches through the same canary whitelist used2661    // by add/restore/sync. Invalid shapes drop the canary; `undefined` clears it.2662    nextPatch.canary = patch.canary ? sanitizeCanary(patch.canary) : undefined;2663  }26642665  if ("passkey" in patch) {2666    // Passkey sub-object replaces all the legacy flat passkey fields. A2667    // patch with `passkey: undefined` removes the credential from the entry2668    // (used by "remove passkey" without deleting the whole entry).2669    if (patch.passkey === undefined || patch.passkey === null) {2670      nextPatch.passkey = undefined;2671    } else {2672      const sanitized = sanitizePasskey(patch.passkey);2673      if (!sanitized) throw new Error("invalid passkey credential");2674      nextPatch.passkey = sanitized;2675    }2676  }26772678  if ("tags" in patch) {2679    // PCH-02-001: bound tags exactly like both import funnels.2680    nextPatch.tags = sanitizeTags(patch.tags);2681  }26822683  if ("cardholderName" in patch) {2684    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;2685  }2686  if ("cardNumber" in patch) {2687    const n = patch.cardNumber ?? "";2688    nextPatch.cardNumber = n || undefined;2689  }2690  if ("expMonth" in patch) {2691    nextPatch.expMonth = patch.expMonth?.trim() || undefined;2692  }2693  if ("expYear" in patch) {2694    nextPatch.expYear = patch.expYear?.trim() || undefined;2695  }2696  if ("cvc" in patch) {2697    nextPatch.cvc = patch.cvc?.trim() || undefined;2698  }2699  if ("cardNickname" in patch) {2700    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;2701  }2702  if ("network" in patch) {2703    nextPatch.network = patch.network;2704  }2705  if ("last4" in patch) {2706    nextPatch.last4 = patch.last4?.trim() || undefined;2707  }27082709  return nextPatch;2710}27112712function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {2713  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;2714  const modifiedAt =2715    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;27162717  const base: VaultEntry = {2718    id: entry.id,2719    type: "type" in entry ? entry.type : "login",2720    title: entry.title,2721    username: entry.username,2722    email: "email" in entry ? entry.email : undefined,2723    password: entry.password,2724    url: entry.url,2725    additionalUrls:2726      "additionalUrls" in entry && Array.isArray(entry.additionalUrls)2727        ? entry.additionalUrls2728        : undefined,2729    totp: entry.totp,2730    // PCH-01-019: carry TOTP params through the normalize/restore path too.2731    totpAlgorithm: entry.totpAlgorithm,2732    totpDigits: entry.totpDigits,2733    totpPeriod: entry.totpPeriod,2734    notes: entry.notes,2735    breach: entry.breach,2736    // Preserve the password-age clock across lock/unlock. Without this the2737    // field is dropped on every decode and the advanced-health "aged password"2738    // signal silently resets to createdAt on each unlock (SEV-2-A).2739    passwordChangedAt:2740      "passwordChangedAt" in entry ? entry.passwordChangedAt : undefined,2741    // Preserve past-password history across lock/unlock. normalizeEntry is a2742    // field whitelist, so an unlisted field is silently dropped on every decode2743    // (same class as the passwordChangedAt SEV-2-A above).2744    passwordHistory:2745      "passwordHistory" in entry && Array.isArray(entry.passwordHistory)2746        ? entry.passwordHistory2747        : undefined,2748    canary: "canary" in entry ? entry.canary : undefined,2749    passkey: "passkey" in entry ? entry.passkey : undefined,2750    createdAt,2751    modifiedAt,2752    deletedAt: entry.deletedAt,2753    tags: "tags" in entry ? entry.tags : undefined,2754    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,2755    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,2756    expMonth: "expMonth" in entry ? entry.expMonth : undefined,2757    expYear: "expYear" in entry ? entry.expYear : undefined,2758    cvc: "cvc" in entry ? entry.cvc : undefined,2759    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,2760    network: "network" in entry ? entry.network : undefined,2761    last4: "last4" in entry ? entry.last4 : undefined,2762  };27632764  return migrateLegacyPasskey(base, entry);2765}27662767/**2768 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`2769 * with flat top-level fields (rpId, credentialId, etc.). This converts them2770 * to the new shape — `type:"login"` with a `passkey` sub-object — so all2771 * downstream consumers can rely on the new model. The result is rewritten2772 * to disk on next vault save (hasLegacyShape detects the un-migrated case).2773 */2774function migrateLegacyPasskey(2775  base: VaultEntry,2776  raw: VaultEntry | LegacyVaultEntry2777): VaultEntry {2778  if (base.passkey) return base;2779  const flat = raw as Partial<VaultEntry>;2780  const isPasskeyType = base.type === "passkey";2781  const hasFlatPasskeyFields =2782    typeof flat.rpId === "string" &&2783    typeof flat.credentialId === "string" &&2784    typeof flat.publicKeyCose === "string" &&2785    typeof flat.userHandle === "string" &&2786    typeof flat.userName === "string";2787  if (!isPasskeyType && !hasFlatPasskeyFields) return base;2788  if (!hasFlatPasskeyFields) return base;27892790  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;2791  base.passkey = {2792    rpId: flat.rpId!,2793    rpName: flat.rpName ?? flat.rpId!,2794    credentialId: flat.credentialId!,2795    publicKeyCose: flat.publicKeyCose!,2796    privateKeyJwk: flat.privateKeyJwk,2797    userHandle: flat.userHandle!,2798    userName: flat.userName!,2799    userDisplayName: flat.userDisplayName ?? flat.userName!,2800    algorithm,2801    signCount: flat.signCount ?? 0,2802    aaguid: flat.aaguid ?? "",2803    backupEligible: flat.backupEligible === true,2804    backupState: flat.backupState === true,2805    lastUsedAt: flat.lastUsedAt,2806    browser: flat.browser,2807    transports: flat.transports,2808    createdAt: base.createdAt,2809  };2810  // Promote to "login" so the unified Entry view renders. Username gets2811  // populated from the passkey's userName so the entry is searchable in the2812  // existing autofill flow without a separate code path.2813  if (isPasskeyType) base.type = "login";2814  if (!base.username) base.username = flat.userName;2815  if (!base.url) base.url = `https://${flat.rpId}`;2816  return base;2817}281828192820// ---------------------------------------------------------------------------2821// Deduplication2822// ---------------------------------------------------------------------------28232824function entryDupeKey(e: VaultEntry): string {2825  if (e.type === "note") return `note|${e.title.toLowerCase()}`;2826  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins2827  // on different sites that happen to share a username are still distinct;2828  // two passkey credentials for the same RP+user are duplicates regardless2829  // of any login fields they may also carry.2830  if (e.passkey) {2831    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;2832  }2833  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;2834}28352836/** Returns the number of duplicate entries (those that would be removed). */2837export function countDuplicates(): number {2838  const best = new Map<string, VaultEntry>();2839  let dupeCount = 0;2840  for (const e of entries.filter((e) => !e.deletedAt)) {2841    const key = entryDupeKey(e);2842    const existing = best.get(key);2843    if (!existing) {2844      best.set(key, e);2845    } else {2846      dupeCount++;2847      // Keep the more recently modified one in the map2848      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2849        best.set(key, e);2850      }2851    }2852  }2853  return dupeCount;2854}28552856/**2857 * Soft-deletes duplicate entries, keeping the most recently modified copy of2858 * each group. Returns the number of entries moved to trash.2859 */2860export async function deduplicateEntries(): Promise<number> {2861  if (!derivedKey) throw new Error("vault_locked");2862  // Dedup reshapes slot B permanently under the decoy key, which is2863  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.2864  if (vaultMode === "decoy") {2865    throw new Error("deduplicate disabled in decoy mode");2866  }28672868  const best = new Map<string, VaultEntry>();2869  for (const e of entries.filter((en) => !en.deletedAt)) {2870    const key = entryDupeKey(e);2871    const existing = best.get(key);2872    if (!existing) {2873      best.set(key, e);2874    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2875      best.set(key, e);2876    }2877  }28782879  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));2880  let removed = 0;2881  entries = entries.map((e) => {2882    if (!e.deletedAt && !keepIds.has(e.id)) {2883      removed++;2884      return { ...e, deletedAt: monotonicEntryTimestamp(e.modifiedAt) };2885    }2886    return e;2887  });28882889  if (removed > 0) await persistEntries();2890  return removed;2891}28922893// ---------------------------------------------------------------------------2894// Vault history — snapshot BEFORE every vault write so the saved blob is2895// what is about to be lost, not what was just written. Capped at 5. The2896// stored blobs are AES-256-GCM ciphertext under the master password, so2897// history adds zero security surface.2898// ---------------------------------------------------------------------------28992900const HISTORY_KEY = "peach_vault_history";2901const HISTORY_MAX = 5;29022903// PCH-01-023: coarsen a live-entry count into a low-resolution bucket so the2904// plaintext snapshot field cannot reveal exact vault size or single-entry change2905// deltas. Buckets widen as the vault grows so absolute leakage stays roughly2906// proportional rather than exact.2907function bucketEntryCount(n: number): number {2908  if (n <= 0) return 0;2909  if (n <= 10) return 10;2910  if (n <= 25) return 25;2911  if (n <= 50) return 50;2912  if (n <= 100) return 100;2913  if (n <= 250) return 250;2914  if (n <= 500) return 500;2915  return Math.ceil(n / 500) * 500;2916}29172918/**2919 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE2920 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost2921 * state. Identical-to-most-recent blobs are skipped to avoid filling history2922 * with no-op writes.2923 */2924export async function takeVaultSnapshot(2925  reason: VaultSnapshot["reason"],2926): Promise<void> {2927  try {2928    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);2929    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];2930    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];29312932    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,2933    // pre-createVault). createVault skips snapshots intentionally — there is2934    // no prior state to preserve.2935    if (!slotA) return;29362937    const vault_a = JSON.stringify(slotA);2938    const vault_b = JSON.stringify(slotB ?? slotA);29392940    // Best-effort entry count: only meaningful when the in-memory state is2941    // unlocked AND in real mode. Decoy mode would leak the decoy entry2942    // count; locked mode has no entries to count. Both fall through to null.2943    // PCH-01-023: store a coarse bucket, not the exact count — chrome.storage2944    // .local sits next to the AES-GCM blobs in plaintext, so an exact count would2945    // leak vault size and per-write deltas (the metadata padding hides this).2946    let entryCount: number | null = null;2947    if (derivedKey && vaultMode === "real") {2948      try {2949        entryCount = bucketEntryCount(entries.filter((e) => !e.deletedAt).length);2950      } catch {2951        entryCount = null;2952      }2953    }29542955    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2956    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)2957      ? existingRaw2958      : { snapshots: [] };29592960    // Deduplication: if the most recent snapshot has byte-identical blobs,2961    // skip — saves the same vault N times in a row would otherwise burn the2962    // entire history window with no recovery value.2963    const latest = history.snapshots[0];2964    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {2965      return;2966    }29672968    const snapshot: VaultSnapshot = {2969      id: crypto.randomUUID(),2970      savedAt: Date.now(),2971      reason,2972      vault_a,2973      vault_b,2974      entryCount,2975    };29762977    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);2978    await chrome.storage.local.set({ [HISTORY_KEY]: history });2979  } catch (err) {2980    // Non-fatal — history must never block a save. A failed snapshot just2981    // means the user loses one rung of the safety ladder for this write.2982    // M4/QUA-006: log so the failure is visible; if it looks like a quota2983    // problem, notify the popup so the user can prune history themselves.2984    console.warn('[Peach] takeVaultSnapshot failed:', err);2985    if (String(err).includes('QUOTA') || String(err).includes('quota')) {2986      try {2987        await chrome.runtime.sendMessage({ type: 'VAULT_STORAGE_QUOTA_WARNING' });2988      } catch {2989        // popup may be closed; surfaced via console.warn above2990      }2991    }2992  }2993}29942995/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */2996export async function clearVaultHistory(): Promise<void> {2997  try {2998    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });2999  } catch {3000    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().3001  }3002}30033004/** Read all snapshots, newest first, for the Settings vault history view. */3005export async function getVaultHistory(): Promise<VaultSnapshot[]> {3006  const raw = await chrome.storage.local.get(HISTORY_KEY);3007  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];3008  if (!history || !Array.isArray(history.snapshots)) return [];3009  return history.snapshots;3010}30113012/**3013 * Replace the on-disk vault with the chosen snapshot's blobs and lock the3014 * vault. The current state is itself snapshotted with reason 'restore' first3015 * so the restore is reversible. After this returns, the user must unlock3016 * with the password that matches the restored blob.3017 */3018export async function restoreVaultSnapshot(3019  snapshotId: string,3020): Promise<{ ok: true } | { ok: false; error: string }> {3021  const history = await getVaultHistory();3022  const snapshot = history.find((s) => s.id === snapshotId);3023  if (!snapshot) return { ok: false, error: "snapshot_not_found" };30243025  // Snapshot the about-to-be-replaced current state so the restore is3026  // reversible. takeVaultSnapshot is no-op when the most recent entry is3027  // byte-identical, so a restore-of-a-restore stays clean.3028  await takeVaultSnapshot("restore");30293030  let restoredA: StoredVault;3031  let restoredB: StoredVault;3032  try {3033    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;3034    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;3035  } catch {3036    return { ok: false, error: "snapshot_corrupt" };3037  }30383039  await writeSlot(SLOT_A, restoredA);3040  await writeSlot(SLOT_B, restoredB);3041  await stampLocalVaultMutation();30423043  // Lock so the user must prove they can decrypt the restored blob. If they3044  // restored a pre-password-change snapshot they discover it immediately at3045  // the lock screen rather than silently holding an inaccessible vault.3046  lock();30473048  return { ok: true };3049}30503051/**3052 * Rotate the vault's sync identity by generating a FRESH ROOT KEY and3053 * re-wrapping everything under it (MV-8). Because every sync credential —3054 * vault_id, the Noise static keypair, the Ed25519 manifest signing key,3055 * and the per-entry envelope keys — derives from the root key, rotation3056 * makes a lost/stolen device's retained key material useless. All paired3057 * devices must re-pair afterwards.3058 *3059 * CONSEQUENCE THE UI MUST SURFACE: the 12 recovery words also derive from3060 * the root key, so rotation INVALIDATES the old recovery phrase. The flow3061 * sets peach_recovery_pending so the popup re-presents the new words for3062 * confirmation, same as the migrateToRecovery rotate path.3063 */3064export async function rotateSyncIdentity(): Promise<{ ok: boolean; error?: string }> {3065  if (!derivedKey || vaultMode !== "real") {3066    return { ok: false, error: "vault_locked" };3067  }3068  const slotA = await loadSlot(SLOT_A);3069  if (!slotA) return { ok: false, error: "missing_slot_a" };30703071  // 1. Fresh root key + recovery escrow (new words).3072  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =3073    await generateAndWrapRootKey();30743075  // 2. Re-wrap the new root key under the current password-derived key.3076  const newRootKey = unhex(rawRootKey);3077  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, newRootKey);30783079  // 3. Snapshot, then single atomic slot write — both escrow wrappings of3080  //    the new root key land together (same transactional rule as3081  //    changePassword). Entries are untouched: they are sealed under the3082  //    password-derived key, not the root key.3083  await takeVaultSnapshot("local_save");3084  const nextSlotA: StoredVault = {3085    ...slotA,3086    recovery_salt: recoverySalt,3087    recovery_wrapped_root_key: wrappedRootKey,3088    recovery_nonce: wrappedNonce,3089    recovery_tag: wrappedTag,3090    ...pwRoot,3091  };3092  await writeSlot(SLOT_A, nextSlotA);30933094  // 4. Swap in memory only after disk committed.3095  if (rootKey) zeroKey(rootKey);3096  rootKey = newRootKey;30973098  // 5. Old peers' identities derive from the dead root key — drop them all.3099  await clearPairedDevices();31003101  // 6. Force re-presentation of the NEW recovery words on next popup view.3102  await setRecoveryConfirmPending(); // same flag the create-vault flow uses31033104  await stampLocalVaultMutation();3105  return { ok: true };3106}31073108/**3109 * Change the master password. Re-encrypts slot A with a fresh salt + new3110 * derived key. Slot B (duress or noise) is left untouched when duress is3111 * configured — it uses an independent key derived from the duress password —3112 * and regenerated as noise when duress is not configured.3113 */3114export async function changePassword(3115  currentPassword: string,3116  newPassword: string3117): Promise<boolean> {3118  if (!derivedKey || vaultMode !== "real") return false;31193120  // Verify current password before allowing any change3121  const valid = await verifyMainPassword(currentPassword);3122  if (!valid) return false;31233124  const slotA = await loadSlot(SLOT_A);3125  if (!slotA) return false;3126  if (!rootKey) return false;31273128  // Derive fresh key from new password + new random salt3129  const newSalt = generateSalt();3130  const newKey = await deriveKey(newPassword, newSalt);31313132  // Re-encrypt the in-memory entries with the new key3133  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);3134  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");31353136  // Pre-wrap root key under the new derived key. Recovery fields wrap rootKey3137  // under recovery words and remain untouched — recovery phrase keeps working3138  // across password changes (H4/COR-002).3139  const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);31403141  const finalSlotA: StoredVault = {3142    ...slotA,3143    salt: hex(newSalt),3144    nonce,3145    ciphertext,3146    tag,3147    ...pwRoot,3148  };31493150  // Snapshot the OLD-key blob before overwriting it. If the user later3151  // restores from this snapshot they will need the OLD password — the3152  // post-restore lock surfaces that immediately.3153  await takeVaultSnapshot("local_save");31543155  // Single atomic write: ciphertext, salt, and pw_root_* fields all land in3156  // one storage transaction. An SW kill before this point leaves the old3157  // slot intact; after this point everything tied to the new password is3158  // already on disk together (M6/COR-004).3159  await writeSlot(SLOT_A, finalSlotA);31603161  // Swap the in-memory key synchronously before any further await. If we3162  // threw between the Slot A write and this swap, the next persistEntries()3163  // would overwrite Slot A with old-key ciphertext, locking the user out.3164  zeroKey(derivedKey);3165  derivedKey = newKey;31663167  // PCH-01-008: the MV3 session escrow still holds the OLD key. Without this,3168  // the next service-worker restore decrypts the new-key Slot A with the stale3169  // key, fails tryDecryptSlotResult, and silently drops the user to a lock3170  // screen. Re-escrow the new key so restore survives a password change.3171  void saveSessionKey(newKey, "real");31723173  // Regenerate noise for slot B when duress is not in use. Runs after the3174  // key swap so a failure here is a disposable noise regeneration, not a3175  // lockout.3176  if (!duressConfigured) {3177    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));3178  }3179  await clearPinUnlockStorage();3180  await stampLocalVaultMutation();31813182  return true;3183}