@nekzus/liop 2.0.0-alpha.1 → 2.0.0-alpha.2
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/dist/bin/agent.d.ts +0 -1
- package/dist/bin/agent.js +5 -306
- package/dist/bin/agent.js.map +1 -0
- package/dist/{bridge/stream.d.ts → bridge.d.ts} +44 -3
- package/dist/bridge.js +2 -0
- package/dist/bridge.js.map +1 -0
- package/dist/chunk-4ABAFG44.js +33 -0
- package/dist/chunk-4ABAFG44.js.map +1 -0
- package/dist/chunk-ANFXJGMP.js +2 -0
- package/dist/chunk-ANFXJGMP.js.map +1 -0
- package/dist/chunk-DBXGYHKY.js +2 -0
- package/dist/chunk-DBXGYHKY.js.map +1 -0
- package/dist/chunk-HM77MWB6.js +2 -0
- package/dist/chunk-HM77MWB6.js.map +1 -0
- package/dist/chunk-HNDVAKEK.js +24 -0
- package/dist/chunk-HNDVAKEK.js.map +1 -0
- package/dist/chunk-HQZHZM6U.js +2 -0
- package/dist/chunk-HQZHZM6U.js.map +1 -0
- package/dist/chunk-P52IE4L6.js +2 -0
- package/dist/chunk-P52IE4L6.js.map +1 -0
- package/dist/chunk-PIBCW4BD.js +13 -0
- package/dist/chunk-PIBCW4BD.js.map +1 -0
- package/dist/chunk-PPCOS2NU.js +2 -0
- package/dist/chunk-PPCOS2NU.js.map +1 -0
- package/dist/chunk-RWRRBYG4.js +2 -0
- package/dist/chunk-RWRRBYG4.js.map +1 -0
- package/dist/chunk-S6RJHZV2.js +2 -0
- package/dist/chunk-S6RJHZV2.js.map +1 -0
- package/dist/chunk-UVTEJYHN.js +2 -0
- package/dist/chunk-UVTEJYHN.js.map +1 -0
- package/dist/chunk-X6FJATUE.js +29 -0
- package/dist/chunk-X6FJATUE.js.map +1 -0
- package/dist/chunk-XLVRRGOX.js +3 -0
- package/dist/chunk-XLVRRGOX.js.map +1 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +2 -0
- package/dist/client.js.map +1 -0
- package/dist/{gateway/router.d.ts → gateway.d.ts} +30 -5
- package/dist/gateway.js +2 -0
- package/dist/gateway.js.map +1 -0
- package/dist/{client/index.d.ts → index-CyxNLlz7.d.ts} +24 -5
- package/dist/index.d.ts +313 -12
- package/dist/index.js +31 -12
- package/dist/index.js.map +1 -0
- package/dist/kyber-2WDOTUQX.js +2 -0
- package/dist/kyber-2WDOTUQX.js.map +1 -0
- package/dist/{mesh/node.d.ts → mesh.d.ts} +5 -3
- package/dist/mesh.js +2 -0
- package/dist/mesh.js.map +1 -0
- package/dist/{server/index.d.ts → server.d.ts} +125 -12
- package/dist/server.js +2 -0
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +17 -14
- package/dist/types.js +2 -26
- package/dist/types.js.map +1 -0
- package/dist/{crypto/verifier.d.ts → verifier-DTCD9imJ.d.ts} +3 -1
- package/dist/verifier-RQRYXA4C.js +2 -0
- package/dist/verifier-RQRYXA4C.js.map +1 -0
- package/dist/workers/logic-execution.d.ts +4 -2
- package/dist/workers/logic-execution.js +2 -123
- package/dist/workers/logic-execution.js.map +1 -0
- package/dist/workers/zk-verifier.d.ts +4 -2
- package/dist/workers/zk-verifier.js +2 -98
- package/dist/workers/zk-verifier.js.map +1 -0
- package/package.json +31 -18
- package/dist/bridge/index.d.ts +0 -37
- package/dist/bridge/index.js +0 -249
- package/dist/bridge/stream.js +0 -210
- package/dist/client/index.js +0 -275
- package/dist/crypto/logic-image-id.d.ts +0 -3
- package/dist/crypto/logic-image-id.js +0 -27
- package/dist/crypto/verifier.js +0 -97
- package/dist/economy/estimator.d.ts +0 -53
- package/dist/economy/estimator.js +0 -69
- package/dist/economy/index.d.ts +0 -5
- package/dist/economy/index.js +0 -3
- package/dist/economy/otel.d.ts +0 -38
- package/dist/economy/otel.js +0 -100
- package/dist/economy/telemetry.d.ts +0 -77
- package/dist/economy/telemetry.js +0 -224
- package/dist/errors.d.ts +0 -14
- package/dist/errors.js +0 -19
- package/dist/gateway/hybrid.d.ts +0 -23
- package/dist/gateway/hybrid.js +0 -199
- package/dist/gateway/router.js +0 -1054
- package/dist/mesh/index.d.ts +0 -1
- package/dist/mesh/index.js +0 -1
- package/dist/mesh/node.js +0 -853
- package/dist/prompts/adapters.d.ts +0 -16
- package/dist/prompts/adapters.js +0 -55
- package/dist/rpc/client.d.ts +0 -22
- package/dist/rpc/client.js +0 -40
- package/dist/rpc/codec/lpm.d.ts +0 -20
- package/dist/rpc/codec/lpm.js +0 -36
- package/dist/rpc/crypto/aes.d.ts +0 -22
- package/dist/rpc/crypto/aes.js +0 -47
- package/dist/rpc/crypto/kyber.d.ts +0 -27
- package/dist/rpc/crypto/kyber.js +0 -70
- package/dist/rpc/proto.d.ts +0 -2
- package/dist/rpc/proto.js +0 -33
- package/dist/rpc/server.d.ts +0 -13
- package/dist/rpc/server.js +0 -50
- package/dist/rpc/tls.d.ts +0 -26
- package/dist/rpc/tls.js +0 -54
- package/dist/rpc/types.d.ts +0 -28
- package/dist/rpc/types.js +0 -5
- package/dist/sandbox/guardian.d.ts +0 -18
- package/dist/sandbox/guardian.js +0 -58
- package/dist/sandbox/wasi.d.ts +0 -36
- package/dist/sandbox/wasi.js +0 -233
- package/dist/security/guardian.d.ts +0 -22
- package/dist/security/guardian.js +0 -52
- package/dist/security/zk.d.ts +0 -37
- package/dist/security/zk.js +0 -76
- package/dist/server/index.js +0 -1047
- package/dist/server/ner-scanner.d.ts +0 -29
- package/dist/server/ner-scanner.js +0 -141
- package/dist/server/pii.d.ts +0 -66
- package/dist/server/pii.js +0 -428
- package/dist/utils/logger.d.ts +0 -21
- package/dist/utils/logger.js +0 -70
- package/dist/utils/mcpCompact.d.ts +0 -11
- package/dist/utils/mcpCompact.js +0 -29
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { z } from
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Base Protocol Types representing parity with Model Context Protocol
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
|
+
declare const ToolSchema: z.ZodObject<{
|
|
6
7
|
name: z.ZodString;
|
|
7
8
|
description: z.ZodOptional<z.ZodString>;
|
|
8
9
|
inputSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
@@ -15,8 +16,8 @@ export declare const ToolSchema: z.ZodObject<{
|
|
|
15
16
|
inputSchema: Record<string, unknown>;
|
|
16
17
|
description?: string | undefined;
|
|
17
18
|
}>;
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
type Tool = z.infer<typeof ToolSchema>;
|
|
20
|
+
declare const ResourceSchema: z.ZodObject<{
|
|
20
21
|
uri: z.ZodString;
|
|
21
22
|
name: z.ZodString;
|
|
22
23
|
description: z.ZodOptional<z.ZodString>;
|
|
@@ -32,8 +33,8 @@ export declare const ResourceSchema: z.ZodObject<{
|
|
|
32
33
|
description?: string | undefined;
|
|
33
34
|
mimeType?: string | undefined;
|
|
34
35
|
}>;
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
type Resource = z.infer<typeof ResourceSchema>;
|
|
37
|
+
declare const PromptSchema: z.ZodObject<{
|
|
37
38
|
name: z.ZodString;
|
|
38
39
|
description: z.ZodOptional<z.ZodString>;
|
|
39
40
|
arguments: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
@@ -66,12 +67,12 @@ export declare const PromptSchema: z.ZodObject<{
|
|
|
66
67
|
required?: boolean | undefined;
|
|
67
68
|
}[] | undefined;
|
|
68
69
|
}>;
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
type Prompt = z.infer<typeof PromptSchema>;
|
|
71
|
+
interface CallToolRequest {
|
|
71
72
|
name: string;
|
|
72
73
|
arguments?: Record<string, unknown>;
|
|
73
74
|
}
|
|
74
|
-
|
|
75
|
+
interface CallToolResult {
|
|
75
76
|
content: Array<{
|
|
76
77
|
type: "text" | "image" | "resource";
|
|
77
78
|
text?: string;
|
|
@@ -85,11 +86,11 @@ export interface CallToolResult {
|
|
|
85
86
|
}>;
|
|
86
87
|
isError?: boolean;
|
|
87
88
|
}
|
|
88
|
-
|
|
89
|
+
interface GetPromptRequest {
|
|
89
90
|
name: string;
|
|
90
91
|
arguments?: Record<string, string>;
|
|
91
92
|
}
|
|
92
|
-
|
|
93
|
+
interface GetPromptResult {
|
|
93
94
|
description?: string;
|
|
94
95
|
messages: Array<{
|
|
95
96
|
role: "user" | "assistant";
|
|
@@ -110,7 +111,7 @@ export interface GetPromptResult {
|
|
|
110
111
|
};
|
|
111
112
|
}>;
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
+
interface ServerInfo {
|
|
114
115
|
name: string;
|
|
115
116
|
version: string;
|
|
116
117
|
capabilities?: {
|
|
@@ -127,13 +128,13 @@ export interface ServerInfo {
|
|
|
127
128
|
logging?: Record<string, unknown>;
|
|
128
129
|
};
|
|
129
130
|
}
|
|
130
|
-
|
|
131
|
+
interface McpRequest {
|
|
131
132
|
method: string;
|
|
132
133
|
params?: unknown;
|
|
133
134
|
id?: string | number | null;
|
|
134
135
|
jsonrpc?: "2.0";
|
|
135
136
|
}
|
|
136
|
-
|
|
137
|
+
interface McpResponse {
|
|
137
138
|
jsonrpc: "2.0";
|
|
138
139
|
id?: string | number | null;
|
|
139
140
|
result?: unknown;
|
|
@@ -143,3 +144,5 @@ export interface McpResponse {
|
|
|
143
144
|
data?: unknown;
|
|
144
145
|
};
|
|
145
146
|
}
|
|
147
|
+
|
|
148
|
+
export { type CallToolRequest, type CallToolResult, type GetPromptRequest, type GetPromptResult, type McpRequest, type McpResponse, type Prompt, PromptSchema, type Resource, ResourceSchema, type ServerInfo, type Tool, ToolSchema };
|
package/dist/types.js
CHANGED
|
@@ -1,26 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Base Protocol Types representing parity with Model Context Protocol
|
|
4
|
-
*/
|
|
5
|
-
export const ToolSchema = z.object({
|
|
6
|
-
name: z.string(),
|
|
7
|
-
description: z.string().optional(),
|
|
8
|
-
inputSchema: z.record(z.string(), z.unknown()), // Represents a JSON Schema
|
|
9
|
-
});
|
|
10
|
-
export const ResourceSchema = z.object({
|
|
11
|
-
uri: z.string(),
|
|
12
|
-
name: z.string(),
|
|
13
|
-
description: z.string().optional(),
|
|
14
|
-
mimeType: z.string().optional(),
|
|
15
|
-
});
|
|
16
|
-
export const PromptSchema = z.object({
|
|
17
|
-
name: z.string(),
|
|
18
|
-
description: z.string().optional(),
|
|
19
|
-
arguments: z
|
|
20
|
-
.array(z.object({
|
|
21
|
-
name: z.string(),
|
|
22
|
-
description: z.string().optional(),
|
|
23
|
-
required: z.boolean().optional(),
|
|
24
|
-
}))
|
|
25
|
-
.optional(),
|
|
26
|
-
});
|
|
1
|
+
export{c as PromptSchema,b as ResourceSchema,a as ToolSchema}from'./chunk-HQZHZM6U.js';//# sourceMappingURL=types.js.map
|
|
2
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"types.js"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* It validates both the integrity of the code (ZkImageID) and the mathematical proof
|
|
6
6
|
* of its execution (ZkSeal), as well as hardware-level attestation (TEE).
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
declare class LiopVerifier {
|
|
9
9
|
private static zkWorkerPool;
|
|
10
10
|
private getZkPool;
|
|
11
11
|
/**
|
|
@@ -27,3 +27,5 @@ export declare class LiopVerifier {
|
|
|
27
27
|
*/
|
|
28
28
|
deriveImageId(logicPayload: Buffer): Buffer;
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
export { LiopVerifier as L };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"verifier-RQRYXA4C.js"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
interface WorkerData {
|
|
2
2
|
ciphertext: Uint8Array;
|
|
3
3
|
secretKeyObj: ArrayLike<number>;
|
|
4
4
|
kyberPublicKey: Uint8Array;
|
|
@@ -9,9 +9,11 @@ export interface WorkerData {
|
|
|
9
9
|
isEncrypted?: boolean;
|
|
10
10
|
aesNonce?: Uint8Array;
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
declare function processLogicExecution(data: WorkerData): Promise<{
|
|
13
13
|
image_id: string;
|
|
14
14
|
output: unknown;
|
|
15
15
|
fuel_consumed: number;
|
|
16
16
|
zk_receipt?: string;
|
|
17
17
|
}>;
|
|
18
|
+
|
|
19
|
+
export { type WorkerData, processLogicExecution as default };
|
|
@@ -1,123 +1,2 @@
|
|
|
1
|
-
import { Buffer } from "
|
|
2
|
-
|
|
3
|
-
import { createMlKem768 } from "mlkem";
|
|
4
|
-
import { deriveLogicImageDigest, normalizeLogicSource, } from "../crypto/logic-image-id.js";
|
|
5
|
-
import { ASTGuardian } from "../sandbox/guardian.js";
|
|
6
|
-
import { WasiSandbox } from "../sandbox/wasi.js";
|
|
7
|
-
export default async function processLogicExecution(data) {
|
|
8
|
-
const { ciphertext, secretKeyObj, wasmBinary, inputs, aesNonce, records, isEncrypted = true, } = data;
|
|
9
|
-
let decryptedPayload;
|
|
10
|
-
const decryptedInputs = {};
|
|
11
|
-
let sessionSecret = Buffer.alloc(32); // Fallback if plain text (no PQC)
|
|
12
|
-
if (isEncrypted) {
|
|
13
|
-
// 1. Decapsulate Kyber secret
|
|
14
|
-
const sk = new Uint8Array(secretKeyObj);
|
|
15
|
-
const ct = new Uint8Array(ciphertext);
|
|
16
|
-
const kem = await createMlKem768();
|
|
17
|
-
const sharedSecret = kem.decap(ct, sk);
|
|
18
|
-
const aesKey = Buffer.from(sharedSecret);
|
|
19
|
-
sessionSecret = aesKey;
|
|
20
|
-
// 2. Decrypt Main Payload (WASM/JS Code)
|
|
21
|
-
// LIOP Serialization: Ciphertext = EncryptedData + 16-byte AuthTag
|
|
22
|
-
const wasmBuffer = Buffer.from(wasmBinary);
|
|
23
|
-
const authTag = wasmBuffer.subarray(-16);
|
|
24
|
-
const encryptedData = wasmBuffer.subarray(0, -16);
|
|
25
|
-
const decipher = crypto.createDecipheriv("aes-256-gcm", aesKey, Buffer.from(aesNonce || new Uint8Array(12)));
|
|
26
|
-
decipher.setAuthTag(authTag);
|
|
27
|
-
let decrypted = decipher.update(encryptedData);
|
|
28
|
-
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
29
|
-
decryptedPayload = decrypted;
|
|
30
|
-
// 3. Decrypt Inputs
|
|
31
|
-
for (const [key, encValue] of Object.entries(inputs || {})) {
|
|
32
|
-
const valBuffer = Buffer.from(encValue);
|
|
33
|
-
// Extract 12-byte prepended nonce, ciphertext, and 16-byte AuthTag
|
|
34
|
-
const inputNonce = valBuffer.subarray(0, 12);
|
|
35
|
-
const valTag = valBuffer.subarray(-16);
|
|
36
|
-
const valData = valBuffer.subarray(12, -16);
|
|
37
|
-
const valDecipher = crypto.createDecipheriv("aes-256-gcm", aesKey, inputNonce);
|
|
38
|
-
valDecipher.setAuthTag(valTag);
|
|
39
|
-
let valDecrypted = valDecipher.update(valData);
|
|
40
|
-
valDecrypted = Buffer.concat([valDecrypted, valDecipher.final()]);
|
|
41
|
-
decryptedInputs[key] = JSON.parse(valDecrypted.toString("utf-8"));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
// Transparent mode: payload is provided directly
|
|
46
|
-
// If it's WASM (Magic bytes: \0asm), keep as Buffer
|
|
47
|
-
if (wasmBinary[0] === 0x00 &&
|
|
48
|
-
wasmBinary[1] === 0x61 &&
|
|
49
|
-
wasmBinary[2] === 0x73 &&
|
|
50
|
-
wasmBinary[3] === 0x6d) {
|
|
51
|
-
decryptedPayload = Buffer.from(wasmBinary);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
decryptedPayload = Buffer.from(wasmBinary).toString("utf-8");
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
// 3. Inspect AST with Guardian-TS (if WASM)
|
|
58
|
-
const isWasm = decryptedPayload[0] === 0x00 &&
|
|
59
|
-
decryptedPayload[1] === 0x61 &&
|
|
60
|
-
decryptedPayload[2] === 0x73 &&
|
|
61
|
-
decryptedPayload[3] === 0x6d;
|
|
62
|
-
if (decryptedPayload instanceof Buffer && isWasm) {
|
|
63
|
-
// Ensure we pass a compatible BufferSource
|
|
64
|
-
const wasmBytes = new Uint8Array(decryptedPayload);
|
|
65
|
-
const compiledModule = await WebAssembly.compile(wasmBytes);
|
|
66
|
-
ASTGuardian.analyze(compiledModule);
|
|
67
|
-
}
|
|
68
|
-
else if (decryptedPayload instanceof Buffer && !isWasm) {
|
|
69
|
-
decryptedPayload = decryptedPayload.toString("utf-8");
|
|
70
|
-
}
|
|
71
|
-
// Strip only a whole-document LIOP envelope (see logic-image-id.ts).
|
|
72
|
-
if (typeof decryptedPayload === "string") {
|
|
73
|
-
decryptedPayload = normalizeLogicSource(decryptedPayload);
|
|
74
|
-
}
|
|
75
|
-
// 4. Instantiate and Execute WASI Sandbox (or V8 Fallback)
|
|
76
|
-
const sandbox = new WasiSandbox();
|
|
77
|
-
await sandbox.init();
|
|
78
|
-
try {
|
|
79
|
-
const result = await sandbox.execute(decryptedPayload, records, decryptedInputs);
|
|
80
|
-
// 5. Generate Cryptographic Proof of Execution (HMAC-SHA256 Commitment)
|
|
81
|
-
let logicBytes;
|
|
82
|
-
if (typeof decryptedPayload === "string") {
|
|
83
|
-
logicBytes = Buffer.from(decryptedPayload, "utf-8");
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
logicBytes = new Uint8Array(decryptedPayload);
|
|
87
|
-
}
|
|
88
|
-
const imageId = deriveLogicImageDigest(logicBytes).toString("hex");
|
|
89
|
-
const journal = Buffer.from(JSON.stringify({
|
|
90
|
-
image_id: imageId,
|
|
91
|
-
output_hash: crypto
|
|
92
|
-
.createHash("sha256")
|
|
93
|
-
.update(typeof result.output === "string"
|
|
94
|
-
? result.output
|
|
95
|
-
: JSON.stringify(result.output))
|
|
96
|
-
.digest("hex"),
|
|
97
|
-
fuel: result.fuelConsumed,
|
|
98
|
-
ts: Date.now(),
|
|
99
|
-
}));
|
|
100
|
-
const seal = crypto
|
|
101
|
-
.createHmac("sha256", sessionSecret)
|
|
102
|
-
.update(journal)
|
|
103
|
-
.digest();
|
|
104
|
-
const journalLen = Buffer.alloc(2);
|
|
105
|
-
journalLen.writeUInt16BE(journal.length);
|
|
106
|
-
const receiptBuf = Buffer.concat([
|
|
107
|
-
Buffer.from([0x01]), // Receipt format v1
|
|
108
|
-
journalLen,
|
|
109
|
-
journal,
|
|
110
|
-
seal, // 32 bytes HMAC
|
|
111
|
-
]);
|
|
112
|
-
const zkReceipt = receiptBuf.toString("base64");
|
|
113
|
-
return {
|
|
114
|
-
image_id: imageId,
|
|
115
|
-
zk_receipt: zkReceipt,
|
|
116
|
-
output: result.output,
|
|
117
|
-
fuel_consumed: result.fuelConsumed,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
finally {
|
|
121
|
-
await sandbox.teardown();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
1
|
+
import {a,b}from'../chunk-HNDVAKEK.js';import {a as a$1,b as b$1}from'../chunk-ANFXJGMP.js';import {Buffer}from'buffer';import c from'crypto';import {createMlKem768}from'mlkem';async function I(B){let{ciphertext:D,secretKeyObj:v,wasmBinary:n,inputs:_,aesNonce:N,records:O,isEncrypted:T=true}=B,e,w={},x=Buffer.alloc(32);if(T){let r=new Uint8Array(v),o=new Uint8Array(D),a=(await createMlKem768()).decap(o,r),s=Buffer.from(a);x=s;let i=Buffer.from(n),h=i.subarray(-16),p=i.subarray(0,-16),m=c.createDecipheriv("aes-256-gcm",s,Buffer.from(N||new Uint8Array(12)));m.setAuthTag(h);let y=m.update(p);y=Buffer.concat([y,m.final()]),e=y;for(let[j,z]of Object.entries(_||{})){let l=Buffer.from(z),K=l.subarray(0,12),L=l.subarray(-16),W=l.subarray(12,-16),d=c.createDecipheriv("aes-256-gcm",s,K);d.setAuthTag(L);let g=d.update(W);g=Buffer.concat([g,d.final()]),w[j]=JSON.parse(g.toString("utf-8"));}}else n[0]===0&&n[1]===97&&n[2]===115&&n[3]===109?e=Buffer.from(n):e=Buffer.from(n).toString("utf-8");let b$2=e[0]===0&&e[1]===97&&e[2]===115&&e[3]===109;if(e instanceof Buffer&&b$2){let r=new Uint8Array(e),o=await WebAssembly.compile(r);a.analyze(o);}else e instanceof Buffer&&!b$2&&(e=e.toString("utf-8"));typeof e=="string"&&(e=a$1(e));let u=new b;await u.init();try{let r=await u.execute(e,O,w),o;typeof e=="string"?o=Buffer.from(e,"utf-8"):o=new Uint8Array(e);let f=b$1(o).toString("hex"),a=Buffer.from(JSON.stringify({image_id:f,output_hash:c.createHash("sha256").update(typeof r.output=="string"?r.output:JSON.stringify(r.output)).digest("hex"),fuel:r.fuelConsumed,ts:Date.now()})),s=c.createHmac("sha256",x).update(a).digest(),i=Buffer.alloc(2);i.writeUInt16BE(a.length);let p=Buffer.concat([Buffer.from([1]),i,a,s]).toString("base64");return {image_id:f,zk_receipt:p,output:r.output,fuel_consumed:r.fuelConsumed}}finally{await u.teardown();}}export{I as default};//# sourceMappingURL=logic-execution.js.map
|
|
2
|
+
//# sourceMappingURL=logic-execution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/workers/logic-execution.ts"],"names":["processLogicExecution","data","ciphertext","secretKeyObj","wasmBinary","inputs","aesNonce","records","isEncrypted","decryptedPayload","decryptedInputs","sessionSecret","Buffer","sk","ct","sharedSecret","createMlKem768","aesKey","wasmBuffer","authTag","encryptedData","decipher","crypto","decrypted","key","encValue","valBuffer","inputNonce","valTag","valData","valDecipher","valDecrypted","isWasm","wasmBytes","compiledModule","ASTGuardian","normalizeLogicSource","sandbox","WasiSandbox","result","logicBytes","imageId","deriveLogicImageDigest","journal","seal","journalLen","zkReceipt"],"mappings":"iLAsBA,eAAOA,EAA6CC,CAAAA,CAKjD,CACF,GAAM,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A,CAClC,MAAA,CAAOgC,CAAO,CAAA,CACd,MAAA,GACIE,CAAAA,CAAajC,MAAAA,CAAO,MAAM,CAAC,CAAA,CACjCiC,CAAAA,CAAW,aAAA,CAAcF,CAAAA,CAAQ,MAAM,EAOvC,IAAMG,CAAAA,CANalC,MAAAA,CAAO,MAAA,CAAO,CAChCA,MAAAA,CAAO,KAAK,CAAC,CAAI,CAAC,CAAA,CAClBiC,CAAAA,CACAF,CAAAA,CACAC,CACD,CAAC,CAAA,CAC4B,SAAS,QAAQ,CAAA,CAE9C,OAAO,CACN,QAAA,CAAUH,CAAAA,CACV,UAAA,CAAYK,CAAAA,CACZ,MAAA,CAAQP,EAAO,MAAA,CACf,aAAA,CAAeA,CAAAA,CAAO,YACvB,CACD,CAAA,OAAE,CACD,MAAMF,CAAAA,CAAQ,QAAA,GACf,CACD","file":"logic-execution.js","sourcesContent":["import { Buffer } from \"node:buffer\";\nimport crypto from \"node:crypto\";\nimport { createMlKem768 } from \"mlkem\";\nimport {\n\tderiveLogicImageDigest,\n\tnormalizeLogicSource,\n} from \"../crypto/logic-image-id.js\";\nimport { ASTGuardian } from \"../sandbox/guardian.js\";\nimport { WasiSandbox } from \"../sandbox/wasi.js\";\n\nexport interface WorkerData {\n\tciphertext: Uint8Array;\n\tsecretKeyObj: ArrayLike<number>;\n\tkyberPublicKey: Uint8Array;\n\twasmBinary: Uint8Array; // Can also be JS code in non-encrypted mode\n\tinputs: Record<string, Uint8Array>;\n\trecords?: Record<string, unknown>[];\n\tsessionToken: string;\n\tisEncrypted?: boolean;\n\taesNonce?: Uint8Array;\n}\n\nexport default async function processLogicExecution(data: WorkerData): Promise<{\n\timage_id: string;\n\toutput: unknown;\n\tfuel_consumed: number;\n\tzk_receipt?: string;\n}> {\n\tconst {\n\t\tciphertext,\n\t\tsecretKeyObj,\n\t\twasmBinary,\n\t\tinputs,\n\t\taesNonce,\n\t\trecords,\n\t\tisEncrypted = true,\n\t} = data;\n\n\tlet decryptedPayload: Buffer | string;\n\tconst decryptedInputs: Record<string, unknown> = {};\n\tlet sessionSecret = Buffer.alloc(32); // Fallback if plain text (no PQC)\n\n\tif (isEncrypted) {\n\t\t// 1. Decapsulate Kyber secret\n\t\tconst sk = new Uint8Array(secretKeyObj);\n\t\tconst ct = new Uint8Array(ciphertext);\n\t\tconst kem = await createMlKem768();\n\t\tconst sharedSecret = kem.decap(ct, sk);\n\t\tconst aesKey = Buffer.from(sharedSecret);\n\t\tsessionSecret = aesKey;\n\n\t\t// 2. Decrypt Main Payload (WASM/JS Code)\n\t\t// LIOP Serialization: Ciphertext = EncryptedData + 16-byte AuthTag\n\t\tconst wasmBuffer = Buffer.from(wasmBinary);\n\t\tconst authTag = wasmBuffer.subarray(-16);\n\t\tconst encryptedData = wasmBuffer.subarray(0, -16);\n\n\t\tconst decipher = crypto.createDecipheriv(\n\t\t\t\"aes-256-gcm\",\n\t\t\taesKey,\n\t\t\tBuffer.from(aesNonce || new Uint8Array(12)),\n\t\t);\n\t\tdecipher.setAuthTag(authTag);\n\t\tlet decrypted = decipher.update(encryptedData);\n\t\tdecrypted = Buffer.concat([decrypted, decipher.final()]);\n\t\tdecryptedPayload = decrypted;\n\n\t\t// 3. Decrypt Inputs\n\t\tfor (const [key, encValue] of Object.entries(inputs || {})) {\n\t\t\tconst valBuffer = Buffer.from(encValue);\n\t\t\t// Extract 12-byte prepended nonce, ciphertext, and 16-byte AuthTag\n\t\t\tconst inputNonce = valBuffer.subarray(0, 12);\n\t\t\tconst valTag = valBuffer.subarray(-16);\n\t\t\tconst valData = valBuffer.subarray(12, -16);\n\n\t\t\tconst valDecipher = crypto.createDecipheriv(\n\t\t\t\t\"aes-256-gcm\",\n\t\t\t\taesKey,\n\t\t\t\tinputNonce,\n\t\t\t);\n\t\t\tvalDecipher.setAuthTag(valTag);\n\t\t\tlet valDecrypted = valDecipher.update(valData);\n\t\t\tvalDecrypted = Buffer.concat([valDecrypted, valDecipher.final()]);\n\t\t\tdecryptedInputs[key] = JSON.parse(valDecrypted.toString(\"utf-8\"));\n\t\t}\n\t} else {\n\t\t// Transparent mode: payload is provided directly\n\t\t// If it's WASM (Magic bytes: \\0asm), keep as Buffer\n\t\tif (\n\t\t\twasmBinary[0] === 0x00 &&\n\t\t\twasmBinary[1] === 0x61 &&\n\t\t\twasmBinary[2] === 0x73 &&\n\t\t\twasmBinary[3] === 0x6d\n\t\t) {\n\t\t\tdecryptedPayload = Buffer.from(wasmBinary);\n\t\t} else {\n\t\t\tdecryptedPayload = Buffer.from(wasmBinary).toString(\"utf-8\");\n\t\t}\n\t}\n\n\t// 3. Inspect AST with Guardian-TS (if WASM)\n\tconst isWasm =\n\t\tdecryptedPayload[0] === 0x00 &&\n\t\tdecryptedPayload[1] === 0x61 &&\n\t\tdecryptedPayload[2] === 0x73 &&\n\t\tdecryptedPayload[3] === 0x6d;\n\n\tif (decryptedPayload instanceof Buffer && isWasm) {\n\t\t// Ensure we pass a compatible BufferSource\n\t\tconst wasmBytes = new Uint8Array(decryptedPayload);\n\t\tconst compiledModule = await WebAssembly.compile(wasmBytes);\n\t\tASTGuardian.analyze(compiledModule);\n\t} else if (decryptedPayload instanceof Buffer && !isWasm) {\n\t\tdecryptedPayload = decryptedPayload.toString(\"utf-8\");\n\t}\n\n\t// Strip only a whole-document LIOP envelope (see logic-image-id.ts).\n\tif (typeof decryptedPayload === \"string\") {\n\t\tdecryptedPayload = normalizeLogicSource(decryptedPayload);\n\t}\n\n\t// 4. Instantiate and Execute WASI Sandbox (or V8 Fallback)\n\tconst sandbox = new WasiSandbox();\n\tawait sandbox.init();\n\n\ttry {\n\t\tconst result = await sandbox.execute(\n\t\t\tdecryptedPayload,\n\t\t\trecords,\n\t\t\tdecryptedInputs,\n\t\t);\n\n\t\t// 5. Generate Cryptographic Proof of Execution (HMAC-SHA256 Commitment)\n\t\tlet logicBytes: Uint8Array;\n\t\tif (typeof decryptedPayload === \"string\") {\n\t\t\tlogicBytes = Buffer.from(decryptedPayload, \"utf-8\");\n\t\t} else {\n\t\t\tlogicBytes = new Uint8Array(decryptedPayload);\n\t\t}\n\t\tconst imageId = deriveLogicImageDigest(logicBytes).toString(\"hex\");\n\n\t\tconst journal = Buffer.from(\n\t\t\tJSON.stringify({\n\t\t\t\timage_id: imageId,\n\t\t\t\toutput_hash: crypto\n\t\t\t\t\t.createHash(\"sha256\")\n\t\t\t\t\t.update(\n\t\t\t\t\t\ttypeof result.output === \"string\"\n\t\t\t\t\t\t\t? result.output\n\t\t\t\t\t\t\t: JSON.stringify(result.output),\n\t\t\t\t\t)\n\t\t\t\t\t.digest(\"hex\"),\n\t\t\t\tfuel: result.fuelConsumed,\n\t\t\t\tts: Date.now(),\n\t\t\t}),\n\t\t);\n\n\t\tconst seal = crypto\n\t\t\t.createHmac(\"sha256\", sessionSecret)\n\t\t\t.update(journal)\n\t\t\t.digest();\n\t\tconst journalLen = Buffer.alloc(2);\n\t\tjournalLen.writeUInt16BE(journal.length);\n\t\tconst receiptBuf = Buffer.concat([\n\t\t\tBuffer.from([0x01]), // Receipt format v1\n\t\t\tjournalLen,\n\t\t\tjournal,\n\t\t\tseal, // 32 bytes HMAC\n\t\t]);\n\t\tconst zkReceipt = receiptBuf.toString(\"base64\");\n\n\t\treturn {\n\t\t\timage_id: imageId,\n\t\t\tzk_receipt: zkReceipt,\n\t\t\toutput: result.output,\n\t\t\tfuel_consumed: result.fuelConsumed,\n\t\t};\n\t} finally {\n\t\tawait sandbox.teardown();\n\t}\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* ZK Verification Payload Structure.
|
|
3
3
|
* Modeled after RISC Zero & SP1 Receipt formats.
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
interface ZkVerificationPayload {
|
|
6
6
|
action: "verify_receipt";
|
|
7
7
|
/** Original logic payload (JS/WASM) sent by client */
|
|
8
8
|
logicPayload: Uint8Array;
|
|
@@ -16,7 +16,9 @@ export interface ZkVerificationPayload {
|
|
|
16
16
|
/**
|
|
17
17
|
* Main worker entry point for Piscina.
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
declare function workerHandler(task: ZkVerificationPayload): Promise<{
|
|
20
20
|
verified: boolean;
|
|
21
21
|
message: string;
|
|
22
22
|
}>;
|
|
23
|
+
|
|
24
|
+
export { type ZkVerificationPayload, workerHandler as default };
|
|
@@ -1,98 +1,2 @@
|
|
|
1
|
-
import crypto from "
|
|
2
|
-
|
|
3
|
-
import { deriveLogicImageDigest } from "../crypto/logic-image-id.js";
|
|
4
|
-
// Ensure this worker is used via Piscina pool
|
|
5
|
-
if (!parentPort) {
|
|
6
|
-
// Not fatal in Piscina, but handled appropriately
|
|
7
|
-
}
|
|
8
|
-
function deriveImageId(logicPayload) {
|
|
9
|
-
return deriveLogicImageDigest(logicPayload);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Simulates heavy ZK-Proof cryptographic verification.
|
|
13
|
-
* In a real environment, this delegates to @risc0/verifier or SP1 FFI bindings.
|
|
14
|
-
*/
|
|
15
|
-
async function verifyZkReceipt(payload) {
|
|
16
|
-
const { logicPayload, remoteImageIdHex, zkReceipt, sessionSecret } = payload;
|
|
17
|
-
// 1. Calculate local ImageID (Integrity Check)
|
|
18
|
-
const localImageId = deriveImageId(logicPayload);
|
|
19
|
-
const localImageIdHex = localImageId.toString("hex");
|
|
20
|
-
if (localImageIdHex !== remoteImageIdHex) {
|
|
21
|
-
return {
|
|
22
|
-
verified: false,
|
|
23
|
-
message: `Integrity Violation: Local (${localImageIdHex.slice(0, 8)}) != Remote (${remoteImageIdHex.slice(0, 8)})`,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
// 2. Structural Verification: Deserialize Binary Receipt
|
|
27
|
-
const receiptBuf = Buffer.from(zkReceipt);
|
|
28
|
-
if (receiptBuf.length < 35) {
|
|
29
|
-
// 1 version + 2 len + 32 seal minimum
|
|
30
|
-
return {
|
|
31
|
-
verified: false,
|
|
32
|
-
message: "Receipt too short for binary format.",
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
const version = receiptBuf[0];
|
|
36
|
-
if (version !== 0x01) {
|
|
37
|
-
return {
|
|
38
|
-
verified: false,
|
|
39
|
-
message: `Unknown receipt version: ${version}`,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
const journalLen = receiptBuf.readUInt16BE(1);
|
|
43
|
-
const journal = receiptBuf.subarray(3, 3 + journalLen);
|
|
44
|
-
const seal = receiptBuf.subarray(3 + journalLen);
|
|
45
|
-
if (seal.length !== 32) {
|
|
46
|
-
return {
|
|
47
|
-
verified: false,
|
|
48
|
-
message: "Invalid seal length (expected 32 bytes HMAC-SHA256).",
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
// 3. Parse journal and verify imageId
|
|
52
|
-
try {
|
|
53
|
-
const journalData = JSON.parse(journal.toString());
|
|
54
|
-
if (journalData.image_id !== localImageIdHex) {
|
|
55
|
-
return {
|
|
56
|
-
verified: false,
|
|
57
|
-
message: `Journal ImageID mismatch: ${journalData.image_id.slice(0, 8)} != ${localImageIdHex.slice(0, 8)}`,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
catch (_e) {
|
|
62
|
-
return { verified: false, message: "Failed to parse journal data." };
|
|
63
|
-
}
|
|
64
|
-
// 4. Mathematical Verification (HMAC-SHA256)
|
|
65
|
-
if (sessionSecret && sessionSecret.length > 0) {
|
|
66
|
-
const expectedSeal = crypto
|
|
67
|
-
.createHmac("sha256", sessionSecret)
|
|
68
|
-
.update(journal)
|
|
69
|
-
.digest();
|
|
70
|
-
if (!crypto.timingSafeEqual(seal, expectedSeal)) {
|
|
71
|
-
return {
|
|
72
|
-
verified: false,
|
|
73
|
-
message: "Invalid seal: HMAC verification failed.",
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
verified: true,
|
|
79
|
-
message: "HMAC Commitment Verified: Integrity intact.",
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Main worker entry point for Piscina.
|
|
84
|
-
*/
|
|
85
|
-
export default async function workerHandler(task) {
|
|
86
|
-
try {
|
|
87
|
-
if (task.action === "verify_receipt") {
|
|
88
|
-
return await verifyZkReceipt(task);
|
|
89
|
-
}
|
|
90
|
-
throw new Error("Unknown action in ZkVerifier Worker.");
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
return {
|
|
94
|
-
verified: false,
|
|
95
|
-
message: `Verification Error: ${error.message}`,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
1
|
+
import {b}from'../chunk-ANFXJGMP.js';import d from'crypto';import'worker_threads';function u(e){return b(e)}async function y(e){let{logicPayload:t,remoteImageIdHex:o,zkReceipt:g,sessionSecret:n}=e,a=u(t).toString("hex");if(a!==o)return {verified:false,message:`Integrity Violation: Local (${a.slice(0,8)}) != Remote (${o.slice(0,8)})`};let r=Buffer.from(g);if(r.length<35)return {verified:false,message:"Receipt too short for binary format."};let s=r[0];if(s!==1)return {verified:false,message:`Unknown receipt version: ${s}`};let c=r.readUInt16BE(1),f=r.subarray(3,3+c),l=r.subarray(3+c);if(l.length!==32)return {verified:false,message:"Invalid seal length (expected 32 bytes HMAC-SHA256)."};try{let i=JSON.parse(f.toString());if(i.image_id!==a)return {verified:!1,message:`Journal ImageID mismatch: ${i.image_id.slice(0,8)} != ${a.slice(0,8)}`}}catch{return {verified:false,message:"Failed to parse journal data."}}if(n&&n.length>0){let i=d.createHmac("sha256",n).update(f).digest();if(!d.timingSafeEqual(l,i))return {verified:false,message:"Invalid seal: HMAC verification failed."}}return {verified:true,message:"HMAC Commitment Verified: Integrity intact."}}async function v(e){try{if(e.action==="verify_receipt")return await y(e);throw new Error("Unknown action in ZkVerifier Worker.")}catch(t){return {verified:false,message:`Verification Error: ${t.message}`}}}export{v as default};//# sourceMappingURL=zk-verifier.js.map
|
|
2
|
+
//# sourceMappingURL=zk-verifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/workers/zk-verifier.ts"],"names":["deriveImageId","logicPayload","deriveLogicImageDigest","verifyZkReceipt","payload","remoteImageIdHex","zkReceipt","sessionSecret","localImageIdHex","receiptBuf","version","journalLen","journal","seal","journalData","expectedSeal","crypto","workerHandler","task","error"],"mappings":"kFAyBA,SAASA,CAAAA,CAAcC,CAAAA,CAAkC,CACxD,OAAOC,CAAAA,CAAuBD,CAAY,CAC3C,CAMA,eAAeE,CAAAA,CACdC,CAAAA,CACkD,CAClD,GAAM,CAAE,YAAA,CAAAH,CAAAA,CAAc,gBAAA,CAAAI,CAAAA,CAAkB,SAAA,CAAAC,CAAAA,CAAW,aAAA,CAAAC,CAAc,CAAA,CAAIH,CAAAA,CAI/DI,CAAAA,CADeR,CAAAA,CAAcC,CAAY,CAAA,CACV,QAAA,CAAS,KAAK,CAAA,CAEnD,GAAIO,CAAAA,GAAoBH,CAAAA,CACvB,OAAO,CACN,QAAA,CAAU,KAAA,CACV,OAAA,CAAS,CAAA,4BAAA,EAA+BG,CAAAA,CAAgB,KAAA,CAAM,CAAA,CAAG,CAAC,CAAC,CAAA,aAAA,EAAgBH,CAAAA,CAAiB,KAAA,CAAM,CAAA,CAAG,CAAC,CAAC,CAAA,CAAA,CAChH,EAID,IAAMI,CAAAA,CAAa,MAAA,CAAO,IAAA,CAAKH,CAAS,CAAA,CACxC,GAAIG,CAAAA,CAAW,MAAA,CAAS,EAAA,CAEvB,OAAO,CACN,QAAA,CAAU,KAAA,CACV,OAAA,CAAS,sCACV,CAAA,CAGD,IAAMC,CAAAA,CAAUD,CAAAA,CAAW,CAAC,CAAA,CAC5B,GAAIC,CAAAA,GAAY,CAAA,CACf,OAAO,CACN,QAAA,CAAU,KAAA,CACV,OAAA,CAAS,4BAA4BA,CAAO,CAAA,CAC7C,CAAA,CAGD,IAAMC,CAAAA,CAAaF,CAAAA,CAAW,YAAA,CAAa,CAAC,CAAA,CACtCG,C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AAM,IAAI,KAAA,CAAM,sCAAsC,CACvD,CAAA,MAASC,CAAAA,CAAO,CACf,OAAO,CACN,QAAA,CAAU,KAAA,CACV,OAAA,CAAS,CAAA,oBAAA,EAAwBA,CAAAA,CAAgB,OAAO,CAAA,CACzD,CACD,CACD","file":"zk-verifier.js","sourcesContent":["import crypto from \"node:crypto\";\nimport { parentPort } from \"node:worker_threads\";\nimport { deriveLogicImageDigest } from \"../crypto/logic-image-id.js\";\n\n// Ensure this worker is used via Piscina pool\nif (!parentPort) {\n\t// Not fatal in Piscina, but handled appropriately\n}\n\n/**\n * ZK Verification Payload Structure.\n * Modeled after RISC Zero & SP1 Receipt formats.\n */\nexport interface ZkVerificationPayload {\n\taction: \"verify_receipt\";\n\t/** Original logic payload (JS/WASM) sent by client */\n\tlogicPayload: Uint8Array;\n\t/** Expected ImageID (SHA-256) of the execution state */\n\tremoteImageIdHex: string;\n\t/** Cbor-encoded or raw buffer containing the execution Receipt (Journal + Seal) */\n\tzkReceipt: Uint8Array;\n\t/** Kyber-derived session secret to verify HMAC signature */\n\tsessionSecret?: Uint8Array;\n}\n\nfunction deriveImageId(logicPayload: Uint8Array): Buffer {\n\treturn deriveLogicImageDigest(logicPayload);\n}\n\n/**\n * Simulates heavy ZK-Proof cryptographic verification.\n * In a real environment, this delegates to @risc0/verifier or SP1 FFI bindings.\n */\nasync function verifyZkReceipt(\n\tpayload: ZkVerificationPayload,\n): Promise<{ verified: boolean; message: string }> {\n\tconst { logicPayload, remoteImageIdHex, zkReceipt, sessionSecret } = payload;\n\n\t// 1. Calculate local ImageID (Integrity Check)\n\tconst localImageId = deriveImageId(logicPayload);\n\tconst localImageIdHex = localImageId.toString(\"hex\");\n\n\tif (localImageIdHex !== remoteImageIdHex) {\n\t\treturn {\n\t\t\tverified: false,\n\t\t\tmessage: `Integrity Violation: Local (${localImageIdHex.slice(0, 8)}) != Remote (${remoteImageIdHex.slice(0, 8)})`,\n\t\t};\n\t}\n\n\t// 2. Structural Verification: Deserialize Binary Receipt\n\tconst receiptBuf = Buffer.from(zkReceipt);\n\tif (receiptBuf.length < 35) {\n\t\t// 1 version + 2 len + 32 seal minimum\n\t\treturn {\n\t\t\tverified: false,\n\t\t\tmessage: \"Receipt too short for binary format.\",\n\t\t};\n\t}\n\n\tconst version = receiptBuf[0];\n\tif (version !== 0x01) {\n\t\treturn {\n\t\t\tverified: false,\n\t\t\tmessage: `Unknown receipt version: ${version}`,\n\t\t};\n\t}\n\n\tconst journalLen = receiptBuf.readUInt16BE(1);\n\tconst journal = receiptBuf.subarray(3, 3 + journalLen);\n\tconst seal = receiptBuf.subarray(3 + journalLen);\n\n\tif (seal.length !== 32) {\n\t\treturn {\n\t\t\tverified: false,\n\t\t\tmessage: \"Invalid seal length (expected 32 bytes HMAC-SHA256).\",\n\t\t};\n\t}\n\n\t// 3. Parse journal and verify imageId\n\ttry {\n\t\tconst journalData = JSON.parse(journal.toString());\n\t\tif (journalData.image_id !== localImageIdHex) {\n\t\t\treturn {\n\t\t\t\tverified: false,\n\t\t\t\tmessage: `Journal ImageID mismatch: ${journalData.image_id.slice(0, 8)} != ${localImageIdHex.slice(0, 8)}`,\n\t\t\t};\n\t\t}\n\t} catch (_e) {\n\t\treturn { verified: false, message: \"Failed to parse journal data.\" };\n\t}\n\n\t// 4. Mathematical Verification (HMAC-SHA256)\n\tif (sessionSecret && sessionSecret.length > 0) {\n\t\tconst expectedSeal = crypto\n\t\t\t.createHmac(\"sha256\", sessionSecret)\n\t\t\t.update(journal)\n\t\t\t.digest();\n\t\tif (!crypto.timingSafeEqual(seal, expectedSeal)) {\n\t\t\treturn {\n\t\t\t\tverified: false,\n\t\t\t\tmessage: \"Invalid seal: HMAC verification failed.\",\n\t\t\t};\n\t\t}\n\t}\n\n\treturn {\n\t\tverified: true,\n\t\tmessage: \"HMAC Commitment Verified: Integrity intact.\",\n\t};\n}\n\n/**\n * Main worker entry point for Piscina.\n */\nexport default async function workerHandler(\n\ttask: ZkVerificationPayload,\n): Promise<{ verified: boolean; message: string }> {\n\ttry {\n\t\tif (task.action === \"verify_receipt\") {\n\t\t\treturn await verifyZkReceipt(task);\n\t\t}\n\t\tthrow new Error(\"Unknown action in ZkVerifier Worker.\");\n\t} catch (error) {\n\t\treturn {\n\t\t\tverified: false,\n\t\t\tmessage: `Verification Error: ${(error as Error).message}`,\n\t\t};\n\t}\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nekzus/liop",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
4
4
|
"description": "Official SDK for Logic-Injection-on-Origin Protocol (LIOP). Deploy Logic-on-Origin with WebAssembly at gRPC speed and bidirectional MCP compatibility.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|
|
8
8
|
"liop-agent": "./dist/bin/agent.js"
|
|
9
9
|
},
|
|
10
|
+
"sideEffects": false,
|
|
10
11
|
"files": [
|
|
11
12
|
"dist",
|
|
12
13
|
"README.md",
|
|
@@ -15,35 +16,35 @@
|
|
|
15
16
|
"exports": {
|
|
16
17
|
".": {
|
|
17
18
|
"types": "./dist/index.d.ts",
|
|
18
|
-
"
|
|
19
|
+
"import": "./dist/index.js"
|
|
19
20
|
},
|
|
20
21
|
"./client": {
|
|
21
|
-
"types": "./dist/client
|
|
22
|
-
"
|
|
22
|
+
"types": "./dist/client.d.ts",
|
|
23
|
+
"import": "./dist/client.js"
|
|
23
24
|
},
|
|
24
25
|
"./server": {
|
|
25
|
-
"types": "./dist/server
|
|
26
|
-
"
|
|
26
|
+
"types": "./dist/server.d.ts",
|
|
27
|
+
"import": "./dist/server.js"
|
|
27
28
|
},
|
|
28
29
|
"./types": {
|
|
29
30
|
"types": "./dist/types.d.ts",
|
|
30
|
-
"
|
|
31
|
+
"import": "./dist/types.js"
|
|
31
32
|
},
|
|
32
33
|
"./bridge": {
|
|
33
|
-
"types": "./dist/bridge
|
|
34
|
-
"
|
|
34
|
+
"types": "./dist/bridge.d.ts",
|
|
35
|
+
"import": "./dist/bridge.js"
|
|
35
36
|
},
|
|
36
37
|
"./gateway": {
|
|
37
|
-
"types": "./dist/gateway
|
|
38
|
-
"
|
|
38
|
+
"types": "./dist/gateway.d.ts",
|
|
39
|
+
"import": "./dist/gateway.js"
|
|
39
40
|
},
|
|
40
41
|
"./mesh": {
|
|
41
|
-
"types": "./dist/mesh
|
|
42
|
-
"
|
|
42
|
+
"types": "./dist/mesh.d.ts",
|
|
43
|
+
"import": "./dist/mesh.js"
|
|
43
44
|
}
|
|
44
45
|
},
|
|
45
46
|
"scripts": {
|
|
46
|
-
"build": "
|
|
47
|
+
"build": "tsup && npx tsx scripts/copy-protos.ts",
|
|
47
48
|
"test": "vitest run --fileParallelism=false",
|
|
48
49
|
"test:all": "vitest run --fileParallelism=false",
|
|
49
50
|
"test:integration": "vitest run tests/integration --fileParallelism=false",
|
|
@@ -107,6 +108,7 @@
|
|
|
107
108
|
"@opentelemetry/sdk-metrics": "^2.7.0",
|
|
108
109
|
"@types/node": "^25.3.1",
|
|
109
110
|
"@vitest/coverage-v8": "^4.0.18",
|
|
111
|
+
"tsup": "^8.5.1",
|
|
110
112
|
"tsx": "^4.21.0",
|
|
111
113
|
"typescript": "^5.9.3",
|
|
112
114
|
"vitest": "^4.0.18"
|
|
@@ -122,17 +124,15 @@
|
|
|
122
124
|
"@libp2p/identify": "^4.0.14",
|
|
123
125
|
"@libp2p/kad-dht": "^16.1.7",
|
|
124
126
|
"@libp2p/mplex": "^12.0.11",
|
|
125
|
-
"@libp2p/noise": "^1.0.1",
|
|
126
127
|
"@libp2p/peer-id": "^4.0.10",
|
|
127
128
|
"@libp2p/peer-id-factory": "^4.0.10",
|
|
128
129
|
"@libp2p/ping": "^3.0.12",
|
|
129
130
|
"@libp2p/tcp": "^11.0.14",
|
|
130
131
|
"@libp2p/websockets": "^10.1.7",
|
|
131
|
-
"@modelcontextprotocol/sdk": "^1.28.0",
|
|
132
132
|
"@multiformats/multiaddr": "^13.0.1",
|
|
133
133
|
"@opentelemetry/api": "^1.9.1",
|
|
134
|
-
"
|
|
135
|
-
"
|
|
134
|
+
"acorn": "^8.16.0",
|
|
135
|
+
"acorn-walk": "^8.3.5",
|
|
136
136
|
"hono": "^4.12.5",
|
|
137
137
|
"it-pipe": "^3.0.1",
|
|
138
138
|
"libp2p": "^3.1.3",
|
|
@@ -144,5 +144,18 @@
|
|
|
144
144
|
"uint8arrays": "^3.1.1",
|
|
145
145
|
"zod": "^3.23.11",
|
|
146
146
|
"zod-to-json-schema": "^3.24.1"
|
|
147
|
+
},
|
|
148
|
+
"optionalDependencies": {
|
|
149
|
+
"@modelcontextprotocol/sdk": "^1.28.0",
|
|
150
|
+
"compromise": "14.15.0",
|
|
151
|
+
"gpt-tokenizer": "^3.4.0"
|
|
152
|
+
},
|
|
153
|
+
"peerDependencies": {
|
|
154
|
+
"@modelcontextprotocol/sdk": "^1.28.0"
|
|
155
|
+
},
|
|
156
|
+
"peerDependenciesMeta": {
|
|
157
|
+
"@modelcontextprotocol/sdk": {
|
|
158
|
+
"optional": true
|
|
159
|
+
}
|
|
147
160
|
}
|
|
148
161
|
}
|
package/dist/bridge/index.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import type { LiopServerOptions } from "../server/index.js";
|
|
3
|
-
import { LiopServer } from "../server/index.js";
|
|
4
|
-
export interface LiopBridgeOptions {
|
|
5
|
-
publishToMesh?: boolean;
|
|
6
|
-
meshIdentity?: string;
|
|
7
|
-
serverInfo?: {
|
|
8
|
-
name: string;
|
|
9
|
-
version: string;
|
|
10
|
-
};
|
|
11
|
-
security?: LiopServerOptions["security"];
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* LIOP MCP Bridge
|
|
15
|
-
* A bi-directional bridge that allows legacy MCP servers to join the LIOP mesh,
|
|
16
|
-
* or exposes a LIOP server as an MCP-compatible stdio process for tools like Claude Desktop.
|
|
17
|
-
*/
|
|
18
|
-
export declare class LiopMcpBridge {
|
|
19
|
-
private options;
|
|
20
|
-
private liopServer;
|
|
21
|
-
private legacyMcpServer;
|
|
22
|
-
constructor(source: LiopServer | McpServer, options?: LiopBridgeOptions);
|
|
23
|
-
/**
|
|
24
|
-
* Handles an incoming standard MCP JSON-RPC 2.0 payload.
|
|
25
|
-
* Pipes it to the underlying server (LIOP or Legacy MCP).
|
|
26
|
-
*/
|
|
27
|
-
handleJsonRpcRequest(payload: Record<string, unknown>): Promise<unknown>;
|
|
28
|
-
private handleLiopToMcp;
|
|
29
|
-
private successResponse;
|
|
30
|
-
private errorResponse;
|
|
31
|
-
private verifyZkReceipt;
|
|
32
|
-
/**
|
|
33
|
-
* Connects the bridge via stdio or Mesh depending on mode.
|
|
34
|
-
*/
|
|
35
|
-
connect(): Promise<void>;
|
|
36
|
-
}
|
|
37
|
-
export * from "./stream.js";
|