aixyz 0.5.2 → 0.6.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/accepts.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { z } from "zod";
2
2
  import { FacilitatorClient, HTTPFacilitatorClient } from "@x402/core/server";
3
- import { getFacilitatorClient } from "./facilitator";
4
3
 
5
4
  export type Accepts = AcceptsX402 | AcceptsFree;
6
5
 
@@ -34,4 +33,6 @@ export { HTTPFacilitatorClient };
34
33
  /**
35
34
  * The default facilitator client provided by aixyz.
36
35
  */
37
- export const facilitator: FacilitatorClient = getFacilitatorClient();
36
+ export const facilitator: FacilitatorClient = new HTTPFacilitatorClient({
37
+ url: "https://x402.agently.to/facilitator",
38
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aixyz",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "Payment-native SDK for AI Agent",
5
5
  "keywords": [
6
6
  "ai",
@@ -23,8 +23,8 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "@a2a-js/sdk": "^0.3.10",
26
- "@aixyz/cli": "0.5.2",
27
- "@aixyz/config": "0.5.2",
26
+ "@aixyz/cli": "0.6.0",
27
+ "@aixyz/config": "0.6.0",
28
28
  "@modelcontextprotocol/sdk": "^1.26.0",
29
29
  "@x402/core": "^2.3.1",
30
30
  "@x402/evm": "^2.3.1",
package/server/index.ts CHANGED
@@ -4,13 +4,12 @@ import { FacilitatorClient, x402ResourceServer } from "@x402/core/server";
4
4
  import { paymentMiddleware, PaymentRequirements } from "@x402/express";
5
5
  import { ExactEvmScheme } from "@x402/evm/exact/server";
6
6
  import { z } from "zod";
7
- import { AcceptsX402 } from "../accepts";
8
- import { getFacilitatorClient } from "../facilitator";
7
+ import { type AcceptsX402, facilitator as defaultFacilitator } from "../accepts";
9
8
 
10
9
  // TODO(@fuxingloh): rename to unstable_AixyzApp?
11
10
  export class AixyzServer extends x402ResourceServer {
12
11
  constructor(
13
- facilitator: FacilitatorClient = getFacilitatorClient(),
12
+ facilitator: FacilitatorClient = defaultFacilitator,
14
13
  public config = getAixyzConfig(),
15
14
  public express: initExpress.Express = initExpress(),
16
15
  ) {
@@ -1,387 +0,0 @@
1
- import { FacilitatorConfig } from "@x402/core/http";
2
- import { SignJWT, importPKCS8, importJWK, JWTPayload } from "jose";
3
- import { getRandomValues } from "crypto";
4
-
5
- const COINBASE_FACILITATOR_BASE_URL = "https://api.cdp.coinbase.com";
6
- const COINBASE_FACILITATOR_V2_ROUTE = "/platform/v2/x402";
7
-
8
- const X402_SDK_VERSION = "2.1.0";
9
- const CDP_SDK_VERSION = "1.29.0";
10
-
11
- /**
12
- * Creates an authorization header for a request to the Coinbase API.
13
- *
14
- * @param apiKeyId - The api key ID
15
- * @param apiKeySecret - The api key secret
16
- * @param requestMethod - The method for the request (e.g. 'POST')
17
- * @param requestHost - The host for the request (e.g. 'https://x402.org/facilitator')
18
- * @param requestPath - The path for the request (e.g. '/verify')
19
- * @returns The authorization header string
20
- */
21
- export async function createAuthHeader(
22
- apiKeyId: string,
23
- apiKeySecret: string,
24
- requestMethod: string,
25
- requestHost: string,
26
- requestPath: string,
27
- ) {
28
- const jwt = await generateJwt({
29
- apiKeyId,
30
- apiKeySecret,
31
- requestMethod,
32
- requestHost,
33
- requestPath,
34
- });
35
- return `Bearer ${jwt}`;
36
- }
37
-
38
- /**
39
- * Creates a correlation header for a request to the Coinbase API.
40
- *
41
- * @returns The correlation header string
42
- */
43
- export function createCorrelationHeader(): string {
44
- const data: Record<string, string> = {
45
- sdk_version: CDP_SDK_VERSION,
46
- sdk_language: "typescript",
47
- source: "x402",
48
- source_version: X402_SDK_VERSION,
49
- };
50
- return Object.keys(data)
51
- .map((key) => `${key}=${encodeURIComponent(data[key])}`)
52
- .join(",");
53
- }
54
-
55
- /**
56
- * Creates a CDP auth header for the facilitator service
57
- *
58
- * @param apiKeyId - The CDP API key ID
59
- * @param apiKeySecret - The CDP API key secret
60
- * @returns A function that returns the auth headers
61
- */
62
- export function createCdpAuthHeaders(apiKeyId?: string, apiKeySecret?: string): FacilitatorConfig["createAuthHeaders"] {
63
- const requestHost = COINBASE_FACILITATOR_BASE_URL.replace("https://", "");
64
-
65
- return async () => {
66
- apiKeyId = apiKeyId ?? process.env.CDP_API_KEY_ID;
67
- apiKeySecret = apiKeySecret ?? process.env.CDP_API_KEY_SECRET;
68
-
69
- const headers = {
70
- verify: {
71
- "Correlation-Context": createCorrelationHeader(),
72
- } as Record<string, string>,
73
- settle: {
74
- "Correlation-Context": createCorrelationHeader(),
75
- } as Record<string, string>,
76
- supported: {
77
- "Correlation-Context": createCorrelationHeader(),
78
- } as Record<string, string>,
79
- list: {
80
- "Correlation-Context": createCorrelationHeader(),
81
- },
82
- };
83
-
84
- if (apiKeyId && apiKeySecret) {
85
- headers.verify.Authorization = await createAuthHeader(
86
- apiKeyId,
87
- apiKeySecret,
88
- "POST",
89
- requestHost,
90
- `${COINBASE_FACILITATOR_V2_ROUTE}/verify`,
91
- );
92
- headers.settle.Authorization = await createAuthHeader(
93
- apiKeyId,
94
- apiKeySecret,
95
- "POST",
96
- requestHost,
97
- `${COINBASE_FACILITATOR_V2_ROUTE}/settle`,
98
- );
99
- headers.supported.Authorization = await createAuthHeader(
100
- apiKeyId,
101
- apiKeySecret,
102
- "GET",
103
- requestHost,
104
- `${COINBASE_FACILITATOR_V2_ROUTE}/supported`,
105
- );
106
- }
107
-
108
- return headers;
109
- };
110
- }
111
-
112
- /**
113
- * Creates a facilitator config for the Coinbase X402 facilitator
114
- *
115
- * @param apiKeyId - The CDP API key ID
116
- * @param apiKeySecret - The CDP API key secret
117
- * @returns A facilitator config
118
- */
119
- function createFacilitatorConfig(apiKeyId?: string, apiKeySecret?: string): FacilitatorConfig {
120
- return {
121
- url: `${COINBASE_FACILITATOR_BASE_URL}${COINBASE_FACILITATOR_V2_ROUTE}`,
122
- createAuthHeaders: createCdpAuthHeaders(apiKeyId, apiKeySecret),
123
- };
124
- }
125
-
126
- /**
127
- * JwtOptions contains configuration for JWT generation.
128
- *
129
- * This interface holds all necessary parameters for generating a JWT token
130
- * for authenticating with Coinbase's REST APIs. It supports both EC (ES256)
131
- * and Ed25519 (EdDSA) keys.
132
- */
133
- export interface JwtOptions {
134
- /**
135
- * The API key ID
136
- *
137
- * Examples:
138
- * 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
139
- * 'organizations/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/apiKeys/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
140
- */
141
- apiKeyId: string;
142
-
143
- /**
144
- * The API key secret
145
- *
146
- * Examples:
147
- * 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==' (Edwards key (Ed25519))
148
- * '-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n' (EC key (ES256))
149
- */
150
- apiKeySecret: string;
151
-
152
- /**
153
- * The HTTP method for the request (e.g. 'GET', 'POST'), or null for JWTs intended for websocket connections
154
- */
155
- requestMethod?: string | null;
156
-
157
- /**
158
- * The host for the request (e.g. 'api.cdp.coinbase.com'), or null for JWTs intended for websocket connections
159
- */
160
- requestHost?: string | null;
161
-
162
- /**
163
- * The path for the request (e.g. '/platform/v1/wallets'), or null for JWTs intended for websocket connections
164
- */
165
- requestPath?: string | null;
166
-
167
- /**
168
- * Optional expiration time in seconds (defaults to 120)
169
- */
170
- expiresIn?: number;
171
-
172
- /**
173
- * Optional audience claim for the JWT
174
- */
175
- audience?: string[];
176
- }
177
-
178
- /**
179
- * Generates a JWT (also known as a Bearer token) for authenticating with Coinbase's REST APIs.
180
- * Supports both EC (ES256) and Ed25519 (EdDSA) keys. Also supports JWTs meant for
181
- * websocket connections by allowing requestMethod, requestHost, and requestPath to all be
182
- * null, in which case the 'uris' claim is omitted from the JWT.
183
- *
184
- * @param options - The configuration options for generating the JWT
185
- * @returns The generated JWT (Bearer token) string
186
- * @throws {Error} If required parameters are missing, invalid, or if JWT signing fails
187
- */
188
- export async function generateJwt(options: JwtOptions): Promise<string> {
189
- // Validate required parameters
190
- if (!options.apiKeyId) {
191
- throw new Error("Key name is required");
192
- }
193
- if (!options.apiKeySecret) {
194
- throw new Error("Private key is required");
195
- }
196
-
197
- // Check if we have a REST API request or a websocket connection
198
- const hasAllRequestParams = Boolean(options.requestMethod && options.requestHost && options.requestPath);
199
- const hasNoRequestParams =
200
- (options.requestMethod === undefined || options.requestMethod === null) &&
201
- (options.requestHost === undefined || options.requestHost === null) &&
202
- (options.requestPath === undefined || options.requestPath === null);
203
-
204
- // Ensure we either have all request parameters or none (for websocket)
205
- if (!hasAllRequestParams && !hasNoRequestParams) {
206
- throw new Error(
207
- "Either all request details (method, host, path) must be provided, or all must be null for JWTs intended for websocket connections",
208
- );
209
- }
210
-
211
- const now = Math.floor(Date.now() / 1000);
212
- const expiresIn = options.expiresIn || 120; // Default to 120 seconds if not specified
213
-
214
- // Prepare the JWT payload
215
- const claims: JWTPayload = {
216
- sub: options.apiKeyId,
217
- iss: "cdp",
218
- aud: options.audience,
219
- };
220
-
221
- // Add the uris claim only for REST API requests
222
- if (hasAllRequestParams) {
223
- claims.uris = [`${options.requestMethod} ${options.requestHost}${options.requestPath}`];
224
- }
225
-
226
- // Generate random nonce for the header
227
- const randomNonce = nonce();
228
-
229
- // Determine if we're using EC or Edwards key based on the key format
230
- if (await isValidECKey(options.apiKeySecret)) {
231
- return await buildECJWT(options.apiKeySecret, options.apiKeyId, claims, now, expiresIn, randomNonce);
232
- } else if (isValidEd25519Key(options.apiKeySecret)) {
233
- return await buildEdwardsJWT(options.apiKeySecret, options.apiKeyId, claims, now, expiresIn, randomNonce);
234
- } else {
235
- throw new UserInputValidationError("Invalid key format - must be either PEM EC key or base64 Ed25519 key");
236
- }
237
- }
238
-
239
- /**
240
- * Builds a JWT using an EC key.
241
- *
242
- * @param privateKey - The EC private key in PEM format
243
- * @param keyName - The key name/ID
244
- * @param claims - The JWT claims
245
- * @param now - Current timestamp in seconds
246
- * @param expiresIn - Number of seconds until the token expires
247
- * @param nonce - Random nonce for the JWT header
248
- * @returns A JWT token signed with an EC key
249
- * @throws {Error} If key conversion, import, or signing fails
250
- */
251
- async function buildECJWT(
252
- privateKey: string,
253
- keyName: string,
254
- claims: JWTPayload,
255
- now: number,
256
- expiresIn: number,
257
- nonce: string,
258
- ): Promise<string> {
259
- try {
260
- // Import the key directly with jose
261
- const ecKey = await importPKCS8(privateKey, "ES256");
262
-
263
- // Sign and return the JWT
264
- return await new SignJWT(claims)
265
- .setProtectedHeader({ alg: "ES256", kid: keyName, typ: "JWT", nonce })
266
- .setIssuedAt(Math.floor(now))
267
- .setNotBefore(Math.floor(now))
268
- .setExpirationTime(Math.floor(now + expiresIn))
269
- .sign(ecKey);
270
- } catch (error) {
271
- throw new Error(`Failed to generate EC JWT: ${(error as Error).message}`);
272
- }
273
- }
274
-
275
- /**
276
- * Builds a JWT using an Ed25519 key.
277
- *
278
- * @param privateKey - The Ed25519 private key in base64 format
279
- * @param keyName - The key name/ID
280
- * @param claims - The JWT claims
281
- * @param now - Current timestamp in seconds
282
- * @param expiresIn - Number of seconds until the token expires
283
- * @param nonce - Random nonce for the JWT header
284
- * @returns A JWT token using an Ed25519 key
285
- * @throws {Error} If key parsing, import, or signing fails
286
- */
287
- async function buildEdwardsJWT(
288
- privateKey: string,
289
- keyName: string,
290
- claims: JWTPayload,
291
- now: number,
292
- expiresIn: number,
293
- nonce: string,
294
- ): Promise<string> {
295
- try {
296
- // Decode the base64 key (expecting 64 bytes: 32 for seed + 32 for public key)
297
- const decoded = Buffer.from(privateKey, "base64");
298
- if (decoded.length !== 64) {
299
- throw new UserInputValidationError("Invalid Ed25519 key length");
300
- }
301
-
302
- const seed = decoded.subarray(0, 32);
303
- const publicKey = decoded.subarray(32);
304
-
305
- // Create JWK from the key components
306
- const jwk = {
307
- kty: "OKP",
308
- crv: "Ed25519",
309
- d: seed.toString("base64url"),
310
- x: publicKey.toString("base64url"),
311
- };
312
-
313
- // Import the key for signing
314
- const key = await importJWK(jwk, "EdDSA");
315
-
316
- // Sign and return the JWT
317
- return await new SignJWT(claims)
318
- .setProtectedHeader({ alg: "EdDSA", kid: keyName, typ: "JWT", nonce })
319
- .setIssuedAt(Math.floor(now))
320
- .setNotBefore(Math.floor(now))
321
- .setExpirationTime(Math.floor(now + expiresIn))
322
- .sign(key);
323
- } catch (error) {
324
- throw new Error(`Failed to generate Ed25519 JWT: ${(error as Error).message}`);
325
- }
326
- }
327
-
328
- /**
329
- * UserInputValidationError is thrown when validation of a user-supplied input fails.
330
- */
331
- export class UserInputValidationError extends Error {
332
- /**
333
- * Initializes a new UserInputValidationError instance.
334
- *
335
- * @param message - The user input validation error message.
336
- */
337
- constructor(message: string) {
338
- super(message);
339
- this.name = "UserInputValidationError";
340
- if (Error.captureStackTrace) {
341
- Error.captureStackTrace(this, UserInputValidationError);
342
- }
343
- }
344
- }
345
- /**
346
- * Determines if a string could be a valid Ed25519 key
347
- *
348
- * @param str - The string to test
349
- * @returns True if the string could be a valid Ed25519 key, false otherwise
350
- */
351
- function isValidEd25519Key(str: string): boolean {
352
- try {
353
- const decoded = Buffer.from(str, "base64");
354
- return decoded.length === 64;
355
- } catch {
356
- return false;
357
- }
358
- }
359
-
360
- /**
361
- * Determines if a string is a valid EC private key in PEM format
362
- *
363
- * @param str - The string to test
364
- * @returns True if the string is a valid EC private key in PEM format
365
- */
366
- async function isValidECKey(str: string): Promise<boolean> {
367
- try {
368
- // Try to import the key with jose - if it works, it's a valid EC key
369
- await importPKCS8(str, "ES256");
370
- return true;
371
- } catch {
372
- return false;
373
- }
374
- }
375
-
376
- /**
377
- * Generates a random nonce for the JWT.
378
- *
379
- * @returns {string} The generated nonce.
380
- */
381
- function nonce(): string {
382
- const bytes = new Uint8Array(16);
383
- getRandomValues(bytes);
384
- return Buffer.from(bytes).toString("hex");
385
- }
386
-
387
- export const facilitator = createFacilitatorConfig();
@@ -1,12 +0,0 @@
1
- import { HTTPFacilitatorClient } from "@x402/core/server";
2
- import { facilitator } from "./coinbase";
3
-
4
- export function getFacilitatorClient() {
5
- if (process.env.CDP_API_KEY_ID) {
6
- return new HTTPFacilitatorClient(facilitator);
7
- }
8
-
9
- return new HTTPFacilitatorClient({
10
- url: process.env.X402_FACILITATOR_URL || "https://www.x402.org/facilitator",
11
- });
12
- }