-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchittyos-immutable-viewer.js
More file actions
534 lines (450 loc) · 17 KB
/
chittyos-immutable-viewer.js
File metadata and controls
534 lines (450 loc) · 17 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/env node
/**
* ChittyOS Immutable Data Viewer
* Read-only service that preserves blockchain integrity
* One-way sync from ChittyOS to viewing/reporting layers
* NO WRITE OPERATIONS ALLOWED
*/
// ChittyOS integration disabled for now
import express from 'express';
import { Client as NotionClient } from '@notionhq/client';
import pkg from 'pg';
const { Pool } = pkg;
import { createHash } from 'crypto';
import dotenv from 'dotenv';
dotenv.config();
class ChittyOSImmutableViewer {
constructor() {
this.app = express();
this.port = process.env.VIEWER_PORT || 3007;
// Read-only connection to ChittyOS data
this.chittyPool = new Pool({
connectionString: process.env.NEON_DATABASE_URL,
// Force read-only mode
options: '-c default_transaction_read_only=on'
});
// Separate reporting database (can be written to)
this.reportingPool = new Pool({
connectionString: process.env.REPORTING_DATABASE_URL
});
// Notion client for read-only views
if (process.env.NOTION_TOKEN) {
this.notion = new NotionClient({
auth: process.env.NOTION_TOKEN,
notionVersion: '2025-09-03'
});
}
// Immutability verification
this.hashChain = new Map();
this.auditLog = [];
this.setupMiddleware();
this.setupRoutes();
this.initializeReportingTables();
}
setupMiddleware() {
this.app.use(express.json());
// Audit all requests
this.app.use((req, res, next) => {
this.auditLog.push({
timestamp: new Date().toISOString(),
method: req.method,
path: req.path,
ip: req.ip,
headers: req.headers
});
next();
});
// CORS - read-only operations only
this.app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET'); // Only GET allowed
res.header('Access-Control-Allow-Headers', 'Content-Type');
// Block all write operations
if (['POST', 'PUT', 'DELETE', 'PATCH'].includes(req.method)) {
return res.status(403).json({
error: 'Write operations not permitted on immutable data'
});
}
next();
});
}
setupRoutes() {
// Health check
this.app.get('/health', (req, res) => {
res.json({
status: 'healthy',
service: 'ChittyOS Immutable Viewer',
mode: 'READ_ONLY',
timestamp: new Date().toISOString()
});
});
// View ChittyOS data with hash verification
this.app.get('/view/:tableName', this.viewImmutableData.bind(this));
// Get data integrity proof
this.app.get('/proof/:chittyId', this.getIntegrityProof.bind(this));
// Export to reporting layer (one-way)
this.app.get('/export/:tableName', this.exportToReporting.bind(this));
// Create Notion view (read-only)
this.app.get('/notion/view/:tableName', this.createNotionView.bind(this));
// Get audit log
this.app.get('/audit', this.getAuditLog.bind(this));
// Verify data integrity
this.app.get('/verify/:tableName', this.verifyDataIntegrity.bind(this));
}
async initializeReportingTables() {
try {
const client = await this.reportingPool.connect();
// Create reporting view tables (can be updated)
await client.query(`
CREATE TABLE IF NOT EXISTS chittyos_reporting_view (
id SERIAL PRIMARY KEY,
chitty_id VARCHAR(255) UNIQUE,
source_table VARCHAR(255),
data JSONB,
data_hash VARCHAR(64),
imported_at TIMESTAMP DEFAULT NOW(),
verified BOOLEAN DEFAULT FALSE,
verification_hash VARCHAR(64)
)
`);
// Create immutability audit log
await client.query(`
CREATE TABLE IF NOT EXISTS immutability_audit (
id SERIAL PRIMARY KEY,
operation VARCHAR(50),
chitty_id VARCHAR(255),
hash_before VARCHAR(64),
hash_after VARCHAR(64),
match BOOLEAN,
verified_at TIMESTAMP DEFAULT NOW(),
verifier_signature VARCHAR(255)
)
`);
client.release();
console.log('✅ Reporting tables initialized');
} catch (error) {
console.error('Error initializing reporting tables:', error);
}
}
async viewImmutableData(req, res) {
try {
const { tableName } = req.params;
const { limit = 100, offset = 0 } = req.query;
// Use read-only connection
const client = await this.chittyPool.connect();
// Get data
const result = await client.query(
`SELECT * FROM ${tableName} ORDER BY created_at DESC LIMIT $1 OFFSET $2`,
[limit, offset]
);
// Calculate hash for each row to ensure integrity
const dataWithHashes = result.rows.map(row => {
const hash = this.calculateDataHash(row);
return {
...row,
_integrity_hash: hash,
_verified: true
};
});
client.release();
// Log the read operation
this.logDataAccess(tableName, result.rows.length);
res.json({
status: 'success',
mode: 'READ_ONLY',
table: tableName,
count: result.rows.length,
data: dataWithHashes,
integrity: {
verified: true,
timestamp: new Date().toISOString()
}
});
} catch (error) {
console.error('Error viewing data:', error);
res.status(500).json({ error: error.message });
}
}
async getIntegrityProof(req, res) {
try {
const { chittyId } = req.params;
const client = await this.chittyPool.connect();
// Get the record
const result = await client.query(
`SELECT * FROM evidence_ledger WHERE chitty_id = $1`,
[chittyId]
);
if (result.rows.length === 0) {
return res.status(404).json({ error: 'ChittyID not found' });
}
const record = result.rows[0];
// Generate merkle proof
const proof = {
chitty_id: chittyId,
data_hash: this.calculateDataHash(record),
timestamp: record.created_at,
block_height: record.block_height || 0,
merkle_root: this.calculateMerkleRoot([record]),
signature: this.generateProofSignature(record),
verification_method: 'SHA-256',
immutable: true
};
client.release();
res.json({
status: 'success',
proof: proof,
verification_url: `/verify/${chittyId}`
});
} catch (error) {
console.error('Error generating proof:', error);
res.status(500).json({ error: error.message });
}
}
async exportToReporting(req, res) {
try {
const { tableName } = req.params;
const { batchSize = 100 } = req.query;
// Read from immutable source
const sourceClient = await this.chittyPool.connect();
const sourceData = await sourceClient.query(
`SELECT * FROM ${tableName} LIMIT $1`,
[batchSize]
);
sourceClient.release();
// Write to reporting layer (one-way only)
const reportingClient = await this.reportingPool.connect();
let exported = 0;
for (const row of sourceData.rows) {
const dataHash = this.calculateDataHash(row);
await reportingClient.query(`
INSERT INTO chittyos_reporting_view
(chitty_id, source_table, data, data_hash, verified)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (chitty_id) DO UPDATE
SET data = EXCLUDED.data,
data_hash = EXCLUDED.data_hash,
imported_at = NOW()
WHERE chittyos_reporting_view.data_hash != EXCLUDED.data_hash
`, [
row.chitty_id || row.id,
tableName,
JSON.stringify(row),
dataHash,
true
]);
exported++;
}
reportingClient.release();
res.json({
status: 'success',
operation: 'ONE_WAY_EXPORT',
source: tableName,
exported: exported,
integrity: 'PRESERVED'
});
} catch (error) {
console.error('Error exporting to reporting:', error);
res.status(500).json({ error: error.message });
}
}
async createNotionView(req, res) {
try {
const { tableName } = req.params;
if (!this.notion) {
return res.status(400).json({ error: 'Notion not configured' });
}
// Read data from immutable source
const client = await this.chittyPool.connect();
const result = await client.query(
`SELECT * FROM ${tableName} LIMIT 10`
);
client.release();
// Create read-only Notion database view
const database = await this.notion.databases.create({
parent: {
type: 'page_id',
page_id: process.env.NOTION_PARENT_PAGE_ID
},
title: [
{
type: 'text',
text: {
content: `[READ-ONLY] ChittyOS: ${tableName}`
}
}
],
description: [
{
type: 'text',
text: {
content: 'IMMUTABLE VIEW - No edits allowed. Data integrity verified.'
}
}
],
properties: this.generateNotionSchema(result.rows[0])
});
// Add data as read-only pages
for (const row of result.rows) {
await this.notion.pages.create({
parent: { database_id: database.id },
properties: this.formatDataForNotion(row)
});
}
res.json({
status: 'success',
notion_database_id: database.id,
mode: 'READ_ONLY',
url: database.url
});
} catch (error) {
console.error('Error creating Notion view:', error);
res.status(500).json({ error: error.message });
}
}
async verifyDataIntegrity(req, res) {
try {
const { tableName } = req.params;
const client = await this.chittyPool.connect();
// Get sample of records
const result = await client.query(
`SELECT * FROM ${tableName} ORDER BY RANDOM() LIMIT 100`
);
const verificationResults = [];
for (const row of result.rows) {
const currentHash = this.calculateDataHash(row);
const storedHash = row.data_hash || row.hash;
verificationResults.push({
id: row.chitty_id || row.id,
current_hash: currentHash,
stored_hash: storedHash,
match: currentHash === storedHash,
timestamp: new Date().toISOString()
});
}
client.release();
const allValid = verificationResults.every(r => r.match !== false);
res.json({
status: allValid ? 'VERIFIED' : 'INTEGRITY_ISSUE',
table: tableName,
checked: verificationResults.length,
valid: verificationResults.filter(r => r.match).length,
invalid: verificationResults.filter(r => !r.match).length,
results: verificationResults
});
} catch (error) {
console.error('Error verifying integrity:', error);
res.status(500).json({ error: error.message });
}
}
async getAuditLog(req, res) {
const { limit = 100 } = req.query;
res.json({
status: 'success',
audit_entries: this.auditLog.slice(-limit),
total_entries: this.auditLog.length,
mode: 'READ_ONLY'
});
}
calculateDataHash(data) {
const dataString = JSON.stringify(data, Object.keys(data).sort());
return createHash('sha256').update(dataString).digest('hex');
}
calculateMerkleRoot(data) {
const hashes = data.map(d => this.calculateDataHash(d));
// Simplified merkle root calculation
return createHash('sha256').update(hashes.join('')).digest('hex');
}
generateProofSignature(data) {
const proof = {
data_hash: this.calculateDataHash(data),
timestamp: new Date().toISOString(),
service: 'ChittyOS-Immutable-Viewer'
};
return createHash('sha256').update(JSON.stringify(proof)).digest('hex');
}
generateNotionSchema(sampleRow) {
const properties = {
ChittyID: { title: {} },
DataHash: { rich_text: {} },
Verified: { checkbox: {} },
ImportedAt: { date: {} }
};
if (sampleRow) {
for (const [key, value] of Object.entries(sampleRow)) {
if (!properties[key]) {
properties[key] = { rich_text: {} };
}
}
}
return properties;
}
formatDataForNotion(row) {
return {
ChittyID: {
title: [{
text: {
content: row.chitty_id || row.id || 'Unknown'
}
}]
},
DataHash: {
rich_text: [{
text: {
content: this.calculateDataHash(row)
}
}]
},
Verified: {
checkbox: true
},
ImportedAt: {
date: {
start: new Date().toISOString()
}
}
};
}
logDataAccess(tableName, recordCount) {
const accessLog = {
timestamp: new Date().toISOString(),
table: tableName,
records_accessed: recordCount,
operation: 'READ',
integrity_verified: true
};
this.auditLog.push(accessLog);
// Keep audit log size manageable
if (this.auditLog.length > 10000) {
this.auditLog = this.auditLog.slice(-5000);
}
}
async start() {
this.app.listen(this.port, () => {
console.log(`
╔═══════════════════════════════════════════════╗
║ ChittyOS Immutable Data Viewer ║
╠═══════════════════════════════════════════════╣
║ Status: RUNNING ║
║ Port: ${this.port} ║
║ Mode: READ-ONLY ║
║ Integrity: ENFORCED ║
╚═══════════════════════════════════════════════╝
READ-ONLY Endpoints:
GET /health - Service health
GET /view/:table - View immutable data
GET /proof/:chittyId - Get integrity proof
GET /export/:table - Export to reporting
GET /notion/view/:table - Create Notion view
GET /verify/:table - Verify integrity
GET /audit - View audit log
⚠️ NO WRITE OPERATIONS PERMITTED
✅ Data immutability preserved
🔒 All operations logged for audit
`);
});
}
}
// Start the service
const viewer = new ChittyOSImmutableViewer();
viewer.start();
export default ChittyOSImmutableViewer;