@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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +413 -0
  3. package/dist/bin/agent.d.ts +2 -0
  4. package/dist/bin/agent.js +307 -0
  5. package/dist/bridge/index.d.ts +37 -0
  6. package/dist/bridge/index.js +249 -0
  7. package/dist/bridge/stream.d.ts +62 -0
  8. package/dist/bridge/stream.js +202 -0
  9. package/dist/client/index.d.ts +60 -0
  10. package/dist/client/index.js +275 -0
  11. package/dist/crypto/logic-image-id.d.ts +3 -0
  12. package/dist/crypto/logic-image-id.js +27 -0
  13. package/dist/crypto/verifier.d.ts +29 -0
  14. package/dist/crypto/verifier.js +96 -0
  15. package/dist/economy/estimator.d.ts +53 -0
  16. package/dist/economy/estimator.js +69 -0
  17. package/dist/economy/index.d.ts +5 -0
  18. package/dist/economy/index.js +3 -0
  19. package/dist/economy/otel.d.ts +38 -0
  20. package/dist/economy/otel.js +100 -0
  21. package/dist/economy/telemetry.d.ts +77 -0
  22. package/dist/economy/telemetry.js +224 -0
  23. package/dist/gateway/hybrid.d.ts +23 -0
  24. package/dist/gateway/hybrid.js +199 -0
  25. package/dist/gateway/router.d.ts +69 -0
  26. package/dist/gateway/router.js +1036 -0
  27. package/dist/index.d.ts +11 -0
  28. package/dist/index.js +11 -0
  29. package/dist/mesh/index.d.ts +1 -0
  30. package/dist/mesh/index.js +1 -0
  31. package/dist/mesh/node.d.ts +129 -0
  32. package/dist/mesh/node.js +853 -0
  33. package/dist/prompts/adapters.d.ts +16 -0
  34. package/dist/prompts/adapters.js +55 -0
  35. package/dist/protocol/liop_core.proto +44 -0
  36. package/dist/rpc/client.d.ts +22 -0
  37. package/dist/rpc/client.js +40 -0
  38. package/dist/rpc/codec/lpm.d.ts +20 -0
  39. package/dist/rpc/codec/lpm.js +36 -0
  40. package/dist/rpc/crypto/aes.d.ts +22 -0
  41. package/dist/rpc/crypto/aes.js +47 -0
  42. package/dist/rpc/crypto/kyber.d.ts +27 -0
  43. package/dist/rpc/crypto/kyber.js +70 -0
  44. package/dist/rpc/proto.d.ts +2 -0
  45. package/dist/rpc/proto.js +33 -0
  46. package/dist/rpc/server.d.ts +13 -0
  47. package/dist/rpc/server.js +50 -0
  48. package/dist/rpc/tls.d.ts +26 -0
  49. package/dist/rpc/tls.js +54 -0
  50. package/dist/rpc/types.d.ts +28 -0
  51. package/dist/rpc/types.js +5 -0
  52. package/dist/sandbox/guardian.d.ts +18 -0
  53. package/dist/sandbox/guardian.js +35 -0
  54. package/dist/sandbox/wasi.d.ts +36 -0
  55. package/dist/sandbox/wasi.js +179 -0
  56. package/dist/security/guardian.d.ts +22 -0
  57. package/dist/security/guardian.js +52 -0
  58. package/dist/security/zk.d.ts +37 -0
  59. package/dist/security/zk.js +66 -0
  60. package/dist/server/index.d.ts +184 -0
  61. package/dist/server/index.js +933 -0
  62. package/dist/server/pii.d.ts +40 -0
  63. package/dist/server/pii.js +266 -0
  64. package/dist/types.d.ts +145 -0
  65. package/dist/types.js +26 -0
  66. package/dist/utils/logger.d.ts +21 -0
  67. package/dist/utils/logger.js +70 -0
  68. package/dist/utils/mcpCompact.d.ts +11 -0
  69. package/dist/utils/mcpCompact.js +29 -0
  70. package/dist/workers/logic-execution.d.ts +17 -0
  71. package/dist/workers/logic-execution.js +121 -0
  72. package/dist/workers/zk-verifier.d.ts +20 -0
  73. package/dist/workers/zk-verifier.js +84 -0
  74. package/package.json +147 -0
