WIP V improve UX [snapshot task 2]

This commit is contained in:
Schluffe committed 2026-10-17 14:51:48 +02:00
1 parent 5f8c6a1916
commit a73b3075b6
16 files changed
+238 -232

No files matched your search

+3 -17
View File
@@ -2,8 +2,7 @@ import { isRecord } from './validation.ts';
export interface Identity { publicKey: string; privateKey: CryptoKey }
export interface IdentityHello { key: string; nonce: string }
export interface Contact { key: string; name: string }
export interface IdentityState { revision?: string; identity: Identity | null; contacts: Contact[] }
export interface IdentityState { revision?: string; identity: Identity | null }
export interface IdentityContext { offer: IdentityHello; answer: IdentityHello; offerFingerprint: string; answerFingerprint: string }
const curve = { name: 'ECDSA', namedCurve: 'P-256' };
const signing = { name: 'ECDSA', hash: 'SHA-256' };
@@ -42,17 +41,6 @@ export async function verifyIdentity(proof: unknown, context: IdentityContext, i
const key = await crypto.subtle.importKey('raw', unhex((initiator ? context.offer : context.answer).key, 65), curve, false, ['verify']);
if (!await crypto.subtle.verify(signing, key, unhex(proof, 64), transcript(context, initiator))) throw new Error('Device proof failed. Disconnect and verify fresh codes.');
}
export function validateContacts(value: unknown): Contact[] {
if (!Array.isArray(value) || value.length > 100) throw new Error('Invalid contact list.');
const keys = new Set<string>();
return value.map(item => {
if (!isRecord(item) || typeof item.name !== 'string' || !item.name.trim() || item.name.length > 80) throw new Error('Invalid contact name.');
unhex(item.key, 65);
const key = item.key as string;
if (keys.has(key)) throw new Error('Duplicate contact identity.');
keys.add(key); return { key, name: item.name.trim() };
});
}
async function database() {
return new Promise<IDBDatabase>((resolve, reject) => {
@@ -71,17 +59,15 @@ export async function loadIdentityState(): Promise<IdentityState> {
const request = tx.objectStore('settings').get('state');
tx.oncomplete = () => resolve(request.result); tx.onabort = tx.onerror = () => reject(new Error('Could not read device storage.'));
});
if (!state) return { identity: null, contacts: [] };
validateContacts(state.contacts);
if (!state) return { identity: null };
if (state.identity) {
unhex(state.identity.publicKey, 65);
if (!(state.identity.privateKey instanceof CryptoKey) || state.identity.privateKey.type !== 'private') throw new Error('Stored identity is invalid. Clear device storage and create a new identity.');
}
return state;
return { revision: state.revision, identity: state.identity ?? null };
} finally { db.close(); }
}
export async function saveIdentityState(state: IdentityState, expectedRevision = state.revision): Promise<IdentityState> {
validateContacts(state.contacts);
const db = await database();
const next = { ...state, revision: crypto.randomUUID() };
try {