@valiron/sdk 1.4.0 → 2.1.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.
Files changed (49) hide show
  1. package/README.md +11 -148
  2. package/bin/valiron-card.mjs +211 -0
  3. package/dist/card.d.ts +565 -0
  4. package/dist/card.js +722 -0
  5. package/dist/clients.d.ts +6 -6
  6. package/dist/clients.js +33 -26
  7. package/dist/edge.d.ts +87 -0
  8. package/dist/edge.js +164 -0
  9. package/dist/edgeGate.d.ts +146 -0
  10. package/dist/edgeGate.js +230 -0
  11. package/dist/edgeGateVerify.d.ts +146 -0
  12. package/dist/edgeGateVerify.js +243 -0
  13. package/dist/identity.d.ts +6 -0
  14. package/dist/identity.js +6 -1
  15. package/dist/identityMiddleware.d.ts +8 -0
  16. package/dist/identityMiddleware.js +12 -4
  17. package/dist/index.d.ts +5 -10
  18. package/dist/index.js +4 -10
  19. package/dist/middleware.d.ts +20 -9
  20. package/dist/middleware.js +47 -19
  21. package/dist/operatorSdk.d.ts +11 -2
  22. package/dist/operatorSdk.js +13 -7
  23. package/dist/types.d.ts +14 -4
  24. package/docs/AGENT-READY-APIS.md +3 -181
  25. package/docs/API-REFERENCE.md +94 -546
  26. package/docs/CHAINS.md +3 -3
  27. package/docs/CLOUDFLARE-GATE-MIDDLEWARE.md +196 -0
  28. package/docs/CRAWLER-PROTECTION.md +545 -0
  29. package/docs/DASHBOARD.md +55 -23
  30. package/docs/E2E-MIDDLEWARE-INTEGRATION.md +19 -13
  31. package/docs/ERRORS.md +29 -19
  32. package/docs/IDENTITY.md +16 -14
  33. package/docs/PROVIDER-IDENTITY.md +15 -5
  34. package/docs/QUICKSTART.md +40 -24
  35. package/docs/README.md +21 -4
  36. package/docs/SANDBOX.md +7 -1
  37. package/docs/SDK-REFERENCE.md +115 -52
  38. package/docs/SECURITY-UPGRADE-2026-10-31.md +98 -0
  39. package/docs/TRUST-MODEL.md +14 -11
  40. package/docs/llms.txt +70 -302
  41. package/examples/cloudflare-gate-worker/README.md +198 -0
  42. package/examples/cloudflare-gate-worker/package.json +20 -0
  43. package/examples/cloudflare-gate-worker/worker.ts +309 -0
  44. package/examples/cloudflare-gate-worker/wrangler.toml +46 -0
  45. package/examples/cloudflare-worker/package.json +8 -0
  46. package/examples/cloudflare-worker/worker.ts +495 -0
  47. package/examples/cloudflare-worker/wrangler.toml +57 -0
  48. package/package.json +35 -5
  49. package/docs/MCP.md +0 -50
