vault.ts

75.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 {15  rootKeyToWords,16  wordsToRootKeyMaterial,17  deriveRecoveryKey,18} from "../shared/recovery-words";19import type {20  AccentColorName,21  LegacyVaultEntry,22  ManualSummonRecord,23  SavedIdentity,24  StoredVault,25  VaultEntry,26  VaultHistory,27  VaultSnapshot,28} from "../shared/types";29import { decrypt, encrypt, zeroKey } from "./aes";30import { clearAutoLock, touchAutoLock } from "./alarms";3132type EntryDraft = Omit<33  VaultEntry,34  "id" | "createdAt" | "modifiedAt" | "deletedAt"35>;3637// Slot-neutral storage keys (v1.4.24+). Both slots always exist; slot B38// either holds the decoy vault (when duress is configured) or random noise39// that looks identical at rest. No external indicator is stored — duress40// configuration is recorded only inside the encrypted payload of slot A.41const SLOT_A = "peach_vault_a";42const SLOT_B = "peach_vault_b";4344// Legacy keys, migrated away on first access.45const LEGACY_MAIN = "peach_vault";46const LEGACY_DECOY = "peach_decoy_vault";47const LEGACY_DURESS_FLAG = "peach_duress";4849// Plaintext is padded to the next multiple of this size before encryption.50// This coarsens ciphertext-length leakage into 1 KiB buckets.51const PAD_BLOCK = 1024;5253let derivedKey: Uint8Array | null = null;54// 32-byte random root key, separate from the derivedKey used for vault encryption.55// The root key is wrapped (encrypted) under the recovery key derived from the56// 12-word phrase. When the user changes their password, only the wrapping changes;57// the root key and recovery words remain stable.58let rootKey: Uint8Array | null = null;59let entries: VaultEntry[] = [];60let domainIndex: Map<string, VaultEntry[]> = new Map();61let vaultMode: "real" | "decoy" = "real";62// Cached post-unlock view of whether the real vault records that duress is63// configured. Only meaningful after a real-mode unlock; for decoy-mode unlock64// this stays false because we never see slot A's plaintext.65let duressConfigured = false;6667let migrationRan = false;6869// ---------------------------------------------------------------------------70// Settings (persisted inside the encrypted vault store as a sidecar key)71// ---------------------------------------------------------------------------7273export type DisplayFont = "cormorant-italic" | "cormorant" | "inter" | "system";74export type TextSize = "small" | "medium" | "large";7576export interface VaultSettings {77  accentColor?: AccentColorName;78  theme?: "dark" | "light";79  autoLockMinutes?: number;80  clipboardClearSeconds?: number;81  lockOnClose?: boolean;82  breachMonitoring?: boolean;83  passkeyProviderEnabled?: boolean;84  trashRetentionDays?: number;85  manualSummonDomains?: Record<string, ManualSummonRecord>;86  declinedCardFingerprints?: string[];87  /**88   * Display font for headlines, brand wordmark, and narrative copy. Default89   * is "cormorant-italic" — the bold italic serif that ships as Peach's90   * voice. Non-italic options exist for users who prefer upright type.91   */92  displayFont?: DisplayFont;93  /**94   * Text size scale for body copy and entry titles. Stops at "large" by95   * design — XL clips dense screens (Vault, Health) inside the fixed96   * 330×480 popup surface.97   */98  textSize?: TextSize;99  /**100   * When true, all entrance/exit transitions and ambient animations are101   * collapsed to instant. Honoured alongside the user's OS-level102   * `prefers-reduced-motion`.103   */104  reduceMotion?: boolean;105  /**106   * Android-only: when true, the master key is escrowed in AndroidKeyStore107   * and the lock screen offers biometric unlock. Toggled from Settings →108   * Security → Biometrics. Has no effect on desktop builds.109   */110  biometricsEnabled?: boolean;111  /**112   * User overrides for keyboard shortcuts. Keys are ShortcutAction names113   * (see src/shared/shortcuts.ts). Missing actions fall through to114   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the115   * canonical lookup. Storing only the deltas keeps the vault payload116   * small and lets default updates land for users who never rebind.117   */118  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;119}120121let settings: VaultSettings = {};122123export function getSettings(): VaultSettings {124  return settings;125}126127export async function ensureSettingsLoaded(): Promise<VaultSettings> {128  await loadSettings();129  return settings;130}131132export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {133  await loadSettings();134  settings = { ...settings, ...patch };135  await persistSettings();136}137138async function persistSettings(): Promise<void> {139  try {140    await chrome.storage.local.set({ peach_settings: settings });141  } catch (err) {142    throw mapStorageError(err);143  }144}145146function mapStorageError(err: unknown): Error {147  const message = err instanceof Error ? err.message : String(err);148  if (/QUOTA_BYTES|quota/i.test(message)) {149    return new Error("STORAGE_QUOTA_EXCEEDED");150  }151  return err instanceof Error ? err : new Error(message);152}153154async function loadSettings(): Promise<void> {155  const result = await chrome.storage.local.get("peach_settings");156  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};157}158159// ---------------------------------------------------------------------------160// HIBP breach check (k-anonymity model)161// ---------------------------------------------------------------------------162163export async function checkBreach(password: string): Promise<number> {164  const encoder = new TextEncoder();165  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));166  const hashHex = Array.from(new Uint8Array(hashBuffer))167    .map((b) => b.toString(16).padStart(2, "0"))168    .join("")169    .toUpperCase();170171  const prefix = hashHex.slice(0, 5);172  const suffix = hashHex.slice(5);173174  try {175    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);176    if (!res.ok) return 0;177    const text = await res.text();178    const match = text.split("\r\n").find((l) => l.startsWith(suffix));179    return match ? parseInt(match.split(":")[1], 10) : 0;180  } catch {181    return 0; // Network unavailable — don't block182  }183}184185export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {186  const fingerprint = await cardFingerprint(last4, expMonth, expYear);187  await loadSettings();188  settings = {189    ...settings,190    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],191  };192  await persistSettings();193}194195export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {196  await loadSettings();197  const fingerprint = await cardFingerprint(last4, expMonth, expYear);198  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);199}200201async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {202  const encoder = new TextEncoder();203  const buf = await crypto.subtle.digest("SHA-1", encoder.encode(`${last4}${expMonth}${expYear}`));204  return Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, "0")).join("");205}206207function hex(buf: Uint8Array): string {208  return Array.from(buf)209    .map((b) => b.toString(16).padStart(2, "0"))210    .join("");211}212213function unhex(s: string): Uint8Array {214  const out = new Uint8Array(s.length / 2);215  for (let i = 0; i < s.length; i += 2) {216    out[i / 2] = parseInt(s.slice(i, i + 2), 16);217  }218  return out;219}220221// ---------------------------------------------------------------------------222// Root key generation and recovery word wrapping223// ---------------------------------------------------------------------------224225/**226 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key227 * derived from the recovery word material via HKDF-SHA256. The wrapped blob228 * fields are stored in slot A alongside the password-wrapped ciphertext. The229 * root key itself is kept in memory for the session so getRecoveryWords() can230 * encode it without re-deriving.231 */232async function generateAndWrapRootKey(): Promise<{233  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey234  recoverySalt: string;235  wrappedRootKey: string;236  wrappedNonce: string;237  wrappedTag: string;238}> {239  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));240241  // Derive the recovery key from the BIP39 words encoding of the root key.242  const words = await rootKeyToWords(rootKeyBytes);243  const wordMaterial = await wordsToRootKeyMaterial(words);244  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));245  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);246247  // Wrap the root key with the recovery key using AES-256-GCM directly.248  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.249  const nonce = crypto.getRandomValues(new Uint8Array(12));250  const encoded = new TextEncoder().encode(hex(rootKeyBytes));251  const raw = await crypto.subtle.encrypt(252    {253      name: "AES-GCM",254      iv: nonce,255      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,256      tagLength: 128,257    },258    recoveryKey,259    encoded260  );261  // WebCrypto returns ciphertext || auth tag concatenated; split them.262  const rawU8 = new Uint8Array(raw);263  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));264  const tag = hex(rawU8.slice(rawU8.length - 16));265266  return {267    rawRootKey: hex(rootKeyBytes),268    recoverySalt: hex(recoverySalt),269    wrappedRootKey: ciphertext,270    wrappedNonce: hex(nonce),271    wrappedTag: tag,272  };273}274275/**276 * Unwrap the root key using recovery words. Called during recovery flow when277 * the user provides their 12-word phrase and a new password.278 * Exported for use by the RECOVER_WITH_WORDS message handler.279 */280export async function unwrapRootKeyForRecovery(281  wrappedRootKey: string,282  wrappedNonce: string,283  wrappedTag: string,284  recoverySaltHex: string,285  words: string[]286): Promise<void> {287  const wordMaterial = await wordsToRootKeyMaterial(words);288  const recoverySalt = unhex(recoverySaltHex);289  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);290291  // Decrypt and cache the root key. The root key bytes were hex-encoded before292  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt293  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.294  const combined = unhex(wrappedRootKey + wrappedNonce + wrappedTag);295  const nonceU8 = combined.slice(0, 12);296  const ciphertext = combined.slice(12, combined.length - 16);297  const storedTag = combined.slice(combined.length - 16);298  const raw = await crypto.subtle.decrypt(299    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },300    recoveryKey,301    new Uint8Array([...ciphertext, ...storedTag])302  );303  const decryptedHex = new TextDecoder().decode(raw);304  rootKey = unhex(decryptedHex);305}306307/**308 * Re-wrap the cached root key under a new derived key (from the user's new309 * password). Called after password change and after recovery re-key so the310 * same recovery words continue to work with the updated password.311 */312export async function rewrapRootKeyForPassword(313  newDerivedKey: Uint8Array,314  _newSalt: Uint8Array315): Promise<void> {316  if (!rootKey) throw new Error("root key not set");317  const { nonce, ciphertext, tag } = await encrypt(318    newDerivedKey,319    hex(rootKey)320  );321  const slotA = await loadSlot(SLOT_A);322  if (!slotA) throw new Error("missing slot A");323  await writeSlot(SLOT_A, {324    ...slotA,325    recovery_wrapped_root_key: ciphertext,326    recovery_nonce: nonce,327    recovery_tag: tag,328    // recovery_salt does NOT change when password changes — the recovery329    // words always derive the same key; only the wrapping key changes.330  });331}332333/**334 * Change the vault encryption to a new derived key (already derived from335 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.336 * Caller is responsible for key swap and snapshot.337 */338async function reencryptVaultForNewKey(339  newKey: Uint8Array,340  slotA: StoredVault341): Promise<void> {342  const plaintext = encodePayload(entries, duressConfigured);343  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);344  await writeSlot(SLOT_A, {345    ...slotA,346    nonce,347    ciphertext,348    tag,349  });350}351352/**353 * Full password change with an already-derived key (used after recovery).354 * Unlike changePassword(), this skips re-verification of current password355 * since recovery flow has already validated the user's identity via words.356 */357export async function changePasswordWithNewKey(358  newSalt: Uint8Array,359  newKey: Uint8Array360): Promise<void> {361  const slotA = await loadSlot(SLOT_A);362  if (!slotA) throw new Error("missing slot A");363  await reencryptVaultForNewKey(newKey, slotA);364  zeroKey(derivedKey!);365  derivedKey = newKey;366  if (!duressConfigured) {367    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));368  }369}370371// ---------------------------------------------------------------------------372// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive373// a new key after password reset without re-importing the crypto module.374// ---------------------------------------------------------------------------375376export { deriveKey, generateSalt };377378export async function loadSlotA(): Promise<StoredVault | null> {379  return loadSlot(SLOT_A);380}381382/**383 * Re-derive the session root key from the already-derived AES key (keyA from384 * unlock) and the recovery salt stored in slot A. This is the inverse of385 * generateAndWrapRootKey() — both operations use the same HKDF with the same386 * salt and info string, so they cancel out.387 *388 * The derivedKey and rootKey are related by:389 *   derivedKey = Argon2id(password, slotA.salt)   → AES key for vault payload390 *   rootKey    = HKDF-SHA256(recovery_words, recovery_salt)  → wrapped by HKDF391 *392 * Since we only store the wrapped root key and the recovery salt, we can393 * only re-derive rootKey during an active (unlocked) session where we have394 * the derivedKey already. On lock, rootKey is zeroed.395 *396 * For pre-v2.12 vaults that have no recovery_salt, this returns null and397 * the caller must invoke migrateToRecovery() to generate the recovery escrow.398 */399async function deriveRootKeyFromDerivedKey(400  _derivedKey: Uint8Array,401  _recoverySaltHex: string402): Promise<Uint8Array | null> {403  // The root key is stored wrapped under the recovery key derived from the404  // 12-word phrase. It cannot be re-derived from derivedKey alone — that would405  // require knowing the original recovery words. The in-memory rootKey is set406  // during unlock if recovery data is present.407  // This function exists as a potential future path if we ever store an408  // additional derived-key → root-key relationship. Currently rootKey is409  // cached directly during unlock.410  return null;411}412413/**414 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),415 * generate the root key, wrap it under the recovery words, and persist the416 * recovery escrow into slot A. The caller must show the recovery words to the417 * user before calling this — once called, those words are the vault's recovery418 * path. This runs once, on the first unlock of an upgraded vault.419 */420export async function migrateToRecovery(): Promise<void> {421  if (!derivedKey || vaultMode !== "real") {422    throw new Error("vault must be unlocked in real mode");423  }424  const slotA = await loadSlot(SLOT_A);425  if (!slotA) throw new Error("missing slot A");426  if (slotA.recovery_salt && slotA.recovery_wrapped_root_key) return; // already done427428  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =429    await generateAndWrapRootKey();430431  rootKey = unhex(rawRootKey);432433  const nextSlotA: StoredVault = {434    ...slotA,435    recovery_salt: recoverySalt,436    recovery_wrapped_root_key: wrappedRootKey,437    recovery_nonce: wrappedNonce,438    recovery_tag: wrappedTag,439  };440  await writeSlot(SLOT_A, nextSlotA);441}442443/**444 * Returns the 12 recovery words for the current session's root key.445 * Must be called while the vault is unlocked. Throws if recovery has not446 * been set up (pre-v2.12 vault that hasn't migrated yet).447 */448export async function getRecoveryWords(): Promise<string[]> {449  if (!rootKey) {450    throw new Error("recovery not set up — vault may need migration");451  }452  return rootKeyToWords(rootKey);453}454455async function loadSlot(key: string): Promise<StoredVault | null> {456  const result = await chrome.storage.local.get(key);457  return (result as Record<string, StoredVault | undefined>)[key] ?? null;458}459460async function writeSlot(key: string, vault: StoredVault): Promise<void> {461  try {462    await chrome.storage.local.set({ [key]: vault });463  } catch (err) {464    throw mapStorageError(err);465  }466}467468/**469 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault470 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +471 * peach_vault_b). Runs at most once per session; safe to call repeatedly.472 */473async function ensureMigrated(): Promise<void> {474  if (migrationRan) return;475  migrationRan = true;476477  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];478  const raw = await chrome.storage.local.get(keys);479  const slotAExists = raw[SLOT_A] !== undefined;480  const slotBExists = raw[SLOT_B] !== undefined;481  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;482  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;483484  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.485  if (!slotAExists && !legacyMain) return;486487  // Already migrated.488  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;489490  if (legacyMain && !slotAExists) {491    await writeSlot(SLOT_A, legacyMain);492  }493494  // If slot B isn't set, populate it: use legacy decoy when present, else noise.495  if (!slotBExists) {496    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;497    if (legacyDecoy) {498      await writeSlot(SLOT_B, legacyDecoy);499    } else if (target) {500      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));501    }502  }503504  const toRemove: string[] = [];505  if (legacyMain) toRemove.push(LEGACY_MAIN);506  if (legacyDecoy) toRemove.push(LEGACY_DECOY);507  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);508  if (toRemove.length > 0) {509    await chrome.storage.local.remove(toRemove);510  }511}512513// ---------------------------------------------------------------------------514// Padding + noise helpers515// ---------------------------------------------------------------------------516517/**518 * Pads the plaintext string with trailing spaces so that the encoded byte519 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing520 * whitespace after a complete document, so the padding is transparent on521 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.522 */523function padPlaintext(json: string): string {524  const encoded = new TextEncoder().encode(json);525  const target = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);526  const padLen = target - encoded.length;527  return json + " ".repeat(padLen);528}529530/**531 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The532 * ciphertext length matches the given byte count. Because GCM ciphertext is533 * pseudo-random, this blob is indistinguishable at rest from a real534 * encrypted vault without the key.535 *536 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto537 * spec hard limit). Chunk the fill so vaults larger than that — common once538 * the user has dozens of entries or any passkey — don't throw.539 */540const CSPRNG_CHUNK = 65_536;541542function fillRandom(buf: Uint8Array): Uint8Array {543  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {544    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);545    const chunk = new Uint8Array(end - offset);546    crypto.getRandomValues(chunk);547    buf.set(chunk, offset);548  }549  return buf;550}551552function generateNoiseBlob(ciphertextBytes: number): StoredVault {553  const salt = crypto.getRandomValues(new Uint8Array(16));554  const nonce = crypto.getRandomValues(new Uint8Array(12));555  const cipher = fillRandom(new Uint8Array(ciphertextBytes));556  const tag = crypto.getRandomValues(new Uint8Array(16));557  return {558    version: 1,559    salt: hex(salt),560    nonce: hex(nonce),561    ciphertext: hex(cipher),562    tag: hex(tag),563  };564}565566// ---------------------------------------------------------------------------567// Encrypted plaintext envelope568// ---------------------------------------------------------------------------569570type VaultPayload = {571  entries: VaultEntry[];572  duressConfigured?: boolean;573};574575type DecryptedPayload = {576  entries: VaultEntry[];577  duressConfigured: boolean;578  /**579   * True when at least one entry in the on-disk payload had a legacy shape580   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)581   * and got rewritten by normalizeEntry. The unlock path uses this to582   * persist the new shape immediately — without it, the migration would583   * re-run on every unlock and old shape never disappears from disk.584   */585  migratedFromLegacy: boolean;586};587588function encodePayload(entries: VaultEntry[], duress: boolean): string {589  const envelope: VaultPayload = { entries, duressConfigured: duress };590  return padPlaintext(JSON.stringify(envelope));591}592593function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {594  if ("created" in raw || "updated" in raw) return true;595  const flat = raw as Partial<VaultEntry>;596  if (flat.type === "passkey") return true;597  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.598  if (flat.rpId !== undefined && flat.passkey === undefined) return true;599  return false;600}601602function decodePayload(plain: string): DecryptedPayload {603  const parsed = JSON.parse(plain) as604    | Array<VaultEntry | LegacyVaultEntry>605    | VaultPayload;606607  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];608  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);609610  const normalized = rawEntries.map(normalizeEntry);611  const { merged } = mergePasskeyOnlyIntoLogins(normalized);612613  if (Array.isArray(parsed)) {614    return {615      entries: normalized,616      duressConfigured: false,617      migratedFromLegacy: true, // pre-A-2 array shape always needs save618    };619  }620621  return {622    entries: normalized,623    duressConfigured: parsed.duressConfigured === true,624    migratedFromLegacy: flatMigrationNeeded || merged,625  };626}627628/**629 * One-time consolidation pass run alongside the legacy passkey migration.630 *631 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`632 * entry per registration — so users who tested registration multiple times633 * accumulated several passkey-bearing entries for the same RP, and the634 * migration to the unified shape carried that duplication forward. This635 * pass collapses the duplicates:636 *637 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.638 *   2. Within each group, the most recently-active credential wins (prefer639 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).640 *   3. If a NON-passkey login entry exists for that RP (matched by url or641 *      additionalUrls hostname), attach the winning passkey to it and642 *      soft-delete every passkey-bearing entry in the group.643 *   4. Otherwise, soft-delete every loser in the group, keeping the winner644 *      as a passkey-only entry.645 *646 * Soft delete (deletedAt) rather than splice — the trash flow keeps the647 * record around for retention and the user can restore if the heuristic648 * picked wrong. The `merged` flag goes back to decodePayload so the649 * unlock path persists the consolidated shape immediately.650 */651function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {652  let merged = false;653  const now = Date.now();654655  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);656  const byRpId = new Map<string, VaultEntry[]>();657  for (const e of candidates) {658    const rpId = e.passkey!.rpId;659    const list = byRpId.get(rpId);660    if (list) list.push(e);661    else byRpId.set(rpId, [e]);662  }663664  for (const [rpId, group] of byRpId) {665    if (group.length === 0) continue;666    group.sort((a, b) => {667      const aT =668        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;669      const bT =670        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;671      return bT - aT;672    });673    const winner = group[0]!;674675    // Look for a NON-passkey login entry matching this RP. Use the URL676    // hostname rather than a substring search so a "login.foo.com" entry677    // doesn't accidentally absorb a passkey for "foo.com".678    const loginTarget = entries.find((e) => {679      if (e.deletedAt || e.passkey || e.type !== "login") return false;680      if (e.url && extractDomain(e.url) === rpId) return true;681      for (const extra of e.additionalUrls ?? []) {682        if (extractDomain(extra) === rpId) return true;683      }684      return false;685    });686687    if (loginTarget) {688      loginTarget.passkey = winner.passkey;689      loginTarget.modifiedAt = now;690      for (const dup of group) {691        if (!dup.deletedAt) {692          dup.deletedAt = now;693          dup.modifiedAt = now;694        }695      }696      merged = true;697    } else if (group.length > 1) {698      for (let i = 1; i < group.length; i += 1) {699        const dup = group[i]!;700        if (!dup.deletedAt) {701          dup.deletedAt = now;702          dup.modifiedAt = now;703        }704      }705      merged = true;706    }707  }708709  return { merged };710}711712// Exported solely for `unlockFromCodex` below, which needs to verify a713// supplied passphrase against a slotA blob *without* committing anything to714// storage. Keep this internal otherwise — callers crossing the messages.ts715// boundary should always go through `unlock` / `verifyMainPassword` so the716// constant-time invariants in Projects.md §3.4 are not bypassed.717export async function tryDecryptSlot(718  vault: StoredVault,719  key: Uint8Array720): Promise<DecryptedPayload | null> {721  try {722    const plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag);723    return decodePayload(plain);724  } catch {725    return null;726  }727}728729// ---------------------------------------------------------------------------730// Core operations731// ---------------------------------------------------------------------------732733function buildDomainIndexSync(all: VaultEntry[]): void {734  domainIndex = new Map();735736  for (const entry of all) {737    if (entry.deletedAt) continue;738    if (entry.type === "note" || entry.type === "card") continue;739740    // An entry can match multiple sites via `additionalUrls`. Index each741    // hostname so `queryDomain` returns the entry regardless of which alias742    // the user is currently visiting. Dedupe per-entry so a single entry743    // never appears twice in one bucket.744    const domains = new Set<string>();745    const primary = extractDomain(entry.url ?? "");746    if (primary) domains.add(primary);747    for (const extra of entry.additionalUrls ?? []) {748      const d = extractDomain(extra);749      if (d) domains.add(d);750    }751    if (domains.size === 0) continue;752753    for (const domain of domains) {754      const bucket = domainIndex.get(domain) ?? [];755      bucket.push(entry);756      domainIndex.set(domain, bucket);757    }758  }759}760761export async function createVault(password: string): Promise<void> {762  await ensureMigrated();763764  const salt = generateSalt();765  derivedKey = await deriveKey(password, salt);766  entries = [];767  vaultMode = "real";768  duressConfigured = false;769  buildDomainIndexSync(entries);770771  // Generate and wrap the root key for recovery word escrow.772  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =773    await generateAndWrapRootKey();774  rootKey = unhex(rawRootKey);775776  const plaintext = encodePayload(entries, duressConfigured);777  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);778779  const slotA: StoredVault = {780    version: 1,781    salt: hex(salt),782    nonce,783    ciphertext,784    tag,785    recovery_salt: recoverySalt,786    recovery_wrapped_root_key: wrappedRootKey,787    recovery_nonce: wrappedNonce,788    recovery_tag: wrappedTag,789  };790  await writeSlot(SLOT_A, slotA);791792  // Matching-size noise blob for slot B, so a fresh install shows two793  // similarly-shaped records regardless of whether duress is ever set up.794  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));795796  touchAutoLock();797}798799export async function hasVault(): Promise<boolean> {800  await ensureMigrated();801  return (await loadSlot(SLOT_A)) !== null;802}803804/**805 * Returns whether the real vault has recorded that a duress passphrase is806 * configured. Reliable only after a real-mode unlock — before unlock or in807 * decoy mode, returns false because we cannot read slot A's plaintext.808 */809export async function isDuressEnabled(): Promise<boolean> {810  return vaultMode === "real" && duressConfigured;811}812813export async function setupDuress(duressPassword: string): Promise<void> {814  if (!derivedKey || vaultMode !== "real") {815    throw new Error("vault must be unlocked in real mode");816  }817  await ensureMigrated();818819  // Encrypt the decoy payload (empty entries) under the duress key. Pad to820  // at least the current slot A ciphertext size so the two slots look the821  // same at rest at setup time.822  const slotA = await loadSlot(SLOT_A);823  if (!slotA) throw new Error("missing slot A");824825  const decoySalt = generateSalt();826  const decoyKey = await deriveKey(duressPassword, decoySalt);827  try {828    // Pad the decoy plaintext to match slot A's ciphertext length (minus the829    // 16-byte tag) so encrypted sizes align exactly.830    const targetBytes = slotA.ciphertext.length / 2;831    let plaintext = encodePayload([], false);832    const encoded = new TextEncoder().encode(plaintext);833    if (encoded.length < targetBytes) {834      plaintext = plaintext + " ".repeat(targetBytes - encoded.length);835    }836    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext);837    await writeSlot(SLOT_B, {838      version: 1,839      salt: hex(decoySalt),840      nonce,841      ciphertext,842      tag,843    });844  } finally {845    zeroKey(decoyKey);846  }847848  // Re-encrypt slot A with the duressConfigured flag set to true. No external849  // flag is written — the bit lives inside the encrypted envelope.850  duressConfigured = true;851  await persistSlotA();852}853854export async function disableDuress(): Promise<void> {855  if (!derivedKey || vaultMode !== "real") {856    throw new Error("vault must be unlocked in real mode");857  }858  await ensureMigrated();859860  // Overwrite slot B with fresh noise that matches slot A's size. The slot861  // remains occupied so its mere existence does not signal that duress was862  // ever configured.863  const slotA = await loadSlot(SLOT_A);864  if (slotA) {865    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));866  }867868  duressConfigured = false;869  await persistSlotA();870}871872export async function verifyMainPassword(password: string): Promise<boolean> {873  await ensureMigrated();874  const slotA = await loadSlot(SLOT_A);875  if (!slotA) return false;876  const key = await deriveKey(password, unhex(slotA.salt));877  try {878    return (await tryDecryptSlot(slotA, key)) !== null;879  } finally {880    zeroKey(key);881  }882}883884export async function unlock(password: string): Promise<boolean> {885  await ensureMigrated();886887  // Load both slots in parallel. Both always exist post-migration; the888  // parallel load and the parallel derivation below keep wall-clock time889  // constant regardless of which slot's passphrase (real or decoy) the890  // caller supplied, so a timing side channel cannot distinguish the891  // duress path from the real-unlock path (DUR-002).892  const [slotA, slotB] = await Promise.all([893    loadSlot(SLOT_A),894    loadSlot(SLOT_B),895  ]);896897  // Derive both keys in parallel, unconditionally.898  const [keyA, keyB] = await Promise.all([899    slotA ? deriveKey(password, unhex(slotA.salt)) : Promise.resolve(null),900    slotB ? deriveKey(password, unhex(slotB.salt)) : Promise.resolve(null),901  ]);902903  // Attempt both decrypts in parallel.904  const [payloadA, payloadB] = await Promise.all([905    keyA && slotA ? tryDecryptSlot(slotA, keyA) : Promise.resolve(null),906    keyB && slotB ? tryDecryptSlot(slotB, keyB) : Promise.resolve(null),907  ]);908909  if (payloadA && keyA) {910    derivedKey = keyA;911    vaultMode = "real";912    entries = payloadA.entries;913    duressConfigured = payloadA.duressConfigured;914    buildDomainIndexSync(entries);915    await loadSettings();916    touchAutoLock();917    if (keyB) zeroKey(keyB);918919    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).920    if (slotA) {921      const needsRecoverySetup =922        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;923      if (!needsRecoverySetup) {924        rootKey = await deriveRootKeyFromDerivedKey(925          keyA,926          slotA.recovery_salt!927        );928      }929    }930931    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat932    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so933    // the next save uses the new format and migration doesn't re-run forever.934    if (payloadA.migratedFromLegacy) {935      await persistSlotA();936    }937    return true;938  }939940  if (payloadB && keyB) {941    derivedKey = keyB;942    vaultMode = "decoy";943    entries = payloadB.entries;944    duressConfigured = false;945    buildDomainIndexSync(entries);946    await loadSettings();947    touchAutoLock();948    if (keyA) zeroKey(keyA);949    return true;950  }951952  if (keyA) zeroKey(keyA);953  if (keyB) zeroKey(keyB);954  return false;955}956957export function lock(): void {958  if (derivedKey) {959    zeroKey(derivedKey);960    derivedKey = null;961  }962  if (rootKey) {963    rootKey.fill(0);964    rootKey = null;965  }966967  entries = [];968  domainIndex = new Map();969  vaultMode = "real";970  duressConfigured = false;971  clearAutoLock();972}973974export function isUnlocked(): boolean {975  return derivedKey !== null;976}977978// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).979// Used by Android biometric unlock — the key was derived on the native side.980export async function unlockWithKey(981  masterKeyHex: string,982): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {983  try {984    const masterKeyBytes = unhex(masterKeyHex);985    const stored = await loadSlot(SLOT_A);986    if (!stored) return { ok: false, error: "invalid_key" };987    const payload = await tryDecryptSlot(stored, masterKeyBytes);988    if (!payload) return { ok: false, error: "invalid_key" };989990    derivedKey = masterKeyBytes;991    vaultMode = "real";992    entries = payload.entries;993    duressConfigured = payload.duressConfigured;994    buildDomainIndexSync(entries);995    await loadSettings();996    touchAutoLock();997    return { ok: true, entries };998  } catch {999    return { ok: false, error: "invalid_key" };1000  }1001}10021003/**1004 * Restore the vault from a parsed Peach Codex payload.1005 *1006 * The supplied passphrase is verified against the Codex's slotA *before* any1007 * storage mutation — wrong passphrases return without touching disk. On1008 * success, the current vault state is snapshotted (reason: "restore") so the1009 * user can revert via Settings -> Vault History, then slotA/slotB are written1010 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1011 * regular `unlock()` follows so the rest of the SW sees the standard1012 * post-unlock state.1013 *1014 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1015 * printed by a Pro user must always be restorable, even on a free device.1016 *1017 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1018 * is an explicit user action with a known passphrase, not a passive lock1019 * screen, so the duress-vs-real timing side channel does not apply.1020 */1021export async function unlockFromCodex(params: {1022  slotA: string; // JSON string of StoredVault for slot A1023  slotB: string | null; // JSON string of StoredVault for slot B, or null1024  timestamp: number;1025  masterPassphrase: string;1026}): Promise<1027  | { ok: true; entryCount: number }1028  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1029> {1030  let parsedSlotA: StoredVault;1031  let parsedSlotB: StoredVault | null = null;1032  try {1033    parsedSlotA = JSON.parse(params.slotA) as StoredVault;1034    if (params.slotB !== null) {1035      parsedSlotB = JSON.parse(params.slotB) as StoredVault;1036    }1037    // Minimal structural validation — the decrypt step below catches deeper1038    // corruption, but bail early on obviously-bad shapes so we don't reach1039    // storage with junk.1040    if (1041      typeof parsedSlotA?.salt !== "string" ||1042      typeof parsedSlotA?.nonce !== "string" ||1043      typeof parsedSlotA?.ciphertext !== "string" ||1044      typeof parsedSlotA?.tag !== "string"1045    ) {1046      return { ok: false, reason: "invalid_slot_format" };1047    }1048  } catch {1049    return { ok: false, reason: "invalid_slot_format" };1050  }10511052  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1053  // If this fails the on-disk vault is left completely untouched.1054  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1055  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey);1056  if (!trialPayload) {1057    zeroKey(trialKey);1058    return { ok: false, reason: "wrong_passphrase" };1059  }1060  // Drop the trial key; `unlock()` below derives its own copy. Holding two1061  // copies of the master key in memory at once would be a needless surface.1062  zeroKey(trialKey);10631064  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1065  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1066  await takeVaultSnapshot("restore");10671068  await writeSlot(SLOT_A, parsedSlotA);1069  if (parsedSlotB) {1070    await writeSlot(SLOT_B, parsedSlotB);1071  } else {1072    // Slot B must always exist with ciphertext length matching slot A1073    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1074    // configured); regenerate noise to satisfy the invariant.1075    await writeSlot(1076      SLOT_B,1077      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1078    );1079  }1080  await chrome.storage.local.set({1081    peach_last_modified_at: String(params.timestamp),1082  });10831084  // Standard unlock path so domain index, settings, auto-lock, and1085  // recovery-key state all populate exactly as a normal unlock would.1086  const ok = await unlock(params.masterPassphrase);1087  if (!ok) {1088    // Should be unreachable — we just verified the passphrase against the1089    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1090    // simple if some race or storage anomaly intrudes.1091    return { ok: false, reason: "wrong_passphrase" };1092  }1093  return { ok: true, entryCount: getEntries().length };1094}10951096// Returns the current in-memory derived key as a hex string.1097// Used by the biometric enable flow in Settings to store the key in Android Keystore.1098export function getDerivedKeyHex(): string {1099  if (!derivedKey) throw new Error("vault_not_unlocked");1100  return hex(derivedKey);1101}11021103export function getEntries(): VaultEntry[] {1104  if (!derivedKey) throw new Error("vault locked");1105  return entries.filter((entry) => !entry.deletedAt);1106}11071108const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;11091110export function getIdentities(): SavedIdentity[] {1111  if (!derivedKey) throw new Error("vault locked");1112  const active = entries.filter((e) => !e.deletedAt && e.type === "login");11131114  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1115  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();11161117  for (const entry of active) {1118    const value = entry.username?.trim();1119    if (!value) continue;1120    const map = EMAIL_RE.test(value) ? emailMap : usernameMap;1121    const existing = map.get(value);1122    const domain = extractDomain(entry.url ?? "") ?? "";1123    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;1124    if (!existing || modifiedAt > existing.lastUsed) {1125      map.set(value, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1126    } else {1127      map.set(value, { ...existing, count: existing.count + 1 });1128    }1129  }11301131  const toIdentity = (type: "email" | "username") =>1132    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1133      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1134    });11351136  return [1137    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1138    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1139  ];1140}11411142export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1143  if (!derivedKey) throw new Error("vault locked");11441145  const now = Date.now();1146  const added: VaultEntry[] = [];1147  for (const draft of drafts) {1148    try {1149      const sanitized = sanitizeEntryDraft(draft);1150      const nextEntry: VaultEntry = {1151        ...sanitized,1152        id: crypto.randomUUID(),1153        createdAt: now,1154        modifiedAt: now,1155      };1156      entries.push(nextEntry);1157      added.push(nextEntry);1158    } catch {1159      // Skip invalid drafts silently — caller can see diff between input and output length1160    }1161  }11621163  if (added.length > 0) {1164    await persistEntries();1165  }11661167  return added;1168}11691170/**1171 * Restore a set of entries in full fidelity — preserves id, createdAt,1172 * modifiedAt, and every other field unlike `batchAddEntries` which1173 * assigns fresh identity. Used by restore paths where the backup is the1174 * authoritative source of truth. `mode` controls how entries already in1175 * the vault are treated.1176 */1177export async function restoreEntries(1178  incoming: VaultEntry[],1179  mode: "replace" | "merge"1180): Promise<{ added: number; updated: number; skipped: number }> {1181  if (!derivedKey) throw new Error("vault locked");11821183  // A replace with zero incoming entries is almost certainly a caller bug1184  // (e.g. an import that silently dropped everything). Refuse rather than1185  // wipe the vault.1186  if (mode === "replace" && incoming.length === 0) {1187    throw new Error("refuse to replace with empty entry set");1188  }11891190  let added = 0;1191  let updated = 0;1192  let skipped = 0;11931194  if (mode === "replace") {1195    entries = [];1196    for (const entry of incoming) {1197      const normalized = sanitizeRestoredEntry(entry);1198      if (!normalized) {1199        skipped += 1;1200        continue;1201      }1202      entries.push(normalized);1203      added += 1;1204    }1205  } else {1206    const byId = new Map<string, VaultEntry>();1207    for (const e of entries) byId.set(e.id, e);12081209    for (const entry of incoming) {1210      const normalized = sanitizeRestoredEntry(entry);1211      if (!normalized) {1212        skipped += 1;1213        continue;1214      }1215      const existing = byId.get(normalized.id);1216      if (!existing) {1217        entries.push(normalized);1218        added += 1;1219      } else if (normalized.modifiedAt > existing.modifiedAt) {1220        const idx = entries.findIndex((e) => e.id === existing.id);1221        if (idx !== -1) {1222          entries[idx] = normalized;1223          updated += 1;1224        }1225      } else {1226        skipped += 1;1227      }1228    }1229  }12301231  await persistEntries("import");1232  return { added, updated, skipped };1233}12341235/**1236 * Validate + normalise a VaultEntry coming from a backup. Accepts1237 * anything structurally sound; keeps all metadata verbatim. Returns1238 * null for values the live vault shouldn't accept (e.g., missing title).1239 */1240function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1241  if (!entry || typeof entry !== "object") return null;1242  const title = typeof entry.title === "string" ? entry.title.trim() : "";1243  if (!title) return null;1244  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1245  const now = Date.now();1246  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1247  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;12481249  // Migrate legacy passkey shape on import (same logic as on-disk read).1250  // Imports from pre-v2.11.0 vault backups still carry type:"passkey" with1251  // flat fields; bring them into the new shape immediately.1252  const migrated: VaultEntry = migrateLegacyPasskey(1253    {1254      ...entry,1255      id,1256      title,1257      type: entry.type ?? "login",1258      createdAt,1259      modifiedAt,1260    },1261    entry1262  );12631264  // Login entries from backup must carry a password (or be passkey-only,1265  // in which case the passkey sub-object replaces the password requirement).1266  if (1267    migrated.type === "login" &&1268    !migrated.passkey &&1269    typeof migrated.password !== "string"1270  ) {1271    return null;1272  }1273  if (migrated.passkey && !migrated.passkey.credentialId) {1274    return null;1275  }12761277  return migrated;1278}12791280// Soft-deletes every non-deleted entry in one call. The literal string1281// argument is a tripwire against accidental calls — any caller that would1282// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1283// to a boolean.1284export async function clearVaultEntries(1285  confirmation: "REPLACE_ALL"1286): Promise<void> {1287  if (confirmation !== "REPLACE_ALL") {1288    throw new Error("clearVaultEntries requires explicit confirmation");1289  }1290  if (!derivedKey) throw new Error("vault locked");1291  for (const entry of entries) {1292    if (!entry.deletedAt) {1293      entry.deletedAt = Date.now();1294      entry.modifiedAt = Date.now();1295    }1296  }1297  await persistEntries();1298}12991300export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1301  if (!derivedKey) throw new Error("vault locked");13021303  const sanitized = sanitizeEntryDraft(entry);1304  const now = Date.now();1305  const nextEntry: VaultEntry = {1306    ...sanitized,1307    id: crypto.randomUUID(),1308    createdAt: now,1309    modifiedAt: now,1310    // Stamp passwordChangedAt at creation so age tracking is accurate from1311    // day one. Non-login entries carry the same stamp harmlessly.1312    passwordChangedAt: sanitized.password ? now : undefined,1313  };13141315  entries.push(nextEntry);1316  await persistEntries();13171318  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1319  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1320    checkBreach(nextEntry.password).then(async (count) => {1321      if (count > 0) {1322        const idx = entries.findIndex((e) => e.id === nextEntry.id);1323        if (idx !== -1) {1324          entries[idx] = { ...entries[idx], breach: count, modifiedAt: Date.now() };1325          await persistEntries();1326        }1327      }1328    });1329  }13301331  return nextEntry;1332}13331334export async function updateEntry(1335  id: string,1336  patch: Partial<VaultEntry>1337): Promise<void> {1338  if (!derivedKey) throw new Error("vault locked");13391340  const idx = entries.findIndex((entry) => entry.id === id);1341  if (idx === -1 || entries[idx].deletedAt) {1342    throw new Error("entry not found");1343  }13441345  const current = entries[idx];1346  const sanitizedPatch = sanitizeEntryPatch(patch);1347  // Refresh passwordChangedAt only when the password value actually changes —1348  // a metadata-only update (title, tags, etc.) must not reset the age clock1349  // that drives the Pro health rotation recommendation.1350  const passwordChanged =1351    "password" in patch &&1352    typeof sanitizedPatch.password === "string" &&1353    sanitizedPatch.password !== current.password;1354  entries[idx] = {1355    ...current,1356    ...sanitizedPatch,1357    modifiedAt: Date.now(),1358    ...(passwordChanged ? { passwordChangedAt: Date.now() } : {}),1359  };13601361  // A login entry must carry SOMETHING fillable — either a password or a1362  // passkey credential. An empty-password login with no passkey is dead1363  // state. Permit the password-clear when a passkey is attached (that's1364  // the legitimate "drop the password, keep the passkey" path).1365  if (1366    entries[idx].type === "login" &&1367    !entries[idx].password &&1368    !entries[idx].passkey1369  ) {1370    entries[idx] = current;1371    throw new Error("password or passkey required");1372  }13731374  // Card entries must retain their card number1375  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1376    entries[idx] = current;1377    throw new Error("card number is required for card entries");1378  }13791380  await persistEntries();13811382  // Breach check when password changes: clear immediately so old breach doesn't linger,1383  // then async-check the new password and only re-set if it's also found.1384  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1385    entries[idx] = { ...entries[idx], breach: undefined };1386    await persistEntries();1387    const password = entries[idx].password;1388    if (password) {1389      checkBreach(password).then(async (count) => {1390        const i = entries.findIndex((e) => e.id === id);1391        if (i !== -1 && count > 0) {1392          entries[i] = { ...entries[i], breach: count, modifiedAt: Date.now() };1393          await persistEntries();1394        }1395      });1396    }1397  }1398}13991400export async function deleteEntry(id: string): Promise<void> {1401  if (!derivedKey) throw new Error("vault locked");14021403  const entry = entries.find((item) => item.id === id);1404  if (!entry || entry.deletedAt) {1405    throw new Error("entry not found");1406  }14071408  entry.deletedAt = Date.now();1409  entry.modifiedAt = Date.now();1410  await persistEntries();1411}14121413export function getDeletedEntries(): VaultEntry[] {1414  if (!derivedKey) return [];1415  return entries.filter((e) => !!e.deletedAt);1416}14171418export async function restoreEntry(id: string): Promise<void> {1419  if (!derivedKey) throw new Error("vault locked");1420  const entry = entries.find((e) => e.id === id && e.deletedAt);1421  if (!entry) throw new Error("deleted entry not found");1422  delete entry.deletedAt;1423  entry.modifiedAt = Date.now();1424  await persistEntries();1425  buildDomainIndexSync(entries);1426}14271428export async function purgeEntry(id: string): Promise<void> {1429  if (!derivedKey) throw new Error("vault locked");1430  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);1431  if (idx === -1) throw new Error("deleted entry not found");1432  entries.splice(idx, 1);1433  await persistEntries();1434}14351436/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */1437export async function purgeExpiredTrash(retentionMs: number): Promise<number> {1438  if (!derivedKey || retentionMs === 0) return 0;1439  const cutoff = Date.now() - retentionMs;1440  const before = entries.length;1441  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));1442  const purged = before - entries.length;1443  if (purged > 0) await persistEntries();1444  return purged;1445}14461447export function queryDomain(domain: string): VaultEntry[] {1448  return domainIndex.get(domain) ?? [];1449}14501451export function getEntryById(id: string): VaultEntry | undefined {1452  return entries.find((entry) => entry.id === id && !entry.deletedAt);1453}14541455async function persistSlotA(1456  reason: VaultSnapshot["reason"] = "local_save",1457): Promise<StoredVault> {1458  if (!derivedKey) throw new Error("vault locked");14591460  const slotA = await loadSlot(SLOT_A);1461  if (!slotA) throw new Error("missing slot A");14621463  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures1464  // are swallowed by takeVaultSnapshot — history must never block a save.1465  await takeVaultSnapshot(reason);14661467  const plaintext = encodePayload(entries, duressConfigured);1468  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);14691470  // Preserve existing recovery escrow fields.1471  const nextSlotA: StoredVault = {1472    ...slotA,1473    nonce,1474    ciphertext,1475    tag,1476    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot1477  };1478  await writeSlot(SLOT_A, nextSlotA);14791480  // When duress is not configured, keep slot B size-matched with slot A by1481  // regenerating its noise. When duress IS configured, leave slot B alone —1482  // we don't have the decoy key to re-encrypt it. This is a known trade-off.1483  if (!duressConfigured) {1484    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1485  }14861487  return nextSlotA;1488}14891490async function persistSlotB(): Promise<void> {1491  if (!derivedKey) throw new Error("vault locked");14921493  const slotB = await loadSlot(SLOT_B);1494  if (!slotB) throw new Error("missing slot B");14951496  // Decoy plaintext always reports duressConfigured=false — the decoy user1497  // sees a clean slate.1498  const plaintext = encodePayload(entries, false);1499  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);15001501  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });1502}15031504async function persistEntries(1505  reason: VaultSnapshot["reason"] = "local_save",1506): Promise<void> {1507  if (!derivedKey) throw new Error("vault locked");15081509  if (vaultMode === "decoy") {1510    await persistSlotB();1511  } else {1512    await persistSlotA(reason);1513  }1514  buildDomainIndexSync(entries);1515}15161517function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {1518  if (!Array.isArray(values)) return undefined;1519  const seen = new Set<string>();1520  const cleaned: string[] = [];1521  for (const value of values) {1522    const trimmed = typeof value === "string" ? value.trim() : "";1523    if (!trimmed) continue;1524    const key = trimmed.toLowerCase();1525    if (seen.has(key)) continue;1526    seen.add(key);1527    cleaned.push(trimmed);1528  }1529  return cleaned.length > 0 ? cleaned : undefined;1530}15311532function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {1533  const title = entry.title.trim();1534  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new1535  // model is "any entry, optionally with a passkey sub-object." Imports and1536  // any in-flight callers from before v2.11.0 keep working without throwing.1537  let type: VaultEntry["type"] = entry.type ?? "login";1538  if (type === "passkey") type = "login";15391540  if (!title) throw new Error("title is required");1541  // A login entry needs either a password or a passkey credential to be useful.1542  // Passkey-only entries (passkey present, no password) are valid.1543  if (type === "login" && !entry.password?.trim() && !entry.passkey) {1544    throw new Error("password or passkey required");1545  }1546  if (entry.passkey) {1547    const p = entry.passkey;1548    if (1549      !p.rpId ||1550      !p.credentialId ||1551      !p.publicKeyCose ||1552      !p.userHandle ||1553      !p.userName ||1554      (p.algorithm !== -7 && p.algorithm !== -8)1555    ) {1556      throw new Error("invalid passkey credential");1557    }1558  }1559  if (type === "card") {1560    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {1561      throw new Error("cardholder name and card number are required");1562    }1563  }15641565  return {1566    type,1567    title,1568    username: type === "login" ? entry.username?.trim() ?? "" : undefined,1569    password: type === "login" ? entry.password : undefined,1570    url: type === "login" ? entry.url?.trim() ?? "" : undefined,1571    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,1572    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,1573    notes: entry.notes?.trim() || undefined,1574    breach: type === "login" ? entry.breach : undefined,1575    canary: type === "login" ? entry.canary : undefined,1576    tags: entry.tags,1577    passkey: entry.passkey,1578    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,1579    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,1580    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,1581    expYear: type === "card" ? entry.expYear?.trim() : undefined,1582    cvc: type === "card" ? entry.cvc?.trim() : undefined,1583    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,1584    network: type === "card" ? entry.network : undefined,1585    last4: type === "card" ? entry.last4?.trim() : undefined,1586  };1587}15881589function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {1590  const nextPatch: Partial<VaultEntry> = {};15911592  if ("type" in patch) {1593    nextPatch.type = patch.type;1594  }15951596  if ("title" in patch) {1597    const title = patch.title?.trim() ?? "";1598    if (!title) throw new Error("title is required");1599    nextPatch.title = title;1600  }16011602  if ("url" in patch) {1603    nextPatch.url = patch.url?.trim() ?? "";1604  }16051606  if ("additionalUrls" in patch) {1607    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];1608    const seen = new Set<string>();1609    const cleaned: string[] = [];1610    for (const value of incoming) {1611      const trimmed = typeof value === "string" ? value.trim() : "";1612      if (!trimmed) continue;1613      const key = trimmed.toLowerCase();1614      if (seen.has(key)) continue;1615      seen.add(key);1616      cleaned.push(trimmed);1617    }1618    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;1619  }16201621  if ("username" in patch) {1622    nextPatch.username = patch.username?.trim() ?? "";1623  }16241625  if ("email" in patch) {1626    nextPatch.email = patch.email?.trim() || undefined;1627  }16281629  if ("password" in patch) {1630    const password = patch.password ?? "";1631    // Allow clearing a password (e.g. on passkey-only entries)1632    nextPatch.password = password || undefined;1633  }16341635  if ("totp" in patch) {1636    nextPatch.totp = patch.totp?.trim() || undefined;1637  }16381639  if ("notes" in patch) {1640    nextPatch.notes = patch.notes?.trim() || undefined;1641  }16421643  if ("breach" in patch) {1644    nextPatch.breach = patch.breach;1645  }16461647  if ("canary" in patch) {1648    nextPatch.canary = patch.canary;1649  }16501651  if ("passkey" in patch) {1652    // Passkey sub-object replaces all the legacy flat passkey fields. A1653    // patch with `passkey: undefined` removes the credential from the entry1654    // (used by "remove passkey" without deleting the whole entry).1655    nextPatch.passkey = patch.passkey;1656  }16571658  if ("tags" in patch) {1659    nextPatch.tags = patch.tags;1660  }16611662  if ("cardholderName" in patch) {1663    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;1664  }1665  if ("cardNumber" in patch) {1666    const n = patch.cardNumber ?? "";1667    nextPatch.cardNumber = n || undefined;1668  }1669  if ("expMonth" in patch) {1670    nextPatch.expMonth = patch.expMonth?.trim() || undefined;1671  }1672  if ("expYear" in patch) {1673    nextPatch.expYear = patch.expYear?.trim() || undefined;1674  }1675  if ("cvc" in patch) {1676    nextPatch.cvc = patch.cvc?.trim() || undefined;1677  }1678  if ("cardNickname" in patch) {1679    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;1680  }1681  if ("network" in patch) {1682    nextPatch.network = patch.network;1683  }1684  if ("last4" in patch) {1685    nextPatch.last4 = patch.last4?.trim() || undefined;1686  }16871688  return nextPatch;1689}16901691function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {1692  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;1693  const modifiedAt =1694    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;16951696  const base: VaultEntry = {1697    id: entry.id,1698    type: "type" in entry ? entry.type : "login",1699    title: entry.title,1700    username: entry.username,1701    password: entry.password,1702    url: entry.url,1703    totp: entry.totp,1704    notes: entry.notes,1705    breach: entry.breach,1706    canary: "canary" in entry ? entry.canary : undefined,1707    passkey: "passkey" in entry ? entry.passkey : undefined,1708    createdAt,1709    modifiedAt,1710    deletedAt: entry.deletedAt,1711    tags: "tags" in entry ? entry.tags : undefined,1712    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,1713    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,1714    expMonth: "expMonth" in entry ? entry.expMonth : undefined,1715    expYear: "expYear" in entry ? entry.expYear : undefined,1716    cvc: "cvc" in entry ? entry.cvc : undefined,1717    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,1718    network: "network" in entry ? entry.network : undefined,1719    last4: "last4" in entry ? entry.last4 : undefined,1720  };17211722  return migrateLegacyPasskey(base, entry);1723}17241725/**1726 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`1727 * with flat top-level fields (rpId, credentialId, etc.). This converts them1728 * to the new shape — `type:"login"` with a `passkey` sub-object — so all1729 * downstream consumers can rely on the new model. The result is rewritten1730 * to disk on next vault save (hasLegacyShape detects the un-migrated case).1731 */1732function migrateLegacyPasskey(1733  base: VaultEntry,1734  raw: VaultEntry | LegacyVaultEntry1735): VaultEntry {1736  if (base.passkey) return base;1737  const flat = raw as Partial<VaultEntry>;1738  const isPasskeyType = base.type === "passkey";1739  const hasFlatPasskeyFields =1740    typeof flat.rpId === "string" &&1741    typeof flat.credentialId === "string" &&1742    typeof flat.publicKeyCose === "string" &&1743    typeof flat.userHandle === "string" &&1744    typeof flat.userName === "string";1745  if (!isPasskeyType && !hasFlatPasskeyFields) return base;1746  if (!hasFlatPasskeyFields) return base;17471748  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;1749  base.passkey = {1750    rpId: flat.rpId!,1751    rpName: flat.rpName ?? flat.rpId!,1752    credentialId: flat.credentialId!,1753    publicKeyCose: flat.publicKeyCose!,1754    privateKeyJwk: flat.privateKeyJwk,1755    userHandle: flat.userHandle!,1756    userName: flat.userName!,1757    userDisplayName: flat.userDisplayName ?? flat.userName!,1758    algorithm,1759    signCount: flat.signCount ?? 0,1760    aaguid: flat.aaguid ?? "",1761    backupEligible: flat.backupEligible === true,1762    backupState: flat.backupState === true,1763    lastUsedAt: flat.lastUsedAt,1764    browser: flat.browser,1765    transports: flat.transports,1766    createdAt: base.createdAt,1767  };1768  // Promote to "login" so the unified Entry view renders. Username gets1769  // populated from the passkey's userName so the entry is searchable in the1770  // existing autofill flow without a separate code path.1771  if (isPasskeyType) base.type = "login";1772  if (!base.username) base.username = flat.userName;1773  if (!base.url) base.url = `https://${flat.rpId}`;1774  return base;1775}177617771778// ---------------------------------------------------------------------------1779// Deduplication1780// ---------------------------------------------------------------------------17811782function entryDupeKey(e: VaultEntry): string {1783  if (e.type === "note") return `note|${e.title.toLowerCase()}`;1784  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins1785  // on different sites that happen to share a username are still distinct;1786  // two passkey credentials for the same RP+user are duplicates regardless1787  // of any login fields they may also carry.1788  if (e.passkey) {1789    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;1790  }1791  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;1792}17931794/** Returns the number of duplicate entries (those that would be removed). */1795export function countDuplicates(): number {1796  const best = new Map<string, VaultEntry>();1797  let dupeCount = 0;1798  for (const e of entries.filter((e) => !e.deletedAt)) {1799    const key = entryDupeKey(e);1800    const existing = best.get(key);1801    if (!existing) {1802      best.set(key, e);1803    } else {1804      dupeCount++;1805      // Keep the more recently modified one in the map1806      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1807        best.set(key, e);1808      }1809    }1810  }1811  return dupeCount;1812}18131814/**1815 * Soft-deletes duplicate entries, keeping the most recently modified copy of1816 * each group. Returns the number of entries moved to trash.1817 */1818export async function deduplicateEntries(): Promise<number> {1819  if (!derivedKey) throw new Error("vault locked");1820  // Dedup reshapes slot B permanently under the decoy key, which is1821  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.1822  if (vaultMode === "decoy") {1823    throw new Error("deduplicate disabled in decoy mode");1824  }18251826  const best = new Map<string, VaultEntry>();1827  for (const e of entries.filter((en) => !en.deletedAt)) {1828    const key = entryDupeKey(e);1829    const existing = best.get(key);1830    if (!existing) {1831      best.set(key, e);1832    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1833      best.set(key, e);1834    }1835  }18361837  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));1838  const now = Date.now();1839  let removed = 0;1840  entries = entries.map((e) => {1841    if (!e.deletedAt && !keepIds.has(e.id)) {1842      removed++;1843      return { ...e, deletedAt: now };1844    }1845    return e;1846  });18471848  if (removed > 0) await persistEntries();1849  return removed;1850}18511852// ---------------------------------------------------------------------------1853// Vault history — snapshot BEFORE every vault write so the saved blob is1854// what is about to be lost, not what was just written. Capped at 10. The1855// stored blobs are AES-256-GCM ciphertext under the master password, so1856// history adds zero security surface.1857// ---------------------------------------------------------------------------18581859const HISTORY_KEY = "peach_vault_history";1860const HISTORY_MAX = 10;18611862/**1863 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE1864 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost1865 * state. Identical-to-most-recent blobs are skipped to avoid filling history1866 * with no-op writes.1867 */1868export async function takeVaultSnapshot(1869  reason: VaultSnapshot["reason"],1870): Promise<void> {1871  try {1872    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);1873    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];1874    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];18751876    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,1877    // pre-createVault). createVault skips snapshots intentionally — there is1878    // no prior state to preserve.1879    if (!slotA) return;18801881    const vault_a = JSON.stringify(slotA);1882    const vault_b = JSON.stringify(slotB ?? slotA);18831884    // Best-effort entry count: only meaningful when the in-memory state is1885    // unlocked AND in real mode. Decoy mode would leak the decoy entry1886    // count; locked mode has no entries to count. Both fall through to null.1887    let entryCount: number | null = null;1888    if (derivedKey && vaultMode === "real") {1889      try {1890        entryCount = entries.filter((e) => !e.deletedAt).length;1891      } catch {1892        entryCount = null;1893      }1894    }18951896    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];1897    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)1898      ? existingRaw1899      : { snapshots: [] };19001901    // Deduplication: if the most recent snapshot has byte-identical blobs,1902    // skip — saves the same vault N times in a row would otherwise burn the1903    // entire history window with no recovery value.1904    const latest = history.snapshots[0];1905    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {1906      return;1907    }19081909    const snapshot: VaultSnapshot = {1910      id: crypto.randomUUID(),1911      savedAt: Date.now(),1912      reason,1913      vault_a,1914      vault_b,1915      entryCount,1916    };19171918    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);1919    await chrome.storage.local.set({ [HISTORY_KEY]: history });1920  } catch {1921    // Non-fatal — history must never block a save. A failed snapshot just1922    // means the user loses one rung of the safety ladder for this write.1923  }1924}19251926/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */1927export async function clearVaultHistory(): Promise<void> {1928  try {1929    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });1930  } catch {1931    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().1932  }1933}19341935/** Read all snapshots, newest first, for the Settings vault history view. */1936export async function getVaultHistory(): Promise<VaultSnapshot[]> {1937  const raw = await chrome.storage.local.get(HISTORY_KEY);1938  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];1939  if (!history || !Array.isArray(history.snapshots)) return [];1940  return history.snapshots;1941}19421943/**1944 * Replace the on-disk vault with the chosen snapshot's blobs and lock the1945 * vault. The current state is itself snapshotted with reason 'restore' first1946 * so the restore is reversible. After this returns, the user must unlock1947 * with the password that matches the restored blob.1948 */1949export async function restoreVaultSnapshot(1950  snapshotId: string,1951): Promise<{ ok: true } | { ok: false; error: string }> {1952  const history = await getVaultHistory();1953  const snapshot = history.find((s) => s.id === snapshotId);1954  if (!snapshot) return { ok: false, error: "snapshot_not_found" };19551956  // Snapshot the about-to-be-replaced current state so the restore is1957  // reversible. takeVaultSnapshot is no-op when the most recent entry is1958  // byte-identical, so a restore-of-a-restore stays clean.1959  await takeVaultSnapshot("restore");19601961  let restoredA: StoredVault;1962  let restoredB: StoredVault;1963  try {1964    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;1965    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;1966  } catch {1967    return { ok: false, error: "snapshot_corrupt" };1968  }19691970  await writeSlot(SLOT_A, restoredA);1971  await writeSlot(SLOT_B, restoredB);19721973  // Lock so the user must prove they can decrypt the restored blob. If they1974  // restored a pre-password-change snapshot they discover it immediately at1975  // the lock screen rather than silently holding an inaccessible vault.1976  lock();19771978  return { ok: true };1979}19801981/**1982 * Change the master password. Re-encrypts slot A with a fresh salt + new1983 * derived key. Slot B (duress or noise) is left untouched when duress is1984 * configured — it uses an independent key derived from the duress password —1985 * and regenerated as noise when duress is not configured.1986 */1987export async function changePassword(1988  currentPassword: string,1989  newPassword: string1990): Promise<boolean> {1991  if (!derivedKey || vaultMode !== "real") return false;19921993  // Verify current password before allowing any change1994  const valid = await verifyMainPassword(currentPassword);1995  if (!valid) return false;19961997  const slotA = await loadSlot(SLOT_A);1998  if (!slotA) return false;19992000  // Derive fresh key from new password + new random salt2001  const newSalt = generateSalt();2002  const newKey = await deriveKey(newPassword, newSalt);20032004  // Re-encrypt the in-memory entries with the new key2005  const plaintext = encodePayload(entries, duressConfigured);2006  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);20072008  const freshSlotA: StoredVault = {2009    ...slotA,2010    salt: hex(newSalt),2011    nonce,2012    ciphertext,2013    tag,2014  };2015  // Snapshot the OLD-key blob before overwriting it. If the user later2016  // restores from this snapshot they will need the OLD password — the2017  // post-restore lock surfaces that immediately.2018  await takeVaultSnapshot("local_save");2019  await writeSlot(SLOT_A, freshSlotA);20202021  // Swap the in-memory key synchronously before any further await. If we2022  // threw between the Slot A write and this swap, the next persistEntries()2023  // would overwrite Slot A with old-key ciphertext, locking the user out.2024  zeroKey(derivedKey);2025  derivedKey = newKey;20262027  // Rotate the root key so the old recovery words are immediately invalidated.2028  // After this point getRecoveryWords() returns a fresh 12-word set.2029  const {2030    rawRootKey: freshRawKey,2031    recoverySalt: freshSalt,2032    wrappedRootKey: freshWrapped,2033    wrappedNonce: freshNonce,2034    wrappedTag: freshTag,2035  } = await generateAndWrapRootKey();2036  rootKey = unhex(freshRawKey);2037  const slotAfterRekey = await loadSlot(SLOT_A);2038  if (slotAfterRekey) {2039    await writeSlot(SLOT_A, {2040      ...slotAfterRekey,2041      recovery_salt: freshSalt,2042      recovery_wrapped_root_key: freshWrapped,2043      recovery_nonce: freshNonce,2044      recovery_tag: freshTag,2045    });2046  }20472048  // Regenerate noise for slot B when duress is not in use. Runs after the2049  // key swap so a failure here is a disposable noise regeneration, not a2050  // lockout.2051  if (!duressConfigured) {2052    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2053  }20542055  return true;2056}20572058// ---------------------------------------------------------------------------2059// Sync helpers2060// ---------------------------------------------------------------------------20612062export async function getVaultBlobForSync(): Promise<{2063  blob: string;2064  timestamp: number;2065  license?: string;2066} | null> {2067  const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, 'peach_last_modified_at', 'peach_license']);2068  const slotA = (raw as Record<string, unknown>)[SLOT_A];2069  if (!slotA) return null;2070  const slotB = (raw as Record<string, unknown>)[SLOT_B];2071  const blob = JSON.stringify({ a: slotA, b: slotB });2072  const rawTs = (raw as Record<string, unknown>)['peach_last_modified_at'];2073  const timestamp = typeof rawTs === 'string' ? parseInt(rawTs) : 0;2074  const license = (raw as Record<string, unknown>)['peach_license'];2075  return {2076    blob,2077    timestamp,2078    license: typeof license === 'string' ? license : undefined,2079  };2080}20812082export async function applyIncomingVault(2083  blobJson: string,2084  remoteTimestamp: number,2085  remoteLicense?: string,2086): Promise<boolean> {2087  const raw = await chrome.storage.local.get('peach_last_modified_at');2088  const rawTs = (raw as Record<string, unknown>)['peach_last_modified_at'];2089  const localTimestamp = typeof rawTs === 'string' ? parseInt(rawTs) : 0;20902091  if (remoteLicense) {2092    const licenseRaw = await chrome.storage.local.get('peach_license');2093    const existing = (licenseRaw as Record<string, unknown>)['peach_license'];2094    if (!existing) {2095      await chrome.storage.local.set({ peach_license: remoteLicense });2096    }2097  }20982099  if (remoteTimestamp <= localTimestamp) return false;21002101  // Snapshot before overwriting — failure is non-fatal2102  await takeVaultSnapshot('sync_received').catch(() => {});21032104  const { a, b } = JSON.parse(blobJson) as { a: unknown; b?: unknown };2105  await writeSlot(SLOT_A, a as StoredVault);2106  if (b) await writeSlot(SLOT_B, b as StoredVault);2107  await chrome.storage.local.set({2108    peach_last_sync_at: String(Date.now()),2109    peach_last_modified_at: String(remoteTimestamp),2110  });21112112  return true;2113}21142115export async function getSyncIdentity(): Promise<{2116  privateKeyBytes: Uint8Array;2117  publicKeyBytes: Uint8Array;2118  publicKeyHex: string;2119}> {2120  const raw = await chrome.storage.local.get('peach_sync_identity');2121  const stored = (raw as Record<string, unknown>)['peach_sync_identity'];2122  if (typeof stored === 'string') {2123    const { priv, pub } = JSON.parse(stored) as { priv: string; pub: string };2124    const privateKeyBytes = base64DecodeBytes(priv);2125    const publicKeyBytes = base64DecodeBytes(pub);2126    return { privateKeyBytes, publicKeyBytes, publicKeyHex: bytesToHex(publicKeyBytes) };2127  }21282129  // Generate a fresh X25519 keypair and persist it.2130  const { generateKeyPair } = await import('@stablelib/x25519');2131  const keypair = generateKeyPair();2132  await chrome.storage.local.set({2133    peach_sync_identity: JSON.stringify({2134      priv: base64EncodeBytes(keypair.secretKey),2135      pub: base64EncodeBytes(keypair.publicKey),2136    }),2137  });2138  return {2139    privateKeyBytes: keypair.secretKey,2140    publicKeyBytes: keypair.publicKey,2141    publicKeyHex: bytesToHex(keypair.publicKey),2142  };2143}21442145// Re-decrypt vault entries from storage using the current in-memory key.2146// Called after applyIncomingVault so the in-memory state stays consistent2147// with what was just written to storage.2148export async function reloadEntriesAfterSync(): Promise<boolean> {2149  if (!derivedKey) return false;2150  const slotA = await loadSlot(SLOT_A);2151  if (!slotA) return false;2152  try {2153    const payload = await tryDecryptSlot(slotA, derivedKey);2154    if (!payload) return false;2155    entries = payload.entries;2156    buildDomainIndexSync(entries);2157    return true;2158  } catch {2159    return false;2160  }2161}21622163function bytesToHex(bytes: Uint8Array): string {2164  return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');2165}21662167function base64EncodeBytes(bytes: Uint8Array): string {2168  let binary = '';2169  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);2170  return btoa(binary);2171}21722173function base64DecodeBytes(b64: string): Uint8Array {2174  const binary = atob(b64);2175  const bytes = new Uint8Array(binary.length);2176  for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);2177  return bytes;2178}