moltspay 0.8.0 → 0.8.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.
@@ -69,14 +69,15 @@ interface MoltsPayServerOptions {
69
69
  port?: number;
70
70
  host?: string;
71
71
  chargeExpirySecs?: number;
72
- /** Private key for claiming payments (can also use MOLTSPAY_PRIVATE_KEY env) */
73
- privateKey?: string;
72
+ /** x402 Facilitator URL (default: https://x402.org/facilitator) */
73
+ facilitatorUrl?: string;
74
74
  }
75
75
 
76
76
  /**
77
77
  * MoltsPay Server - Payment infrastructure for AI Agents
78
78
  *
79
- * Uses x402 protocol for gasless, pay-for-success payments.
79
+ * Uses x402 protocol with public facilitator (https://x402.org/facilitator).
80
+ * Server does NOT need private key - facilitator handles on-chain settlement.
80
81
  *
81
82
  * Usage:
82
83
  * const server = new MoltsPayServer('./moltspay.services.json');
@@ -88,17 +89,19 @@ declare class MoltsPayServer {
88
89
  private manifest;
89
90
  private skills;
90
91
  private options;
91
- private provider;
92
- private wallet;
92
+ private facilitatorUrl;
93
93
  constructor(servicesPath: string, options?: MoltsPayServerOptions);
94
94
  /**
95
95
  * Register a skill handler for a service
96
96
  */
97
97
  skill(serviceId: string, handler: SkillFunction): this;
98
98
  /**
99
- * Start the server
99
+ * Start HTTP server
100
100
  */
101
101
  listen(port?: number): void;
102
+ /**
103
+ * Handle incoming request
104
+ */
102
105
  private handleRequest;
103
106
  /**
104
107
  * GET /services - List available services
@@ -115,13 +118,17 @@ declare class MoltsPayServer {
115
118
  */
116
119
  private sendPaymentRequired;
117
120
  /**
118
- * Validate x402 payment payload
121
+ * Basic payment validation (before calling facilitator)
119
122
  */
120
123
  private validatePayment;
121
124
  /**
122
- * Claim payment using transferWithAuthorization
125
+ * Verify payment with facilitator
126
+ */
127
+ private verifyWithFacilitator;
128
+ /**
129
+ * Settle payment with facilitator (execute on-chain transfer)
123
130
  */
124
- private claimPayment;
131
+ private settleWithFacilitator;
125
132
  private readBody;
126
133
  private sendJson;
127
134
  }
@@ -69,14 +69,15 @@ interface MoltsPayServerOptions {
69
69
  port?: number;
70
70
  host?: string;
71
71
  chargeExpirySecs?: number;
72
- /** Private key for claiming payments (can also use MOLTSPAY_PRIVATE_KEY env) */
73
- privateKey?: string;
72
+ /** x402 Facilitator URL (default: https://x402.org/facilitator) */
73
+ facilitatorUrl?: string;
74
74
  }
75
75
 
76
76
  /**
77
77
  * MoltsPay Server - Payment infrastructure for AI Agents
78
78
  *
79
- * Uses x402 protocol for gasless, pay-for-success payments.
79
+ * Uses x402 protocol with public facilitator (https://x402.org/facilitator).
80
+ * Server does NOT need private key - facilitator handles on-chain settlement.
80
81
  *
81
82
  * Usage:
82
83
  * const server = new MoltsPayServer('./moltspay.services.json');
@@ -88,17 +89,19 @@ declare class MoltsPayServer {
88
89
  private manifest;
89
90
  private skills;
90
91
  private options;
91
- private provider;
92
- private wallet;
92
+ private facilitatorUrl;
93
93
  constructor(servicesPath: string, options?: MoltsPayServerOptions);
94
94
  /**
95
95
  * Register a skill handler for a service
96
96
  */
97
97
  skill(serviceId: string, handler: SkillFunction): this;
98
98
  /**
99
- * Start the server
99
+ * Start HTTP server
100
100
  */
101
101
  listen(port?: number): void;
102
+ /**
103
+ * Handle incoming request
104
+ */
102
105
  private handleRequest;
103
106
  /**
104
107
  * GET /services - List available services
@@ -115,13 +118,17 @@ declare class MoltsPayServer {
115
118
  */
116
119
  private sendPaymentRequired;
117
120
  /**
118
- * Validate x402 payment payload
121
+ * Basic payment validation (before calling facilitator)
119
122
  */
120
123
  private validatePayment;
121
124
  /**
122
- * Claim payment using transferWithAuthorization
125
+ * Verify payment with facilitator
126
+ */
127
+ private verifyWithFacilitator;
128
+ /**
129
+ * Settle payment with facilitator (execute on-chain transfer)
123
130
  */
124
- private claimPayment;
131
+ private settleWithFacilitator;
125
132
  private readBody;
126
133
  private sendJson;
127
134
  }
