diff --git a/client/src/components/KeyBackup.jsx b/client/src/components/KeyBackup.jsx new file mode 100644 index 0000000..978db0e --- /dev/null +++ b/client/src/components/KeyBackup.jsx @@ -0,0 +1,96 @@ +import { useState } from 'react' +import axios from 'axios' +import { useAuth } from '../context/AuthContext' +import { exportKeyBackup, importKeyBackup } from '../lib/dmCrypto' +import { KeyRound, Download, Upload } from 'lucide-react' + +// Passphrase-protected backup of this device's DM key, so message history can +// move to another device. The exported blob is encrypted with the passphrase; +// the server never sees it. +export default function KeyBackup() { + const { user } = useAuth() + const [tab, setTab] = useState('export') + const [pass, setPass] = useState('') + const [blob, setBlob] = useState('') + const [msg, setMsg] = useState(null) // { kind: 'ok'|'err', text } + + const doExport = async () => { + setMsg(null) + try { + const out = await exportKeyBackup(user.id, pass) + setBlob(out) + const url = URL.createObjectURL(new Blob([out], { type: 'application/json' })) + const a = document.createElement('a') + a.href = url + a.download = `beacon-key-${user.username}.txt` + a.click() + setTimeout(() => URL.revokeObjectURL(url), 30_000) + setMsg({ kind: 'ok', text: 'Backup downloaded. Keep it and the passphrase safe.' }) + } catch (e) { + setMsg({ kind: 'err', text: e.message }) + } + } + + const doImport = async () => { + setMsg(null) + try { + const publicJwk = await importKeyBackup(user.id, pass, blob.trim()) + // Republish the restored public key so peers encrypt to it. + await axios.post('/api/auth/public-key', { publicKey: JSON.stringify(publicJwk) }).catch(() => {}) + setMsg({ kind: 'ok', text: 'Key restored. Reopen a conversation to read its history.' }) + } catch (e) { + setMsg({ kind: 'err', text: e.message }) + } + } + + return ( +
+
+
+ +
+

Message key backup

+
+ +

+ Your messages are encrypted with a key that lives only on this device. Back it up with a passphrase + to read your history on another device. The backup is useless without the passphrase — keep both safe. +

+ +
+ {['export', 'import'].map((t) => ( + + ))} +
+ +
+ setPass(e.target.value)} data-testid="keybackup-pass" /> + + {tab === 'import' && ( +