-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.ts
More file actions
457 lines (393 loc) · 13.7 KB
/
node.ts
File metadata and controls
457 lines (393 loc) · 13.7 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* @module node
* @description NodeZero — the orchestrator that wires all primitives together.
*
* A NodeZero instance manages:
* - Identity (key generation, signing, verification)
* - State (metabolic vector, voltage, spoons, scope tier)
* - Vault (layered encrypted storage)
* - Bonds (authenticated channels with peer nodes)
* - Transport (WebSocket/BroadcastChannel communication)
*
* Note: The skeleton primitives' initiate/accept/broadcast methods throw
* "not implemented". This orchestrator handles those flows directly,
* bypassing the skeleton stubs until they're fully wired.
*
* @example
* ```ts
* const node = new NodeZero();
* await node.boot();
*
* node.onPeerDiscovered(async (peer) => {
* await node.initiateBond(peer);
* });
*
* await node.updateState("URGENCY", 0.3);
* ```
*/
import { WebCryptoIdentityProvider } from "./backends/webcrypto-identity.js";
import { StateEngine } from "./primitives/state-engine.js";
import { VaultStore } from "./primitives/vault-store.js";
import { BroadcastChannelTransport } from "./transports/websocket.js";
import {
serializeStateUpdate,
deserializeStateUpdate,
} from "./codec/index.js";
import {
deriveSharedSecret,
deriveNodeId,
} from "./backends/crypto-utils.js";
import type {
NodeId,
CompressedPublicKey,
SharedSecret,
UnixTimestamp,
Uint8 as U8,
} from "./types/branded.js";
import type { Axis, StateWireData } from "./types/state.js";
import type { DiscoveredPeer } from "./types/transport.js";
import type { NodeZeroIdentity } from "./types/identity.js";
import type { BondStatus, TrustTier } from "./types/bond.js";
// ─── Configuration ────────────────────────────────────────────────
export interface NodeZeroConfig {
/** BroadcastChannel name for same-origin transport. Default: "node-zero-mesh" */
channelName?: string;
/** State broadcast interval in milliseconds. Default: 10000 (10s) */
broadcastInterval?: number;
/** Auto-discover peers on boot. Default: true */
autoDiscover?: boolean;
/** Discovery beacon interval in milliseconds. Default: 5000 */
discoveryInterval?: number;
}
// ─── Local Bond Record ────────────────────────────────────────────
export interface LocalBond {
peerId: NodeId;
peerPublicKey: CompressedPublicKey;
sharedSecret: SharedSecret;
status: BondStatus;
trustTier: TrustTier;
careScore: number;
createdAt: number;
lastInteraction: number;
totalExchanges: number;
}
// ─── Orchestrator ──────────────────────────────────────────────────
export class NodeZero {
readonly identity: WebCryptoIdentityProvider;
readonly state: StateEngine;
readonly vault: VaultStore;
readonly transport: BroadcastChannelTransport;
private config: Required<NodeZeroConfig>;
private broadcastTimer: ReturnType<typeof setInterval> | null = null;
private discoveryTimer: ReturnType<typeof setInterval> | null = null;
private booted = false;
private lastBroadcastTime = 0;
/** Local bond store (bypasses ChannelManager stubs) */
private bonds = new Map<string, LocalBond>();
// Event callbacks
private peerCallbacks = new Set<(peer: DiscoveredPeer) => void>();
private bondCallbacks = new Set<(bond: LocalBond) => void>();
private stateCallbacks = new Set<
(
nodeId: NodeId,
state: { voltage: number; spoons: number; tier: string }
) => void
>();
constructor(config: NodeZeroConfig = {}) {
this.config = {
channelName: config.channelName ?? "node-zero-mesh",
broadcastInterval: config.broadcastInterval ?? 10000,
autoDiscover: config.autoDiscover ?? true,
discoveryInterval: config.discoveryInterval ?? 5000,
};
this.identity = new WebCryptoIdentityProvider();
this.state = new StateEngine("pending" as NodeId);
this.vault = new VaultStore();
this.transport = new BroadcastChannelTransport(this.config.channelName);
}
// ─── Lifecycle ──────────────────────────────────────────────────
/**
* Boot the node: generate identity, configure transport, start broadcasting.
* @returns The full public identity.
*/
async boot(): Promise<NodeZeroIdentity> {
if (this.booted) {
return this.identity.exportPublicKey();
}
// 1. Generate cryptographic identity
await this.identity.generateKeypair();
const fullIdentity = await this.identity.exportPublicKey();
// 2. Configure transport
await this.transport.configure({
medium: "WEBSOCKET",
mtu: 65535,
});
this.transport.setLocalIdentity(fullIdentity.publicKey.data);
// 3. Wire up transport → receive handler
this.transport.onReceive((data: Uint8Array) => {
this.handleIncomingData(data);
});
// 4. Wire up peer discovery
this.transport.onPeerDiscovered((peer: DiscoveredPeer) => {
for (const cb of this.peerCallbacks) {
cb(peer);
}
});
// 5. Start periodic state broadcast
this.broadcastTimer = setInterval(() => {
this.broadcastState().catch(() => {});
}, this.config.broadcastInterval);
// 6. Start periodic discovery beacons
if (this.config.autoDiscover) {
await this.transport.discover();
this.discoveryTimer = setInterval(() => {
this.transport.discover().catch(() => {});
}, this.config.discoveryInterval);
}
this.booted = true;
return fullIdentity;
}
/**
* Shut down the node cleanly.
*/
shutdown(): void {
if (this.broadcastTimer) {
clearInterval(this.broadcastTimer);
this.broadcastTimer = null;
}
if (this.discoveryTimer) {
clearInterval(this.discoveryTimer);
this.discoveryTimer = null;
}
this.transport.close();
this.peerCallbacks.clear();
this.bondCallbacks.clear();
this.stateCallbacks.clear();
this.bonds.clear();
this.booted = false;
}
// ─── State ──────────────────────────────────────────────────────
/**
* Update a state axis and trigger a broadcast.
*/
async updateState(axis: Axis, value: number): Promise<void> {
await this.state.updateAxis(axis, value);
await this.broadcastState();
}
/**
* Get the current voltage (composite stress metric).
*/
getVoltage(): number {
return this.state.getComposite().composite as number;
}
/**
* Get the current spoon count.
*/
getSpoons(): number {
return this.state.getSpoonCount();
}
/**
* Get the current scope tier.
*/
getTier(): string {
return this.state.getCurrentTier();
}
// ─── Bonds ──────────────────────────────────────────────────────
/**
* Initiate a bond with a discovered peer.
* Bypasses the ChannelManager stub and handles ECDH directly.
*/
async initiateBond(peer: DiscoveredPeer): Promise<LocalBond | null> {
if (!peer.publicKey || peer.publicKey.length < 33) return null;
const peerPubKey = peer.publicKey.slice(0, 33) as CompressedPublicKey;
const peerNodeId = await deriveNodeId(peerPubKey);
// Don't bond with self
if (peerNodeId === this.identity.getNodeId()) return null;
// Don't duplicate bonds
if (this.bonds.has(peerNodeId as string)) {
return this.bonds.get(peerNodeId as string)!;
}
// Enforce K4 topology (max 4 bonds)
if (this.bonds.size >= 4) return null;
// Derive shared secret via ECDH + HKDF
const ecdhPrivate = this.identity.getECDHPrivateKey();
const sharedSecret = await deriveSharedSecret(
ecdhPrivate,
peerPubKey,
"node-zero-bond-v1"
);
const now = Math.floor(Date.now() / 1000);
const bond: LocalBond = {
peerId: peerNodeId,
peerPublicKey: peerPubKey,
sharedSecret,
status: "ACTIVE",
trustTier: "STRUT",
careScore: 0.5,
createdAt: now,
lastInteraction: now,
totalExchanges: 0,
};
this.bonds.set(peerNodeId as string, bond);
for (const cb of this.bondCallbacks) {
cb(bond);
}
return bond;
}
/**
* Get all active bonds.
*/
getActiveBonds(): LocalBond[] {
return Array.from(this.bonds.values()).filter(
(b) => b.status === "ACTIVE"
);
}
/**
* Get a specific bond.
*/
getBond(peerId: NodeId): LocalBond | undefined {
return this.bonds.get(peerId as string);
}
/**
* Close a bond.
*/
closeBond(peerId: NodeId): void {
this.bonds.delete(peerId as string);
}
// ─── Events ─────────────────────────────────────────────────────
/**
* Register callback for peer discovery.
*/
onPeerDiscovered(callback: (peer: DiscoveredPeer) => void): () => void {
this.peerCallbacks.add(callback);
return () => this.peerCallbacks.delete(callback);
}
/**
* Register callback for bond formation.
*/
onBondFormed(callback: (bond: LocalBond) => void): () => void {
this.bondCallbacks.add(callback);
return () => this.bondCallbacks.delete(callback);
}
/**
* Register callback for receiving remote state updates.
*/
onRemoteState(
callback: (
nodeId: NodeId,
state: { voltage: number; spoons: number; tier: string }
) => void
): () => void {
this.stateCallbacks.add(callback);
return () => this.stateCallbacks.delete(callback);
}
/**
* Trigger peer discovery.
*/
async discover(): Promise<void> {
await this.transport.discover();
}
/**
* Get current node summary.
*/
getStatus(): {
nodeId: NodeId;
voltage: number;
spoons: number;
tier: string;
bondCount: number;
peers: readonly DiscoveredPeer[];
} {
return {
nodeId: this.identity.getNodeId(),
voltage: this.getVoltage(),
spoons: this.getSpoons(),
tier: this.getTier(),
bondCount: this.bonds.size,
peers: this.transport.getDiscoveredPeers(),
};
}
// ─── Internal ───────────────────────────────────────────────────
private async broadcastState(): Promise<void> {
const now = Math.floor(Date.now() / 1000);
if (now - this.lastBroadcastTime < 5) return;
this.lastBroadcastTime = now;
try {
const composite = this.state.getComposite();
const pubKey = this.identity.getCompressedPublicKey();
const keySequence = (await this.identity.exportPublicKey()).recovery
.keySequence;
// Quantize float values to uint8 for wire format
const urgencyByte = Math.round(
(composite.magnitudes.URGENCY as number) * 255
) as U8;
const emotionalByte = Math.round(
((composite.magnitudes.VALENCE as number) + 1) * 127.5
) as U8;
const cognitiveByte = Math.round(
(composite.magnitudes.COGNITIVE as number) * 255
) as U8;
const stateData: StateWireData = {
urgency: urgencyByte,
emotional: emotionalByte,
cognitive: cognitiveByte,
timestamp: now as UnixTimestamp,
};
// Build signed message: pubKey || urgency || emotional || cognitive
const sigPayload = new Uint8Array(36);
sigPayload.set(pubKey, 0);
sigPayload[33] = urgencyByte as number;
sigPayload[34] = emotionalByte as number;
sigPayload[35] = cognitiveByte as number;
const signature = await this.identity.sign(sigPayload);
const wireMessage = serializeStateUpdate({
identity: { publicKey: pubKey, keySequence },
stateData,
signature,
});
await this.transport.transmit(wireMessage);
} catch {
// Best-effort broadcast
}
}
private handleIncomingData(data: Uint8Array): void {
if (data.length === 105) {
try {
const update = deserializeStateUpdate(data);
const peerPubKey = update.identity.publicKey;
// Don't process our own broadcasts
const localPub = this.identity.getCompressedPublicKey();
if (arrayEqual(peerPubKey, localPub)) return;
// Dequantize wire format back to floats
const urgency = (update.stateData.urgency as number) / 255;
const cognitive = (update.stateData.cognitive as number) / 255;
const emotional =
(update.stateData.emotional as number) / 127.5 - 1;
const voltage =
(urgency + Math.abs(emotional) * 0.5 + cognitive) / 2.5;
const clampedVoltage = Math.max(0, Math.min(1, voltage));
const spoons = Math.max(
1,
Math.min(12, Math.round(12 * (1 - clampedVoltage)))
);
const tier =
spoons >= 9 ? "FULL" : spoons >= 4 ? "PATTERN" : "REFLEX";
deriveNodeId(peerPubKey).then((peerId) => {
for (const cb of this.stateCallbacks) {
cb(peerId, { voltage: clampedVoltage, spoons, tier });
}
});
} catch {
// Invalid state update — ignore
}
}
}
}
// ─── Utility ───────────────────────────────────────────────────────
function arrayEqual(a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}