@@ -0,0 +1,121 @@
1
+ import { Buffer } from "node:buffer";
2
+ import crypto from "node:crypto";
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
+ const valTag = valBuffer.subarray(-16);
34
+ const valData = valBuffer.subarray(0, -16);
35
+ const valDecipher = crypto.createDecipheriv("aes-256-gcm", aesKey, Buffer.from(aesNonce || new Uint8Array(12)));
36
+ valDecipher.setAuthTag(valTag);
37
+ let valDecrypted = valDecipher.update(valData);
38
+ valDecrypted = Buffer.concat([valDecrypted, valDecipher.final()]);
39
+ decryptedInputs[key] = JSON.parse(valDecrypted.toString("utf-8"));
40
+ }
41
+ }
42
+ else {
43
+ // Transparent mode: payload is provided directly
44
+ // If it's WASM (Magic bytes: \0asm), keep as Buffer
45
+ if (wasmBinary[0] === 0x00 &&
46
+ wasmBinary[1] === 0x61 &&
47
+ wasmBinary[2] === 0x73 &&
48
+ wasmBinary[3] === 0x6d) {
49
+ decryptedPayload = Buffer.from(wasmBinary);
50
+ }
51
+ else {
52
+ decryptedPayload = Buffer.from(wasmBinary).toString("utf-8");
53
+ }
54
+ }
55
+ // 3. Inspect AST with Guardian-TS (if WASM)
56
+ const isWasm = decryptedPayload[0] === 0x00 &&
57
+ decryptedPayload[1] === 0x61 &&
58
+ decryptedPayload[2] === 0x73 &&
59
+ decryptedPayload[3] === 0x6d;
60
+ if (decryptedPayload instanceof Buffer && isWasm) {
61
+ // Ensure we pass a compatible BufferSource
62
+ const wasmBytes = new Uint8Array(decryptedPayload);
63
+ const compiledModule = await WebAssembly.compile(wasmBytes);
64
+ ASTGuardian.analyze(compiledModule);
65
+ }
66
+ else if (decryptedPayload instanceof Buffer && !isWasm) {
67
+ decryptedPayload = decryptedPayload.toString("utf-8");
68
+ }
69
+ // Strip only a whole-document LIOP envelope (see logic-image-id.ts).
70
+ if (typeof decryptedPayload === "string") {
71
+ decryptedPayload = normalizeLogicSource(decryptedPayload);
72
+ }
73
+ // 4. Instantiate and Execute WASI Sandbox (or V8 Fallback)
74
+ const sandbox = new WasiSandbox();
75
+ await sandbox.init();
76
+ try {
77
+ const result = await sandbox.execute(decryptedPayload, records, decryptedInputs);
78
+ // 5. Generate Cryptographic Proof of Execution (HMAC-SHA256 Commitment)
79
+ let logicBytes;
80
+ if (typeof decryptedPayload === "string") {
81
+ logicBytes = Buffer.from(decryptedPayload, "utf-8");
82
+ }
83
+ else {
84
+ logicBytes = new Uint8Array(decryptedPayload);
85
+ }
86
+ const imageId = deriveLogicImageDigest(logicBytes).toString("hex");
87
+ const journal = Buffer.from(JSON.stringify({
88
+ image_id: imageId,
89
+ output_hash: crypto
90
+ .createHash("sha256")
91
+ .update(typeof result.output === "string"
92
+ ? result.output
93
+ : JSON.stringify(result.output))
94
+ .digest("hex"),
95
+ fuel: result.fuelConsumed,
96
+ ts: Date.now(),
97
+ }));
98
+ const seal = crypto
99
+ .createHmac("sha256", sessionSecret)
100
+ .update(journal)
101
+ .digest();
102
+ const journalLen = Buffer.alloc(2);
103
+ journalLen.writeUInt16BE(journal.length);
104
+ const receiptBuf = Buffer.concat([
105
+ Buffer.from([0x01]), // Receipt format v1
106
+ journalLen,
107
+ journal,
108
+ seal, // 32 bytes HMAC
109
+ ]);
110
+ const zkReceipt = receiptBuf.toString("base64");
111
+ return {
112
+ image_id: imageId,
113
+ zk_receipt: zkReceipt,
114
+ output: result.output,
115
+ fuel_consumed: result.fuelConsumed,
116
+ };
117
+ }
118
+ finally {
119
+ await sandbox.teardown();
120
+ }
121
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * ZK Verification Payload Structure.
3
+ * Modeled after RISC Zero & SP1 Receipt formats.
4
+ */
5
+ export interface ZkVerificationPayload {
6
+ action: "verify_receipt";
7
+ /** Original logic payload (JS/WASM) sent by client */
8
+ logicPayload: Uint8Array;
9
+ /** Expected ImageID (SHA-256) of the execution state */
10
+ remoteImageIdHex: string;
11
+ /** Cbor-encoded or raw buffer containing the execution Receipt (Journal + Seal) */
12
+ zkReceipt: Uint8Array;
13
+ }
14
+ /**
15
+ * Main worker entry point for Piscina.
16
+ */
17
+ export default function workerHandler(task: ZkVerificationPayload): Promise<{
18
+ verified: boolean;
19
+ message: string;
20
+ }>;
@@ -0,0 +1,84 @@
1
+ import { parentPort } from "node:worker_threads";
2
+ import { deriveLogicImageDigest } from "../crypto/logic-image-id.js";
3
+ // Ensure this worker is used via Piscina pool
4
+ if (!parentPort) {
5
+ // Not fatal in Piscina, but handled appropriately
6
+ }
7
+ function deriveImageId(logicPayload) {
8
+ return deriveLogicImageDigest(logicPayload);
9
+ }
10
+ /**
11
+ * Simulates heavy ZK-Proof cryptographic verification.
12
+ * In a real environment, this delegates to @risc0/verifier or SP1 FFI bindings.
13
+ */
14
+ async function verifyZkReceipt(payload) {
15
+ const { logicPayload, remoteImageIdHex, zkReceipt } = payload;
16
+ // 1. Calculate local ImageID (Integrity Check)
17
+ const localImageId = deriveImageId(logicPayload);
18
+ const localImageIdHex = localImageId.toString("hex");
19
+ if (localImageIdHex !== remoteImageIdHex) {
20
+ return {
21
+ verified: false,
22
+ message: `Integrity Violation: Local (${localImageIdHex.slice(0, 8)}) != Remote (${remoteImageIdHex.slice(0, 8)})`,
23
+ };
24
+ }
25
+ // 2. Structural Verification: Deserialize Binary Receipt
26
+ const receiptBuf = Buffer.from(zkReceipt);
27
+ if (receiptBuf.length < 35) {
28
+ // 1 version + 2 len + 32 seal minimum
29
+ return {
30
+ verified: false,
31
+ message: "Receipt too short for binary format.",
32
+ };
33
+ }
34
+ const version = receiptBuf[0];
35
+ if (version !== 0x01) {
36
+ return {
37
+ verified: false,
38
+ message: `Unknown receipt version: ${version}`,
39
+ };
40
+ }
41
+ const journalLen = receiptBuf.readUInt16BE(1);
42
+ const journal = receiptBuf.subarray(3, 3 + journalLen);
43
+ const seal = receiptBuf.subarray(3 + journalLen);
44
+ if (seal.length !== 32) {
45
+ return {
46
+ verified: false,
47
+ message: "Invalid seal length (expected 32 bytes HMAC-SHA256).",
48
+ };
49
+ }
50
+ // 3. Parse journal and verify imageId
51
+ try {
52
+ const journalData = JSON.parse(journal.toString());
53
+ if (journalData.image_id !== localImageIdHex) {
54
+ return {
55
+ verified: false,
56
+ message: `Journal ImageID mismatch: ${journalData.image_id.slice(0, 8)} != ${localImageIdHex.slice(0, 8)}`,
57
+ };
58
+ }
59
+ }
60
+ catch (_e) {
61
+ return { verified: false, message: "Failed to parse journal data." };
62
+ }
63
+ return {
64
+ verified: true,
65
+ message: "HMAC Commitment Verified: Integrity intact.",
66
+ };
67
+ }
68
+ /**
69
+ * Main worker entry point for Piscina.
70
+ */
71
+ export default async function workerHandler(task) {
72
+ try {
73
+ if (task.action === "verify_receipt") {
74
+ return await verifyZkReceipt(task);
75
+ }
76
+ throw new Error("Unknown action in ZkVerifier Worker.");
77
+ }
78
+ catch (error) {
79
+ return {
80
+ verified: false,
81
+ message: `Verification Error: ${error.message}`,
82
+ };
83
+ }
84
+ }
package/package.json ADDED
@@ -0,0 +1,147 @@
1
+ {
2
+ "name": "@nekzus/liop",
3
+ "version": "1.2.0-alpha.10",
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
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "bin": {
8
+ "liop-agent": "./dist/bin/agent.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./client": {
21
+ "types": "./dist/client/index.d.ts",
22
+ "default": "./dist/client/index.js"
23
+ },
24
+ "./server": {
25
+ "types": "./dist/server/index.d.ts",
26
+ "default": "./dist/server/index.js"
27
+ },
28
+ "./types": {
29
+ "types": "./dist/types.d.ts",
30
+ "default": "./dist/types.js"
31
+ },
32
+ "./bridge": {
33
+ "types": "./dist/bridge/index.d.ts",
34
+ "default": "./dist/bridge/index.js"
35
+ },
36
+ "./gateway": {
37
+ "types": "./dist/gateway/hybrid.d.ts",
38
+ "default": "./dist/gateway/hybrid.js"
39
+ },
40
+ "./mesh": {
41
+ "types": "./dist/mesh/index.d.ts",
42
+ "default": "./dist/mesh/index.js"
43
+ }
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -p tsconfig.build.json && npx tsx scripts/copy-protos.ts",
47
+ "test": "vitest run --fileParallelism=false",
48
+ "test:all": "vitest run --fileParallelism=false",
49
+ "test:integration": "vitest run tests/integration --fileParallelism=false",
50
+ "test:conformance": "vitest run tests/conformance --fileParallelism=false",
51
+ "test:watch": "vitest",
52
+ "test:coverage": "vitest run --coverage",
53
+ "lint": "biome lint .",
54
+ "format": "biome format --write .",
55
+ "check": "biome check .",
56
+ "demo:client-quickstart": "pnpm --filter @liop/example-client-quickstart start",
57
+ "demo:server-quickstart": "pnpm --filter @liop/example-server-quickstart start",
58
+ "demo:server": "pnpm --filter @liop/example-server start",
59
+ "demo:client": "pnpm --filter @liop/example-client start",
60
+ "test:crossnet": "tsx tests/infra/cli/crossnet.ts",
61
+ "test:crossnet:burn": "tsx tests/infra/cli/crossnet-burn.ts",
62
+ "demo:build": "tsx tests/infra/cli/demo-build.ts",
63
+ "demo:start": "tsx tests/infra/cli/demo-start.ts",
64
+ "demo:start:rebuild": "tsx tests/infra/cli/demo-start-rebuild.ts",
65
+ "demo:stop": "tsx tests/infra/cli/demo-stop.ts",
66
+ "demo:clean": "tsx tests/infra/cli/demo-clean.ts",
67
+ "demo:claude": "tsx tests/infra/cli/demo-claude.ts",
68
+ "demo:inspector": "tsx tests/infra/cli/demo-inspector.ts"
69
+ },
70
+ "keywords": [
71
+ "liop",
72
+ "logic-injection-on-origin",
73
+ "model-context-protocol",
74
+ "mcp",
75
+ "logic-on-origin",
76
+ "wasi",
77
+ "sandbox",
78
+ "p2p",
79
+ "libp2p",
80
+ "zero-trust",
81
+ "kyber",
82
+ "post-quantum",
83
+ "ai-agent",
84
+ "llm"
85
+ ],
86
+ "author": "Nekzus",
87
+ "license": "MIT",
88
+ "type": "module",
89
+ "repository": {
90
+ "type": "git",
91
+ "url": "git+https://github.com/Nekzus/Neural-Mesh-Protocol.git"
92
+ },
93
+ "bugs": {
94
+ "url": "https://github.com/Nekzus/Neural-Mesh-Protocol/issues"
95
+ },
96
+ "homepage": "https://nekzus-32.mintlify.app/",
97
+ "publishConfig": {
98
+ "access": "public",
99
+ "registry": "https://registry.npmjs.org/",
100
+ "provenance": true
101
+ },
102
+ "engines": {
103
+ "node": ">=20.0"
104
+ },
105
+ "devDependencies": {
106
+ "@biomejs/biome": "^2.4.4",
107
+ "@opentelemetry/sdk-metrics": "^2.7.0",
108
+ "@types/node": "^25.3.1",
109
+ "@vitest/coverage-v8": "^4.0.18",
110
+ "tsx": "^4.21.0",
111
+ "typescript": "^5.9.3",
112
+ "vitest": "^4.0.18"
113
+ },
114
+ "dependencies": {
115
+ "@chainsafe/libp2p-noise": "^17.0.0",
116
+ "@chainsafe/libp2p-yamux": "^8.0.1",
117
+ "@grpc/grpc-js": "^1.14.3",
118
+ "@grpc/proto-loader": "^0.8.0",
119
+ "@hono/node-server": "^1.19.11",
120
+ "@libp2p/bootstrap": "^10.0.15",
121
+ "@libp2p/crypto": "^5.1.14",
122
+ "@libp2p/identify": "^4.0.14",
123
+ "@libp2p/kad-dht": "^16.1.7",
124
+ "@libp2p/mplex": "^12.0.11",
125
+ "@libp2p/noise": "^1.0.1",
126
+ "@libp2p/peer-id": "^4.0.10",
127
+ "@libp2p/peer-id-factory": "^4.0.10",
128
+ "@libp2p/ping": "^3.0.12",
129
+ "@libp2p/tcp": "^11.0.14",
130
+ "@libp2p/websockets": "^10.1.7",
131
+ "@modelcontextprotocol/sdk": "^1.28.0",
132
+ "@multiformats/multiaddr": "^13.0.1",
133
+ "@opentelemetry/api": "^1.9.1",
134
+ "gpt-tokenizer": "^3.4.0",
135
+ "hono": "^4.12.5",
136
+ "it-pipe": "^3.0.1",
137
+ "libp2p": "^3.1.3",
138
+ "mlkem": "^2.7.0",
139
+ "multiformats": "^13.4.2",
140
+ "p-event": "^7.1.0",
141
+ "piscina": "^5.1.4",
142
+ "uint8arraylist": "^2.4.8",
143
+ "uint8arrays": "^3.1.1",
144
+ "zod": "^3.23.11",
145
+ "zod-to-json-schema": "^3.24.1"
146
+ }
147
+ }