localgreenchain/lib/transparency/index.ts
Claude 0fcc2763fe
Add comprehensive transparency system for LocalGreenChain
This commit introduces a complete transparency infrastructure including:

Core Transparency Modules:
- AuditLog: Immutable, cryptographically-linked audit trail for all actions
- EventStream: Real-time SSE streaming and webhook support
- TransparencyDashboard: Aggregated metrics and system health monitoring
- DigitalSignatures: Cryptographic verification for handoffs and certificates

API Endpoints:
- /api/transparency/dashboard - Full platform metrics
- /api/transparency/audit - Query and log audit entries
- /api/transparency/events - SSE stream and event history
- /api/transparency/webhooks - Webhook management
- /api/transparency/signatures - Digital signature operations
- /api/transparency/certificate/[plantId] - Plant authenticity certificates
- /api/transparency/export - Multi-format data export
- /api/transparency/report - Compliance reporting
- /api/transparency/health - System health checks

Features:
- Immutable audit logging with chain integrity verification
- Real-time event streaming via Server-Sent Events
- Webhook support with HMAC signature verification
- Digital signatures for transport handoffs and ownership transfers
- Certificate of Authenticity generation for plants
- Multi-format data export (JSON, CSV, summary)
- Public transparency portal at /transparency
- System health monitoring for all components

Documentation:
- Comprehensive TRANSPARENCY.md guide with API examples
2025-11-23 03:29:56 +00:00

104 lines
2.8 KiB
TypeScript

/**
* LocalGreenChain Transparency Module
*
* Comprehensive transparency system providing:
* - Immutable audit logging
* - Real-time event streaming
* - Transparency dashboards
* - Data export & reporting
* - Digital signatures for verification
*/
export * from './AuditLog';
export * from './EventStream';
export * from './TransparencyDashboard';
export * from './DigitalSignatures';
import { getAuditLog } from './AuditLog';
import { getEventStream } from './EventStream';
import { getTransparencyDashboard } from './TransparencyDashboard';
import { getSignatureManager } from './DigitalSignatures';
/**
* Initialize all transparency components
*/
export function initializeTransparency(): void {
console.log('[Transparency] Initializing transparency module...');
// Initialize components
const auditLog = getAuditLog();
const eventStream = getEventStream();
const dashboard = getTransparencyDashboard();
const signatures = getSignatureManager();
// Log initialization
auditLog.logSystemEvent('Transparency module initialized', 'INFO', {
components: ['AuditLog', 'EventStream', 'TransparencyDashboard', 'DigitalSignatures']
});
// Emit initialization event
eventStream.emit('system.health', 'transparency', {
status: 'initialized',
components: ['AuditLog', 'EventStream', 'TransparencyDashboard', 'DigitalSignatures']
});
console.log('[Transparency] Transparency module ready');
}
/**
* Middleware helper for API routes to log requests
*/
export function createAuditMiddleware() {
const auditLog = getAuditLog();
const eventStream = getEventStream();
return {
logRequest: (
endpoint: string,
method: string,
actor: { id?: string; ip?: string; userAgent?: string }
) => {
const correlationId = `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
auditLog.logApiCall(endpoint, method, {
id: actor.id || 'anonymous',
type: actor.id ? 'USER' : 'ANONYMOUS',
ip: actor.ip,
userAgent: actor.userAgent
}, {});
return correlationId;
},
logResponse: (
endpoint: string,
method: string,
statusCode: number,
responseTime: number,
error?: string
) => {
const severity = error ? 'ERROR' : statusCode >= 400 ? 'WARNING' : 'INFO';
if (severity !== 'INFO') {
eventStream.emit('system.alert', 'api', {
endpoint,
method,
statusCode,
error
}, { priority: severity === 'ERROR' ? 'HIGH' : 'NORMAL' });
}
}
};
}
/**
* Quick access to transparency components
*/
export const transparency = {
get audit() { return getAuditLog(); },
get events() { return getEventStream(); },
get dashboard() { return getTransparencyDashboard(); },
get signatures() { return getSignatureManager(); }
};
export default transparency;