import { createSignal, createMemo, For, onCleanup, onMount, Show } from "solid-js"; import { sendMessage } from "../App"; import { PeachWordmark } from "../components/PeachMark"; import { RecoveryRuneBackground } from "../components/RecoveryRuneBackground"; interface RecoveryWordDisplayViewProps { onDone: () => void; } // Three random positions are confirmed each time this screen mounts, so a // user can't get away with only memorizing a fixed set of slots. function pickConfirmIndices(): number[] { const pool = Array.from({ length: 12 }, (_, i) => i); const rand = new Uint32Array(pool.length); crypto.getRandomValues(rand); for (let i = pool.length - 1; i > 0; i--) { const j = (rand[i] ?? 0) % (i + 1); const tmp = pool[i]!; pool[i] = pool[j]!; pool[j] = tmp; } return pool.slice(0, 3).sort((a, b) => a - b); } export function RecoveryWordDisplayView(props: RecoveryWordDisplayViewProps) { // Chosen once per mount; the labels and the validation must agree. const confirmIndices = pickConfirmIndices(); const [words, setWords] = createSignal([]); const [error, setError] = createSignal(""); const [loading, setLoading] = createSignal(true); const [needsPassword, setNeedsPassword] = createSignal(false); const [password, setPassword] = createSignal(""); let requestEpoch = 0; let disposed = false; const [confirmWords, setConfirmWords] = createSignal(["", "", ""]); const [confirmError, setConfirmError] = createSignal(""); const [confirmed, setConfirmed] = createSignal(false); // 5-second countdown before confirmation input is enabled const [countdown, setCountdown] = createSignal(5); let countdownTimer: ReturnType | null = null; onMount(async () => { await refreshWords(); if (disposed) return; // Start 5-second countdown let remaining = 5; countdownTimer = setInterval(() => { remaining -= 1; setCountdown(remaining); if (remaining <= 0 && countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; } }, 1000); }); async function refreshWords(proof?: string): Promise { const epoch = ++requestEpoch; setLoading(true); setError(""); setPassword(""); const current = () => epoch === requestEpoch && document.visibilityState !== "hidden"; try { let res = await sendMessage({ type: "GET_RECOVERY_WORDS", ...(proof ? { payload: { password: proof } } : {}), }) as { ok: boolean; data?: string[]; error?: string }; if (!current()) return; if (res.error === "vault_locked" && proof) { const unlocked = await sendMessage({ type: "UNLOCK", payload: { password: proof } }) as { ok: boolean }; if (!current()) return; if (!unlocked.ok) { setError("incorrect password"); setNeedsPassword(true); return; } res = await sendMessage({ type: "GET_RECOVERY_WORDS", payload: { password: proof } }) as typeof res; if (!current()) return; } if (res.ok && res.data?.length === 12) { setWords(res.data); setNeedsPassword(false); } else { setNeedsPassword(true); if (res.error === "wrong_password") setError("incorrect password"); else if (res.error !== "password_required" && res.error !== "vault_locked") setError("could not retrieve recovery words"); } } catch { if (current()) { setNeedsPassword(true); setError("could not retrieve recovery words"); } } finally { if (current()) setLoading(false); } } function hideWords(): void { requestEpoch++; setWords([]); setConfirmed(false); setPassword(""); setConfirmWords(["", "", ""]); setConfirmError(""); setError(""); setNeedsPassword(true); setLoading(false); } const handleVisibilityChange = () => { if (document.visibilityState === "hidden") hideWords(); else if (!confirmed()) void refreshWords(); }; const handleVaultMessage = (message: { type?: string }) => { if (message.type === "VAULT_LOCKED") hideWords(); }; document.addEventListener("visibilitychange", handleVisibilityChange); chrome.runtime?.onMessage?.addListener(handleVaultMessage); onCleanup(() => { disposed = true; requestEpoch++; if (countdownTimer) clearInterval(countdownTimer); document.removeEventListener("visibilitychange", handleVisibilityChange); chrome.runtime?.onMessage?.removeListener?.(handleVaultMessage); }); async function handleConfirm() { const confirmed = confirmWords().map((w) => w.toLowerCase().trim()); if (confirmed.some((w) => !w)) { setConfirmError("all three words are required"); return; } // Verify against stored words const stored = words(); for (let i = 0; i < confirmIndices.length; i++) { const pos = confirmIndices[i]!; if (confirmed[i] !== stored[pos]) { setConfirmError(`word ${pos + 1} is incorrect`); return; } } const epoch = requestEpoch; setConfirmed(true); setConfirmError(""); // Successful confirmation ends the routing obligation. await sendMessage({ type: "CLEAR_RECOVERY_CONFIRM_PENDING" }); // Small pause so user sees the success state, then proceed. await new Promise((r) => setTimeout(r, 600)); if (epoch === requestEpoch && !disposed) props.onDone(); } const canConfirm = createMemo(() => words().length === 12 && countdown() <= 0 && !confirmed()); return (

save your recovery words

write these down — you can view them again in Settings with your master password

loading...

{error()}

{ event.preventDefault(); if (password() && !loading()) void refreshWords(password()); }}> setPassword(event.currentTarget.value)} disabled={loading()} />
your 12 recovery words
    {(word, i) => (
  1. {i() + 1} {word}
  2. )}
{/* Confirmation step */}

{countdown() > 0 ? `type the highlighted words to confirm (${countdown()}s)` : "type the highlighted words to confirm"}

✓ words verified

{(pos, i) => (
word {pos + 1} { const updated = [...confirmWords()]; updated[i()] = (e.target as HTMLInputElement).value; setConfirmWords(updated); if (confirmError()) setConfirmError(""); }} onKeyDown={(e) => { if (e.key === "Enter" && canConfirm()) void handleConfirm(); }} />
)}

{confirmError()}

); } function recoveryWordsText(words: string[]): string { const numbered = words.map((w, i) => `${String(i + 1).padStart(2, " ")}. ${w}`).join("\n"); return ( "PEACH RECOVERY WORDS\n" + "====================\n\n" + "Anyone with these 12 words can recover your vault.\n" + "Store this somewhere only you can access.\n\n" + numbered + "\n" ); } // Desktop only — writes via a blob to a user-chosen save location. The native // path deliberately does NOT use a file download: on Android the data:-URL // download sink writes to world-readable public Downloads (audit MEDIUM), so // native offers a clipboard copy (copyRecoveryWords) the user routes instead. function saveRecoveryWords(words: string[]): void { const blob = new Blob([recoveryWordsText(words)], { type: "text/plain" }); const a = document.createElement("a"); a.download = "peach-recovery-words.txt"; a.href = URL.createObjectURL(blob); setTimeout(() => URL.revokeObjectURL(a.href), 1000); document.body.appendChild(a); a.click(); a.remove(); }