import { useSignal } from "@preact/signals"; import { save_master_key } from "@/src/key_store.ts"; import { type GetKeyResponse, import_key_text, unwrap_with_passphrase, verify_key_check } from "@/src/keys.ts"; const MAX_KEY_FILE_SIZE = 1024; interface KeysUnlockProps { account: GetKeyResponse; message: string; on_done: () => void; } export default function KeysUnlock(props: KeysUnlockProps) { const passphrase = useSignal(""); const key_text = useSignal(""); const working = useSignal(false); const error = useSignal(props.message); async function unlock(get_key: () => Promise) { working.value = true; error.value = ""; try { const key = await get_key(); if (!props.account.key_check || !(await verify_key_check(key, props.account.key_check))) { error.value = "That key doesn't belong to this account"; return; } await save_master_key(props.account.user_id, key); props.on_done(); } catch (err) { error.value = err instanceof Error ? err.message : String(err); } finally { working.value = false; } } function unlock_with_passphrase(event: SubmitEvent) { event.preventDefault(); const backup = props.account.passphrase_backup; if (backup && passphrase.value) { unlock(() => unwrap_with_passphrase(backup, passphrase.value)); } } function unlock_with_key_text(event: SubmitEvent) { event.preventDefault(); if (key_text.value) { unlock(() => import_key_text(key_text.value)); } } async function unlock_with_key_file(event: Event) { const input = event.currentTarget as HTMLInputElement; const file = input.files?.[0]; input.value = ""; if (!file) { return; } if (file.size > MAX_KEY_FILE_SIZE) { error.value = "That doesn't look like a VCES key"; return; } const text = await file.text(); unlock(() => import_key_text(text)); } return (

This account already has an encryption key. Unlock it on this device with one of your backups.

{props.account.passphrase_backup && (

Passphrase

passphrase.value = event.currentTarget.value} />
)}

Key file

Or paste the key: