@valiron/sdk 0.12.0 → 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.
@@ -20,8 +20,8 @@ function extractHeader(req, name) {
20
20
  function defaultExtractAgentId(req) {
21
21
  return extractHeader(req, "x-agent-id");
22
22
  }
23
- function defaultExtractAgentKeyHash(req) {
24
- return extractHeader(req, "x-agent-key-hash");
23
+ function defaultExtractAgentAddress(req) {
24
+ return extractHeader(req, "x-agent-address") || extractHeader(req, "x-agent-key-hash");
25
25
  }
26
26
  /**
27
27
  * Normalize a KeyAgentProfile into a GateResult so all middleware
@@ -39,7 +39,7 @@ function keyProfileToGateResult(profile, minScore) {
39
39
  tier,
40
40
  riskLevel,
41
41
  route,
42
- agentId: profile.agentKeyHash,
42
+ agentId: profile.agentAddress,
43
43
  wallet: "",
44
44
  chain: {
45
45
  network: "web2",
@@ -68,16 +68,16 @@ async function runGateCheck(config, req) {
68
68
  }
69
69
  }
70
70
  // 2. Fall back to key-based agent (web2)
71
- const extractKeyHash = config.extractAgentKeyHash || defaultExtractAgentKeyHash;
72
- const keyHash = extractKeyHash(req);
73
- if (keyHash) {
71
+ const extractAddress = config.extractAgentAddress || defaultExtractAgentAddress;
72
+ const agentAddr = extractAddress(req);
73
+ if (agentAddr) {
74
74
  try {
75
- const profile = await config.sdk.getKeyAgentProfile(keyHash);
75
+ const profile = await config.sdk.getKeyAgentProfile(agentAddr);
76
76
  const result = keyProfileToGateResult(profile, config.minScore ?? 65);
77
- return { agentId: keyHash, result };
77
+ return { agentId: agentAddr, result };
78
78
  }
79
79
  catch (error) {
80
- return { agentId: keyHash, error };
80
+ return { agentId: agentAddr, error };
81
81
  }
82
82
  }
83
83
  // 3. No agent identity found
@@ -101,7 +101,7 @@ async function runGateCheck(config, req) {
101
101
  * const valiron = new ValironSDK({ chain: 'monad' });
102
102
  *
103
103
  * // Apply before your x402 / payment handler
104
- * // Works with both on-chain (x-agent-id) and key-based (x-agent-key-hash) agents
104
+ * Works with both on-chain (x-agent-id) and key-based (x-agent-address) agents
105
105
  * app.use('/api/paid', createValironGate({ sdk: valiron }));
106
106
  *
107
107
  * // Custom threshold
@@ -119,7 +119,11 @@ function createValironGate(config) {
119
119
  return async function valironGate(req, res, next) {
120
120
  const { agentId, result, error } = await runGateCheck(frozen, req);
121
121
  if (!agentId) {
122
- return res.status(401).json({ error: "Missing agent identity (provide x-agent-id or x-agent-key-hash header)" });
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
+ });
123
127
  }
124
128
  if (error) {
125
129
  return res.status(503).json({
@@ -172,7 +176,7 @@ async function valironFastifyPlugin(fastify, opts) {
172
176
  const gateConfig = {
173
177
  sdk: opts.sdk,
174
178
  extractAgentId: opts.extractAgentId,
175
- extractAgentKeyHash: opts.extractAgentKeyHash,
179
+ extractAgentAddress: opts.extractAgentAddress,
176
180
  minScore: opts.minScore,
177
181
  ttlMs: opts.ttlMs,
178
182
  onAllow: opts.onAllow,
@@ -194,7 +198,11 @@ async function valironFastifyPlugin(fastify, opts) {
194
198
  }
195
199
  const { agentId, result, error } = await runGateCheck(gateConfig, request);
196
200
  if (!agentId) {
197
- return reply.status(401).send({ error: "Missing agent identity (provide x-agent-id or x-agent-key-hash header)" });
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
+ });
198
206
  }
199
207
  if (error) {
200
208
  return reply.status(503).send({
@@ -247,8 +255,8 @@ function createValironNextMiddleware(config) {
247
255
  const { sdk, minScore, ttlMs, paths } = config;
248
256
  const extractAgentId = config.extractAgentId ||
249
257
  ((req) => req.headers.get("x-agent-id"));
250
- const extractKeyHash = config.extractAgentKeyHash ||
251
- ((req) => req.headers.get("x-agent-key-hash"));
258
+ const extractAddress = config.extractAgentAddress ||
259
+ ((req) => req.headers.get("x-agent-address") || req.headers.get("x-agent-key-hash"));
252
260
  return async function valironNextGate(request) {
253
261
  // Skip paths not in scope
254
262
  if (paths && paths.length > 0) {
@@ -257,11 +265,15 @@ function createValironNextMiddleware(config) {
257
265
  return undefined; // pass through
258
266
  }
259
267
  }
260
- // Try on-chain agent ID first, then key-based hash
268
+ // Try on-chain agent ID first, then key-based address
261
269
  const agentId = extractAgentId(request);
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 });
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 });
265
277
  }
266
278
  try {
267
279
  let result;
@@ -269,7 +281,7 @@ function createValironNextMiddleware(config) {
269
281
  result = await sdk.gate(agentId, { minScore, ttlMs });
270
282
  }
271
283
  else {
272
- const profile = await sdk.getKeyAgentProfile(keyHash);
284
+ const profile = await sdk.getKeyAgentProfile(agentAddr);
273
285
  result = keyProfileToGateResult(profile, minScore ?? 65);
274
286
  }
275
287
  if (result.allow) {
@@ -298,7 +310,7 @@ function createValironNextMiddleware(config) {
298
310
  * app.use('/api/*', async (c, next) => {
299
311
  * const gate = await valironGateCheck(valiron, {
300
312
  * agentId: c.req.header('x-agent-id') ?? null,
301
- * agentKeyHash: c.req.header('x-agent-key-hash') ?? null,
313
+ * agentAddress: c.req.header('x-agent-address') ?? null,
302
314
  * });
303
315
  * if (!gate.allow) return c.json(gate.body, gate.status);
304
316
  * c.set('valiron', gate.result);
@@ -312,7 +324,7 @@ function createValironNextMiddleware(config) {
312
324
  * app.use(async (ctx, next) => {
313
325
  * const gate = await valironGateCheck(valiron, {
314
326
  * agentId: ctx.get('x-agent-id') || null,
315
- * agentKeyHash: ctx.get('x-agent-key-hash') || null,
327
+ * agentAddress: ctx.get('x-agent-address') || null,
316
328
  * });
317
329
  * if (!gate.allow) {
318
330
  * ctx.status = gate.status;
@@ -325,15 +337,19 @@ function createValironNextMiddleware(config) {
325
337
  * ```
326
338
  */
327
339
  async function valironGateCheck(sdk, opts) {
328
- const { agentId, agentKeyHash, minScore, ttlMs } = opts;
329
- const identifier = agentId || agentKeyHash || null;
340
+ const { agentId, agentAddress, minScore, ttlMs } = opts;
341
+ const identifier = agentId || agentAddress || null;
330
342
  if (!identifier) {
331
343
  return {
332
344
  hasAgent: false,
333
345
  agentId: null,
334
346
  ok: false,
335
347
  status: 401,
336
- body: { error: "Missing agent identity" },
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
+ },
337
353
  };
338
354
  }
339
355
  try {
@@ -342,7 +358,7 @@ async function valironGateCheck(sdk, opts) {
342
358
  result = await sdk.gate(agentId, { minScore, ttlMs });
343
359
  }
344
360
  else {
345
- const profile = await sdk.getKeyAgentProfile(agentKeyHash);
361
+ const profile = await sdk.getKeyAgentProfile(agentAddress);
346
362
  result = keyProfileToGateResult(profile, minScore ?? 65);
347
363
  }
348
364
  if (result.allow) {
package/dist/types.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * Matches the actual edge-proxy /operator/* endpoints
6
6
  */
7
7
  /** Blockchain networks supported by the Valiron Operator API. */
8
- export type SupportedChain = "ethereum" | "monad" | "arbitrum" | "base" | "avalanche" | "celo" | "polygon" | "linea" | "abstract" | "bsc" | "gnosis" | "goat" | "mantle" | "megaeth" | "metis" | "optimism" | "scroll" | "skale_base" | "soneium" | "taiko" | "xlayer" | "base_sepolia" | "ethereum_sepolia" | "abstract_testnet" | "arbitrum_sepolia" | "avalanche_testnet" | "bsc_testnet" | "celo_testnet" | "linea_sepolia" | "mantle_testnet" | "megaeth_testnet" | "metis_sepolia" | "optimism_sepolia" | "polygon_amoy" | "scroll_sepolia" | "skale_base_sepolia" | "soneium_minato" | "taiko_hoodi" | "xlayer_testnet" | "hedera_testnet" | "arc_testnet" | "world_chain" | "solana";
8
+ export type SupportedChain = "ethereum" | "monad" | "arbitrum" | "base" | "avalanche" | "celo" | "polygon" | "linea" | "abstract" | "bsc" | "gnosis" | "goat" | "mantle" | "megaeth" | "metis" | "optimism" | "scroll" | "skale_base" | "soneium" | "taiko" | "xlayer" | "base_sepolia" | "ethereum_sepolia" | "abstract_testnet" | "arbitrum_sepolia" | "avalanche_testnet" | "bsc_testnet" | "celo_testnet" | "linea_sepolia" | "mantle_testnet" | "megaeth_testnet" | "metis_sepolia" | "optimism_sepolia" | "polygon_amoy" | "scroll_sepolia" | "skale_base_sepolia" | "soneium_minato" | "taiko_hoodi" | "xlayer_testnet" | "hedera_testnet" | "arc_testnet" | "solana";
9
9
  export type RouteDecision = "prod" | "prod_throttled" | "sandbox" | "sandbox_only";
10
10
  export type RiskLevel = "GREEN" | "YELLOW" | "RED";
11
11
  export type MoodysRating = "AAA" | "AA" | "A" | "BAA" | "BA" | "B" | "CAA" | "CA" | "C";
@@ -203,15 +203,57 @@ export interface GateResult {
203
203
  provisional?: boolean;
204
204
  }
205
205
  /**
206
- * Response from `POST /operator/key/:agentKeyHash/challenge`
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`
207
249
  */
208
250
  export interface KeyAgentChallenge {
209
251
  /** Random nonce the agent must sign */
210
252
  challenge: string;
211
253
  /** ISO 8601 expiry timestamp for the challenge */
212
254
  expiresAt: string;
213
- /** The agent key hash this challenge was issued for */
214
- agentKeyHash: string;
255
+ /** The agent's Ethereum address (keccak256-derived) */
256
+ agentAddress: string;
215
257
  }
216
258
  /** Verification level from World ID */
217
259
  export type WorldIdVerificationLevel = "orb" | "device" | "phone";
@@ -291,11 +333,11 @@ export interface IcebreakerOwnership {
291
333
  /**
292
334
  * Trust profile for a key-based (non-ERC-8004) agent.
293
335
  *
294
- * Response from `POST /operator/key/verify` and `GET /operator/key/:agentKeyHash`
336
+ * Response from `POST /operator/key/verify` and `GET /operator/key/:agentAddress`
295
337
  */
296
338
  export interface KeyAgentProfile {
297
- /** SHA-256 hash of the agent's public key */
298
- agentKeyHash: string;
339
+ /** Ethereum address derived via keccak256 of the agent's secp256k1 public key */
340
+ agentAddress: string;
299
341
  /** Whether identity has been verified via challenge-response */
300
342
  verified: boolean;
301
343
  /** Behavioral score (0-110) — same scoring as ERC-8004 agents */
@@ -374,88 +416,6 @@ export interface ValironConfig {
374
416
  */
375
417
  telemetry?: TelemetryConfig;
376
418
  }
377
- /** Response from `POST /channel/preflight/:agentId` */
378
- export interface ChannelPreflightResponse {
379
- agentId: string;
380
- tier: string;
381
- score: number;
382
- riskLevel: string;
383
- creditLine: string;
384
- creditLineReadable: string;
385
- maxRequests: number | null;
386
- durationSeconds: number;
387
- escrowAddress: string;
388
- }
389
- /** Response from `POST /channel/open/:agentId` */
390
- export interface ChannelOpenResponse {
391
- sessionId: string;
392
- agentId: string;
393
- tier: string;
394
- score: number;
395
- riskLevel: string;
396
- creditLine: string;
397
- creditLineReadable: string;
398
- maxRequests: number | null;
399
- expiresAt: string;
400
- durationSeconds: number;
401
- depositConfirmed: boolean;
402
- depositSignature: string;
403
- escrowAddress: string;
404
- }
405
- /** Response from `POST /channel/consume/:sessionId` */
406
- export interface ChannelConsumeResponse {
407
- requestId: string;
408
- cost: string;
409
- costReadable: string;
410
- description?: string;
411
- session: {
412
- consumed: string;
413
- consumedReadable: string;
414
- remaining: string;
415
- remainingReadable: string;
416
- requestCount: number;
417
- maxRequests: number | null;
418
- secondsRemaining: number;
419
- };
420
- }
421
- /** Response from `GET /channel/status/:sessionId` */
422
- export interface ChannelStatusResponse {
423
- sessionId: string;
424
- agentId: string;
425
- tier: string;
426
- creditLine: string;
427
- creditLineReadable: string;
428
- consumed: string;
429
- consumedReadable: string;
430
- remaining: string;
431
- remainingReadable: string;
432
- requestCount: number;
433
- maxRequests: number | null;
434
- expiresAt: string;
435
- secondsRemaining: number;
436
- active: boolean;
437
- settled: boolean;
438
- }
439
- /** Response from `POST /channel/settle/:sessionId` */
440
- export interface ChannelSettlementResponse {
441
- sessionId: string;
442
- settled: boolean;
443
- totalConsumed: string;
444
- totalConsumedReadable: string;
445
- requestsServed: number;
446
- unusedCredit: string;
447
- unusedCreditReadable: string;
448
- refundAmount?: string;
449
- refundReadable?: string;
450
- refundSignature?: string;
451
- refundWarning?: string;
452
- }
453
- /** Response from `GET /channel/health` */
454
- export interface ChannelHealthResponse {
455
- status: string;
456
- service: string;
457
- escrowAddress: string | null;
458
- }
459
419
  export declare class ValironError extends Error {
460
420
  code: string;
461
421
  statusCode?: number | undefined;