@nekzus/liop 1.2.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +413 -0
- package/dist/bin/agent.d.ts +2 -0
- package/dist/bin/agent.js +307 -0
- package/dist/bridge/index.d.ts +37 -0
- package/dist/bridge/index.js +249 -0
- package/dist/bridge/stream.d.ts +62 -0
- package/dist/bridge/stream.js +202 -0
- package/dist/client/index.d.ts +60 -0
- package/dist/client/index.js +275 -0
- package/dist/crypto/logic-image-id.d.ts +3 -0
- package/dist/crypto/logic-image-id.js +27 -0
- package/dist/crypto/verifier.d.ts +29 -0
- package/dist/crypto/verifier.js +96 -0
- package/dist/economy/estimator.d.ts +53 -0
- package/dist/economy/estimator.js +69 -0
- package/dist/economy/index.d.ts +5 -0
- package/dist/economy/index.js +3 -0
- package/dist/economy/otel.d.ts +38 -0
- package/dist/economy/otel.js +100 -0
- package/dist/economy/telemetry.d.ts +77 -0
- package/dist/economy/telemetry.js +224 -0
- package/dist/gateway/hybrid.d.ts +23 -0
- package/dist/gateway/hybrid.js +199 -0
- package/dist/gateway/router.d.ts +69 -0
- package/dist/gateway/router.js +1036 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/mesh/index.d.ts +1 -0
- package/dist/mesh/index.js +1 -0
- package/dist/mesh/node.d.ts +129 -0
- package/dist/mesh/node.js +853 -0
- package/dist/prompts/adapters.d.ts +16 -0
- package/dist/prompts/adapters.js +55 -0
- package/dist/protocol/liop_core.proto +44 -0
- package/dist/rpc/client.d.ts +22 -0
- package/dist/rpc/client.js +40 -0
- package/dist/rpc/codec/lpm.d.ts +20 -0
- package/dist/rpc/codec/lpm.js +36 -0
- package/dist/rpc/crypto/aes.d.ts +22 -0
- package/dist/rpc/crypto/aes.js +47 -0
- package/dist/rpc/crypto/kyber.d.ts +27 -0
- package/dist/rpc/crypto/kyber.js +70 -0
- package/dist/rpc/proto.d.ts +2 -0
- package/dist/rpc/proto.js +33 -0
- package/dist/rpc/server.d.ts +13 -0
- package/dist/rpc/server.js +50 -0
- package/dist/rpc/tls.d.ts +26 -0
- package/dist/rpc/tls.js +54 -0
- package/dist/rpc/types.d.ts +28 -0
- package/dist/rpc/types.js +5 -0
- package/dist/sandbox/guardian.d.ts +18 -0
- package/dist/sandbox/guardian.js +35 -0
- package/dist/sandbox/wasi.d.ts +36 -0
- package/dist/sandbox/wasi.js +179 -0
- package/dist/security/guardian.d.ts +22 -0
- package/dist/security/guardian.js +52 -0
- package/dist/security/zk.d.ts +37 -0
- package/dist/security/zk.js +66 -0
- package/dist/server/index.d.ts +184 -0
- package/dist/server/index.js +933 -0
- package/dist/server/pii.d.ts +40 -0
- package/dist/server/pii.js +266 -0
- package/dist/types.d.ts +145 -0
- package/dist/types.js +26 -0
- package/dist/utils/logger.d.ts +21 -0
- package/dist/utils/logger.js +70 -0
- package/dist/utils/mcpCompact.d.ts +11 -0
- package/dist/utils/mcpCompact.js +29 -0
- package/dist/workers/logic-execution.d.ts +17 -0
- package/dist/workers/logic-execution.js +121 -0
- package/dist/workers/zk-verifier.d.ts +20 -0
- package/dist/workers/zk-verifier.js +84 -0
- package/package.json +147 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { serve } from "@hono/node-server";
|
|
3
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
4
|
+
import { Hono } from "hono";
|
|
5
|
+
import { cors } from "hono/cors";
|
|
6
|
+
import { log } from "../utils/logger.js";
|
|
7
|
+
import { LiopMcpBridge } from "./index.js";
|
|
8
|
+
const DEFAULT_MAX_SESSIONS_PER_IP = 10;
|
|
9
|
+
const DEFAULT_SESSION_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
|
|
10
|
+
const EVICTION_INTERVAL_MS = 60 * 1000; // Check every minute
|
|
11
|
+
/**
|
|
12
|
+
* LiopStreamBridge
|
|
13
|
+
*
|
|
14
|
+
* Exposes a LiopServer over a remote HTTP network using the industry-standard
|
|
15
|
+
* MCP Streamable HTTP Transport + Hono JS.
|
|
16
|
+
*
|
|
17
|
+
* Supports concurrent multi-client connections via per-session transport instances (Map pattern).
|
|
18
|
+
* External agents connect using only a URL + Bearer Token (Zero-Trust).
|
|
19
|
+
*
|
|
20
|
+
* Security hardening:
|
|
21
|
+
* - Zero-Trust Bearer Token enforcement
|
|
22
|
+
* - Per-IP rate limiting on session creation
|
|
23
|
+
* - Automatic eviction of idle sessions (TTL)
|
|
24
|
+
*/
|
|
25
|
+
export class LiopStreamBridge {
|
|
26
|
+
options;
|
|
27
|
+
app;
|
|
28
|
+
httpServer = null;
|
|
29
|
+
bridgeLogic;
|
|
30
|
+
activeSessions;
|
|
31
|
+
evictionTimer = null;
|
|
32
|
+
maxSessionsPerIp;
|
|
33
|
+
sessionTimeoutMs;
|
|
34
|
+
constructor(internalServer, options = {}) {
|
|
35
|
+
this.options = options;
|
|
36
|
+
this.app = new Hono();
|
|
37
|
+
this.bridgeLogic = new LiopMcpBridge(internalServer);
|
|
38
|
+
this.activeSessions = new Map();
|
|
39
|
+
this.maxSessionsPerIp =
|
|
40
|
+
options.maxSessionsPerIp ?? DEFAULT_MAX_SESSIONS_PER_IP;
|
|
41
|
+
this.sessionTimeoutMs =
|
|
42
|
+
options.sessionTimeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS;
|
|
43
|
+
this.setupRoutes();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new per-session transport instance and wires it to the LIOPMcpBridge logic.
|
|
47
|
+
*/
|
|
48
|
+
createSessionTransport(clientIp) {
|
|
49
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
50
|
+
sessionIdGenerator: () => randomUUID(),
|
|
51
|
+
onsessioninitialized: (sessionId) => {
|
|
52
|
+
this.activeSessions.set(sessionId, {
|
|
53
|
+
transport,
|
|
54
|
+
lastActivity: Date.now(),
|
|
55
|
+
clientIp,
|
|
56
|
+
});
|
|
57
|
+
log.info(`[LIOP-StreamBridge] Session opened: ${sessionId} (IP: ${clientIp})`);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
// Wire the transport's incoming messages to the LiopMcpBridge JSON-RPC router
|
|
61
|
+
transport.onmessage = async (message) => {
|
|
62
|
+
// Touch activity timestamp on every message
|
|
63
|
+
if (transport.sessionId) {
|
|
64
|
+
const entry = this.activeSessions.get(transport.sessionId);
|
|
65
|
+
if (entry)
|
|
66
|
+
entry.lastActivity = Date.now();
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const result = await this.bridgeLogic.handleJsonRpcRequest(message);
|
|
70
|
+
// Notifications return undefined — no response needed
|
|
71
|
+
if (result !== undefined) {
|
|
72
|
+
await transport.send(result);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
log.info("[LIOP-StreamBridge] JSON-RPC error:", err.message);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
transport.onclose = () => {
|
|
80
|
+
if (transport.sessionId) {
|
|
81
|
+
this.activeSessions.delete(transport.sessionId);
|
|
82
|
+
log.info(`[LIOP-StreamBridge] Session closed: ${transport.sessionId}`);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return transport;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Returns the number of active sessions for a given IP.
|
|
89
|
+
*/
|
|
90
|
+
countSessionsByIp(ip) {
|
|
91
|
+
let count = 0;
|
|
92
|
+
for (const entry of this.activeSessions.values()) {
|
|
93
|
+
if (entry.clientIp === ip)
|
|
94
|
+
count++;
|
|
95
|
+
}
|
|
96
|
+
return count;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Extracts client IP from the request (supports X-Forwarded-For for reverse proxies).
|
|
100
|
+
*/
|
|
101
|
+
getClientIp(c) {
|
|
102
|
+
return (c.req.header("x-forwarded-for")?.split(",")[0]?.trim() ||
|
|
103
|
+
c.req.header("x-real-ip") ||
|
|
104
|
+
"unknown");
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Evicts sessions that have been idle longer than the configured timeout.
|
|
108
|
+
*/
|
|
109
|
+
evictIdleSessions() {
|
|
110
|
+
const now = Date.now();
|
|
111
|
+
for (const [sessionId, entry] of this.activeSessions) {
|
|
112
|
+
if (now - entry.lastActivity > this.sessionTimeoutMs) {
|
|
113
|
+
log.info(`[LIOP-StreamBridge] Evicting idle session: ${sessionId}`);
|
|
114
|
+
entry.transport.close().catch(() => {
|
|
115
|
+
/* Swallow close errors */
|
|
116
|
+
});
|
|
117
|
+
this.activeSessions.delete(sessionId);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
setupRoutes() {
|
|
122
|
+
this.app.use("*", cors());
|
|
123
|
+
// ZTA (Zero-Trust Architecture) Security Middleware
|
|
124
|
+
this.app.use("/mcp", async (c, next) => {
|
|
125
|
+
const auth = c.req.header("Authorization");
|
|
126
|
+
const expectedToken = process.env.ZERO_TRUST_TOKEN;
|
|
127
|
+
if (expectedToken) {
|
|
128
|
+
if (!auth?.startsWith("Bearer ") ||
|
|
129
|
+
auth.split(" ")[1] !== expectedToken) {
|
|
130
|
+
log.info("[LIOP-StreamBridge] ALERT: Access denied - Invalid Zero-Trust token.");
|
|
131
|
+
return c.json({ error: "Unauthorized: LIOP Zero-Trust Policy Enforced" }, 401);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
await next();
|
|
135
|
+
});
|
|
136
|
+
// Multi-Session Streamable HTTP Handler
|
|
137
|
+
this.app.all("/mcp", async (c) => {
|
|
138
|
+
const sessionId = c.req.header("mcp-session-id");
|
|
139
|
+
// Route to existing session if session ID is present
|
|
140
|
+
if (sessionId) {
|
|
141
|
+
const existing = this.activeSessions.get(sessionId);
|
|
142
|
+
if (!existing) {
|
|
143
|
+
return c.json({ error: "Session not found" }, 404);
|
|
144
|
+
}
|
|
145
|
+
// Touch activity on every routed request
|
|
146
|
+
existing.lastActivity = Date.now();
|
|
147
|
+
const response = await existing.transport.handleRequest(c.req.raw);
|
|
148
|
+
// If DELETE, the transport closes internally but onclose may not fire.
|
|
149
|
+
// Explicitly clean up the session from the Map.
|
|
150
|
+
if (c.req.method === "DELETE") {
|
|
151
|
+
this.activeSessions.delete(sessionId);
|
|
152
|
+
log.info(`[LIOP-StreamBridge] Session closed (DELETE): ${sessionId}`);
|
|
153
|
+
}
|
|
154
|
+
return response;
|
|
155
|
+
}
|
|
156
|
+
// No session ID → New client initializing.
|
|
157
|
+
// Rate-limit: enforce max sessions per IP
|
|
158
|
+
const clientIp = this.getClientIp(c);
|
|
159
|
+
const currentSessions = this.countSessionsByIp(clientIp);
|
|
160
|
+
if (currentSessions >= this.maxSessionsPerIp) {
|
|
161
|
+
log.info(`[LIOP-StreamBridge] Rate limit hit for IP: ${clientIp} (${currentSessions} sessions)`);
|
|
162
|
+
return c.json({ error: "Too Many Sessions: Rate limit exceeded" }, 429);
|
|
163
|
+
}
|
|
164
|
+
const transport = this.createSessionTransport(clientIp);
|
|
165
|
+
return transport.handleRequest(c.req.raw);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Starts the LiopStreamBridge HTTP server and session eviction timer.
|
|
170
|
+
*/
|
|
171
|
+
async start(port) {
|
|
172
|
+
const listenPort = port ?? this.options.port ?? 3000;
|
|
173
|
+
// Start the idle session eviction timer
|
|
174
|
+
this.evictionTimer = setInterval(() => this.evictIdleSessions(), EVICTION_INTERVAL_MS);
|
|
175
|
+
return new Promise((resolve) => {
|
|
176
|
+
this.httpServer = serve({
|
|
177
|
+
fetch: this.app.fetch,
|
|
178
|
+
port: listenPort,
|
|
179
|
+
}, (info) => {
|
|
180
|
+
log.info(`[LIOP-StreamBridge] Streamable HTTP Gateway on http://localhost:${info.port}/mcp`);
|
|
181
|
+
resolve();
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Graceful shutdown — closes all active sessions, stops timers, and releases port.
|
|
187
|
+
*/
|
|
188
|
+
async stop() {
|
|
189
|
+
if (this.evictionTimer) {
|
|
190
|
+
clearInterval(this.evictionTimer);
|
|
191
|
+
this.evictionTimer = null;
|
|
192
|
+
}
|
|
193
|
+
for (const [id, entry] of this.activeSessions) {
|
|
194
|
+
await entry.transport.close();
|
|
195
|
+
this.activeSessions.delete(id);
|
|
196
|
+
}
|
|
197
|
+
if (this.httpServer) {
|
|
198
|
+
this.httpServer.close();
|
|
199
|
+
log.info("[LIOP-StreamBridge] HTTP ports released.");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { LiopVerifier } from "../crypto/verifier.js";
|
|
2
|
+
import { type MeshNodeConfig } from "../mesh/node.js";
|
|
3
|
+
import type { LiopTlsOptions } from "../rpc/tls.js";
|
|
4
|
+
import type { CallToolRequest, CallToolResult } from "../types.js";
|
|
5
|
+
/**
|
|
6
|
+
* LIOP Client
|
|
7
|
+
* High-level orchestration for discovery and execution in the Logic-Injection-on-Origin mesh.
|
|
8
|
+
*/
|
|
9
|
+
export declare class LiopClient {
|
|
10
|
+
private meshNode;
|
|
11
|
+
private rpcClients;
|
|
12
|
+
private manifests;
|
|
13
|
+
private tlsOptions?;
|
|
14
|
+
private serverInfo?;
|
|
15
|
+
verifier: LiopVerifier;
|
|
16
|
+
constructor(tls?: LiopTlsOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Discovers and connects to the target server or mesh capability.
|
|
19
|
+
* If address is omitted, it sets up the MeshNode to act purely dynamically.
|
|
20
|
+
*/
|
|
21
|
+
connect(address?: string, options?: {
|
|
22
|
+
meshConfig?: MeshNodeConfig;
|
|
23
|
+
}): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Dynamically queries Kademlia DHT to find the optimal PeerID providing the Capability
|
|
26
|
+
* and returns the physical gRPC target (host:port) resolved from the provider's manifest.
|
|
27
|
+
*/
|
|
28
|
+
resolveCapability(toolName: string): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Discovers remote capabilities via the LIOP Manifest Protocol.
|
|
31
|
+
*/
|
|
32
|
+
discoverTools(): Promise<{
|
|
33
|
+
name: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
}[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Invokes a tool.
|
|
38
|
+
*/
|
|
39
|
+
callTool(request: CallToolRequest, _wasmPayload?: Buffer): Promise<CallToolResult>;
|
|
40
|
+
private getOrCreateRpcClient;
|
|
41
|
+
/**
|
|
42
|
+
* Reads a specific resource by URI.
|
|
43
|
+
* In LIOP, resources can be static definitions or dynamic streams.
|
|
44
|
+
*/
|
|
45
|
+
readResource(uri: string): Promise<{
|
|
46
|
+
contents: Array<{
|
|
47
|
+
uri: string;
|
|
48
|
+
mimeType?: string;
|
|
49
|
+
text: string;
|
|
50
|
+
}>;
|
|
51
|
+
}>;
|
|
52
|
+
getServerInfo(): {
|
|
53
|
+
name: string;
|
|
54
|
+
version: string;
|
|
55
|
+
} | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Destroys the active Mesh Node resources.
|
|
58
|
+
*/
|
|
59
|
+
close(): Promise<void>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { LiopVerifier } from "../crypto/verifier.js";
|
|
2
|
+
import { MeshNode, } from "../mesh/node.js";
|
|
3
|
+
import { LiopRpcClient } from "../rpc/client.js";
|
|
4
|
+
import { AesGcmWrapper } from "../rpc/crypto/aes.js";
|
|
5
|
+
import { Kyber768Wrapper } from "../rpc/crypto/kyber.js";
|
|
6
|
+
import { log } from "../utils/logger.js";
|
|
7
|
+
/**
|
|
8
|
+
* LIOP Client
|
|
9
|
+
* High-level orchestration for discovery and execution in the Logic-Injection-on-Origin mesh.
|
|
10
|
+
*/
|
|
11
|
+
export class LiopClient {
|
|
12
|
+
meshNode = null;
|
|
13
|
+
rpcClients = new Map();
|
|
14
|
+
manifests = new Map();
|
|
15
|
+
tlsOptions;
|
|
16
|
+
serverInfo;
|
|
17
|
+
verifier = new LiopVerifier();
|
|
18
|
+
constructor(tls) {
|
|
19
|
+
this.tlsOptions = tls;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Discovers and connects to the target server or mesh capability.
|
|
23
|
+
* If address is omitted, it sets up the MeshNode to act purely dynamically.
|
|
24
|
+
*/
|
|
25
|
+
async connect(address, options) {
|
|
26
|
+
this.meshNode = new MeshNode(options?.meshConfig);
|
|
27
|
+
await this.meshNode.start();
|
|
28
|
+
log.info(`[LiopClient] Mesh Node synchronized. PeerID: ${this.meshNode.getPeerId()}`);
|
|
29
|
+
if (address) {
|
|
30
|
+
this.rpcClients.set("static", new LiopRpcClient(address, this.tlsOptions));
|
|
31
|
+
this.serverInfo = { name: `LiopServer (${address})`, version: "1.0.0" };
|
|
32
|
+
log.info(`[LiopClient] Static gRPC configured for: ${address}`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.serverInfo = { name: "LiopServer (Mesh Alpha)", version: "1.0.0" };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Dynamically queries Kademlia DHT to find the optimal PeerID providing the Capability
|
|
40
|
+
* and returns the physical gRPC target (host:port) resolved from the provider's manifest.
|
|
41
|
+
*/
|
|
42
|
+
async resolveCapability(toolName) {
|
|
43
|
+
if (!this.meshNode)
|
|
44
|
+
throw new Error("Client must be connected to Mesh to resolve capabilities.");
|
|
45
|
+
log.info(`[LiopClient] Querying Mesh DHT for Provider: ${toolName}...`);
|
|
46
|
+
const providers = await this.meshNode.findProviders(toolName);
|
|
47
|
+
if (providers.length === 0) {
|
|
48
|
+
throw new Error(`Kademlia DHT found zero providers for capability: ${toolName}`);
|
|
49
|
+
}
|
|
50
|
+
const providerId = providers[0];
|
|
51
|
+
log.info(`[LiopClient] Identified Alpha Provider PeerID: ${providerId}`);
|
|
52
|
+
let grpcPort = 50051;
|
|
53
|
+
const manifest = await this.meshNode.queryManifest(providerId);
|
|
54
|
+
if (manifest) {
|
|
55
|
+
grpcPort = manifest.grpcPort;
|
|
56
|
+
log.info(`[LiopClient] Manifest resolved: gRPC port ${grpcPort}`);
|
|
57
|
+
}
|
|
58
|
+
const addrs = await this.meshNode.resolvePeer(providerId);
|
|
59
|
+
for (const maddr of addrs) {
|
|
60
|
+
const parts = maddr.split("/");
|
|
61
|
+
if (parts[1] === "ip4") {
|
|
62
|
+
const grpcHost = `${parts[2]}:${grpcPort}`;
|
|
63
|
+
log.info(`[LiopClient] Translated Multiaddr to gRPC Target: ${grpcHost}`);
|
|
64
|
+
return grpcHost;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return `127.0.0.1:${grpcPort}`;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Discovers remote capabilities via the LIOP Manifest Protocol.
|
|
71
|
+
*/
|
|
72
|
+
async discoverTools() {
|
|
73
|
+
if (!this.meshNode) {
|
|
74
|
+
throw new Error("Client must be connected before discovering tools.");
|
|
75
|
+
}
|
|
76
|
+
log.info(`[LiopClient] Discovery started...`);
|
|
77
|
+
const providerIds = await this.meshNode.discoverManifestProviders();
|
|
78
|
+
const tools = [];
|
|
79
|
+
const seenNames = new Set();
|
|
80
|
+
for (const peerId of providerIds) {
|
|
81
|
+
try {
|
|
82
|
+
log.info(`[LiopClient] Querying manifest from: ${peerId}`);
|
|
83
|
+
const manifest = await this.meshNode.queryManifest(peerId);
|
|
84
|
+
if (manifest) {
|
|
85
|
+
this.manifests.set(peerId, manifest);
|
|
86
|
+
for (const tool of manifest.tools) {
|
|
87
|
+
if (!seenNames.has(tool.name)) {
|
|
88
|
+
tools.push({ name: tool.name, description: tool.description });
|
|
89
|
+
seenNames.add(tool.name);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
log.info(`[LiopClient] Error querying manifest from ${peerId}:`, err instanceof Error ? err.message : String(err));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
log.info(`[LiopClient] Discovery finished. Found ${tools.length} unique tools.`);
|
|
99
|
+
return tools;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Invokes a tool.
|
|
103
|
+
*/
|
|
104
|
+
async callTool(request, _wasmPayload) {
|
|
105
|
+
if (!this.meshNode) {
|
|
106
|
+
throw new Error("Client must be connected before calling tools.");
|
|
107
|
+
}
|
|
108
|
+
const toolName = request.name;
|
|
109
|
+
log.info(`[LiopClient] Resolving Tool: ${toolName}`);
|
|
110
|
+
// [ALPHA-FIX] Bypass DHT discovery if we are already statically connected to a provider (Enterprise/Test mode)
|
|
111
|
+
let rpcClient = this.rpcClients.get("static");
|
|
112
|
+
if (!rpcClient) {
|
|
113
|
+
const dynamicAddress = await this.resolveCapability(toolName);
|
|
114
|
+
rpcClient = this.getOrCreateRpcClient(toolName, dynamicAddress);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
log.info(`[LiopClient] Using existing static gRPC connection for ${toolName}.`);
|
|
118
|
+
}
|
|
119
|
+
log.info(`[LiopClient] Negotiating intent for ${toolName}...`);
|
|
120
|
+
const agentDid = this.meshNode
|
|
121
|
+
? `did:liop:${this.meshNode.getPeerId()}`
|
|
122
|
+
: "did:liop:ephemeral";
|
|
123
|
+
const intentPayload = Buffer.from(`${toolName}:${Date.now()}`);
|
|
124
|
+
const proofOfIntent = this.meshNode
|
|
125
|
+
? await this.meshNode.sign(intentPayload)
|
|
126
|
+
: intentPayload;
|
|
127
|
+
const intentResponse = (await rpcClient.negotiateIntent({
|
|
128
|
+
agent_did: agentDid,
|
|
129
|
+
capability_hash: toolName,
|
|
130
|
+
proof_of_intent: proofOfIntent,
|
|
131
|
+
}));
|
|
132
|
+
if (!intentResponse.accepted) {
|
|
133
|
+
throw new Error(`Intent denied by host: ${intentResponse.error_message}`);
|
|
134
|
+
}
|
|
135
|
+
// LIOP Robust Field Extraction (Supports both snake_case and camelCase via gRPC-JS)
|
|
136
|
+
const publicKey = intentResponse.kyber_public_key || intentResponse.kyberPublicKey;
|
|
137
|
+
const sessionToken = intentResponse.session_token || intentResponse.sessionToken;
|
|
138
|
+
if (!publicKey) {
|
|
139
|
+
log.info("[LiopClient] Critical Error: Kyber Public Key not found in IntentResponse.", intentResponse);
|
|
140
|
+
throw new Error("Handshake failed: Remote host did not provide a valid Kyber Public Key.");
|
|
141
|
+
}
|
|
142
|
+
// 2. Post-Quantum Encapsulation (ML-KEM-768)
|
|
143
|
+
log.info(`[LiopClient] Encapsulating Post-Quantum Shared Secret for ${request.name}...`);
|
|
144
|
+
const { ciphertext: kyberCiphertext, sharedSecret } = await Kyber768Wrapper.encapsulateAsymmetric(publicKey);
|
|
145
|
+
// 3. Symmetric Sealing (AES-256-GCM)
|
|
146
|
+
log.info(`[LiopClient] Sealing WASM Payload and Inputs...`);
|
|
147
|
+
const _safePayload = _wasmPayload || Buffer.from("");
|
|
148
|
+
// Encrypt WASM binary
|
|
149
|
+
const { ciphertext: encryptedWasm, nonce: aesNonce } = AesGcmWrapper.encryptPayload(_safePayload, sharedSecret);
|
|
150
|
+
// Encrypt inputs using the SAME session nonce for the multi-payload request (Standard LIOP V1)
|
|
151
|
+
const encryptedInputs = {};
|
|
152
|
+
for (const [key, value] of Object.entries(request.arguments || {})) {
|
|
153
|
+
// We manually encrypt with the same nonce/key to match the Proto structure
|
|
154
|
+
// ideally we'd have per-field nonces, but for Alpha we follow the liop_core.proto v1.
|
|
155
|
+
const crypto = await import("node:crypto");
|
|
156
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", sharedSecret, aesNonce);
|
|
157
|
+
const encrypted = Buffer.concat([
|
|
158
|
+
cipher.update(JSON.stringify(value)),
|
|
159
|
+
cipher.final(),
|
|
160
|
+
]);
|
|
161
|
+
const authTag = cipher.getAuthTag();
|
|
162
|
+
encryptedInputs[key] = Buffer.concat([encrypted, authTag]);
|
|
163
|
+
}
|
|
164
|
+
// 4. Assemble and Execute gRPC LogicRequest
|
|
165
|
+
const logicRequest = {
|
|
166
|
+
session_token: sessionToken,
|
|
167
|
+
wasm_binary: encryptedWasm,
|
|
168
|
+
inputs: encryptedInputs,
|
|
169
|
+
pqc_ciphertext: kyberCiphertext,
|
|
170
|
+
aes_nonce: aesNonce,
|
|
171
|
+
};
|
|
172
|
+
return new Promise((resolve, reject) => {
|
|
173
|
+
const stream = rpcClient.executeLogic(logicRequest);
|
|
174
|
+
if (!stream) {
|
|
175
|
+
reject(new Error("RPC Client unavailable or failed to create stream."));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
let resultFulfilled = false;
|
|
179
|
+
let hasReceivedData = false;
|
|
180
|
+
stream.on("data", async (response) => {
|
|
181
|
+
if (resultFulfilled)
|
|
182
|
+
return;
|
|
183
|
+
hasReceivedData = true;
|
|
184
|
+
log.info("[LiopClient] Logic Executed. Verification in progress...");
|
|
185
|
+
try {
|
|
186
|
+
const isValid = await this.verifier.verifyZkReceipt(_safePayload, Buffer.from(response.cryptographic_proof).toString("hex"), Buffer.from(response.zk_receipt));
|
|
187
|
+
if (!isValid) {
|
|
188
|
+
reject(new Error("PROTOCOL INTEGRITY VIOLATION: ZK-Receipt verification failed."));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
resultFulfilled = true;
|
|
192
|
+
resolve({
|
|
193
|
+
content: [
|
|
194
|
+
{
|
|
195
|
+
type: "text",
|
|
196
|
+
text: response.semantic_evidence,
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
isError: response.is_error,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
reject(err);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
stream.on("error", (err) => {
|
|
207
|
+
if (resultFulfilled)
|
|
208
|
+
return;
|
|
209
|
+
log.error("[LiopClient] Stream Error:", err);
|
|
210
|
+
reject(err);
|
|
211
|
+
});
|
|
212
|
+
stream.on("end", () => {
|
|
213
|
+
// We don't throw here if we already received a response block that is currently
|
|
214
|
+
// undergoing ZK Verification in the Piscina worker pool.
|
|
215
|
+
if (!hasReceivedData && !resultFulfilled) {
|
|
216
|
+
reject(new Error("Logic-on-Origin stream closed without results."));
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
getOrCreateRpcClient(peerId, address) {
|
|
222
|
+
let client = this.rpcClients.get(peerId);
|
|
223
|
+
if (!client) {
|
|
224
|
+
client = new LiopRpcClient(address, this.tlsOptions);
|
|
225
|
+
this.rpcClients.set(peerId, client);
|
|
226
|
+
}
|
|
227
|
+
return client;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Reads a specific resource by URI.
|
|
231
|
+
* In LIOP, resources can be static definitions or dynamic streams.
|
|
232
|
+
*/
|
|
233
|
+
async readResource(uri) {
|
|
234
|
+
if (!this.meshNode) {
|
|
235
|
+
throw new Error("Client must be connected before reading resources.");
|
|
236
|
+
}
|
|
237
|
+
log.info(`[LiopClient] Querying Mesh for Resource: ${uri}...`);
|
|
238
|
+
// We search for the peer hosting the resource in the P2P Mesh
|
|
239
|
+
const providers = await this.meshNode.findProviders(uri);
|
|
240
|
+
if (providers.length === 0) {
|
|
241
|
+
throw new Error(`No mesh providers found for resource: ${uri}`);
|
|
242
|
+
}
|
|
243
|
+
// Query the remote peer's manifest
|
|
244
|
+
const manifest = await this.meshNode.queryManifest(providers[0]);
|
|
245
|
+
if (!manifest) {
|
|
246
|
+
throw new Error("Target peer did not return a valid LIOP Manifest.");
|
|
247
|
+
}
|
|
248
|
+
// Locate the exact resource metadata
|
|
249
|
+
const resourceDef = manifest.resources?.find((r) => r.uri === uri);
|
|
250
|
+
if (!resourceDef) {
|
|
251
|
+
throw new Error(`Resource ${uri} not listed in remote manifest.`);
|
|
252
|
+
}
|
|
253
|
+
// Return the declarative metadata (Logic-Injection is required for actual data extraction)
|
|
254
|
+
return {
|
|
255
|
+
contents: [
|
|
256
|
+
{
|
|
257
|
+
uri,
|
|
258
|
+
mimeType: resourceDef.mimeType || "application/json",
|
|
259
|
+
text: JSON.stringify(resourceDef, null, 2),
|
|
260
|
+
},
|
|
261
|
+
],
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
getServerInfo() {
|
|
265
|
+
return this.serverInfo;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Destroys the active Mesh Node resources.
|
|
269
|
+
*/
|
|
270
|
+
async close() {
|
|
271
|
+
if (this.meshNode) {
|
|
272
|
+
await this.meshNode.stop();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Top-level LIOP v1 envelope only. Must NOT use multiline (^/$) mode:
|
|
4
|
+
* proxy logic embeds a full envelope inside JSON strings; `^` per line would
|
|
5
|
+
* incorrectly treat that as the document root and desync ImageID vs the worker.
|
|
6
|
+
*/
|
|
7
|
+
const TOP_LEVEL_ENVELOPE = /^\s*LIOP_MAGIC:0x00FF\s*\n?\s*MANIFEST:\{[\s\S]*?\}\s*\n?\s*---BEGIN_LOGIC---\n?([\s\S]*?)\n?---END_LOGIC---\s*$/;
|
|
8
|
+
export function normalizeLogicSource(logicUtf8) {
|
|
9
|
+
const match = logicUtf8.match(TOP_LEVEL_ENVELOPE);
|
|
10
|
+
if (match?.[1] !== undefined) {
|
|
11
|
+
return match[1].trim();
|
|
12
|
+
}
|
|
13
|
+
return logicUtf8.trim();
|
|
14
|
+
}
|
|
15
|
+
/** SHA-256 digest of logic bytes (WASM raw; JS UTF-8 after top-level envelope strip). */
|
|
16
|
+
export function deriveLogicImageDigest(logicPayload) {
|
|
17
|
+
const isWasm = logicPayload[0] === 0x00 && logicPayload[1] === 0x61;
|
|
18
|
+
if (isWasm) {
|
|
19
|
+
return crypto.createHash("sha256").update(logicPayload).digest();
|
|
20
|
+
}
|
|
21
|
+
const text = Buffer.from(logicPayload).toString("utf-8");
|
|
22
|
+
const normalized = normalizeLogicSource(text);
|
|
23
|
+
return crypto
|
|
24
|
+
.createHash("sha256")
|
|
25
|
+
.update(Buffer.from(normalized, "utf-8"))
|
|
26
|
+
.digest();
|
|
27
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LIOP Tier-0 Industrial Verifier
|
|
3
|
+
*
|
|
4
|
+
* This engine is responsible for the trustless verification of remote logic execution.
|
|
5
|
+
* It validates both the integrity of the code (ZkImageID) and the mathematical proof
|
|
6
|
+
* of its execution (ZkSeal), as well as hardware-level attestation (TEE).
|
|
7
|
+
*/
|
|
8
|
+
export declare class LiopVerifier {
|
|
9
|
+
private static zkWorkerPool;
|
|
10
|
+
private getZkPool;
|
|
11
|
+
/**
|
|
12
|
+
* Verifies a Zero-Knowledge Receipt from a remote LIOP node via Worker Pool.
|
|
13
|
+
*
|
|
14
|
+
* @param logicPayload The raw WASM or JS logic that was sent to the provider.
|
|
15
|
+
* @param remoteImageIdHex The ImageID reported by the provider (must match our local calculation).
|
|
16
|
+
* @param zkReceipt The mathematical proof (Seal + Journal) from the zkVM.
|
|
17
|
+
*/
|
|
18
|
+
verifyZkReceipt(logicPayload: Buffer, remoteImageIdHex: string, zkReceipt: Buffer): Promise<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* Verifies if a node is running inside an authenticated TEE (e.g. AWS Nitro).
|
|
21
|
+
*
|
|
22
|
+
* @param attestationReport The COSE-signed attestation document from the hardware.
|
|
23
|
+
*/
|
|
24
|
+
verifyTeeAttestation(attestationReport: Buffer): Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* Derives the ImageID of a logic payload following the LIOP v1 Standard.
|
|
27
|
+
*/
|
|
28
|
+
deriveImageId(logicPayload: Buffer): Buffer;
|
|
29
|
+
}
|