package/dist/card.d.ts ADDED
@@ -0,0 +1,565 @@
1
+ export type CardMode = "owner_account" | "delegated_agent";
2
+ export interface PassRecognition {
3
+ recognized: true;
4
+ verificationMode: "delegated_agent";
5
+ visitId: string;
6
+ merchantOwnerSubject: string;
7
+ environment: CardEnvironment;
8
+ owner: {
9
+ name: string;
10
+ email: string;
11
+ };
12
+ profile: {
13
+ kind: string;
14
+ value: Record<string, unknown>;
15
+ source: string;
16
+ assurance: string;
17
+ expiresAt: string | null;
18
+ }[];
19
+ connectedAccounts: {
20
+ provider: string;
21
+ name: string;
22
+ status: "connected";
23
+ }[];
24
+ agent: {
25
+ name: string;
26
+ thumbprint: string;
27
+ expiresAt: string;
28
+ verification: "owner_approved_key";
29
+ };
30
+ offers: {
31
+ id: string;
32
+ name: string;
33
+ benefit: CardResolution["benefit"];
34
+ }[];
35
+ verifiedAt: string;
36
+ expiresAt: string;
37
+ }
38
+ export interface CardResolution {
39
+ decisionId: string;
40
+ eligible: boolean;
41
+ verificationMode: CardMode;
42
+ merchantOwnerSubject: string;
43
+ customerBindingId: string;
44
+ versions: {
45
+ binding: number;
46
+ grant: number;
47
+ offer: number;
48
+ };
49
+ claims?: Record<string, unknown>;
50
+ benefit: {
51
+ type: "rate_limit";
52
+ requests: number;
53
+ baseRequests: number;
54
+ windowSeconds: 60;
55
+ };
56
+ expiresAt: string;
57
+ enforcementRequired: true;
58
+ }
59
+ export declare class CardAPIError extends Error {
60
+ status: number;
61
+ code: string;
62
+ /** Non-secret extra fields the API returned with the error. */
63
+ details?: {
64
+ delegationExpiresAt?: number;
65
+ } | undefined;
66
+ constructor(status: number, code: string,
67
+ /** Non-secret extra fields the API returned with the error. */
68
+ details?: {
69
+ delegationExpiresAt?: number;
70
+ } | undefined);
71
+ }
72
+ export declare const cardNonce: () => string;
73
+ export declare function createCardKey(): {
74
+ privateKey: string;
75
+ publicKey: string;
76
+ };
77
+ export type CardEnvironment = "test" | "live";
78
+ export type CardCredentialScope = "bindings:write" | "benefits:resolve" | "presentations:verify" | "enforcement:write" | "offers:write";
79
+ export interface CardOfferInput {
80
+ name: string;
81
+ mode: CardMode;
82
+ scopes?: string[];
83
+ baseLimit: number;
84
+ cardLimit: number;
85
+ routeOrigin: string;
86
+ routePath: string;
87
+ routeMethod?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
88
+ requirements?: {
89
+ kind: string;
90
+ assurances: string[];
91
+ source?: string;
92
+ field?: string;
93
+ equals?: string | boolean;
94
+ }[];
95
+ }
96
+ export interface CardOffer {
97
+ id: string;
98
+ name: string;
99
+ environment: CardEnvironment;
100
+ status: "draft" | "published" | "disabled";
101
+ mode: CardMode;
102
+ scopes: string[];
103
+ base_limit: number;
104
+ card_limit: number;
105
+ route_origin: string;
106
+ route_path: string;
107
+ route_method: string;
108
+ requirements: NonNullable<CardOfferInput["requirements"]>;
109
+ enforcement_verified_at: string | null;
110
+ }
111
+ export interface CardSellerSetup {
112
+ merchant: {
113
+ id: string;
114
+ name: string;
115
+ status: "pending" | "active" | "suspended";
116
+ return_uris: string[];
117
+ } | null;
118
+ offers: CardOffer[];
119
+ credentials: {
120
+ id: string;
121
+ prefix: string;
122
+ scopes: CardCredentialScope[];
123
+ environment: CardEnvironment;
124
+ expires_at: string;
125
+ revoked_at: string | null;
126
+ }[];
127
+ }
128
+ /** Bootstrap with the seller's existing authorized operator session. Never use an admin key or share a browser password. */
129
+ export declare class CardSellerClient {
130
+ private client;
131
+ constructor(options: {
132
+ baseUrl: string;
133
+ operatorToken: string;
134
+ transport?: "service" | "website";
135
+ fetch?: typeof fetch;
136
+ timeoutMs?: number;
137
+ });
138
+ getSetup(): Promise<CardSellerSetup>;
139
+ registerBusiness(input: {
140
+ name: string;
141
+ returnUris: string[];
142
+ }): Promise<{
143
+ merchant: NonNullable<CardSellerSetup["merchant"]>;
144
+ }>;
145
+ createCredential(input: {
146
+ environment: CardEnvironment;
147
+ scopes: CardCredentialScope[];
148
+ }): Promise<{
149
+ id: string;
150
+ key: string;
151
+ expires_at: string;
152
+ }>;
153
+ revokeCredential(id: string): Promise<{
154
+ ok: boolean;
155
+ }>;
156
+ }
157
+ export declare class CardClient {
158
+ private options;
159
+ /** delegationId -> delegation expiry (epoch s), learned from the pairing poll
160
+ * or from the server's own expiry-only rejection. In-memory only. */
161
+ private delegationExpiry;
162
+ constructor(options: {
163
+ baseUrl: string;
164
+ merchantKey?: string;
165
+ transport?: "service" | "website";
166
+ fetch?: typeof fetch;
167
+ timeoutMs?: number;
168
+ });
169
+ request<T = any>(path: string, body?: unknown, method?: string, extraHeaders?: Record<string, string>): Promise<T>;
170
+ /** Requires a scoped offers:write key; response is restricted to that merchant and environment. */
171
+ listOffers(): Promise<{
172
+ offers: CardOffer[];
173
+ environment: CardEnvironment;
174
+ merchantId: string;
175
+ }>;
176
+ createOffer(input: CardOfferInput): Promise<CardOffer>;
177
+ enableTestOffer(id: string): Promise<CardOffer>;
178
+ publishOffer(id: string): Promise<CardOffer>;
179
+ disableOffer(id: string): Promise<{
180
+ ok: boolean;
181
+ }>;
182
+ createConnect(input: {
183
+ offerId: string;
184
+ customerRef: string;
185
+ accountAlias: string;
186
+ returnUri: string;
187
+ idempotencyKey?: string;
188
+ }): Promise<{
189
+ state: string;
190
+ verifier: string;
191
+ id: string;
192
+ connectUrl: string;
193
+ expiresAt: string;
194
+ }>;
195
+ exchangeConnect(input: {
196
+ id: string;
197
+ code: string;
198
+ state: string;
199
+ expectedState: string;
200
+ verifier: string;
201
+ }): Promise<{
202
+ customerBindingId: string;
203
+ grantId: string;
204
+ }>;
205
+ resolveAccount(input: {
206
+ offerId: string;
207
+ customerBindingId: string;
208
+ requestId: string;
209
+ }): Promise<CardResolution>;
210
+ reportEnforcement(input: {
211
+ decisionId: string;
212
+ requestId: string;
213
+ allowed: boolean;
214
+ }): Promise<any>;
215
+ startApplication(input: {
216
+ privateKey: string;
217
+ merchantId?: string;
218
+ name: string;
219
+ scopes?: CardAgentScope[];
220
+ }): Promise<{
221
+ id: string;
222
+ approvalUrl: string;
223
+ pollSecret: string;
224
+ comparisonCode: string;
225
+ expiresAt: string;
226
+ pollIntervalSeconds: number;
227
+ }>;
228
+ /** Redeem a human-created one-use code with a fresh agent key. Human approval is still required. */
229
+ linkWithInvite(input: {
230
+ code: string;
231
+ name: string;
232
+ privateKey: string;
233
+ scopes?: CardAgentScope[];
234
+ }): Promise<{
235
+ id: string;
236
+ pollSecret: string;
237
+ comparisonCode: string;
238
+ approvalUrl: string;
239
+ expiresAt: string;
240
+ pollIntervalSeconds: number;
241
+ }>;
242
+ pollApplication(input: {
243
+ id: string;
244
+ pollSecret: string;
245
+ privateKey: string;
246
+ }): Promise<{
247
+ status: string;
248
+ delegationId?: string;
249
+ delegationExpiresAt?: number;
250
+ }>;
251
+ mintToken(input: {
252
+ delegationId: string;
253
+ customerBindingId: string;
254
+ offerId: string;
255
+ privateKey: string;
256
+ }): Promise<{
257
+ token: string;
258
+ expiresIn: number;
259
+ }>;
260
+ verifyPresentation(input: {
261
+ presentationToken: string;
262
+ proof: string;
263
+ request: {
264
+ method: string;
265
+ publicUrl: string;
266
+ bodyDigest: string;
267
+ customerBindingId: string;
268
+ requestId: string;
269
+ };
270
+ }): Promise<CardResolution>;
271
+ getMerchantIdentity(merchantId: string): Promise<{
272
+ merchantId: string;
273
+ name: string;
274
+ origins: string[];
275
+ protocol: "valiron-pass-v1";
276
+ }>;
277
+ mintIdentityToken(input: {
278
+ delegationId: string;
279
+ merchantId: string;
280
+ environment: CardEnvironment;
281
+ audienceOrigin: string;
282
+ privateKey: string;
283
+ /** "jws" requests a Valiron-signed, locally-verifiable token (edge/Worker
284
+ * verification against Valiron's JWKS at GET /card/v1/pass/jwks). Omit for
285
+ * the default opaque token, which requires the origin recognize path. */
286
+ format?: "jws";
287
+ }): Promise<{
288
+ token: string;
289
+ expiresIn: number;
290
+ format?: "jws";
291
+ }>;
292
+ /** Registers a Web Bot Auth signing key into Valiron's public key directory
293
+ * (/.well-known/http-message-signatures-directory). While the delegation is
294
+ * active, Valiron vouches for the key: requests signed with it verify as
295
+ * coming from a current Valiron Pass holder. Keys expire with the delegation
296
+ * or after 30 days, whichever comes first; re-register to rotate.
297
+ *
298
+ * Registration includes a directory self-signature made with the NEW WBA key
299
+ * (draft-meunier-http-message-signatures-directory-03 section 5.2: each key
300
+ * in a directory provides one signature over "@authority";req, proving the
301
+ * registrant holds the private key). `privateKey` is the DELEGATION key used
302
+ * only to authenticate the registration; `wbaPrivateKey` is the new key. */
303
+ registerWbaKey(input: {
304
+ delegationId: string;
305
+ privateKey: string;
306
+ publicKey: string;
307
+ wbaPrivateKey: string;
308
+ /** The delegation's expiry in epoch seconds. Optional: when omitted the
309
+ * client uses the expiry it learned from pollApplication, and otherwise
310
+ * discovers it from the server's rejection of an over-long signature and
311
+ * retries once. An explicit value is always used as given (never retried
312
+ * with a different one). The signature lifetime is min(30 days, expiry). */
313
+ delegationExpiresAt?: number;
314
+ }): Promise<{
315
+ kid: string;
316
+ expiresInDays: number;
317
+ }>;
318
+ private registerWbaKeyOnce;
319
+ revokeWbaKey(input: {
320
+ delegationId: string;
321
+ privateKey: string;
322
+ publicKey: string;
323
+ kid: string;
324
+ }): Promise<{
325
+ ok: true;
326
+ }>;
327
+ recognizeAgent(input: {
328
+ presentationToken: string;
329
+ proof: string;
330
+ request: {
331
+ method: string;
332
+ publicUrl: string;
333
+ bodyDigest: string;
334
+ requestId: string;
335
+ };
336
+ }): Promise<PassRecognition>;
337
+ reportAgentEnforcement(input: {
338
+ visitId: string;
339
+ offerId?: string;
340
+ allowed: boolean;
341
+ }): Promise<{
342
+ ok: true;
343
+ }>;
344
+ }
345
+ export declare function createPassRequestProof(input: {
346
+ privateKey: string;
347
+ token: string;
348
+ merchantId: string;
349
+ environment: CardEnvironment;
350
+ delegationId: string;
351
+ method: string;
352
+ url: string;
353
+ body?: Uint8Array | string;
354
+ }): string;
355
+ /** Recognizes an explicitly presented Pass. Anonymous requests keep the merchant's normal path.
356
+ * Install before JSON parsing, or supply rawBody containing the exact bytes received.
357
+ * Set requirePass for protected endpoints. A verified human-agent link is identity context,
358
+ * never a replacement for the merchant's account authorization.
359
+ */
360
+ export declare function createValironPassMiddleware(options: {
361
+ client: CardClient;
362
+ publicOrigin: string;
363
+ requirePass?: boolean;
364
+ applyPerk?: (identity: PassRecognition) => Promise<{
365
+ allowed: boolean;
366
+ offerId?: string;
367
+ retryAfter?: number;
368
+ }>;
369
+ }): (req: any, res: any, next: () => void) => Promise<any>;
370
+ export declare function createCardRequestProof(input: {
371
+ privateKey: string;
372
+ token: string;
373
+ merchantId: string;
374
+ delegationId: string;
375
+ customerBindingId: string;
376
+ method: string;
377
+ url: string;
378
+ body?: Uint8Array | string;
379
+ }): string;
380
+ export declare function cardPresentationHeaders(token: string, proof: string): {
381
+ "X-Valiron-Card-Token": string;
382
+ "X-Valiron-Card-Proof": string;
383
+ };
384
+ /** A Web Bot Auth signing identity: a key the agent generated locally and
385
+ * registered into Valiron's directory with CardClient.registerWbaKey, plus
386
+ * the directory URL verifiers resolve (the `Signature-Agent` value). */
387
+ export interface WbaSigningIdentity {
388
+ privateKey: string;
389
+ /** base64url JWK thumbprint returned by registerWbaKey. */
390
+ keyid: string;
391
+ /** Directory origin URL, e.g. "https://api.valiron.co" - never a value the
392
+ * agent invented; it must match the directory Cloudflare has registered. */
393
+ signatureAgent: string;
394
+ }
395
+ export declare function createWbaKeyPair(): {
396
+ privateKey: string;
397
+ publicKey: string;
398
+ };
399
+ /** The signature every directory key provides over the directory's own
400
+ * authority ("@authority";req with tag http-message-signatures-directory),
401
+ * proving the key's holder controls it. Made once at registration and served
402
+ * verbatim; re-register to rotate. */
403
+ export declare function createDirectorySelfSignature(identity: {
404
+ privateKey: string;
405
+ keyid: string;
406
+ }, authority: string, options?: {
407
+ created?: number;
408
+ expires?: number;
409
+ }): {
410
+ params: string;
411
+ signature: string;
412
+ };
413
+ /** Signs one outbound request per draft-ietf-webbotauth-httpsig-protocol-01
414
+ * section 5.2: covers @method (lowercase, RFC 9421 2.2.1), @path, @authority
415
+ * and the Signature-Agent member keyed to the signature label, with
416
+ * tag="web-bot-auth" and a base64url JWK-thumbprint keyid; Signature-Agent is
417
+ * sent in the required Dictionary form. The Pass presentation headers
418
+ * (X-Valiron-Card-Token / X-Valiron-Card-Proof) are covered when present - so
419
+ * a merchant never sees a Pass proof without the WBA signature that binds it
420
+ * (5.2.4 additional headers). Conforms to Valiron's published verifier
421
+ * limits (no @query / @target-uri, ASCII-only covered values). */
422
+ export declare function signWbaRequest(identity: WbaSigningIdentity, request: {
423
+ method: string;
424
+ url: string;
425
+ headers: Record<string, string>;
426
+ }): Record<string, string>;
427
+ export declare function accountSnapshotUsable(value: CardResolution, expected: {
428
+ bindingId: string;
429
+ offerVersion: number;
430
+ }, now?: number): boolean;
431
+ export declare function createCardQuotaAdapter(options: {
432
+ redis: {
433
+ eval: (...args: any[]) => Promise<unknown>;
434
+ };
435
+ merchantId: string;
436
+ environment: "test" | "live";
437
+ endpoint: string;
438
+ hardOwnerLimit: number;
439
+ globalLimit: number;
440
+ }): {
441
+ consume(input: {
442
+ subject: string;
443
+ limit: number;
444
+ }): Promise<{
445
+ allowed: boolean;
446
+ retryAfter: number;
447
+ remaining: number;
448
+ }>;
449
+ };
450
+ export declare function createValironCardGate(options: {
451
+ client: CardClient;
452
+ mode: "delegated_agent";
453
+ publicOrigin: string;
454
+ customerBinding: (req: any) => string;
455
+ consumeQuota: (resolution: CardResolution) => Promise<{
456
+ allowed: boolean;
457
+ retryAfter: number;
458
+ }>;
459
+ onVerified?: (req: any, result: CardResolution) => void;
460
+ }): (req: any, res: any, next: () => void) => Promise<any>;
461
+ /** Automatically renews short-lived credentials without extending owner consent or delegation. */
462
+ export declare class CardAgent {
463
+ private options;
464
+ private tokens;
465
+ private pending;
466
+ constructor(options: {
467
+ client: CardClient;
468
+ privateKey: string;
469
+ delegationId: string;
470
+ /** When set, presentation headers are additionally signed with Web Bot
471
+ * Auth so requests verify on Cloudflare zones and WBA-aware origins. */
472
+ webBotAuth?: WbaSigningIdentity;
473
+ });
474
+ /** Carries the approved human identity to an integrated business without an offer or account binding. */
475
+ identityFor(input: {
476
+ merchantId: string;
477
+ environment?: CardEnvironment;
478
+ method: string;
479
+ url: string;
480
+ body?: Uint8Array | string;
481
+ }): Promise<Record<string, string> | {
482
+ "X-Valiron-Card-Token": string;
483
+ "X-Valiron-Card-Proof": string;
484
+ }>;
485
+ presentationFor(input: {
486
+ merchantId: string;
487
+ customerBindingId: string;
488
+ offerId: string;
489
+ method: string;
490
+ url: string;
491
+ body?: Uint8Array | string;
492
+ }): Promise<Record<string, string> | {
493
+ "X-Valiron-Card-Token": string;
494
+ "X-Valiron-Card-Proof": string;
495
+ }>;
496
+ clearCredentials(): void;
497
+ }
498
+ export type CardAgentScope = "offers:read" | "offers:claim" | "pass:read" | "profile:write" | "evaluation:run" | "pass:present" | "channels:manage";
499
+ export interface CardMcpToolResult<T = any> {
500
+ content: {
501
+ type: string;
502
+ text: string;
503
+ }[];
504
+ structuredContent?: T;
505
+ isError?: boolean;
506
+ }
507
+ /** Deterministic JSON matching the Card backend so signatures cover identical bytes. */
508
+ export declare function canonicalCardJson(value: any): string;
509
+ /**
510
+ * Speaks MCP (JSON-RPC over POST) to the Valiron Pass MCP endpoint. Following
511
+ * the MCP authorization spec, the endpoint is an OAuth-style resource server:
512
+ * this client mints a short-lived scoped bearer token by signing a token grant
513
+ * with the owner-approved agent key, then sends it as Authorization: Bearer.
514
+ * The server enforces the scopes the owner granted at approval and records the
515
+ * action history.
516
+ */
517
+ export declare class CardMcpClient {
518
+ private options;
519
+ private token?;
520
+ private pending?;
521
+ constructor(options: {
522
+ client: CardClient;
523
+ privateKey: string;
524
+ delegationId: string;
525
+ scopes?: CardAgentScope[];
526
+ });
527
+ private accessToken;
528
+ private rpc;
529
+ initialize(): Promise<any>;
530
+ listTools(): Promise<{
531
+ tools: {
532
+ name: string;
533
+ description: string;
534
+ inputSchema: unknown;
535
+ }[];
536
+ }>;
537
+ callTool<T = any>(name: string, args?: Record<string, unknown>): Promise<CardMcpToolResult<T>>;
538
+ listOffers(): Promise<CardMcpToolResult<any>>;
539
+ getOffer(offerId: string): Promise<CardMcpToolResult<any>>;
540
+ /** Claiming shares what a manual owner claim shares today (email + Card profile, future updates included) - the owner approved this when linking the agent. */
541
+ claimOffer(offerId: string): Promise<CardMcpToolResult<any>>;
542
+ clearCredentials(): void;
543
+ }
544
+ /**
545
+ * Canonical payload the Card backend asks the agent to sign when the owner
546
+ * links their delegation to an operator agent identity (wallet, optionally an
547
+ * ERC-8004 agent id). Mirrors linkPayload in the edge card agentLinks module;
548
+ * the challenge endpoint also returns the exact string to sign, so prefer
549
+ * signing that value and use this only to cross-check it.
550
+ */
551
+ export declare function buildAgentLinkPayload(input: {
552
+ delegationId: string;
553
+ agentAddress: string;
554
+ network: string;
555
+ agentId: string | null;
556
+ nonce: string;
557
+ expiresAt: string;
558
+ }): string;
559
+ /**
560
+ * Signs an agent-link payload with the owner-approved delegation key
561
+ * (Ed25519, base64url). The operator agent wallet signs the same payload with
562
+ * any EIP-191 signer (for example viem's signMessage); the backend verifies
563
+ * both proofs and, for an on-chain agent id, the registry ownerOf match.
564
+ */
565
+ export declare function signAgentLinkPayload(privateKey: string, payload: string): string;