@valiron/sdk 0.1.1 → 0.3.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 +177 -316
- package/dist/clients.d.ts +30 -74
- package/dist/clients.d.ts.map +1 -1
- package/dist/clients.js +75 -122
- package/dist/clients.js.map +1 -1
- package/dist/index.d.ts +68 -83
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +74 -85
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +120 -73
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -2
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @valiron/sdk
|
|
2
2
|
|
|
3
|
-
Official TypeScript SDK for **Valiron**
|
|
3
|
+
Official TypeScript SDK for **Valiron** — Trust Infrastructure for AI Agent Systems
|
|
4
4
|
|
|
5
5
|
Valiron verifies AI agent trustworthiness using on-chain reputation (ERC-8004) and behavioral analysis. This SDK provides routing decisions based on agent trust scores, enabling you to protect infrastructure while maintaining service availability for verified agents.
|
|
6
6
|
|
|
@@ -16,215 +16,199 @@ pnpm add @valiron/sdk
|
|
|
16
16
|
|
|
17
17
|
## Getting Started
|
|
18
18
|
|
|
19
|
-
1. **Initialize the SDK:**
|
|
20
|
-
|
|
21
19
|
```typescript
|
|
22
20
|
import { ValironSDK } from '@valiron/sdk';
|
|
23
21
|
|
|
24
|
-
// Simple initialization (uses default production endpoint)
|
|
25
22
|
const valiron = new ValironSDK();
|
|
26
23
|
|
|
27
|
-
//
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
});
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
> **Note:** API key authentication is not yet enforced. The `apiKey` field is
|
|
36
|
-
> reserved for future use when authentication is implemented.
|
|
37
|
-
|
|
38
|
-
2. **Check agent trust before processing requests:**
|
|
24
|
+
// Quick routing check (defaults to Ethereum)
|
|
25
|
+
const route = await valiron.checkAgent('25459');
|
|
26
|
+
if (route === 'prod') {
|
|
27
|
+
// Allow full production access
|
|
28
|
+
}
|
|
39
29
|
|
|
40
|
-
|
|
41
|
-
const
|
|
30
|
+
// Query a Monad agent — per-call override
|
|
31
|
+
const monadRoute = await valiron.checkAgent('8348', { chain: 'monad' });
|
|
42
32
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
33
|
+
// Or set the default chain at construction
|
|
34
|
+
const monadValiron = new ValironSDK({ chain: 'monad' });
|
|
35
|
+
const profile = await monadValiron.getAgentProfile('8348');
|
|
36
|
+
console.log(profile.routing.finalRoute); // 'prod'
|
|
37
|
+
console.log(profile.localReputation?.tier); // 'AAA'
|
|
38
|
+
console.log(profile.onchainReputation.averageScore); // 92.5
|
|
48
39
|
```
|
|
49
40
|
|
|
41
|
+
> **Note:** API key authentication is not yet enforced. The `apiKey` config
|
|
42
|
+
> field is reserved for future use.
|
|
43
|
+
|
|
50
44
|
## Core Concepts
|
|
51
45
|
|
|
52
46
|
### Route Decisions
|
|
53
47
|
|
|
54
48
|
The SDK returns one of four routing decisions:
|
|
55
49
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
| Route | Meaning | Trust Tiers |
|
|
51
|
+
|-------|---------|-------------|
|
|
52
|
+
| `prod` | Full access | AAA – BAA |
|
|
53
|
+
| `prod_throttled` | Rate-limited access | BA – B |
|
|
54
|
+
| `sandbox` | Agent under evaluation | Temporary |
|
|
55
|
+
| `sandbox_only` | Access denied | CAA – C |
|
|
60
56
|
|
|
61
57
|
### Trust Evaluation
|
|
62
58
|
|
|
63
59
|
Valiron evaluates agents using:
|
|
64
60
|
|
|
65
|
-
1. **On-chain Reputation**
|
|
66
|
-
2. **Behavioral Analysis**
|
|
67
|
-
3. **Credit Rating**
|
|
61
|
+
1. **On-chain Reputation** — ERC-8004 feedback submitted by other organizations
|
|
62
|
+
2. **Behavioral Analysis** — Sandbox evaluation of rate-limit compliance, error rates, and request patterns
|
|
63
|
+
3. **Credit Rating** — Moody's-style scoring system (AAA to C)
|
|
68
64
|
|
|
69
65
|
### Architecture
|
|
70
66
|
|
|
71
67
|
```
|
|
72
|
-
Request → SDK.checkAgent() → Valiron API → Route Decision →
|
|
68
|
+
Request → SDK.checkAgent() → Valiron Operator API → Route Decision → Your App
|
|
73
69
|
```
|
|
74
70
|
|
|
75
|
-
The SDK queries Valiron's trust infrastructure, which evaluates on-chain reputation and behavioral metrics to return a routing decision. Your application implements access control based on this decision.
|
|
76
|
-
|
|
77
71
|
## API Reference
|
|
78
72
|
|
|
79
73
|
### Constructor
|
|
80
74
|
|
|
81
75
|
```typescript
|
|
82
|
-
new ValironSDK(config
|
|
76
|
+
new ValironSDK(config?: ValironConfig)
|
|
83
77
|
```
|
|
84
78
|
|
|
85
|
-
|
|
79
|
+
| Option | Type | Default | Description |
|
|
80
|
+
|--------|------|---------|-------------|
|
|
81
|
+
| `apiKey` | `string` | — | API key (reserved for future use) |
|
|
82
|
+
| `endpoint` | `string` | `https://valiron-edge-proxy.onrender.com` | API endpoint URL |
|
|
83
|
+
| `chain` | `'ethereum' \| 'monad'` | `undefined` (server default) | Default blockchain network for all calls |
|
|
84
|
+
| `timeout` | `number` | `5000` | Request timeout (ms) |
|
|
85
|
+
| `debug` | `boolean` | `false` | Enable debug logging |
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|--------|------|----------|---------|-------------|
|
|
89
|
-
| `apiKey` | string | No | - | API key (reserved for future use) |
|
|
90
|
-
| `endpoint` | string | No | `https://valiron-edge-proxy.onrender.com` | API endpoint URL |
|
|
91
|
-
| `timeout` | number | No | `5000` | Request timeout (ms) |
|
|
92
|
-
| `debug` | boolean | No | `false` | Enable debug logging |
|
|
93
|
-
|
|
94
|
-
### Methods
|
|
87
|
+
---
|
|
95
88
|
|
|
96
|
-
|
|
89
|
+
### `checkAgent(agentId, options?): Promise<RouteDecision>`
|
|
97
90
|
|
|
98
|
-
|
|
91
|
+
Quick routing check — returns just the route decision string.
|
|
99
92
|
|
|
100
|
-
**Returns:**
|
|
101
93
|
```typescript
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
reason: string,
|
|
105
|
-
explanation: string,
|
|
106
|
-
onChainData?: {
|
|
107
|
-
feedbackCount: number,
|
|
108
|
-
averageScore: number,
|
|
109
|
-
agentId: string
|
|
110
|
-
},
|
|
111
|
-
localReputation?: {
|
|
112
|
-
score: number,
|
|
113
|
-
tier: MoodysRating,
|
|
114
|
-
riskLevel: 'GREEN' | 'YELLOW' | 'RED',
|
|
115
|
-
graduated: boolean
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
```
|
|
94
|
+
const route = await valiron.checkAgent('25459');
|
|
95
|
+
// route: 'prod' | 'prod_throttled' | 'sandbox' | 'sandbox_only'
|
|
119
96
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const decision = await valiron.checkAgent('417');
|
|
123
|
-
console.log(decision.route); // 'prod'
|
|
124
|
-
console.log(decision.localReputation?.tier); // 'AAA'
|
|
97
|
+
// With chain override
|
|
98
|
+
const monadRoute = await valiron.checkAgent('8348', { chain: 'monad' });
|
|
125
99
|
```
|
|
126
100
|
|
|
127
101
|
---
|
|
128
102
|
|
|
129
|
-
|
|
103
|
+
### `getAgentProfile(agentId, options?): Promise<AgentProfile>`
|
|
130
104
|
|
|
131
|
-
|
|
105
|
+
Full trust profile combining on-chain identity, reputation, behavioral data, and routing.
|
|
132
106
|
|
|
133
|
-
**Example:**
|
|
134
107
|
```typescript
|
|
135
|
-
const
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
---
|
|
108
|
+
const profile = await valiron.getAgentProfile('25459');
|
|
139
109
|
|
|
140
|
-
|
|
110
|
+
// Query a Monad agent
|
|
111
|
+
const monadProfile = await valiron.getAgentProfile('8348', { chain: 'monad' });
|
|
112
|
+
```
|
|
141
113
|
|
|
142
|
-
|
|
114
|
+
**Response shape:**
|
|
143
115
|
|
|
144
|
-
**Returns:**
|
|
145
116
|
```typescript
|
|
146
117
|
{
|
|
147
118
|
agentId: string,
|
|
148
119
|
identity: {
|
|
120
|
+
agentId: string,
|
|
149
121
|
wallet: string,
|
|
122
|
+
tokenUri: string,
|
|
150
123
|
name: string,
|
|
151
|
-
description: string,
|
|
152
124
|
image: string,
|
|
153
|
-
|
|
125
|
+
description: string,
|
|
126
|
+
endpoints: Record<string, string>,
|
|
127
|
+
trustModels: string[],
|
|
154
128
|
},
|
|
155
129
|
onchainReputation: {
|
|
156
|
-
count:
|
|
130
|
+
count: string,
|
|
157
131
|
averageScore: number,
|
|
158
|
-
feedbackEntries: FeedbackEntry[]
|
|
132
|
+
feedbackEntries: FeedbackEntry[],
|
|
133
|
+
totalFeedback: number,
|
|
159
134
|
},
|
|
160
135
|
localReputation: {
|
|
136
|
+
exists: boolean,
|
|
161
137
|
score: number,
|
|
162
138
|
tier: MoodysRating,
|
|
163
139
|
riskLevel: 'GREEN' | 'YELLOW' | 'RED',
|
|
164
|
-
|
|
165
|
-
|
|
140
|
+
graduated: boolean,
|
|
141
|
+
metrics: BehavioralMetrics,
|
|
142
|
+
} | null,
|
|
166
143
|
routing: {
|
|
167
144
|
finalRoute: RouteDecision,
|
|
168
|
-
decision: string,
|
|
169
|
-
reasons: string[]
|
|
170
|
-
|
|
145
|
+
decision: string, // human-readable explanation
|
|
146
|
+
reasons: string[], // array of reasoning steps
|
|
147
|
+
signals: {
|
|
148
|
+
onchain: { feedbackCount, averageScore, threshold },
|
|
149
|
+
local: { exists, graduated, tier, riskLevel, score },
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
timestamp: string,
|
|
171
153
|
}
|
|
172
154
|
```
|
|
173
155
|
|
|
174
|
-
**Example:**
|
|
175
|
-
```typescript
|
|
176
|
-
const profile = await valiron.getAgentProfile('417');
|
|
177
|
-
console.log(`Score: ${profile.localReputation.score}/100`);
|
|
178
|
-
console.log(`Tier: ${profile.localReputation.tier}`);
|
|
179
|
-
console.log(`On-chain feedback: ${profile.onchainReputation.count} entries`);
|
|
180
|
-
```
|
|
181
|
-
|
|
182
156
|
---
|
|
183
157
|
|
|
184
|
-
|
|
158
|
+
### `getWalletProfile(wallet, options?): Promise<WalletProfile>`
|
|
185
159
|
|
|
186
|
-
|
|
160
|
+
Reverse-lookup a wallet address to get its trust profile.
|
|
187
161
|
|
|
188
|
-
**Parameters:**
|
|
189
162
|
```typescript
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
[key: string]: any
|
|
196
|
-
}
|
|
197
|
-
}
|
|
163
|
+
const profile = await valiron.getWalletProfile('0x52ce…');
|
|
164
|
+
console.log(profile.routing.finalRoute);
|
|
165
|
+
|
|
166
|
+
// With chain override
|
|
167
|
+
const monadProfile = await valiron.getWalletProfile('0xABC…', { chain: 'monad' });
|
|
198
168
|
```
|
|
199
169
|
|
|
200
|
-
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
### `triggerSandboxTest(agentId, options?): Promise<SandboxResult>`
|
|
173
|
+
|
|
174
|
+
Run real sandbox tests against an agent and compute a Valiron score.
|
|
175
|
+
|
|
201
176
|
```typescript
|
|
202
|
-
await valiron.
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
recordsProcessed: 10000
|
|
210
|
-
}
|
|
211
|
-
});
|
|
177
|
+
const result = await valiron.triggerSandboxTest('25459');
|
|
178
|
+
console.log(result.valironScore); // 95
|
|
179
|
+
console.log(result.tier); // 'AAA'
|
|
180
|
+
console.log(result.riskLevel); // 'GREEN'
|
|
181
|
+
console.log(result.mode); // 'endpoint-probe' | 'sandbox-relay'
|
|
182
|
+
console.log(result.testSummary);
|
|
183
|
+
// { totalRequests, successCount, rateLimitCount, errorCount, avgLatencyMs }
|
|
212
184
|
```
|
|
213
185
|
|
|
214
186
|
---
|
|
215
187
|
|
|
216
|
-
|
|
188
|
+
## Multichain Support
|
|
217
189
|
|
|
218
|
-
|
|
190
|
+
Valiron supports multiple blockchain networks. Set the chain globally or per-call:
|
|
219
191
|
|
|
220
|
-
**Example:**
|
|
221
192
|
```typescript
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
193
|
+
// Option A: Set default chain at construction
|
|
194
|
+
const valiron = new ValironSDK({ chain: 'monad' });
|
|
195
|
+
await valiron.getAgentProfile('8348'); // Uses Monad
|
|
196
|
+
|
|
197
|
+
// Option B: Override per-call (takes precedence over constructor default)
|
|
198
|
+
const valiron2 = new ValironSDK(); // No default chain
|
|
199
|
+
await valiron2.getAgentProfile('8348', { chain: 'monad' }); // Monad
|
|
200
|
+
await valiron2.getAgentProfile('25459', { chain: 'ethereum' }); // Ethereum
|
|
201
|
+
await valiron2.getAgentProfile('25459'); // Server default (ethereum)
|
|
202
|
+
|
|
203
|
+
// Option C: Both (per-call overrides constructor)
|
|
204
|
+
const valiron3 = new ValironSDK({ chain: 'ethereum' });
|
|
205
|
+
await valiron3.getAgentProfile('8348', { chain: 'monad' }); // Monad override
|
|
226
206
|
```
|
|
227
207
|
|
|
208
|
+
**Supported chains:** `ethereum`, `monad`
|
|
209
|
+
|
|
210
|
+
All SDK methods accept an optional `CallOptions` object with a `chain` field.
|
|
211
|
+
|
|
228
212
|
---
|
|
229
213
|
|
|
230
214
|
## Integration Examples
|
|
@@ -232,216 +216,77 @@ await valiron.triggerSandboxTest({
|
|
|
232
216
|
### Express.js Middleware
|
|
233
217
|
|
|
234
218
|
```typescript
|
|
235
|
-
import { ValironSDK } from '@valiron/sdk';
|
|
236
219
|
import express from 'express';
|
|
220
|
+
import { ValironSDK } from '@valiron/sdk';
|
|
237
221
|
|
|
238
222
|
const app = express();
|
|
239
|
-
const valiron = new ValironSDK(
|
|
240
|
-
|
|
241
|
-
async
|
|
242
|
-
const agentId = req.headers['x-agent-id'];
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
223
|
+
const valiron = new ValironSDK();
|
|
224
|
+
|
|
225
|
+
app.use('/api/protected/*', async (req, res, next) => {
|
|
226
|
+
const agentId = req.headers['x-agent-id'] as string;
|
|
227
|
+
if (!agentId) return res.status(400).json({ error: 'Missing x-agent-id' });
|
|
228
|
+
|
|
229
|
+
const profile = await valiron.getAgentProfile(agentId);
|
|
230
|
+
const { finalRoute } = profile.routing;
|
|
231
|
+
|
|
232
|
+
if (finalRoute === 'prod' || finalRoute === 'prod_throttled') {
|
|
233
|
+
(req as any).valironProfile = profile;
|
|
247
234
|
return next();
|
|
248
235
|
}
|
|
249
|
-
|
|
236
|
+
|
|
250
237
|
return res.status(403).json({
|
|
251
|
-
error: 'Access denied',
|
|
252
|
-
|
|
238
|
+
error: finalRoute === 'sandbox' ? 'Agent under evaluation' : 'Access denied',
|
|
239
|
+
message: profile.routing.decision,
|
|
253
240
|
});
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
app.use('/api/protected/*', valironMiddleware);
|
|
241
|
+
});
|
|
257
242
|
```
|
|
258
243
|
|
|
259
244
|
### Next.js Middleware
|
|
260
245
|
|
|
261
246
|
```typescript
|
|
262
|
-
// middleware.ts
|
|
263
247
|
import { NextRequest, NextResponse } from 'next/server';
|
|
264
248
|
import { ValironSDK } from '@valiron/sdk';
|
|
265
249
|
|
|
266
|
-
const valiron = new ValironSDK(
|
|
250
|
+
const valiron = new ValironSDK();
|
|
267
251
|
|
|
268
252
|
export async function middleware(request: NextRequest) {
|
|
269
253
|
const agentId = request.headers.get('x-agent-id');
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (decision.route === 'prod' || decision.route === 'prod_throttled') {
|
|
273
|
-
return NextResponse.next();
|
|
254
|
+
if (!agentId) {
|
|
255
|
+
return NextResponse.json({ error: 'Missing x-agent-id' }, { status: 400 });
|
|
274
256
|
}
|
|
275
|
-
|
|
276
|
-
return NextResponse.json(
|
|
277
|
-
{ error: 'Access denied' },
|
|
278
|
-
{ status: 403 }
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
257
|
|
|
282
|
-
|
|
283
|
-
matcher: '/api/protected/:path*'
|
|
284
|
-
};
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
### Simple API Gateway
|
|
258
|
+
const route = await valiron.checkAgent(agentId);
|
|
288
259
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const valiron = new ValironSDK({ apiKey: 'vln_xxx' });
|
|
293
|
-
|
|
294
|
-
async function handleRequest(agentId: string) {
|
|
295
|
-
const decision = await valiron.checkAgent(agentId);
|
|
296
|
-
|
|
297
|
-
switch (decision.route) {
|
|
298
|
-
case 'prod':
|
|
299
|
-
return processRequest({ rateLimit: null });
|
|
300
|
-
|
|
301
|
-
case 'prod_throttled':
|
|
302
|
-
return processRequest({ rateLimit: '100/hour' });
|
|
303
|
-
|
|
304
|
-
case 'sandbox':
|
|
305
|
-
return { error: 'Agent under evaluation', retryAfter: 30 };
|
|
306
|
-
|
|
307
|
-
case 'sandbox_only':
|
|
308
|
-
return { error: 'Access denied', reason: decision.reason };
|
|
260
|
+
if (route === 'prod' || route === 'prod_throttled') {
|
|
261
|
+
return NextResponse.next();
|
|
309
262
|
}
|
|
310
|
-
}
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
### Integration with x402 Payment APIs
|
|
314
|
-
|
|
315
|
-
Valiron integrates with **x402** payment-gated APIs (e.g., [Orthogonal](https://www.orth.sh/)). Verify trust before processing payments to prevent malicious agents from accessing paid endpoints.
|
|
316
|
-
|
|
317
|
-
#### Server-Side: Validate Trust + Payment
|
|
318
263
|
|
|
319
|
-
|
|
320
|
-
import { ValironSDK } from '@valiron/sdk';
|
|
321
|
-
import { privateKeyToAccount } from 'viem/accounts';
|
|
322
|
-
import { getAddress } from 'viem';
|
|
323
|
-
import { exact } from 'x402/schemes'; // Real x402 package
|
|
324
|
-
|
|
325
|
-
const valiron = new ValironSDK();
|
|
326
|
-
|
|
327
|
-
async function trustThenPayment(req, res, next) {
|
|
328
|
-
const agentId = req.headers['x-agent-id'];
|
|
329
|
-
const paymentHeader = req.headers['x-payment'];
|
|
330
|
-
|
|
331
|
-
// Step 1: Verify trust before payment
|
|
332
|
-
const { route, localReputation } = await valiron.checkAgent(agentId);
|
|
333
|
-
|
|
334
|
-
if (route === 'sandbox_only') {
|
|
335
|
-
return res.status(403).json({
|
|
336
|
-
error: 'Agent banned',
|
|
337
|
-
reason: 'Failed behavioral trust requirements'
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (route === 'sandbox') {
|
|
342
|
-
return res.status(403).json({
|
|
343
|
-
error: 'Agent under evaluation',
|
|
344
|
-
retryAfter: 30
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// Step 2: Verify payment (trusted agents only)
|
|
349
|
-
if (!paymentHeader) {
|
|
350
|
-
// Dynamic pricing based on trust tier
|
|
351
|
-
const pricing = {
|
|
352
|
-
'AAA': 5, 'AA': 5, 'A': 8, 'BAA': 10,
|
|
353
|
-
'BA': 25, 'B': 50, 'CAA': 100, 'CA': 100, 'C': 100
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
return res.status(402).json({
|
|
357
|
-
error: 'Payment Required',
|
|
358
|
-
accepts: [{
|
|
359
|
-
scheme: 'exact',
|
|
360
|
-
network: 'base',
|
|
361
|
-
maxAmountRequired: pricing[localReputation?.tier || 'B'],
|
|
362
|
-
asset: '0x...',
|
|
363
|
-
payTo: '0x...',
|
|
364
|
-
// ... x402 payment requirements
|
|
365
|
-
}]
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Verify payment (use x402 library)
|
|
370
|
-
const valid = await exact.evm.verifyPayment(paymentHeader);
|
|
371
|
-
if (!valid) {
|
|
372
|
-
return res.status(402).json({ error: 'Invalid payment' });
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
// Step 3: Apply trust-based rate limits
|
|
376
|
-
req.rateLimit = route === 'prod' ? 1000 : 100; // requests/hour
|
|
377
|
-
next();
|
|
264
|
+
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
|
378
265
|
}
|
|
379
266
|
|
|
380
|
-
|
|
267
|
+
export const config = { matcher: '/api/protected/:path*' };
|
|
381
268
|
```
|
|
382
269
|
|
|
383
|
-
|
|
270
|
+
### x402 Payment Integration
|
|
384
271
|
|
|
385
|
-
|
|
386
|
-
import { ValironSDK } from '@valiron/sdk';
|
|
387
|
-
import { exact } from 'x402/schemes';
|
|
388
|
-
import { privateKeyToAccount } from 'viem/accounts';
|
|
272
|
+
Valiron integrates with x402 payment-gated APIs (e.g., [Orthogonal](https://www.orth.sh/)). Verify trust before processing payments to prevent malicious agents from accessing paid endpoints.
|
|
389
273
|
|
|
390
|
-
|
|
391
|
-
const
|
|
274
|
+
```typescript
|
|
275
|
+
const profile = await valiron.getAgentProfile(agentId);
|
|
392
276
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const { route } = await valiron.checkAgent(agentId);
|
|
396
|
-
if (route === 'sandbox_only') {
|
|
397
|
-
throw new Error('Agent access denied');
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// Get payment requirements
|
|
401
|
-
const res = await fetch(url);
|
|
402
|
-
const { accepts } = await res.json();
|
|
403
|
-
|
|
404
|
-
// Create x402 payment header
|
|
405
|
-
const paymentHeader = await exact.evm.createPaymentHeader(
|
|
406
|
-
account,
|
|
407
|
-
1,
|
|
408
|
-
accepts[0]
|
|
409
|
-
);
|
|
410
|
-
|
|
411
|
-
// Retry with payment
|
|
412
|
-
const data = await fetch(url, {
|
|
413
|
-
headers: { 'X-PAYMENT': paymentHeader }
|
|
414
|
-
}).then(r => r.json());
|
|
415
|
-
|
|
416
|
-
return data;
|
|
277
|
+
if (profile.routing.finalRoute === 'sandbox_only') {
|
|
278
|
+
return res.status(403).json({ error: 'Agent banned' });
|
|
417
279
|
}
|
|
418
|
-
```
|
|
419
|
-
|
|
420
|
-
**Trust + Payment Benefits:**
|
|
421
|
-
- Reject untrusted agents before payment verification
|
|
422
|
-
- Implement dynamic pricing based on trust tier
|
|
423
|
-
- Reduce transaction overhead by filtering banned agents
|
|
424
|
-
- Mitigate DDoS attacks from paid malicious requests
|
|
425
|
-
|
|
426
|
-
**Compatible with:**
|
|
427
|
-
- [Orthogonal](https://www.orth.sh/) x402 API marketplace
|
|
428
|
-
- Any HTTP 402 Payment Required API
|
|
429
|
-
|
|
430
|
-
See [examples/x402-integration.ts](examples/x402-integration.ts) for complete implementation.
|
|
431
280
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
281
|
+
// Trust-based pricing — better agents pay less
|
|
282
|
+
const pricing = { AAA: 5, AA: 5, A: 8, BAA: 10, BA: 25, B: 50 };
|
|
283
|
+
const tier = profile.localReputation?.tier || 'B';
|
|
284
|
+
const price = pricing[tier] || 50;
|
|
435
285
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
MoodysRating,
|
|
441
|
-
AgentProfile,
|
|
442
|
-
RoutingResponse,
|
|
443
|
-
ValironError
|
|
444
|
-
} from '@valiron/sdk';
|
|
286
|
+
// Return x402 payment challenge
|
|
287
|
+
return res.status(402).json({
|
|
288
|
+
accepts: [{ scheme: 'exact', maxAmountRequired: price, payTo: '0x…' }],
|
|
289
|
+
});
|
|
445
290
|
```
|
|
446
291
|
|
|
447
292
|
## Moody's Credit Rating Tiers
|
|
@@ -458,29 +303,45 @@ import {
|
|
|
458
303
|
| CA | 20-34 | Extremely speculative | Sandbox only |
|
|
459
304
|
| C | 0-19 | Default risk | Sandbox only |
|
|
460
305
|
|
|
306
|
+
## TypeScript Types
|
|
307
|
+
|
|
308
|
+
The SDK is fully typed. Import types as needed:
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
import {
|
|
312
|
+
ValironSDK,
|
|
313
|
+
SupportedChain,
|
|
314
|
+
CallOptions,
|
|
315
|
+
RouteDecision,
|
|
316
|
+
MoodysRating,
|
|
317
|
+
RiskLevel,
|
|
318
|
+
AgentProfile,
|
|
319
|
+
WalletProfile,
|
|
320
|
+
SandboxResult,
|
|
321
|
+
AgentIdentity,
|
|
322
|
+
OnchainReputation,
|
|
323
|
+
LocalReputation,
|
|
324
|
+
RoutingExplanation,
|
|
325
|
+
BehavioralMetrics,
|
|
326
|
+
FeedbackEntry,
|
|
327
|
+
ValironError,
|
|
328
|
+
ValironClient,
|
|
329
|
+
} from '@valiron/sdk';
|
|
330
|
+
```
|
|
331
|
+
|
|
461
332
|
## Error Handling
|
|
462
333
|
|
|
463
334
|
```typescript
|
|
464
335
|
import { ValironError } from '@valiron/sdk';
|
|
465
336
|
|
|
466
337
|
try {
|
|
467
|
-
const
|
|
338
|
+
const profile = await valiron.getAgentProfile('123');
|
|
468
339
|
} catch (error) {
|
|
469
340
|
if (error instanceof ValironError) {
|
|
470
|
-
console.error('Code:', error.code);
|
|
471
|
-
console.error('Message:', error.message);
|
|
472
|
-
console.error('Status:', error.statusCode);
|
|
473
|
-
|
|
474
341
|
switch (error.code) {
|
|
475
|
-
case 'TIMEOUT_ERROR':
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
case 'NETWORK_ERROR':
|
|
479
|
-
// Handle network issues
|
|
480
|
-
break;
|
|
481
|
-
case 'API_ERROR':
|
|
482
|
-
// Handle API errors
|
|
483
|
-
break;
|
|
342
|
+
case 'TIMEOUT_ERROR': // Request timed out
|
|
343
|
+
case 'NETWORK_ERROR': // Could not reach API
|
|
344
|
+
case 'API_ERROR': // Non-2xx response (check error.statusCode)
|
|
484
345
|
}
|
|
485
346
|
}
|
|
486
347
|
}
|
|
@@ -492,7 +353,7 @@ try {
|
|
|
492
353
|
|
|
493
354
|
## Support
|
|
494
355
|
|
|
495
|
-
- **Email**: founders@valiron.
|
|
356
|
+
- **Email**: founders@valiron.co
|
|
496
357
|
|
|
497
358
|
## License
|
|
498
359
|
|