/** * The Map key: one AES key per owner, generated here, that seals every estate item's institution, * reference and notes. The owner holds it wrapped under the Vault Key; each executor (a handoff * recipient) holds it wrapped to their own key, so the executor guide is built on their device. */ import * as mb from './crypto'; import { api } from './api'; import { openText, sealText } from './metadata'; import type { EstateAssetDto, EstateKeyDto, EstateShareDto, RecipientWrapDto } from './types'; import type { PlanAsset } from './executorPlan'; export interface OpenedAsset extends PlanAsset { id: string; renewalDate?: string | null; sealed: boolean } /** The owner's Map key — created on first use, never replaced. */ export async function loadEstateKey(vaultKey: CryptoKey): Promise<{ raw: Uint8Array; key: CryptoKey; dto: EstateKeyDto }> { let dto = (await api('/estate/key')) ?? null; let raw: Uint8Array; if (!dto) { const key = await mb.generateVaultKey(); raw = await mb.exportRawKey(key); dto = await api('/estate/key', { method: 'PUT', body: { ownerWrap: await mb.aesEncrypt(vaultKey, raw) } }); // Two devices racing: the server keeps the first key; re-read ours from what it kept. raw = await mb.aesDecrypt(vaultKey, dto.ownerWrap); } else { raw = await mb.aesDecrypt(vaultKey, dto.ownerWrap); } return { raw, key: await mb.importAesKey(raw), dto }; } export async function sealAssetFields(key: CryptoKey, f: { institution: string; reference?: string | null; notes?: string | null }) { return { institutionCipher: await sealText(key, f.institution.trim()), referenceCipher: f.reference?.trim() ? await sealText(key, f.reference.trim()) : null, notesCipher: f.notes?.trim() ? await sealText(key, f.notes.trim()) : null, }; } /** Open one item with the Map key; legacy plaintext rows pass through; without a key, sealed fields read "(sealed)". */ export async function openAsset(key: CryptoKey | null, a: EstateAssetDto): Promise { const open = async (cipher?: { iv: string; ct: string } | null, plain?: string | null): Promise => { if (cipher) { if (!key) return '(sealed)'; try { return await openText(key, cipher); } catch { return '(sealed)'; } } return plain ?? null; }; return { id: a.id, type: a.type, action: a.action, institution: (await open(a.institutionCipher, a.institution)) ?? '(sealed)', reference: await open(a.referenceCipher, a.reference), notes: await open(a.notesCipher, a.notes), renewalDate: a.renewalDate, walletKind: a.walletKind ?? null, sealed: !!a.institutionCipher, }; } /** Wrap the Map key to a recipient (Keycard, and Account once they accepted) unless already done. */ export async function shareEstateKey(raw: Uint8Array, recipient: { id: string; publicJwk: string; accountPublicJwk?: string | null }, existing: EstateShareDto[]): Promise { const has = (kind: string) => existing.some((s) => s.recipientId === recipient.id && s.keyKind === kind); if (!has('Keycard')) { const sealed = await mb.sealToRecipient(raw, recipient.publicJwk); await api('/estate/key/shares', { method: 'POST', body: { recipientId: recipient.id, ...mb.packSealed(sealed), keyKind: 'Keycard' } }); } if (recipient.accountPublicJwk && !has('Account')) { const sealed = await mb.sealToRecipient(raw, recipient.accountPublicJwk); await api('/estate/key/shares', { method: 'POST', body: { recipientId: recipient.id, ...mb.packSealed(sealed), keyKind: 'Account' } }); } } /** Executor side: recover the Map key from the wrap that matches the key they hold. */ export async function openEstateKeyAsRecipient(privateKey: string, wraps: RecipientWrapDto[], prefer: 'Keycard' | 'Account'): Promise { const ordered = [...wraps.filter((w) => (w.keyKind ?? 'Keycard') === prefer), ...wraps.filter((w) => (w.keyKind ?? 'Keycard') !== prefer)]; for (const w of ordered) { try { const raw = await mb.openAsRecipient(mb.unpackSealed({ ephemeralPublicJwk: w.ephemeralPublicJwk, wrap: w.wrap }), privateKey); return await mb.importAesKey(raw); } catch { /* try the next wrap */ } } return null; }