vault.ts

96.2 KB · raw

1/**2 * vault.ts — sole owner of vault state and chrome.storage vault keys.3 *4 * Every read/write of `peach_vault_a`, `peach_vault_b`, and `peach_settings`5 * goes through this module. No other file accesses those keys directly.6 * External callers reach vault operations via `messages.ts` — never by7 * importing from here cross-boundary (popup/content must not). Invariants8 * for the two-slot schema, unlock timing, and key/disk atomicity are in9 * Projects.md §3.2–§3.6.10 */1112import { deriveKey, generateSalt } from "../crypto/argon2";13import { extractDomain } from "../shared/domain";14import { clampSyncTimestamp } from "../shared/sync-limits";15import { recordSyncAlerts, type SyncAlert } from "./sync-alerts";16import {17  rootKeyToWords,18  wordsToRootKeyMaterial,19  deriveRecoveryKey,20} from "../shared/recovery-words";21import type {22  AccentColorName,23  LegacyVaultEntry,24  ManualSummonRecord,25  SavedIdentity,26  StoredVault,27  VaultEntry,28  VaultHistory,29  VaultSnapshot,30} from "../shared/types";31import { decrypt, encrypt, zeroKey } from "./aes";32import { clearAutoLock, touchAutoLock } from "./alarms";3334type EntryDraft = Omit<35  VaultEntry,36  "id" | "createdAt" | "modifiedAt" | "deletedAt"37>;3839// Slot-neutral storage keys (v1.4.24+). Both slots always exist; slot B40// either holds the decoy vault (when duress is configured) or random noise41// that looks identical at rest. No external indicator is stored — duress42// configuration is recorded only inside the encrypted payload of slot A.43const SLOT_A = "peach_vault_a";44const SLOT_B = "peach_vault_b";4546// Legacy keys, migrated away on first access.47const LEGACY_MAIN = "peach_vault";48const LEGACY_DECOY = "peach_decoy_vault";49const LEGACY_DURESS_FLAG = "peach_duress";5051// Plaintext is padded to the next multiple of this size before encryption.52// This coarsens ciphertext-length leakage into 1 KiB buckets.53const PAD_BLOCK = 1024;5455// How many past passwords to retain per entry. Generous enough to be useful as56// a recovery aid while bounding the secret material we hold (and the ciphertext57// growth) for an entry whose password is rotated frequently.58const PASSWORD_HISTORY_MAX = 50;5960// When duress is configured, BOTH slots are padded to this fixed ceiling so61// their ciphertext lengths are identical and cannot reveal that a hidden decoy62// vault exists (audit MEDIUM — plausible-deniability leak). 256 KiB comfortably63// holds a very large vault (thousands of entries incl. passkeys). The decoy64// slot can't be re-encrypted on every real-vault save (the real session has no65// decoy key), so a fixed ceiling — rather than size-matching on each write — is66// what keeps the two slots equal. A real vault whose plaintext exceeds this67// would grow past the ceiling and re-expose the bit; that is an extreme,68// documented edge well beyond normal use.69const DURESS_SLOT_SIZE = 262_144;7071let derivedKey: Uint8Array | null = null;72// 32-byte random root key, separate from the derivedKey used for vault encryption.73// The root key is wrapped (encrypted) under the recovery key derived from the74// 12-word phrase. When the user changes their password, only the wrapping changes;75// the root key and recovery words remain stable.76let rootKey: Uint8Array | null = null;77let entries: VaultEntry[] = [];78let domainIndex: Map<string, VaultEntry[]> = new Map();79let vaultMode: "real" | "decoy" = "real";80// Cached post-unlock view of whether the real vault records that duress is81// configured. Only meaningful after a real-mode unlock; for decoy-mode unlock82// this stays false because we never see slot A's plaintext.83let duressConfigured = false;8485let migrationRan = false;8687// Set to a corruption reason during the last unlock() attempt when both slots88// decrypted but failed schema validation. Consumed and cleared by89// consumeLastUnlockFailureReason() so messages.ts can surface vault_corrupted90// distinctly from wrong-password (M7/COR-007).91//92// NOTE: module-level state — if vault.ts is ever moved to a worker context,93// this must be replaced with a structured return value from unlock() or a94// postMessage channel. The signal is invisible across module-instance95// boundaries.96let lastUnlockFailureReason: string | null = null;9798export function consumeLastUnlockFailureReason(): string | null {99  const r = lastUnlockFailureReason;100  lastUnlockFailureReason = null;101  return r;102}103104// ---------------------------------------------------------------------------105// Settings (persisted inside the encrypted vault store as a sidecar key)106// ---------------------------------------------------------------------------107108export type TextSize = "small" | "medium" | "large";109110export interface VaultSettings {111  accentColor?: AccentColorName;112  theme?: "dark" | "light";113  autoLockMinutes?: number;114  clipboardClearSeconds?: number;115  lockOnClose?: boolean;116  breachMonitoring?: boolean;117  passkeyProviderEnabled?: boolean;118  trashRetentionDays?: number;119  manualSummonDomains?: Record<string, ManualSummonRecord>;120  declinedCardFingerprints?: string[];121  /**122   * Text size scale for body copy and entry titles. Stops at "large" by123   * design — XL clips dense screens (Vault, Health) inside the fixed124   * 330×480 popup surface.125   */126  textSize?: TextSize;127  /**128   * When true, all entrance/exit transitions and ambient animations are129   * collapsed to instant. Honoured alongside the user's OS-level130   * `prefers-reduced-motion`.131   */132  reduceMotion?: boolean;133  /**134   * Android-only: when true, the master key is escrowed in AndroidKeyStore135   * and the lock screen offers biometric unlock. Toggled from Settings →136   * Security → Biometrics. Has no effect on desktop builds.137   */138  biometricsEnabled?: boolean;139  /**140   * User overrides for keyboard shortcuts. Keys are ShortcutAction names141   * (see src/shared/shortcuts.ts). Missing actions fall through to142   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the143   * canonical lookup. Storing only the deltas keeps the vault payload144   * small and lets default updates land for users who never rebind.145   */146  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;147}148149let settings: VaultSettings = {};150151export function getSettings(): VaultSettings {152  return settings;153}154155export async function ensureSettingsLoaded(): Promise<VaultSettings> {156  await loadSettings();157  return settings;158}159160export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {161  await loadSettings();162  settings = { ...settings, ...patch };163  await persistSettings();164}165166async function persistSettings(): Promise<void> {167  try {168    await chrome.storage.local.set({ peach_settings: settings });169  } catch (err) {170    throw mapStorageError(err);171  }172}173174function mapStorageError(err: unknown): Error {175  const message = err instanceof Error ? err.message : String(err);176  if (/QUOTA_BYTES|quota/i.test(message)) {177    return new Error("STORAGE_QUOTA_EXCEEDED");178  }179  return err instanceof Error ? err : new Error(message);180}181182async function loadSettings(): Promise<void> {183  const result = await chrome.storage.local.get("peach_settings");184  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};185}186187// Fire-and-forget sync trigger after a committed vault write. Lazy188// dynamic import avoids a circular dependency between vault.ts and189// sync-trigger.ts (the trigger imports paired-devices, which only reads190// storage, but the lazy load also gives us a clean opt-out path in191// tests where sync-trigger has not been initialised).192function fireSyncTrigger(reason: "vault_change"): void {193  void import("./sync-trigger")194    .then((mod) => mod.triggerSyncIfReachable({ reason }))195    .catch(() => {196      /* sync trigger module not available — non-fatal */197    });198}199200// ---------------------------------------------------------------------------201// HIBP breach check (k-anonymity model)202// ---------------------------------------------------------------------------203204export async function checkBreach(password: string): Promise<number> {205  // HIBP k-anonymity API is keyed on SHA-1 prefixes — this is dictated by206  // the public API, not a security choice. Do not migrate to SHA-256 here.207  const encoder = new TextEncoder();208  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));209  const hashHex = Array.from(new Uint8Array(hashBuffer))210    .map((b) => b.toString(16).padStart(2, "0"))211    .join("")212    .toUpperCase();213214  const prefix = hashHex.slice(0, 5);215  const suffix = hashHex.slice(5);216217  try {218    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);219    if (!res.ok) return 0;220    const text = await res.text();221    const match = text.split("\r\n").find((l) => l.startsWith(suffix));222    return match ? parseInt(match.split(":")[1], 10) : 0;223  } catch {224    return 0; // Network unavailable — don't block225  }226}227228export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {229  const fingerprint = await cardFingerprint(last4, expMonth, expYear);230  await loadSettings();231  settings = {232    ...settings,233    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],234  };235  await persistSettings();236}237238export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {239  await loadSettings();240  const fingerprint = await cardFingerprint(last4, expMonth, expYear);241  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);242}243244async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {245  const encoder = new TextEncoder();246  const buf = await crypto.subtle.digest("SHA-256", encoder.encode(`${last4}|${expMonth}|${expYear}`));247  return Array.from(new Uint8Array(buf).slice(0, 8))248    .map((b) => b.toString(16).padStart(2, "0"))249    .join("");250}251252function hex(buf: Uint8Array): string {253  return Array.from(buf)254    .map((b) => b.toString(16).padStart(2, "0"))255    .join("");256}257258function unhex(s: string): Uint8Array {259  const out = new Uint8Array(s.length / 2);260  for (let i = 0; i < s.length; i += 2) {261    out[i / 2] = parseInt(s.slice(i, i + 2), 16);262  }263  return out;264}265266// ---------------------------------------------------------------------------267// Root key generation and recovery word wrapping268// ---------------------------------------------------------------------------269270/**271 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key272 * derived from the recovery word material via HKDF-SHA256. The wrapped blob273 * fields are stored in slot A alongside the password-wrapped ciphertext. The274 * root key itself is kept in memory for the session so getRecoveryWords() can275 * encode it without re-deriving.276 */277async function generateAndWrapRootKey(): Promise<{278  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey279  recoverySalt: string;280  wrappedRootKey: string;281  wrappedNonce: string;282  wrappedTag: string;283}> {284  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));285286  // Derive the recovery key from the BIP39 words encoding of the root key.287  const words = await rootKeyToWords(rootKeyBytes);288  const wordMaterial = await wordsToRootKeyMaterial(words);289  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));290  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);291292  // Wrap the root key with the recovery key using AES-256-GCM directly.293  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.294  const nonce = crypto.getRandomValues(new Uint8Array(12));295  const encoded = new TextEncoder().encode(hex(rootKeyBytes));296  const raw = await crypto.subtle.encrypt(297    {298      name: "AES-GCM",299      iv: nonce,300      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,301      tagLength: 128,302    },303    recoveryKey,304    encoded305  );306  // WebCrypto returns ciphertext || auth tag concatenated; split them.307  const rawU8 = new Uint8Array(raw);308  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));309  const tag = hex(rawU8.slice(rawU8.length - 16));310311  return {312    rawRootKey: hex(rootKeyBytes),313    recoverySalt: hex(recoverySalt),314    wrappedRootKey: ciphertext,315    wrappedNonce: hex(nonce),316    wrappedTag: tag,317  };318}319320/**321 * Unwrap the root key using recovery words. Called during recovery flow when322 * the user provides their 12-word phrase and a new password.323 * Exported for use by the RECOVER_WITH_WORDS message handler.324 */325export async function unwrapRootKeyForRecovery(326  wrappedRootKey: string,327  wrappedNonce: string,328  wrappedTag: string,329  recoverySaltHex: string,330  words: string[]331): Promise<void> {332  const wordMaterial = await wordsToRootKeyMaterial(words);333  const recoverySalt = unhex(recoverySaltHex);334  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);335336  // Decrypt and cache the root key. The root key bytes were hex-encoded before337  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt338  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.339  // Each component is stored separately in the vault slot; decode them individually340  // so the nonce is always exactly the 12 bytes from wrappedNonce.341  const nonceRaw = unhex(wrappedNonce);342  const ciphertextRaw = unhex(wrappedRootKey);343  const tagRaw = unhex(wrappedTag);344  // Combine ciphertext and tag for AES-GCM (expects ciphertext||tag).345  // Use explicit ArrayBuffer allocations to satisfy TS BufferSource constraints.346  const nonceU8 = nonceRaw as unknown as Uint8Array<ArrayBuffer>;347  const combined = new Uint8Array(ciphertextRaw.length + tagRaw.length);348  combined.set(ciphertextRaw, 0);349  combined.set(tagRaw, ciphertextRaw.length);350  const raw = await crypto.subtle.decrypt(351    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },352    recoveryKey,353    combined354  );355  const decryptedHex = new TextDecoder().decode(raw);356  rootKey = unhex(decryptedHex);357}358359/**360 * Re-wrap the cached root key under a new derived key (from the user's new361 * password). Called after password change and after recovery re-key so the362 * same recovery words continue to work with the updated password — and so363 * subsequent unlocks can rehydrate rootKey via deriveRootKeyFromDerivedKey.364 *365 * recovery_* fields are intentionally not touched: those wrap rootKey under366 * the recovery words, which do not change when the password changes.367 */368export async function rewrapRootKeyForPassword(369  newDerivedKey: Uint8Array,370  _newSalt: Uint8Array371): Promise<void> {372  if (!rootKey) throw new Error("root key not set");373  const slotA = await loadSlot(SLOT_A);374  if (!slotA) throw new Error("missing slot A");375  const pwRoot = await wrapRootKeyForDerivedKey(newDerivedKey, rootKey);376  await writeSlot(SLOT_A, { ...slotA, ...pwRoot });377}378379/**380 * Change the vault encryption to a new derived key (already derived from381 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.382 * Caller is responsible for key swap and snapshot.383 */384async function reencryptVaultForNewKey(385  newKey: Uint8Array,386  slotA: StoredVault387): Promise<void> {388  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);389  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");390  await writeSlot(SLOT_A, {391    ...slotA,392    nonce,393    ciphertext,394    tag,395  });396}397398/**399 * Full password change with an already-derived key (used after recovery).400 * Unlike changePassword(), this skips re-verification of current password401 * since recovery flow has already validated the user's identity via words.402 */403export async function changePasswordWithNewKey(404  newSalt: Uint8Array,405  newKey: Uint8Array406): Promise<void> {407  const slotA = await loadSlot(SLOT_A);408  if (!slotA) throw new Error("missing slot A");409  // reencryptVaultForNewKey must receive the updated salt so that subsequent410  // UNLOCK calls with the new password can re-derive the correct key.411  await reencryptVaultForNewKey(newKey, { ...slotA, salt: hex(newSalt) });412  if (derivedKey) zeroKey(derivedKey);413  derivedKey = newKey;414  // Refresh pw_root_* under the new key so deriveRootKeyFromDerivedKey works415  // on the next unlock (H2/COR-006). If rootKey is missing — recovery flow416  // should have populated it — fall back silently rather than blowing up the417  // recovery path; migrateToRecovery will rehydrate on the next unlock.418  if (rootKey) {419    const slotAfter = await loadSlot(SLOT_A);420    if (slotAfter) {421      const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);422      await writeSlot(SLOT_A, { ...slotAfter, ...pwRoot });423    }424  }425  if (!duressConfigured) {426    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));427  }428  await stampLocalVaultMutation();429}430431// ---------------------------------------------------------------------------432// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive433// a new key after password reset without re-importing the crypto module.434// ---------------------------------------------------------------------------435436export { deriveKey, generateSalt };437438export async function loadSlotA(): Promise<StoredVault | null> {439  return loadSlot(SLOT_A);440}441442/**443 * Recover the session root key by AES-GCM-decrypting the pw_root_* fields444 * (a second wrapping of the root key under the password-derived key). This445 * is what makes GET_RECOVERY_WORDS work across lock/unlock cycles without446 * the user re-entering their recovery phrase: the same root key bytes —447 * and therefore the same 12 words — survive every unlock.448 *449 * Returns null when the pw_root fields are absent (pre-v2.12 vault that450 * hasn't been migrated yet) or when decryption fails (wrong derived key,451 * tampered ciphertext). On null the caller should fall through to452 * migrateToRecovery() to populate the fields.453 */454async function deriveRootKeyFromDerivedKey(455  derivedKeyBytes: Uint8Array,456  slotA: StoredVault,457): Promise<Uint8Array | null> {458  if (!slotA.pw_root_ciphertext || !slotA.pw_root_nonce || !slotA.pw_root_tag) {459    return null;460  }461  try {462    const rootHex = await decrypt(463      derivedKeyBytes,464      slotA.pw_root_nonce,465      slotA.pw_root_ciphertext,466      slotA.pw_root_tag,467      "pw_root_v1",468    );469    return unhex(rootHex);470  } catch {471    return null;472  }473}474475// AES-GCM-wrap rootKey under derivedKey for the pw_root_* fields. Returns the476// hex-encoded ciphertext/nonce/tag triple ready to embed in StoredVault.477async function wrapRootKeyForDerivedKey(478  derivedKeyBytes: Uint8Array,479  rootKeyBytes: Uint8Array,480): Promise<{ pw_root_ciphertext: string; pw_root_nonce: string; pw_root_tag: string }> {481  const { nonce, ciphertext, tag } = await encrypt(derivedKeyBytes, hex(rootKeyBytes), "pw_root_v1");482  return {483    pw_root_ciphertext: ciphertext,484    pw_root_nonce: nonce,485    pw_root_tag: tag,486  };487}488489/**490 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),491 * generate the root key, wrap it under the recovery words, and persist the492 * recovery escrow into slot A. The caller must show the recovery words to the493 * user before calling this — once called, those words are the vault's recovery494 * path. This runs once, on the first unlock of an upgraded vault.495 */496export async function migrateToRecovery(): Promise<void> {497  if (!derivedKey || vaultMode !== "real") {498    throw new Error("vault must be unlocked in real mode");499  }500  const slotA = await loadSlot(SLOT_A);501  if (!slotA) throw new Error("missing slot A");502  const hasRecoveryEscrow =503    !!slotA.recovery_salt && !!slotA.recovery_wrapped_root_key;504  const hasPwRoot = !!slotA.pw_root_ciphertext && !!slotA.pw_root_nonce;505  if (hasRecoveryEscrow && hasPwRoot) return; // already done506507  // If recovery escrow already exists but pw_root doesn't, we cannot recover508  // the original rootKey without the user's words. The least-surprising path509  // is to rotate to a fresh root key — the user's old recovery words become510  // invalid but we surface peach_recovery_pending so the popup re-presents511  // the words. The unlock-path callers all set that flag.512  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =513    await generateAndWrapRootKey();514  rootKey = unhex(rawRootKey);515516  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);517  const nextSlotA: StoredVault = {518    ...slotA,519    recovery_salt: recoverySalt,520    recovery_wrapped_root_key: wrappedRootKey,521    recovery_nonce: wrappedNonce,522    recovery_tag: wrappedTag,523    ...pwRoot,524  };525  await writeSlot(SLOT_A, nextSlotA);526  await stampLocalVaultMutation();527}528529/**530 * Returns the 12 recovery words for the current session's root key.531 * Must be called while the vault is unlocked. Throws if recovery has not532 * been set up (pre-v2.12 vault that hasn't migrated yet).533 */534export async function getRecoveryWords(): Promise<string[]> {535  if (!rootKey) {536    throw new Error("recovery not set up — vault may need migration");537  }538  return rootKeyToWords(rootKey);539}540541async function loadSlot(key: string): Promise<StoredVault | null> {542  const result = await chrome.storage.local.get(key);543  return (result as Record<string, StoredVault | undefined>)[key] ?? null;544}545546async function writeSlot(key: string, vault: StoredVault): Promise<void> {547  try {548    await chrome.storage.local.set({ [key]: vault });549  } catch (err) {550    throw mapStorageError(err);551  }552}553554// Monotonic clock for per-entry modifiedAt / deletedAt timestamps. Guards the555// sync layer's last-write-wins merge against wall-clock regressions (NTP step,556// manual clock change, DST glitch): a backwards Date.now() must never produce557// an entry timestamp that's <= the entry's existing modifiedAt, or a genuine558// local edit would lose to a stale remote copy. The returned value is always559// strictly greater than both the entry's current modifiedAt and the previous560// call's result (SEV-2-E).561let lastEntryTimestamp = 0;562function monotonicEntryTimestamp(currentModifiedAt?: number): number {563  const base = Math.max(564    Date.now(),565    (currentModifiedAt ?? 0) + 1,566    lastEntryTimestamp + 1,567  );568  lastEntryTimestamp = base;569  return base;570}571572async function stampLocalVaultMutation(): Promise<void> {573  const raw = await chrome.storage.local.get("peach_last_modified_at");574  const previous = Number(575    (raw as Record<string, unknown>).peach_last_modified_at,576  );577  const next = Math.max(578    Date.now(),579    Number.isFinite(previous) ? previous + 1 : 0,580  );581  await chrome.storage.local.set({582    peach_last_modified_at: String(next),583  });584}585586/**587 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault588 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +589 * peach_vault_b). Runs at most once per session; safe to call repeatedly.590 */591async function ensureMigrated(): Promise<void> {592  if (migrationRan) return;593  migrationRan = true;594595  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];596  const raw = await chrome.storage.local.get(keys);597  const slotAExists = raw[SLOT_A] !== undefined;598  const slotBExists = raw[SLOT_B] !== undefined;599  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;600  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;601602  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.603  if (!slotAExists && !legacyMain) return;604605  // Already migrated.606  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;607608  let didMigrate = false;609  if (legacyMain && !slotAExists) {610    await writeSlot(SLOT_A, legacyMain);611    didMigrate = true;612  }613614  // If slot B isn't set, populate it: use legacy decoy when present, else noise.615  if (!slotBExists) {616    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;617    if (legacyDecoy) {618      await writeSlot(SLOT_B, legacyDecoy);619      didMigrate = true;620    } else if (target) {621      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));622      didMigrate = true;623    }624  }625626  const toRemove: string[] = [];627  if (legacyMain) toRemove.push(LEGACY_MAIN);628  if (legacyDecoy) toRemove.push(LEGACY_DECOY);629  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);630  if (toRemove.length > 0) {631    await chrome.storage.local.remove(toRemove);632    didMigrate = true;633  }634  if (didMigrate) {635    await stampLocalVaultMutation();636  }637}638639// ---------------------------------------------------------------------------640// Padding + noise helpers641// ---------------------------------------------------------------------------642643/**644 * Pads the plaintext string with trailing spaces so that the encoded byte645 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing646 * whitespace after a complete document, so the padding is transparent on647 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.648 */649function padPlaintext(json: string, minBytes = 0): string {650  const encoded = new TextEncoder().encode(json);651  const bucket = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);652  // minBytes lets the duress path force a fixed ceiling so both slots match.653  const target = Math.max(bucket, minBytes);654  const padLen = target - encoded.length;655  return json + " ".repeat(padLen);656}657658/**659 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The660 * ciphertext length matches the given byte count. Because GCM ciphertext is661 * pseudo-random, this blob is indistinguishable at rest from a real662 * encrypted vault without the key.663 *664 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto665 * spec hard limit). Chunk the fill so vaults larger than that — common once666 * the user has dozens of entries or any passkey — don't throw.667 */668const CSPRNG_CHUNK = 65_536;669670function fillRandom(buf: Uint8Array): Uint8Array {671  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {672    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);673    const chunk = new Uint8Array(end - offset);674    crypto.getRandomValues(chunk);675    buf.set(chunk, offset);676  }677  return buf;678}679680function generateNoiseBlob(ciphertextBytes: number): StoredVault {681  const salt = crypto.getRandomValues(new Uint8Array(16));682  const nonce = crypto.getRandomValues(new Uint8Array(12));683  const cipher = fillRandom(new Uint8Array(ciphertextBytes));684  const tag = crypto.getRandomValues(new Uint8Array(16));685  return {686    version: 1,687    salt: hex(salt),688    nonce: hex(nonce),689    ciphertext: hex(cipher),690    tag: hex(tag),691  };692}693694// ---------------------------------------------------------------------------695// Encrypted plaintext envelope696// ---------------------------------------------------------------------------697698type VaultPayload = {699  entries: VaultEntry[];700  duressConfigured?: boolean;701};702703type DecryptedPayload = {704  entries: VaultEntry[];705  duressConfigured: boolean;706  /**707   * True when at least one entry in the on-disk payload had a legacy shape708   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)709   * and got rewritten by normalizeEntry. The unlock path uses this to710   * persist the new shape immediately — without it, the migration would711   * re-run on every unlock and old shape never disappears from disk.712   */713  migratedFromLegacy: boolean;714};715716function encodePayload(entries: VaultEntry[], duress: boolean, minBytes = 0): string {717  const envelope: VaultPayload = { entries, duressConfigured: duress };718  return padPlaintext(JSON.stringify(envelope), minBytes);719}720721function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {722  if ("created" in raw || "updated" in raw) return true;723  const flat = raw as Partial<VaultEntry>;724  if (flat.type === "passkey") return true;725  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.726  if (flat.rpId !== undefined && flat.passkey === undefined) return true;727  return false;728}729730function decodePayload(plain: string): DecryptedPayload {731  const parsed = JSON.parse(plain) as732    | Array<VaultEntry | LegacyVaultEntry>733    | VaultPayload;734735  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];736  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);737738  const normalized = rawEntries.map(normalizeEntry);739  const { merged } = mergePasskeyOnlyIntoLogins(normalized);740741  if (Array.isArray(parsed)) {742    return {743      entries: normalized,744      duressConfigured: false,745      migratedFromLegacy: true, // pre-A-2 array shape always needs save746    };747  }748749  return {750    entries: normalized,751    duressConfigured: parsed.duressConfigured === true,752    migratedFromLegacy: flatMigrationNeeded || merged,753  };754}755756/**757 * One-time consolidation pass run alongside the legacy passkey migration.758 *759 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`760 * entry per registration — so users who tested registration multiple times761 * accumulated several passkey-bearing entries for the same RP, and the762 * migration to the unified shape carried that duplication forward. This763 * pass collapses the duplicates:764 *765 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.766 *   2. Within each group, the most recently-active credential wins (prefer767 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).768 *   3. If a NON-passkey login entry exists for that RP (matched by url or769 *      additionalUrls hostname), attach the winning passkey to it and770 *      soft-delete every passkey-bearing entry in the group.771 *   4. Otherwise, soft-delete every loser in the group, keeping the winner772 *      as a passkey-only entry.773 *774 * Soft delete (deletedAt) rather than splice — the trash flow keeps the775 * record around for retention and the user can restore if the heuristic776 * picked wrong. The `merged` flag goes back to decodePayload so the777 * unlock path persists the consolidated shape immediately.778 */779function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {780  let merged = false;781  const now = Date.now();782783  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);784  const byRpId = new Map<string, VaultEntry[]>();785  for (const e of candidates) {786    const rpId = e.passkey!.rpId;787    const list = byRpId.get(rpId);788    if (list) list.push(e);789    else byRpId.set(rpId, [e]);790  }791792  for (const [rpId, group] of byRpId) {793    if (group.length === 0) continue;794    group.sort((a, b) => {795      const aT =796        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;797      const bT =798        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;799      return bT - aT;800    });801    const winner = group[0]!;802803    // Look for a NON-passkey login entry matching this RP. Use the URL804    // hostname rather than a substring search so a "login.foo.com" entry805    // doesn't accidentally absorb a passkey for "foo.com".806    const loginTarget = entries.find((e) => {807      if (e.deletedAt || e.passkey || e.type !== "login") return false;808      if (e.url && extractDomain(e.url) === rpId) return true;809      for (const extra of e.additionalUrls ?? []) {810        if (extractDomain(extra) === rpId) return true;811      }812      return false;813    });814815    if (loginTarget) {816      loginTarget.passkey = winner.passkey;817      loginTarget.modifiedAt = now;818      for (const dup of group) {819        if (!dup.deletedAt) {820          dup.deletedAt = now;821          dup.modifiedAt = now;822        }823      }824      merged = true;825    } else if (group.length > 1) {826      for (let i = 1; i < group.length; i += 1) {827        const dup = group[i]!;828        if (!dup.deletedAt) {829          dup.deletedAt = now;830          dup.modifiedAt = now;831        }832      }833      merged = true;834    }835  }836837  return { merged };838}839840// Validates that a `StoredVault` record on disk has the field shape we expect841// before treating an AES-GCM decrypt success as a clean unlock. AES-GCM only842// authenticates the static VERSION_BYTE AAD, not the `version` field on the843// outer record — a corrupted `version: 42` slot still decrypts cleanly. This844// check surfaces that as `corrupted` (M7/COR-007) so the popup can offer a845// history-restore path instead of falsely reporting "wrong password".846function hasValidVaultSchema(v: unknown): v is StoredVault {847  if (typeof v !== "object" || v === null) return false;848  const r = v as Record<string, unknown>;849  return (850    r.version === 1 &&851    typeof r.salt === "string" &&852    typeof r.nonce === "string" &&853    typeof r.ciphertext === "string" &&854    typeof r.tag === "string"855  );856}857858export type TryDecryptResult =859  | { status: "ok"; payload: DecryptedPayload }860  | { status: "wrong_key" }861  | { status: "corrupted"; reason: string };862863// Exported solely for `unlockFromCodex` below, which needs to verify a864// supplied passphrase against a slotA blob *without* committing anything to865// storage. Keep this internal otherwise — callers crossing the messages.ts866// boundary should always go through `unlock` / `verifyMainPassword` so the867// constant-time invariants in Projects.md §3.4 are not bypassed.868export async function tryDecryptSlot(869  vault: StoredVault,870  key: Uint8Array,871  aadTag?: string,872): Promise<DecryptedPayload | null> {873  const res = await tryDecryptSlotResult(vault, key, aadTag);874  return res.status === "ok" ? res.payload : null;875}876877export async function tryDecryptSlotResult(878  vault: StoredVault,879  key: Uint8Array,880  aadTag?: string,881): Promise<TryDecryptResult> {882  if (!hasValidVaultSchema(vault)) {883    return { status: "corrupted", reason: "invalid_slot_schema" };884  }885  let plain: string;886  try {887    plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag, aadTag);888  } catch {889    return { status: "wrong_key" };890  }891  try {892    const parsed = JSON.parse(plain) as unknown;893    if (typeof parsed !== "object" || parsed === null) {894      return { status: "corrupted", reason: "invalid_payload_shape" };895    }896    if (!Array.isArray(parsed) && !Array.isArray((parsed as VaultPayload).entries)) {897      return { status: "corrupted", reason: "invalid_payload_shape" };898    }899    return { status: "ok", payload: decodePayload(plain) };900  } catch {901    return { status: "corrupted", reason: "invalid_payload_json" };902  }903}904905// ---------------------------------------------------------------------------906// Core operations907// ---------------------------------------------------------------------------908909function buildDomainIndexSync(all: VaultEntry[]): void {910  domainIndex = new Map();911912  for (const entry of all) {913    if (entry.deletedAt) continue;914    if (entry.type === "note" || entry.type === "card") continue;915916    // An entry can match multiple sites via `additionalUrls`. Index each917    // hostname so `queryDomain` returns the entry regardless of which alias918    // the user is currently visiting. Dedupe per-entry so a single entry919    // never appears twice in one bucket.920    const domains = new Set<string>();921    const primary = extractDomain(entry.url ?? "");922    if (primary) domains.add(primary);923    for (const extra of entry.additionalUrls ?? []) {924      const d = extractDomain(extra);925      if (d) domains.add(d);926    }927    if (domains.size === 0) continue;928929    for (const domain of domains) {930      const bucket = domainIndex.get(domain) ?? [];931      bucket.push(entry);932      domainIndex.set(domain, bucket);933    }934  }935}936937export async function createVault(password: string): Promise<void> {938  await ensureMigrated();939940  const salt = generateSalt();941  derivedKey = await deriveKey(password, salt);942  entries = [];943  vaultMode = "real";944  duressConfigured = false;945  buildDomainIndexSync(entries);946947  // Generate and wrap the root key for recovery word escrow.948  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =949    await generateAndWrapRootKey();950  rootKey = unhex(rawRootKey);951952  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);953  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");954955  // Second wrapping of rootKey under derivedKey so future unlocks can rehydrate956  // it without recovery words (H2/COR-006).957  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);958959  const slotA: StoredVault = {960    version: 1,961    salt: hex(salt),962    nonce,963    ciphertext,964    tag,965    recovery_salt: recoverySalt,966    recovery_wrapped_root_key: wrappedRootKey,967    recovery_nonce: wrappedNonce,968    recovery_tag: wrappedTag,969    ...pwRoot,970  };971  await writeSlot(SLOT_A, slotA);972973  // Matching-size noise blob for slot B, so a fresh install shows two974  // similarly-shaped records regardless of whether duress is ever set up.975  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));976  await stampLocalVaultMutation();977978  touchAutoLock();979}980981export async function hasVault(): Promise<boolean> {982  await ensureMigrated();983  return (await loadSlot(SLOT_A)) !== null;984}985986/**987 * Returns whether the real vault has recorded that a duress passphrase is988 * configured. Reliable only after a real-mode unlock — before unlock or in989 * decoy mode, returns false because we cannot read slot A's plaintext.990 */991export async function isDuressEnabled(): Promise<boolean> {992  return vaultMode === "real" && duressConfigured;993}994995export async function setupDuress(duressPassword: string): Promise<void> {996  if (!derivedKey || vaultMode !== "real") {997    throw new Error("vault must be unlocked in real mode");998  }999  await ensureMigrated();10001001  // Encrypt the decoy payload (empty entries) under the duress key. Pad to1002  // at least the current slot A ciphertext size so the two slots look the1003  // same at rest at setup time.1004  const slotA = await loadSlot(SLOT_A);1005  if (!slotA) throw new Error("missing slot A");10061007  const decoySalt = generateSalt();1008  const decoyKey = await deriveKey(duressPassword, decoySalt);1009  try {1010    // Pad the decoy to the fixed duress ceiling. Slot A is re-encrypted to the1011    // same ceiling by the persistSlotA() call below (duressConfigured is now1012    // true), so both slots are exactly DURESS_SLOT_SIZE+tag at rest and their1013    // lengths cannot reveal that duress is configured (audit MEDIUM).1014    const plaintext = encodePayload([], false, DURESS_SLOT_SIZE);1015    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext, "slot_b_v1");1016    await writeSlot(SLOT_B, {1017      version: 1,1018      salt: hex(decoySalt),1019      nonce,1020      ciphertext,1021      tag,1022    });1023  } finally {1024    zeroKey(decoyKey);1025  }10261027  // Re-encrypt slot A with the duressConfigured flag set to true. No external1028  // flag is written — the bit lives inside the encrypted envelope.1029  duressConfigured = true;1030  await persistSlotA();1031}10321033export async function disableDuress(): Promise<void> {1034  if (!derivedKey || vaultMode !== "real") {1035    throw new Error("vault must be unlocked in real mode");1036  }1037  await ensureMigrated();10381039  // Overwrite slot B with fresh noise that matches slot A's size. The slot1040  // remains occupied so its mere existence does not signal that duress was1041  // ever configured.1042  const slotA = await loadSlot(SLOT_A);1043  if (slotA) {1044    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));1045  }10461047  duressConfigured = false;1048  await persistSlotA();1049}10501051// L14/COR-015: this function intentionally checks slot A (real vault) only.1052// A duress-mode user attempting changePassword is rejected earlier by the1053// vaultMode === 'decoy' check at changePassword. The real-vs-decoy1054// distinction here is by design — duress users cannot rotate the real1055// password by guessing it, and the asymmetry in error timing is acceptable1056// because duress mode is opt-in and the user knows their own decoy key.1057export async function verifyMainPassword(password: string): Promise<boolean> {1058  await ensureMigrated();1059  const slotA = await loadSlot(SLOT_A);1060  if (!slotA) return false;1061  const key = await deriveKey(password, unhex(slotA.salt));1062  try {1063    return (await tryDecryptSlot(slotA, key, "slot_a_v1")) !== null;1064  } finally {1065    zeroKey(key);1066  }1067}10681069export async function unlock(password: string): Promise<boolean> {1070  await ensureMigrated();10711072  // Load both slots in parallel. Both always exist post-migration; the1073  // parallel load and the parallel derivation below keep wall-clock time1074  // constant regardless of which slot's passphrase (real or decoy) the1075  // caller supplied, so a timing side channel cannot distinguish the1076  // duress path from the real-unlock path (DUR-002).1077  const [slotA, slotB] = await Promise.all([1078    loadSlot(SLOT_A),1079    loadSlot(SLOT_B),1080  ]);10811082  // Derive both keys in parallel, unconditionally.1083  const [keyA, keyB] = await Promise.all([1084    slotA ? deriveKey(password, unhex(slotA.salt)) : Promise.resolve(null),1085    slotB ? deriveKey(password, unhex(slotB.salt)) : Promise.resolve(null),1086  ]);10871088  // Attempt both decrypts in parallel. The Result variant lets us distinguish1089  // 'wrong_key' from 'corrupted' so callers can offer a history-restore path1090  // when a slot's schema is invalid even though AES-GCM authenticated (M7).1091  const [resultA, resultB] = await Promise.all([1092    keyA && slotA1093      ? tryDecryptSlotResult(slotA, keyA, "slot_a_v1")1094      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1095    keyB && slotB1096      ? tryDecryptSlotResult(slotB, keyB, "slot_b_v1")1097      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1098  ]);1099  const payloadA = resultA.status === "ok" ? resultA.payload : null;1100  const payloadB = resultB.status === "ok" ? resultB.payload : null;11011102  if (payloadA && keyA) {1103    derivedKey = keyA;1104    void saveSessionKey(keyA, "real");1105    vaultMode = "real";1106    entries = payloadA.entries;1107    duressConfigured = payloadA.duressConfigured;1108    buildDomainIndexSync(entries);1109    await loadSettings();1110    touchAutoLock();1111    if (keyB) zeroKey(keyB);11121113    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).1114    if (slotA) {1115      const needsRecoverySetup =1116        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;1117      if (!needsRecoverySetup) {1118        rootKey = await deriveRootKeyFromDerivedKey(keyA, slotA);1119      }1120      // Pre-v2.12 vault, or a vault whose pw_root_* fields haven't been written1121      // yet: generate the recovery escrow and seed pw_root_* so subsequent1122      // unlocks can rehydrate rootKey without the user's recovery words.1123      // Best-effort — a failure here must not block unlock.1124      if (needsRecoverySetup || !rootKey) {1125        try {1126          await migrateToRecovery();1127          if (needsRecoverySetup) {1128            // String "1" matches the popup's existing peach_recovery_pending1129            // check; surfaced so the user re-confirms recovery words after a1130            // legacy-vault unlock (H2/COR-006).1131            await chrome.storage.local.set({ peach_recovery_pending: '1' });1132          }1133        } catch (migrationErr) {1134          console.warn('[Peach] recovery migration failed:', migrationErr);1135        }1136      }1137    }11381139    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat1140    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so1141    // the next save uses the new format and migration doesn't re-run forever.1142    if (payloadA.migratedFromLegacy) {1143      await persistSlotA();1144    }1145    return true;1146  }11471148  if (payloadB && keyB) {1149    derivedKey = keyB;1150    void saveSessionKey(keyB, "decoy");1151    vaultMode = "decoy";1152    entries = payloadB.entries;1153    duressConfigured = false;1154    buildDomainIndexSync(entries);1155    await loadSettings();1156    touchAutoLock();1157    if (keyA) zeroKey(keyA);1158    return true;1159  }11601161  if (keyA) zeroKey(keyA);1162  if (keyB) zeroKey(keyB);1163  // Surface corruption (good AES tag, broken schema) distinctly from1164  // wrong-password so the popup can offer history-restore (M7/COR-007).1165  if (resultA.status === "corrupted") {1166    lastUnlockFailureReason = resultA.reason;1167  } else if (resultB.status === "corrupted") {1168    lastUnlockFailureReason = resultB.reason;1169  }1170  return false;1171}11721173// ---------------------------------------------------------------------------1174// MV3 service worker session key — survives SW restarts within a browser session1175// ---------------------------------------------------------------------------11761177// chrome.storage.session is cleared on browser close; survives SW restart.1178// Firefox does not implement it (persistent background script, no restart risk).1179const SESSION_KEY_STORAGE = "peach_session_dk";11801181async function saveSessionKey(key: Uint8Array, mode: "real" | "decoy"): Promise<void> {1182  if (!chrome.storage.session) return;1183  await chrome.storage.session.set({ [SESSION_KEY_STORAGE]: { keyHex: hex(key), mode } });1184}11851186async function clearSessionKey(): Promise<void> {1187  if (!chrome.storage.session) return;1188  await chrome.storage.session.remove(SESSION_KEY_STORAGE);1189}11901191/**1192 * On SW restart, re-derive all in-memory vault state from the session key1193 * stored in chrome.storage.session (set at unlock time, cleared on lock).1194 * Returns true if the vault was restored, false if no session key was found.1195 * No-ops if the vault is already unlocked.1196 */1197export async function restoreFromSession(): Promise<boolean> {1198  if (derivedKey !== null) return true;1199  if (!chrome.storage.session) return false;1200  const stored = await chrome.storage.session.get(SESSION_KEY_STORAGE);1201  const entry = stored[SESSION_KEY_STORAGE] as { keyHex: string; mode: "real" | "decoy" } | undefined;1202  if (!entry?.keyHex) return false;12031204  const key = unhex(entry.keyHex);1205  const slotKey = entry.mode === "real" ? SLOT_A : SLOT_B;1206  const aadTag = entry.mode === "real" ? "slot_a_v1" : "slot_b_v1";1207  const slot = await loadSlot(slotKey);1208  if (!slot) { await clearSessionKey(); return false; }12091210  const result = await tryDecryptSlotResult(slot, key, aadTag);1211  if (result.status !== "ok") { await clearSessionKey(); return false; }12121213  derivedKey = key;1214  vaultMode = entry.mode;1215  entries = result.payload.entries;1216  duressConfigured = entry.mode === "real" ? result.payload.duressConfigured : false;1217  buildDomainIndexSync(entries);1218  await loadSettings();1219  touchAutoLock();12201221  if (entry.mode === "real" && slot.recovery_salt && slot.recovery_wrapped_root_key) {1222    try { rootKey = await deriveRootKeyFromDerivedKey(key, slot); } catch { /* non-fatal */ }1223  }12241225  return true;1226}12271228export function lock(): void {1229  void clearSessionKey();1230  if (derivedKey) {1231    zeroKey(derivedKey);1232    derivedKey = null;1233  }1234  if (rootKey) {1235    rootKey.fill(0);1236    rootKey = null;1237  }12381239  entries = [];1240  domainIndex = new Map();1241  vaultMode = "real";1242  duressConfigured = false;1243  clearAutoLock();1244}12451246export function isUnlocked(): boolean {1247  return derivedKey !== null;1248}12491250// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).1251// Used by Android biometric unlock — the key was derived on the native side.1252export async function unlockWithKey(1253  masterKeyHex: string,1254): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {1255  try {1256    const masterKeyBytes = unhex(masterKeyHex);1257    const stored = await loadSlot(SLOT_A);1258    if (!stored) return { ok: false, error: "invalid_key" };1259    const payload = await tryDecryptSlot(stored, masterKeyBytes, "slot_a_v1");1260    if (!payload) return { ok: false, error: "invalid_key" };12611262    derivedKey = masterKeyBytes;1263    vaultMode = "real";1264    entries = payload.entries;1265    duressConfigured = payload.duressConfigured;1266    buildDomainIndexSync(entries);1267    await loadSettings();1268    touchAutoLock();1269    return { ok: true, entries };1270  } catch {1271    return { ok: false, error: "invalid_key" };1272  }1273}12741275/**1276 * Restore the vault from a parsed Peach Codex payload.1277 *1278 * The supplied passphrase is verified against the Codex's slotA *before* any1279 * storage mutation — wrong passphrases return without touching disk. On1280 * success, the current vault state is snapshotted (reason: "restore") so the1281 * user can revert via Settings -> Vault History, then slotA/slotB are written1282 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1283 * regular `unlock()` follows so the rest of the SW sees the standard1284 * post-unlock state.1285 *1286 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1287 * printed by a Pro user must always be restorable, even on a free device.1288 *1289 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1290 * is an explicit user action with a known passphrase, not a passive lock1291 * screen, so the duress-vs-real timing side channel does not apply.1292 */1293export async function unlockFromCodex(params: {1294  slotA: string; // JSON string of StoredVault for slot A1295  slotB: string | null; // JSON string of StoredVault for slot B, or null1296  timestamp: number;1297  masterPassphrase: string;1298}): Promise<1299  | { ok: true; entryCount: number }1300  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1301> {1302  let parsedSlotA: StoredVault;1303  let parsedSlotB: StoredVault | null = null;1304  try {1305    const rawA = JSON.parse(params.slotA) as unknown;1306    if (!hasValidVaultSchema(rawA)) {1307      return { ok: false, reason: "invalid_slot_format" };1308    }1309    parsedSlotA = rawA;1310    if (params.slotB !== null) {1311      const rawB = JSON.parse(params.slotB) as unknown;1312      if (!hasValidVaultSchema(rawB)) {1313        return { ok: false, reason: "invalid_slot_format" };1314      }1315      parsedSlotB = rawB;1316    }1317  } catch {1318    return { ok: false, reason: "invalid_slot_format" };1319  }13201321  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1322  // If this fails the on-disk vault is left completely untouched.1323  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1324  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey, "slot_a_v1");1325  if (!trialPayload) {1326    zeroKey(trialKey);1327    return { ok: false, reason: "wrong_passphrase" };1328  }1329  // Drop the trial key; `unlock()` below derives its own copy. Holding two1330  // copies of the master key in memory at once would be a needless surface.1331  zeroKey(trialKey);13321333  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1334  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1335  await takeVaultSnapshot("restore");13361337  await writeSlot(SLOT_A, parsedSlotA);1338  if (parsedSlotB) {1339    await writeSlot(SLOT_B, parsedSlotB);1340  } else {1341    // Slot B must always exist with ciphertext length matching slot A1342    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1343    // configured); regenerate noise to satisfy the invariant.1344    await writeSlot(1345      SLOT_B,1346      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1347    );1348  }1349  await chrome.storage.local.set({1350    peach_last_modified_at: String(params.timestamp),1351  });13521353  // Standard unlock path so domain index, settings, auto-lock, and1354  // recovery-key state all populate exactly as a normal unlock would.1355  const ok = await unlock(params.masterPassphrase);1356  if (!ok) {1357    // Should be unreachable — we just verified the passphrase against the1358    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1359    // simple if some race or storage anomaly intrudes.1360    return { ok: false, reason: "wrong_passphrase" };1361  }1362  return { ok: true, entryCount: getEntries().length };1363}13641365// Returns the current in-memory derived key as a hex string.1366// Used by the biometric enable flow in Settings to store the key in Android Keystore.1367export function getDerivedKeyHex(): string {1368  if (!derivedKey) throw new Error("vault_not_unlocked");1369  return hex(derivedKey);1370}13711372/**1373 * Returns the vault's stable root key as hex, or null when locked / not yet1374 * populated. The root key is random per-vault, generated at creation, escrowed1375 * under the recovery words, and re-wrapped (not regenerated) on every password1376 * change — so it is INVARIANT across password rotation, unlike getDerivedKeyHex.1377 *1378 * This is the correct input for sync identity derivation (vault_id, the Noise1379 * XX static key, the Ed25519 manifest signing key, and per-entry envelope1380 * keys): paired devices keep the same identity and can still decrypt each1381 * other's entries after either side rotates its master password (SEV-2-B).1382 *1383 * Returns null rather than throwing so sync callers degrade to VAULT_LOCKED1384 * instead of crashing; rootKey is populated on every real-mode unlock via1385 * deriveRootKeyFromDerivedKey / migrateToRecovery.1386 */1387export function getRootKeyHex(): string | null {1388  return rootKey ? hex(rootKey) : null;1389}13901391export function getEntries(): VaultEntry[] {1392  if (!derivedKey) throw new Error("vault_locked");1393  return entries.filter((entry) => !entry.deletedAt);1394}13951396const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;13971398export function getIdentities(): SavedIdentity[] {1399  if (!derivedKey) throw new Error("vault_locked");1400  const active = entries.filter((e) => !e.deletedAt && e.type === "login");14011402  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1403  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();14041405  for (const entry of active) {1406    const domain = extractDomain(entry.url ?? "") ?? "";1407    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;14081409    function tally(map: Map<string, { lastUsed: number; count: number; domain: string }>, v: string) {1410      const existing = map.get(v);1411      if (!existing || modifiedAt > existing.lastUsed) {1412        map.set(v, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1413      } else {1414        map.set(v, { ...existing, count: existing.count + 1 });1415      }1416    }14171418    const uVal = entry.username?.trim();1419    if (uVal) tally(EMAIL_RE.test(uVal) ? emailMap : usernameMap, uVal);14201421    // Also count the dedicated email field — entries created via the inline1422    // popup new-entry form store email here rather than in username.1423    const eVal = entry.email?.trim();1424    if (eVal) tally(emailMap, eVal);1425  }14261427  const toIdentity = (type: "email" | "username") =>1428    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1429      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1430    });14311432  return [1433    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1434    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1435  ];1436}14371438export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1439  if (!derivedKey) throw new Error("vault_locked");14401441  const now = Date.now();1442  const added: VaultEntry[] = [];1443  for (const draft of drafts) {1444    try {1445      const sanitized = sanitizeEntryDraft(draft);1446      const nextEntry: VaultEntry = {1447        ...sanitized,1448        id: crypto.randomUUID(),1449        createdAt: now,1450        modifiedAt: monotonicEntryTimestamp(),1451      };1452      entries.push(nextEntry);1453      added.push(nextEntry);1454    } catch {1455      // Skip invalid drafts silently — caller can see diff between input and output length1456    }1457  }14581459  if (added.length > 0) {1460    await persistEntries();1461  }14621463  return added;1464}14651466/**1467 * Restore a set of entries in full fidelity — preserves id, createdAt,1468 * modifiedAt, and every other field unlike `batchAddEntries` which1469 * assigns fresh identity. Used by restore paths where the backup is the1470 * authoritative source of truth. `mode` controls how entries already in1471 * the vault are treated.1472 */1473export async function restoreEntries(1474  incoming: VaultEntry[],1475  mode: "replace" | "merge"1476): Promise<{ added: number; updated: number; skipped: number }> {1477  if (!derivedKey) throw new Error("vault_locked");14781479  // A replace with zero incoming entries is almost certainly a caller bug1480  // (e.g. an import that silently dropped everything). Refuse rather than1481  // wipe the vault.1482  if (mode === "replace" && incoming.length === 0) {1483    throw new Error("refuse to replace with empty entry set");1484  }14851486  let added = 0;1487  let updated = 0;1488  let skipped = 0;14891490  if (mode === "replace") {1491    entries = [];1492    for (const entry of incoming) {1493      const normalized = sanitizeRestoredEntry(entry);1494      if (!normalized) {1495        skipped += 1;1496        continue;1497      }1498      entries.push(normalized);1499      added += 1;1500    }1501  } else {1502    const byId = new Map<string, VaultEntry>();1503    for (const e of entries) byId.set(e.id, e);15041505    for (const entry of incoming) {1506      const normalized = sanitizeRestoredEntry(entry);1507      if (!normalized) {1508        skipped += 1;1509        continue;1510      }1511      const existing = byId.get(normalized.id);1512      if (!existing) {1513        entries.push(normalized);1514        added += 1;1515      } else if (normalized.modifiedAt > existing.modifiedAt) {1516        const idx = entries.findIndex((e) => e.id === existing.id);1517        if (idx !== -1) {1518          entries[idx] = normalized;1519          updated += 1;1520        }1521      } else {1522        skipped += 1;1523      }1524    }1525  }15261527  await persistEntries("import");1528  return { added, updated, skipped };1529}15301531/**1532 * Validate + normalise a VaultEntry coming from a backup. Accepts1533 * anything structurally sound; keeps all metadata verbatim. Returns1534 * null for values the live vault shouldn't accept (e.g., missing title).1535 */1536function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1537  if (!entry || typeof entry !== "object") return null;1538  const title = typeof entry.title === "string" ? entry.title.trim() : "";1539  if (!title) return null;1540  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1541  const now = Date.now();1542  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1543  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;15441545  // Migrate legacy passkey shape on import (same logic as on-disk read).1546  // Imports from pre-v2.11.0 vault backups still carry type:"passkey" with1547  // flat fields; bring them into the new shape immediately.1548  const migrated: VaultEntry = migrateLegacyPasskey(1549    {1550      ...entry,1551      id,1552      title,1553      type: entry.type ?? "login",1554      createdAt,1555      modifiedAt,1556    },1557    entry1558  );15591560  // Login entries from backup must carry a password (or be passkey-only,1561  // in which case the passkey sub-object replaces the password requirement).1562  if (1563    migrated.type === "login" &&1564    !migrated.passkey &&1565    typeof migrated.password !== "string"1566  ) {1567    return null;1568  }1569  if (migrated.passkey && !migrated.passkey.credentialId) {1570    return null;1571  }15721573  return migrated;1574}15751576// Soft-deletes every non-deleted entry in one call. The literal string1577// argument is a tripwire against accidental calls — any caller that would1578// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1579// to a boolean.1580export async function clearVaultEntries(1581  confirmation: "REPLACE_ALL"1582): Promise<void> {1583  if (confirmation !== "REPLACE_ALL") {1584    throw new Error("clearVaultEntries requires explicit confirmation");1585  }1586  if (!derivedKey) throw new Error("vault_locked");1587  for (const entry of entries) {1588    if (!entry.deletedAt) {1589      const stamp = monotonicEntryTimestamp(entry.modifiedAt);1590      entry.deletedAt = stamp;1591      entry.modifiedAt = stamp;1592    }1593  }1594  await persistEntries();1595}15961597export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1598  if (!derivedKey) throw new Error("vault_locked");15991600  const sanitized = sanitizeEntryDraft(entry);1601  const now = Date.now();1602  const stamp = monotonicEntryTimestamp();1603  const nextEntry: VaultEntry = {1604    ...sanitized,1605    id: crypto.randomUUID(),1606    createdAt: now,1607    modifiedAt: stamp,1608    // Stamp passwordChangedAt at creation so age tracking is accurate from1609    // day one. Non-login entries carry the same stamp harmlessly.1610    passwordChangedAt: sanitized.password ? stamp : undefined,1611  };16121613  // Clone-and-replace so a failed persist rolls back the in-memory state1614  // (COR-008). Without this, a quota-full or storage error leaves the1615  // module-level `entries` ahead of what's actually on disk.1616  const rollback = entries;1617  entries = [...entries, nextEntry];1618  try {1619    await persistEntries();1620  } catch (err) {1621    entries = rollback;1622    throw err;1623  }16241625  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1626  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1627    checkBreach(nextEntry.password).then(async (count) => {1628      if (count > 0) {1629        const idx = entries.findIndex((e) => e.id === nextEntry.id);1630        if (idx !== -1) {1631          entries[idx] = { ...entries[idx], breach: count, modifiedAt: monotonicEntryTimestamp(entries[idx].modifiedAt) };1632          await persistEntries();1633        }1634      }1635    });1636  }16371638  fireSyncTrigger("vault_change");16391640  return nextEntry;1641}16421643/**1644 * Run the passkey-merge pass on the live in-memory entries and persist if1645 * anything changed. Called immediately after any passkey registration so that1646 * a same-domain TOTP-only entry already in the vault gets consolidated right1647 * away instead of waiting until the next lock/unlock cycle.1648 */1649export async function consolidatePasskeyEntries(): Promise<void> {1650  if (!derivedKey) return;1651  const { merged } = mergePasskeyOnlyIntoLogins(entries);1652  if (merged) {1653    await persistEntries();1654  }1655}16561657export async function updateEntry(1658  id: string,1659  patch: Partial<VaultEntry>1660): Promise<void> {1661  if (!derivedKey) throw new Error("vault_locked");16621663  const idx = entries.findIndex((entry) => entry.id === id);1664  if (idx === -1 || entries[idx].deletedAt) {1665    throw new Error("entry_not_found");1666  }16671668  const current = entries[idx];1669  const sanitizedPatch = sanitizeEntryPatch(patch);1670  // Refresh passwordChangedAt only when the password value actually changes —1671  // a metadata-only update (title, tags, etc.) must not reset the age clock1672  // that drives the Pro health rotation recommendation.1673  const passwordChanged =1674    "password" in patch &&1675    typeof sanitizedPatch.password === "string" &&1676    sanitizedPatch.password !== current.password;1677  const stamp = monotonicEntryTimestamp(current.modifiedAt);1678  // When the password actually changes, push the outgoing one onto the history1679  // (newest first) so the user can reveal/copy/restore it later. We record the1680  // age it carried (passwordChangedAt, falling back to createdAt) rather than1681  // `now`, so the list reflects when each password was last in effect. Only1682  // capture a non-empty prior value — clearing a password leaves no secret to1683  // keep, and we skip an immediate exact duplicate (e.g. restore-then-restore)1684  // to avoid noise.1685  const priorPassword = current.password;1686  const shouldCapture =1687    passwordChanged &&1688    typeof priorPassword === "string" &&1689    priorPassword.length > 0 &&1690    current.passwordHistory?.[0]?.password !== priorPassword;1691  const nextHistory = shouldCapture1692    ? [1693        { password: priorPassword!, changedAt: current.passwordChangedAt ?? current.createdAt },1694        ...(current.passwordHistory ?? []),1695      ].slice(0, PASSWORD_HISTORY_MAX)1696    : current.passwordHistory;1697  entries[idx] = {1698    ...current,1699    ...sanitizedPatch,1700    modifiedAt: stamp,1701    ...(passwordChanged ? { passwordChangedAt: stamp, passwordHistory: nextHistory } : {}),1702  };1703170417051706  // Card entries must retain their card number1707  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1708    entries[idx] = current;1709    throw new Error("card number is required for card entries");1710  }17111712  // Roll the in-memory entry back to its pre-edit state if the disk write1713  // fails (e.g. storage quota). Without this the module-level `entries` array1714  // would hold an edit that never landed on disk, diverging from the vault1715  // until the next unlock silently reverts it (SEV-3-A). Mirrors addEntry.1716  try {1717    await persistEntries();1718  } catch (err) {1719    entries[idx] = current;1720    throw err;1721  }17221723  // Breach check when password changes: clear immediately so old breach doesn't linger,1724  // then async-check the new password and only re-set if it's also found.1725  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1726    entries[idx] = { ...entries[idx], breach: undefined };1727    await persistEntries();1728    const password = entries[idx].password;1729    if (password) {1730      checkBreach(password).then(async (count) => {1731        const i = entries.findIndex((e) => e.id === id);1732        if (i !== -1 && count > 0) {1733          entries[i] = { ...entries[i], breach: count, modifiedAt: monotonicEntryTimestamp(entries[i].modifiedAt) };1734          await persistEntries();1735        }1736      });1737    }1738  }17391740  fireSyncTrigger("vault_change");1741}17421743export async function deleteEntry(id: string): Promise<void> {1744  if (!derivedKey) throw new Error("vault_locked");17451746  const entry = entries.find((item) => item.id === id);1747  if (!entry || entry.deletedAt) {1748    throw new Error("entry_not_found");1749  }17501751  // Tombstone timestamp must be strictly greater than the entry's last1752  // modification so the deletion wins LWW against any in-flight edit, even1753  // under clock skew (SEV-2-E).1754  const previousModifiedAt = entry.modifiedAt;1755  const stamp = monotonicEntryTimestamp(entry.modifiedAt);1756  entry.deletedAt = stamp;1757  entry.modifiedAt = stamp;1758  // Roll the tombstone back if the disk write fails, so a quota error doesn't1759  // leave the entry deleted in memory but alive on disk (SEV-3-A).1760  try {1761    await persistEntries();1762  } catch (err) {1763    delete entry.deletedAt;1764    entry.modifiedAt = previousModifiedAt;1765    throw err;1766  }1767  fireSyncTrigger("vault_change");1768}17691770export function getDeletedEntries(): VaultEntry[] {1771  if (!derivedKey) return [];1772  return entries.filter((e) => !!e.deletedAt);1773}17741775/**1776 * Returns the vault's monotonic version — the `peach_last_modified_at` value1777 * stamped on every user-initiated write. Remote (sync) writes do NOT advance1778 * this version so a sync round never cascades into a second outbound sync.1779 */1780export async function getVaultVersion(): Promise<number> {1781  try {1782    const raw = await chrome.storage.local.get("peach_last_modified_at");1783    const v = (raw as Record<string, unknown>).peach_last_modified_at;1784    if (typeof v === "number") return v;1785    if (typeof v === "string" && v !== "") {1786      const n = Number.parseInt(v, 10);1787      return Number.isFinite(n) ? n : 0;1788    }1789    return 0;1790  } catch {1791    return 0;1792  }1793}17941795/**1796 * Internal write variants — bypass `fireSyncTrigger` so the sync layer can1797 * apply entries without cascading into another outbound sync round.1798 *1799 * INVARIANT: callers must not pass entries whose IDs collide with existing1800 * active (non-deleted) entries unless the intent is an explicit overwrite.1801 * These functions do not validate field-level constraints (password required,1802 * card number required, etc.) — they are trust-the-caller paths for sync and1803 * test harness use only.1804 */1805export async function addEntryDirect(entry: VaultEntry): Promise<void> {1806  if (!derivedKey) throw new Error("vault_locked");1807  entries = [...entries, entry];1808  await persistEntries("sync_received");1809}18101811export async function updateEntryDirect(patch: VaultEntry): Promise<void> {1812  if (!derivedKey) throw new Error("vault_locked");1813  const idx = entries.findIndex((e) => e.id === patch.id);1814  if (idx === -1) throw new Error("entry_not_found");1815  entries[idx] = { ...entries[idx], ...patch };1816  await persistEntries("sync_received");1817}18181819export async function deleteEntryDirect(id: string, deletedAt: number): Promise<void> {1820  if (!derivedKey) throw new Error("vault_locked");1821  const idx = entries.findIndex((e) => e.id === id);1822  if (idx === -1) throw new Error("entry_not_found");1823  entries[idx] = { ...entries[idx], deletedAt, modifiedAt: deletedAt };1824  await persistEntries("sync_received");1825}18261827/**1828 * Apply a batch of remote entries + tombstones from a chirp-sync peer.1829 * Last-write-wins by `modifiedAt`; a tombstone supersedes a live entry when1830 * `deletedAt > modifiedAt` on the local side. Mutates `entries` directly and1831 * persists with reason `sync_received` — does NOT fire the sync dispatcher, so1832 * applying a sync round cannot cascade into another outbound sync.1833 *1834 * `applied` counts entries that actually changed local state (new add,1835 * overwrite-by-newer, or new tombstone). `received` is the raw payload size1836 * the caller can surface in the sync result UI.1837 */1838export async function applyMergedRemoteEntries(1839  remoteEntries: VaultEntry[],1840  remoteTombstones: Record<string, number>,1841  context: { deviceName?: string } = {},1842): Promise<{ received: number; applied: number; conflicts: number }> {1843  // DESIGN: last-write-wins is applied PER-ENTRY, not per-field. If two devices1844  // concurrently edit different fields of the same entry, the one with the1845  // newer modifiedAt wins the whole entry and the other device's field edits1846  // are dropped silently. This is the deliberate trade-off for a simple,1847  // conflict-free merge — per-field CRDT merge is out of scope. Guidance for1848  // users: edit a given entry on one device at a time.1849  if (!derivedKey) throw new Error("vault_locked");18501851  const received = remoteEntries.length + Object.keys(remoteTombstones).length;1852  let applied = 0;1853  let conflicts = 0;18541855  const byId = new Map<string, VaultEntry>();1856  for (const e of entries) byId.set(e.id, e);18571858  const alerts: SyncAlert[] = [];1859  const deviceName = context.deviceName ?? "paired device";1860  const mergeNow = Date.now();18611862  for (const remote of remoteEntries) {1863    const sanitized = sanitizeRestoredEntry(remote);1864    if (!sanitized) continue;1865    // HV-1a: a far-future remote modifiedAt must not permanently pin a1866    // malicious value against honest local edits. Clamp BEFORE any1867    // comparison against local timestamps.1868    sanitized.modifiedAt = clampSyncTimestamp(sanitized.modifiedAt, mergeNow);1869    if (typeof sanitized.deletedAt === "number") {1870      sanitized.deletedAt = clampSyncTimestamp(sanitized.deletedAt, mergeNow);1871    }1872    const tombstoneTs = remoteTombstones[sanitized.id];1873    if (tombstoneTs !== undefined && clampSyncTimestamp(tombstoneTs, mergeNow) >= sanitized.modifiedAt) {1874      continue;1875    }1876    const local = byId.get(sanitized.id);1877    if (!local) {1878      entries.push(sanitized);1879      byId.set(sanitized.id, sanitized);1880      applied += 1;1881    } else if (sanitized.modifiedAt > local.modifiedAt) {1882      const idx = entries.findIndex((e) => e.id === local.id);1883      if (idx !== -1) {1884        // HV-1b: surface remote password REPLACEMENTS (existing id, password1885        // differs) so a compromised peer can't silently rewrite credentials.1886        if (1887          !local.deletedAt &&1888          typeof sanitized.password === "string" &&1889          typeof local.password === "string" &&1890          sanitized.password !== local.password1891        ) {1892          alerts.push({1893            type: "REMOTE_PASSWORD_CHANGE",1894            entryId: sanitized.id,1895            entryTitle: sanitized.title,1896            deviceName,1897            timestamp: mergeNow,1898          });1899        }1900        entries[idx] = sanitized;1901        byId.set(sanitized.id, sanitized);1902        applied += 1;1903        if (!local.deletedAt) conflicts += 1;1904      }1905    }1906  }19071908  for (const [id, rawDeletedAt] of Object.entries(remoteTombstones)) {1909    if (typeof rawDeletedAt !== "number" || !Number.isFinite(rawDeletedAt)) continue;1910    // HV-1a/1c: clamp the tombstone timestamp before every comparison.1911    const deletedAt = clampSyncTimestamp(rawDeletedAt, mergeNow);1912    const local = byId.get(id);1913    if (!local) continue;1914    if (local.deletedAt && local.deletedAt >= deletedAt) continue;1915    if (deletedAt < local.modifiedAt) continue;1916    const idx = entries.findIndex((e) => e.id === id);1917    if (idx !== -1) {1918      // HV-1b: surface remote deletes of live local entries.1919      if (!local.deletedAt) {1920        alerts.push({1921          type: "REMOTE_DELETE",1922          entryId: id,1923          entryTitle: local.title,1924          deviceName,1925          timestamp: mergeNow,1926        });1927      }1928      entries[idx] = { ...local, deletedAt, modifiedAt: deletedAt };1929      applied += 1;1930    }1931  }19321933  if (applied > 0) {1934    await persistEntries("sync_received");1935    buildDomainIndexSync(entries);1936  }1937  // Queued after the merge committed — an alert must describe a mutation1938  // that actually landed. recordSyncAlerts never throws.1939  await recordSyncAlerts(alerts);1940  return { received, applied, conflicts };1941}19421943export async function restoreEntry(id: string): Promise<void> {1944  if (!derivedKey) throw new Error("vault_locked");1945  const entry = entries.find((e) => e.id === id && e.deletedAt);1946  if (!entry) throw new Error("deleted entry not found");1947  delete entry.deletedAt;1948  entry.modifiedAt = monotonicEntryTimestamp(entry.modifiedAt);1949  await persistEntries();1950  buildDomainIndexSync(entries);1951}19521953export async function purgeEntry(id: string): Promise<void> {1954  if (!derivedKey) throw new Error("vault_locked");1955  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);1956  if (idx === -1) throw new Error("deleted entry not found");1957  entries.splice(idx, 1);1958  await persistEntries();1959}19601961/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */1962export async function purgeExpiredTrash(retentionMs: number): Promise<number> {1963  if (!derivedKey || retentionMs === 0) return 0;1964  const cutoff = Date.now() - retentionMs;1965  const before = entries.length;1966  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));1967  const purged = before - entries.length;1968  if (purged > 0) await persistEntries();1969  return purged;1970}19711972export function queryDomain(domain: string): VaultEntry[] {1973  return domainIndex.get(domain) ?? [];1974}19751976export function getEntryById(id: string): VaultEntry | undefined {1977  return entries.find((entry) => entry.id === id && !entry.deletedAt);1978}19791980async function persistSlotA(1981  reason: VaultSnapshot["reason"] = "local_save",1982): Promise<StoredVault> {1983  if (!derivedKey) throw new Error("vault_locked");19841985  const slotA = await loadSlot(SLOT_A);1986  if (!slotA) throw new Error("missing slot A");19871988  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures1989  // are swallowed by takeVaultSnapshot — history must never block a save.1990  await takeVaultSnapshot(reason);19911992  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);1993  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");19941995  // Preserve existing recovery escrow fields.1996  const nextSlotA: StoredVault = {1997    ...slotA,1998    nonce,1999    ciphertext,2000    tag,2001    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot2002  };2003  await writeSlot(SLOT_A, nextSlotA);20042005  // Slot A (the real vault) is now committed. Wrap every later write so a2006  // mid-persist failure (e.g. a storage error after slot A landed) restores the2007  // prior slot A before rethrowing. Without this, a "failed" save that already2008  // wrote slot A leaves disk ahead of the in-memory rollback in addEntry/2009  // updateEntry/deleteEntry — the entry stays silently on disk and resurfaces2010  // as a duplicate on the next unlock (observed under storage-quota pressure).2011  try {2012    // When duress is not configured, keep slot B size-matched with slot A by2013    // regenerating its noise. When duress IS configured, leave slot B alone —2014    // we don't have the decoy key to re-encrypt it. This is a known trade-off.2015    if (!duressConfigured) {2016      await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2017    }2018    // Only advance the vault version for locally-originated writes. Entries2019    // applied from a sync peer ("sync_received") must NOT bump the counter —2020    // otherwise applying a remote round makes us look newer than the peer and2021    // triggers a redundant outbound sync, defeating the no-op optimisation2022    // and risking a ping-pong between two devices (SEV-2-C).2023    if (reason !== "sync_received") {2024      await stampLocalVaultMutation();2025    }2026  } catch (err) {2027    // Best-effort restore — the prior slot A is smaller-or-equal, so this write2028    // fits even when the failure was a quota overflow.2029    await writeSlot(SLOT_A, slotA).catch(() => {});2030    throw err;2031  }20322033  return nextSlotA;2034}20352036async function persistSlotB(): Promise<void> {2037  if (!derivedKey) throw new Error("vault_locked");20382039  const slotB = await loadSlot(SLOT_B);2040  if (!slotB) throw new Error("missing slot B");20412042  // Decoy plaintext always reports duressConfigured=false — the decoy user2043  // sees a clean slate. persistSlotB only runs in decoy mode, which only2044  // exists once duress is configured, so pad to the fixed duress ceiling to2045  // keep slot B's length equal to slot A's (audit MEDIUM — deniability).2046  const plaintext = encodePayload(entries, false, DURESS_SLOT_SIZE);2047  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_b_v1");20482049  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });2050  // Symmetric with persistSlotA: if the version stamp fails after the decoy2051  // slot committed, restore the prior slot B so a failed decoy-mode save can't2052  // leave disk ahead of the in-memory rollback.2053  try {2054    await stampLocalVaultMutation();2055  } catch (err) {2056    await writeSlot(SLOT_B, slotB).catch(() => {});2057    throw err;2058  }2059}20602061async function persistEntries(2062  reason: VaultSnapshot["reason"] = "local_save",2063): Promise<void> {2064  if (!derivedKey) throw new Error("vault_locked");20652066  if (vaultMode === "decoy") {2067    await persistSlotB();2068  } else {2069    await persistSlotA(reason);2070  }2071  buildDomainIndexSync(entries);2072}20732074function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {2075  if (!Array.isArray(values)) return undefined;2076  const seen = new Set<string>();2077  const cleaned: string[] = [];2078  for (const value of values) {2079    const trimmed = typeof value === "string" ? value.trim() : "";2080    if (!trimmed) continue;2081    const key = trimmed.toLowerCase();2082    if (seen.has(key)) continue;2083    seen.add(key);2084    cleaned.push(trimmed);2085  }2086  return cleaned.length > 0 ? cleaned : undefined;2087}20882089function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {2090  const title = entry.title.trim();2091  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new2092  // model is "any entry, optionally with a passkey sub-object." Imports and2093  // any in-flight callers from before v2.11.0 keep working without throwing.2094  let type: VaultEntry["type"] = entry.type ?? "login";2095  if (type === "passkey") type = "login";20962097  if (!title) throw new Error("title is required");20982099  if (entry.passkey) {2100    const p = entry.passkey;2101    if (2102      !p.rpId ||2103      !p.credentialId ||2104      !p.publicKeyCose ||2105      !p.userHandle ||2106      !p.userName ||2107      (p.algorithm !== -7 && p.algorithm !== -8)2108    ) {2109      throw new Error("invalid passkey credential");2110    }2111  }2112  if (type === "card") {2113    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {2114      throw new Error("cardholder name and card number are required");2115    }2116  }21172118  return {2119    type,2120    title,2121    username: type === "login" ? entry.username?.trim() ?? "" : undefined,2122    email: type === "login" ? entry.email?.trim() || undefined : undefined,2123    password: type === "login" ? entry.password : undefined,2124    url: type === "login" ? entry.url?.trim() ?? "" : undefined,2125    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,2126    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,2127    notes: entry.notes?.trim() || undefined,2128    breach: type === "login" ? entry.breach : undefined,2129    canary: type === "login" ? entry.canary : undefined,2130    tags: entry.tags,2131    passkey: entry.passkey,2132    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,2133    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,2134    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,2135    expYear: type === "card" ? entry.expYear?.trim() : undefined,2136    cvc: type === "card" ? entry.cvc?.trim() : undefined,2137    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,2138    network: type === "card" ? entry.network : undefined,2139    last4: type === "card" ? entry.last4?.trim() : undefined,2140  };2141}21422143function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {2144  const nextPatch: Partial<VaultEntry> = {};21452146  if ("type" in patch) {2147    nextPatch.type = patch.type;2148  }21492150  if ("title" in patch) {2151    const title = patch.title?.trim() ?? "";2152    if (!title) throw new Error("title is required");2153    nextPatch.title = title;2154  }21552156  if ("url" in patch) {2157    nextPatch.url = patch.url?.trim() ?? "";2158  }21592160  if ("additionalUrls" in patch) {2161    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];2162    const seen = new Set<string>();2163    const cleaned: string[] = [];2164    for (const value of incoming) {2165      const trimmed = typeof value === "string" ? value.trim() : "";2166      if (!trimmed) continue;2167      const key = trimmed.toLowerCase();2168      if (seen.has(key)) continue;2169      seen.add(key);2170      cleaned.push(trimmed);2171    }2172    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;2173  }21742175  if ("username" in patch) {2176    nextPatch.username = patch.username?.trim() ?? "";2177  }21782179  if ("email" in patch) {2180    nextPatch.email = patch.email?.trim() || undefined;2181  }21822183  if ("password" in patch) {2184    const password = patch.password ?? "";2185    // Allow clearing a password (e.g. on passkey-only entries)2186    nextPatch.password = password || undefined;2187  }21882189  if ("totp" in patch) {2190    nextPatch.totp = patch.totp?.trim() || undefined;2191  }21922193  if ("notes" in patch) {2194    nextPatch.notes = patch.notes?.trim() || undefined;2195  }21962197  if ("breach" in patch) {2198    nextPatch.breach = patch.breach;2199  }22002201  if ("canary" in patch) {2202    nextPatch.canary = patch.canary;2203  }22042205  if ("passkey" in patch) {2206    // Passkey sub-object replaces all the legacy flat passkey fields. A2207    // patch with `passkey: undefined` removes the credential from the entry2208    // (used by "remove passkey" without deleting the whole entry).2209    nextPatch.passkey = patch.passkey;2210  }22112212  if ("tags" in patch) {2213    nextPatch.tags = patch.tags;2214  }22152216  if ("cardholderName" in patch) {2217    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;2218  }2219  if ("cardNumber" in patch) {2220    const n = patch.cardNumber ?? "";2221    nextPatch.cardNumber = n || undefined;2222  }2223  if ("expMonth" in patch) {2224    nextPatch.expMonth = patch.expMonth?.trim() || undefined;2225  }2226  if ("expYear" in patch) {2227    nextPatch.expYear = patch.expYear?.trim() || undefined;2228  }2229  if ("cvc" in patch) {2230    nextPatch.cvc = patch.cvc?.trim() || undefined;2231  }2232  if ("cardNickname" in patch) {2233    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;2234  }2235  if ("network" in patch) {2236    nextPatch.network = patch.network;2237  }2238  if ("last4" in patch) {2239    nextPatch.last4 = patch.last4?.trim() || undefined;2240  }22412242  return nextPatch;2243}22442245function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {2246  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;2247  const modifiedAt =2248    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;22492250  const base: VaultEntry = {2251    id: entry.id,2252    type: "type" in entry ? entry.type : "login",2253    title: entry.title,2254    username: entry.username,2255    email: "email" in entry ? entry.email : undefined,2256    password: entry.password,2257    url: entry.url,2258    additionalUrls:2259      "additionalUrls" in entry && Array.isArray(entry.additionalUrls)2260        ? entry.additionalUrls2261        : undefined,2262    totp: entry.totp,2263    notes: entry.notes,2264    breach: entry.breach,2265    // Preserve the password-age clock across lock/unlock. Without this the2266    // field is dropped on every decode and the advanced-health "aged password"2267    // signal silently resets to createdAt on each unlock (SEV-2-A).2268    passwordChangedAt:2269      "passwordChangedAt" in entry ? entry.passwordChangedAt : undefined,2270    // Preserve past-password history across lock/unlock. normalizeEntry is a2271    // field whitelist, so an unlisted field is silently dropped on every decode2272    // (same class as the passwordChangedAt SEV-2-A above).2273    passwordHistory:2274      "passwordHistory" in entry && Array.isArray(entry.passwordHistory)2275        ? entry.passwordHistory2276        : undefined,2277    canary: "canary" in entry ? entry.canary : undefined,2278    passkey: "passkey" in entry ? entry.passkey : undefined,2279    createdAt,2280    modifiedAt,2281    deletedAt: entry.deletedAt,2282    tags: "tags" in entry ? entry.tags : undefined,2283    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,2284    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,2285    expMonth: "expMonth" in entry ? entry.expMonth : undefined,2286    expYear: "expYear" in entry ? entry.expYear : undefined,2287    cvc: "cvc" in entry ? entry.cvc : undefined,2288    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,2289    network: "network" in entry ? entry.network : undefined,2290    last4: "last4" in entry ? entry.last4 : undefined,2291  };22922293  return migrateLegacyPasskey(base, entry);2294}22952296/**2297 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`2298 * with flat top-level fields (rpId, credentialId, etc.). This converts them2299 * to the new shape — `type:"login"` with a `passkey` sub-object — so all2300 * downstream consumers can rely on the new model. The result is rewritten2301 * to disk on next vault save (hasLegacyShape detects the un-migrated case).2302 */2303function migrateLegacyPasskey(2304  base: VaultEntry,2305  raw: VaultEntry | LegacyVaultEntry2306): VaultEntry {2307  if (base.passkey) return base;2308  const flat = raw as Partial<VaultEntry>;2309  const isPasskeyType = base.type === "passkey";2310  const hasFlatPasskeyFields =2311    typeof flat.rpId === "string" &&2312    typeof flat.credentialId === "string" &&2313    typeof flat.publicKeyCose === "string" &&2314    typeof flat.userHandle === "string" &&2315    typeof flat.userName === "string";2316  if (!isPasskeyType && !hasFlatPasskeyFields) return base;2317  if (!hasFlatPasskeyFields) return base;23182319  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;2320  base.passkey = {2321    rpId: flat.rpId!,2322    rpName: flat.rpName ?? flat.rpId!,2323    credentialId: flat.credentialId!,2324    publicKeyCose: flat.publicKeyCose!,2325    privateKeyJwk: flat.privateKeyJwk,2326    userHandle: flat.userHandle!,2327    userName: flat.userName!,2328    userDisplayName: flat.userDisplayName ?? flat.userName!,2329    algorithm,2330    signCount: flat.signCount ?? 0,2331    aaguid: flat.aaguid ?? "",2332    backupEligible: flat.backupEligible === true,2333    backupState: flat.backupState === true,2334    lastUsedAt: flat.lastUsedAt,2335    browser: flat.browser,2336    transports: flat.transports,2337    createdAt: base.createdAt,2338  };2339  // Promote to "login" so the unified Entry view renders. Username gets2340  // populated from the passkey's userName so the entry is searchable in the2341  // existing autofill flow without a separate code path.2342  if (isPasskeyType) base.type = "login";2343  if (!base.username) base.username = flat.userName;2344  if (!base.url) base.url = `https://${flat.rpId}`;2345  return base;2346}234723482349// ---------------------------------------------------------------------------2350// Deduplication2351// ---------------------------------------------------------------------------23522353function entryDupeKey(e: VaultEntry): string {2354  if (e.type === "note") return `note|${e.title.toLowerCase()}`;2355  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins2356  // on different sites that happen to share a username are still distinct;2357  // two passkey credentials for the same RP+user are duplicates regardless2358  // of any login fields they may also carry.2359  if (e.passkey) {2360    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;2361  }2362  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;2363}23642365/** Returns the number of duplicate entries (those that would be removed). */2366export function countDuplicates(): number {2367  const best = new Map<string, VaultEntry>();2368  let dupeCount = 0;2369  for (const e of entries.filter((e) => !e.deletedAt)) {2370    const key = entryDupeKey(e);2371    const existing = best.get(key);2372    if (!existing) {2373      best.set(key, e);2374    } else {2375      dupeCount++;2376      // Keep the more recently modified one in the map2377      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2378        best.set(key, e);2379      }2380    }2381  }2382  return dupeCount;2383}23842385/**2386 * Soft-deletes duplicate entries, keeping the most recently modified copy of2387 * each group. Returns the number of entries moved to trash.2388 */2389export async function deduplicateEntries(): Promise<number> {2390  if (!derivedKey) throw new Error("vault_locked");2391  // Dedup reshapes slot B permanently under the decoy key, which is2392  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.2393  if (vaultMode === "decoy") {2394    throw new Error("deduplicate disabled in decoy mode");2395  }23962397  const best = new Map<string, VaultEntry>();2398  for (const e of entries.filter((en) => !en.deletedAt)) {2399    const key = entryDupeKey(e);2400    const existing = best.get(key);2401    if (!existing) {2402      best.set(key, e);2403    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2404      best.set(key, e);2405    }2406  }24072408  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));2409  let removed = 0;2410  entries = entries.map((e) => {2411    if (!e.deletedAt && !keepIds.has(e.id)) {2412      removed++;2413      return { ...e, deletedAt: monotonicEntryTimestamp(e.modifiedAt) };2414    }2415    return e;2416  });24172418  if (removed > 0) await persistEntries();2419  return removed;2420}24212422// ---------------------------------------------------------------------------2423// Vault history — snapshot BEFORE every vault write so the saved blob is2424// what is about to be lost, not what was just written. Capped at 5. The2425// stored blobs are AES-256-GCM ciphertext under the master password, so2426// history adds zero security surface.2427// ---------------------------------------------------------------------------24282429const HISTORY_KEY = "peach_vault_history";2430const HISTORY_MAX = 5;24312432/**2433 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE2434 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost2435 * state. Identical-to-most-recent blobs are skipped to avoid filling history2436 * with no-op writes.2437 */2438export async function takeVaultSnapshot(2439  reason: VaultSnapshot["reason"],2440): Promise<void> {2441  try {2442    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);2443    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];2444    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];24452446    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,2447    // pre-createVault). createVault skips snapshots intentionally — there is2448    // no prior state to preserve.2449    if (!slotA) return;24502451    const vault_a = JSON.stringify(slotA);2452    const vault_b = JSON.stringify(slotB ?? slotA);24532454    // Best-effort entry count: only meaningful when the in-memory state is2455    // unlocked AND in real mode. Decoy mode would leak the decoy entry2456    // count; locked mode has no entries to count. Both fall through to null.2457    let entryCount: number | null = null;2458    if (derivedKey && vaultMode === "real") {2459      try {2460        entryCount = entries.filter((e) => !e.deletedAt).length;2461      } catch {2462        entryCount = null;2463      }2464    }24652466    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2467    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)2468      ? existingRaw2469      : { snapshots: [] };24702471    // Deduplication: if the most recent snapshot has byte-identical blobs,2472    // skip — saves the same vault N times in a row would otherwise burn the2473    // entire history window with no recovery value.2474    const latest = history.snapshots[0];2475    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {2476      return;2477    }24782479    const snapshot: VaultSnapshot = {2480      id: crypto.randomUUID(),2481      savedAt: Date.now(),2482      reason,2483      vault_a,2484      vault_b,2485      entryCount,2486    };24872488    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);2489    await chrome.storage.local.set({ [HISTORY_KEY]: history });2490  } catch (err) {2491    // Non-fatal — history must never block a save. A failed snapshot just2492    // means the user loses one rung of the safety ladder for this write.2493    // M4/QUA-006: log so the failure is visible; if it looks like a quota2494    // problem, notify the popup so the user can prune history themselves.2495    console.warn('[Peach] takeVaultSnapshot failed:', err);2496    if (String(err).includes('QUOTA') || String(err).includes('quota')) {2497      try {2498        await chrome.runtime.sendMessage({ type: 'VAULT_STORAGE_QUOTA_WARNING' });2499      } catch {2500        // popup may be closed; surfaced via console.warn above2501      }2502    }2503  }2504}25052506/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */2507export async function clearVaultHistory(): Promise<void> {2508  try {2509    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });2510  } catch {2511    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().2512  }2513}25142515/** Read all snapshots, newest first, for the Settings vault history view. */2516export async function getVaultHistory(): Promise<VaultSnapshot[]> {2517  const raw = await chrome.storage.local.get(HISTORY_KEY);2518  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2519  if (!history || !Array.isArray(history.snapshots)) return [];2520  return history.snapshots;2521}25222523/**2524 * Replace the on-disk vault with the chosen snapshot's blobs and lock the2525 * vault. The current state is itself snapshotted with reason 'restore' first2526 * so the restore is reversible. After this returns, the user must unlock2527 * with the password that matches the restored blob.2528 */2529export async function restoreVaultSnapshot(2530  snapshotId: string,2531): Promise<{ ok: true } | { ok: false; error: string }> {2532  const history = await getVaultHistory();2533  const snapshot = history.find((s) => s.id === snapshotId);2534  if (!snapshot) return { ok: false, error: "snapshot_not_found" };25352536  // Snapshot the about-to-be-replaced current state so the restore is2537  // reversible. takeVaultSnapshot is no-op when the most recent entry is2538  // byte-identical, so a restore-of-a-restore stays clean.2539  await takeVaultSnapshot("restore");25402541  let restoredA: StoredVault;2542  let restoredB: StoredVault;2543  try {2544    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;2545    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;2546  } catch {2547    return { ok: false, error: "snapshot_corrupt" };2548  }25492550  await writeSlot(SLOT_A, restoredA);2551  await writeSlot(SLOT_B, restoredB);2552  await stampLocalVaultMutation();25532554  // Lock so the user must prove they can decrypt the restored blob. If they2555  // restored a pre-password-change snapshot they discover it immediately at2556  // the lock screen rather than silently holding an inaccessible vault.2557  lock();25582559  return { ok: true };2560}25612562/**2563 * Change the master password. Re-encrypts slot A with a fresh salt + new2564 * derived key. Slot B (duress or noise) is left untouched when duress is2565 * configured — it uses an independent key derived from the duress password —2566 * and regenerated as noise when duress is not configured.2567 */2568export async function changePassword(2569  currentPassword: string,2570  newPassword: string2571): Promise<boolean> {2572  if (!derivedKey || vaultMode !== "real") return false;25732574  // Verify current password before allowing any change2575  const valid = await verifyMainPassword(currentPassword);2576  if (!valid) return false;25772578  const slotA = await loadSlot(SLOT_A);2579  if (!slotA) return false;2580  if (!rootKey) return false;25812582  // Derive fresh key from new password + new random salt2583  const newSalt = generateSalt();2584  const newKey = await deriveKey(newPassword, newSalt);25852586  // Re-encrypt the in-memory entries with the new key2587  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);2588  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");25892590  // Pre-wrap root key under the new derived key. Recovery fields wrap rootKey2591  // under recovery words and remain untouched — recovery phrase keeps working2592  // across password changes (H4/COR-002).2593  const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);25942595  const finalSlotA: StoredVault = {2596    ...slotA,2597    salt: hex(newSalt),2598    nonce,2599    ciphertext,2600    tag,2601    ...pwRoot,2602  };26032604  // Snapshot the OLD-key blob before overwriting it. If the user later2605  // restores from this snapshot they will need the OLD password — the2606  // post-restore lock surfaces that immediately.2607  await takeVaultSnapshot("local_save");26082609  // Single atomic write: ciphertext, salt, and pw_root_* fields all land in2610  // one storage transaction. An SW kill before this point leaves the old2611  // slot intact; after this point everything tied to the new password is2612  // already on disk together (M6/COR-004).2613  await writeSlot(SLOT_A, finalSlotA);26142615  // Swap the in-memory key synchronously before any further await. If we2616  // threw between the Slot A write and this swap, the next persistEntries()2617  // would overwrite Slot A with old-key ciphertext, locking the user out.2618  zeroKey(derivedKey);2619  derivedKey = newKey;26202621  // Regenerate noise for slot B when duress is not in use. Runs after the2622  // key swap so a failure here is a disposable noise regeneration, not a2623  // lockout.2624  if (!duressConfigured) {2625    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2626  }2627  await stampLocalVaultMutation();26282629  return true;2630}