/** * Guided handoffs are sealed like capsules: a fresh content key per handoff encrypts the title and * every step's text; the key is wrapped to the owner (under the Vault Key) and to the recipient * (Keycard, and Account once they accepted). The server keeps ciphertext, wraps, order and delays. */ import * as mb from './crypto'; import { openText, sealText } from './metadata'; import type { CipherBlobDto, HandoffDto, RecipientHandoffDto, RecipientWrapDto } from './types'; export interface StepInput { title: string; instruction: string | null; delayHours: number; capsuleId: string | null } export interface OpenedHandoff { title: string; steps: Record } async function recipientWraps(raw: Uint8Array, recipient: { id: string; publicJwk: string; accountPublicJwk?: string | null }) { const wraps = [{ recipientId: recipient.id, ...mb.packSealed(await mb.sealToRecipient(raw, recipient.publicJwk)), keyKind: 'Keycard' }]; if (recipient.accountPublicJwk) wraps.push({ recipientId: recipient.id, ...mb.packSealed(await mb.sealToRecipient(raw, recipient.accountPublicJwk)), keyKind: 'Account' }); return wraps; } /** Seal a new handoff for the create request. */ export async function sealHandoff(vaultKey: CryptoKey, recipient: { id: string; publicJwk: string; accountPublicJwk?: string | null }, title: string, steps: StepInput[]) { const key = await mb.generateVaultKey(); const raw = await mb.exportRawKey(key); return { title: null, titleCipher: await sealText(key, title), ownerWrap: await mb.aesEncrypt(vaultKey, raw), recipientWraps: await recipientWraps(raw, recipient), steps: await Promise.all(steps.map(async (s) => ({ title: null, instruction: null, titleCipher: await sealText(key, s.title), instructionCipher: s.instruction ? await sealText(key, s.instruction) : null, delayHours: s.delayHours, capsuleId: s.capsuleId, }))), }; } /** Seal a legacy (plaintext) handoff for the metadata upgrade. */ export async function sealLegacyHandoff( vaultKey: CryptoKey, h: { id: string; title: string; recipientId: string; recipientPublicJwk: string; recipientAccountPublicJwk?: string | null; steps: { id: string; title: string; instruction?: string | null }[] }, ) { const key = await mb.generateVaultKey(); const raw = await mb.exportRawKey(key); return { id: h.id, titleCipher: await sealText(key, h.title), ownerWrap: await mb.aesEncrypt(vaultKey, raw), recipientWraps: await recipientWraps(raw, { id: h.recipientId, publicJwk: h.recipientPublicJwk, accountPublicJwk: h.recipientAccountPublicJwk }), steps: await Promise.all(h.steps.map(async (s) => ({ id: s.id, titleCipher: await sealText(key, s.title || '(step)'), instructionCipher: s.instruction ? await sealText(key, s.instruction) : null }))), }; } async function openWith(key: CryptoKey | null, cipher?: CipherBlobDto | null, plain?: string | null, fallback = '(sealed)'): Promise { if (cipher) { if (!key) return fallback; try { return await openText(key, cipher); } catch { return fallback; } } return plain ?? null; } /** Owner side: through ownerWrap. Legacy plaintext handoffs pass through. */ export async function openHandoffAsOwner(vaultKey: CryptoKey | null, h: HandoffDto): Promise { let key: CryptoKey | null = null; if (h.titleCipher && h.ownerWrap && vaultKey) { try { key = await mb.importAesKey(await mb.aesDecrypt(vaultKey, h.ownerWrap)); } catch { key = null; } } const steps: OpenedHandoff['steps'] = {}; for (const s of h.steps) steps[s.order] = { title: (await openWith(key, s.titleCipher, s.title)) ?? '(sealed)', instruction: await openWith(key, s.instructionCipher, s.instruction) }; return { title: (await openWith(key, h.titleCipher, h.title)) ?? '(sealed)', steps }; } /** Recipient side: through the wrap that matches the key they hold. Returns null when a sealed handoff cannot be opened. */ export async function openHandoffAsRecipient(privateKey: string | null, d: RecipientHandoffDto, prefer: 'Keycard' | 'Account'): Promise { let key: CryptoKey | null = null; if (d.titleCipher) { if (!privateKey) return null; const wraps: RecipientWrapDto[] = d.wraps ?? []; const ordered = [...wraps.filter((w) => (w.keyKind ?? 'Keycard') === prefer), ...wraps.filter((w) => (w.keyKind ?? 'Keycard') !== prefer)]; for (const w of ordered) { try { key = await mb.importAesKey(await mb.openAsRecipient(mb.unpackSealed({ ephemeralPublicJwk: w.ephemeralPublicJwk, wrap: w.wrap }), privateKey)); break; } catch { /* next */ } } if (!key) return null; } const steps: OpenedHandoff['steps'] = {}; for (const s of d.steps) steps[s.order] = { title: (await openWith(key, s.titleCipher, s.title)) ?? '(sealed)', instruction: await openWith(key, s.instructionCipher, s.instruction) }; return { title: (await openWith(key, d.titleCipher, d.title)) ?? '(sealed)', steps }; }