@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.
- package/README.md +11 -148
- package/bin/valiron-card.mjs +211 -0
- package/dist/card.d.ts +565 -0
- package/dist/card.js +722 -0
- package/dist/clients.d.ts +6 -6
- package/dist/clients.js +33 -26
- package/dist/edge.d.ts +87 -0
- package/dist/edge.js +164 -0
- package/dist/edgeGate.d.ts +146 -0
- package/dist/edgeGate.js +230 -0
- package/dist/edgeGateVerify.d.ts +146 -0
- package/dist/edgeGateVerify.js +243 -0
- package/dist/identity.d.ts +6 -0
- package/dist/identity.js +6 -1
- package/dist/identityMiddleware.d.ts +8 -0
- package/dist/identityMiddleware.js +12 -4
- package/dist/index.d.ts +5 -10
- package/dist/index.js +4 -10
- package/dist/middleware.d.ts +20 -9
- package/dist/middleware.js +47 -19
- package/dist/operatorSdk.d.ts +11 -2
- package/dist/operatorSdk.js +13 -7
- package/dist/types.d.ts +14 -4
- package/docs/AGENT-READY-APIS.md +3 -181
- package/docs/API-REFERENCE.md +94 -546
- package/docs/CHAINS.md +3 -3
- package/docs/CLOUDFLARE-GATE-MIDDLEWARE.md +196 -0
- package/docs/CRAWLER-PROTECTION.md +545 -0
- package/docs/DASHBOARD.md +55 -23
- package/docs/E2E-MIDDLEWARE-INTEGRATION.md +19 -13
- package/docs/ERRORS.md +29 -19
- package/docs/IDENTITY.md +16 -14
- package/docs/PROVIDER-IDENTITY.md +15 -5
- package/docs/QUICKSTART.md +40 -24
- package/docs/README.md +21 -4
- package/docs/SANDBOX.md +7 -1
- package/docs/SDK-REFERENCE.md +115 -52
- package/docs/SECURITY-UPGRADE-2026-10-31.md +98 -0
- package/docs/TRUST-MODEL.md +14 -11
- package/docs/llms.txt +70 -302
- package/examples/cloudflare-gate-worker/README.md +198 -0
- package/examples/cloudflare-gate-worker/package.json +20 -0
- package/examples/cloudflare-gate-worker/worker.ts +309 -0
- package/examples/cloudflare-gate-worker/wrangler.toml +46 -0
- package/examples/cloudflare-worker/package.json +8 -0
- package/examples/cloudflare-worker/worker.ts +495 -0
- package/examples/cloudflare-worker/wrangler.toml +57 -0
- package/package.json +35 -5
- package/docs/MCP.md +0 -50
package/dist/edgeGate.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Valiron trust gate for Cloudflare Workers and other WebCrypto-only edge
|
|
4
|
+
* runtimes: the same general trust gate the Node adapters in ./middleware
|
|
5
|
+
* expose (createValironGate, valironFastifyPlugin, createValironNextMiddleware,
|
|
6
|
+
* valironGateCheck), packaged so it loads inside workerd.
|
|
7
|
+
*
|
|
8
|
+
* Semantics are identical to the Node middleware:
|
|
9
|
+
*
|
|
10
|
+
* - identity headers: `x-agent-id` (on-chain ERC-8004 / Solana) or
|
|
11
|
+
* `x-agent-address` / `x-agent-key-hash` (key-based web2 agents)
|
|
12
|
+
* - a fresh, request-bound ownership proof is required before EVERY
|
|
13
|
+
* decision, including profile-cache hits (`verifyRequest`)
|
|
14
|
+
* - live profile evaluation through the public operator API
|
|
15
|
+
* (getAgentProfile / getKeyAgentProfile), with the same client-side
|
|
16
|
+
* profile cache (default 30s, per isolate at the edge)
|
|
17
|
+
* - allow only when operator routing is `prod` / `prod_throttled` AND the
|
|
18
|
+
* combined score meets `minScore` (default 65)
|
|
19
|
+
* - 401 MISSING_AGENT_IDENTITY, 401 AGENT_OWNERSHIP_REQUIRED, 403
|
|
20
|
+
* TRUST_GATE_DENIED or AGENT_EVALUATION_PENDING, 503 fail-closed on
|
|
21
|
+
* evaluation errors
|
|
22
|
+
*
|
|
23
|
+
* This module imports only ./middleware and ./clients at runtime (both free
|
|
24
|
+
* of Node APIs), so the `@valiron/sdk/edge-gate` subpath loads in Workers.
|
|
25
|
+
* The Pass-specific edge verifier is a separate subpath (`@valiron/sdk/edge`)
|
|
26
|
+
* and is not required for the general trust gate.
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.webCryptoSolanaVerifier = exports.verifyWalletRequestProof = exports.createEdgeWalletProofVerifier = exports.buildWalletProofPayload = exports.base64urlToBytes = exports.base58Decode = exports.MemoryNonceStore = exports.EDGE_IDENTITY_HEADERS = exports.VALIRON_EDGE_GATE_HEADERS = exports.ValironClient = void 0;
|
|
30
|
+
exports.stripEdgeGateHeaders = stripEdgeGateHeaders;
|
|
31
|
+
exports.runValironEdgeGate = runValironEdgeGate;
|
|
32
|
+
exports.edgeSha256RequestDigest = edgeSha256RequestDigest;
|
|
33
|
+
exports.createEdgeIdentityVerifier = createEdgeIdentityVerifier;
|
|
34
|
+
const middleware_1 = require("./middleware");
|
|
35
|
+
const clients_1 = require("./clients");
|
|
36
|
+
Object.defineProperty(exports, "ValironClient", { enumerable: true, get: function () { return clients_1.ValironClient; } });
|
|
37
|
+
/** Headers the edge sets on the outbound request after an allow decision. */
|
|
38
|
+
exports.VALIRON_EDGE_GATE_HEADERS = [
|
|
39
|
+
"x-valiron-gate-decision",
|
|
40
|
+
"x-valiron-gate-agent-id",
|
|
41
|
+
"x-valiron-gate-score",
|
|
42
|
+
"x-valiron-gate-tier",
|
|
43
|
+
"x-valiron-gate-risk-level",
|
|
44
|
+
"x-valiron-gate-route",
|
|
45
|
+
];
|
|
46
|
+
function defaultExtractAgentId(request) {
|
|
47
|
+
return request.headers.get("x-agent-id");
|
|
48
|
+
}
|
|
49
|
+
function defaultExtractAgentAddress(request) {
|
|
50
|
+
return (request.headers.get("x-agent-address") ||
|
|
51
|
+
request.headers.get("x-agent-key-hash"));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Remove every client-supplied trusted-decision header from an inbound
|
|
55
|
+
* request. Run this on EVERY request the edge fronts (gated or not) before
|
|
56
|
+
* forwarding, so callers can never inject a gate decision. Only meaningful
|
|
57
|
+
* when the origin cannot be reached directly (see the template README).
|
|
58
|
+
*/
|
|
59
|
+
function stripEdgeGateHeaders(request) {
|
|
60
|
+
const headers = new Headers(request.headers);
|
|
61
|
+
const names = [];
|
|
62
|
+
headers.forEach((_value, name) => names.push(name));
|
|
63
|
+
for (const name of names) {
|
|
64
|
+
if (name.toLowerCase().startsWith("x-valiron-gate-"))
|
|
65
|
+
headers.delete(name);
|
|
66
|
+
}
|
|
67
|
+
return new Request(request, { headers });
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Run the Valiron trust gate for one incoming request.
|
|
71
|
+
*
|
|
72
|
+
* On allow: returns the GateResult plus the trusted headers to set on the
|
|
73
|
+
* outbound origin request. On any block: returns the exact response the
|
|
74
|
+
* runtime must send back, with the same status codes and error bodies as
|
|
75
|
+
* the Node middleware adapters.
|
|
76
|
+
*/
|
|
77
|
+
async function runValironEdgeGate(request, config) {
|
|
78
|
+
const extractAgentId = config.extractAgentId || defaultExtractAgentId;
|
|
79
|
+
const extractAgentAddress = config.extractAgentAddress || defaultExtractAgentAddress;
|
|
80
|
+
const agentId = extractAgentId(request);
|
|
81
|
+
const agentAddress = agentId ? null : extractAgentAddress(request);
|
|
82
|
+
const gate = await (0, middleware_1.valironGateCheck)(config.sdk, {
|
|
83
|
+
agentId,
|
|
84
|
+
agentAddress,
|
|
85
|
+
minScore: config.minScore,
|
|
86
|
+
trustSignals: config.trustSignals,
|
|
87
|
+
cacheTtlMs: config.cacheTtlMs,
|
|
88
|
+
waitUntil: config.waitUntil,
|
|
89
|
+
verifyRequest: config.verifyRequest
|
|
90
|
+
? (identifier) => config.verifyRequest(request, identifier)
|
|
91
|
+
: undefined,
|
|
92
|
+
});
|
|
93
|
+
if (gate.ok && gate.allow && gate.result) {
|
|
94
|
+
const result = gate.result;
|
|
95
|
+
return {
|
|
96
|
+
allowed: true,
|
|
97
|
+
result,
|
|
98
|
+
headers: {
|
|
99
|
+
"x-valiron-gate-decision": "allow",
|
|
100
|
+
"x-valiron-gate-agent-id": result.agentId,
|
|
101
|
+
"x-valiron-gate-score": String(result.score),
|
|
102
|
+
"x-valiron-gate-tier": String(result.tier),
|
|
103
|
+
"x-valiron-gate-risk-level": String(result.riskLevel),
|
|
104
|
+
"x-valiron-gate-route": String(result.route),
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
allowed: false,
|
|
110
|
+
response: Response.json(gate.body ?? { code: "TRUST_GATE_UNAVAILABLE" }, {
|
|
111
|
+
status: gate.status,
|
|
112
|
+
}),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// ─── Hosted provider-identity verifier ──────────────────────────────────────
|
|
116
|
+
//
|
|
117
|
+
// Header names mirror AGENT_IDENTITY_HEADERS in ./identity. They are
|
|
118
|
+
// re-declared here (not imported) because ./identity uses Node crypto, which
|
|
119
|
+
// workerd does not provide. test/validate-edge-gate.mjs asserts the two stay
|
|
120
|
+
// in sync.
|
|
121
|
+
exports.EDGE_IDENTITY_HEADERS = {
|
|
122
|
+
provider: "x-valiron-identity-provider",
|
|
123
|
+
agentId: "x-valiron-identity-agent-id",
|
|
124
|
+
signature: "x-valiron-identity-signature",
|
|
125
|
+
timestamp: "x-valiron-identity-timestamp",
|
|
126
|
+
nonce: "x-valiron-identity-nonce",
|
|
127
|
+
keyId: "x-valiron-identity-key-id",
|
|
128
|
+
};
|
|
129
|
+
/** WebCrypto equivalent of sha256RequestDigest in ./identity: `sha256:<hex>`. */
|
|
130
|
+
async function edgeSha256RequestDigest(body) {
|
|
131
|
+
const bytes = body == null
|
|
132
|
+
? new Uint8Array(0)
|
|
133
|
+
: typeof body === "string"
|
|
134
|
+
? new TextEncoder().encode(body)
|
|
135
|
+
: body instanceof ArrayBuffer
|
|
136
|
+
? new Uint8Array(body)
|
|
137
|
+
: body;
|
|
138
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
139
|
+
const hex = Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
140
|
+
return `sha256:${hex}`;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Out-of-the-box `verifyRequest` for the edge gate: Valiron's hosted
|
|
144
|
+
* provider-identity verification (POST /operator/identity/verify), the same
|
|
145
|
+
* verification the Node identity middleware (createValironIdentityGate)
|
|
146
|
+
* performs server-side.
|
|
147
|
+
*
|
|
148
|
+
* The agent sends `x-valiron-identity-*` headers produced by the SDK's
|
|
149
|
+
* createAgentIdentityProof / agentIdentityProofHeaders, signing the canonical
|
|
150
|
+
* `valiron-agent-request-v2` payload with the private key bound to its
|
|
151
|
+
* provider identity. This verifier:
|
|
152
|
+
*
|
|
153
|
+
* - requires the proof's agentId to equal the gate identifier;
|
|
154
|
+
* - recomputes the request body digest at the edge (WebCrypto), exactly as
|
|
155
|
+
* the Node gate recomputes it server-side;
|
|
156
|
+
* - binds method and the exact public URL (same trusted origin rule as the
|
|
157
|
+
* Node identity middleware);
|
|
158
|
+
* - asks Valiron to verify the signature, freshness and replay state, and
|
|
159
|
+
* returns true only for identityStatus "verified".
|
|
160
|
+
*
|
|
161
|
+
* Operators with their own request-signing scheme can pass their own
|
|
162
|
+
* verifyRequest to runValironEdgeGate instead; the gate contract is unchanged.
|
|
163
|
+
*/
|
|
164
|
+
function createEdgeIdentityVerifier(config) {
|
|
165
|
+
return async (request, identifier) => {
|
|
166
|
+
const read = (name) => request.headers.get(name) ?? undefined;
|
|
167
|
+
const proof = {
|
|
168
|
+
provider: read(exports.EDGE_IDENTITY_HEADERS.provider),
|
|
169
|
+
agentId: read(exports.EDGE_IDENTITY_HEADERS.agentId),
|
|
170
|
+
signature: read(exports.EDGE_IDENTITY_HEADERS.signature),
|
|
171
|
+
timestamp: read(exports.EDGE_IDENTITY_HEADERS.timestamp),
|
|
172
|
+
nonce: read(exports.EDGE_IDENTITY_HEADERS.nonce),
|
|
173
|
+
keyId: read(exports.EDGE_IDENTITY_HEADERS.keyId),
|
|
174
|
+
};
|
|
175
|
+
if (!proof.provider ||
|
|
176
|
+
!proof.agentId ||
|
|
177
|
+
!proof.signature ||
|
|
178
|
+
!proof.timestamp ||
|
|
179
|
+
!proof.nonce) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
// The proof must bind the exact identity the gate is evaluating.
|
|
183
|
+
if (proof.agentId !== identifier)
|
|
184
|
+
return false;
|
|
185
|
+
const origin = config.origin ?? config.audience;
|
|
186
|
+
let url;
|
|
187
|
+
try {
|
|
188
|
+
const parsed = new URL(request.url);
|
|
189
|
+
if (parsed.origin !== new URL(origin).origin)
|
|
190
|
+
return false;
|
|
191
|
+
url = parsed.toString();
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
const body = ["GET", "HEAD"].includes(request.method)
|
|
197
|
+
? undefined
|
|
198
|
+
: new Uint8Array(await request.clone().arrayBuffer());
|
|
199
|
+
const requestDigest = await edgeSha256RequestDigest(body);
|
|
200
|
+
const payload = {
|
|
201
|
+
provider: proof.provider,
|
|
202
|
+
agentId: proof.agentId,
|
|
203
|
+
signature: proof.signature,
|
|
204
|
+
timestamp: proof.timestamp,
|
|
205
|
+
nonce: proof.nonce,
|
|
206
|
+
requestDigest,
|
|
207
|
+
audience: config.audience,
|
|
208
|
+
method: request.method.toUpperCase(),
|
|
209
|
+
url,
|
|
210
|
+
...(proof.keyId ? { keyId: proof.keyId } : {}),
|
|
211
|
+
};
|
|
212
|
+
try {
|
|
213
|
+
const identity = await config.sdk.verifyAgentIdentity(payload);
|
|
214
|
+
return identity.identityStatus === "verified";
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
// Application-owned wallet proof verification (valiron-wallet-request-v2):
|
|
222
|
+
// the default verifier for ordinary non-ANS agents at the edge.
|
|
223
|
+
var edgeGateVerify_1 = require("./edgeGateVerify");
|
|
224
|
+
Object.defineProperty(exports, "MemoryNonceStore", { enumerable: true, get: function () { return edgeGateVerify_1.MemoryNonceStore; } });
|
|
225
|
+
Object.defineProperty(exports, "base58Decode", { enumerable: true, get: function () { return edgeGateVerify_1.base58Decode; } });
|
|
226
|
+
Object.defineProperty(exports, "base64urlToBytes", { enumerable: true, get: function () { return edgeGateVerify_1.base64urlToBytes; } });
|
|
227
|
+
Object.defineProperty(exports, "buildWalletProofPayload", { enumerable: true, get: function () { return edgeGateVerify_1.buildWalletProofPayload; } });
|
|
228
|
+
Object.defineProperty(exports, "createEdgeWalletProofVerifier", { enumerable: true, get: function () { return edgeGateVerify_1.createEdgeWalletProofVerifier; } });
|
|
229
|
+
Object.defineProperty(exports, "verifyWalletRequestProof", { enumerable: true, get: function () { return edgeGateVerify_1.verifyWalletRequestProof; } });
|
|
230
|
+
Object.defineProperty(exports, "webCryptoSolanaVerifier", { enumerable: true, get: function () { return edgeGateVerify_1.webCryptoSolanaVerifier; } });
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application-owned wallet proof verification for the Cloudflare trust gate.
|
|
3
|
+
*
|
|
4
|
+
* This is the edge (workerd-safe) verify side of the `valiron-wallet-request-v2`
|
|
5
|
+
* signed-request contract documented in docs/SECURITY-UPGRADE-2026-10-31.md and
|
|
6
|
+
* implemented server-side in services/edge-proxy requestProof.ts. It lets an API
|
|
7
|
+
* seller verify an ordinary (non-ANS) agent's ownership proof locally in a
|
|
8
|
+
* Cloudflare Worker, bound to the agent's current registry owner wallet from the
|
|
9
|
+
* live Valiron profile.
|
|
10
|
+
*
|
|
11
|
+
* Contract mirrored exactly:
|
|
12
|
+
* - headers: x-agent-timestamp (13-digit ms), x-agent-nonce (24-128 URL-safe
|
|
13
|
+
* chars), x-agent-request-signature
|
|
14
|
+
* - payload lines joined with "\n":
|
|
15
|
+
* valiron-wallet-request-v2
|
|
16
|
+
* agent:<exact-request-agent-id>
|
|
17
|
+
* chain:<network string, as resolved for the agent>
|
|
18
|
+
* method:<UPPERCASE method>
|
|
19
|
+
* url:<absolute public request URL (request.url verbatim)>
|
|
20
|
+
* body-sha256:<lowercase hex sha256 of ("" for no body, else
|
|
21
|
+
* JSON.stringify(JSON.parse(rawBody)))>
|
|
22
|
+
* timestamp:<ms>
|
|
23
|
+
* nonce:<fresh nonce>
|
|
24
|
+
* - timestamp window: |now - timestamp| <= 60_000 ms
|
|
25
|
+
* - nonce consumed exactly once per wallet+network+nonce scope
|
|
26
|
+
* (sha256 hex of `<wallet|wallet.toLowerCase()>:<network>:<nonce>`), TTL 121s
|
|
27
|
+
* - EVM: EIP-191 personal_sign over the UTF-8 payload, recovered address must
|
|
28
|
+
* equal the registry owner wallet (case-insensitive)
|
|
29
|
+
* - Solana: Ed25519 over the UTF-8 payload, signature base64url, wallet is the
|
|
30
|
+
* base58 public key
|
|
31
|
+
*
|
|
32
|
+
* Replay protection is MANDATORY: pass a durable `nonceStore`. In Cloudflare
|
|
33
|
+
* Workers use a Durable Object (see examples/cloudflare-gate-worker). Without a
|
|
34
|
+
* store this module fails closed.
|
|
35
|
+
*/
|
|
36
|
+
/** Durable, atomic nonce claimer. Implementations must be atomic across all
|
|
37
|
+
* isolates that serve the gate (a Durable Object or equivalent shared store). */
|
|
38
|
+
export interface NonceStore {
|
|
39
|
+
/** Returns true exactly once per `scope` within `ttlSeconds`; false on replay
|
|
40
|
+
* or when the store is unavailable (callers fail closed). */
|
|
41
|
+
claim(scope: string, ttlSeconds: number): Promise<boolean>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* TEST/LOCAL ONLY. Atomic within a single isolate; NOT safe as a production
|
|
45
|
+
* replay control across Cloudflare isolates or restarts. The deployable
|
|
46
|
+
* template binds a Durable Object instead.
|
|
47
|
+
*/
|
|
48
|
+
export declare class MemoryNonceStore implements NonceStore {
|
|
49
|
+
private seen;
|
|
50
|
+
claim(scope: string, ttlSeconds: number): Promise<boolean>;
|
|
51
|
+
}
|
|
52
|
+
/** Chain-specific cryptographic primitives, supplied by the integrating app so
|
|
53
|
+
* the SDK stays dependency-free. The Cloudflare template ships ready-made
|
|
54
|
+
* adapters (viem for EVM, WebCrypto Ed25519 for Solana). */
|
|
55
|
+
export interface WalletProofCrypto {
|
|
56
|
+
/** EIP-191: recover the signer address of `message` (UTF-8) from a 0x-hex
|
|
57
|
+
* signature. Return lowercase hex. */
|
|
58
|
+
recoverEvmAddress(message: string, signature: string): Promise<string>;
|
|
59
|
+
/** Ed25519: verify a base64url signature over the UTF-8 `message` against the
|
|
60
|
+
* base58 Solana public key. */
|
|
61
|
+
verifySolanaSignature(message: string, signatureBase64url: string, publicKeyBase58: string): Promise<boolean>;
|
|
62
|
+
}
|
|
63
|
+
export interface WalletProofVerificationConfig {
|
|
64
|
+
/** Exact agent id as the request claims it (gate-resolved identifier). */
|
|
65
|
+
agentId: string;
|
|
66
|
+
/** Network/chain string the proof signs over - the same string the gate used
|
|
67
|
+
* to resolve the agent (e.g. the profile's chain.network value). */
|
|
68
|
+
network: string;
|
|
69
|
+
/** Current registry owner wallet from the live profile (EVM 0x address, or
|
|
70
|
+
* base58 Solana public key). For key-based agents: the agent's own address. */
|
|
71
|
+
registryWallet: string;
|
|
72
|
+
/** True when `network` is a Solana network (wallet is base58, Ed25519). */
|
|
73
|
+
isSolana: boolean;
|
|
74
|
+
/** Durable nonce store. REQUIRED - verification fails closed without it. */
|
|
75
|
+
nonceStore: NonceStore;
|
|
76
|
+
crypto: WalletProofCrypto;
|
|
77
|
+
/** Timestamp tolerance in ms (default 60_000, matching the server). */
|
|
78
|
+
maxAgeMs?: number;
|
|
79
|
+
/** Injectable clock for tests. */
|
|
80
|
+
now?: number;
|
|
81
|
+
}
|
|
82
|
+
export type WalletProofResult = {
|
|
83
|
+
ok: true;
|
|
84
|
+
} | {
|
|
85
|
+
ok: false;
|
|
86
|
+
reason: string;
|
|
87
|
+
};
|
|
88
|
+
declare function base64urlToBytes(value: string): Uint8Array;
|
|
89
|
+
/** Build the exact canonical payload the agent signed. Exported for tests and
|
|
90
|
+
* for the sign-side recipe; the layout must stay byte-identical with the
|
|
91
|
+
* server implementation. */
|
|
92
|
+
export declare function buildWalletProofPayload(input: {
|
|
93
|
+
agentId: string;
|
|
94
|
+
network: string;
|
|
95
|
+
method: string;
|
|
96
|
+
url: string;
|
|
97
|
+
bodySha256: string;
|
|
98
|
+
timestamp: string;
|
|
99
|
+
nonce: string;
|
|
100
|
+
}): string;
|
|
101
|
+
/**
|
|
102
|
+
* Verify one inbound request's wallet proof headers against the live registry
|
|
103
|
+
* owner. `rawBody` is the exact request body text ("" when none); JSON bodies
|
|
104
|
+
* are canonicalized exactly as the server does (parse then stringify), and a
|
|
105
|
+
* body that fails to parse can never produce a valid proof.
|
|
106
|
+
*/
|
|
107
|
+
export declare function verifyWalletRequestProof(request: {
|
|
108
|
+
method: string;
|
|
109
|
+
url: string;
|
|
110
|
+
headers: {
|
|
111
|
+
get(name: string): string | null;
|
|
112
|
+
};
|
|
113
|
+
}, rawBody: string, config: WalletProofVerificationConfig): Promise<WalletProofResult>;
|
|
114
|
+
/** Decode a base58 Solana public key to its 32 raw bytes. Shipped so templates
|
|
115
|
+
* need no dependency for the Ed25519 path. Throws on invalid input. */
|
|
116
|
+
export declare function base58Decode(value: string): Uint8Array;
|
|
117
|
+
/** WebCrypto Ed25519 adapter for Solana wallets - ready to use in Workers.
|
|
118
|
+
* Builds the SPKI wrapper around the raw 32-byte key exactly like the server's
|
|
119
|
+
* Node verifier does. */
|
|
120
|
+
export declare const webCryptoSolanaVerifier: WalletProofCrypto["verifySolanaSignature"];
|
|
121
|
+
/** base64urlToBytes exported for adapters/tests. */
|
|
122
|
+
export { base64urlToBytes };
|
|
123
|
+
/** Owner resolution for one gate identifier: the current registry owner wallet
|
|
124
|
+
* plus the network string the proof signs over, from the LIVE Valiron profile.
|
|
125
|
+
* Return null when the identifier is unknown (verification fails closed). */
|
|
126
|
+
export type WalletProofOwnerResolver = (identifier: string) => Promise<{
|
|
127
|
+
wallet: string;
|
|
128
|
+
network: string;
|
|
129
|
+
isSolana: boolean;
|
|
130
|
+
} | null>;
|
|
131
|
+
/**
|
|
132
|
+
* Ready-to-use `verifyRequest` for `runValironEdgeGate`: verifies the
|
|
133
|
+
* application-owned wallet proof (x-agent-timestamp / x-agent-nonce /
|
|
134
|
+
* x-agent-request-signature) of every identified request against the live
|
|
135
|
+
* registry owner, with mandatory durable replay protection.
|
|
136
|
+
*
|
|
137
|
+
* This is the default verifier for ordinary non-ANS agents. Valiron's hosted
|
|
138
|
+
* ANS provider-identity verification remains available via
|
|
139
|
+
* createEdgeIdentityVerifier for ans:// identities.
|
|
140
|
+
*/
|
|
141
|
+
export declare function createEdgeWalletProofVerifier(config: {
|
|
142
|
+
resolveOwner: WalletProofOwnerResolver;
|
|
143
|
+
nonceStore: NonceStore;
|
|
144
|
+
crypto: WalletProofCrypto;
|
|
145
|
+
maxAgeMs?: number;
|
|
146
|
+
}): (request: Request, identifier: string) => Promise<boolean>;
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Application-owned wallet proof verification for the Cloudflare trust gate.
|
|
4
|
+
*
|
|
5
|
+
* This is the edge (workerd-safe) verify side of the `valiron-wallet-request-v2`
|
|
6
|
+
* signed-request contract documented in docs/SECURITY-UPGRADE-2026-10-31.md and
|
|
7
|
+
* implemented server-side in services/edge-proxy requestProof.ts. It lets an API
|
|
8
|
+
* seller verify an ordinary (non-ANS) agent's ownership proof locally in a
|
|
9
|
+
* Cloudflare Worker, bound to the agent's current registry owner wallet from the
|
|
10
|
+
* live Valiron profile.
|
|
11
|
+
*
|
|
12
|
+
* Contract mirrored exactly:
|
|
13
|
+
* - headers: x-agent-timestamp (13-digit ms), x-agent-nonce (24-128 URL-safe
|
|
14
|
+
* chars), x-agent-request-signature
|
|
15
|
+
* - payload lines joined with "\n":
|
|
16
|
+
* valiron-wallet-request-v2
|
|
17
|
+
* agent:<exact-request-agent-id>
|
|
18
|
+
* chain:<network string, as resolved for the agent>
|
|
19
|
+
* method:<UPPERCASE method>
|
|
20
|
+
* url:<absolute public request URL (request.url verbatim)>
|
|
21
|
+
* body-sha256:<lowercase hex sha256 of ("" for no body, else
|
|
22
|
+
* JSON.stringify(JSON.parse(rawBody)))>
|
|
23
|
+
* timestamp:<ms>
|
|
24
|
+
* nonce:<fresh nonce>
|
|
25
|
+
* - timestamp window: |now - timestamp| <= 60_000 ms
|
|
26
|
+
* - nonce consumed exactly once per wallet+network+nonce scope
|
|
27
|
+
* (sha256 hex of `<wallet|wallet.toLowerCase()>:<network>:<nonce>`), TTL 121s
|
|
28
|
+
* - EVM: EIP-191 personal_sign over the UTF-8 payload, recovered address must
|
|
29
|
+
* equal the registry owner wallet (case-insensitive)
|
|
30
|
+
* - Solana: Ed25519 over the UTF-8 payload, signature base64url, wallet is the
|
|
31
|
+
* base58 public key
|
|
32
|
+
*
|
|
33
|
+
* Replay protection is MANDATORY: pass a durable `nonceStore`. In Cloudflare
|
|
34
|
+
* Workers use a Durable Object (see examples/cloudflare-gate-worker). Without a
|
|
35
|
+
* store this module fails closed.
|
|
36
|
+
*/
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.webCryptoSolanaVerifier = exports.MemoryNonceStore = void 0;
|
|
39
|
+
exports.buildWalletProofPayload = buildWalletProofPayload;
|
|
40
|
+
exports.verifyWalletRequestProof = verifyWalletRequestProof;
|
|
41
|
+
exports.base58Decode = base58Decode;
|
|
42
|
+
exports.base64urlToBytes = base64urlToBytes;
|
|
43
|
+
exports.createEdgeWalletProofVerifier = createEdgeWalletProofVerifier;
|
|
44
|
+
/**
|
|
45
|
+
* TEST/LOCAL ONLY. Atomic within a single isolate; NOT safe as a production
|
|
46
|
+
* replay control across Cloudflare isolates or restarts. The deployable
|
|
47
|
+
* template binds a Durable Object instead.
|
|
48
|
+
*/
|
|
49
|
+
class MemoryNonceStore {
|
|
50
|
+
constructor() {
|
|
51
|
+
this.seen = new Map();
|
|
52
|
+
}
|
|
53
|
+
async claim(scope, ttlSeconds) {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
for (const [k, exp] of this.seen)
|
|
56
|
+
if (exp <= now)
|
|
57
|
+
this.seen.delete(k);
|
|
58
|
+
if (this.seen.has(scope))
|
|
59
|
+
return false;
|
|
60
|
+
this.seen.set(scope, now + ttlSeconds * 1000);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.MemoryNonceStore = MemoryNonceStore;
|
|
65
|
+
const encoder = new TextEncoder();
|
|
66
|
+
async function sha256Hex(input) {
|
|
67
|
+
const digest = await crypto.subtle.digest("SHA-256", encoder.encode(input));
|
|
68
|
+
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
69
|
+
}
|
|
70
|
+
function base64urlToBytes(value) {
|
|
71
|
+
const b64 = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
72
|
+
const bin = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
|
|
73
|
+
const out = new Uint8Array(bin.length);
|
|
74
|
+
for (let i = 0; i < bin.length; i++)
|
|
75
|
+
out[i] = bin.charCodeAt(i);
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
/** Build the exact canonical payload the agent signed. Exported for tests and
|
|
79
|
+
* for the sign-side recipe; the layout must stay byte-identical with the
|
|
80
|
+
* server implementation. */
|
|
81
|
+
function buildWalletProofPayload(input) {
|
|
82
|
+
return [
|
|
83
|
+
"valiron-wallet-request-v2",
|
|
84
|
+
`agent:${input.agentId}`,
|
|
85
|
+
`chain:${input.network}`,
|
|
86
|
+
`method:${input.method.toUpperCase()}`,
|
|
87
|
+
`url:${input.url}`,
|
|
88
|
+
`body-sha256:${input.bodySha256.toLowerCase()}`,
|
|
89
|
+
`timestamp:${input.timestamp}`,
|
|
90
|
+
`nonce:${input.nonce}`,
|
|
91
|
+
].join("\n");
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Verify one inbound request's wallet proof headers against the live registry
|
|
95
|
+
* owner. `rawBody` is the exact request body text ("" when none); JSON bodies
|
|
96
|
+
* are canonicalized exactly as the server does (parse then stringify), and a
|
|
97
|
+
* body that fails to parse can never produce a valid proof.
|
|
98
|
+
*/
|
|
99
|
+
async function verifyWalletRequestProof(request, rawBody, config) {
|
|
100
|
+
const timestamp = request.headers.get("x-agent-timestamp") ?? "";
|
|
101
|
+
const nonce = request.headers.get("x-agent-nonce") ?? "";
|
|
102
|
+
const signature = request.headers.get("x-agent-request-signature") ?? "";
|
|
103
|
+
const now = config.now ?? Date.now();
|
|
104
|
+
const maxAge = config.maxAgeMs ?? 60000;
|
|
105
|
+
// Custom windows may tighten freshness, never extend it beyond the durable
|
|
106
|
+
// store's retention horizon. Include the future-skew boundary in retention.
|
|
107
|
+
if (!Number.isFinite(maxAge) || maxAge <= 0 || maxAge > 60000)
|
|
108
|
+
return { ok: false, reason: "invalid_timestamp_window" };
|
|
109
|
+
if (!/^\d{13}$/.test(timestamp) || Math.abs(now - Number(timestamp)) > maxAge) {
|
|
110
|
+
return { ok: false, reason: "stale_or_invalid_timestamp" };
|
|
111
|
+
}
|
|
112
|
+
if (!/^[A-Za-z0-9_-]{24,128}$/.test(nonce) || !signature) {
|
|
113
|
+
return { ok: false, reason: "missing_or_invalid_proof_headers" };
|
|
114
|
+
}
|
|
115
|
+
// Canonical body digest: empty string for no body, otherwise
|
|
116
|
+
// JSON.stringify of the parsed JSON body (mirrors the server exactly).
|
|
117
|
+
let bodyForDigest = "";
|
|
118
|
+
if (rawBody !== "") {
|
|
119
|
+
try {
|
|
120
|
+
bodyForDigest = JSON.stringify(JSON.parse(rawBody));
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return { ok: false, reason: "body_not_canonical_json" };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const bodySha256 = await sha256Hex(bodyForDigest);
|
|
127
|
+
const payload = buildWalletProofPayload({
|
|
128
|
+
agentId: config.agentId,
|
|
129
|
+
network: config.network,
|
|
130
|
+
method: request.method,
|
|
131
|
+
url: request.url,
|
|
132
|
+
bodySha256,
|
|
133
|
+
timestamp,
|
|
134
|
+
nonce,
|
|
135
|
+
});
|
|
136
|
+
let valid = false;
|
|
137
|
+
try {
|
|
138
|
+
if (config.isSolana) {
|
|
139
|
+
valid = await config.crypto.verifySolanaSignature(payload, signature, config.registryWallet);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
const recovered = await config.crypto.recoverEvmAddress(payload, signature);
|
|
143
|
+
valid = recovered.toLowerCase() === config.registryWallet.toLowerCase();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return { ok: false, reason: "signature_verification_error" };
|
|
148
|
+
}
|
|
149
|
+
if (!valid)
|
|
150
|
+
return { ok: false, reason: "ownership_proof_invalid" };
|
|
151
|
+
const scopeWallet = config.isSolana ? config.registryWallet : config.registryWallet.toLowerCase();
|
|
152
|
+
const scope = await sha256Hex(`${scopeWallet}:${config.network}:${nonce}`);
|
|
153
|
+
if (!config.nonceStore)
|
|
154
|
+
return { ok: false, reason: "replay_store_unavailable" };
|
|
155
|
+
let claimed = false;
|
|
156
|
+
try {
|
|
157
|
+
claimed = await config.nonceStore.claim(scope, 121);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
claimed = false;
|
|
161
|
+
}
|
|
162
|
+
if (!claimed)
|
|
163
|
+
return { ok: false, reason: "proof_replay_or_store_unavailable" };
|
|
164
|
+
return { ok: true };
|
|
165
|
+
}
|
|
166
|
+
/** Decode a base58 Solana public key to its 32 raw bytes. Shipped so templates
|
|
167
|
+
* need no dependency for the Ed25519 path. Throws on invalid input. */
|
|
168
|
+
function base58Decode(value) {
|
|
169
|
+
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
170
|
+
let num = 0n;
|
|
171
|
+
for (const ch of value) {
|
|
172
|
+
const idx = ALPHABET.indexOf(ch);
|
|
173
|
+
if (idx < 0)
|
|
174
|
+
throw new Error("invalid base58");
|
|
175
|
+
num = num * 58n + BigInt(idx);
|
|
176
|
+
}
|
|
177
|
+
let hex = num.toString(16);
|
|
178
|
+
if (hex.length % 2)
|
|
179
|
+
hex = "0" + hex;
|
|
180
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
181
|
+
for (let i = 0; i < bytes.length; i++)
|
|
182
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
183
|
+
// Restore leading zero bytes (base58 '1's).
|
|
184
|
+
let leading = 0;
|
|
185
|
+
for (const ch of value) {
|
|
186
|
+
if (ch === "1")
|
|
187
|
+
leading++;
|
|
188
|
+
else
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
const out = new Uint8Array(leading + bytes.length);
|
|
192
|
+
out.set(bytes, leading);
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
/** WebCrypto Ed25519 adapter for Solana wallets - ready to use in Workers.
|
|
196
|
+
* Builds the SPKI wrapper around the raw 32-byte key exactly like the server's
|
|
197
|
+
* Node verifier does. */
|
|
198
|
+
const webCryptoSolanaVerifier = async (message, signatureBase64url, publicKeyBase58) => {
|
|
199
|
+
const SPKI_ED25519_PREFIX = base64urlToBytes("MCowBQYDK2VwAyEA"); // 302a300506032b6570032100
|
|
200
|
+
const rawKey = base58Decode(publicKeyBase58);
|
|
201
|
+
if (rawKey.length !== 32)
|
|
202
|
+
return false;
|
|
203
|
+
const spki = new Uint8Array(SPKI_ED25519_PREFIX.length + 32);
|
|
204
|
+
spki.set(SPKI_ED25519_PREFIX, 0);
|
|
205
|
+
spki.set(rawKey, SPKI_ED25519_PREFIX.length);
|
|
206
|
+
const key = await crypto.subtle.importKey("spki", spki, { name: "Ed25519" }, false, ["verify"]);
|
|
207
|
+
return crypto.subtle.verify("Ed25519", key, base64urlToBytes(signatureBase64url), encoder.encode(message));
|
|
208
|
+
};
|
|
209
|
+
exports.webCryptoSolanaVerifier = webCryptoSolanaVerifier;
|
|
210
|
+
/**
|
|
211
|
+
* Ready-to-use `verifyRequest` for `runValironEdgeGate`: verifies the
|
|
212
|
+
* application-owned wallet proof (x-agent-timestamp / x-agent-nonce /
|
|
213
|
+
* x-agent-request-signature) of every identified request against the live
|
|
214
|
+
* registry owner, with mandatory durable replay protection.
|
|
215
|
+
*
|
|
216
|
+
* This is the default verifier for ordinary non-ANS agents. Valiron's hosted
|
|
217
|
+
* ANS provider-identity verification remains available via
|
|
218
|
+
* createEdgeIdentityVerifier for ans:// identities.
|
|
219
|
+
*/
|
|
220
|
+
function createEdgeWalletProofVerifier(config) {
|
|
221
|
+
return async (request, identifier) => {
|
|
222
|
+
let owner;
|
|
223
|
+
try {
|
|
224
|
+
owner = await config.resolveOwner(identifier);
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
if (!owner || !owner.wallet || !owner.network)
|
|
230
|
+
return false;
|
|
231
|
+
const rawBody = await request.clone().text();
|
|
232
|
+
const result = await verifyWalletRequestProof(request, rawBody, {
|
|
233
|
+
agentId: identifier,
|
|
234
|
+
network: owner.network,
|
|
235
|
+
registryWallet: owner.wallet,
|
|
236
|
+
isSolana: owner.isSolana,
|
|
237
|
+
nonceStore: config.nonceStore,
|
|
238
|
+
crypto: config.crypto,
|
|
239
|
+
...(config.maxAgeMs !== undefined ? { maxAgeMs: config.maxAgeMs } : {}),
|
|
240
|
+
});
|
|
241
|
+
return result.ok;
|
|
242
|
+
};
|
|
243
|
+
}
|
package/dist/identity.d.ts
CHANGED
|
@@ -12,6 +12,9 @@ export interface CreateAgentIdentityProofInput {
|
|
|
12
12
|
provider: string;
|
|
13
13
|
agentId: string;
|
|
14
14
|
body?: AgentIdentityBody;
|
|
15
|
+
audience?: string;
|
|
16
|
+
method?: string;
|
|
17
|
+
url?: string;
|
|
15
18
|
/** Sign the exact UTF-8 payload with the private key bound to the provider identity. */
|
|
16
19
|
sign: (payload: string) => string | Uint8Array | Promise<string | Uint8Array>;
|
|
17
20
|
timestamp?: string;
|
|
@@ -42,6 +45,9 @@ export declare function buildAgentIdentitySigningPayload(input: {
|
|
|
42
45
|
timestamp: string;
|
|
43
46
|
nonce: string;
|
|
44
47
|
requestDigest: string;
|
|
48
|
+
audience?: string;
|
|
49
|
+
method?: string;
|
|
50
|
+
url?: string;
|
|
45
51
|
}): string;
|
|
46
52
|
/** Create a signed proof for one exact request body. Private keys remain with the agent. */
|
|
47
53
|
export declare function createAgentIdentityProof(input: CreateAgentIdentityProofInput): Promise<AgentIdentityProof>;
|
package/dist/identity.js
CHANGED
|
@@ -45,7 +45,10 @@ function sha256RequestDigest(body) {
|
|
|
45
45
|
/** Build the canonical payload understood by Valiron's provider identity verifier. */
|
|
46
46
|
function buildAgentIdentitySigningPayload(input) {
|
|
47
47
|
return [
|
|
48
|
-
"valiron-agent-request-
|
|
48
|
+
"valiron-agent-request-v2",
|
|
49
|
+
`audience:${input.audience || ""}`,
|
|
50
|
+
`method:${input.method?.toUpperCase() || ""}`,
|
|
51
|
+
`url:${input.url || ""}`,
|
|
49
52
|
`agent-id:${input.agentId}`,
|
|
50
53
|
`timestamp:${input.timestamp}`,
|
|
51
54
|
`nonce:${input.nonce}`,
|
|
@@ -62,6 +65,7 @@ async function createAgentIdentityProof(input) {
|
|
|
62
65
|
timestamp,
|
|
63
66
|
nonce,
|
|
64
67
|
requestDigest,
|
|
68
|
+
audience: input.audience, method: input.method, url: input.url,
|
|
65
69
|
}));
|
|
66
70
|
const signature = typeof signed === "string" ? signed : Buffer.from(signed).toString("base64url");
|
|
67
71
|
return {
|
|
@@ -71,6 +75,7 @@ async function createAgentIdentityProof(input) {
|
|
|
71
75
|
timestamp,
|
|
72
76
|
nonce,
|
|
73
77
|
requestDigest,
|
|
78
|
+
audience: input.audience, method: input.method, url: input.url,
|
|
74
79
|
...(input.keyId ? { keyId: input.keyId } : {}),
|
|
75
80
|
};
|
|
76
81
|
}
|