@@ -25,7 +25,6 @@ __export(server_exports, {
25
25
  module.exports = __toCommonJS(server_exports);
26
26
  var import_fs = require("fs");
27
27
  var import_http = require("http");
28
- var import_ethers = require("ethers");
29
28
 
30
29
  // src/chains/index.ts
31
30
  var CHAINS = {
@@ -89,34 +88,26 @@ function getChain(name) {
89
88
  var X402_VERSION = 2;
90
89
  var PAYMENT_REQUIRED_HEADER = "x-payment-required";
91
90
  var PAYMENT_HEADER = "x-payment";
91
+ var PAYMENT_RESPONSE_HEADER = "x-payment-response";
92
+ var DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator";
92
93
  var MoltsPayServer = class {
93
94
  manifest;
94
95
  skills = /* @__PURE__ */ new Map();
95
96
  options;
96
- provider = null;
97
- wallet = null;
97
+ facilitatorUrl;
98
98
  constructor(servicesPath, options = {}) {
99
99
  const content = (0, import_fs.readFileSync)(servicesPath, "utf-8");
100
100
  this.manifest = JSON.parse(content);
101
101
  this.options = {
102
102
  port: options.port || 3e3,
103
- host: options.host || "0.0.0.0",
104
- privateKey: options.privateKey || process.env.MOLTSPAY_PRIVATE_KEY
103
+ host: options.host || "0.0.0.0"
105
104
  };
106
- if (this.options.privateKey) {
107
- try {
108
- const chain = getChain(this.manifest.provider.chain);
109
- this.provider = new import_ethers.ethers.JsonRpcProvider(chain.rpc);
110
- this.wallet = new import_ethers.ethers.Wallet(this.options.privateKey, this.provider);
111
- console.log(`[MoltsPay] Payment wallet: ${this.wallet.address}`);
112
- } catch (err) {
113
- console.warn("[MoltsPay] Warning: Could not initialize wallet for payment claims");
114
- }
115
- }
105
+ this.facilitatorUrl = options.facilitatorUrl || DEFAULT_FACILITATOR_URL;
116
106
  console.log(`[MoltsPay] Loaded ${this.manifest.services.length} services from ${servicesPath}`);
117
107
  console.log(`[MoltsPay] Provider: ${this.manifest.provider.name}`);
118
108
  console.log(`[MoltsPay] Receive wallet: ${this.manifest.provider.wallet}`);
119
- console.log(`[MoltsPay] Protocol: x402 (gasless, pay-for-success)`);
109
+ console.log(`[MoltsPay] Facilitator: ${this.facilitatorUrl}`);
110
+ console.log(`[MoltsPay] Protocol: x402 (gasless for both client AND server)`);
120
111
  }
121
112
  /**
122
113
  * Register a skill handler for a service
@@ -126,48 +117,45 @@ var MoltsPayServer = class {
126
117
  if (!config) {
127
118
  throw new Error(`Service '${serviceId}' not found in manifest`);
128
119
  }
129
- this.skills.set(serviceId, {
130
- id: serviceId,
131
- config,
132
- handler
133
- });
134
- console.log(`[MoltsPay] Registered skill: ${serviceId} ($${config.price} ${config.currency})`);
120
+ this.skills.set(serviceId, { id: serviceId, config, handler });
135
121
  return this;
136
122
  }
137
123
  /**
138
- * Start the server
124
+ * Start HTTP server
139
125
  */
140
126
  listen(port) {
141
- const p = port || this.options.port;
127
+ const p = port || this.options.port || 3e3;
128
+ const host = this.options.host || "0.0.0.0";
142
129
  const server = (0, import_http.createServer)((req, res) => this.handleRequest(req, res));
143
- server.listen(p, this.options.host, () => {
144
- console.log(`[MoltsPay] Server listening on http://${this.options.host}:${p}`);
130
+ server.listen(p, host, () => {
131
+ console.log(`[MoltsPay] Server listening on http://${host}:${p}`);
145
132
  console.log(`[MoltsPay] Endpoints:`);
146
133
  console.log(` GET /services - List available services`);
147
134
  console.log(` POST /execute - Execute service (x402 payment)`);
148
135
  });
149
136
  }
137
+ /**
138
+ * Handle incoming request
139
+ */
150
140
  async handleRequest(req, res) {
151
- const url = new URL(req.url || "/", `http://${req.headers.host}`);
152
- const path = url.pathname;
153
- const method = req.method || "GET";
154
141
  res.setHeader("Access-Control-Allow-Origin", "*");
155
142
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
156
143
  res.setHeader("Access-Control-Allow-Headers", "Content-Type, X-Payment");
157
144
  res.setHeader("Access-Control-Expose-Headers", "X-Payment-Required, X-Payment-Response");
158
- if (method === "OPTIONS") {
145
+ if (req.method === "OPTIONS") {
159
146
  res.writeHead(204);
160
147
  res.end();
161
148
  return;
162
149
  }
163
150
  try {
164
- if (method === "GET" && path === "/services") {
151
+ const url = new URL(req.url || "/", `http://${req.headers.host}`);
152
+ if (url.pathname === "/services" && req.method === "GET") {
165
153
  return this.handleGetServices(res);
166
154
  }
167
- if (method === "POST" && path === "/execute") {
155
+ if (url.pathname === "/execute" && req.method === "POST") {
168
156
  const body = await this.readBody(req);
169
157
  const paymentHeader = req.headers[PAYMENT_HEADER];
170
- return this.handleExecute(body, paymentHeader, res);
158
+ return await this.handleExecute(body, paymentHeader, res);
171
159
  }
172
160
  this.sendJson(res, 404, { error: "Not found" });
173
161
  } catch (err) {
@@ -196,7 +184,8 @@ var MoltsPayServer = class {
196
184
  x402: {
197
185
  version: X402_VERSION,
198
186
  network: `eip155:${chain.chainId}`,
199
- schemes: ["exact"]
187
+ schemes: ["exact"],
188
+ facilitator: this.facilitatorUrl
200
189
  }
201
190
  });
202
191
  }
@@ -233,6 +222,11 @@ var MoltsPayServer = class {
233
222
  if (!validation.valid) {
234
223
  return this.sendJson(res, 402, { error: validation.error });
235
224
  }
225
+ console.log(`[MoltsPay] Verifying payment with facilitator...`);
226
+ const verifyResult = await this.verifyWithFacilitator(payment, skill.config);
227
+ if (!verifyResult.valid) {
228
+ return this.sendJson(res, 402, { error: `Payment verification failed: ${verifyResult.error}` });
229
+ }
236
230
  console.log(`[MoltsPay] Executing skill: ${service}`);
237
231
  let result;
238
232
  try {
@@ -244,19 +238,30 @@ var MoltsPayServer = class {
244
238
  message: err.message
245
239
  });
246
240
  }
247
- console.log(`[MoltsPay] Skill succeeded, claiming payment...`);
248
- let txHash = null;
241
+ console.log(`[MoltsPay] Skill succeeded, settling payment...`);
242
+ let settlement = null;
249
243
  try {
250
- txHash = await this.claimPayment(payment);
251
- console.log(`[MoltsPay] Payment claimed: ${txHash}`);
244
+ settlement = await this.settleWithFacilitator(payment, skill.config);
245
+ console.log(`[MoltsPay] Payment settled: ${settlement.transaction || "pending"}`);
252
246
  } catch (err) {
253
- console.error("[MoltsPay] Payment claim failed:", err.message);
247
+ console.error("[MoltsPay] Settlement failed:", err.message);
248
+ }
249
+ const responseHeaders = {};
250
+ if (settlement) {
251
+ const responsePayload = {
252
+ success: true,
253
+ transaction: settlement.transaction,
254
+ network: payment.network
255
+ };
256
+ responseHeaders[PAYMENT_RESPONSE_HEADER] = Buffer.from(
257
+ JSON.stringify(responsePayload)
258
+ ).toString("base64");
254
259
  }
255
260
  this.sendJson(res, 200, {
256
261
  success: true,
257
262
  result,
258
- payment: txHash ? { txHash, status: "claimed" } : { status: "pending" }
259
- });
263
+ payment: settlement ? { transaction: settlement.transaction, status: "settled" } : { status: "pending" }
264
+ }, responseHeaders);
260
265
  }
261
266
  /**
262
267
  * Return 402 with x402 payment requirements
@@ -269,7 +274,9 @@ var MoltsPayServer = class {
269
274
  network: `eip155:${chain.chainId}`,
270
275
  maxAmountRequired: amountInUnits,
271
276
  resource: this.manifest.provider.wallet,
272
- description: `${config.name} - $${config.price} ${config.currency}`
277
+ description: `${config.name} - $${config.price} ${config.currency}`,
278
+ // Include facilitator info for client
279
+ extra: JSON.stringify({ facilitator: this.facilitatorUrl })
273
280
  }];
274
281
  const encoded = Buffer.from(JSON.stringify(requirements)).toString("base64");
275
282
  res.writeHead(402, {
@@ -283,7 +290,7 @@ var MoltsPayServer = class {
283
290
  }, null, 2));
284
291
  }
285
292
  /**
286
- * Validate x402 payment payload
293
+ * Basic payment validation (before calling facilitator)
287
294
  */
288
295
  validatePayment(payment, config) {
289
296
  if (payment.x402Version !== X402_VERSION) {
@@ -297,51 +304,66 @@ var MoltsPayServer = class {
297
304
  if (payment.network !== expectedNetwork) {
298
305
  return { valid: false, error: `Network mismatch: expected ${expectedNetwork}` };
299
306
  }
300
- const auth = payment.payload.authorization;
301
- if (auth.to.toLowerCase() !== this.manifest.provider.wallet.toLowerCase()) {
302
- return { valid: false, error: "Payment recipient mismatch" };
303
- }
304
- const amount = Number(auth.value) / 1e6;
305
- if (amount < config.price) {
306
- return { valid: false, error: `Insufficient amount: $${amount} < $${config.price}` };
307
- }
308
- const now = Math.floor(Date.now() / 1e3);
309
- if (Number(auth.validBefore) < now) {
310
- return { valid: false, error: "Payment authorization expired" };
311
- }
312
- if (Number(auth.validAfter) > now) {
313
- return { valid: false, error: "Payment authorization not yet valid" };
314
- }
315
307
  return { valid: true };
316
308
  }
317
309
  /**
318
- * Claim payment using transferWithAuthorization
310
+ * Verify payment with facilitator
319
311
  */
320
- async claimPayment(payment) {
321
- if (!this.wallet || !this.provider) {
322
- throw new Error("Wallet not configured for payment claims");
312
+ async verifyWithFacilitator(payment, config) {
313
+ try {
314
+ const chain = getChain(this.manifest.provider.chain);
315
+ const amountInUnits = Math.floor(config.price * 1e6).toString();
316
+ const requirements = {
317
+ scheme: "exact",
318
+ network: `eip155:${chain.chainId}`,
319
+ maxAmountRequired: amountInUnits,
320
+ resource: this.manifest.provider.wallet
321
+ };
322
+ const response = await fetch(`${this.facilitatorUrl}/verify`, {
323
+ method: "POST",
324
+ headers: { "Content-Type": "application/json" },
325
+ body: JSON.stringify({
326
+ paymentPayload: payment,
327
+ paymentRequirements: requirements
328
+ })
329
+ });
330
+ const result = await response.json();
331
+ if (!response.ok || !result.isValid) {
332
+ return { valid: false, error: result.invalidReason || "Verification failed" };
333
+ }
334
+ return { valid: true };
335
+ } catch (err) {
336
+ return { valid: false, error: `Facilitator error: ${err.message}` };
323
337
  }
338
+ }
339
+ /**
340
+ * Settle payment with facilitator (execute on-chain transfer)
341
+ */
342
+ async settleWithFacilitator(payment, config) {
324
343
  const chain = getChain(this.manifest.provider.chain);
325
- const auth = payment.payload.authorization;
326
- const sig = payment.payload.signature;
327
- const { r, s, v } = import_ethers.ethers.Signature.from(sig);
328
- const usdcAbi = [
329
- "function transferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s)"
330
- ];
331
- const usdc = new import_ethers.ethers.Contract(chain.usdc, usdcAbi, this.wallet);
332
- const tx = await usdc.transferWithAuthorization(
333
- auth.from,
334
- auth.to,
335
- auth.value,
336
- auth.validAfter,
337
- auth.validBefore,
338
- auth.nonce,
339
- v,
340
- r,
341
- s
342
- );
343
- const receipt = await tx.wait();
344
- return receipt.hash;
344
+ const amountInUnits = Math.floor(config.price * 1e6).toString();
345
+ const requirements = {
346
+ scheme: "exact",
347
+ network: `eip155:${chain.chainId}`,
348
+ maxAmountRequired: amountInUnits,
349
+ resource: this.manifest.provider.wallet
350
+ };
351
+ const response = await fetch(`${this.facilitatorUrl}/settle`, {
352
+ method: "POST",
353
+ headers: { "Content-Type": "application/json" },
354
+ body: JSON.stringify({
355
+ paymentPayload: payment,
356
+ paymentRequirements: requirements
357
+ })
358
+ });
359
+ const result = await response.json();
360
+ if (!response.ok) {
361
+ throw new Error(result.error || "Settlement failed");
362
+ }
363
+ return {
364
+ transaction: result.transaction,
365
+ status: result.status || "settled"
366
+ };
345
367
  }
346
368
  async readBody(req) {
347
369
  return new Promise((resolve, reject) => {
@@ -357,8 +379,12 @@ var MoltsPayServer = class {
357
379
  req.on("error", reject);
358
380
  });
359
381
  }
360
- sendJson(res, status, data) {
361
- res.writeHead(status, { "Content-Type": "application/json" });
382
+ sendJson(res, status, data, extraHeaders) {
383
+ const headers = { "Content-Type": "application/json" };
384
+ if (extraHeaders) {
385
+ Object.assign(headers, extraHeaders);
386
+ }
387
+ res.writeHead(status, headers);
362
388
  res.end(JSON.stringify(data, null, 2));
363
389
  }
364
390
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/server/index.ts","../../src/chains/index.ts"],"sourcesContent":["/**\n * MoltsPay Server - Payment infrastructure for AI Agents\n * \n * Uses x402 protocol for gasless, pay-for-success payments.\n * \n * Usage:\n * const server = new MoltsPayServer('./moltspay.services.json');\n * server.skill('text-to-video', async (params) => { ... });\n * server.listen(3000);\n */\n\nimport { readFileSync } from 'fs';\nimport { createServer, IncomingMessage, ServerResponse } from 'http';\nimport { ethers } from 'ethers';\nimport { getChain } from '../chains/index.js';\nimport type { ChainName } from '../chains/index.js';\nimport {\n ServicesManifest,\n ServiceConfig,\n SkillFunction,\n RegisteredSkill,\n MoltsPayServerOptions,\n} from './types.js';\n\nexport * from './types.js';\n\n// x402 constants\nconst X402_VERSION = 2;\nconst PAYMENT_REQUIRED_HEADER = 'x-payment-required';\nconst PAYMENT_HEADER = 'x-payment';\n\ninterface EIP3009Authorization {\n from: string;\n to: string;\n value: string;\n validAfter: string;\n validBefore: string;\n nonce: string;\n}\n\ninterface X402PaymentPayload {\n x402Version: number;\n scheme: string;\n network: string;\n payload: {\n signature: string;\n authorization: EIP3009Authorization;\n };\n}\n\nexport class MoltsPayServer {\n private manifest: ServicesManifest;\n private skills: Map<string, RegisteredSkill> = new Map();\n private options: MoltsPayServerOptions;\n private provider: ethers.JsonRpcProvider | null = null;\n private wallet: ethers.Wallet | null = null;\n\n constructor(servicesPath: string, options: MoltsPayServerOptions = {}) {\n // Load services manifest\n const content = readFileSync(servicesPath, 'utf-8');\n this.manifest = JSON.parse(content) as ServicesManifest;\n \n this.options = {\n port: options.port || 3000,\n host: options.host || '0.0.0.0',\n privateKey: options.privateKey || process.env.MOLTSPAY_PRIVATE_KEY,\n };\n\n // Initialize provider and wallet for claiming payments\n if (this.options.privateKey) {\n try {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n this.provider = new ethers.JsonRpcProvider(chain.rpc);\n this.wallet = new ethers.Wallet(this.options.privateKey, this.provider);\n console.log(`[MoltsPay] Payment wallet: ${this.wallet.address}`);\n } catch (err) {\n console.warn('[MoltsPay] Warning: Could not initialize wallet for payment claims');\n }\n }\n\n console.log(`[MoltsPay] Loaded ${this.manifest.services.length} services from ${servicesPath}`);\n console.log(`[MoltsPay] Provider: ${this.manifest.provider.name}`);\n console.log(`[MoltsPay] Receive wallet: ${this.manifest.provider.wallet}`);\n console.log(`[MoltsPay] Protocol: x402 (gasless, pay-for-success)`);\n }\n\n /**\n * Register a skill handler for a service\n */\n skill(serviceId: string, handler: SkillFunction): this {\n const config = this.manifest.services.find(s => s.id === serviceId);\n if (!config) {\n throw new Error(`Service '${serviceId}' not found in manifest`);\n }\n\n this.skills.set(serviceId, {\n id: serviceId,\n config,\n handler,\n });\n\n console.log(`[MoltsPay] Registered skill: ${serviceId} ($${config.price} ${config.currency})`);\n return this;\n }\n\n /**\n * Start the server\n */\n listen(port?: number): void {\n const p = port || this.options.port!;\n \n const server = createServer((req, res) => this.handleRequest(req, res));\n \n server.listen(p, this.options.host, () => {\n console.log(`[MoltsPay] Server listening on http://${this.options.host}:${p}`);\n console.log(`[MoltsPay] Endpoints:`);\n console.log(` GET /services - List available services`);\n console.log(` POST /execute - Execute service (x402 payment)`);\n });\n }\n\n private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {\n const url = new URL(req.url || '/', `http://${req.headers.host}`);\n const path = url.pathname;\n const method = req.method || 'GET';\n\n // CORS headers\n res.setHeader('Access-Control-Allow-Origin', '*');\n res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');\n res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Payment');\n res.setHeader('Access-Control-Expose-Headers', 'X-Payment-Required, X-Payment-Response');\n\n if (method === 'OPTIONS') {\n res.writeHead(204);\n res.end();\n return;\n }\n\n try {\n if (method === 'GET' && path === '/services') {\n return this.handleGetServices(res);\n }\n\n if (method === 'POST' && path === '/execute') {\n const body = await this.readBody(req);\n const paymentHeader = req.headers[PAYMENT_HEADER] as string | undefined;\n return this.handleExecute(body, paymentHeader, res);\n }\n\n // Not found\n this.sendJson(res, 404, { error: 'Not found' });\n } catch (err: any) {\n console.error('[MoltsPay] Error:', err);\n this.sendJson(res, 500, { error: err.message || 'Internal error' });\n }\n }\n\n /**\n * GET /services - List available services\n */\n private handleGetServices(res: ServerResponse): void {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n \n const services = this.manifest.services.map(s => ({\n id: s.id,\n name: s.name,\n description: s.description,\n price: s.price,\n currency: s.currency,\n input: s.input,\n output: s.output,\n available: this.skills.has(s.id),\n }));\n\n this.sendJson(res, 200, {\n provider: this.manifest.provider,\n services,\n x402: {\n version: X402_VERSION,\n network: `eip155:${chain.chainId}`,\n schemes: ['exact'],\n },\n });\n }\n\n /**\n * POST /execute - Execute service with x402 payment\n * Body: { service: string, params: object }\n * Header: X-Payment (optional - if missing, returns 402)\n */\n private async handleExecute(\n body: any,\n paymentHeader: string | undefined,\n res: ServerResponse\n ): Promise<void> {\n const { service, params } = body;\n\n if (!service) {\n return this.sendJson(res, 400, { error: 'Missing service' });\n }\n\n const skill = this.skills.get(service);\n if (!skill) {\n return this.sendJson(res, 404, { error: `Service '${service}' not found or not registered` });\n }\n\n // Validate required params\n for (const [key, field] of Object.entries(skill.config.input)) {\n if (field.required && (!params || params[key] === undefined)) {\n return this.sendJson(res, 400, { error: `Missing required param: ${key}` });\n }\n }\n\n // If no payment header, return 402 with payment requirements\n if (!paymentHeader) {\n return this.sendPaymentRequired(skill.config, res);\n }\n\n // Verify payment\n let payment: X402PaymentPayload;\n try {\n const decoded = Buffer.from(paymentHeader, 'base64').toString('utf-8');\n payment = JSON.parse(decoded);\n } catch {\n return this.sendJson(res, 400, { error: 'Invalid X-Payment header' });\n }\n\n // Validate payment\n const validation = this.validatePayment(payment, skill.config);\n if (!validation.valid) {\n return this.sendJson(res, 402, { error: validation.error });\n }\n\n // Execute skill FIRST (pay-for-success)\n console.log(`[MoltsPay] Executing skill: ${service}`);\n let result: any;\n try {\n result = await skill.handler(params || {});\n } catch (err: any) {\n console.error('[MoltsPay] Skill execution failed:', err.message);\n // Don't claim payment if skill fails\n return this.sendJson(res, 500, {\n error: 'Service execution failed',\n message: err.message,\n });\n }\n\n // Skill succeeded - now claim payment\n console.log(`[MoltsPay] Skill succeeded, claiming payment...`);\n let txHash: string | null = null;\n try {\n txHash = await this.claimPayment(payment);\n console.log(`[MoltsPay] Payment claimed: ${txHash}`);\n } catch (err: any) {\n console.error('[MoltsPay] Payment claim failed:', err.message);\n // Still return result even if payment claim fails\n // (we can retry claiming later)\n }\n\n this.sendJson(res, 200, {\n success: true,\n result,\n payment: txHash ? { txHash, status: 'claimed' } : { status: 'pending' },\n });\n }\n\n /**\n * Return 402 with x402 payment requirements\n */\n private sendPaymentRequired(config: ServiceConfig, res: ServerResponse): void {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const amountInUnits = Math.floor(config.price * 1e6).toString();\n\n const requirements = [{\n scheme: 'exact',\n network: `eip155:${chain.chainId}`,\n maxAmountRequired: amountInUnits,\n resource: this.manifest.provider.wallet,\n description: `${config.name} - $${config.price} ${config.currency}`,\n }];\n\n const encoded = Buffer.from(JSON.stringify(requirements)).toString('base64');\n\n res.writeHead(402, {\n 'Content-Type': 'application/json',\n [PAYMENT_REQUIRED_HEADER]: encoded,\n });\n res.end(JSON.stringify({\n error: 'Payment required',\n message: `Service requires $${config.price} ${config.currency}`,\n x402: requirements[0],\n }, null, 2));\n }\n\n /**\n * Validate x402 payment payload\n */\n private validatePayment(\n payment: X402PaymentPayload,\n config: ServiceConfig\n ): { valid: boolean; error?: string } {\n if (payment.x402Version !== X402_VERSION) {\n return { valid: false, error: `Unsupported x402 version: ${payment.x402Version}` };\n }\n\n if (payment.scheme !== 'exact') {\n return { valid: false, error: `Unsupported scheme: ${payment.scheme}` };\n }\n\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const expectedNetwork = `eip155:${chain.chainId}`;\n if (payment.network !== expectedNetwork) {\n return { valid: false, error: `Network mismatch: expected ${expectedNetwork}` };\n }\n\n const auth = payment.payload.authorization;\n \n // Check recipient\n if (auth.to.toLowerCase() !== this.manifest.provider.wallet.toLowerCase()) {\n return { valid: false, error: 'Payment recipient mismatch' };\n }\n\n // Check amount\n const amount = Number(auth.value) / 1e6;\n if (amount < config.price) {\n return { valid: false, error: `Insufficient amount: $${amount} < $${config.price}` };\n }\n\n // Check expiry\n const now = Math.floor(Date.now() / 1000);\n if (Number(auth.validBefore) < now) {\n return { valid: false, error: 'Payment authorization expired' };\n }\n\n if (Number(auth.validAfter) > now) {\n return { valid: false, error: 'Payment authorization not yet valid' };\n }\n\n return { valid: true };\n }\n\n /**\n * Claim payment using transferWithAuthorization\n */\n private async claimPayment(payment: X402PaymentPayload): Promise<string> {\n if (!this.wallet || !this.provider) {\n throw new Error('Wallet not configured for payment claims');\n }\n\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const auth = payment.payload.authorization;\n const sig = payment.payload.signature;\n\n // Parse signature\n const { r, s, v } = ethers.Signature.from(sig);\n\n // USDC transferWithAuthorization ABI\n const usdcAbi = [\n 'function transferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s)'\n ];\n\n const usdc = new ethers.Contract(chain.usdc, usdcAbi, this.wallet);\n\n const tx = await usdc.transferWithAuthorization(\n auth.from,\n auth.to,\n auth.value,\n auth.validAfter,\n auth.validBefore,\n auth.nonce,\n v,\n r,\n s\n );\n\n const receipt = await tx.wait();\n return receipt.hash;\n }\n\n private async readBody(req: IncomingMessage): Promise<any> {\n return new Promise((resolve, reject) => {\n let body = '';\n req.on('data', chunk => body += chunk);\n req.on('end', () => {\n try {\n resolve(body ? JSON.parse(body) : {});\n } catch {\n reject(new Error('Invalid JSON'));\n }\n });\n req.on('error', reject);\n });\n }\n\n private sendJson(res: ServerResponse, status: number, data: any): void {\n res.writeHead(status, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify(data, null, 2));\n }\n}\n","/**\n * Blockchain Configuration\n */\n\nimport type { ChainConfig, ChainName } from '../types/index.js';\n\nexport const CHAINS: Record<ChainName, ChainConfig> = {\n // ============ Mainnet ============\n base: {\n name: 'Base',\n chainId: 8453,\n rpc: 'https://mainnet.base.org',\n usdc: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',\n explorer: 'https://basescan.org/address/',\n explorerTx: 'https://basescan.org/tx/',\n avgBlockTime: 2,\n },\n polygon: {\n name: 'Polygon',\n chainId: 137,\n rpc: 'https://polygon-rpc.com',\n usdc: '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359',\n explorer: 'https://polygonscan.com/address/',\n explorerTx: 'https://polygonscan.com/tx/',\n avgBlockTime: 2,\n },\n ethereum: {\n name: 'Ethereum',\n chainId: 1,\n rpc: 'https://eth.llamarpc.com',\n usdc: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',\n explorer: 'https://etherscan.io/address/',\n explorerTx: 'https://etherscan.io/tx/',\n avgBlockTime: 12,\n },\n\n // ============ Testnet ============\n base_sepolia: {\n name: 'Base Sepolia',\n chainId: 84532,\n rpc: 'https://sepolia.base.org',\n usdc: '0x036CbD53842c5426634e7929541eC2318f3dCF7e',\n explorer: 'https://sepolia.basescan.org/address/',\n explorerTx: 'https://sepolia.basescan.org/tx/',\n avgBlockTime: 2,\n },\n sepolia: {\n name: 'Sepolia',\n chainId: 11155111,\n rpc: 'https://rpc.sepolia.org',\n usdc: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238',\n explorer: 'https://sepolia.etherscan.io/address/',\n explorerTx: 'https://sepolia.etherscan.io/tx/',\n avgBlockTime: 12,\n },\n};\n\n/**\n * Get chain configuration\n */\nexport function getChain(name: ChainName): ChainConfig {\n const config = CHAINS[name];\n if (!config) {\n throw new Error(`Unsupported chain: ${name}. Supported: ${Object.keys(CHAINS).join(', ')}`);\n }\n return config;\n}\n\n/**\n * List all supported chains\n */\nexport function listChains(): ChainName[] {\n return Object.keys(CHAINS) as ChainName[];\n}\n\n/**\n * Get chain config by chainId\n */\nexport function getChainById(chainId: number): ChainConfig | undefined {\n return Object.values(CHAINS).find(c => c.chainId === chainId);\n}\n\n/**\n * ERC20 ABI (minimal, only required methods)\n */\nexport const ERC20_ABI = [\n 'function balanceOf(address owner) view returns (uint256)',\n 'function transfer(address to, uint256 amount) returns (bool)',\n 'function approve(address spender, uint256 amount) returns (bool)',\n 'function allowance(address owner, address spender) view returns (uint256)',\n 'function decimals() view returns (uint8)',\n 'function symbol() view returns (string)',\n 'function name() view returns (string)',\n 'function nonces(address owner) view returns (uint256)',\n 'function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)',\n 'event Transfer(address indexed from, address indexed to, uint256 value)',\n 'event Approval(address indexed owner, address indexed spender, uint256 value)',\n];\n\nexport type { ChainConfig, ChainName };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,gBAA6B;AAC7B,kBAA8D;AAC9D,oBAAuB;;;ACPhB,IAAM,SAAyC;AAAA;AAAA,EAEpD,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA,EACA,SAAS;AAAA,IACP,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA,EACA,UAAU;AAAA,IACR,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA;AAAA,EAGA,cAAc;AAAA,IACZ,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA,EACA,SAAS;AAAA,IACP,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AACF;AAKO,SAAS,SAAS,MAA8B;AACrD,QAAM,SAAS,OAAO,IAAI;AAC1B,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,sBAAsB,IAAI,gBAAgB,OAAO,KAAK,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE;AAAA,EAC5F;AACA,SAAO;AACT;;;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AAU,MAAM,GAAG,KAAK;AAC9B,WAAO,QAAQ;AAAA,EACjB;AAAA,EAEA,MAAc,SAAS,KAAoC;AACzD,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAI,OAAO;AACX,UAAI,GAAG,QAAQ,WAAS,QAAQ,KAAK;AACrC,UAAI,GAAG,OAAO,MAAM;AAClB,YAAI;AACF,kBAAQ,OAAO,KAAK,MAAM,IAAI,IAAI,CAAC,CAAC;AAAA,QACtC,QAAQ;AACN,iBAAO,IAAI,MAAM,cAAc,CAAC;AAAA,QAClC;AAAA,MACF,CAAC;AACD,UAAI,GAAG,SAAS,MAAM;AAAA,IACxB,CAAC;AAAA,EACH;AAAA,EAEQ,SAAS,KAAqB,QAAgB,MAAiB;AACrE,QAAI,UAAU,QAAQ,EAAE,gBAAgB,mBAAmB,CAAC;AAC5D,QAAI,IAAI,KAAK,UAAU,MAAM,MAAM,CAAC,CAAC;AAAA,EACvC;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/server/index.ts","../../src/chains/index.ts"],"sourcesContent":["/**\n * MoltsPay Server - Payment infrastructure for AI Agents\n * \n * Uses x402 protocol with public facilitator (https://x402.org/facilitator).\n * Server does NOT need private key - facilitator handles on-chain settlement.\n * \n * Usage:\n * const server = new MoltsPayServer('./moltspay.services.json');\n * server.skill('text-to-video', async (params) => { ... });\n * server.listen(3000);\n */\n\nimport { readFileSync } from 'fs';\nimport { createServer, IncomingMessage, ServerResponse } from 'http';\nimport { getChain } from '../chains/index.js';\nimport type { ChainName } from '../chains/index.js';\nimport {\n ServicesManifest,\n ServiceConfig,\n SkillFunction,\n RegisteredSkill,\n MoltsPayServerOptions,\n} from './types.js';\n\nexport * from './types.js';\n\n// x402 constants\nconst X402_VERSION = 2;\nconst PAYMENT_REQUIRED_HEADER = 'x-payment-required';\nconst PAYMENT_HEADER = 'x-payment';\nconst PAYMENT_RESPONSE_HEADER = 'x-payment-response';\n\n// Default facilitator URL\nconst DEFAULT_FACILITATOR_URL = 'https://x402.org/facilitator';\n\ninterface X402PaymentPayload {\n x402Version: number;\n scheme: string;\n network: string;\n payload: any;\n}\n\nexport class MoltsPayServer {\n private manifest: ServicesManifest;\n private skills: Map<string, RegisteredSkill> = new Map();\n private options: MoltsPayServerOptions;\n private facilitatorUrl: string;\n\n constructor(servicesPath: string, options: MoltsPayServerOptions = {}) {\n // Load services manifest\n const content = readFileSync(servicesPath, 'utf-8');\n this.manifest = JSON.parse(content) as ServicesManifest;\n \n this.options = {\n port: options.port || 3000,\n host: options.host || '0.0.0.0',\n };\n\n this.facilitatorUrl = options.facilitatorUrl || DEFAULT_FACILITATOR_URL;\n\n console.log(`[MoltsPay] Loaded ${this.manifest.services.length} services from ${servicesPath}`);\n console.log(`[MoltsPay] Provider: ${this.manifest.provider.name}`);\n console.log(`[MoltsPay] Receive wallet: ${this.manifest.provider.wallet}`);\n console.log(`[MoltsPay] Facilitator: ${this.facilitatorUrl}`);\n console.log(`[MoltsPay] Protocol: x402 (gasless for both client AND server)`);\n }\n\n /**\n * Register a skill handler for a service\n */\n skill(serviceId: string, handler: SkillFunction): this {\n const config = this.manifest.services.find(s => s.id === serviceId);\n if (!config) {\n throw new Error(`Service '${serviceId}' not found in manifest`);\n }\n this.skills.set(serviceId, { id: serviceId, config, handler });\n return this;\n }\n\n /**\n * Start HTTP server\n */\n listen(port?: number): void {\n const p = port || this.options.port || 3000;\n const host = this.options.host || '0.0.0.0';\n\n const server = createServer((req, res) => this.handleRequest(req, res));\n server.listen(p, host, () => {\n console.log(`[MoltsPay] Server listening on http://${host}:${p}`);\n console.log(`[MoltsPay] Endpoints:`);\n console.log(` GET /services - List available services`);\n console.log(` POST /execute - Execute service (x402 payment)`);\n });\n }\n\n /**\n * Handle incoming request\n */\n private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {\n // CORS\n res.setHeader('Access-Control-Allow-Origin', '*');\n res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');\n res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Payment');\n res.setHeader('Access-Control-Expose-Headers', 'X-Payment-Required, X-Payment-Response');\n\n if (req.method === 'OPTIONS') {\n res.writeHead(204);\n res.end();\n return;\n }\n\n try {\n const url = new URL(req.url || '/', `http://${req.headers.host}`);\n \n if (url.pathname === '/services' && req.method === 'GET') {\n return this.handleGetServices(res);\n }\n\n if (url.pathname === '/execute' && req.method === 'POST') {\n const body = await this.readBody(req);\n const paymentHeader = req.headers[PAYMENT_HEADER] as string | undefined;\n return await this.handleExecute(body, paymentHeader, res);\n }\n\n // Not found\n this.sendJson(res, 404, { error: 'Not found' });\n } catch (err: any) {\n console.error('[MoltsPay] Error:', err);\n this.sendJson(res, 500, { error: err.message || 'Internal error' });\n }\n }\n\n /**\n * GET /services - List available services\n */\n private handleGetServices(res: ServerResponse): void {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n \n const services = this.manifest.services.map(s => ({\n id: s.id,\n name: s.name,\n description: s.description,\n price: s.price,\n currency: s.currency,\n input: s.input,\n output: s.output,\n available: this.skills.has(s.id),\n }));\n\n this.sendJson(res, 200, {\n provider: this.manifest.provider,\n services,\n x402: {\n version: X402_VERSION,\n network: `eip155:${chain.chainId}`,\n schemes: ['exact'],\n facilitator: this.facilitatorUrl,\n },\n });\n }\n\n /**\n * POST /execute - Execute service with x402 payment\n * Body: { service: string, params: object }\n * Header: X-Payment (optional - if missing, returns 402)\n */\n private async handleExecute(\n body: any,\n paymentHeader: string | undefined,\n res: ServerResponse\n ): Promise<void> {\n const { service, params } = body;\n\n if (!service) {\n return this.sendJson(res, 400, { error: 'Missing service' });\n }\n\n const skill = this.skills.get(service);\n if (!skill) {\n return this.sendJson(res, 404, { error: `Service '${service}' not found or not registered` });\n }\n\n // Validate required params\n for (const [key, field] of Object.entries(skill.config.input)) {\n if (field.required && (!params || params[key] === undefined)) {\n return this.sendJson(res, 400, { error: `Missing required param: ${key}` });\n }\n }\n\n // If no payment header, return 402 with payment requirements\n if (!paymentHeader) {\n return this.sendPaymentRequired(skill.config, res);\n }\n\n // Parse payment payload\n let payment: X402PaymentPayload;\n try {\n const decoded = Buffer.from(paymentHeader, 'base64').toString('utf-8');\n payment = JSON.parse(decoded);\n } catch {\n return this.sendJson(res, 400, { error: 'Invalid X-Payment header' });\n }\n\n // Validate basic payment fields\n const validation = this.validatePayment(payment, skill.config);\n if (!validation.valid) {\n return this.sendJson(res, 402, { error: validation.error });\n }\n\n // Verify payment with facilitator\n console.log(`[MoltsPay] Verifying payment with facilitator...`);\n const verifyResult = await this.verifyWithFacilitator(payment, skill.config);\n if (!verifyResult.valid) {\n return this.sendJson(res, 402, { error: `Payment verification failed: ${verifyResult.error}` });\n }\n\n // Execute skill FIRST (pay-for-success)\n console.log(`[MoltsPay] Executing skill: ${service}`);\n let result: any;\n try {\n result = await skill.handler(params || {});\n } catch (err: any) {\n console.error('[MoltsPay] Skill execution failed:', err.message);\n // Don't settle payment if skill fails\n return this.sendJson(res, 500, {\n error: 'Service execution failed',\n message: err.message,\n });\n }\n\n // Skill succeeded - now settle payment with facilitator\n console.log(`[MoltsPay] Skill succeeded, settling payment...`);\n let settlement: any = null;\n try {\n settlement = await this.settleWithFacilitator(payment, skill.config);\n console.log(`[MoltsPay] Payment settled: ${settlement.transaction || 'pending'}`);\n } catch (err: any) {\n console.error('[MoltsPay] Settlement failed:', err.message);\n // Still return result - facilitator may settle later\n }\n\n // Build response with settlement info\n const responseHeaders: Record<string, string> = {};\n if (settlement) {\n const responsePayload = {\n success: true,\n transaction: settlement.transaction,\n network: payment.network,\n };\n responseHeaders[PAYMENT_RESPONSE_HEADER] = Buffer.from(\n JSON.stringify(responsePayload)\n ).toString('base64');\n }\n\n this.sendJson(res, 200, {\n success: true,\n result,\n payment: settlement \n ? { transaction: settlement.transaction, status: 'settled' }\n : { status: 'pending' },\n }, responseHeaders);\n }\n\n /**\n * Return 402 with x402 payment requirements\n */\n private sendPaymentRequired(config: ServiceConfig, res: ServerResponse): void {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const amountInUnits = Math.floor(config.price * 1e6).toString();\n\n const requirements = [{\n scheme: 'exact',\n network: `eip155:${chain.chainId}`,\n maxAmountRequired: amountInUnits,\n resource: this.manifest.provider.wallet,\n description: `${config.name} - $${config.price} ${config.currency}`,\n // Include facilitator info for client\n extra: JSON.stringify({ facilitator: this.facilitatorUrl }),\n }];\n\n const encoded = Buffer.from(JSON.stringify(requirements)).toString('base64');\n\n res.writeHead(402, {\n 'Content-Type': 'application/json',\n [PAYMENT_REQUIRED_HEADER]: encoded,\n });\n res.end(JSON.stringify({\n error: 'Payment required',\n message: `Service requires $${config.price} ${config.currency}`,\n x402: requirements[0],\n }, null, 2));\n }\n\n /**\n * Basic payment validation (before calling facilitator)\n */\n private validatePayment(\n payment: X402PaymentPayload,\n config: ServiceConfig\n ): { valid: boolean; error?: string } {\n if (payment.x402Version !== X402_VERSION) {\n return { valid: false, error: `Unsupported x402 version: ${payment.x402Version}` };\n }\n\n if (payment.scheme !== 'exact') {\n return { valid: false, error: `Unsupported scheme: ${payment.scheme}` };\n }\n\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const expectedNetwork = `eip155:${chain.chainId}`;\n if (payment.network !== expectedNetwork) {\n return { valid: false, error: `Network mismatch: expected ${expectedNetwork}` };\n }\n\n return { valid: true };\n }\n\n /**\n * Verify payment with facilitator\n */\n private async verifyWithFacilitator(\n payment: X402PaymentPayload,\n config: ServiceConfig\n ): Promise<{ valid: boolean; error?: string }> {\n try {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const amountInUnits = Math.floor(config.price * 1e6).toString();\n\n const requirements = {\n scheme: 'exact',\n network: `eip155:${chain.chainId}`,\n maxAmountRequired: amountInUnits,\n resource: this.manifest.provider.wallet,\n };\n\n const response = await fetch(`${this.facilitatorUrl}/verify`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n paymentPayload: payment,\n paymentRequirements: requirements,\n }),\n });\n\n const result = await response.json() as any;\n\n if (!response.ok || !result.isValid) {\n return { valid: false, error: result.invalidReason || 'Verification failed' };\n }\n\n return { valid: true };\n } catch (err: any) {\n return { valid: false, error: `Facilitator error: ${err.message}` };\n }\n }\n\n /**\n * Settle payment with facilitator (execute on-chain transfer)\n */\n private async settleWithFacilitator(\n payment: X402PaymentPayload,\n config: ServiceConfig\n ): Promise<{ transaction?: string; status: string }> {\n const chain = getChain(this.manifest.provider.chain as ChainName);\n const amountInUnits = Math.floor(config.price * 1e6).toString();\n\n const requirements = {\n scheme: 'exact',\n network: `eip155:${chain.chainId}`,\n maxAmountRequired: amountInUnits,\n resource: this.manifest.provider.wallet,\n };\n\n const response = await fetch(`${this.facilitatorUrl}/settle`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n paymentPayload: payment,\n paymentRequirements: requirements,\n }),\n });\n\n const result = await response.json() as any;\n\n if (!response.ok) {\n throw new Error(result.error || 'Settlement failed');\n }\n\n return {\n transaction: result.transaction,\n status: result.status || 'settled',\n };\n }\n\n private async readBody(req: IncomingMessage): Promise<any> {\n return new Promise((resolve, reject) => {\n let body = '';\n req.on('data', chunk => body += chunk);\n req.on('end', () => {\n try {\n resolve(body ? JSON.parse(body) : {});\n } catch {\n reject(new Error('Invalid JSON'));\n }\n });\n req.on('error', reject);\n });\n }\n\n private sendJson(\n res: ServerResponse, \n status: number, \n data: any,\n extraHeaders?: Record<string, string>\n ): void {\n const headers: Record<string, string> = { 'Content-Type': 'application/json' };\n if (extraHeaders) {\n Object.assign(headers, extraHeaders);\n }\n res.writeHead(status, headers);\n res.end(JSON.stringify(data, null, 2));\n }\n}\n","/**\n * Blockchain Configuration\n */\n\nimport type { ChainConfig, ChainName } from '../types/index.js';\n\nexport const CHAINS: Record<ChainName, ChainConfig> = {\n // ============ Mainnet ============\n base: {\n name: 'Base',\n chainId: 8453,\n rpc: 'https://mainnet.base.org',\n usdc: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',\n explorer: 'https://basescan.org/address/',\n explorerTx: 'https://basescan.org/tx/',\n avgBlockTime: 2,\n },\n polygon: {\n name: 'Polygon',\n chainId: 137,\n rpc: 'https://polygon-rpc.com',\n usdc: '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359',\n explorer: 'https://polygonscan.com/address/',\n explorerTx: 'https://polygonscan.com/tx/',\n avgBlockTime: 2,\n },\n ethereum: {\n name: 'Ethereum',\n chainId: 1,\n rpc: 'https://eth.llamarpc.com',\n usdc: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',\n explorer: 'https://etherscan.io/address/',\n explorerTx: 'https://etherscan.io/tx/',\n avgBlockTime: 12,\n },\n\n // ============ Testnet ============\n base_sepolia: {\n name: 'Base Sepolia',\n chainId: 84532,\n rpc: 'https://sepolia.base.org',\n usdc: '0x036CbD53842c5426634e7929541eC2318f3dCF7e',\n explorer: 'https://sepolia.basescan.org/address/',\n explorerTx: 'https://sepolia.basescan.org/tx/',\n avgBlockTime: 2,\n },\n sepolia: {\n name: 'Sepolia',\n chainId: 11155111,\n rpc: 'https://rpc.sepolia.org',\n usdc: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238',\n explorer: 'https://sepolia.etherscan.io/address/',\n explorerTx: 'https://sepolia.etherscan.io/tx/',\n avgBlockTime: 12,\n },\n};\n\n/**\n * Get chain configuration\n */\nexport function getChain(name: ChainName): ChainConfig {\n const config = CHAINS[name];\n if (!config) {\n throw new Error(`Unsupported chain: ${name}. Supported: ${Object.keys(CHAINS).join(', ')}`);\n }\n return config;\n}\n\n/**\n * List all supported chains\n */\nexport function listChains(): ChainName[] {\n return Object.keys(CHAINS) as ChainName[];\n}\n\n/**\n * Get chain config by chainId\n */\nexport function getChainById(chainId: number): ChainConfig | undefined {\n return Object.values(CHAINS).find(c => c.chainId === chainId);\n}\n\n/**\n * ERC20 ABI (minimal, only required methods)\n */\nexport const ERC20_ABI = [\n 'function balanceOf(address owner) view returns (uint256)',\n 'function transfer(address to, uint256 amount) returns (bool)',\n 'function approve(address spender, uint256 amount) returns (bool)',\n 'function allowance(address owner, address spender) view returns (uint256)',\n 'function decimals() view returns (uint8)',\n 'function symbol() view returns (string)',\n 'function name() view returns (string)',\n 'function nonces(address owner) view returns (uint256)',\n 'function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)',\n 'event Transfer(address indexed from, address indexed to, uint256 value)',\n 'event Approval(address indexed owner, address indexed spender, uint256 value)',\n];\n\nexport type { ChainConfig, ChainName };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,gBAA6B;AAC7B,kBAA8D;;;ACPvD,IAAM,SAAyC;AAAA;AAAA,EAEpD,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA,EACA,SAAS;AAAA,IACP,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA,EACA,UAAU;AAAA,IACR,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA;AAAA,EAGA,cAAc;AAAA,IACZ,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AAAA,EACA,SAAS;AAAA,IACP,MAAM;AAAA,IACN,SAAS;AAAA,IACT,KAAK;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,cAAc;AAAA,EAChB;AACF;AAKO,SAAS,SAAS,MAA8B;AAC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