SPB Git

Repository Public

RecoveryKit.tsx 5.7 KB 85 lines tsx

1// Recovery kit: shows a new recovery key once, offers Print / Copy, then verifies that it was really written down.2import { useMemo, useState } from "react";3import { uniform } from "../../../../packages/protocol/src/index.ts";4import { copyText } from "../state.ts";5import { Btn, Callout, Field, Icon } from "./common.tsx";67const norm = (s: string) => s.toUpperCase().replace(/[\s-]/g, "").replace(/O/g, "0").replace(/[IL]/g, "1");89export function RecoveryKit({ recoveryKey, handle, onDone, reason }: { recoveryKey: string; handle: string; onDone: () => void; reason: "new" | "rotated" | "rekeyed" }) {10  const groups = useMemo(() => recoveryKey.split("-").slice(1), [recoveryKey]);11  const picks = useMemo(() => { const s = new Set<number>(); while (s.size < 3) s.add(uniform(groups.length)); return [...s].sort((a, b) => a - b); }, [groups]);12  const [step, setStep] = useState<"show" | "verify">("show");13  const [typed, setTyped] = useState<string[]>(["", "", ""]);14  const [tried, setTried] = useState(false);15  const ok = picks.map((p, i) => norm(typed[i]!) === groups[p]);16  const today = new Date().toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric" });1718  return (19    <div className="kit" data-testid="recovery-kit">20      <div className="kit-steps" aria-hidden="true"><span className={step === "show" ? "on" : "done"}>1 · Save</span><span className={step === "verify" ? "on" : ""}>2 · Verify</span></div>21      {step === "show" ? (22        <div className="stack">23          <h2 className="serif">{reason === "new" ? "Your recovery key" : "Your new recovery key"}</h2>24          <p className="muted">25            {reason === "new" ? "This key is shown once. " : reason === "rekeyed" ? "The account was rekeyed: the previous recovery key no longer works. This new key is shown once. " : "The previous recovery key no longer works. This new key is shown once. "}26            Print it or write it down and keep it somewhere safe, away from this device.27          </p>28          <div className="kit-key" role="group" aria-label="Recovery key">29            <span className="kit-prefix mono">SPV1</span>30            <ol className="kit-groups mono" data-testid="recovery-key">31              {groups.map((g, i) => <li key={i}><span className="kit-n" aria-hidden="true">{i + 1}</span><span className="kit-g">{g}</span></li>)}32            </ol>33          </div>34          <div className="row wrap">35            <Btn icon="print" onClick={() => window.print()}>Print recovery kit</Btn>36            <Btn icon="copy" onClick={() => copyText(recoveryKey, "Recovery key")} data-testid="kit-copy">Copy</Btn>37          </div>38          <Callout kind="warn" title="Nobody can recover this vault for you">39            <p>Without a trusted device or this key, the vault cannot be recovered by anyone — including the server operator. There is no reset by e-mail and no back door.</p>40          </Callout>41          <div className="row end"><Btn variant="primary" onClick={() => setStep("verify")} data-testid="kit-next">I saved it — continue</Btn></div>42        </div>43      ) : (44        <form className="stack" onSubmit={(e) => { e.preventDefault(); setTried(true); if (ok.every(Boolean)) onDone(); }}>45          <h2 className="serif">Check your copy</h2>46          <p className="muted">Type the three groups below from your printed or written copy. This confirms the kit is readable before you depend on it.</p>47          <div className="kit-verify">48            {picks.map((p, i) => (49              <Field key={p} label={`Group ${p + 1}`} error={tried && !ok[i] ? "Does not match" : undefined}>50                <input className="input mono kit-input" value={typed[i]} maxLength={6} autoComplete="off" autoCapitalize="characters" spellCheck={false} inputMode="text" data-testid={`kit-verify-${p + 1}`}51                  data-autofocus={i === 0 || undefined} autoFocus={i === 0} aria-invalid={tried && !ok[i]} onChange={(e) => setTyped(typed.map((t, j) => (j === i ? e.target.value : t)))} />52              </Field>53            ))}54          </div>55          <div className="row between">56            <Btn variant="ghost" icon="back" onClick={() => { setStep("show"); setTried(false); }}>Show the key again</Btn>57            <Btn variant="primary" type="submit" data-testid="kit-confirm">Confirm</Btn>58          </div>59        </form>60      )}6162      {/* Printed page only (see @media print in styles.css). */}63      <div className="print-sheet" aria-hidden="true">64        <div className="print-head"><span className="print-mark"><Icon name="lock" size={22} /></span><h1>SP Vault recovery kit</h1></div>65        <table className="print-meta"><tbody>66          <tr><th>Account handle</th><td>{handle}</td></tr>67          <tr><th>Vault address</th><td>{location.origin}</td></tr>68          <tr><th>Created</th><td>{today}</td></tr>69        </tbody></table>70        <h2>Recovery key</h2>71        <div className="print-key">SPV1-{groups.slice(0, 7).join("-")}<br />{groups.slice(7).join("-")}</div>72        <h2>How to use it</h2>73        <ol>74          <li>Open the vault address above in a browser and choose “Recover with recovery key”.</li>75          <li>Enter the account handle and the recovery key exactly as printed (dashes and case do not matter).</li>76          <li>Create a new passkey for that browser when asked, then issue a new recovery key from Settings.</li>77        </ol>78        <p className="print-warn">Anyone holding this page can open the vault. Keep it in a safe place, separate from your devices. Without a trusted device or this key the vault cannot be recovered by anyone, including the server operator.</p>79        <h2>Notes</h2>80        <div className="print-lines"><span /><span /><span /><span /><span /></div>81      </div>82    </div>83  );84}85
Files main
Menu

Theme