@valiron/sdk 0.11.1 → 1.0.0
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 +18 -0
- package/dist/clients.d.ts +66 -1
- package/dist/clients.js +73 -0
- package/dist/identity.d.ts +16 -0
- package/dist/identity.js +19 -0
- package/dist/index.d.ts +147 -1
- package/dist/index.js +147 -1
- package/dist/middleware.d.ts +30 -4
- package/dist/middleware.js +114 -27
- package/dist/operatorSdk.d.ts +103 -0
- package/dist/operatorSdk.js +128 -0
- package/dist/types.d.ts +159 -0
- package/docs/API-REFERENCE.md +569 -9
- package/docs/DASHBOARD.md +253 -0
- package/docs/ERRORS.md +43 -0
- package/docs/IDENTITY.md +71 -0
- package/docs/QUICKSTART.md +42 -0
- package/docs/README.md +1 -0
- package/docs/SANDBOX.md +2 -15
- package/docs/SDK-REFERENCE.md +292 -7
- package/docs/TRUST-MODEL.md +2 -27
- package/docs/llms.txt +54 -0
- package/package.json +3 -2
package/dist/middleware.js
CHANGED
|
@@ -5,35 +5,83 @@ exports.valironFastifyPlugin = valironFastifyPlugin;
|
|
|
5
5
|
exports.createValironNextMiddleware = createValironNextMiddleware;
|
|
6
6
|
exports.valironGateCheck = valironGateCheck;
|
|
7
7
|
// ─── Shared gate logic ──────────────────────────────────────────────────────
|
|
8
|
-
function
|
|
9
|
-
// Works for Express, Fastify, Koa, raw Node http (object headers)
|
|
10
|
-
// and Fetch-based frameworks like Hono (Headers with .get())
|
|
8
|
+
function extractHeader(req, name) {
|
|
11
9
|
const headers = req?.headers ?? {};
|
|
12
10
|
let value = null;
|
|
13
11
|
if (headers && typeof headers.get === "function") {
|
|
14
|
-
value = headers.get(
|
|
12
|
+
value = headers.get(name);
|
|
15
13
|
}
|
|
16
14
|
else {
|
|
17
|
-
const raw = headers[
|
|
15
|
+
const raw = headers[name];
|
|
18
16
|
value = Array.isArray(raw) ? raw[0] : raw;
|
|
19
17
|
}
|
|
20
18
|
return value ?? null;
|
|
21
19
|
}
|
|
20
|
+
function defaultExtractAgentId(req) {
|
|
21
|
+
return extractHeader(req, "x-agent-id");
|
|
22
|
+
}
|
|
23
|
+
function defaultExtractAgentAddress(req) {
|
|
24
|
+
return extractHeader(req, "x-agent-address") || extractHeader(req, "x-agent-key-hash");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Normalize a KeyAgentProfile into a GateResult so all middleware
|
|
28
|
+
* consumers get one consistent type regardless of agent kind.
|
|
29
|
+
*/
|
|
30
|
+
function keyProfileToGateResult(profile, minScore) {
|
|
31
|
+
const score = profile.score ?? 0;
|
|
32
|
+
const tier = (profile.tier ?? "C");
|
|
33
|
+
const riskLevel = (profile.riskLevel ?? "RED");
|
|
34
|
+
const route = (profile.route ?? "sandbox_only");
|
|
35
|
+
const allow = profile.verified && score >= minScore;
|
|
36
|
+
return {
|
|
37
|
+
allow,
|
|
38
|
+
score,
|
|
39
|
+
tier,
|
|
40
|
+
riskLevel,
|
|
41
|
+
route,
|
|
42
|
+
agentId: profile.agentAddress,
|
|
43
|
+
wallet: "",
|
|
44
|
+
chain: {
|
|
45
|
+
network: "web2",
|
|
46
|
+
rpcUrl: "",
|
|
47
|
+
identityRegistry: "",
|
|
48
|
+
reputationRegistry: "",
|
|
49
|
+
},
|
|
50
|
+
sandboxRan: false,
|
|
51
|
+
cached: true,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
22
54
|
async function runGateCheck(config, req) {
|
|
55
|
+
// 1. Try on-chain agent ID (ERC-8004 / Solana)
|
|
23
56
|
const extractAgentId = config.extractAgentId || defaultExtractAgentId;
|
|
24
57
|
const agentId = extractAgentId(req);
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
58
|
+
if (agentId) {
|
|
59
|
+
try {
|
|
60
|
+
const result = await config.sdk.gate(agentId, {
|
|
61
|
+
minScore: config.minScore,
|
|
62
|
+
ttlMs: config.ttlMs,
|
|
63
|
+
});
|
|
64
|
+
return { agentId, result };
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
return { agentId, error };
|
|
68
|
+
}
|
|
33
69
|
}
|
|
34
|
-
|
|
35
|
-
|
|
70
|
+
// 2. Fall back to key-based agent (web2)
|
|
71
|
+
const extractAddress = config.extractAgentAddress || defaultExtractAgentAddress;
|
|
72
|
+
const agentAddr = extractAddress(req);
|
|
73
|
+
if (agentAddr) {
|
|
74
|
+
try {
|
|
75
|
+
const profile = await config.sdk.getKeyAgentProfile(agentAddr);
|
|
76
|
+
const result = keyProfileToGateResult(profile, config.minScore ?? 65);
|
|
77
|
+
return { agentId: agentAddr, result };
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
return { agentId: agentAddr, error };
|
|
81
|
+
}
|
|
36
82
|
}
|
|
83
|
+
// 3. No agent identity found
|
|
84
|
+
return { agentId: null };
|
|
37
85
|
}
|
|
38
86
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
39
87
|
// EXPRESS / CONNECT MIDDLEWARE
|
|
@@ -53,6 +101,7 @@ async function runGateCheck(config, req) {
|
|
|
53
101
|
* const valiron = new ValironSDK({ chain: 'monad' });
|
|
54
102
|
*
|
|
55
103
|
* // Apply before your x402 / payment handler
|
|
104
|
+
* Works with both on-chain (x-agent-id) and key-based (x-agent-address) agents
|
|
56
105
|
* app.use('/api/paid', createValironGate({ sdk: valiron }));
|
|
57
106
|
*
|
|
58
107
|
* // Custom threshold
|
|
@@ -70,7 +119,11 @@ function createValironGate(config) {
|
|
|
70
119
|
return async function valironGate(req, res, next) {
|
|
71
120
|
const { agentId, result, error } = await runGateCheck(frozen, req);
|
|
72
121
|
if (!agentId) {
|
|
73
|
-
return res.status(401).json({
|
|
122
|
+
return res.status(401).json({
|
|
123
|
+
error: "Missing agent identity",
|
|
124
|
+
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
125
|
+
identitySkill: "https://valiron.co/auth.md",
|
|
126
|
+
});
|
|
74
127
|
}
|
|
75
128
|
if (error) {
|
|
76
129
|
return res.status(503).json({
|
|
@@ -123,6 +176,7 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
123
176
|
const gateConfig = {
|
|
124
177
|
sdk: opts.sdk,
|
|
125
178
|
extractAgentId: opts.extractAgentId,
|
|
179
|
+
extractAgentAddress: opts.extractAgentAddress,
|
|
126
180
|
minScore: opts.minScore,
|
|
127
181
|
ttlMs: opts.ttlMs,
|
|
128
182
|
onAllow: opts.onAllow,
|
|
@@ -144,7 +198,11 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
144
198
|
}
|
|
145
199
|
const { agentId, result, error } = await runGateCheck(gateConfig, request);
|
|
146
200
|
if (!agentId) {
|
|
147
|
-
return reply.status(401).send({
|
|
201
|
+
return reply.status(401).send({
|
|
202
|
+
error: "Missing agent identity",
|
|
203
|
+
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
204
|
+
identitySkill: "https://valiron.co/auth.md",
|
|
205
|
+
});
|
|
148
206
|
}
|
|
149
207
|
if (error) {
|
|
150
208
|
return reply.status(503).send({
|
|
@@ -197,6 +255,8 @@ function createValironNextMiddleware(config) {
|
|
|
197
255
|
const { sdk, minScore, ttlMs, paths } = config;
|
|
198
256
|
const extractAgentId = config.extractAgentId ||
|
|
199
257
|
((req) => req.headers.get("x-agent-id"));
|
|
258
|
+
const extractAddress = config.extractAgentAddress ||
|
|
259
|
+
((req) => req.headers.get("x-agent-address") || req.headers.get("x-agent-key-hash"));
|
|
200
260
|
return async function valironNextGate(request) {
|
|
201
261
|
// Skip paths not in scope
|
|
202
262
|
if (paths && paths.length > 0) {
|
|
@@ -205,12 +265,25 @@ function createValironNextMiddleware(config) {
|
|
|
205
265
|
return undefined; // pass through
|
|
206
266
|
}
|
|
207
267
|
}
|
|
268
|
+
// Try on-chain agent ID first, then key-based address
|
|
208
269
|
const agentId = extractAgentId(request);
|
|
209
|
-
|
|
210
|
-
|
|
270
|
+
const agentAddr = agentId ? null : extractAddress(request);
|
|
271
|
+
if (!agentId && !agentAddr) {
|
|
272
|
+
return Response.json({
|
|
273
|
+
error: "Missing agent identity",
|
|
274
|
+
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
275
|
+
identitySkill: "https://valiron.co/auth.md",
|
|
276
|
+
}, { status: 401 });
|
|
211
277
|
}
|
|
212
278
|
try {
|
|
213
|
-
|
|
279
|
+
let result;
|
|
280
|
+
if (agentId) {
|
|
281
|
+
result = await sdk.gate(agentId, { minScore, ttlMs });
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
const profile = await sdk.getKeyAgentProfile(agentAddr);
|
|
285
|
+
result = keyProfileToGateResult(profile, minScore ?? 65);
|
|
286
|
+
}
|
|
214
287
|
if (result.allow) {
|
|
215
288
|
return undefined; // continue to route handler
|
|
216
289
|
}
|
|
@@ -237,6 +310,7 @@ function createValironNextMiddleware(config) {
|
|
|
237
310
|
* app.use('/api/*', async (c, next) => {
|
|
238
311
|
* const gate = await valironGateCheck(valiron, {
|
|
239
312
|
* agentId: c.req.header('x-agent-id') ?? null,
|
|
313
|
+
* agentAddress: c.req.header('x-agent-address') ?? null,
|
|
240
314
|
* });
|
|
241
315
|
* if (!gate.allow) return c.json(gate.body, gate.status);
|
|
242
316
|
* c.set('valiron', gate.result);
|
|
@@ -250,6 +324,7 @@ function createValironNextMiddleware(config) {
|
|
|
250
324
|
* app.use(async (ctx, next) => {
|
|
251
325
|
* const gate = await valironGateCheck(valiron, {
|
|
252
326
|
* agentId: ctx.get('x-agent-id') || null,
|
|
327
|
+
* agentAddress: ctx.get('x-agent-address') || null,
|
|
253
328
|
* });
|
|
254
329
|
* if (!gate.allow) {
|
|
255
330
|
* ctx.status = gate.status;
|
|
@@ -262,24 +337,36 @@ function createValironNextMiddleware(config) {
|
|
|
262
337
|
* ```
|
|
263
338
|
*/
|
|
264
339
|
async function valironGateCheck(sdk, opts) {
|
|
265
|
-
const { agentId, minScore, ttlMs } = opts;
|
|
266
|
-
|
|
340
|
+
const { agentId, agentAddress, minScore, ttlMs } = opts;
|
|
341
|
+
const identifier = agentId || agentAddress || null;
|
|
342
|
+
if (!identifier) {
|
|
267
343
|
return {
|
|
268
344
|
hasAgent: false,
|
|
269
345
|
agentId: null,
|
|
270
346
|
ok: false,
|
|
271
347
|
status: 401,
|
|
272
|
-
body: {
|
|
348
|
+
body: {
|
|
349
|
+
error: "Missing agent identity",
|
|
350
|
+
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
351
|
+
identitySkill: "https://valiron.co/auth.md",
|
|
352
|
+
},
|
|
273
353
|
};
|
|
274
354
|
}
|
|
275
355
|
try {
|
|
276
|
-
|
|
356
|
+
let result;
|
|
357
|
+
if (agentId) {
|
|
358
|
+
result = await sdk.gate(agentId, { minScore, ttlMs });
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
const profile = await sdk.getKeyAgentProfile(agentAddress);
|
|
362
|
+
result = keyProfileToGateResult(profile, minScore ?? 65);
|
|
363
|
+
}
|
|
277
364
|
if (result.allow) {
|
|
278
|
-
return { hasAgent: true, agentId, ok: true, allow: true, result, status: 200 };
|
|
365
|
+
return { hasAgent: true, agentId: identifier, ok: true, allow: true, result, status: 200 };
|
|
279
366
|
}
|
|
280
367
|
return {
|
|
281
368
|
hasAgent: true,
|
|
282
|
-
agentId,
|
|
369
|
+
agentId: identifier,
|
|
283
370
|
ok: true,
|
|
284
371
|
allow: false,
|
|
285
372
|
result,
|
|
@@ -290,7 +377,7 @@ async function valironGateCheck(sdk, opts) {
|
|
|
290
377
|
catch {
|
|
291
378
|
return {
|
|
292
379
|
hasAgent: true,
|
|
293
|
-
agentId,
|
|
380
|
+
agentId: identifier,
|
|
294
381
|
ok: false,
|
|
295
382
|
status: 503,
|
|
296
383
|
body: { error: "Trust gate unavailable \u2014 request denied (fail-closed)" },
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ValironSDK } from "./index.js";
|
|
2
|
+
import type { SupportedChain } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the Valiron Operator SDK.
|
|
5
|
+
*/
|
|
6
|
+
export interface ValironOperatorConfig {
|
|
7
|
+
/** Operator API key (val_op_xxxx) — obtained from valiron.co/dashboard. */
|
|
8
|
+
apiKey: string;
|
|
9
|
+
/** Default blockchain (default: 'ethereum'). */
|
|
10
|
+
chain?: SupportedChain;
|
|
11
|
+
/** Edge proxy endpoint (default: production). */
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
/** Request timeout in ms (default: 5000). */
|
|
14
|
+
timeout?: number;
|
|
15
|
+
/** Enable debug logging (default: false). */
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Configuration for the paywall middleware.
|
|
20
|
+
*/
|
|
21
|
+
export interface PaywallConfig {
|
|
22
|
+
/** Price per API call in USDC. */
|
|
23
|
+
pricePerCall: number;
|
|
24
|
+
/** Minimum trust score to allow (default: 45). */
|
|
25
|
+
minTrustScore?: number;
|
|
26
|
+
/** TTL for cached trust results in ms (default: 86400000 = 24h). */
|
|
27
|
+
ttlMs?: number;
|
|
28
|
+
/** Custom handler when an agent is denied. */
|
|
29
|
+
onDeny?: (req: any, res: any, result: any) => void;
|
|
30
|
+
/** Hook called after an agent is allowed. */
|
|
31
|
+
onAllow?: (req: any, result: any) => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Valiron Operator SDK — for API sellers who want to monetize their endpoints.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import { ValironOperator } from '@valiron/sdk';
|
|
39
|
+
*
|
|
40
|
+
* const operator = new ValironOperator({
|
|
41
|
+
* apiKey: 'val_op_xxxx',
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // Express: one line to monetize an endpoint
|
|
45
|
+
* app.use('/api/inference', operator.paywall({ pricePerCall: 0.05 }));
|
|
46
|
+
*
|
|
47
|
+
* // Fastify: register as plugin
|
|
48
|
+
* app.register(operator.fastifyPaywall({
|
|
49
|
+
* pricePerCall: 0.10,
|
|
50
|
+
* prefix: '/api',
|
|
51
|
+
* }));
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare class ValironOperator {
|
|
55
|
+
private sdk;
|
|
56
|
+
private apiKey;
|
|
57
|
+
private debug;
|
|
58
|
+
constructor(config: ValironOperatorConfig);
|
|
59
|
+
/**
|
|
60
|
+
* Create Express/Connect middleware that gates access + tracks usage.
|
|
61
|
+
*
|
|
62
|
+
* Extracts `X-Agent-Id` from request, runs trust gate, and attaches
|
|
63
|
+
* `req.valiron` with the gate result + pricing info.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* app.use('/api/inference', operator.paywall({
|
|
68
|
+
* pricePerCall: 0.05,
|
|
69
|
+
* minTrustScore: 45,
|
|
70
|
+
* }));
|
|
71
|
+
*
|
|
72
|
+
* app.post('/api/inference', (req, res) => {
|
|
73
|
+
* // req.valiron.allow === true (guaranteed by paywall)
|
|
74
|
+
* // req.valiron.pricePerCall === 0.05
|
|
75
|
+
* res.json({ result: 'inference output' });
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
paywall(config: PaywallConfig): (req: any, res: any, next: any) => Promise<any>;
|
|
80
|
+
/**
|
|
81
|
+
* Create a Fastify plugin that gates access + tracks usage.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* app.register(operator.fastifyPaywall({
|
|
86
|
+
* pricePerCall: 0.10,
|
|
87
|
+
* prefix: '/api',
|
|
88
|
+
* minTrustScore: 65,
|
|
89
|
+
* }));
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
fastifyPaywall(config: PaywallConfig & {
|
|
93
|
+
prefix?: string;
|
|
94
|
+
}): (fastify: any, opts: any, done: any) => void;
|
|
95
|
+
/**
|
|
96
|
+
* Get the underlying ValironSDK instance for advanced usage.
|
|
97
|
+
*/
|
|
98
|
+
getSDK(): ValironSDK;
|
|
99
|
+
/**
|
|
100
|
+
* Flush telemetry and release resources.
|
|
101
|
+
*/
|
|
102
|
+
dispose(): Promise<void>;
|
|
103
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValironOperator = void 0;
|
|
4
|
+
const index_js_1 = require("./index.js");
|
|
5
|
+
const middleware_js_1 = require("./middleware.js");
|
|
6
|
+
/**
|
|
7
|
+
* Valiron Operator SDK — for API sellers who want to monetize their endpoints.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { ValironOperator } from '@valiron/sdk';
|
|
12
|
+
*
|
|
13
|
+
* const operator = new ValironOperator({
|
|
14
|
+
* apiKey: 'val_op_xxxx',
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Express: one line to monetize an endpoint
|
|
18
|
+
* app.use('/api/inference', operator.paywall({ pricePerCall: 0.05 }));
|
|
19
|
+
*
|
|
20
|
+
* // Fastify: register as plugin
|
|
21
|
+
* app.register(operator.fastifyPaywall({
|
|
22
|
+
* pricePerCall: 0.10,
|
|
23
|
+
* prefix: '/api',
|
|
24
|
+
* }));
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class ValironOperator {
|
|
28
|
+
constructor(config) {
|
|
29
|
+
if (!config.apiKey?.startsWith("val_op_")) {
|
|
30
|
+
throw new Error("Invalid operator API key — must start with 'val_op_'");
|
|
31
|
+
}
|
|
32
|
+
this.apiKey = config.apiKey;
|
|
33
|
+
this.debug = config.debug || false;
|
|
34
|
+
this.sdk = new index_js_1.ValironSDK({
|
|
35
|
+
apiKey: config.apiKey,
|
|
36
|
+
chain: config.chain,
|
|
37
|
+
endpoint: config.endpoint,
|
|
38
|
+
timeout: config.timeout,
|
|
39
|
+
debug: config.debug,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create Express/Connect middleware that gates access + tracks usage.
|
|
44
|
+
*
|
|
45
|
+
* Extracts `X-Agent-Id` from request, runs trust gate, and attaches
|
|
46
|
+
* `req.valiron` with the gate result + pricing info.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* app.use('/api/inference', operator.paywall({
|
|
51
|
+
* pricePerCall: 0.05,
|
|
52
|
+
* minTrustScore: 45,
|
|
53
|
+
* }));
|
|
54
|
+
*
|
|
55
|
+
* app.post('/api/inference', (req, res) => {
|
|
56
|
+
* // req.valiron.allow === true (guaranteed by paywall)
|
|
57
|
+
* // req.valiron.pricePerCall === 0.05
|
|
58
|
+
* res.json({ result: 'inference output' });
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
paywall(config) {
|
|
63
|
+
const gateConfig = {
|
|
64
|
+
sdk: this.sdk,
|
|
65
|
+
minScore: config.minTrustScore ?? 45,
|
|
66
|
+
ttlMs: config.ttlMs,
|
|
67
|
+
onDeny: config.onDeny,
|
|
68
|
+
onAllow: (req, result) => {
|
|
69
|
+
// Enrich the gate result with pricing
|
|
70
|
+
result.pricePerCall = config.pricePerCall;
|
|
71
|
+
result.operatorApiKey = this.apiKey.slice(0, 15) + "...";
|
|
72
|
+
if (config.onAllow)
|
|
73
|
+
config.onAllow(req, result);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
const middleware = (0, middleware_js_1.createValironGate)(gateConfig);
|
|
77
|
+
if (this.debug) {
|
|
78
|
+
return async (req, res, next) => {
|
|
79
|
+
console.log(`[ValironOperator] Gate check for ${req.headers?.["x-agent-id"] || "unknown"} @ $${config.pricePerCall}/call`);
|
|
80
|
+
return middleware(req, res, next);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return middleware;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a Fastify plugin that gates access + tracks usage.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* app.register(operator.fastifyPaywall({
|
|
91
|
+
* pricePerCall: 0.10,
|
|
92
|
+
* prefix: '/api',
|
|
93
|
+
* minTrustScore: 65,
|
|
94
|
+
* }));
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
fastifyPaywall(config) {
|
|
98
|
+
const pluginConfig = {
|
|
99
|
+
sdk: this.sdk,
|
|
100
|
+
minScore: config.minTrustScore ?? 45,
|
|
101
|
+
ttlMs: config.ttlMs,
|
|
102
|
+
prefix: config.prefix,
|
|
103
|
+
onDeny: config.onDeny,
|
|
104
|
+
onAllow: (req, result) => {
|
|
105
|
+
result.pricePerCall = config.pricePerCall;
|
|
106
|
+
result.operatorApiKey = this.apiKey.slice(0, 15) + "...";
|
|
107
|
+
if (config.onAllow)
|
|
108
|
+
config.onAllow(req, result);
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
return (fastify, opts, done) => {
|
|
112
|
+
(0, middleware_js_1.valironFastifyPlugin)(fastify, { ...pluginConfig, ...opts }).then(() => done()).catch(done);
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get the underlying ValironSDK instance for advanced usage.
|
|
117
|
+
*/
|
|
118
|
+
getSDK() {
|
|
119
|
+
return this.sdk;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Flush telemetry and release resources.
|
|
123
|
+
*/
|
|
124
|
+
dispose() {
|
|
125
|
+
return this.sdk.dispose();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.ValironOperator = ValironOperator;
|
package/dist/types.d.ts
CHANGED
|
@@ -74,6 +74,10 @@ export interface RoutingSignals {
|
|
|
74
74
|
tier: string;
|
|
75
75
|
riskLevel: string;
|
|
76
76
|
};
|
|
77
|
+
worldId?: {
|
|
78
|
+
verified: boolean;
|
|
79
|
+
verificationLevel: string | null;
|
|
80
|
+
};
|
|
77
81
|
}
|
|
78
82
|
export interface RoutingExplanation {
|
|
79
83
|
finalRoute: RouteDecision;
|
|
@@ -99,6 +103,8 @@ export interface AgentProfile {
|
|
|
99
103
|
localReputation: LocalReputation | null;
|
|
100
104
|
routing: RoutingExplanation;
|
|
101
105
|
chain: ChainContext;
|
|
106
|
+
/** World ID proof-of-personhood verification status */
|
|
107
|
+
worldId?: WorldIdStatus;
|
|
102
108
|
timestamp: string;
|
|
103
109
|
}
|
|
104
110
|
/**
|
|
@@ -196,6 +202,159 @@ export interface GateResult {
|
|
|
196
202
|
/** Whether the score is from a micro-sandbox (fast, limited coverage). When true, a full deep test is running in the background and will update the cached score. */
|
|
197
203
|
provisional?: boolean;
|
|
198
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Options for the proxy gateway request.
|
|
207
|
+
*
|
|
208
|
+
* `POST /proxy/:agentId`
|
|
209
|
+
*/
|
|
210
|
+
export interface ProxyRequest {
|
|
211
|
+
/** The real API endpoint URL to forward the request to. */
|
|
212
|
+
target: string;
|
|
213
|
+
/** HTTP method to use (default: GET). */
|
|
214
|
+
method?: string;
|
|
215
|
+
/** Headers to forward to the target API. */
|
|
216
|
+
headers?: Record<string, string>;
|
|
217
|
+
/** Request body to forward (for POST, PUT, PATCH). */
|
|
218
|
+
body?: unknown;
|
|
219
|
+
/** Minimum trust score to allow (default: 65). */
|
|
220
|
+
minScore?: number;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Response from the proxy gateway.
|
|
224
|
+
*
|
|
225
|
+
* On success, contains the real API response wrapped with Valiron metadata.
|
|
226
|
+
* On deny (score below threshold), the SDK will throw — callers should
|
|
227
|
+
* catch the error and inspect the response body for gate details.
|
|
228
|
+
*/
|
|
229
|
+
export interface ProxyResponse<T = unknown> {
|
|
230
|
+
/** Valiron gate evaluation result. */
|
|
231
|
+
_valiron: {
|
|
232
|
+
allow: boolean;
|
|
233
|
+
score: number;
|
|
234
|
+
tier: MoodysRating;
|
|
235
|
+
riskLevel: RiskLevel;
|
|
236
|
+
agentId: string;
|
|
237
|
+
wallet: string;
|
|
238
|
+
sandboxRan: boolean;
|
|
239
|
+
provisional: boolean;
|
|
240
|
+
cached: boolean;
|
|
241
|
+
};
|
|
242
|
+
/** HTTP status code from the real API. */
|
|
243
|
+
_apiStatus: number;
|
|
244
|
+
/** The real API response body. */
|
|
245
|
+
response: T;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Response from `POST /operator/key/:agentAddress/challenge`
|
|
249
|
+
*/
|
|
250
|
+
export interface KeyAgentChallenge {
|
|
251
|
+
/** Random nonce the agent must sign */
|
|
252
|
+
challenge: string;
|
|
253
|
+
/** ISO 8601 expiry timestamp for the challenge */
|
|
254
|
+
expiresAt: string;
|
|
255
|
+
/** The agent's Ethereum address (keccak256-derived) */
|
|
256
|
+
agentAddress: string;
|
|
257
|
+
}
|
|
258
|
+
/** Verification level from World ID */
|
|
259
|
+
export type WorldIdVerificationLevel = "orb" | "device" | "phone";
|
|
260
|
+
/**
|
|
261
|
+
* World ID verification status for an agent.
|
|
262
|
+
*
|
|
263
|
+
* Returned from `GET /operator/world-id/status/:agentId`
|
|
264
|
+
*/
|
|
265
|
+
export interface WorldIdStatus {
|
|
266
|
+
/** Whether the agent has a verified World ID human link */
|
|
267
|
+
verified: boolean;
|
|
268
|
+
/** Verification level (orb = biometric, device = passkey, phone = SMS) */
|
|
269
|
+
verificationLevel?: WorldIdVerificationLevel;
|
|
270
|
+
/** Nullifier hash — unique per (user, action, app) tuple */
|
|
271
|
+
nullifierHash?: string;
|
|
272
|
+
/** The agent ID this verification is linked to */
|
|
273
|
+
agentId?: string;
|
|
274
|
+
/** Network this verification is scoped to */
|
|
275
|
+
network?: string;
|
|
276
|
+
/** ISO 8601 timestamp of when verification occurred */
|
|
277
|
+
verifiedAt?: string;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Enriched human-agent trust profile combining World ID + Icebreaker.
|
|
281
|
+
*
|
|
282
|
+
* Response from `GET /operator/world-id/profile/:agentId`
|
|
283
|
+
*/
|
|
284
|
+
export interface WorldIdAgentProfile {
|
|
285
|
+
agentId: string;
|
|
286
|
+
/** World ID proof-of-personhood status */
|
|
287
|
+
worldId: WorldIdStatus;
|
|
288
|
+
/** Icebreaker background enrichment (LinkedIn, GitHub, etc.) */
|
|
289
|
+
icebreaker: {
|
|
290
|
+
attested: boolean;
|
|
291
|
+
owner?: string;
|
|
292
|
+
bio?: string;
|
|
293
|
+
verifiedHandles?: {
|
|
294
|
+
source: string;
|
|
295
|
+
handle: string;
|
|
296
|
+
}[];
|
|
297
|
+
organization?: string;
|
|
298
|
+
profileUrl?: string;
|
|
299
|
+
};
|
|
300
|
+
/** On-chain identity summary */
|
|
301
|
+
identity: {
|
|
302
|
+
wallet: string;
|
|
303
|
+
name: string;
|
|
304
|
+
} | null;
|
|
305
|
+
/** Aggregated human trust signals */
|
|
306
|
+
humanTrust: {
|
|
307
|
+
hasHumanLink: boolean;
|
|
308
|
+
hasBackground: boolean;
|
|
309
|
+
verificationLevel: WorldIdVerificationLevel | null;
|
|
310
|
+
};
|
|
311
|
+
reasons: string[];
|
|
312
|
+
timestamp: string;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Icebreaker ownership attestation linked to an agent key hash.
|
|
316
|
+
*/
|
|
317
|
+
export interface IcebreakerOwnership {
|
|
318
|
+
/** Whether an Icebreaker attestation was found */
|
|
319
|
+
attested: boolean;
|
|
320
|
+
/** The human owner's name or address */
|
|
321
|
+
owner?: string;
|
|
322
|
+
/** Human-readable bio or description */
|
|
323
|
+
description?: string;
|
|
324
|
+
/** Verified social handles (e.g. "github: alice", "x: @alice") */
|
|
325
|
+
verifiedHandles?: string[];
|
|
326
|
+
/** Workplace or organization affiliation, if verified */
|
|
327
|
+
organization?: string;
|
|
328
|
+
/** EAS attestation UID on Base */
|
|
329
|
+
attestationUid?: string;
|
|
330
|
+
/** Icebreaker profile URL for the agent's linked address */
|
|
331
|
+
profileUrl?: string;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Trust profile for a key-based (non-ERC-8004) agent.
|
|
335
|
+
*
|
|
336
|
+
* Response from `POST /operator/key/verify` and `GET /operator/key/:agentAddress`
|
|
337
|
+
*/
|
|
338
|
+
export interface KeyAgentProfile {
|
|
339
|
+
/** Ethereum address derived via keccak256 of the agent's secp256k1 public key */
|
|
340
|
+
agentAddress: string;
|
|
341
|
+
/** Whether identity has been verified via challenge-response */
|
|
342
|
+
verified: boolean;
|
|
343
|
+
/** Behavioral score (0-110) — same scoring as ERC-8004 agents */
|
|
344
|
+
score: number | null;
|
|
345
|
+
/** Moody's-style tier (AAA → C) */
|
|
346
|
+
tier: MoodysRating | null;
|
|
347
|
+
/** Risk level derived from tier */
|
|
348
|
+
riskLevel: RiskLevel | null;
|
|
349
|
+
/** Route decision */
|
|
350
|
+
route: RouteDecision | null;
|
|
351
|
+
/** Icebreaker ownership context (optional enrichment) */
|
|
352
|
+
icebreaker: IcebreakerOwnership | null;
|
|
353
|
+
/** Human-readable trust explanation */
|
|
354
|
+
reasons: string[];
|
|
355
|
+
/** ISO 8601 timestamp */
|
|
356
|
+
timestamp: string;
|
|
357
|
+
}
|
|
199
358
|
/** Options that can be passed to individual SDK method calls. */
|
|
200
359
|
export interface CallOptions {
|
|
201
360
|
/**
|