@4mica/x402 1.2.4 → 2.0.0-alpha.2
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/CHANGELOG.md +63 -1
- package/README.md +67 -82
- package/dist/client/scheme.d.ts +40 -4
- package/dist/client/scheme.js +96 -38
- package/dist/domain.d.ts +12 -0
- package/dist/domain.js +30 -0
- package/dist/index.d.ts +2 -2
- package/dist/server/express/adapter.d.ts +2 -2
- package/dist/server/express/index.d.ts +13 -46
- package/dist/server/express/index.js +32 -71
- package/dist/server/facilitator.d.ts +2 -24
- package/dist/server/facilitator.js +0 -36
- package/dist/server/index.d.ts +2 -4
- package/dist/server/index.js +1 -2
- package/dist/server/scheme.d.ts +54 -10
- package/dist/server/scheme.js +118 -56
- package/dist/types.d.ts +36 -12
- package/package.json +33 -32
- package/.eslintrc.cjs +0 -29
- package/.prettierignore +0 -3
- package/.prettierrc +0 -6
- package/demo/.env.example +0 -8
- package/demo/README.md +0 -125
- package/demo/package.json +0 -26
- package/demo/src/client.ts +0 -54
- package/demo/src/deposit.ts +0 -37
- package/demo/src/server.ts +0 -81
- package/demo/tsconfig.json +0 -8
- package/demo/yarn.lock +0 -925
- package/eslint.config.mjs +0 -22
- package/src/client/index.ts +0 -1
- package/src/client/scheme.ts +0 -111
- package/src/index.ts +0 -9
- package/src/server/express/adapter.ts +0 -100
- package/src/server/express/index.ts +0 -499
- package/src/server/facilitator.ts +0 -206
- package/src/server/index.ts +0 -10
- package/src/server/scheme.ts +0 -229
- package/src/types.ts +0 -24
- package/tests/client-scheme.test.ts +0 -99
- package/tests/facilitator.test.ts +0 -174
- package/tsconfig.build.json +0 -5
- package/tsconfig.json +0 -17
- package/vitest.config.ts +0 -12
|
@@ -1,27 +1,6 @@
|
|
|
1
|
-
import { PaywallConfig, PaywallProvider,
|
|
2
|
-
import {
|
|
3
|
-
import { NextFunction, Request, Response } from 'express';
|
|
4
|
-
/**
|
|
5
|
-
* Configuration for payment tab handling
|
|
6
|
-
*/
|
|
7
|
-
interface TabConfig {
|
|
8
|
-
/**
|
|
9
|
-
* The full URL endpoint for opening payment tabs. This URL is injected into
|
|
10
|
-
* paymentRequirements.extra and clients use it to open a payment tab.
|
|
11
|
-
* When a request matches this endpoint's path, the middleware will parse
|
|
12
|
-
* the request body and call the 4mica facilitator to open a tab.
|
|
13
|
-
*
|
|
14
|
-
* @example "https://api.example.com/x402/tab"
|
|
15
|
-
*/
|
|
16
|
-
advertisedEndpoint: string;
|
|
17
|
-
/**
|
|
18
|
-
* The lifetime of the payment tab in seconds. Defines how long the tab
|
|
19
|
-
* remains valid before expiring.
|
|
20
|
-
*
|
|
21
|
-
* @example 3600 // 1 hour
|
|
22
|
-
*/
|
|
23
|
-
ttlSeconds?: number;
|
|
24
|
-
}
|
|
1
|
+
import { type FacilitatorClient, type PaywallConfig, type PaywallProvider, type RoutesConfig, x402HTTPResourceServer, x402ResourceServer } from '@x402/core/server';
|
|
2
|
+
import type { Network, SchemeNetworkServer } from '@x402/core/types';
|
|
3
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
25
4
|
/**
|
|
26
5
|
* Configuration for registering a payment scheme with a specific network
|
|
27
6
|
*/
|
|
@@ -41,7 +20,6 @@ export interface SchemeRegistration {
|
|
|
41
20
|
* Use this when you need to configure HTTP-level hooks.
|
|
42
21
|
*
|
|
43
22
|
* @param httpServer - Pre-configured x402HTTPResourceServer instance
|
|
44
|
-
* @param tabConfig - Configuration for payment tab handling (endpoint URL and TTL)
|
|
45
23
|
* @param paywallConfig - Optional configuration for the built-in paywall UI
|
|
46
24
|
* @param paywall - Optional custom paywall provider (overrides default)
|
|
47
25
|
* @param syncFacilitatorOnStart - Whether to sync with the facilitator on startup (defaults to true)
|
|
@@ -49,7 +27,7 @@ export interface SchemeRegistration {
|
|
|
49
27
|
*
|
|
50
28
|
* @example
|
|
51
29
|
* ```typescript
|
|
52
|
-
* import { paymentMiddlewareFromHTTPServer, x402ResourceServer, x402HTTPResourceServer } from "@x402/express";
|
|
30
|
+
* import { paymentMiddlewareFromHTTPServer, x402ResourceServer, x402HTTPResourceServer } from "@4mica/x402/server/express";
|
|
53
31
|
*
|
|
54
32
|
* const resourceServer = new x402ResourceServer(facilitatorClient)
|
|
55
33
|
* .register(NETWORK, new ExactEvmScheme())
|
|
@@ -57,12 +35,10 @@ export interface SchemeRegistration {
|
|
|
57
35
|
* const httpServer = new x402HTTPResourceServer(resourceServer, routes)
|
|
58
36
|
* .onProtectedRequest(requestHook);
|
|
59
37
|
*
|
|
60
|
-
* app.use(paymentMiddlewareFromHTTPServer(
|
|
61
|
-
*
|
|
62
|
-
* { advertisedEndpoint: "https://api.example.com/x402/tab" },
|
|
63
|
-
* )); * ```
|
|
38
|
+
* app.use(paymentMiddlewareFromHTTPServer(httpServer));
|
|
39
|
+
* ```
|
|
64
40
|
*/
|
|
65
|
-
export declare function paymentMiddlewareFromHTTPServer(httpServer: x402HTTPResourceServer,
|
|
41
|
+
export declare function paymentMiddlewareFromHTTPServer(httpServer: x402HTTPResourceServer, paywallConfig?: PaywallConfig, paywall?: PaywallProvider, syncFacilitatorOnStart?: boolean): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
66
42
|
/**
|
|
67
43
|
* Express payment middleware for x402 protocol (direct server instance).
|
|
68
44
|
*
|
|
@@ -72,7 +48,6 @@ export declare function paymentMiddlewareFromHTTPServer(httpServer: x402HTTPReso
|
|
|
72
48
|
*
|
|
73
49
|
* @param routes - Route configurations for protected endpoints
|
|
74
50
|
* @param server - Pre-configured x402ResourceServer instance
|
|
75
|
-
* @param tabConfig - Configuration for payment tab handling (endpoint URL and TTL)
|
|
76
51
|
* @param paywallConfig - Optional configuration for the built-in paywall UI
|
|
77
52
|
* @param paywall - Optional custom paywall provider (overrides default)
|
|
78
53
|
* @param syncFacilitatorOnStart - Whether to sync with the facilitator on startup (defaults to true)
|
|
@@ -80,19 +55,15 @@ export declare function paymentMiddlewareFromHTTPServer(httpServer: x402HTTPReso
|
|
|
80
55
|
*
|
|
81
56
|
* @example
|
|
82
57
|
* ```typescript
|
|
83
|
-
* import { paymentMiddleware } from "@x402/express";
|
|
58
|
+
* import { paymentMiddleware } from "@4mica/x402/server/express";
|
|
84
59
|
*
|
|
85
60
|
* const server = new x402ResourceServer(myFacilitatorClient)
|
|
86
61
|
* .register(NETWORK, new ExactEvmScheme());
|
|
87
62
|
*
|
|
88
|
-
* app.use(paymentMiddleware(
|
|
89
|
-
* routes,
|
|
90
|
-
* server,
|
|
91
|
-
* { advertisedEndpoint: "https://api.example.com/x402/tab" },
|
|
92
|
-
* ));
|
|
63
|
+
* app.use(paymentMiddleware(routes, server));
|
|
93
64
|
* ```
|
|
94
65
|
*/
|
|
95
|
-
export declare function paymentMiddleware(routes: RoutesConfig, server: x402ResourceServer,
|
|
66
|
+
export declare function paymentMiddleware(routes: RoutesConfig, server: x402ResourceServer, paywallConfig?: PaywallConfig, paywall?: PaywallProvider, syncFacilitatorOnStart?: boolean): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
96
67
|
/**
|
|
97
68
|
* Express payment middleware for x402 protocol (configuration-based).
|
|
98
69
|
*
|
|
@@ -100,7 +71,6 @@ export declare function paymentMiddleware(routes: RoutesConfig, server: x402Reso
|
|
|
100
71
|
* This function creates and configures the x402ResourceServer internally.
|
|
101
72
|
*
|
|
102
73
|
* @param routes - Route configurations for protected endpoints
|
|
103
|
-
* @param tabConfig - Configuration for payment tab handling
|
|
104
74
|
* @param facilitatorClients - Optional facilitator client(s) for payment processing
|
|
105
75
|
* @param schemes - Optional array of scheme registrations for server-side payment processing
|
|
106
76
|
* @param paywallConfig - Optional configuration for the built-in paywall UI
|
|
@@ -110,13 +80,10 @@ export declare function paymentMiddleware(routes: RoutesConfig, server: x402Reso
|
|
|
110
80
|
*
|
|
111
81
|
* @example
|
|
112
82
|
* ```typescript
|
|
113
|
-
* import { paymentMiddlewareFromConfig } from "@x402/express";
|
|
83
|
+
* import { paymentMiddlewareFromConfig } from "@4mica/x402/server/express";
|
|
114
84
|
*
|
|
115
|
-
* app.use(paymentMiddlewareFromConfig(
|
|
116
|
-
* routes,
|
|
117
|
-
* { advertisedEndpoint: "https://api.example.com/x402/tab" },
|
|
118
|
-
* ));
|
|
85
|
+
* app.use(paymentMiddlewareFromConfig(routes));
|
|
119
86
|
* ```
|
|
120
87
|
*/
|
|
121
|
-
export declare function paymentMiddlewareFromConfig(routes: RoutesConfig,
|
|
88
|
+
export declare function paymentMiddlewareFromConfig(routes: RoutesConfig, facilitatorClients?: FacilitatorClient | FacilitatorClient[], schemes?: SchemeRegistration[], paywallConfig?: PaywallConfig, paywall?: PaywallProvider, syncFacilitatorOnStart?: boolean): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
122
89
|
export { ExpressAdapter } from './adapter.js';
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import { x402HTTPResourceServer, x402ResourceServer, } from '@x402/core/server';
|
|
2
|
-
import { ExpressAdapter } from './adapter.js';
|
|
3
|
-
import { FourMicaEvmScheme, SUPPORTED_NETWORKS } from '../scheme.js';
|
|
4
2
|
import { FourMicaFacilitatorClient } from '../facilitator.js';
|
|
3
|
+
import { FourMicaEvmScheme, SUPPORTED_NETWORKS } from '../scheme.js';
|
|
4
|
+
import { ExpressAdapter } from './adapter.js';
|
|
5
5
|
function getHTTPServerInternals(httpServer) {
|
|
6
6
|
return httpServer;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Register the default 4mica scheme server for every hosted network, leaving
|
|
10
|
+
* alone any network the caller already registered one for (for example a
|
|
11
|
+
* `FourMicaEvmScheme` pointed at a self-hosted core).
|
|
12
|
+
*/
|
|
13
|
+
function registerNetworkServers(httpServer) {
|
|
14
|
+
const schemeServer = new FourMicaEvmScheme();
|
|
10
15
|
const server = getHTTPServerInternals(httpServer);
|
|
11
16
|
SUPPORTED_NETWORKS.forEach((network) => {
|
|
17
|
+
const registered = server.ResourceServer.registeredServerSchemes?.get(network);
|
|
18
|
+
if (registered?.has(schemeServer.scheme))
|
|
19
|
+
return;
|
|
12
20
|
server.ResourceServer.register(network, schemeServer);
|
|
13
21
|
});
|
|
14
22
|
}
|
|
@@ -20,20 +28,12 @@ function checkIfBazaarNeeded(routes) {
|
|
|
20
28
|
return !!(routeConfig.extensions && 'bazaar' in routeConfig.extensions);
|
|
21
29
|
});
|
|
22
30
|
}
|
|
23
|
-
function isOpenTabHttpError(error) {
|
|
24
|
-
if (typeof error !== 'object' || error === null) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
const candidate = error;
|
|
28
|
-
return typeof candidate.status === 'number' && 'response' in candidate;
|
|
29
|
-
}
|
|
30
31
|
/**
|
|
31
32
|
* Express payment middleware for x402 protocol (direct HTTP server instance).
|
|
32
33
|
*
|
|
33
34
|
* Use this when you need to configure HTTP-level hooks.
|
|
34
35
|
*
|
|
35
36
|
* @param httpServer - Pre-configured x402HTTPResourceServer instance
|
|
36
|
-
* @param tabConfig - Configuration for payment tab handling (endpoint URL and TTL)
|
|
37
37
|
* @param paywallConfig - Optional configuration for the built-in paywall UI
|
|
38
38
|
* @param paywall - Optional custom paywall provider (overrides default)
|
|
39
39
|
* @param syncFacilitatorOnStart - Whether to sync with the facilitator on startup (defaults to true)
|
|
@@ -41,7 +41,7 @@ function isOpenTabHttpError(error) {
|
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* ```typescript
|
|
44
|
-
* import { paymentMiddlewareFromHTTPServer, x402ResourceServer, x402HTTPResourceServer } from "@x402/express";
|
|
44
|
+
* import { paymentMiddlewareFromHTTPServer, x402ResourceServer, x402HTTPResourceServer } from "@4mica/x402/server/express";
|
|
45
45
|
*
|
|
46
46
|
* const resourceServer = new x402ResourceServer(facilitatorClient)
|
|
47
47
|
* .register(NETWORK, new ExactEvmScheme())
|
|
@@ -49,14 +49,11 @@ function isOpenTabHttpError(error) {
|
|
|
49
49
|
* const httpServer = new x402HTTPResourceServer(resourceServer, routes)
|
|
50
50
|
* .onProtectedRequest(requestHook);
|
|
51
51
|
*
|
|
52
|
-
* app.use(paymentMiddlewareFromHTTPServer(
|
|
53
|
-
*
|
|
54
|
-
* { advertisedEndpoint: "https://api.example.com/x402/tab" },
|
|
55
|
-
* )); * ```
|
|
52
|
+
* app.use(paymentMiddlewareFromHTTPServer(httpServer));
|
|
53
|
+
* ```
|
|
56
54
|
*/
|
|
57
|
-
export function paymentMiddlewareFromHTTPServer(httpServer,
|
|
58
|
-
|
|
59
|
-
registerNetworkServers(httpServer, tabConfig.advertisedEndpoint);
|
|
55
|
+
export function paymentMiddlewareFromHTTPServer(httpServer, paywallConfig, paywall, syncFacilitatorOnStart = true) {
|
|
56
|
+
registerNetworkServers(httpServer);
|
|
60
57
|
// Register custom paywall provider if provided
|
|
61
58
|
if (paywall) {
|
|
62
59
|
httpServer.registerPaywallProvider(paywall);
|
|
@@ -79,33 +76,6 @@ export function paymentMiddlewareFromHTTPServer(httpServer, tabConfig, paywallCo
|
|
|
79
76
|
});
|
|
80
77
|
}
|
|
81
78
|
return async (req, res, next) => {
|
|
82
|
-
// Check if this request is for the tab opening endpoint
|
|
83
|
-
try {
|
|
84
|
-
const advertisedUrl = new URL(tabConfig.advertisedEndpoint);
|
|
85
|
-
if (req.path === advertisedUrl.pathname) {
|
|
86
|
-
// Parse the request body
|
|
87
|
-
const { userAddress, paymentRequirements, x402Version } = req.body;
|
|
88
|
-
try {
|
|
89
|
-
// Call the facilitator to open the tab
|
|
90
|
-
const openTabResponse = await facilitatorClient.openTab(userAddress, paymentRequirements, tabConfig.ttlSeconds, x402Version);
|
|
91
|
-
// Return the response
|
|
92
|
-
return res.json(openTabResponse);
|
|
93
|
-
}
|
|
94
|
-
catch (error) {
|
|
95
|
-
if (isOpenTabHttpError(error)) {
|
|
96
|
-
return res.status(error.status).json(error.response);
|
|
97
|
-
}
|
|
98
|
-
console.error('Failed to open tab:', error);
|
|
99
|
-
return res.status(500).json({
|
|
100
|
-
error: 'Failed to open tab',
|
|
101
|
-
details: error instanceof Error ? error.message : 'Unknown error',
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
catch (urlError) {
|
|
107
|
-
console.error('Invalid advertisedEndpoint URL:', urlError);
|
|
108
|
-
}
|
|
109
79
|
// Create adapter and context
|
|
110
80
|
const adapter = new ExpressAdapter(req);
|
|
111
81
|
const context = {
|
|
@@ -165,21 +135,21 @@ export function paymentMiddlewareFromHTTPServer(httpServer, tabConfig, paywallCo
|
|
|
165
135
|
const endPromise = new Promise((resolve) => {
|
|
166
136
|
endCalled = resolve;
|
|
167
137
|
});
|
|
168
|
-
res.writeHead =
|
|
138
|
+
res.writeHead = ((...args) => {
|
|
169
139
|
if (!settled) {
|
|
170
140
|
bufferedCalls.push(['writeHead', args]);
|
|
171
141
|
return res;
|
|
172
142
|
}
|
|
173
143
|
return originalWriteHead(...args);
|
|
174
|
-
};
|
|
175
|
-
res.write =
|
|
144
|
+
});
|
|
145
|
+
res.write = ((...args) => {
|
|
176
146
|
if (!settled) {
|
|
177
147
|
bufferedCalls.push(['write', args]);
|
|
178
148
|
return true;
|
|
179
149
|
}
|
|
180
150
|
return originalWrite(...args);
|
|
181
|
-
};
|
|
182
|
-
res.end =
|
|
151
|
+
});
|
|
152
|
+
res.end = ((...args) => {
|
|
183
153
|
if (!settled) {
|
|
184
154
|
bufferedCalls.push(['end', args]);
|
|
185
155
|
// Signal that the handler has finished
|
|
@@ -187,8 +157,8 @@ export function paymentMiddlewareFromHTTPServer(httpServer, tabConfig, paywallCo
|
|
|
187
157
|
return res;
|
|
188
158
|
}
|
|
189
159
|
return originalEnd(...args);
|
|
190
|
-
};
|
|
191
|
-
res.flushHeaders =
|
|
160
|
+
});
|
|
161
|
+
res.flushHeaders = () => {
|
|
192
162
|
if (!settled) {
|
|
193
163
|
bufferedCalls.push(['flushHeaders', []]);
|
|
194
164
|
return;
|
|
@@ -279,7 +249,6 @@ export function paymentMiddlewareFromHTTPServer(httpServer, tabConfig, paywallCo
|
|
|
279
249
|
*
|
|
280
250
|
* @param routes - Route configurations for protected endpoints
|
|
281
251
|
* @param server - Pre-configured x402ResourceServer instance
|
|
282
|
-
* @param tabConfig - Configuration for payment tab handling (endpoint URL and TTL)
|
|
283
252
|
* @param paywallConfig - Optional configuration for the built-in paywall UI
|
|
284
253
|
* @param paywall - Optional custom paywall provider (overrides default)
|
|
285
254
|
* @param syncFacilitatorOnStart - Whether to sync with the facilitator on startup (defaults to true)
|
|
@@ -287,22 +256,18 @@ export function paymentMiddlewareFromHTTPServer(httpServer, tabConfig, paywallCo
|
|
|
287
256
|
*
|
|
288
257
|
* @example
|
|
289
258
|
* ```typescript
|
|
290
|
-
* import { paymentMiddleware } from "@x402/express";
|
|
259
|
+
* import { paymentMiddleware } from "@4mica/x402/server/express";
|
|
291
260
|
*
|
|
292
261
|
* const server = new x402ResourceServer(myFacilitatorClient)
|
|
293
262
|
* .register(NETWORK, new ExactEvmScheme());
|
|
294
263
|
*
|
|
295
|
-
* app.use(paymentMiddleware(
|
|
296
|
-
* routes,
|
|
297
|
-
* server,
|
|
298
|
-
* { advertisedEndpoint: "https://api.example.com/x402/tab" },
|
|
299
|
-
* ));
|
|
264
|
+
* app.use(paymentMiddleware(routes, server));
|
|
300
265
|
* ```
|
|
301
266
|
*/
|
|
302
|
-
export function paymentMiddleware(routes, server,
|
|
267
|
+
export function paymentMiddleware(routes, server, paywallConfig, paywall, syncFacilitatorOnStart = true) {
|
|
303
268
|
// Create the x402 HTTP server instance with the resource server
|
|
304
269
|
const httpServer = new x402HTTPResourceServer(server, routes);
|
|
305
|
-
return paymentMiddlewareFromHTTPServer(httpServer,
|
|
270
|
+
return paymentMiddlewareFromHTTPServer(httpServer, paywallConfig, paywall, syncFacilitatorOnStart);
|
|
306
271
|
}
|
|
307
272
|
/**
|
|
308
273
|
* Express payment middleware for x402 protocol (configuration-based).
|
|
@@ -311,7 +276,6 @@ export function paymentMiddleware(routes, server, tabConfig, paywallConfig, payw
|
|
|
311
276
|
* This function creates and configures the x402ResourceServer internally.
|
|
312
277
|
*
|
|
313
278
|
* @param routes - Route configurations for protected endpoints
|
|
314
|
-
* @param tabConfig - Configuration for payment tab handling
|
|
315
279
|
* @param facilitatorClients - Optional facilitator client(s) for payment processing
|
|
316
280
|
* @param schemes - Optional array of scheme registrations for server-side payment processing
|
|
317
281
|
* @param paywallConfig - Optional configuration for the built-in paywall UI
|
|
@@ -321,15 +285,12 @@ export function paymentMiddleware(routes, server, tabConfig, paywallConfig, payw
|
|
|
321
285
|
*
|
|
322
286
|
* @example
|
|
323
287
|
* ```typescript
|
|
324
|
-
* import { paymentMiddlewareFromConfig } from "@x402/express";
|
|
288
|
+
* import { paymentMiddlewareFromConfig } from "@4mica/x402/server/express";
|
|
325
289
|
*
|
|
326
|
-
* app.use(paymentMiddlewareFromConfig(
|
|
327
|
-
* routes,
|
|
328
|
-
* { advertisedEndpoint: "https://api.example.com/x402/tab" },
|
|
329
|
-
* ));
|
|
290
|
+
* app.use(paymentMiddlewareFromConfig(routes));
|
|
330
291
|
* ```
|
|
331
292
|
*/
|
|
332
|
-
export function paymentMiddlewareFromConfig(routes,
|
|
293
|
+
export function paymentMiddlewareFromConfig(routes, facilitatorClients, schemes, paywallConfig, paywall, syncFacilitatorOnStart = true) {
|
|
333
294
|
const facilitators = facilitatorClients
|
|
334
295
|
? Array.isArray(facilitatorClients)
|
|
335
296
|
? facilitatorClients
|
|
@@ -346,6 +307,6 @@ export function paymentMiddlewareFromConfig(routes, tabConfig, facilitatorClient
|
|
|
346
307
|
}
|
|
347
308
|
// Use the direct paymentMiddleware with the configured server
|
|
348
309
|
// Note: paymentMiddleware handles dynamic bazaar registration
|
|
349
|
-
return paymentMiddleware(routes, ResourceServer,
|
|
310
|
+
return paymentMiddleware(routes, ResourceServer, paywallConfig, paywall, syncFacilitatorOnStart);
|
|
350
311
|
}
|
|
351
312
|
export { ExpressAdapter } from './adapter.js';
|
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
import { FacilitatorConfig, HTTPFacilitatorClient } from '@x402/core/server';
|
|
2
|
-
import {
|
|
3
|
-
export interface OpenTabRequest {
|
|
4
|
-
userAddress: string;
|
|
5
|
-
recipientAddress: string;
|
|
6
|
-
network?: Network;
|
|
7
|
-
erc20Token?: string;
|
|
8
|
-
ttlSeconds?: number;
|
|
9
|
-
}
|
|
10
|
-
export interface OpenTabResponse {
|
|
11
|
-
tabId: string;
|
|
12
|
-
userAddress: string;
|
|
13
|
-
recipientAddress: string;
|
|
14
|
-
assetAddress: string;
|
|
15
|
-
startTimestamp: number;
|
|
16
|
-
ttlSeconds: number;
|
|
17
|
-
nextReqId: string;
|
|
18
|
-
}
|
|
1
|
+
import { type FacilitatorConfig, HTTPFacilitatorClient } from '@x402/core/server';
|
|
2
|
+
import type { PaymentPayload, PaymentRequirements, SettleResponse } from '@x402/core/types';
|
|
19
3
|
export interface CertificateResponse {
|
|
20
4
|
claims: string;
|
|
21
5
|
signature: string;
|
|
@@ -26,14 +10,8 @@ export type FourMicaSettleResponse = SettleResponse & {
|
|
|
26
10
|
networkId?: string;
|
|
27
11
|
error?: string;
|
|
28
12
|
};
|
|
29
|
-
export declare class OpenTabError extends Error {
|
|
30
|
-
readonly status: number;
|
|
31
|
-
readonly response: OpenTabResponse;
|
|
32
|
-
constructor(status: number, response: OpenTabResponse);
|
|
33
|
-
}
|
|
34
13
|
export declare class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
|
|
35
14
|
constructor(config?: FacilitatorConfig);
|
|
36
|
-
openTab(userAddress: string, paymentRequirements: PaymentRequirements, ttlSeconds?: number, guaranteeVersion?: number): Promise<OpenTabResponse>;
|
|
37
15
|
settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<FourMicaSettleResponse>;
|
|
38
16
|
/**
|
|
39
17
|
* Helper to convert objects to JSON-safe format.
|
|
@@ -1,45 +1,9 @@
|
|
|
1
1
|
import { HTTPFacilitatorClient } from '@x402/core/server';
|
|
2
2
|
const DEFAULT_FACILITATOR_URL = 'https://x402.4mica.xyz';
|
|
3
|
-
export class OpenTabError extends Error {
|
|
4
|
-
constructor(status, response) {
|
|
5
|
-
super(`OpenTab failed with status ${status}`);
|
|
6
|
-
this.status = status;
|
|
7
|
-
this.response = response;
|
|
8
|
-
this.name = 'OpenTabError';
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
3
|
export class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
|
|
12
4
|
constructor(config) {
|
|
13
5
|
super({ ...config, url: config?.url ?? DEFAULT_FACILITATOR_URL });
|
|
14
6
|
}
|
|
15
|
-
async openTab(userAddress, paymentRequirements, ttlSeconds, guaranteeVersion) {
|
|
16
|
-
let headers = {
|
|
17
|
-
'Content-Type': 'application/json',
|
|
18
|
-
};
|
|
19
|
-
const authHeaders = await this.createAuthHeaders('tabs');
|
|
20
|
-
headers = { ...headers, ...authHeaders.headers };
|
|
21
|
-
const response = await fetch(`${this.url}/tabs`, {
|
|
22
|
-
method: 'POST',
|
|
23
|
-
headers,
|
|
24
|
-
body: JSON.stringify(this.safeJson({
|
|
25
|
-
userAddress,
|
|
26
|
-
recipientAddress: paymentRequirements.payTo,
|
|
27
|
-
network: paymentRequirements.network,
|
|
28
|
-
erc20Token: paymentRequirements.asset,
|
|
29
|
-
ttlSeconds,
|
|
30
|
-
guaranteeVersion: guaranteeVersion ?? 1,
|
|
31
|
-
})),
|
|
32
|
-
});
|
|
33
|
-
const data = await response.json();
|
|
34
|
-
if (typeof data === 'object' && data !== null && 'tabId' in data) {
|
|
35
|
-
const openTabResponse = data;
|
|
36
|
-
if (!response.ok) {
|
|
37
|
-
throw new OpenTabError(response.status, openTabResponse);
|
|
38
|
-
}
|
|
39
|
-
return openTabResponse;
|
|
40
|
-
}
|
|
41
|
-
throw new Error(`Facilitator openTab failed (${response.status}): ${JSON.stringify(data)}`);
|
|
42
|
-
}
|
|
43
7
|
async settle(paymentPayload, paymentRequirements) {
|
|
44
8
|
let headers = {
|
|
45
9
|
'Content-Type': 'application/json',
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
+
export type { PaywallConfig, PaywallProvider, RouteValidationError } from '@x402/core/server';
|
|
2
|
+
export { RouteConfigurationError, x402HTTPResourceServer, x402ResourceServer, } from '@x402/core/server';
|
|
1
3
|
export * from './facilitator.js';
|
|
2
4
|
export * from './scheme.js';
|
|
3
|
-
export { x402ResourceServer, x402HTTPResourceServer } from '@x402/core/server';
|
|
4
|
-
export type { PaywallProvider, PaywallConfig } from '@x402/core/server';
|
|
5
|
-
export { RouteConfigurationError } from '@x402/core/server';
|
|
6
|
-
export type { RouteValidationError } from '@x402/core/server';
|
package/dist/server/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
+
export { RouteConfigurationError, x402HTTPResourceServer, x402ResourceServer, } from '@x402/core/server';
|
|
1
2
|
export * from './facilitator.js';
|
|
2
3
|
export * from './scheme.js';
|
|
3
|
-
export { x402ResourceServer, x402HTTPResourceServer } from '@x402/core/server';
|
|
4
|
-
export { RouteConfigurationError } from '@x402/core/server';
|
package/dist/server/scheme.d.ts
CHANGED
|
@@ -1,13 +1,46 @@
|
|
|
1
|
-
import { AssetAmount, Network, PaymentRequirements, Price, SchemeNetworkServer
|
|
1
|
+
import type { AssetAmount, MoneyParser, Network, PaymentFlowConfig, PaymentRequirements, Price, SchemeNetworkServer } from '@x402/core/types';
|
|
2
2
|
export declare const SUPPORTED_NETWORKS: Network[];
|
|
3
|
+
/** The token a `Money` price resolves to, as core lists it for the network. */
|
|
4
|
+
export interface DefaultAsset {
|
|
5
|
+
address: string;
|
|
6
|
+
decimals: number;
|
|
7
|
+
}
|
|
8
|
+
export interface FourMicaEvmSchemeOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Core API URL per network. Defaults to the hosted deployments in `@4mica/sdk`'s
|
|
11
|
+
* `NETWORKS`; set an entry to point a network at a self-hosted core.
|
|
12
|
+
*/
|
|
13
|
+
coreUrls?: Partial<Record<Network, string>>;
|
|
14
|
+
/**
|
|
15
|
+
* Symbol of the token a `Money` price (`"$0.10"`) is denominated in, matched
|
|
16
|
+
* case-insensitively against core's token list. Defaults to `USDC`.
|
|
17
|
+
*/
|
|
18
|
+
stablecoinSymbol?: string;
|
|
19
|
+
}
|
|
3
20
|
/**
|
|
4
21
|
* EVM server implementation for the 4mica payment scheme.
|
|
22
|
+
*
|
|
23
|
+
* A `Money` price resolves to the stablecoin core lists for the network
|
|
24
|
+
* (`GET /core/tokens`), so the advertised `asset` is always one core accepts a
|
|
25
|
+
* guarantee against. Every requirement also carries core's EIP-712 domain in
|
|
26
|
+
* `extra` (`name`, `version`, `verifyingContract`, from
|
|
27
|
+
* `GET /core/public-params`), so a payer can sign without calling core. Both
|
|
28
|
+
* are fetched once per network and cached for the life of the instance.
|
|
5
29
|
*/
|
|
6
30
|
export declare class FourMicaEvmScheme implements SchemeNetworkServer {
|
|
7
|
-
readonly advertisedTabEndpoint: string;
|
|
8
31
|
readonly scheme = "4mica-credit";
|
|
32
|
+
readonly defaultAssetTransferMethod = "default";
|
|
33
|
+
readonly paymentFlows: Readonly<Record<string, PaymentFlowConfig>>;
|
|
9
34
|
private moneyParsers;
|
|
10
|
-
|
|
35
|
+
private readonly coreUrls;
|
|
36
|
+
private readonly stablecoinSymbol;
|
|
37
|
+
private readonly defaultAssets;
|
|
38
|
+
private readonly domains;
|
|
39
|
+
constructor(options?: FourMicaEvmSchemeOptions);
|
|
40
|
+
/** Core's token list. Private static so tests can stub the network call. */
|
|
41
|
+
private static loadSupportedTokens;
|
|
42
|
+
/** Core's public parameters. Private static so tests can stub the network call. */
|
|
43
|
+
private static loadPublicParams;
|
|
11
44
|
/**
|
|
12
45
|
* Register a custom money parser in the parser chain.
|
|
13
46
|
* Multiple parsers can be registered - they will be tried in registration order.
|
|
@@ -33,7 +66,7 @@ export declare class FourMicaEvmScheme implements SchemeNetworkServer {
|
|
|
33
66
|
* Parses a price into an asset amount.
|
|
34
67
|
* If price is already an AssetAmount, returns it directly.
|
|
35
68
|
* If price is Money (string | number), parses to decimal and tries custom parsers.
|
|
36
|
-
* Falls back to
|
|
69
|
+
* Falls back to the stablecoin core lists for the network if all custom parsers return null.
|
|
37
70
|
*
|
|
38
71
|
* @param price - The price to parse
|
|
39
72
|
* @param network - The network to use
|
|
@@ -41,7 +74,12 @@ export declare class FourMicaEvmScheme implements SchemeNetworkServer {
|
|
|
41
74
|
*/
|
|
42
75
|
parsePrice(price: Price, network: Network): Promise<AssetAmount>;
|
|
43
76
|
/**
|
|
44
|
-
* Build payment requirements for this scheme/network combination
|
|
77
|
+
* Build payment requirements for this scheme/network combination.
|
|
78
|
+
*
|
|
79
|
+
* Adds core's EIP-712 domain to `extra` so the payer can sign without a
|
|
80
|
+
* round trip to core. A domain the resource server set itself is kept; only
|
|
81
|
+
* the missing keys are filled in. The chain id is not added: a client takes
|
|
82
|
+
* it from `network`.
|
|
45
83
|
*
|
|
46
84
|
* @param paymentRequirements - The base payment requirements
|
|
47
85
|
* @param supportedKind - The supported kind from facilitator (unused)
|
|
@@ -68,7 +106,7 @@ export declare class FourMicaEvmScheme implements SchemeNetworkServer {
|
|
|
68
106
|
private parseMoneyToDecimal;
|
|
69
107
|
/**
|
|
70
108
|
* Default money conversion implementation.
|
|
71
|
-
* Converts decimal amount to the
|
|
109
|
+
* Converts a decimal amount to the stablecoin core lists for the network.
|
|
72
110
|
*
|
|
73
111
|
* @param amount - The decimal amount (e.g., 1.50)
|
|
74
112
|
* @param network - The network to use
|
|
@@ -83,11 +121,17 @@ export declare class FourMicaEvmScheme implements SchemeNetworkServer {
|
|
|
83
121
|
* @returns The token amount as a string
|
|
84
122
|
*/
|
|
85
123
|
private convertToTokenAmount;
|
|
124
|
+
private coreUrlFor;
|
|
86
125
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* @param network - The network to get asset info for
|
|
90
|
-
* @returns The asset information including address, name, version, and decimals
|
|
126
|
+
* The stablecoin core lists for `network`, fetched once and cached. A failed
|
|
127
|
+
* lookup is not cached, so the next price parse retries.
|
|
91
128
|
*/
|
|
92
129
|
private getDefaultAsset;
|
|
130
|
+
private resolveDefaultAsset;
|
|
131
|
+
/**
|
|
132
|
+
* Core's EIP-712 domain for `network`, fetched once and cached. A failed
|
|
133
|
+
* lookup is not cached, so the next 402 retries.
|
|
134
|
+
*/
|
|
135
|
+
private getDomain;
|
|
136
|
+
private resolveDomain;
|
|
93
137
|
}
|