@4mica/x402 1.2.1 → 1.2.3

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.
@@ -7,6 +7,9 @@ app.use(express.json())
7
7
 
8
8
  const PORT = process.env.PORT || 3000
9
9
  const PAY_TO_ADDRESS = process.env.PAY_TO_ADDRESS
10
+ // This endpoint is hosted by this resource server. Clients call it after receiving
11
+ // a 402 response, and the middleware below forwards the tab-open request to the
12
+ // 4Mica facilitator on the server's behalf.
10
13
  const ADVERTISED_ENDPOINT =
11
14
  process.env.ADVERTISED_ENDPOINT || `http://localhost:${PORT}/payment/tab`
12
15
 
@@ -29,6 +32,8 @@ app.use(
29
32
  },
30
33
  },
31
34
  {
35
+ // The middleware injects this URL into paymentRequirements.extra.tabEndpoint
36
+ // and also serves the POST route on this Express app.
32
37
  advertisedEndpoint: ADVERTISED_ENDPOINT,
33
38
  ttlSeconds: 3600, // 1 hour
34
39
  }
@@ -70,5 +75,7 @@ app.listen(PORT, () => {
70
75
  console.log(`x402 Demo Server running on http://localhost:${PORT}`)
71
76
  console.log(`Protected endpoint: http://localhost:${PORT}/api/premium-data`)
72
77
  console.log(`Payment required: $0.01 (4mica credit on Sepolia)`)
73
- console.log(`Payment tab endpoint: ${ADVERTISED_ENDPOINT}`)
78
+ console.log(`Tab endpoint hosted by this server: POST ${ADVERTISED_ENDPOINT}`)
79
+ console.log('Who calls it: payer clients after a 402 Payment Required response')
80
+ console.log('What it does: opens or reuses a tab via the 4Mica facilitator and returns tab JSON')
74
81
  })
@@ -84,10 +84,10 @@ export function paymentMiddlewareFromHTTPServer(httpServer, tabConfig, paywallCo
84
84
  const advertisedUrl = new URL(tabConfig.advertisedEndpoint);
85
85
  if (req.path === advertisedUrl.pathname) {
86
86
  // Parse the request body
87
- const { userAddress, paymentRequirements } = req.body;
87
+ const { userAddress, paymentRequirements, x402Version } = req.body;
88
88
  try {
89
89
  // Call the facilitator to open the tab
90
- const openTabResponse = await facilitatorClient.openTab(userAddress, paymentRequirements, tabConfig.ttlSeconds);
90
+ const openTabResponse = await facilitatorClient.openTab(userAddress, paymentRequirements, tabConfig.ttlSeconds, x402Version);
91
91
  // Return the response
92
92
  return res.json(openTabResponse);
93
93
  }
@@ -33,7 +33,7 @@ export declare class OpenTabError extends Error {
33
33
  }
34
34
  export declare class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
35
35
  constructor(config?: FacilitatorConfig);
36
- openTab(userAddress: string, paymentRequirements: PaymentRequirements, ttlSeconds?: number): Promise<OpenTabResponse>;
36
+ openTab(userAddress: string, paymentRequirements: PaymentRequirements, ttlSeconds?: number, guaranteeVersion?: number): Promise<OpenTabResponse>;
37
37
  settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<FourMicaSettleResponse>;
38
38
  /**
39
39
  * Helper to convert objects to JSON-safe format.
@@ -12,7 +12,7 @@ export class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
12
12
  constructor(config) {
13
13
  super({ ...config, url: config?.url ?? DEFAULT_FACILITATOR_URL });
14
14
  }
15
- async openTab(userAddress, paymentRequirements, ttlSeconds) {
15
+ async openTab(userAddress, paymentRequirements, ttlSeconds, guaranteeVersion) {
16
16
  let headers = {
17
17
  'Content-Type': 'application/json',
18
18
  };
@@ -27,6 +27,7 @@ export class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
27
27
  network: paymentRequirements.network,
28
28
  erc20Token: paymentRequirements.asset,
29
29
  ttlSeconds,
30
+ guaranteeVersion: guaranteeVersion ?? 1,
30
31
  })),
31
32
  });
32
33
  const data = await response.json();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4mica/x402",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "type": "module",
5
5
  "description": "TypeScript x402 utilities for interacting with the 4Mica payment network",
6
6
  "license": "MIT",
@@ -168,14 +168,15 @@ export function paymentMiddlewareFromHTTPServer(
168
168
  const advertisedUrl = new URL(tabConfig.advertisedEndpoint)
169
169
  if (req.path === advertisedUrl.pathname) {
170
170
  // Parse the request body
171
- const { userAddress, paymentRequirements } = req.body
171
+ const { userAddress, paymentRequirements, x402Version } = req.body
172
172
 
173
173
  try {
174
174
  // Call the facilitator to open the tab
175
175
  const openTabResponse = await facilitatorClient.openTab(
176
176
  userAddress,
177
177
  paymentRequirements,
178
- tabConfig.ttlSeconds
178
+ tabConfig.ttlSeconds,
179
+ x402Version
179
180
  )
180
181
 
181
182
  // Return the response
@@ -51,7 +51,8 @@ export class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
51
51
  async openTab(
52
52
  userAddress: string,
53
53
  paymentRequirements: PaymentRequirements,
54
- ttlSeconds?: number
54
+ ttlSeconds?: number,
55
+ guaranteeVersion?: number
55
56
  ): Promise<OpenTabResponse> {
56
57
  let headers: Record<string, string> = {
57
58
  'Content-Type': 'application/json',
@@ -70,6 +71,7 @@ export class FourMicaFacilitatorClient extends HTTPFacilitatorClient {
70
71
  network: paymentRequirements.network,
71
72
  erc20Token: paymentRequirements.asset,
72
73
  ttlSeconds,
74
+ guaranteeVersion: guaranteeVersion ?? 1,
73
75
  })
74
76
  ),
75
77
  })