UNPKG

@zenithcore/core

Version:

Core functionality for ZenithKernel framework

650 lines (557 loc) 18.9 kB
/** * Router Integration with ZenithKernel Systems * Connects router with ECS, Hydra, WASM, and other kernel systems */ import { ZenithRouter, RouteDefinition } from './router'; import { AuthGuardFactory, CommonGuards } from './guards'; import { ZenithKernel } from '../ZenithKernel'; import { RegistryServer } from '@modules/RegistryServer/RegistryServer'; import { signal, computed } from '../signals'; import { handleVerifyRequest, VerifySystem } from '@modules/RegistryServer/VerifySystem'; import { ComponentType } from 'react'; import { HydraLoader } from '@components/hydra/HydraLoader'; import { ChallengeSystem } from '@modules/RegistryServer/ChallengeSystem'; /** * Enhanced router with ZenithKernel integration */ export class ZenithRouterIntegration { [x: string]: any; private router: ZenithRouter; private kernel: ZenithKernel; private registryServer: RegistryServer; private guardFactory?: AuthGuardFactory; // Reactive state signals public readonly isHydraRoute = signal(false); public readonly currentHydraContext = signal(null); public readonly zkVerificationStatus = signal<'pending' | 'verified' | 'failed' | null>(null); constructor( router: ZenithRouter, kernel: ZenithKernel, registryServer: RegistryServer ) { this.router = router; this.kernel = kernel; this.registryServer = registryServer; this.setupRouterIntegration(); } /** * Register Hydra-aware routes */ registerHydraRoutes(routes: Array<RouteDefinition & { hydraId?: string; execType?: 'local' | 'remote' | 'edge'; zkRequired?: boolean; trustLevel?: 'unverified' | 'local' | 'community' | 'verified'; }>): void { const enhancedRoutes = routes.map(route => ({ ...route, component: route.hydraId ? this.createHydraComponent(route.hydraId, route.execType || 'local') : route.component, guards: [ ...(route.guards || []), ...(route.zkRequired ? [this.createZKGuard(route.trustLevel)] : []) ], loader: route.loader || (route.hydraId ? this.createHydraLoader(route.hydraId) : undefined) })); this.router.register(enhancedRoutes as RouteDefinition[]); } /** * Create ZK-verified navigation function */ createSecureNavigator<TPath extends string>( path: TPath, options: { requireZKProof?: boolean; trustLevel?: 'unverified' | 'local' | 'community' | 'verified'; challengeType?: string; } = {} ) { return async (params: any, navOptions?: any) => { // Generate challenge if ZK proof required if (options.requireZKProof) { const challenge = await this.registryServer.handleChallenge({ body: { publicKey: params.peerId || 'anonymous' } }); // Wait for proof verification this.zkVerificationStatus.value = 'pending'; try { const proof = await this.waitForZKProof(challenge as unknown as string); const isValid: boolean | void = await handleVerifyRequest(this.kernel, { type: 'auth/verify/request', payload: { challenge: challenge as unknown as string, proof, replyTo: 'zenith-router' } }); if (!isValid as boolean) { this.zkVerificationStatus.value = 'failed'; throw new Error('ZK proof verification failed'); } this.zkVerificationStatus.value = 'verified'; } catch (error) { this.zkVerificationStatus.value = 'failed'; throw error; } } // Proceed with navigation const navigator = this.router.createNavigator(path); return navigator(params, navOptions); }; } /** * Register ECS-integrated routes */ registerECSRoutes(routes: Array<RouteDefinition & { ecsComponents?: string[]; ecsQueries?: Array<{ include: string[]; exclude?: string[]; }>; }>): void { const enhancedRoutes = routes.map(route => ({ ...route, loader: async (params: any, query: any) => { let data = {}; if (route.loader && typeof route.loader === 'function') { data = await route.loader(params.toString(), query); } // Fetch ECS data if specified if (route.ecsComponents) { const ecsManager = this.kernel.getECS(); if (!ecsManager) { throw new Error('ECSManager not found in kernel'); } const ecsData: { [key: string]: unknown } = {}; for (const componentName of route.ecsComponents) { // This would integrate with the actual ECS system this.kernel.getSystem(`${componentName}`); } return { ...data, ecs: ecsData }; } return data; } })); this.router.register(enhancedRoutes); } /** * Create WASM-integrated routes */ registerWasmRoutes(routes: Array<RouteDefinition & { wasmManifest?: string; wasmSandbox?: boolean; }>): void { const enhancedRoutes = routes.map(route => ({ ...route, component: route.wasmManifest ? this.createWasmComponent(route.wasmManifest, route.wasmSandbox) : route.component, guards: [ ...(route.guards || []), this.createWasmSecurityGuard() ] })); this.router.register(enhancedRoutes); } /** * Register quantum-consensus routes */ registerQuantumRoutes(routes: Array<RouteDefinition & { quantumConsensus?: boolean; consensusThreshold?: number; }>): void { const enhancedRoutes = routes.map(route => ({ ...route, guards: [ ...(route.guards || []), ...(route.quantumConsensus ? [this.createQuantumConsensusGuard(route.consensusThreshold)] : []) ], loader: route.quantumConsensus ? this.createQuantumLoader(route.loader) : route.loader })); this.router.register(enhancedRoutes); } // Private implementation methods private setupRouterIntegration(): void { // Listen to route changes and update Hydra context this.router.getCurrentRoute().subscribe(route => { if (route) { this.isHydraRoute.value = !!route.route.meta?.hydraId; if (route.route.meta?.hydraId) { //@ts-ignore this.currentHydraContext.value = { hydraId: route.route.meta.hydraId, params: route.params, query: route.query }; } else { this.currentHydraContext.value = null; } } }); // Setup ECS integration for route state this.setupECSIntegration(); } private setupECSIntegration(): void { const ecsManager = this.kernel.getECS(); if (!ecsManager) { throw new Error('ECSManager not found in kernel'); } if (this.kernel.getECS()) { // Ensure ECSManager is initialized // Create router entity in ECS const routerEntity = ecsManager.createEntity(); // Add router state component this.kernel.getECS().addComponent(routerEntity, { // @ts-ignore id: 'RouterState', currentRoute: this.router.getCurrentRouteSignal().value, isNavigating: false }, 'RouterState' as unknown); // Sync router state with ECS this.router.getState().subscribe(state => { //@ts-ignore this.kernel.getECS().updateComponent(routerEntity, {currentRoute: state?.currentRoute, isNavigating: state?.isNavigating}, 'RouterState' as unknown); }); } } private createHydraComponent(hydraId: string, execType: 'local' | 'remote' | 'edge') { return async () => { // Import React dynamically const React = await import('react'); return ({ params, query }: any) => { return React.createElement(typeof HydraLoader, { id: `route-${hydraId}`, entry: hydraId, execType, context: { peerId: params.peerId || query.peerId, zkProof: query.zkProof, routeParams: params, routeQuery: query }, props: { params, query } }); }; }; } private createHydraLoader(hydraId: string) { return async (params: any, query: any) => { // Load Hydra manifest and prepare context const manifest = await this.registryServer.getEntityRegistry({ hydraId }); if (!manifest) { throw new Error(`Hydra manifest not found for id: ${hydraId}`); } return { hydraManifest: manifest, hydraContext: { peerId: params.peerId || query.peerId, zkProof: query.zkProof, routeParams: params, routeQuery: query } }; }; } private createWasmComponent(manifestUrl: string, sandbox: boolean = true) { return async () => { const WasmModuleProxy = await import('../WasmModuleProxy'); return ({ params, query }: any) => { const React = require('react'); const [module, setModule] = React.useState(null); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(null); React.useEffect(() => { const loadWasm = async () => { try { // @ts-ignore const wasmProxy:any = new WasmModuleProxy.default(); const loadedModule = await wasmProxy.loadWasmModule({ manifestUrl, sandbox }); setModule(loadedModule); } catch (err) { setError(err); } finally { setLoading(false); } }; loadWasm(); }, []); if (loading) return React.createElement('div', null, 'Loading WASM module...'); if (error) return React.createElement('div', null, `Error: ${error.message}`); if (!module) return React.createElement('div', null, 'Module not found'); return React.createElement('div', { ref: (el: HTMLElement) => { if (el && module) { module.mount(el, { params, query }); } } }); }; }; } private createZKGuard(trustLevel?: string) { return this.guardFactory?.createZKGuard({ zkProofRequired: true, trustLevelRequired: trustLevel as any || 'local', redirectTo: '/auth/zk' }); } private createWasmSecurityGuard() { return { canActivate: async (params: any, query: any) => { // Verify WASM module signature and permissions const wasmSecurity:any = this.registryServer.getEntityRegistry( { hydraId: 'WasmSecurity' } ); if (!wasmSecurity) { throw new Error('WasmSecurity system not found'); } if (wasmSecurity) { return wasmSecurity.verifyWasmSecurity(query.manifestUrl); } return true; }, redirectTo: '/security/wasm-blocked' }; } private createQuantumConsensusGuard(threshold: number = 0.66) { return { canActivate: async (params: any, query: any) => { const quantumConsensus:any = this.kernel.getSystem('QuantumConsensus'); if (quantumConsensus) { const consensus = await quantumConsensus.checkConsensus( `route:${params.toString()}`, threshold ); return consensus.reached; } return true; }, redirectTo: '/consensus/pending' }; } private createQuantumLoader(originalLoader?: any) { return async (params: any, query: any) => { const data = originalLoader ? await originalLoader(params, query) : {}; // Add quantum consensus data const quantumConsensus:any = this.kernel.getSystem('QuantumConsensus'); if (quantumConsensus) { const consensusData = await quantumConsensus.getConsensusState(); return { ...data, quantum: consensusData }; } return data; }; } private async waitForZKProof(challengeId: string): Promise<string> { // This would implement a real challenge-response mechanism return new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error('ZK proof challenge timeout')); }, 30000); // 30 second timeout // Mock implementation - in real system this would listen for proof submission setTimeout(() => { clearTimeout(timeout); resolve(`zk:proof:${challengeId}:verified`); }, 1000); }); } // Public API methods /** * Navigate with ZK verification */ async navigateSecure( path: string, params: any = {}, options: { zkProof?: string; trustLevel?: string; challenge?: boolean; } = {} ): Promise<void> { if (options.challenge || options.zkProof) { this.zkVerificationStatus.value = 'pending'; try { if (options.zkProof) { const isValid = await this.verifySystem.verifyProof( params.peerId || 'anonymous', options.zkProof ); if (!isValid) { this.zkVerificationStatus.value = 'failed'; throw new Error('ZK proof verification failed'); } } this.zkVerificationStatus.value = 'verified'; } catch (error) { this.zkVerificationStatus.value = 'failed'; throw error; } } return this.router.navigate(path, { state: { params, ...options } }); } /** * Get router with full ZenithKernel integration */ getRouter(): ZenithRouter { return this.router; } /** * Get current Hydra context */ getCurrentHydraContext() { return this.currentHydraContext.value; } /** * Check if current route is Hydra-enabled */ isCurrentRouteHydra(): boolean { return this.isHydraRoute.value; } /** * Get ZK verification status */ getZKStatus() { return this.zkVerificationStatus.value; } } /** * Factory for creating integrated router */ export function createZenithRouter(kernel: ZenithKernel): ZenithRouterIntegration { const router = new ZenithRouter(); const registryServer = kernel.getSystem('RegistryServer') as unknown as RegistryServer; return new ZenithRouterIntegration( router, kernel, registryServer ); } /** * Router middleware for ZenithKernel systems */ export class RouterMiddleware { private integration: ZenithRouterIntegration; constructor(integration: ZenithRouterIntegration) { this.integration = integration; } /** * Middleware for ECS state injection */ ecsStateMiddleware() { return async (params: any, query: any, next: Function) => { const ecsManager:any = this.integration['kernel'].getSystem('ECSManager'); if (ecsManager) { const ecsState = { entities: ecsManager.getAllEntities?.() || [], components: ecsManager.getAllComponents?.() || {}, systems: ecsManager.getActiveSystems?.() || [] }; // Inject ECS state into route context return next({ ...params, ecs: ecsState }, query); } return next(params, query); }; } /** * Middleware for ZK proof validation */ zkValidationMiddleware(required: boolean = false) { return async (params: any, query: any, next: Function) => { if (required && !query.zkProof) { throw new Error('ZK proof required for this route'); } if (query.zkProof) { const verifySystem = this.integration['verifySystem']; const isValid = await verifySystem.verifyProof( params.peerId || query.peerId || 'anonymous', query.zkProof ); if (!isValid) { throw new Error('Invalid ZK proof'); } return next({ ...params, zkVerified: true }, query); } return next(params, query); }; } /** * Middleware for Hydra context preparation */ hydraContextMiddleware() { return async (params: any, query: any, next: Function) => { const hydraContext = { peerId: params.peerId || query.peerId, zkProof: query.zkProof, trustLevel: this.deriveTrustLevel(query.zkProof), routeParams: params, routeQuery: query, timestamp: Date.now() }; return next({ ...params, hydraContext }, query); }; } /** * Middleware for quantum consensus validation */ quantumConsensusMiddleware(threshold: number = 0.66) { return async (params: any, query: any, next: Function) => { const quantumConsensus:any = this.integration['kernel'].getSystem('QuantumConsensus'); if (quantumConsensus) { const routeKey = `route:${JSON.stringify(params)}:${JSON.stringify(query)}`; const consensus = await quantumConsensus.checkConsensus(routeKey, threshold); if (!consensus.reached) { throw new Error('Quantum consensus not reached for this route'); } return next({ ...params, quantumConsensus: consensus }, query); } return next(params, query); }; } private deriveTrustLevel(zkProof?: string): 'unverified' | 'local' | 'community' | 'verified' { if (!zkProof) return 'unverified'; if (zkProof.startsWith('zk:verified:')) return 'verified'; if (zkProof.startsWith('zk:community:')) return 'community'; if (zkProof.startsWith('zk:local:')) return 'local'; return 'unverified'; } } /** * Route decorator for enhanced functionality */ export function RouteEnhancer(options: { zkRequired?: boolean; trustLevel?: 'unverified' | 'local' | 'community' | 'verified'; ecsComponents?: string[]; quantumConsensus?: boolean; cacheStrategy?: 'aggressive' | 'normal' | 'minimal'; }) { return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) { const originalMethod = descriptor.value; descriptor.value = async function(...args: any[]) { const [params, query] = args; // Apply enhancements based on options let enhancedParams = { ...params }; let enhancedQuery = { ...query }; if (options.zkRequired && !query.zkProof) { throw new Error('ZK proof required for this route'); } if (options.ecsComponents) { // Inject ECS component data const ecsData: Record<string, any> = {}; // This would fetch actual ECS data options.ecsComponents.forEach(component => { ecsData[component] = `mock-${component}-data`; }); enhancedParams = { ...enhancedParams, ecs: ecsData }; } return originalMethod.apply(this, [enhancedParams, enhancedQuery]); }; return descriptor; }; } export default ZenithRouterIntegration;