import { doc, setDoc, getDoc } from 'firebase/firestore'; import { db, getActiveUser, cloudSubstrateService } from './cloudSubstrateService'; // GDPR compliant node registration metrics export interface VirtualNode { nodeId: string; publicKey: string; capabilities: Record; status: 'registered' | 'active' | 'revoked'; registeredAt: number; lastSeen: number; } export const RegistrationService = { /** * The Chancellor formalizes the registration of a new contributor node. * Adheres to the principle of Scholastic Data Sovereignty. */ async registerNode( nodeId: string, publicKey: string, capabilities: Record, ): Promise { const user = getActiveUser(); if (!user) throw new Error( 'AUTHORIZATION_REQUIRED: Registration requires a verified scholarly identity.', ); if (cloudSubstrateService.isSimulated()) { const stored = localStorage.getItem('simulated_virtual_nodes'); const nodes: Record = stored ? JSON.parse(stored) : {}; if (nodes[nodeId]) { throw new Error('IDENTITY_COLLISION: Node ID already exists in the ledger.'); } const registrationData: VirtualNode = { nodeId, publicKey, capabilities, status: 'registered', registeredAt: Date.now(), lastSeen: Date.now(), }; nodes[nodeId] = registrationData; nodes[user.uid] = registrationData; localStorage.setItem('simulated_virtual_nodes', JSON.stringify(nodes)); window.dispatchEvent(new Event('simulated_nodes_updated')); return; } const nodeRef = doc(db, 'virtualNodes', nodeId); // Check if node already exists to prevent identity collision const nodeDoc = await getDoc(nodeRef); if (nodeDoc.exists()) throw new Error('IDENTITY_COLLISION: Node ID already exists in the ledger.'); const registrationData: VirtualNode = { nodeId, publicKey, capabilities, status: 'registered', registeredAt: Date.now(), lastSeen: Date.now(), }; try { await setDoc(nodeRef, registrationData); console.info( `[CHANCELLOR_REGISTRY]: Node [${nodeId}] successfully registered as a contributor.`, ); } catch (error) { console.error('[REGISTRY_FAILURE]: Registration anomaly detected.', error); throw new Error( 'COMPUTATIONAL_DONATION_REJECTED: Anomaly during registration ledger commit.', { cause: error }, ); } }, };