@nekzus/liop 2.0.1-beta.1 → 2.1.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/README.md +69 -12
- package/dist/bin/agent.js +303 -4
- package/dist/bin/agent.js.map +1 -1
- package/dist/bridge.d.ts +2 -2
- package/dist/bridge.js +4 -1
- package/dist/chunk-32ADSAJS.js +104 -0
- package/dist/chunk-32ADSAJS.js.map +1 -0
- package/dist/chunk-72MNYFR6.js +64 -0
- package/dist/chunk-72MNYFR6.js.map +1 -0
- package/dist/chunk-E5QBDD5E.js +469 -0
- package/dist/chunk-E5QBDD5E.js.map +1 -0
- package/dist/chunk-EEYEVHO2.js +9329 -0
- package/dist/chunk-EEYEVHO2.js.map +1 -0
- package/dist/chunk-F5YNYL5L.js +14512 -0
- package/dist/chunk-F5YNYL5L.js.map +1 -0
- package/dist/chunk-HB5DXX3Q.js +1976 -0
- package/dist/chunk-HB5DXX3Q.js.map +1 -0
- package/dist/chunk-IJHTRIZC.js +56 -0
- package/dist/chunk-IJHTRIZC.js.map +1 -0
- package/dist/chunk-IMPCCZ2Y.js +463 -0
- package/dist/chunk-IMPCCZ2Y.js.map +1 -0
- package/dist/chunk-J3WPBMJ5.js +332 -0
- package/dist/chunk-J3WPBMJ5.js.map +1 -0
- package/dist/chunk-MCXMS5ZI.js +30 -0
- package/dist/chunk-MCXMS5ZI.js.map +1 -0
- package/dist/chunk-NJRSFFD7.js +815 -0
- package/dist/chunk-NJRSFFD7.js.map +1 -0
- package/dist/chunk-OUUTDSOW.js +24 -0
- package/dist/chunk-OUUTDSOW.js.map +1 -0
- package/dist/chunk-PHTWUTY7.js +300 -0
- package/dist/chunk-PHTWUTY7.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +9 -0
- package/dist/{chunk-4C666HHU.js.map → chunk-PZ5AY32C.js.map} +1 -1
- package/dist/chunk-QLCOEP5J.js +68 -0
- package/dist/chunk-QLCOEP5J.js.map +1 -0
- package/dist/chunk-RDWCGZ2A.js +87 -0
- package/dist/chunk-RDWCGZ2A.js.map +1 -0
- package/dist/chunk-RWRRBYG4.js +1 -0
- package/dist/client.d.ts +3 -3
- package/dist/client.js +9 -1
- package/dist/gateway.d.ts +3 -3
- package/dist/gateway.js +10 -1
- package/dist/{index-CL8m1L1d.d.ts → index-BlGc0iym.d.ts} +24 -1
- package/dist/{index-B_Vbrb_I.d.ts → index-qM8ZH8sC.d.ts} +2 -2
- package/dist/index.d.ts +6 -6
- package/dist/index.js +60 -4
- package/dist/index.js.map +1 -1
- package/dist/kyber-FCVPX6CE.js +4 -0
- package/dist/{kyber-NONMBQNH.js.map → kyber-FCVPX6CE.js.map} +1 -1
- package/dist/mesh.js +5 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +8 -1
- package/dist/{types-DzEXgi4s.d.ts → types-sKeUxuky.d.ts} +4 -46
- package/dist/types.d.ts +1 -1
- package/dist/types.js +4 -1
- package/dist/{verifier-DTCD9imJ.d.ts → verifier-COnid_dg.d.ts} +1 -1
- package/dist/verifier-GCZDNZK7.js +6 -0
- package/dist/{verifier-Z26UC7M4.js.map → verifier-GCZDNZK7.js.map} +1 -1
- package/dist/workers/logic-execution.js +256 -1
- package/dist/workers/logic-execution.js.map +1 -1
- package/dist/workers/zk-verifier.d.ts +2 -0
- package/dist/workers/zk-verifier.js +174 -1
- package/dist/workers/zk-verifier.js.map +1 -1
- package/package.json +43 -44
- package/dist/chunk-2MGFSIXN.js +0 -2
- package/dist/chunk-2MGFSIXN.js.map +0 -1
- package/dist/chunk-4C666HHU.js +0 -2
- package/dist/chunk-ANFXJGMP.js +0 -2
- package/dist/chunk-ANFXJGMP.js.map +0 -1
- package/dist/chunk-DBXGYHKY.js +0 -2
- package/dist/chunk-DBXGYHKY.js.map +0 -1
- package/dist/chunk-DQ6UW6L7.js +0 -2
- package/dist/chunk-DQ6UW6L7.js.map +0 -1
- package/dist/chunk-L5A64CNT.js +0 -54
- package/dist/chunk-L5A64CNT.js.map +0 -1
- package/dist/chunk-N6FGTZ6A.js +0 -3
- package/dist/chunk-N6FGTZ6A.js.map +0 -1
- package/dist/chunk-RYYRR4N5.js +0 -31
- package/dist/chunk-RYYRR4N5.js.map +0 -1
- package/dist/chunk-S6RJHZV2.js +0 -2
- package/dist/chunk-S6RJHZV2.js.map +0 -1
- package/dist/chunk-SB5XJXKV.js +0 -2
- package/dist/chunk-SB5XJXKV.js.map +0 -1
- package/dist/chunk-SW53FNSN.js +0 -2
- package/dist/chunk-SW53FNSN.js.map +0 -1
- package/dist/chunk-TYVG6TXQ.js +0 -2
- package/dist/chunk-TYVG6TXQ.js.map +0 -1
- package/dist/chunk-V5MKJT6S.js +0 -2
- package/dist/chunk-V5MKJT6S.js.map +0 -1
- package/dist/chunk-VGXNGTIC.js +0 -33
- package/dist/chunk-VGXNGTIC.js.map +0 -1
- package/dist/chunk-W2QGWRTT.js +0 -3
- package/dist/chunk-W2QGWRTT.js.map +0 -1
- package/dist/chunk-YZVCAJJO.js +0 -13
- package/dist/chunk-YZVCAJJO.js.map +0 -1
- package/dist/kyber-NONMBQNH.js +0 -2
- package/dist/verifier-Z26UC7M4.js +0 -2
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { deriveLogicImageDigest } from './chunk-OUUTDSOW.js';
|
|
2
|
+
import { log } from './chunk-72MNYFR6.js';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import { createRequire } from 'module';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
7
|
+
import { Piscina } from 'piscina';
|
|
8
|
+
|
|
9
|
+
var __filename$1 = fileURLToPath(import.meta.url);
|
|
10
|
+
var __dirname$1 = path.dirname(__filename$1);
|
|
11
|
+
var LiopVerifier = class _LiopVerifier {
|
|
12
|
+
// Singleton Worker Pool for heavy ZK verification
|
|
13
|
+
static zkWorkerPool = null;
|
|
14
|
+
getZkPool() {
|
|
15
|
+
if (!_LiopVerifier.zkWorkerPool) {
|
|
16
|
+
const isTS = import.meta.url.endsWith(".ts");
|
|
17
|
+
const workerExt = isTS ? ".ts" : ".js";
|
|
18
|
+
let execArgv = [];
|
|
19
|
+
if (isTS) {
|
|
20
|
+
try {
|
|
21
|
+
const req = createRequire(import.meta.url);
|
|
22
|
+
const tsxPkg = req.resolve("tsx/package.json");
|
|
23
|
+
const absoluteTsx = pathToFileURL(
|
|
24
|
+
path.join(path.dirname(tsxPkg), "dist", "loader.mjs")
|
|
25
|
+
).href;
|
|
26
|
+
execArgv = ["--import", absoluteTsx];
|
|
27
|
+
} catch (_e) {
|
|
28
|
+
execArgv = ["--import", "tsx"];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const workerPaths = [
|
|
32
|
+
path.resolve(__dirname$1, `./workers/zk-verifier${workerExt}`),
|
|
33
|
+
// Flat dist/ (tsup)
|
|
34
|
+
path.resolve(__dirname$1, `../workers/zk-verifier${workerExt}`)
|
|
35
|
+
// Original src/
|
|
36
|
+
];
|
|
37
|
+
const workerFilename = workerPaths.find((p) => fs.existsSync(p)) || workerPaths[1];
|
|
38
|
+
_LiopVerifier.zkWorkerPool = new Piscina({
|
|
39
|
+
filename: workerFilename,
|
|
40
|
+
minThreads: 1,
|
|
41
|
+
maxThreads: 2,
|
|
42
|
+
// Minimal footprint since verification is fast compared to generation
|
|
43
|
+
idleTimeout: 3e4,
|
|
44
|
+
execArgv
|
|
45
|
+
});
|
|
46
|
+
_LiopVerifier.zkWorkerPool.run({ action: "warmup" }).catch((err) => {
|
|
47
|
+
log.debug(
|
|
48
|
+
`[LiopVerifier] Verification pool warm-up ping failed: ${err.message}`
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return _LiopVerifier.zkWorkerPool;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Verifies a Zero-Knowledge Receipt from a remote LIOP node via Worker Pool.
|
|
56
|
+
*
|
|
57
|
+
* @param logicPayload The raw WASM or JS logic that was sent to the provider.
|
|
58
|
+
* @param remoteImageIdHex The ImageID reported by the provider (must match our local calculation).
|
|
59
|
+
* @param zkReceipt The mathematical proof (Seal + Journal) from the zkVM.
|
|
60
|
+
*/
|
|
61
|
+
async verifyZkReceipt(logicPayload, remoteImageIdHex, zkReceipt, sessionSecret, expectedOutput) {
|
|
62
|
+
const pool = this.getZkPool();
|
|
63
|
+
if (!pool) throw new Error("Worker pool initialization failed");
|
|
64
|
+
const result = await pool.run({
|
|
65
|
+
action: "verify_receipt",
|
|
66
|
+
logicPayload: new Uint8Array(logicPayload),
|
|
67
|
+
remoteImageIdHex,
|
|
68
|
+
zkReceipt: new Uint8Array(zkReceipt),
|
|
69
|
+
sessionSecret: sessionSecret ? new Uint8Array(sessionSecret) : void 0,
|
|
70
|
+
expectedOutput
|
|
71
|
+
});
|
|
72
|
+
if (result.verified) {
|
|
73
|
+
log.info(`[LiopVerifier] ${result.message}`);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
log.error(`[LiopVerifier] FAILED: ${result.message}`);
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Verifies if a node is running inside an authenticated TEE (e.g. AWS Nitro).
|
|
81
|
+
*
|
|
82
|
+
* @param attestationReport The COSE-signed attestation document from the hardware.
|
|
83
|
+
*/
|
|
84
|
+
async verifyTeeAttestation(attestationReport) {
|
|
85
|
+
if (attestationReport.length === 0) return true;
|
|
86
|
+
try {
|
|
87
|
+
log.info("[LiopVerifier] TEE Attestation: Not configured (no-op).");
|
|
88
|
+
return true;
|
|
89
|
+
} catch (err) {
|
|
90
|
+
log.error("[LiopVerifier] TEE Verification Failed:", err);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Derives the ImageID of a logic payload following the LIOP v1 Standard.
|
|
96
|
+
*/
|
|
97
|
+
deriveImageId(logicPayload) {
|
|
98
|
+
return deriveLogicImageDigest(logicPayload);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { LiopVerifier };
|
|
103
|
+
//# sourceMappingURL=chunk-32ADSAJS.js.map
|
|
104
|
+
//# sourceMappingURL=chunk-32ADSAJS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/crypto/verifier.ts"],"names":["__filename","__dirname"],"mappings":"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","file":"chunk-32ADSAJS.js","sourcesContent":["import * as fs from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport path from \"node:path\";\nimport { fileURLToPath, pathToFileURL } from \"node:url\";\nimport { Piscina } from \"piscina\";\nimport { log } from \"../utils/logger.js\";\nimport { deriveLogicImageDigest } from \"./logic-image-id.js\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\n/**\n * LIOP Tier-0 Industrial Verifier\n *\n * This engine is responsible for the trustless verification of remote logic execution.\n * It validates both the integrity of the code (ZkImageID) and the mathematical proof\n * of its execution (ZkSeal), as well as hardware-level attestation (TEE).\n */\nexport class LiopVerifier {\n\t// Singleton Worker Pool for heavy ZK verification\n\tprivate static zkWorkerPool: Piscina | null = null;\n\n\tprivate getZkPool() {\n\t\tif (!LiopVerifier.zkWorkerPool) {\n\t\t\tconst isTS = import.meta.url.endsWith(\".ts\");\n\t\t\tconst workerExt = isTS ? \".ts\" : \".js\";\n\n\t\t\tlet execArgv: string[] = [];\n\t\t\tif (isTS) {\n\t\t\t\ttry {\n\t\t\t\t\tconst req = createRequire(import.meta.url);\n\t\t\t\t\tconst tsxPkg = req.resolve(\"tsx/package.json\");\n\t\t\t\t\tconst absoluteTsx = pathToFileURL(\n\t\t\t\t\t\tpath.join(path.dirname(tsxPkg), \"dist\", \"loader.mjs\"),\n\t\t\t\t\t).href;\n\t\t\t\t\texecArgv = [\"--import\", absoluteTsx];\n\t\t\t\t} catch (_e) {\n\t\t\t\t\texecArgv = [\"--import\", \"tsx\"];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Support both flat dist/ and original src/ structure\n\t\t\tconst workerPaths = [\n\t\t\t\tpath.resolve(__dirname, `./workers/zk-verifier${workerExt}`), // Flat dist/ (tsup)\n\t\t\t\tpath.resolve(__dirname, `../workers/zk-verifier${workerExt}`), // Original src/\n\t\t\t];\n\n\t\t\tconst workerFilename =\n\t\t\t\tworkerPaths.find((p) => fs.existsSync(p)) || workerPaths[1];\n\n\t\t\tLiopVerifier.zkWorkerPool = new Piscina({\n\t\t\t\tfilename: workerFilename,\n\t\t\t\tminThreads: 1,\n\t\t\t\tmaxThreads: 2, // Minimal footprint since verification is fast compared to generation\n\t\t\t\tidleTimeout: 30000,\n\t\t\t\texecArgv,\n\t\t\t});\n\n\t\t\t// Pre-warm the verification worker\n\t\t\tLiopVerifier.zkWorkerPool.run({ action: \"warmup\" }).catch((err) => {\n\t\t\t\tlog.debug(\n\t\t\t\t\t`[LiopVerifier] Verification pool warm-up ping failed: ${err.message}`,\n\t\t\t\t);\n\t\t\t});\n\t\t}\n\t\treturn LiopVerifier.zkWorkerPool;\n\t}\n\n\t/**\n\t * Verifies a Zero-Knowledge Receipt from a remote LIOP node via Worker Pool.\n\t *\n\t * @param logicPayload The raw WASM or JS logic that was sent to the provider.\n\t * @param remoteImageIdHex The ImageID reported by the provider (must match our local calculation).\n\t * @param zkReceipt The mathematical proof (Seal + Journal) from the zkVM.\n\t */\n\tpublic async verifyZkReceipt(\n\t\tlogicPayload: Buffer,\n\t\tremoteImageIdHex: string,\n\t\tzkReceipt: Buffer,\n\t\tsessionSecret?: Buffer,\n\t\texpectedOutput?: unknown,\n\t): Promise<boolean> {\n\t\tconst pool = this.getZkPool();\n\t\tif (!pool) throw new Error(\"Worker pool initialization failed\");\n\t\tconst result = await pool.run({\n\t\t\taction: \"verify_receipt\",\n\t\t\tlogicPayload: new Uint8Array(logicPayload),\n\t\t\tremoteImageIdHex,\n\t\t\tzkReceipt: new Uint8Array(zkReceipt),\n\t\t\tsessionSecret: sessionSecret ? new Uint8Array(sessionSecret) : undefined,\n\t\t\texpectedOutput,\n\t\t});\n\n\t\tif (result.verified) {\n\t\t\tlog.info(`[LiopVerifier] ${result.message}`);\n\t\t\treturn true;\n\t\t}\n\n\t\tlog.error(`[LiopVerifier] FAILED: ${result.message}`);\n\t\treturn false;\n\t}\n\n\t/**\n\t * Verifies if a node is running inside an authenticated TEE (e.g. AWS Nitro).\n\t *\n\t * @param attestationReport The COSE-signed attestation document from the hardware.\n\t */\n\tpublic async verifyTeeAttestation(\n\t\tattestationReport: Buffer,\n\t): Promise<boolean> {\n\t\tif (attestationReport.length === 0) return true; // Optional in Mesh Alpha\n\n\t\ttry {\n\t\t\t// Architecture for AWS Nitro Enclaves:\n\t\t\t// 1. Decode CBOR/COSE\n\t\t\t// 2. Verify Signature against AWS Nitro Root CA\n\t\t\t// 3. Compare PCRs\n\t\t\tlog.info(\"[LiopVerifier] TEE Attestation: Not configured (no-op).\");\n\t\t\treturn true;\n\t\t} catch (err) {\n\t\t\tlog.error(\"[LiopVerifier] TEE Verification Failed:\", err);\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Derives the ImageID of a logic payload following the LIOP v1 Standard.\n\t */\n\tpublic deriveImageId(logicPayload: Buffer): Buffer {\n\t\treturn deriveLogicImageDigest(logicPayload);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/utils/logger.ts
|
|
2
|
+
var LiopLogger = class _LiopLogger {
|
|
3
|
+
static instance;
|
|
4
|
+
level = "info";
|
|
5
|
+
constructor() {
|
|
6
|
+
this.setLevelFromEnv();
|
|
7
|
+
}
|
|
8
|
+
static getInstance() {
|
|
9
|
+
if (!_LiopLogger.instance) {
|
|
10
|
+
_LiopLogger.instance = new _LiopLogger();
|
|
11
|
+
}
|
|
12
|
+
return _LiopLogger.instance;
|
|
13
|
+
}
|
|
14
|
+
setLevelFromEnv() {
|
|
15
|
+
const envLevel = process.env.LIOP_LOG_LEVEL?.toLowerCase();
|
|
16
|
+
if (envLevel === "silent" || envLevel === "error" || envLevel === "warn" || envLevel === "info" || envLevel === "debug") {
|
|
17
|
+
this.level = envLevel;
|
|
18
|
+
} else {
|
|
19
|
+
this.level = "info";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
setLevel(level) {
|
|
23
|
+
this.level = level;
|
|
24
|
+
}
|
|
25
|
+
shouldLog(targetLevel) {
|
|
26
|
+
const levels = {
|
|
27
|
+
silent: 0,
|
|
28
|
+
error: 1,
|
|
29
|
+
warn: 2,
|
|
30
|
+
info: 3,
|
|
31
|
+
debug: 4
|
|
32
|
+
};
|
|
33
|
+
return levels[this.level] >= levels[targetLevel];
|
|
34
|
+
}
|
|
35
|
+
formatMessage(level, message) {
|
|
36
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
37
|
+
return `[${ts}] [${level}] ${message}`;
|
|
38
|
+
}
|
|
39
|
+
error(message, ...args) {
|
|
40
|
+
if (this.shouldLog("error")) {
|
|
41
|
+
console.error(this.formatMessage("ERROR", message), ...args);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
warn(message, ...args) {
|
|
45
|
+
if (this.shouldLog("warn")) {
|
|
46
|
+
console.error(this.formatMessage("WARN", message), ...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
info(message, ...args) {
|
|
50
|
+
if (this.shouldLog("info")) {
|
|
51
|
+
console.error(this.formatMessage("INFO", message), ...args);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
debug(message, ...args) {
|
|
55
|
+
if (this.shouldLog("debug")) {
|
|
56
|
+
console.error(this.formatMessage("DEBUG", message), ...args);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var log = LiopLogger.getInstance();
|
|
61
|
+
|
|
62
|
+
export { log };
|
|
63
|
+
//# sourceMappingURL=chunk-72MNYFR6.js.map
|
|
64
|
+
//# sourceMappingURL=chunk-72MNYFR6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/logger.ts"],"names":[],"mappings":";AAOO,IAAM,UAAA,GAAN,MAAM,WAAA,CAAW;AAAA,EACvB,OAAe,QAAA;AAAA,EACP,KAAA,GAAkB,MAAA;AAAA,EAElB,WAAA,GAAc;AACrB,IAAA,IAAA,CAAK,eAAA,EAAgB;AAAA,EACtB;AAAA,EAEA,OAAc,WAAA,GAA0B;AACvC,IAAA,IAAI,CAAC,YAAW,QAAA,EAAU;AACzB,MAAA,WAAA,CAAW,QAAA,GAAW,IAAI,WAAA,EAAW;AAAA,IACtC;AACA,IAAA,OAAO,WAAA,CAAW,QAAA;AAAA,EACnB;AAAA,EAEQ,eAAA,GAAkB;AACzB,IAAA,MAAM,QAAA,GAAW,OAAA,CAAQ,GAAA,CAAI,cAAA,EAAgB,WAAA,EAAY;AACzD,IAAA,IACC,QAAA,KAAa,YACb,QAAA,KAAa,OAAA,IACb,aAAa,MAAA,IACb,QAAA,KAAa,MAAA,IACb,QAAA,KAAa,OAAA,EACZ;AACD,MAAA,IAAA,CAAK,KAAA,GAAQ,QAAA;AAAA,IACd,CAAA,MAAO;AAEN,MAAA,IAAA,CAAK,KAAA,GAAQ,MAAA;AAAA,IACd;AAAA,EACD;AAAA,EAEO,SAAS,KAAA,EAAiB;AAChC,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AAAA,EACd;AAAA,EAEQ,UAAU,WAAA,EAAgC;AACjD,IAAA,MAAM,MAAA,GAAmC;AAAA,MACxC,MAAA,EAAQ,CAAA;AAAA,MACR,KAAA,EAAO,CAAA;AAAA,MACP,IAAA,EAAM,CAAA;AAAA,MACN,IAAA,EAAM,CAAA;AAAA,MACN,KAAA,EAAO;AAAA,KACR;AACA,IAAA,OAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,IAAK,OAAO,WAAW,CAAA;AAAA,EAChD;AAAA,EAEQ,aAAA,CAAc,OAAe,OAAA,EAAyB;AAC7D,IAAA,MAAM,EAAA,GAAA,iBAAK,IAAI,IAAA,EAAK,EAAE,WAAA,EAAY;AAClC,IAAA,OAAO,CAAA,CAAA,EAAI,EAAE,CAAA,GAAA,EAAM,KAAK,KAAK,OAAO,CAAA,CAAA;AAAA,EACrC;AAAA,EAEO,KAAA,CAAM,YAAoB,IAAA,EAAiB;AACjD,IAAA,IAAI,IAAA,CAAK,SAAA,CAAU,OAAO,CAAA,EAAG;AAC5B,MAAA,OAAA,CAAQ,MAAM,IAAA,CAAK,aAAA,CAAc,SAAS,OAAO,CAAA,EAAG,GAAG,IAAI,CAAA;AAAA,IAC5D;AAAA,EACD;AAAA,EAEO,IAAA,CAAK,YAAoB,IAAA,EAAiB;AAChD,IAAA,IAAI,IAAA,CAAK,SAAA,CAAU,MAAM,CAAA,EAAG;AAC3B,MAAA,OAAA,CAAQ,MAAM,IAAA,CAAK,aAAA,CAAc,QAAQ,OAAO,CAAA,EAAG,GAAG,IAAI,CAAA;AAAA,IAC3D;AAAA,EACD;AAAA,EAEO,IAAA,CAAK,YAAoB,IAAA,EAAiB;AAChD,IAAA,IAAI,IAAA,CAAK,SAAA,CAAU,MAAM,CAAA,EAAG;AAC3B,MAAA,OAAA,CAAQ,MAAM,IAAA,CAAK,aAAA,CAAc,QAAQ,OAAO,CAAA,EAAG,GAAG,IAAI,CAAA;AAAA,IAC3D;AAAA,EACD;AAAA,EAEO,KAAA,CAAM,YAAoB,IAAA,EAAiB;AACjD,IAAA,IAAI,IAAA,CAAK,SAAA,CAAU,OAAO,CAAA,EAAG;AAC5B,MAAA,OAAA,CAAQ,MAAM,IAAA,CAAK,aAAA,CAAc,SAAS,OAAO,CAAA,EAAG,GAAG,IAAI,CAAA;AAAA,IAC5D;AAAA,EACD;AACD,CAAA;AAEO,IAAM,GAAA,GAAM,WAAW,WAAA","file":"chunk-72MNYFR6.js","sourcesContent":["export type LogLevel = \"silent\" | \"error\" | \"warn\" | \"info\" | \"debug\";\n\n/**\n * LiopLogger - Structured Logging Abstraction\n * Configurable via `process.env.LIOP_LOG_LEVEL`.\n * Emits strictly to stderr to comply with MCP stdio protocols.\n */\nexport class LiopLogger {\n\tprivate static instance: LiopLogger;\n\tprivate level: LogLevel = \"info\";\n\n\tprivate constructor() {\n\t\tthis.setLevelFromEnv();\n\t}\n\n\tpublic static getInstance(): LiopLogger {\n\t\tif (!LiopLogger.instance) {\n\t\t\tLiopLogger.instance = new LiopLogger();\n\t\t}\n\t\treturn LiopLogger.instance;\n\t}\n\n\tprivate setLevelFromEnv() {\n\t\tconst envLevel = process.env.LIOP_LOG_LEVEL?.toLowerCase();\n\t\tif (\n\t\t\tenvLevel === \"silent\" ||\n\t\t\tenvLevel === \"error\" ||\n\t\t\tenvLevel === \"warn\" ||\n\t\t\tenvLevel === \"info\" ||\n\t\t\tenvLevel === \"debug\"\n\t\t) {\n\t\t\tthis.level = envLevel as LogLevel;\n\t\t} else {\n\t\t\t// Default level: info\n\t\t\tthis.level = \"info\";\n\t\t}\n\t}\n\n\tpublic setLevel(level: LogLevel) {\n\t\tthis.level = level;\n\t}\n\n\tprivate shouldLog(targetLevel: LogLevel): boolean {\n\t\tconst levels: Record<LogLevel, number> = {\n\t\t\tsilent: 0,\n\t\t\terror: 1,\n\t\t\twarn: 2,\n\t\t\tinfo: 3,\n\t\t\tdebug: 4,\n\t\t};\n\t\treturn levels[this.level] >= levels[targetLevel];\n\t}\n\n\tprivate formatMessage(level: string, message: string): string {\n\t\tconst ts = new Date().toISOString();\n\t\treturn `[${ts}] [${level}] ${message}`;\n\t}\n\n\tpublic error(message: string, ...args: unknown[]) {\n\t\tif (this.shouldLog(\"error\")) {\n\t\t\tconsole.error(this.formatMessage(\"ERROR\", message), ...args);\n\t\t}\n\t}\n\n\tpublic warn(message: string, ...args: unknown[]) {\n\t\tif (this.shouldLog(\"warn\")) {\n\t\t\tconsole.error(this.formatMessage(\"WARN\", message), ...args);\n\t\t}\n\t}\n\n\tpublic info(message: string, ...args: unknown[]) {\n\t\tif (this.shouldLog(\"info\")) {\n\t\t\tconsole.error(this.formatMessage(\"INFO\", message), ...args);\n\t\t}\n\t}\n\n\tpublic debug(message: string, ...args: unknown[]) {\n\t\tif (this.shouldLog(\"debug\")) {\n\t\t\tconsole.error(this.formatMessage(\"DEBUG\", message), ...args);\n\t\t}\n\t}\n}\n\nexport const log = LiopLogger.getInstance();\n"]}
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import { LiopVerifier } from './chunk-32ADSAJS.js';
|
|
2
|
+
import { Kyber768Wrapper } from './chunk-QLCOEP5J.js';
|
|
3
|
+
import { createChannelCredentials, liopV1 } from './chunk-RDWCGZ2A.js';
|
|
4
|
+
import { MeshNode } from './chunk-NJRSFFD7.js';
|
|
5
|
+
import { log } from './chunk-72MNYFR6.js';
|
|
6
|
+
import * as grpc from '@grpc/grpc-js';
|
|
7
|
+
import { createDecipheriv, randomBytes, createCipheriv } from 'crypto';
|
|
8
|
+
|
|
9
|
+
var LiopRpcClient = class {
|
|
10
|
+
// biome-ignore lint/suspicious/noExplicitAny: internal gRPC client type
|
|
11
|
+
client;
|
|
12
|
+
token;
|
|
13
|
+
constructor(address, tls, token) {
|
|
14
|
+
const credentials = createChannelCredentials(tls);
|
|
15
|
+
this.client = new liopV1.LogicMesh(address, credentials);
|
|
16
|
+
this.token = token;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Negotiates intent with the remote host.
|
|
20
|
+
* Returns the ephemeral Kyber public key for payload encryption.
|
|
21
|
+
*/
|
|
22
|
+
async negotiateIntent(request) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const metadata = new grpc.Metadata();
|
|
25
|
+
if (this.token) {
|
|
26
|
+
metadata.add("authorization", `Bearer ${this.token}`);
|
|
27
|
+
}
|
|
28
|
+
this.client.NegotiateIntent(
|
|
29
|
+
request,
|
|
30
|
+
metadata,
|
|
31
|
+
(error, response) => {
|
|
32
|
+
if (error) {
|
|
33
|
+
reject(error);
|
|
34
|
+
} else {
|
|
35
|
+
resolve(response);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Pushes the encrypted Logic-on-Origin payload to the origin.
|
|
43
|
+
* Returns a stream of semantic responses and ZK proofs.
|
|
44
|
+
*/
|
|
45
|
+
executeLogic(request) {
|
|
46
|
+
const metadata = new grpc.Metadata();
|
|
47
|
+
if (this.token) {
|
|
48
|
+
metadata.add("authorization", `Bearer ${this.token}`);
|
|
49
|
+
}
|
|
50
|
+
return this.client.ExecuteLogic(request, metadata);
|
|
51
|
+
}
|
|
52
|
+
close() {
|
|
53
|
+
this.client.close();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var AesGcmWrapper = {
|
|
57
|
+
/**
|
|
58
|
+
* Encrypts a raw WASM payload using the shared secret negotiated via Kyber768.
|
|
59
|
+
*
|
|
60
|
+
* @param payload Raw incoming WASM byte array or string.
|
|
61
|
+
* @param sharedSecret A perfectly derived 32-byte (256-bit) shared secret array
|
|
62
|
+
* @returns The encrypted buffer to push to the GRPc stream, along with the 12-byte initialization vector natively generated.
|
|
63
|
+
*/
|
|
64
|
+
encryptPayload(payload, sharedSecret) {
|
|
65
|
+
if (sharedSecret.length !== 32) {
|
|
66
|
+
throw new Error("Symmetric Key must be exactly 32 bytes (256 bits).");
|
|
67
|
+
}
|
|
68
|
+
const nonce = randomBytes(12);
|
|
69
|
+
const cipher = createCipheriv("aes-256-gcm", sharedSecret, nonce);
|
|
70
|
+
const encrypted = Buffer.concat([cipher.update(payload), cipher.final()]);
|
|
71
|
+
const authTag = cipher.getAuthTag();
|
|
72
|
+
const finalCiphertext = Buffer.concat([encrypted, authTag]);
|
|
73
|
+
return {
|
|
74
|
+
ciphertext: finalCiphertext,
|
|
75
|
+
nonce
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* Decrypts a remote Zero-Knowledge receipt using AES-256-GCM.
|
|
80
|
+
*/
|
|
81
|
+
decryptPayload(ciphertextBuffer, nonce, sharedSecret) {
|
|
82
|
+
if (ciphertextBuffer.length < 16) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
"Invalid GCM Ciphertext; missing authentication tag length"
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const encryptedData = ciphertextBuffer.subarray(0, -16);
|
|
88
|
+
const authTag = ciphertextBuffer.subarray(-16);
|
|
89
|
+
const decipher = createDecipheriv("aes-256-gcm", sharedSecret, nonce);
|
|
90
|
+
decipher.setAuthTag(authTag);
|
|
91
|
+
return Buffer.concat([decipher.update(encryptedData), decipher.final()]);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/client/index.ts
|
|
96
|
+
var LiopClient = class {
|
|
97
|
+
meshNode = null;
|
|
98
|
+
rpcClients = /* @__PURE__ */ new Map();
|
|
99
|
+
manifests = /* @__PURE__ */ new Map();
|
|
100
|
+
tlsOptions;
|
|
101
|
+
serverInfo;
|
|
102
|
+
verifier = new LiopVerifier();
|
|
103
|
+
oauthToken;
|
|
104
|
+
constructor(tls) {
|
|
105
|
+
this.tlsOptions = tls;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Requests an M2M access token from the Nexus Authorization Server using Client Credentials.
|
|
109
|
+
*/
|
|
110
|
+
async acquireM2MToken(authOpts) {
|
|
111
|
+
const baseUrl = authOpts.nexusUrl.endsWith("/oidc") ? authOpts.nexusUrl : `${authOpts.nexusUrl}/oidc`;
|
|
112
|
+
const tokenUrl = `${baseUrl}/token`;
|
|
113
|
+
log.info(`[LiopClient] Requesting M2M Token from Nexus AS: ${tokenUrl}`);
|
|
114
|
+
const params = new URLSearchParams({
|
|
115
|
+
grant_type: "client_credentials",
|
|
116
|
+
scope: authOpts.scope || "liop:tools:call liop:tools:list liop:resources:read liop:schema:read liop:mesh:query",
|
|
117
|
+
resource: authOpts.audience,
|
|
118
|
+
client_id: authOpts.clientId,
|
|
119
|
+
client_secret: authOpts.clientSecret
|
|
120
|
+
});
|
|
121
|
+
const response = await fetch(tokenUrl, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: {
|
|
124
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
125
|
+
},
|
|
126
|
+
body: params.toString()
|
|
127
|
+
});
|
|
128
|
+
if (!response.ok) {
|
|
129
|
+
const text = await response.text();
|
|
130
|
+
throw new Error(
|
|
131
|
+
`OAuth token request failed with status ${response.status}: ${text}`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const data = await response.json();
|
|
135
|
+
if (!data.access_token) {
|
|
136
|
+
throw new Error("OAuth token response did not contain an access_token.");
|
|
137
|
+
}
|
|
138
|
+
log.info("[LiopClient] M2M Token acquired successfully.");
|
|
139
|
+
return data.access_token;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Discovers and connects to the target server or mesh capability.
|
|
143
|
+
* If address is omitted, it sets up the MeshNode to act purely dynamically.
|
|
144
|
+
*/
|
|
145
|
+
async connect(address, options) {
|
|
146
|
+
const clientId = options?.auth?.clientId || process.env.LIOP_OAUTH_CLIENT_ID || process.env.LIOP_CLIENT_ID;
|
|
147
|
+
const clientSecret = options?.auth?.clientSecret || process.env.LIOP_OAUTH_CLIENT_SECRET || process.env.LIOP_CLIENT_SECRET;
|
|
148
|
+
const nexusUrl = options?.auth?.nexusUrl || process.env.LIOP_NEXUS_URL || "http://localhost:3000";
|
|
149
|
+
const audience = options?.auth?.audience || process.env.LIOP_OAUTH_AUDIENCE || "urn:liop:mesh:api";
|
|
150
|
+
const scope = options?.auth?.scope || process.env.LIOP_OAUTH_SCOPE || "liop:tools:call liop:tools:list liop:resources:read liop:schema:read liop:mesh:query";
|
|
151
|
+
this.oauthToken = options?.auth?.token || process.env.LIOP_OAUTH_TOKEN || process.env.LIOP_TOKEN;
|
|
152
|
+
if (clientId && clientSecret) {
|
|
153
|
+
try {
|
|
154
|
+
this.oauthToken = await this.acquireM2MToken({
|
|
155
|
+
clientId,
|
|
156
|
+
clientSecret,
|
|
157
|
+
nexusUrl,
|
|
158
|
+
audience,
|
|
159
|
+
scope
|
|
160
|
+
});
|
|
161
|
+
} catch (err) {
|
|
162
|
+
log.error(
|
|
163
|
+
`[LiopClient] Failed to acquire OAuth M2M Token: ${err instanceof Error ? err.message : String(err)}`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
this.meshNode = new MeshNode(options?.meshConfig);
|
|
168
|
+
await this.meshNode.start();
|
|
169
|
+
log.info(
|
|
170
|
+
`[LiopClient] Mesh Node synchronized. PeerID: ${this.meshNode.getPeerId()}`
|
|
171
|
+
);
|
|
172
|
+
if (address) {
|
|
173
|
+
this.rpcClients.set(
|
|
174
|
+
"static",
|
|
175
|
+
new LiopRpcClient(address, this.tlsOptions, this.oauthToken)
|
|
176
|
+
);
|
|
177
|
+
this.serverInfo = { name: `LiopServer (${address})`, version: "1.0.0" };
|
|
178
|
+
log.info(`[LiopClient] Static gRPC configured for: ${address}`);
|
|
179
|
+
} else {
|
|
180
|
+
this.serverInfo = { name: "LiopServer (Mesh Alpha)", version: "1.0.0" };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Dynamically queries Kademlia DHT to find the optimal PeerID providing the Capability
|
|
185
|
+
* and returns the physical gRPC target (host:port) resolved from the provider's manifest.
|
|
186
|
+
*/
|
|
187
|
+
async resolveCapability(toolName) {
|
|
188
|
+
if (!this.meshNode)
|
|
189
|
+
throw new Error(
|
|
190
|
+
"Client must be connected to Mesh to resolve capabilities."
|
|
191
|
+
);
|
|
192
|
+
log.info(`[LiopClient] Querying Mesh DHT for Provider: ${toolName}...`);
|
|
193
|
+
const providers = await this.meshNode.findProviders(toolName);
|
|
194
|
+
if (providers.length === 0) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`Kademlia DHT found zero providers for capability: ${toolName}`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const providerId = providers[0];
|
|
200
|
+
log.info(`[LiopClient] Identified Alpha Provider PeerID: ${providerId}`);
|
|
201
|
+
let grpcPort = 50051;
|
|
202
|
+
const manifest = await this.meshNode.queryManifest(providerId);
|
|
203
|
+
if (manifest) {
|
|
204
|
+
grpcPort = manifest.grpcPort;
|
|
205
|
+
log.info(`[LiopClient] Manifest resolved: gRPC port ${grpcPort}`);
|
|
206
|
+
}
|
|
207
|
+
const addrs = await this.meshNode.resolvePeer(providerId);
|
|
208
|
+
for (const maddr of addrs) {
|
|
209
|
+
const parts = maddr.split("/");
|
|
210
|
+
if (parts[1] === "ip4") {
|
|
211
|
+
const grpcHost = `${parts[2]}:${grpcPort}`;
|
|
212
|
+
log.info(
|
|
213
|
+
`[LiopClient] Translated Multiaddr to gRPC Target: ${grpcHost}`
|
|
214
|
+
);
|
|
215
|
+
return grpcHost;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return `127.0.0.1:${grpcPort}`;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Discovers remote capabilities via the LIOP Manifest Protocol.
|
|
222
|
+
*/
|
|
223
|
+
async discoverTools() {
|
|
224
|
+
if (!this.meshNode) {
|
|
225
|
+
throw new Error("Client must be connected before discovering tools.");
|
|
226
|
+
}
|
|
227
|
+
log.info(`[LiopClient] Discovery started...`);
|
|
228
|
+
const providerIds = await this.meshNode.discoverManifestProviders();
|
|
229
|
+
const tools = [];
|
|
230
|
+
const seenNames = /* @__PURE__ */ new Set();
|
|
231
|
+
for (const peerId of providerIds) {
|
|
232
|
+
try {
|
|
233
|
+
log.info(`[LiopClient] Querying manifest from: ${peerId}`);
|
|
234
|
+
const manifest = await this.meshNode.queryManifest(peerId);
|
|
235
|
+
if (manifest) {
|
|
236
|
+
this.manifests.set(peerId, manifest);
|
|
237
|
+
for (const tool of manifest.tools) {
|
|
238
|
+
if (!seenNames.has(tool.name)) {
|
|
239
|
+
tools.push({ name: tool.name, description: tool.description });
|
|
240
|
+
seenNames.add(tool.name);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} catch (err) {
|
|
245
|
+
log.info(
|
|
246
|
+
`[LiopClient] Error querying manifest from ${peerId}:`,
|
|
247
|
+
err instanceof Error ? err.message : String(err)
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
log.info(
|
|
252
|
+
`[LiopClient] Discovery finished. Found ${tools.length} unique tools.`
|
|
253
|
+
);
|
|
254
|
+
return tools;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Invokes a tool.
|
|
258
|
+
*/
|
|
259
|
+
async callTool(request, _wasmPayload) {
|
|
260
|
+
if (!this.meshNode) {
|
|
261
|
+
throw new Error("Client must be connected before calling tools.");
|
|
262
|
+
}
|
|
263
|
+
const toolName = request.name;
|
|
264
|
+
log.info(`[LiopClient] Resolving Tool: ${toolName}`);
|
|
265
|
+
let rpcClient = this.rpcClients.get("static");
|
|
266
|
+
if (!rpcClient) {
|
|
267
|
+
const dynamicAddress = await this.resolveCapability(toolName);
|
|
268
|
+
rpcClient = this.getOrCreateRpcClient(toolName, dynamicAddress);
|
|
269
|
+
} else {
|
|
270
|
+
log.info(
|
|
271
|
+
`[LiopClient] Using existing static gRPC connection for ${toolName}.`
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
log.info(`[LiopClient] Negotiating intent for ${toolName}...`);
|
|
275
|
+
const agentDid = this.meshNode ? `did:liop:${this.meshNode.getPeerId()}` : "did:liop:ephemeral";
|
|
276
|
+
const intentPayload = Buffer.from(`${toolName}:${Date.now()}`);
|
|
277
|
+
const proofOfIntent = this.meshNode ? await this.meshNode.sign(intentPayload) : intentPayload;
|
|
278
|
+
const intentResponse = await rpcClient.negotiateIntent({
|
|
279
|
+
agent_did: agentDid,
|
|
280
|
+
capability_hash: toolName,
|
|
281
|
+
proof_of_intent: proofOfIntent
|
|
282
|
+
});
|
|
283
|
+
if (!intentResponse.accepted) {
|
|
284
|
+
throw new Error(`Intent denied by host: ${intentResponse.error_message}`);
|
|
285
|
+
}
|
|
286
|
+
const publicKey = intentResponse.kyber_public_key || intentResponse.kyberPublicKey;
|
|
287
|
+
const sessionToken = intentResponse.session_token || intentResponse.sessionToken;
|
|
288
|
+
if (!publicKey) {
|
|
289
|
+
log.info(
|
|
290
|
+
"[LiopClient] Critical Error: Kyber Public Key not found in IntentResponse.",
|
|
291
|
+
intentResponse
|
|
292
|
+
);
|
|
293
|
+
throw new Error(
|
|
294
|
+
"Handshake failed: Remote host did not provide a valid Kyber Public Key."
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
log.info(
|
|
298
|
+
`[LiopClient] Encapsulating Post-Quantum Shared Secret for ${request.name}...`
|
|
299
|
+
);
|
|
300
|
+
const { ciphertext: kyberCiphertext, sharedSecret } = await Kyber768Wrapper.encapsulateAsymmetric(publicKey);
|
|
301
|
+
log.info(`[LiopClient] Sealing WASM Payload and Inputs...`);
|
|
302
|
+
const _safePayload = _wasmPayload || Buffer.from("");
|
|
303
|
+
const { ciphertext: encryptedWasm, nonce: aesNonce } = AesGcmWrapper.encryptPayload(_safePayload, sharedSecret);
|
|
304
|
+
const encryptedInputs = {};
|
|
305
|
+
const crypto = await import('crypto');
|
|
306
|
+
for (const [key, value] of Object.entries(request.arguments || {})) {
|
|
307
|
+
const inputNonce = crypto.randomBytes(12);
|
|
308
|
+
const cipher = crypto.createCipheriv(
|
|
309
|
+
"aes-256-gcm",
|
|
310
|
+
sharedSecret,
|
|
311
|
+
inputNonce
|
|
312
|
+
);
|
|
313
|
+
const encrypted = Buffer.concat([
|
|
314
|
+
cipher.update(JSON.stringify(value)),
|
|
315
|
+
cipher.final()
|
|
316
|
+
]);
|
|
317
|
+
const authTag = cipher.getAuthTag();
|
|
318
|
+
encryptedInputs[key] = Buffer.concat([inputNonce, encrypted, authTag]);
|
|
319
|
+
}
|
|
320
|
+
const logicRequest = {
|
|
321
|
+
session_token: sessionToken,
|
|
322
|
+
wasm_binary: encryptedWasm,
|
|
323
|
+
inputs: encryptedInputs,
|
|
324
|
+
pqc_ciphertext: kyberCiphertext,
|
|
325
|
+
aes_nonce: aesNonce
|
|
326
|
+
};
|
|
327
|
+
return new Promise((resolve, reject) => {
|
|
328
|
+
const stream = rpcClient.executeLogic(logicRequest);
|
|
329
|
+
if (!stream) {
|
|
330
|
+
reject(new Error("RPC Client unavailable or failed to create stream."));
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
let resultFulfilled = false;
|
|
334
|
+
let hasReceivedData = false;
|
|
335
|
+
stream.on("data", async (response) => {
|
|
336
|
+
if (resultFulfilled) return;
|
|
337
|
+
hasReceivedData = true;
|
|
338
|
+
log.info("[LiopClient] Logic Executed. Verification in progress...");
|
|
339
|
+
try {
|
|
340
|
+
if (!response.is_error) {
|
|
341
|
+
const isValid = await this.verifier.verifyZkReceipt(
|
|
342
|
+
_safePayload,
|
|
343
|
+
Buffer.from(response.cryptographic_proof).toString("hex"),
|
|
344
|
+
Buffer.from(response.zk_receipt),
|
|
345
|
+
Buffer.from(sharedSecret),
|
|
346
|
+
response.semantic_evidence
|
|
347
|
+
);
|
|
348
|
+
if (!isValid) {
|
|
349
|
+
reject(
|
|
350
|
+
new Error(
|
|
351
|
+
"PROTOCOL INTEGRITY VIOLATION: ZK-Receipt verification failed."
|
|
352
|
+
)
|
|
353
|
+
);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
resultFulfilled = true;
|
|
358
|
+
resolve({
|
|
359
|
+
content: [
|
|
360
|
+
{
|
|
361
|
+
type: "text",
|
|
362
|
+
text: response.semantic_evidence
|
|
363
|
+
}
|
|
364
|
+
],
|
|
365
|
+
isError: response.is_error
|
|
366
|
+
});
|
|
367
|
+
} catch (err) {
|
|
368
|
+
reject(err);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
stream.on("error", (err) => {
|
|
372
|
+
if (resultFulfilled) return;
|
|
373
|
+
log.error("[LiopClient] Stream Error:", err);
|
|
374
|
+
reject(err);
|
|
375
|
+
});
|
|
376
|
+
stream.on("end", () => {
|
|
377
|
+
if (!hasReceivedData && !resultFulfilled) {
|
|
378
|
+
reject(new Error("Logic-on-Origin stream closed without results."));
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
getOrCreateRpcClient(peerId, address) {
|
|
384
|
+
let client = this.rpcClients.get(peerId);
|
|
385
|
+
if (!client) {
|
|
386
|
+
let nodeToken = this.oauthToken;
|
|
387
|
+
let manifest = this.manifests.get(peerId);
|
|
388
|
+
let realPeerId = peerId;
|
|
389
|
+
if (!manifest) {
|
|
390
|
+
for (const [pId, m] of this.manifests.entries()) {
|
|
391
|
+
if (m.tools.some((t) => t.name === peerId)) {
|
|
392
|
+
manifest = m;
|
|
393
|
+
realPeerId = pId;
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const providerName = manifest?.serverInfo?.name?.toLowerCase() || "";
|
|
399
|
+
let envToken;
|
|
400
|
+
const slug = manifest?.tokenSlug;
|
|
401
|
+
if (slug) {
|
|
402
|
+
envToken = process.env[`LIOP_TOKEN_${slug}`] || process.env[`LIOP_OAUTH_TOKEN_${slug}`];
|
|
403
|
+
}
|
|
404
|
+
if (!envToken && realPeerId) {
|
|
405
|
+
const shortId = realPeerId.slice(-8).toUpperCase();
|
|
406
|
+
envToken = process.env[`LIOP_TOKEN_${shortId}`] || process.env[`LIOP_OAUTH_TOKEN_${shortId}`];
|
|
407
|
+
}
|
|
408
|
+
if (!envToken && providerName) {
|
|
409
|
+
const cleanName = providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_");
|
|
410
|
+
envToken = process.env[`LIOP_TOKEN_${cleanName}`] || process.env[`LIOP_OAUTH_TOKEN_${cleanName}`];
|
|
411
|
+
}
|
|
412
|
+
if (envToken) {
|
|
413
|
+
log.info(
|
|
414
|
+
`[LiopClient] Resolved node-specific token for peer ${realPeerId.slice(-8)} (${providerName || "unknown"})`
|
|
415
|
+
);
|
|
416
|
+
nodeToken = envToken;
|
|
417
|
+
}
|
|
418
|
+
client = new LiopRpcClient(address, this.tlsOptions, nodeToken);
|
|
419
|
+
this.rpcClients.set(peerId, client);
|
|
420
|
+
}
|
|
421
|
+
return client;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Reads a specific resource by URI.
|
|
425
|
+
* In LIOP, resources can be static definitions or dynamic streams.
|
|
426
|
+
*/
|
|
427
|
+
async readResource(uri) {
|
|
428
|
+
if (!this.meshNode) {
|
|
429
|
+
throw new Error("Client must be connected before reading resources.");
|
|
430
|
+
}
|
|
431
|
+
log.info(`[LiopClient] Querying Mesh for Resource: ${uri}...`);
|
|
432
|
+
const providers = await this.meshNode.findProviders(uri);
|
|
433
|
+
if (providers.length === 0) {
|
|
434
|
+
throw new Error(`No mesh providers found for resource: ${uri}`);
|
|
435
|
+
}
|
|
436
|
+
const manifest = await this.meshNode.queryManifest(providers[0]);
|
|
437
|
+
if (!manifest) {
|
|
438
|
+
throw new Error("Target peer did not return a valid LIOP Manifest.");
|
|
439
|
+
}
|
|
440
|
+
const resourceDef = manifest.resources?.find((r) => r.uri === uri);
|
|
441
|
+
if (!resourceDef) {
|
|
442
|
+
throw new Error(`Resource ${uri} not listed in remote manifest.`);
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
contents: [
|
|
446
|
+
{
|
|
447
|
+
uri,
|
|
448
|
+
mimeType: resourceDef.mimeType || "application/json",
|
|
449
|
+
text: JSON.stringify(resourceDef, null, 2)
|
|
450
|
+
}
|
|
451
|
+
]
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
getServerInfo() {
|
|
455
|
+
return this.serverInfo;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Destroys the active Mesh Node resources.
|
|
459
|
+
*/
|
|
460
|
+
async close() {
|
|
461
|
+
if (this.meshNode) {
|
|
462
|
+
await this.meshNode.stop();
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
export { LiopClient, LiopRpcClient };
|
|
468
|
+
//# sourceMappingURL=chunk-E5QBDD5E.js.map
|
|
469
|
+
//# sourceMappingURL=chunk-E5QBDD5E.js.map
|