vault.ts

76.3 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  // Each component is stored separately in the vault slot; decode them individually295  // so the nonce is always exactly the 12 bytes from wrappedNonce.296  const nonceRaw = unhex(wrappedNonce);297  const ciphertextRaw = unhex(wrappedRootKey);298  const tagRaw = unhex(wrappedTag);299  // Combine ciphertext and tag for AES-GCM (expects ciphertext||tag).300  // Use explicit ArrayBuffer allocations to satisfy TS BufferSource constraints.301  const nonceU8 = nonceRaw as unknown as Uint8Array<ArrayBuffer>;302  const combined = new Uint8Array(ciphertextRaw.length + tagRaw.length);303  combined.set(ciphertextRaw, 0);304  combined.set(tagRaw, ciphertextRaw.length);305  const raw = await crypto.subtle.decrypt(306    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },307    recoveryKey,308    combined309  );310  const decryptedHex = new TextDecoder().decode(raw);311  rootKey = unhex(decryptedHex);312}313314/**315 * Re-wrap the cached root key under a new derived key (from the user's new316 * password). Called after password change and after recovery re-key so the317 * same recovery words continue to work with the updated password.318 */319export async function rewrapRootKeyForPassword(320  _newDerivedKey: Uint8Array,321  _newSalt: Uint8Array322): Promise<void> {323  if (!rootKey) throw new Error("root key not set");324  const slotA = await loadSlot(SLOT_A);325  if (!slotA) throw new Error("missing slot A");326  // This build has only recovery-word escrow fields for rootKey. Do not write327  // password-derived wrapping material into recovery_* fields; the vault328  // payload itself is re-encrypted under the new password by the caller.329  // Future pw_root_* fields should be updated here without touching recovery_*.330}331332/**333 * Change the vault encryption to a new derived key (already derived from334 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.335 * Caller is responsible for key swap and snapshot.336 */337async function reencryptVaultForNewKey(338  newKey: Uint8Array,339  slotA: StoredVault340): Promise<void> {341  const plaintext = encodePayload(entries, duressConfigured);342  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);343  await writeSlot(SLOT_A, {344    ...slotA,345    nonce,346    ciphertext,347    tag,348  });349}350351/**352 * Full password change with an already-derived key (used after recovery).353 * Unlike changePassword(), this skips re-verification of current password354 * since recovery flow has already validated the user's identity via words.355 */356export async function changePasswordWithNewKey(357  newSalt: Uint8Array,358  newKey: Uint8Array359): Promise<void> {360  const slotA = await loadSlot(SLOT_A);361  if (!slotA) throw new Error("missing slot A");362  // reencryptVaultForNewKey must receive the updated salt so that subsequent363  // UNLOCK calls with the new password can re-derive the correct key.364  await reencryptVaultForNewKey(newKey, { ...slotA, salt: hex(newSalt) });365  if (derivedKey) zeroKey(derivedKey);366  derivedKey = newKey;367  if (!duressConfigured) {368    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));369  }370  await stampLocalVaultMutation();371}372373// ---------------------------------------------------------------------------374// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive375// a new key after password reset without re-importing the crypto module.376// ---------------------------------------------------------------------------377378export { deriveKey, generateSalt };379380export async function loadSlotA(): Promise<StoredVault | null> {381  return loadSlot(SLOT_A);382}383384/**385 * Re-derive the session root key from the already-derived AES key (keyA from386 * unlock) and the recovery salt stored in slot A. This is the inverse of387 * generateAndWrapRootKey() — both operations use the same HKDF with the same388 * salt and info string, so they cancel out.389 *390 * The derivedKey and rootKey are related by:391 *   derivedKey = Argon2id(password, slotA.salt)   → AES key for vault payload392 *   rootKey    = HKDF-SHA256(recovery_words, recovery_salt)  → wrapped by HKDF393 *394 * Since we only store the wrapped root key and the recovery salt, we can395 * only re-derive rootKey during an active (unlocked) session where we have396 * the derivedKey already. On lock, rootKey is zeroed.397 *398 * For pre-v2.12 vaults that have no recovery_salt, this returns null and399 * the caller must invoke migrateToRecovery() to generate the recovery escrow.400 */401async function deriveRootKeyFromDerivedKey(402  _derivedKey: Uint8Array,403  _recoverySaltHex: string404): Promise<Uint8Array | null> {405  // The root key is stored wrapped under the recovery key derived from the406  // 12-word phrase. It cannot be re-derived from derivedKey alone — that would407  // require knowing the original recovery words. The in-memory rootKey is set408  // during unlock if recovery data is present.409  // This function exists as a potential future path if we ever store an410  // additional derived-key → root-key relationship. Currently rootKey is411  // cached directly during unlock.412  return null;413}414415/**416 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),417 * generate the root key, wrap it under the recovery words, and persist the418 * recovery escrow into slot A. The caller must show the recovery words to the419 * user before calling this — once called, those words are the vault's recovery420 * path. This runs once, on the first unlock of an upgraded vault.421 */422export async function migrateToRecovery(): Promise<void> {423  if (!derivedKey || vaultMode !== "real") {424    throw new Error("vault must be unlocked in real mode");425  }426  const slotA = await loadSlot(SLOT_A);427  if (!slotA) throw new Error("missing slot A");428  if (slotA.recovery_salt && slotA.recovery_wrapped_root_key) return; // already done429430  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =431    await generateAndWrapRootKey();432433  rootKey = unhex(rawRootKey);434435  const nextSlotA: StoredVault = {436    ...slotA,437    recovery_salt: recoverySalt,438    recovery_wrapped_root_key: wrappedRootKey,439    recovery_nonce: wrappedNonce,440    recovery_tag: wrappedTag,441  };442  await writeSlot(SLOT_A, nextSlotA);443  await stampLocalVaultMutation();444}445446/**447 * Returns the 12 recovery words for the current session's root key.448 * Must be called while the vault is unlocked. Throws if recovery has not449 * been set up (pre-v2.12 vault that hasn't migrated yet).450 */451export async function getRecoveryWords(): Promise<string[]> {452  if (!rootKey) {453    throw new Error("recovery not set up — vault may need migration");454  }455  return rootKeyToWords(rootKey);456}457458async function loadSlot(key: string): Promise<StoredVault | null> {459  const result = await chrome.storage.local.get(key);460  return (result as Record<string, StoredVault | undefined>)[key] ?? null;461}462463async function writeSlot(key: string, vault: StoredVault): Promise<void> {464  try {465    await chrome.storage.local.set({ [key]: vault });466  } catch (err) {467    throw mapStorageError(err);468  }469}470471async function stampLocalVaultMutation(): Promise<void> {472  await chrome.storage.local.set({473    peach_last_modified_at: String(Date.now()),474  });475}476477/**478 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault479 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +480 * peach_vault_b). Runs at most once per session; safe to call repeatedly.481 */482async function ensureMigrated(): Promise<void> {483  if (migrationRan) return;484  migrationRan = true;485486  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];487  const raw = await chrome.storage.local.get(keys);488  const slotAExists = raw[SLOT_A] !== undefined;489  const slotBExists = raw[SLOT_B] !== undefined;490  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;491  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;492493  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.494  if (!slotAExists && !legacyMain) return;495496  // Already migrated.497  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;498499  let didMigrate = false;500  if (legacyMain && !slotAExists) {501    await writeSlot(SLOT_A, legacyMain);502    didMigrate = true;503  }504505  // If slot B isn't set, populate it: use legacy decoy when present, else noise.506  if (!slotBExists) {507    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;508    if (legacyDecoy) {509      await writeSlot(SLOT_B, legacyDecoy);510      didMigrate = true;511    } else if (target) {512      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));513      didMigrate = true;514    }515  }516517  const toRemove: string[] = [];518  if (legacyMain) toRemove.push(LEGACY_MAIN);519  if (legacyDecoy) toRemove.push(LEGACY_DECOY);520  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);521  if (toRemove.length > 0) {522    await chrome.storage.local.remove(toRemove);523    didMigrate = true;524  }525  if (didMigrate) {526    await stampLocalVaultMutation();527  }528}529530// ---------------------------------------------------------------------------531// Padding + noise helpers532// ---------------------------------------------------------------------------533534/**535 * Pads the plaintext string with trailing spaces so that the encoded byte536 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing537 * whitespace after a complete document, so the padding is transparent on538 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.539 */540function padPlaintext(json: string): string {541  const encoded = new TextEncoder().encode(json);542  const target = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);543  const padLen = target - encoded.length;544  return json + " ".repeat(padLen);545}546547/**548 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The549 * ciphertext length matches the given byte count. Because GCM ciphertext is550 * pseudo-random, this blob is indistinguishable at rest from a real551 * encrypted vault without the key.552 *553 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto554 * spec hard limit). Chunk the fill so vaults larger than that — common once555 * the user has dozens of entries or any passkey — don't throw.556 */557const CSPRNG_CHUNK = 65_536;558559function fillRandom(buf: Uint8Array): Uint8Array {560  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {561    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);562    const chunk = new Uint8Array(end - offset);563    crypto.getRandomValues(chunk);564    buf.set(chunk, offset);565  }566  return buf;567}568569function generateNoiseBlob(ciphertextBytes: number): StoredVault {570  const salt = crypto.getRandomValues(new Uint8Array(16));571  const nonce = crypto.getRandomValues(new Uint8Array(12));572  const cipher = fillRandom(new Uint8Array(ciphertextBytes));573  const tag = crypto.getRandomValues(new Uint8Array(16));574  return {575    version: 1,576    salt: hex(salt),577    nonce: hex(nonce),578    ciphertext: hex(cipher),579    tag: hex(tag),580  };581}582583// ---------------------------------------------------------------------------584// Encrypted plaintext envelope585// ---------------------------------------------------------------------------586587type VaultPayload = {588  entries: VaultEntry[];589  duressConfigured?: boolean;590};591592type DecryptedPayload = {593  entries: VaultEntry[];594  duressConfigured: boolean;595  /**596   * True when at least one entry in the on-disk payload had a legacy shape597   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)598   * and got rewritten by normalizeEntry. The unlock path uses this to599   * persist the new shape immediately — without it, the migration would600   * re-run on every unlock and old shape never disappears from disk.601   */602  migratedFromLegacy: boolean;603};604605function encodePayload(entries: VaultEntry[], duress: boolean): string {606  const envelope: VaultPayload = { entries, duressConfigured: duress };607  return padPlaintext(JSON.stringify(envelope));608}609610function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {611  if ("created" in raw || "updated" in raw) return true;612  const flat = raw as Partial<VaultEntry>;613  if (flat.type === "passkey") return true;614  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.615  if (flat.rpId !== undefined && flat.passkey === undefined) return true;616  return false;617}618619function decodePayload(plain: string): DecryptedPayload {620  const parsed = JSON.parse(plain) as621    | Array<VaultEntry | LegacyVaultEntry>622    | VaultPayload;623624  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];625  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);626627  const normalized = rawEntries.map(normalizeEntry);628  const { merged } = mergePasskeyOnlyIntoLogins(normalized);629630  if (Array.isArray(parsed)) {631    return {632      entries: normalized,633      duressConfigured: false,634      migratedFromLegacy: true, // pre-A-2 array shape always needs save635    };636  }637638  return {639    entries: normalized,640    duressConfigured: parsed.duressConfigured === true,641    migratedFromLegacy: flatMigrationNeeded || merged,642  };643}644645/**646 * One-time consolidation pass run alongside the legacy passkey migration.647 *648 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`649 * entry per registration — so users who tested registration multiple times650 * accumulated several passkey-bearing entries for the same RP, and the651 * migration to the unified shape carried that duplication forward. This652 * pass collapses the duplicates:653 *654 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.655 *   2. Within each group, the most recently-active credential wins (prefer656 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).657 *   3. If a NON-passkey login entry exists for that RP (matched by url or658 *      additionalUrls hostname), attach the winning passkey to it and659 *      soft-delete every passkey-bearing entry in the group.660 *   4. Otherwise, soft-delete every loser in the group, keeping the winner661 *      as a passkey-only entry.662 *663 * Soft delete (deletedAt) rather than splice — the trash flow keeps the664 * record around for retention and the user can restore if the heuristic665 * picked wrong. The `merged` flag goes back to decodePayload so the666 * unlock path persists the consolidated shape immediately.667 */668function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {669  let merged = false;670  const now = Date.now();671672  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);673  const byRpId = new Map<string, VaultEntry[]>();674  for (const e of candidates) {675    const rpId = e.passkey!.rpId;676    const list = byRpId.get(rpId);677    if (list) list.push(e);678    else byRpId.set(rpId, [e]);679  }680681  for (const [rpId, group] of byRpId) {682    if (group.length === 0) continue;683    group.sort((a, b) => {684      const aT =685        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;686      const bT =687        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;688      return bT - aT;689    });690    const winner = group[0]!;691692    // Look for a NON-passkey login entry matching this RP. Use the URL693    // hostname rather than a substring search so a "login.foo.com" entry694    // doesn't accidentally absorb a passkey for "foo.com".695    const loginTarget = entries.find((e) => {696      if (e.deletedAt || e.passkey || e.type !== "login") return false;697      if (e.url && extractDomain(e.url) === rpId) return true;698      for (const extra of e.additionalUrls ?? []) {699        if (extractDomain(extra) === rpId) return true;700      }701      return false;702    });703704    if (loginTarget) {705      loginTarget.passkey = winner.passkey;706      loginTarget.modifiedAt = now;707      for (const dup of group) {708        if (!dup.deletedAt) {709          dup.deletedAt = now;710          dup.modifiedAt = now;711        }712      }713      merged = true;714    } else if (group.length > 1) {715      for (let i = 1; i < group.length; i += 1) {716        const dup = group[i]!;717        if (!dup.deletedAt) {718          dup.deletedAt = now;719          dup.modifiedAt = now;720        }721      }722      merged = true;723    }724  }725726  return { merged };727}728729// Exported solely for `unlockFromCodex` below, which needs to verify a730// supplied passphrase against a slotA blob *without* committing anything to731// storage. Keep this internal otherwise — callers crossing the messages.ts732// boundary should always go through `unlock` / `verifyMainPassword` so the733// constant-time invariants in Projects.md §3.4 are not bypassed.734export async function tryDecryptSlot(735  vault: StoredVault,736  key: Uint8Array737): Promise<DecryptedPayload | null> {738  try {739    const plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag);740    return decodePayload(plain);741  } catch {742    return null;743  }744}745746// ---------------------------------------------------------------------------747// Core operations748// ---------------------------------------------------------------------------749750function buildDomainIndexSync(all: VaultEntry[]): void {751  domainIndex = new Map();752753  for (const entry of all) {754    if (entry.deletedAt) continue;755    if (entry.type === "note" || entry.type === "card") continue;756757    // An entry can match multiple sites via `additionalUrls`. Index each758    // hostname so `queryDomain` returns the entry regardless of which alias759    // the user is currently visiting. Dedupe per-entry so a single entry760    // never appears twice in one bucket.761    const domains = new Set<string>();762    const primary = extractDomain(entry.url ?? "");763    if (primary) domains.add(primary);764    for (const extra of entry.additionalUrls ?? []) {765      const d = extractDomain(extra);766      if (d) domains.add(d);767    }768    if (domains.size === 0) continue;769770    for (const domain of domains) {771      const bucket = domainIndex.get(domain) ?? [];772      bucket.push(entry);773      domainIndex.set(domain, bucket);774    }775  }776}777778export async function createVault(password: string): Promise<void> {779  await ensureMigrated();780781  const salt = generateSalt();782  derivedKey = await deriveKey(password, salt);783  entries = [];784  vaultMode = "real";785  duressConfigured = false;786  buildDomainIndexSync(entries);787788  // Generate and wrap the root key for recovery word escrow.789  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =790    await generateAndWrapRootKey();791  rootKey = unhex(rawRootKey);792793  const plaintext = encodePayload(entries, duressConfigured);794  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);795796  const slotA: StoredVault = {797    version: 1,798    salt: hex(salt),799    nonce,800    ciphertext,801    tag,802    recovery_salt: recoverySalt,803    recovery_wrapped_root_key: wrappedRootKey,804    recovery_nonce: wrappedNonce,805    recovery_tag: wrappedTag,806  };807  await writeSlot(SLOT_A, slotA);808809  // Matching-size noise blob for slot B, so a fresh install shows two810  // similarly-shaped records regardless of whether duress is ever set up.811  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));812  await stampLocalVaultMutation();813814  touchAutoLock();815}816817export async function hasVault(): Promise<boolean> {818  await ensureMigrated();819  return (await loadSlot(SLOT_A)) !== null;820}821822/**823 * Returns whether the real vault has recorded that a duress passphrase is824 * configured. Reliable only after a real-mode unlock — before unlock or in825 * decoy mode, returns false because we cannot read slot A's plaintext.826 */827export async function isDuressEnabled(): Promise<boolean> {828  return vaultMode === "real" && duressConfigured;829}830831export async function setupDuress(duressPassword: string): Promise<void> {832  if (!derivedKey || vaultMode !== "real") {833    throw new Error("vault must be unlocked in real mode");834  }835  await ensureMigrated();836837  // Encrypt the decoy payload (empty entries) under the duress key. Pad to838  // at least the current slot A ciphertext size so the two slots look the839  // same at rest at setup time.840  const slotA = await loadSlot(SLOT_A);841  if (!slotA) throw new Error("missing slot A");842843  const decoySalt = generateSalt();844  const decoyKey = await deriveKey(duressPassword, decoySalt);845  try {846    // Pad the decoy plaintext to match slot A's ciphertext length (minus the847    // 16-byte tag) so encrypted sizes align exactly.848    const targetBytes = slotA.ciphertext.length / 2;849    let plaintext = encodePayload([], false);850    const encoded = new TextEncoder().encode(plaintext);851    if (encoded.length < targetBytes) {852      plaintext = plaintext + " ".repeat(targetBytes - encoded.length);853    }854    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext);855    await writeSlot(SLOT_B, {856      version: 1,857      salt: hex(decoySalt),858      nonce,859      ciphertext,860      tag,861    });862  } finally {863    zeroKey(decoyKey);864  }865866  // Re-encrypt slot A with the duressConfigured flag set to true. No external867  // flag is written — the bit lives inside the encrypted envelope.868  duressConfigured = true;869  await persistSlotA();870}871872export async function disableDuress(): Promise<void> {873  if (!derivedKey || vaultMode !== "real") {874    throw new Error("vault must be unlocked in real mode");875  }876  await ensureMigrated();877878  // Overwrite slot B with fresh noise that matches slot A's size. The slot879  // remains occupied so its mere existence does not signal that duress was880  // ever configured.881  const slotA = await loadSlot(SLOT_A);882  if (slotA) {883    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));884  }885886  duressConfigured = false;887  await persistSlotA();888}889890export async function verifyMainPassword(password: string): Promise<boolean> {891  await ensureMigrated();892  const slotA = await loadSlot(SLOT_A);893  if (!slotA) return false;894  const key = await deriveKey(password, unhex(slotA.salt));895  try {896    return (await tryDecryptSlot(slotA, key)) !== null;897  } finally {898    zeroKey(key);899  }900}901902export async function unlock(password: string): Promise<boolean> {903  await ensureMigrated();904905  // Load both slots in parallel. Both always exist post-migration; the906  // parallel load and the parallel derivation below keep wall-clock time907  // constant regardless of which slot's passphrase (real or decoy) the908  // caller supplied, so a timing side channel cannot distinguish the909  // duress path from the real-unlock path (DUR-002).910  const [slotA, slotB] = await Promise.all([911    loadSlot(SLOT_A),912    loadSlot(SLOT_B),913  ]);914915  // Derive both keys in parallel, unconditionally.916  const [keyA, keyB] = await Promise.all([917    slotA ? deriveKey(password, unhex(slotA.salt)) : Promise.resolve(null),918    slotB ? deriveKey(password, unhex(slotB.salt)) : Promise.resolve(null),919  ]);920921  // Attempt both decrypts in parallel.922  const [payloadA, payloadB] = await Promise.all([923    keyA && slotA ? tryDecryptSlot(slotA, keyA) : Promise.resolve(null),924    keyB && slotB ? tryDecryptSlot(slotB, keyB) : Promise.resolve(null),925  ]);926927  if (payloadA && keyA) {928    derivedKey = keyA;929    vaultMode = "real";930    entries = payloadA.entries;931    duressConfigured = payloadA.duressConfigured;932    buildDomainIndexSync(entries);933    await loadSettings();934    touchAutoLock();935    if (keyB) zeroKey(keyB);936937    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).938    if (slotA) {939      const needsRecoverySetup =940        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;941      if (!needsRecoverySetup) {942        rootKey = await deriveRootKeyFromDerivedKey(943          keyA,944          slotA.recovery_salt!945        );946      }947    }948949    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat950    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so951    // the next save uses the new format and migration doesn't re-run forever.952    if (payloadA.migratedFromLegacy) {953      await persistSlotA();954    }955    return true;956  }957958  if (payloadB && keyB) {959    derivedKey = keyB;960    vaultMode = "decoy";961    entries = payloadB.entries;962    duressConfigured = false;963    buildDomainIndexSync(entries);964    await loadSettings();965    touchAutoLock();966    if (keyA) zeroKey(keyA);967    return true;968  }969970  if (keyA) zeroKey(keyA);971  if (keyB) zeroKey(keyB);972  return false;973}974975export function lock(): void {976  if (derivedKey) {977    zeroKey(derivedKey);978    derivedKey = null;979  }980  if (rootKey) {981    rootKey.fill(0);982    rootKey = null;983  }984985  entries = [];986  domainIndex = new Map();987  vaultMode = "real";988  duressConfigured = false;989  clearAutoLock();990}991992export function isUnlocked(): boolean {993  return derivedKey !== null;994}995996// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).997// Used by Android biometric unlock — the key was derived on the native side.998export async function unlockWithKey(999  masterKeyHex: string,1000): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {1001  try {1002    const masterKeyBytes = unhex(masterKeyHex);1003    const stored = await loadSlot(SLOT_A);1004    if (!stored) return { ok: false, error: "invalid_key" };1005    const payload = await tryDecryptSlot(stored, masterKeyBytes);1006    if (!payload) return { ok: false, error: "invalid_key" };10071008    derivedKey = masterKeyBytes;1009    vaultMode = "real";1010    entries = payload.entries;1011    duressConfigured = payload.duressConfigured;1012    buildDomainIndexSync(entries);1013    await loadSettings();1014    touchAutoLock();1015    return { ok: true, entries };1016  } catch {1017    return { ok: false, error: "invalid_key" };1018  }1019}10201021/**1022 * Restore the vault from a parsed Peach Codex payload.1023 *1024 * The supplied passphrase is verified against the Codex's slotA *before* any1025 * storage mutation — wrong passphrases return without touching disk. On1026 * success, the current vault state is snapshotted (reason: "restore") so the1027 * user can revert via Settings -> Vault History, then slotA/slotB are written1028 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1029 * regular `unlock()` follows so the rest of the SW sees the standard1030 * post-unlock state.1031 *1032 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1033 * printed by a Pro user must always be restorable, even on a free device.1034 *1035 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1036 * is an explicit user action with a known passphrase, not a passive lock1037 * screen, so the duress-vs-real timing side channel does not apply.1038 */1039export async function unlockFromCodex(params: {1040  slotA: string; // JSON string of StoredVault for slot A1041  slotB: string | null; // JSON string of StoredVault for slot B, or null1042  timestamp: number;1043  masterPassphrase: string;1044}): Promise<1045  | { ok: true; entryCount: number }1046  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1047> {1048  let parsedSlotA: StoredVault;1049  let parsedSlotB: StoredVault | null = null;1050  try {1051    parsedSlotA = JSON.parse(params.slotA) as StoredVault;1052    if (params.slotB !== null) {1053      parsedSlotB = JSON.parse(params.slotB) as StoredVault;1054    }1055    // Minimal structural validation — the decrypt step below catches deeper1056    // corruption, but bail early on obviously-bad shapes so we don't reach1057    // storage with junk.1058    if (1059      typeof parsedSlotA?.salt !== "string" ||1060      typeof parsedSlotA?.nonce !== "string" ||1061      typeof parsedSlotA?.ciphertext !== "string" ||1062      typeof parsedSlotA?.tag !== "string"1063    ) {1064      return { ok: false, reason: "invalid_slot_format" };1065    }1066  } catch {1067    return { ok: false, reason: "invalid_slot_format" };1068  }10691070  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1071  // If this fails the on-disk vault is left completely untouched.1072  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1073  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey);1074  if (!trialPayload) {1075    zeroKey(trialKey);1076    return { ok: false, reason: "wrong_passphrase" };1077  }1078  // Drop the trial key; `unlock()` below derives its own copy. Holding two1079  // copies of the master key in memory at once would be a needless surface.1080  zeroKey(trialKey);10811082  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1083  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1084  await takeVaultSnapshot("restore");10851086  await writeSlot(SLOT_A, parsedSlotA);1087  if (parsedSlotB) {1088    await writeSlot(SLOT_B, parsedSlotB);1089  } else {1090    // Slot B must always exist with ciphertext length matching slot A1091    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1092    // configured); regenerate noise to satisfy the invariant.1093    await writeSlot(1094      SLOT_B,1095      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1096    );1097  }1098  await chrome.storage.local.set({1099    peach_last_modified_at: String(params.timestamp),1100  });11011102  // Standard unlock path so domain index, settings, auto-lock, and1103  // recovery-key state all populate exactly as a normal unlock would.1104  const ok = await unlock(params.masterPassphrase);1105  if (!ok) {1106    // Should be unreachable — we just verified the passphrase against the1107    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1108    // simple if some race or storage anomaly intrudes.1109    return { ok: false, reason: "wrong_passphrase" };1110  }1111  return { ok: true, entryCount: getEntries().length };1112}11131114// Returns the current in-memory derived key as a hex string.1115// Used by the biometric enable flow in Settings to store the key in Android Keystore.1116export function getDerivedKeyHex(): string {1117  if (!derivedKey) throw new Error("vault_not_unlocked");1118  return hex(derivedKey);1119}11201121export function getEntries(): VaultEntry[] {1122  if (!derivedKey) throw new Error("vault locked");1123  return entries.filter((entry) => !entry.deletedAt);1124}11251126const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;11271128export function getIdentities(): SavedIdentity[] {1129  if (!derivedKey) throw new Error("vault locked");1130  const active = entries.filter((e) => !e.deletedAt && e.type === "login");11311132  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1133  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();11341135  for (const entry of active) {1136    const value = entry.username?.trim();1137    if (!value) continue;1138    const map = EMAIL_RE.test(value) ? emailMap : usernameMap;1139    const existing = map.get(value);1140    const domain = extractDomain(entry.url ?? "") ?? "";1141    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;1142    if (!existing || modifiedAt > existing.lastUsed) {1143      map.set(value, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1144    } else {1145      map.set(value, { ...existing, count: existing.count + 1 });1146    }1147  }11481149  const toIdentity = (type: "email" | "username") =>1150    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1151      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1152    });11531154  return [1155    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1156    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1157  ];1158}11591160export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1161  if (!derivedKey) throw new Error("vault locked");11621163  const now = Date.now();1164  const added: VaultEntry[] = [];1165  for (const draft of drafts) {1166    try {1167      const sanitized = sanitizeEntryDraft(draft);1168      const nextEntry: VaultEntry = {1169        ...sanitized,1170        id: crypto.randomUUID(),1171        createdAt: now,1172        modifiedAt: now,1173      };1174      entries.push(nextEntry);1175      added.push(nextEntry);1176    } catch {1177      // Skip invalid drafts silently — caller can see diff between input and output length1178    }1179  }11801181  if (added.length > 0) {1182    await persistEntries();1183  }11841185  return added;1186}11871188/**1189 * Restore a set of entries in full fidelity — preserves id, createdAt,1190 * modifiedAt, and every other field unlike `batchAddEntries` which1191 * assigns fresh identity. Used by restore paths where the backup is the1192 * authoritative source of truth. `mode` controls how entries already in1193 * the vault are treated.1194 */1195export async function restoreEntries(1196  incoming: VaultEntry[],1197  mode: "replace" | "merge"1198): Promise<{ added: number; updated: number; skipped: number }> {1199  if (!derivedKey) throw new Error("vault locked");12001201  // A replace with zero incoming entries is almost certainly a caller bug1202  // (e.g. an import that silently dropped everything). Refuse rather than1203  // wipe the vault.1204  if (mode === "replace" && incoming.length === 0) {1205    throw new Error("refuse to replace with empty entry set");1206  }12071208  let added = 0;1209  let updated = 0;1210  let skipped = 0;12111212  if (mode === "replace") {1213    entries = [];1214    for (const entry of incoming) {1215      const normalized = sanitizeRestoredEntry(entry);1216      if (!normalized) {1217        skipped += 1;1218        continue;1219      }1220      entries.push(normalized);1221      added += 1;1222    }1223  } else {1224    const byId = new Map<string, VaultEntry>();1225    for (const e of entries) byId.set(e.id, e);12261227    for (const entry of incoming) {1228      const normalized = sanitizeRestoredEntry(entry);1229      if (!normalized) {1230        skipped += 1;1231        continue;1232      }1233      const existing = byId.get(normalized.id);1234      if (!existing) {1235        entries.push(normalized);1236        added += 1;1237      } else if (normalized.modifiedAt > existing.modifiedAt) {1238        const idx = entries.findIndex((e) => e.id === existing.id);1239        if (idx !== -1) {1240          entries[idx] = normalized;1241          updated += 1;1242        }1243      } else {1244        skipped += 1;1245      }1246    }1247  }12481249  await persistEntries("import");1250  return { added, updated, skipped };1251}12521253/**1254 * Validate + normalise a VaultEntry coming from a backup. Accepts1255 * anything structurally sound; keeps all metadata verbatim. Returns1256 * null for values the live vault shouldn't accept (e.g., missing title).1257 */1258function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1259  if (!entry || typeof entry !== "object") return null;1260  const title = typeof entry.title === "string" ? entry.title.trim() : "";1261  if (!title) return null;1262  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1263  const now = Date.now();1264  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1265  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;12661267  // Migrate legacy passkey shape on import (same logic as on-disk read).1268  // Imports from pre-v2.11.0 vault backups still carry type:"passkey" with1269  // flat fields; bring them into the new shape immediately.1270  const migrated: VaultEntry = migrateLegacyPasskey(1271    {1272      ...entry,1273      id,1274      title,1275      type: entry.type ?? "login",1276      createdAt,1277      modifiedAt,1278    },1279    entry1280  );12811282  // Login entries from backup must carry a password (or be passkey-only,1283  // in which case the passkey sub-object replaces the password requirement).1284  if (1285    migrated.type === "login" &&1286    !migrated.passkey &&1287    typeof migrated.password !== "string"1288  ) {1289    return null;1290  }1291  if (migrated.passkey && !migrated.passkey.credentialId) {1292    return null;1293  }12941295  return migrated;1296}12971298// Soft-deletes every non-deleted entry in one call. The literal string1299// argument is a tripwire against accidental calls — any caller that would1300// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1301// to a boolean.1302export async function clearVaultEntries(1303  confirmation: "REPLACE_ALL"1304): Promise<void> {1305  if (confirmation !== "REPLACE_ALL") {1306    throw new Error("clearVaultEntries requires explicit confirmation");1307  }1308  if (!derivedKey) throw new Error("vault locked");1309  for (const entry of entries) {1310    if (!entry.deletedAt) {1311      entry.deletedAt = Date.now();1312      entry.modifiedAt = Date.now();1313    }1314  }1315  await persistEntries();1316}13171318export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1319  if (!derivedKey) throw new Error("vault locked");13201321  const sanitized = sanitizeEntryDraft(entry);1322  const now = Date.now();1323  const nextEntry: VaultEntry = {1324    ...sanitized,1325    id: crypto.randomUUID(),1326    createdAt: now,1327    modifiedAt: now,1328    // Stamp passwordChangedAt at creation so age tracking is accurate from1329    // day one. Non-login entries carry the same stamp harmlessly.1330    passwordChangedAt: sanitized.password ? now : undefined,1331  };13321333  entries.push(nextEntry);1334  await persistEntries();13351336  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1337  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1338    checkBreach(nextEntry.password).then(async (count) => {1339      if (count > 0) {1340        const idx = entries.findIndex((e) => e.id === nextEntry.id);1341        if (idx !== -1) {1342          entries[idx] = { ...entries[idx], breach: count, modifiedAt: Date.now() };1343          await persistEntries();1344        }1345      }1346    });1347  }13481349  return nextEntry;1350}13511352export async function updateEntry(1353  id: string,1354  patch: Partial<VaultEntry>1355): Promise<void> {1356  if (!derivedKey) throw new Error("vault locked");13571358  const idx = entries.findIndex((entry) => entry.id === id);1359  if (idx === -1 || entries[idx].deletedAt) {1360    throw new Error("entry not found");1361  }13621363  const current = entries[idx];1364  const sanitizedPatch = sanitizeEntryPatch(patch);1365  // Refresh passwordChangedAt only when the password value actually changes —1366  // a metadata-only update (title, tags, etc.) must not reset the age clock1367  // that drives the Pro health rotation recommendation.1368  const passwordChanged =1369    "password" in patch &&1370    typeof sanitizedPatch.password === "string" &&1371    sanitizedPatch.password !== current.password;1372  entries[idx] = {1373    ...current,1374    ...sanitizedPatch,1375    modifiedAt: Date.now(),1376    ...(passwordChanged ? { passwordChangedAt: Date.now() } : {}),1377  };13781379  // A login entry must carry SOMETHING fillable — either a password or a1380  // passkey credential. An empty-password login with no passkey is dead1381  // state. Permit the password-clear when a passkey is attached (that's1382  // the legitimate "drop the password, keep the passkey" path).1383  if (1384    entries[idx].type === "login" &&1385    !entries[idx].password &&1386    !entries[idx].passkey1387  ) {1388    entries[idx] = current;1389    throw new Error("password or passkey required");1390  }13911392  // Card entries must retain their card number1393  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1394    entries[idx] = current;1395    throw new Error("card number is required for card entries");1396  }13971398  await persistEntries();13991400  // Breach check when password changes: clear immediately so old breach doesn't linger,1401  // then async-check the new password and only re-set if it's also found.1402  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1403    entries[idx] = { ...entries[idx], breach: undefined };1404    await persistEntries();1405    const password = entries[idx].password;1406    if (password) {1407      checkBreach(password).then(async (count) => {1408        const i = entries.findIndex((e) => e.id === id);1409        if (i !== -1 && count > 0) {1410          entries[i] = { ...entries[i], breach: count, modifiedAt: Date.now() };1411          await persistEntries();1412        }1413      });1414    }1415  }1416}14171418export async function deleteEntry(id: string): Promise<void> {1419  if (!derivedKey) throw new Error("vault locked");14201421  const entry = entries.find((item) => item.id === id);1422  if (!entry || entry.deletedAt) {1423    throw new Error("entry not found");1424  }14251426  entry.deletedAt = Date.now();1427  entry.modifiedAt = Date.now();1428  await persistEntries();1429}14301431export function getDeletedEntries(): VaultEntry[] {1432  if (!derivedKey) return [];1433  return entries.filter((e) => !!e.deletedAt);1434}14351436export async function restoreEntry(id: string): Promise<void> {1437  if (!derivedKey) throw new Error("vault locked");1438  const entry = entries.find((e) => e.id === id && e.deletedAt);1439  if (!entry) throw new Error("deleted entry not found");1440  delete entry.deletedAt;1441  entry.modifiedAt = Date.now();1442  await persistEntries();1443  buildDomainIndexSync(entries);1444}14451446export async function purgeEntry(id: string): Promise<void> {1447  if (!derivedKey) throw new Error("vault locked");1448  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);1449  if (idx === -1) throw new Error("deleted entry not found");1450  entries.splice(idx, 1);1451  await persistEntries();1452}14531454/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */1455export async function purgeExpiredTrash(retentionMs: number): Promise<number> {1456  if (!derivedKey || retentionMs === 0) return 0;1457  const cutoff = Date.now() - retentionMs;1458  const before = entries.length;1459  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));1460  const purged = before - entries.length;1461  if (purged > 0) await persistEntries();1462  return purged;1463}14641465export function queryDomain(domain: string): VaultEntry[] {1466  return domainIndex.get(domain) ?? [];1467}14681469export function getEntryById(id: string): VaultEntry | undefined {1470  return entries.find((entry) => entry.id === id && !entry.deletedAt);1471}14721473async function persistSlotA(1474  reason: VaultSnapshot["reason"] = "local_save",1475): Promise<StoredVault> {1476  if (!derivedKey) throw new Error("vault locked");14771478  const slotA = await loadSlot(SLOT_A);1479  if (!slotA) throw new Error("missing slot A");14801481  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures1482  // are swallowed by takeVaultSnapshot — history must never block a save.1483  await takeVaultSnapshot(reason);14841485  const plaintext = encodePayload(entries, duressConfigured);1486  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);14871488  // Preserve existing recovery escrow fields.1489  const nextSlotA: StoredVault = {1490    ...slotA,1491    nonce,1492    ciphertext,1493    tag,1494    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot1495  };1496  await writeSlot(SLOT_A, nextSlotA);14971498  // When duress is not configured, keep slot B size-matched with slot A by1499  // regenerating its noise. When duress IS configured, leave slot B alone —1500  // we don't have the decoy key to re-encrypt it. This is a known trade-off.1501  if (!duressConfigured) {1502    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));1503  }1504  await stampLocalVaultMutation();15051506  return nextSlotA;1507}15081509async function persistSlotB(): Promise<void> {1510  if (!derivedKey) throw new Error("vault locked");15111512  const slotB = await loadSlot(SLOT_B);1513  if (!slotB) throw new Error("missing slot B");15141515  // Decoy plaintext always reports duressConfigured=false — the decoy user1516  // sees a clean slate.1517  const plaintext = encodePayload(entries, false);1518  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext);15191520  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });1521  await stampLocalVaultMutation();1522}15231524async function persistEntries(1525  reason: VaultSnapshot["reason"] = "local_save",1526): Promise<void> {1527  if (!derivedKey) throw new Error("vault locked");15281529  if (vaultMode === "decoy") {1530    await persistSlotB();1531  } else {1532    await persistSlotA(reason);1533  }1534  buildDomainIndexSync(entries);1535}15361537function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {1538  if (!Array.isArray(values)) return undefined;1539  const seen = new Set<string>();1540  const cleaned: string[] = [];1541  for (const value of values) {1542    const trimmed = typeof value === "string" ? value.trim() : "";1543    if (!trimmed) continue;1544    const key = trimmed.toLowerCase();1545    if (seen.has(key)) continue;1546    seen.add(key);1547    cleaned.push(trimmed);1548  }1549  return cleaned.length > 0 ? cleaned : undefined;1550}15511552function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {1553  const title = entry.title.trim();1554  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new1555  // model is "any entry, optionally with a passkey sub-object." Imports and1556  // any in-flight callers from before v2.11.0 keep working without throwing.1557  let type: VaultEntry["type"] = entry.type ?? "login";1558  if (type === "passkey") type = "login";15591560  if (!title) throw new Error("title is required");1561  // A login entry needs either a password or a passkey credential to be useful.1562  // Passkey-only entries (passkey present, no password) are valid.1563  if (type === "login" && !entry.password?.trim() && !entry.passkey) {1564    throw new Error("password or passkey required");1565  }1566  if (entry.passkey) {1567    const p = entry.passkey;1568    if (1569      !p.rpId ||1570      !p.credentialId ||1571      !p.publicKeyCose ||1572      !p.userHandle ||1573      !p.userName ||1574      (p.algorithm !== -7 && p.algorithm !== -8)1575    ) {1576      throw new Error("invalid passkey credential");1577    }1578  }1579  if (type === "card") {1580    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {1581      throw new Error("cardholder name and card number are required");1582    }1583  }15841585  return {1586    type,1587    title,1588    username: type === "login" ? entry.username?.trim() ?? "" : undefined,1589    password: type === "login" ? entry.password : undefined,1590    url: type === "login" ? entry.url?.trim() ?? "" : undefined,1591    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,1592    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,1593    notes: entry.notes?.trim() || undefined,1594    breach: type === "login" ? entry.breach : undefined,1595    canary: type === "login" ? entry.canary : undefined,1596    tags: entry.tags,1597    passkey: entry.passkey,1598    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,1599    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,1600    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,1601    expYear: type === "card" ? entry.expYear?.trim() : undefined,1602    cvc: type === "card" ? entry.cvc?.trim() : undefined,1603    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,1604    network: type === "card" ? entry.network : undefined,1605    last4: type === "card" ? entry.last4?.trim() : undefined,1606  };1607}16081609function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {1610  const nextPatch: Partial<VaultEntry> = {};16111612  if ("type" in patch) {1613    nextPatch.type = patch.type;1614  }16151616  if ("title" in patch) {1617    const title = patch.title?.trim() ?? "";1618    if (!title) throw new Error("title is required");1619    nextPatch.title = title;1620  }16211622  if ("url" in patch) {1623    nextPatch.url = patch.url?.trim() ?? "";1624  }16251626  if ("additionalUrls" in patch) {1627    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];1628    const seen = new Set<string>();1629    const cleaned: string[] = [];1630    for (const value of incoming) {1631      const trimmed = typeof value === "string" ? value.trim() : "";1632      if (!trimmed) continue;1633      const key = trimmed.toLowerCase();1634      if (seen.has(key)) continue;1635      seen.add(key);1636      cleaned.push(trimmed);1637    }1638    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;1639  }16401641  if ("username" in patch) {1642    nextPatch.username = patch.username?.trim() ?? "";1643  }16441645  if ("email" in patch) {1646    nextPatch.email = patch.email?.trim() || undefined;1647  }16481649  if ("password" in patch) {1650    const password = patch.password ?? "";1651    // Allow clearing a password (e.g. on passkey-only entries)1652    nextPatch.password = password || undefined;1653  }16541655  if ("totp" in patch) {1656    nextPatch.totp = patch.totp?.trim() || undefined;1657  }16581659  if ("notes" in patch) {1660    nextPatch.notes = patch.notes?.trim() || undefined;1661  }16621663  if ("breach" in patch) {1664    nextPatch.breach = patch.breach;1665  }16661667  if ("canary" in patch) {1668    nextPatch.canary = patch.canary;1669  }16701671  if ("passkey" in patch) {1672    // Passkey sub-object replaces all the legacy flat passkey fields. A1673    // patch with `passkey: undefined` removes the credential from the entry1674    // (used by "remove passkey" without deleting the whole entry).1675    nextPatch.passkey = patch.passkey;1676  }16771678  if ("tags" in patch) {1679    nextPatch.tags = patch.tags;1680  }16811682  if ("cardholderName" in patch) {1683    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;1684  }1685  if ("cardNumber" in patch) {1686    const n = patch.cardNumber ?? "";1687    nextPatch.cardNumber = n || undefined;1688  }1689  if ("expMonth" in patch) {1690    nextPatch.expMonth = patch.expMonth?.trim() || undefined;1691  }1692  if ("expYear" in patch) {1693    nextPatch.expYear = patch.expYear?.trim() || undefined;1694  }1695  if ("cvc" in patch) {1696    nextPatch.cvc = patch.cvc?.trim() || undefined;1697  }1698  if ("cardNickname" in patch) {1699    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;1700  }1701  if ("network" in patch) {1702    nextPatch.network = patch.network;1703  }1704  if ("last4" in patch) {1705    nextPatch.last4 = patch.last4?.trim() || undefined;1706  }17071708  return nextPatch;1709}17101711function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {1712  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;1713  const modifiedAt =1714    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;17151716  const base: VaultEntry = {1717    id: entry.id,1718    type: "type" in entry ? entry.type : "login",1719    title: entry.title,1720    username: entry.username,1721    password: entry.password,1722    url: entry.url,1723    totp: entry.totp,1724    notes: entry.notes,1725    breach: entry.breach,1726    canary: "canary" in entry ? entry.canary : undefined,1727    passkey: "passkey" in entry ? entry.passkey : undefined,1728    createdAt,1729    modifiedAt,1730    deletedAt: entry.deletedAt,1731    tags: "tags" in entry ? entry.tags : undefined,1732    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,1733    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,1734    expMonth: "expMonth" in entry ? entry.expMonth : undefined,1735    expYear: "expYear" in entry ? entry.expYear : undefined,1736    cvc: "cvc" in entry ? entry.cvc : undefined,1737    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,1738    network: "network" in entry ? entry.network : undefined,1739    last4: "last4" in entry ? entry.last4 : undefined,1740  };17411742  return migrateLegacyPasskey(base, entry);1743}17441745/**1746 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`1747 * with flat top-level fields (rpId, credentialId, etc.). This converts them1748 * to the new shape — `type:"login"` with a `passkey` sub-object — so all1749 * downstream consumers can rely on the new model. The result is rewritten1750 * to disk on next vault save (hasLegacyShape detects the un-migrated case).1751 */1752function migrateLegacyPasskey(1753  base: VaultEntry,1754  raw: VaultEntry | LegacyVaultEntry1755): VaultEntry {1756  if (base.passkey) return base;1757  const flat = raw as Partial<VaultEntry>;1758  const isPasskeyType = base.type === "passkey";1759  const hasFlatPasskeyFields =1760    typeof flat.rpId === "string" &&1761    typeof flat.credentialId === "string" &&1762    typeof flat.publicKeyCose === "string" &&1763    typeof flat.userHandle === "string" &&1764    typeof flat.userName === "string";1765  if (!isPasskeyType && !hasFlatPasskeyFields) return base;1766  if (!hasFlatPasskeyFields) return base;17671768  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;1769  base.passkey = {1770    rpId: flat.rpId!,1771    rpName: flat.rpName ?? flat.rpId!,1772    credentialId: flat.credentialId!,1773    publicKeyCose: flat.publicKeyCose!,1774    privateKeyJwk: flat.privateKeyJwk,1775    userHandle: flat.userHandle!,1776    userName: flat.userName!,1777    userDisplayName: flat.userDisplayName ?? flat.userName!,1778    algorithm,1779    signCount: flat.signCount ?? 0,1780    aaguid: flat.aaguid ?? "",1781    backupEligible: flat.backupEligible === true,1782    backupState: flat.backupState === true,1783    lastUsedAt: flat.lastUsedAt,1784    browser: flat.browser,1785    transports: flat.transports,1786    createdAt: base.createdAt,1787  };1788  // Promote to "login" so the unified Entry view renders. Username gets1789  // populated from the passkey's userName so the entry is searchable in the1790  // existing autofill flow without a separate code path.1791  if (isPasskeyType) base.type = "login";1792  if (!base.username) base.username = flat.userName;1793  if (!base.url) base.url = `https://${flat.rpId}`;1794  return base;1795}179617971798// ---------------------------------------------------------------------------1799// Deduplication1800// ---------------------------------------------------------------------------18011802function entryDupeKey(e: VaultEntry): string {1803  if (e.type === "note") return `note|${e.title.toLowerCase()}`;1804  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins1805  // on different sites that happen to share a username are still distinct;1806  // two passkey credentials for the same RP+user are duplicates regardless1807  // of any login fields they may also carry.1808  if (e.passkey) {1809    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;1810  }1811  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;1812}18131814/** Returns the number of duplicate entries (those that would be removed). */1815export function countDuplicates(): number {1816  const best = new Map<string, VaultEntry>();1817  let dupeCount = 0;1818  for (const e of entries.filter((e) => !e.deletedAt)) {1819    const key = entryDupeKey(e);1820    const existing = best.get(key);1821    if (!existing) {1822      best.set(key, e);1823    } else {1824      dupeCount++;1825      // Keep the more recently modified one in the map1826      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1827        best.set(key, e);1828      }1829    }1830  }1831  return dupeCount;1832}18331834/**1835 * Soft-deletes duplicate entries, keeping the most recently modified copy of1836 * each group. Returns the number of entries moved to trash.1837 */1838export async function deduplicateEntries(): Promise<number> {1839  if (!derivedKey) throw new Error("vault locked");1840  // Dedup reshapes slot B permanently under the decoy key, which is1841  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.1842  if (vaultMode === "decoy") {1843    throw new Error("deduplicate disabled in decoy mode");1844  }18451846  const best = new Map<string, VaultEntry>();1847  for (const e of entries.filter((en) => !en.deletedAt)) {1848    const key = entryDupeKey(e);1849    const existing = best.get(key);1850    if (!existing) {1851      best.set(key, e);1852    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {1853      best.set(key, e);1854    }1855  }18561857  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));1858  const now = Date.now();1859  let removed = 0;1860  entries = entries.map((e) => {1861    if (!e.deletedAt && !keepIds.has(e.id)) {1862      removed++;1863      return { ...e, deletedAt: now };1864    }1865    return e;1866  });18671868  if (removed > 0) await persistEntries();1869  return removed;1870}18711872// ---------------------------------------------------------------------------1873// Vault history — snapshot BEFORE every vault write so the saved blob is1874// what is about to be lost, not what was just written. Capped at 10. The1875// stored blobs are AES-256-GCM ciphertext under the master password, so1876// history adds zero security surface.1877// ---------------------------------------------------------------------------18781879const HISTORY_KEY = "peach_vault_history";1880const HISTORY_MAX = 10;18811882/**1883 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE1884 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost1885 * state. Identical-to-most-recent blobs are skipped to avoid filling history1886 * with no-op writes.1887 */1888export async function takeVaultSnapshot(1889  reason: VaultSnapshot["reason"],1890): Promise<void> {1891  try {1892    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);1893    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];1894    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];18951896    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,1897    // pre-createVault). createVault skips snapshots intentionally — there is1898    // no prior state to preserve.1899    if (!slotA) return;19001901    const vault_a = JSON.stringify(slotA);1902    const vault_b = JSON.stringify(slotB ?? slotA);19031904    // Best-effort entry count: only meaningful when the in-memory state is1905    // unlocked AND in real mode. Decoy mode would leak the decoy entry1906    // count; locked mode has no entries to count. Both fall through to null.1907    let entryCount: number | null = null;1908    if (derivedKey && vaultMode === "real") {1909      try {1910        entryCount = entries.filter((e) => !e.deletedAt).length;1911      } catch {1912        entryCount = null;1913      }1914    }19151916    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];1917    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)1918      ? existingRaw1919      : { snapshots: [] };19201921    // Deduplication: if the most recent snapshot has byte-identical blobs,1922    // skip — saves the same vault N times in a row would otherwise burn the1923    // entire history window with no recovery value.1924    const latest = history.snapshots[0];1925    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {1926      return;1927    }19281929    const snapshot: VaultSnapshot = {1930      id: crypto.randomUUID(),1931      savedAt: Date.now(),1932      reason,1933      vault_a,1934      vault_b,1935      entryCount,1936    };19371938    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);1939    await chrome.storage.local.set({ [HISTORY_KEY]: history });1940  } catch {1941    // Non-fatal — history must never block a save. A failed snapshot just1942    // means the user loses one rung of the safety ladder for this write.1943  }1944}19451946/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */1947export async function clearVaultHistory(): Promise<void> {1948  try {1949    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });1950  } catch {1951    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().1952  }1953}19541955/** Read all snapshots, newest first, for the Settings vault history view. */1956export async function getVaultHistory(): Promise<VaultSnapshot[]> {1957  const raw = await chrome.storage.local.get(HISTORY_KEY);1958  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];1959  if (!history || !Array.isArray(history.snapshots)) return [];1960  return history.snapshots;1961}19621963/**1964 * Replace the on-disk vault with the chosen snapshot's blobs and lock the1965 * vault. The current state is itself snapshotted with reason 'restore' first1966 * so the restore is reversible. After this returns, the user must unlock1967 * with the password that matches the restored blob.1968 */1969export async function restoreVaultSnapshot(1970  snapshotId: string,1971): Promise<{ ok: true } | { ok: false; error: string }> {1972  const history = await getVaultHistory();1973  const snapshot = history.find((s) => s.id === snapshotId);1974  if (!snapshot) return { ok: false, error: "snapshot_not_found" };19751976  // Snapshot the about-to-be-replaced current state so the restore is1977  // reversible. takeVaultSnapshot is no-op when the most recent entry is1978  // byte-identical, so a restore-of-a-restore stays clean.1979  await takeVaultSnapshot("restore");19801981  let restoredA: StoredVault;1982  let restoredB: StoredVault;1983  try {1984    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;1985    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;1986  } catch {1987    return { ok: false, error: "snapshot_corrupt" };1988  }19891990  await writeSlot(SLOT_A, restoredA);1991  await writeSlot(SLOT_B, restoredB);1992  await stampLocalVaultMutation();19931994  // Lock so the user must prove they can decrypt the restored blob. If they1995  // restored a pre-password-change snapshot they discover it immediately at1996  // the lock screen rather than silently holding an inaccessible vault.1997  lock();19981999  return { ok: true };2000}20012002/**2003 * Change the master password. Re-encrypts slot A with a fresh salt + new2004 * derived key. Slot B (duress or noise) is left untouched when duress is2005 * configured — it uses an independent key derived from the duress password —2006 * and regenerated as noise when duress is not configured.2007 */2008export async function changePassword(2009  currentPassword: string,2010  newPassword: string2011): Promise<boolean> {2012  if (!derivedKey || vaultMode !== "real") return false;20132014  // Verify current password before allowing any change2015  const valid = await verifyMainPassword(currentPassword);2016  if (!valid) return false;20172018  const slotA = await loadSlot(SLOT_A);2019  if (!slotA) return false;20202021  // Derive fresh key from new password + new random salt2022  const newSalt = generateSalt();2023  const newKey = await deriveKey(newPassword, newSalt);20242025  // Re-encrypt the in-memory entries with the new key2026  const plaintext = encodePayload(entries, duressConfigured);2027  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext);20282029  const freshSlotA: StoredVault = {2030    ...slotA,2031    salt: hex(newSalt),2032    nonce,2033    ciphertext,2034    tag,2035  };2036  // Snapshot the OLD-key blob before overwriting it. If the user later2037  // restores from this snapshot they will need the OLD password — the2038  // post-restore lock surfaces that immediately.2039  await takeVaultSnapshot("local_save");2040  await writeSlot(SLOT_A, freshSlotA);20412042  // Swap the in-memory key synchronously before any further await. If we2043  // threw between the Slot A write and this swap, the next persistEntries()2044  // would overwrite Slot A with old-key ciphertext, locking the user out.2045  zeroKey(derivedKey);2046  derivedKey = newKey;20472048  // Rotate the root key so the old recovery words are immediately invalidated.2049  // After this point getRecoveryWords() returns a fresh 12-word set.2050  const {2051    rawRootKey: freshRawKey,2052    recoverySalt: freshSalt,2053    wrappedRootKey: freshWrapped,2054    wrappedNonce: freshNonce,2055    wrappedTag: freshTag,2056  } = await generateAndWrapRootKey();2057  rootKey = unhex(freshRawKey);2058  const slotAfterRekey = await loadSlot(SLOT_A);2059  if (slotAfterRekey) {2060    await writeSlot(SLOT_A, {2061      ...slotAfterRekey,2062      recovery_salt: freshSalt,2063      recovery_wrapped_root_key: freshWrapped,2064      recovery_nonce: freshNonce,2065      recovery_tag: freshTag,2066    });2067  }20682069  // Regenerate noise for slot B when duress is not in use. Runs after the2070  // key swap so a failure here is a disposable noise regeneration, not a2071  // lockout.2072  if (!duressConfigured) {2073    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2074  }2075  await stampLocalVaultMutation();20762077  return true;2078}20792080// ---------------------------------------------------------------------------2081// Sync helpers2082// ---------------------------------------------------------------------------20832084export async function getVaultBlobForSync(): Promise<{2085  blob: string;2086  timestamp: number;2087  license?: string;2088} | null> {2089  const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, 'peach_last_modified_at', 'peach_license']);2090  const slotA = (raw as Record<string, unknown>)[SLOT_A];2091  if (!slotA) return null;2092  const slotB = (raw as Record<string, unknown>)[SLOT_B];2093  const blob = JSON.stringify({ a: slotA, b: slotB });2094  const rawTs = (raw as Record<string, unknown>)['peach_last_modified_at'];2095  const timestamp = typeof rawTs === 'string' ? parseInt(rawTs) : 0;2096  const license = (raw as Record<string, unknown>)['peach_license'];2097  return {2098    blob,2099    timestamp,2100    license: typeof license === 'string' ? license : undefined,2101  };2102}21032104export async function applyIncomingVault(2105  blobJson: string,2106  remoteTimestamp: number,2107  remoteLicense?: string,2108): Promise<boolean> {2109  const raw = await chrome.storage.local.get('peach_last_modified_at');2110  const rawTs = (raw as Record<string, unknown>)['peach_last_modified_at'];2111  const localTimestamp = typeof rawTs === 'string' ? parseInt(rawTs) : 0;21122113  if (remoteLicense) {2114    const licenseRaw = await chrome.storage.local.get('peach_license');2115    const existing = (licenseRaw as Record<string, unknown>)['peach_license'];2116    if (!existing) {2117      await chrome.storage.local.set({ peach_license: remoteLicense });2118    }2119  }21202121  if (remoteTimestamp <= localTimestamp) return false;21222123  // Snapshot before overwriting — failure is non-fatal2124  await takeVaultSnapshot('sync_received').catch(() => {});21252126  const { a, b } = JSON.parse(blobJson) as { a: unknown; b?: unknown };2127  await writeSlot(SLOT_A, a as StoredVault);2128  if (b) await writeSlot(SLOT_B, b as StoredVault);2129  await chrome.storage.local.set({2130    peach_last_sync_at: String(Date.now()),2131    peach_last_modified_at: String(remoteTimestamp),2132  });21332134  return true;2135}21362137export async function getSyncIdentity(): Promise<{2138  privateKeyBytes: Uint8Array;2139  publicKeyBytes: Uint8Array;2140  publicKeyHex: string;2141}> {2142  const raw = await chrome.storage.local.get('peach_sync_identity');2143  const stored = (raw as Record<string, unknown>)['peach_sync_identity'];2144  if (typeof stored === 'string') {2145    const { priv, pub } = JSON.parse(stored) as { priv: string; pub: string };2146    const privateKeyBytes = base64DecodeBytes(priv);2147    const publicKeyBytes = base64DecodeBytes(pub);2148    return { privateKeyBytes, publicKeyBytes, publicKeyHex: bytesToHex(publicKeyBytes) };2149  }21502151  // Generate a fresh X25519 keypair and persist it.2152  const { generateKeyPair } = await import('@stablelib/x25519');2153  const keypair = generateKeyPair();2154  await chrome.storage.local.set({2155    peach_sync_identity: JSON.stringify({2156      priv: base64EncodeBytes(keypair.secretKey),2157      pub: base64EncodeBytes(keypair.publicKey),2158    }),2159  });2160  return {2161    privateKeyBytes: keypair.secretKey,2162    publicKeyBytes: keypair.publicKey,2163    publicKeyHex: bytesToHex(keypair.publicKey),2164  };2165}21662167// Re-decrypt vault entries from storage using the current in-memory key.2168// Called after applyIncomingVault so the in-memory state stays consistent2169// with what was just written to storage.2170export async function reloadEntriesAfterSync(): Promise<boolean> {2171  if (!derivedKey) return false;2172  const slotA = await loadSlot(SLOT_A);2173  if (!slotA) return false;2174  try {2175    const payload = await tryDecryptSlot(slotA, derivedKey);2176    if (!payload) return false;2177    entries = payload.entries;2178    buildDomainIndexSync(entries);2179    return true;2180  } catch {2181    return false;2182  }2183}21842185function bytesToHex(bytes: Uint8Array): string {2186  return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');2187}21882189function base64EncodeBytes(bytes: Uint8Array): string {2190  let binary = '';2191  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);2192  return btoa(binary);2193}21942195function base64DecodeBytes(b64: string): Uint8Array {2196  const binary = atob(b64);2197  const bytes = new Uint8Array(binary.length);2198  for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);2199  return bytes;2200}