@valiron/sdk 0.11.0 → 0.12.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 +26 -61
- package/dist/clients.d.ts +113 -1
- package/dist/clients.js +228 -0
- package/dist/identity.d.ts +80 -0
- package/dist/identity.js +121 -0
- package/dist/index.d.ts +218 -1
- package/dist/index.js +229 -1
- package/dist/middleware.d.ts +30 -4
- package/dist/middleware.js +98 -27
- package/dist/operatorSdk.d.ts +103 -0
- package/dist/operatorSdk.js +128 -0
- package/dist/types.d.ts +200 -1
- package/docs/API-REFERENCE.md +375 -3
- package/docs/CHAINS.md +37 -3
- package/docs/ERRORS.md +40 -2
- package/docs/IDENTITY.md +94 -8
- package/docs/QUICKSTART.md +185 -28
- package/docs/README.md +21 -11
- package/docs/SANDBOX.md +58 -23
- package/docs/SDK-REFERENCE.md +275 -1
- package/docs/TRUST-MODEL.md +57 -60
- package/docs/llms.txt +52 -4
- package/package.json +5 -4
package/dist/middleware.d.ts
CHANGED
|
@@ -2,15 +2,25 @@ import type { ValironSDK } from "./index";
|
|
|
2
2
|
import type { GateResult } from "./types";
|
|
3
3
|
/**
|
|
4
4
|
* Configuration for the Valiron trust gate middleware.
|
|
5
|
+
*
|
|
6
|
+
* Supports both on-chain (ERC-8004/Solana) and key-based (web2) agents.
|
|
7
|
+
* Auto-detects agent type from request headers:
|
|
8
|
+
* - `x-agent-id` → on-chain gate
|
|
9
|
+
* - `x-agent-key-hash` → key-based profile lookup
|
|
5
10
|
*/
|
|
6
11
|
export interface ValironGateConfig {
|
|
7
12
|
/** Configured ValironSDK instance. */
|
|
8
13
|
sdk: ValironSDK;
|
|
9
14
|
/**
|
|
10
|
-
* Extract the agent ID from an incoming request.
|
|
15
|
+
* Extract the on-chain agent ID from an incoming request.
|
|
11
16
|
* Defaults to reading `x-agent-id` header.
|
|
12
17
|
*/
|
|
13
18
|
extractAgentId?: (req: any) => string | null;
|
|
19
|
+
/**
|
|
20
|
+
* Extract the key-based agent hash from an incoming request.
|
|
21
|
+
* Defaults to reading `x-agent-key-hash` header.
|
|
22
|
+
*/
|
|
23
|
+
extractAgentKeyHash?: (req: any) => string | null;
|
|
14
24
|
/** Minimum Valiron score to allow (default: 65). */
|
|
15
25
|
minScore?: number;
|
|
16
26
|
/** TTL for cached sandbox results in ms (default: 86400000 = 24 h). */
|
|
@@ -35,6 +45,7 @@ export interface ValironGateConfig {
|
|
|
35
45
|
* const valiron = new ValironSDK({ chain: 'monad' });
|
|
36
46
|
*
|
|
37
47
|
* // Apply before your x402 / payment handler
|
|
48
|
+
* // Works with both on-chain (x-agent-id) and key-based (x-agent-key-hash) agents
|
|
38
49
|
* app.use('/api/paid', createValironGate({ sdk: valiron }));
|
|
39
50
|
*
|
|
40
51
|
* // Custom threshold
|
|
@@ -53,10 +64,15 @@ export interface ValironFastifyConfig {
|
|
|
53
64
|
/** Configured ValironSDK instance. */
|
|
54
65
|
sdk: ValironSDK;
|
|
55
66
|
/**
|
|
56
|
-
* Extract the agent ID from a Fastify request.
|
|
67
|
+
* Extract the on-chain agent ID from a Fastify request.
|
|
57
68
|
* Defaults to reading `x-agent-id` header.
|
|
58
69
|
*/
|
|
59
70
|
extractAgentId?: (req: any) => string | null;
|
|
71
|
+
/**
|
|
72
|
+
* Extract the key-based agent hash from a Fastify request.
|
|
73
|
+
* Defaults to reading `x-agent-key-hash` header.
|
|
74
|
+
*/
|
|
75
|
+
extractAgentKeyHash?: (req: any) => string | null;
|
|
60
76
|
/** Minimum Valiron score to allow (default: 65). */
|
|
61
77
|
minScore?: number;
|
|
62
78
|
/** TTL for cached sandbox results in ms (default: 86400000 = 24 h). */
|
|
@@ -106,10 +122,15 @@ export interface ValironNextConfig {
|
|
|
106
122
|
/** Configured ValironSDK instance. */
|
|
107
123
|
sdk: ValironSDK;
|
|
108
124
|
/**
|
|
109
|
-
* Extract the agent ID from a Next.js request.
|
|
125
|
+
* Extract the on-chain agent ID from a Next.js request.
|
|
110
126
|
* Defaults to reading `x-agent-id` header.
|
|
111
127
|
*/
|
|
112
128
|
extractAgentId?: (req: Request) => string | null;
|
|
129
|
+
/**
|
|
130
|
+
* Extract the key-based agent hash from a Next.js request.
|
|
131
|
+
* Defaults to reading `x-agent-key-hash` header.
|
|
132
|
+
*/
|
|
133
|
+
extractAgentKeyHash?: (req: Request) => string | null;
|
|
113
134
|
/** Minimum Valiron score to allow (default: 65). */
|
|
114
135
|
minScore?: number;
|
|
115
136
|
/** TTL for cached sandbox results in ms (default: 86400000 = 24 h). */
|
|
@@ -185,6 +206,7 @@ export interface ValironGateCheckResult {
|
|
|
185
206
|
* app.use('/api/*', async (c, next) => {
|
|
186
207
|
* const gate = await valironGateCheck(valiron, {
|
|
187
208
|
* agentId: c.req.header('x-agent-id') ?? null,
|
|
209
|
+
* agentKeyHash: c.req.header('x-agent-key-hash') ?? null,
|
|
188
210
|
* });
|
|
189
211
|
* if (!gate.allow) return c.json(gate.body, gate.status);
|
|
190
212
|
* c.set('valiron', gate.result);
|
|
@@ -198,6 +220,7 @@ export interface ValironGateCheckResult {
|
|
|
198
220
|
* app.use(async (ctx, next) => {
|
|
199
221
|
* const gate = await valironGateCheck(valiron, {
|
|
200
222
|
* agentId: ctx.get('x-agent-id') || null,
|
|
223
|
+
* agentKeyHash: ctx.get('x-agent-key-hash') || null,
|
|
201
224
|
* });
|
|
202
225
|
* if (!gate.allow) {
|
|
203
226
|
* ctx.status = gate.status;
|
|
@@ -210,7 +233,10 @@ export interface ValironGateCheckResult {
|
|
|
210
233
|
* ```
|
|
211
234
|
*/
|
|
212
235
|
export declare function valironGateCheck(sdk: ValironSDK, opts: {
|
|
213
|
-
|
|
236
|
+
/** On-chain agent ID (ERC-8004 / Solana). */
|
|
237
|
+
agentId?: string | null;
|
|
238
|
+
/** Key-based agent hash (web2). Checked if agentId is not provided. */
|
|
239
|
+
agentKeyHash?: string | null;
|
|
214
240
|
minScore?: number;
|
|
215
241
|
ttlMs?: number;
|
|
216
242
|
}): Promise<ValironGateCheckResult>;
|
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 defaultExtractAgentKeyHash(req) {
|
|
24
|
+
return 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.agentKeyHash,
|
|
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 extractKeyHash = config.extractAgentKeyHash || defaultExtractAgentKeyHash;
|
|
72
|
+
const keyHash = extractKeyHash(req);
|
|
73
|
+
if (keyHash) {
|
|
74
|
+
try {
|
|
75
|
+
const profile = await config.sdk.getKeyAgentProfile(keyHash);
|
|
76
|
+
const result = keyProfileToGateResult(profile, config.minScore ?? 65);
|
|
77
|
+
return { agentId: keyHash, result };
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
return { agentId: keyHash, 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-key-hash) agents
|
|
56
105
|
* app.use('/api/paid', createValironGate({ sdk: valiron }));
|
|
57
106
|
*
|
|
58
107
|
* // Custom threshold
|
|
@@ -70,7 +119,7 @@ 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({ error: "Missing agent id" });
|
|
122
|
+
return res.status(401).json({ error: "Missing agent identity (provide x-agent-id or x-agent-key-hash header)" });
|
|
74
123
|
}
|
|
75
124
|
if (error) {
|
|
76
125
|
return res.status(503).json({
|
|
@@ -123,6 +172,7 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
123
172
|
const gateConfig = {
|
|
124
173
|
sdk: opts.sdk,
|
|
125
174
|
extractAgentId: opts.extractAgentId,
|
|
175
|
+
extractAgentKeyHash: opts.extractAgentKeyHash,
|
|
126
176
|
minScore: opts.minScore,
|
|
127
177
|
ttlMs: opts.ttlMs,
|
|
128
178
|
onAllow: opts.onAllow,
|
|
@@ -144,7 +194,7 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
144
194
|
}
|
|
145
195
|
const { agentId, result, error } = await runGateCheck(gateConfig, request);
|
|
146
196
|
if (!agentId) {
|
|
147
|
-
return reply.status(401).send({ error: "Missing agent id" });
|
|
197
|
+
return reply.status(401).send({ error: "Missing agent identity (provide x-agent-id or x-agent-key-hash header)" });
|
|
148
198
|
}
|
|
149
199
|
if (error) {
|
|
150
200
|
return reply.status(503).send({
|
|
@@ -197,6 +247,8 @@ function createValironNextMiddleware(config) {
|
|
|
197
247
|
const { sdk, minScore, ttlMs, paths } = config;
|
|
198
248
|
const extractAgentId = config.extractAgentId ||
|
|
199
249
|
((req) => req.headers.get("x-agent-id"));
|
|
250
|
+
const extractKeyHash = config.extractAgentKeyHash ||
|
|
251
|
+
((req) => req.headers.get("x-agent-key-hash"));
|
|
200
252
|
return async function valironNextGate(request) {
|
|
201
253
|
// Skip paths not in scope
|
|
202
254
|
if (paths && paths.length > 0) {
|
|
@@ -205,12 +257,21 @@ function createValironNextMiddleware(config) {
|
|
|
205
257
|
return undefined; // pass through
|
|
206
258
|
}
|
|
207
259
|
}
|
|
260
|
+
// Try on-chain agent ID first, then key-based hash
|
|
208
261
|
const agentId = extractAgentId(request);
|
|
209
|
-
|
|
210
|
-
|
|
262
|
+
const keyHash = agentId ? null : extractKeyHash(request);
|
|
263
|
+
if (!agentId && !keyHash) {
|
|
264
|
+
return Response.json({ error: "Missing agent identity (provide x-agent-id or x-agent-key-hash header)" }, { status: 401 });
|
|
211
265
|
}
|
|
212
266
|
try {
|
|
213
|
-
|
|
267
|
+
let result;
|
|
268
|
+
if (agentId) {
|
|
269
|
+
result = await sdk.gate(agentId, { minScore, ttlMs });
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
const profile = await sdk.getKeyAgentProfile(keyHash);
|
|
273
|
+
result = keyProfileToGateResult(profile, minScore ?? 65);
|
|
274
|
+
}
|
|
214
275
|
if (result.allow) {
|
|
215
276
|
return undefined; // continue to route handler
|
|
216
277
|
}
|
|
@@ -237,6 +298,7 @@ function createValironNextMiddleware(config) {
|
|
|
237
298
|
* app.use('/api/*', async (c, next) => {
|
|
238
299
|
* const gate = await valironGateCheck(valiron, {
|
|
239
300
|
* agentId: c.req.header('x-agent-id') ?? null,
|
|
301
|
+
* agentKeyHash: c.req.header('x-agent-key-hash') ?? null,
|
|
240
302
|
* });
|
|
241
303
|
* if (!gate.allow) return c.json(gate.body, gate.status);
|
|
242
304
|
* c.set('valiron', gate.result);
|
|
@@ -250,6 +312,7 @@ function createValironNextMiddleware(config) {
|
|
|
250
312
|
* app.use(async (ctx, next) => {
|
|
251
313
|
* const gate = await valironGateCheck(valiron, {
|
|
252
314
|
* agentId: ctx.get('x-agent-id') || null,
|
|
315
|
+
* agentKeyHash: ctx.get('x-agent-key-hash') || null,
|
|
253
316
|
* });
|
|
254
317
|
* if (!gate.allow) {
|
|
255
318
|
* ctx.status = gate.status;
|
|
@@ -262,24 +325,32 @@ function createValironNextMiddleware(config) {
|
|
|
262
325
|
* ```
|
|
263
326
|
*/
|
|
264
327
|
async function valironGateCheck(sdk, opts) {
|
|
265
|
-
const { agentId, minScore, ttlMs } = opts;
|
|
266
|
-
|
|
328
|
+
const { agentId, agentKeyHash, minScore, ttlMs } = opts;
|
|
329
|
+
const identifier = agentId || agentKeyHash || null;
|
|
330
|
+
if (!identifier) {
|
|
267
331
|
return {
|
|
268
332
|
hasAgent: false,
|
|
269
333
|
agentId: null,
|
|
270
334
|
ok: false,
|
|
271
335
|
status: 401,
|
|
272
|
-
body: { error: "Missing agent
|
|
336
|
+
body: { error: "Missing agent identity" },
|
|
273
337
|
};
|
|
274
338
|
}
|
|
275
339
|
try {
|
|
276
|
-
|
|
340
|
+
let result;
|
|
341
|
+
if (agentId) {
|
|
342
|
+
result = await sdk.gate(agentId, { minScore, ttlMs });
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
const profile = await sdk.getKeyAgentProfile(agentKeyHash);
|
|
346
|
+
result = keyProfileToGateResult(profile, minScore ?? 65);
|
|
347
|
+
}
|
|
277
348
|
if (result.allow) {
|
|
278
|
-
return { hasAgent: true, agentId, ok: true, allow: true, result, status: 200 };
|
|
349
|
+
return { hasAgent: true, agentId: identifier, ok: true, allow: true, result, status: 200 };
|
|
279
350
|
}
|
|
280
351
|
return {
|
|
281
352
|
hasAgent: true,
|
|
282
|
-
agentId,
|
|
353
|
+
agentId: identifier,
|
|
283
354
|
ok: true,
|
|
284
355
|
allow: false,
|
|
285
356
|
result,
|
|
@@ -290,7 +361,7 @@ async function valironGateCheck(sdk, opts) {
|
|
|
290
361
|
catch {
|
|
291
362
|
return {
|
|
292
363
|
hasAgent: true,
|
|
293
|
-
agentId,
|
|
364
|
+
agentId: identifier,
|
|
294
365
|
ok: false,
|
|
295
366
|
status: 503,
|
|
296
367
|
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;
|