@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/docs/CHAINS.md CHANGED
@@ -119,7 +119,7 @@ Wallet history is a bounded sample. See [TRUST-MODEL.md](./TRUST-MODEL.md#wallet
119
119
 
120
120
  ```typescript
121
121
  // At initialization (default for all calls)
122
- const valiron = new ValironSDK({ chain: "monad" });
122
+ const valiron = new ValironSDK({ apiKey: process.env.VALIRON_API_KEY, chain: "monad" });
123
123
 
124
124
  // Per-call override
125
125
  const route = await valiron.checkAgent("25459", { chain: "arbitrum" });
@@ -129,7 +129,7 @@ const route = await valiron.checkAgent("25459", { chain: "arbitrum" });
129
129
 
130
130
  ```bash
131
131
  # Query parameter
132
- curl https://valiron-edge-proxy.onrender.com/operator/agent/25459?chain=monad
132
+ curl -H "Authorization: Bearer $VALIRON_API_KEY" https://valiron-edge-proxy.onrender.com/operator/agent/25459?chain=monad
133
133
  ```
134
134
 
135
135
  ## Querying Supported Chains
@@ -142,7 +142,7 @@ Returns:
142
142
  ```json
143
143
  {
144
144
  "status": "ok",
145
- "supportedChains": ["ethereum", "monad", "arbitrum", ...],
145
+ "supportedChains": ["ethereum", "monad", "arbitrum"],
146
146
  "defaultChain": "ethereum"
147
147
  }
148
148
  ```
@@ -0,0 +1,196 @@
1
+ # Valiron trust gate on Cloudflare Workers
2
+
3
+ Run the general Valiron trust gate - the same middleware semantics as the
4
+ Express, Fastify, Next.js and generic adapters in `@valiron/sdk` - as a
5
+ Cloudflare Worker in front of your API. Agents are identified, ownership is
6
+ proven per request, the live trust profile is evaluated, and only allowed
7
+ requests reach your origin.
8
+
9
+ This document describes the current source contract on this branch. Package
10
+ publication and production deployment are separate release steps; confirm the
11
+ installed `@valiron/sdk` exposes the `edge-gate` subpath and the
12
+ `examples/cloudflare-gate-worker` template before building on them.
13
+
14
+ ## Who this is for, and whether it is required
15
+
16
+ This is an optional integration for a business that sells or exposes an API and
17
+ wants agent ownership and trust checks before requests reach its backend. The
18
+ business runs the Worker in its Cloudflare account, chooses the routes, score
19
+ threshold and trust signals, and protects its origin from direct access. The
20
+ backend can use any language. Valiron still serves profile reads and sandbox
21
+ evaluation; the default verifier also reads the current owner from Valiron.
22
+
23
+ Cloudflare is not required to use Valiron. Businesses can keep Node middleware,
24
+ call the API from their own enforcement code, or use this Worker. Use one
25
+ trust-gate layer per protected route. Agents calling those routes must identify
26
+ themselves and sign fresh requests. The gate verifies agent ownership and
27
+ applies the business's trust policy. Customer permissions, benefits and payment
28
+ authorization remain application responsibilities.
29
+
30
+
31
+ ## Components
32
+
33
+ - **`@valiron/sdk/edge-gate`.** A WebCrypto-only SDK subpath that loads in
34
+ Workers and other edge runtimes without Node APIs: `runValironEdgeGate`,
35
+ `createEdgeIdentityVerifier`, `stripEdgeGateHeaders`,
36
+ `edgeSha256RequestDigest`, and an edge-safe `ValironClient` re-export. The
37
+ gate decision itself is computed by the exact same `valironGateCheck`
38
+ implementation the Node middleware uses, so statuses, error bodies, cache
39
+ behavior and allow/deny semantics are identical.
40
+ - **Worker template.** `packages/valiron-sdk/examples/cloudflare-gate-worker/`
41
+ is a deployable Worker: identity extraction, per-request proof
42
+ verification, live profile evaluation, trusted-header strip/set, and
43
+ path scoping. `packages/valiron-sdk/test/edge-gate-worker/` is a committed
44
+ miniflare (workerd) regression harness for the template.
45
+
46
+ ## Request flow
47
+
48
+ ```text
49
+ Agent request
50
+ -> Worker reads x-agent-id / x-agent-address
51
+ -> Worker verifies the request-bound proof (before any decision, cache hits included)
52
+ -> Worker evaluates the live profile through the Valiron operator API
53
+ -> allow: strip client x-valiron-gate-*, set trusted decision headers, forward to origin
54
+ -> deny: 401 / 403 / 503 response; the origin never runs
55
+ ```
56
+
57
+ Decision semantics are the Node gate's, unchanged:
58
+
59
+ | Condition | Status | Code |
60
+ | --- | --- | --- |
61
+ | No identity headers | 401 | `MISSING_AGENT_IDENTITY` |
62
+ | Missing or invalid request proof | 401 | `AGENT_OWNERSHIP_REQUIRED` |
63
+ | Route not production-eligible, or score below `minScore` | 403 | `TRUST_GATE_DENIED` |
64
+ | New agent (no reputation yet) | 403 | `AGENT_EVALUATION_PENDING` |
65
+ | Evaluation error / API unreachable (after a successful proof) | 503 | `TRUST_GATE_UNAVAILABLE` (fail closed) |
66
+ | API unreachable during hosted proof verification | 401 | `AGENT_OWNERSHIP_REQUIRED` (fail closed) |
67
+
68
+ Allow requires routing `prod` or `prod_throttled` AND the combined score at
69
+ or above `minScore` (default 65). A new agent's denial triggers a background
70
+ sandbox evaluation, exactly like the Node gate.
71
+
72
+ Outage behavior depends on where the failure lands. The default wallet
73
+ verifier checks signatures locally, but first reads the current owner from
74
+ Valiron; failure of that lookup returns 401 `AGENT_OWNERSHIP_REQUIRED`.
75
+ Hosted verification failures also return 401. If proof verification succeeds
76
+ and the later trust-profile evaluation fails, the gate returns 503
77
+ `TRUST_GATE_UNAVAILABLE`. The origin is never touched on failure.
78
+
79
+ ## Deploy the Worker template
80
+
81
+ Copy `packages/valiron-sdk/examples/cloudflare-gate-worker/`, configure
82
+ `wrangler.toml`, and `wrangler deploy`. Required: `VALIRON_ENDPOINT` (the
83
+ Valiron operator API), `VALIRON_API_KEY` (secret - profile routes are
84
+ operator-authenticated) and the `VALIRON_NONCE_STORE` Durable Object binding
85
+ (durable replay protection; the gate fails closed without it). Recommended:
86
+ `VALIRON_ORIGIN_SECRET` (secret forwarded to the origin as
87
+ `x-valiron-gate-secret`). Optional: `VALIRON_MIN_SCORE`,
88
+ `VALIRON_GATE_PATHS`, `VALIRON_TRUST_SIGNALS`, `VALIRON_CACHE_TTL_MS`,
89
+ `VALIRON_VERIFIER` (`hosted` selects hosted ANS verification - see below),
90
+ `VALIRON_PUBLIC_ORIGIN` (audience for the hosted variant only).
91
+
92
+ ## Required trust boundary
93
+
94
+ 1. **The origin must reject anything that did not pass the gate.** The
95
+ Worker strips client-supplied `x-valiron-gate-*` headers and sets its own
96
+ (`x-valiron-gate-decision`, `-agent-id`, `-score`, `-tier`,
97
+ `-risk-level`, `-route`), and forwards `x-valiron-gate-secret` when
98
+ `VALIRON_ORIGIN_SECRET` is set. The origin must reject requests missing
99
+ that secret, or use an exclusive authenticated origin path (private Tunnel or custom
100
+ per-zone mTLS). Cloudflare IP allowlisting or a shared origin-pull
101
+ certificate alone does not identify this Worker. If the origin is reachable
102
+ directly, the gate is bypassable.
103
+ 2. **Replay protection is durable.** The default verifier refuses to run
104
+ without the `VALIRON_NONCE_STORE` Durable Object binding (503,
105
+ fail-closed). Nonces are claimed atomically in one named Durable Object
106
+ with a 121-second TTL matching the proof validity window, so a proof
107
+ replayed against any isolate or after a restart is rejected. The SDK's
108
+ in-memory `MemoryNonceStore` is for local tests only.
109
+ 3. **Enforce the gate at one layer per route.** Do not run the origin's
110
+ `createValironGate` on the same routes the Worker gates; requests would be
111
+ evaluated twice.
112
+ 4. **Header claims alone never authorize.** Every identified request carries
113
+ a fresh proof or is denied `401 AGENT_OWNERSHIP_REQUIRED` before trust
114
+ evaluation. The default verifier first reads the current registry owner
115
+ from a live profile to determine which key must sign.
116
+ 5. **The profile cache is per isolate** (default 30000 ms, matching the Node
117
+ gate's per-process default). It is not shared across isolates or colos.
118
+ The ownership proof is verified on every request regardless of cache.
119
+
120
+ ## Request-bound proofs
121
+
122
+ The template's default `verifyRequest` is `createEdgeWalletProofVerifier`:
123
+ application-owned verification of the `valiron-wallet-request-v2` signed
124
+ request scheme (see `SECURITY-UPGRADE-2026-10-31.md`), the same scheme the
125
+ hosted API enforces server-side. Agents send `x-agent-timestamp`,
126
+ `x-agent-nonce` and `x-agent-request-signature`: the registry owner's EIP-191
127
+ (EVM) or Ed25519 (Solana) signature over the canonical payload binding the
128
+ agent id, chain, method, the exact public URL, a canonical JSON body digest,
129
+ a millisecond timestamp (60-second window) and a fresh nonce. The Worker
130
+ verifies the signature against the agent's current registry owner wallet
131
+ from the live profile, then consumes the nonce in the Durable Object; a
132
+ replayed proof is rejected for 121 seconds. Key-based web2 agents send
133
+ `x-agent-address` and sign with that address's key (chain `key`). The sign
134
+ side ships in the SDK's hosted clients (`signRequest`) and the recipe is in
135
+ the template README.
136
+
137
+ `VALIRON_VERIFIER=hosted` switches to `createEdgeIdentityVerifier`: Valiron's
138
+ hosted ANS provider-identity verification (`POST /operator/identity/verify`,
139
+ `x-valiron-identity-*` headers from `createAgentIdentityProof`, canonical
140
+ `valiron-agent-request-v2` payload). That variant requires Valiron backend
141
+ ANS configuration and a registered `ans://` identity, and was not exercised
142
+ in the template's isolated test deployment.
143
+
144
+ Operators with their own request-signing scheme replace `verifyRequest` in
145
+ `worker.ts` - the same application-owned verifier contract as the Node
146
+ middleware (`verifyAgentRequest` in the end-to-end guide). The verifier must
147
+ bind identity, method, full URL and body, enforce freshness, and reject
148
+ replayed nonces in durable shared storage.
149
+
150
+ ## What this is not
151
+
152
+ - It does not cache or replicate trust scores at the edge beyond the short
153
+ per-isolate profile cache the Node middleware already has; the operator API
154
+ remains the decision source.
155
+ - It grants no Cloudflare verified-agent status and changes no scoring or
156
+ privacy behavior.
157
+
158
+ ## Verification
159
+
160
+ - SDK suite: `pnpm --filter @valiron/sdk test` includes
161
+ `test/validate-edge-gate.mjs` (decision parity with the Node gate, digest
162
+ parity with Node `sha256RequestDigest`, header constants, verifier
163
+ binding rules).
164
+ - Workers-runtime harness: `packages/valiron-sdk/test/edge-gate-worker/`
165
+ (miniflare; allow/deny, both signing chains through the default
166
+ wallet-proof verifier with real signatures, Durable Object replay
167
+ rejection, tampered/stale/wrong-key rejection, fail-closed without the
168
+ nonce-store binding, origin-secret forwarding, hosted-variant coverage,
169
+ header stripping, path scoping, profile cache, digest recomputation). This
170
+ is hermetic coverage with a mocked API; it complements a live deployment
171
+ test and does not replace one.
172
+
173
+
174
+ ## Origin targeting and nonce expiry
175
+
176
+ For a standalone `workers.dev` deployment, set `VALIRON_ORIGIN` to the fixed
177
+ HTTPS origin, for example `https://origin.example.com`. It must have no
178
+ credentials, path, query, or fragment and must differ from the incoming
179
+ origin. Leave it unset only when Cloudflare zone-route origin resolution is
180
+ configured. The Worker verifies the original incoming URL and body before
181
+ rewriting the destination. Path and query are preserved; caller-provided
182
+ URLs and host-like paths cannot select another host. Redirects from an
183
+ explicit origin are returned without following them, so trusted headers and
184
+ the origin secret are never sent to a redirect destination. Out-of-scope
185
+ paths are forwarded without the gate secret and cannot access protected
186
+ origin routes.
187
+
188
+ Durable Object nonce claims use a storage transaction and an explicit expiry
189
+ timestamp, not KV-only `expirationTtl`. A scheduled alarm removes expired
190
+ entries. Concurrent claims have exactly one winner; a claimed nonce stays
191
+ blocked until its 121-second expiry, including across isolates. Expiry makes
192
+ the nonce reusable but does not make an old signature fresh.
193
+
194
+ The template registers sandbox-trigger promises with `ExecutionContext.waitUntil` so pending evaluations can continue after the denial response. All inbound `x-valiron-gate-*` headers, including a forged origin secret, are stripped before forwarding.
195
+
196
+ Wallet proof freshness can be tightened with `maxAgeMs`; values above 60,000 ms are rejected. Durable nonce retention is 121 seconds to cover the entire 60-second past/future validity horizon, including its boundary.