File size: 2,558 Bytes
35b8b9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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<string, unknown>;
  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<string, unknown>,
  ): Promise<void> {
    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<string, VirtualNode> = 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 },
      );
    }
  },
};