create-mantle-facilitator 0.2.1 → 0.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-mantle-facilitator",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  import express from "express";
3
+ import cors from "cors";
3
4
 
4
5
  // src/config.ts
5
6
  import "dotenv/config";
@@ -97,6 +98,14 @@ function supportedRoute(_req, res) {
97
98
 
98
99
  // src/x402.ts
99
100
  import { ethers as ethers3 } from "ethers";
101
+ function getChainIdFromNetwork(network) {
102
+ switch (network) {
103
+ case "mantle-mainnet":
104
+ return 5e3;
105
+ default:
106
+ throw new Error(`Unsupported network: ${network}`);
107
+ }
108
+ }
100
109
  function decodePaymentHeader(paymentHeader) {
101
110
  try {
102
111
  const json = Buffer.from(paymentHeader, "base64").toString("utf8");
@@ -124,12 +133,13 @@ function validateHeaderShape(headerObj) {
124
133
  }
125
134
  return { ok: true };
126
135
  }
127
- function getUsdcTypedData(authorization) {
136
+ function getUsdcTypedData(authorization, paymentRequirements) {
137
+ const chainId = getChainIdFromNetwork(paymentRequirements.network);
128
138
  const domain = {
129
139
  name: "USD Coin",
130
140
  version: "2",
131
- chainId: CONFIG.chainId,
132
- verifyingContract: CONFIG.usdcAddress
141
+ chainId,
142
+ verifyingContract: paymentRequirements.asset
133
143
  };
134
144
  const types = {
135
145
  TransferWithAuthorization: [
@@ -143,8 +153,8 @@ function getUsdcTypedData(authorization) {
143
153
  };
144
154
  return { domain, types, primaryType: "TransferWithAuthorization", message: authorization };
145
155
  }
146
- function verifyAuthorizationSignature(authorization, signature) {
147
- const { domain, types, message } = getUsdcTypedData(authorization);
156
+ function verifyAuthorizationSignature(authorization, signature, paymentRequirements) {
157
+ const { domain, types, message } = getUsdcTypedData(authorization, paymentRequirements);
148
158
  return ethers3.verifyTypedData(domain, types, message, signature);
149
159
  }
150
160
  function verifyPayment(headerObj, paymentRequirements) {
@@ -165,8 +175,22 @@ function verifyPayment(headerObj, paymentRequirements) {
165
175
  if (authValue !== maxValue) {
166
176
  return { isValid: false, invalidReason: "Authorization.value does not match maxAmountRequired" };
167
177
  }
178
+ const expectedChainId = CONFIG.chainId;
179
+ const requestChainId = getChainIdFromNetwork(paymentRequirements.network);
180
+ if (requestChainId !== expectedChainId) {
181
+ return {
182
+ isValid: false,
183
+ invalidReason: `Network mismatch: expected chainId ${expectedChainId}, got ${requestChainId}`
184
+ };
185
+ }
186
+ if (paymentRequirements.asset.toLowerCase() !== CONFIG.usdcAddress.toLowerCase()) {
187
+ return {
188
+ isValid: false,
189
+ invalidReason: `Asset mismatch: expected ${CONFIG.usdcAddress}, got ${paymentRequirements.asset}`
190
+ };
191
+ }
168
192
  try {
169
- const recovered = verifyAuthorizationSignature(authorization, signature);
193
+ const recovered = verifyAuthorizationSignature(authorization, signature, paymentRequirements);
170
194
  if (recovered.toLowerCase() !== authorization.from.toLowerCase()) {
171
195
  return { isValid: false, invalidReason: "Signature does not match authorization.from" };
172
196
  }
@@ -313,6 +337,13 @@ async function settleRoute(req, res) {
313
337
 
314
338
  // src/index.ts
315
339
  var app = express();
340
+ app.use(cors({
341
+ origin: "*",
342
+ // Allow all origins in development
343
+ methods: ["GET", "POST", "OPTIONS"],
344
+ allowedHeaders: ["Content-Type", "Authorization"],
345
+ credentials: true
346
+ }));
316
347
  app.use(express.json({ limit: "1mb" }));
317
348
  app.get("/health", healthRoute);
318
349
  app.get("/supported", supportedRoute);