moltspay 0.9.2 → 0.9.5

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/dist/index.mjs CHANGED
@@ -30923,6 +30923,9 @@ var MoltsPayServer = class {
30923
30923
  if (url.pathname === "/services" && req.method === "GET") {
30924
30924
  return this.handleGetServices(res);
30925
30925
  }
30926
+ if (url.pathname === "/.well-known/agent-services.json" && req.method === "GET") {
30927
+ return this.handleAgentServicesDiscovery(res);
30928
+ }
30926
30929
  if (url.pathname === "/health" && req.method === "GET") {
30927
30930
  return await this.handleHealthCheck(res);
30928
30931
  }
@@ -30946,6 +30949,43 @@ var MoltsPayServer = class {
30946
30949
  this.sendJson(res, 500, { error: err.message || "Internal error" });
30947
30950
  }
30948
30951
  }
30952
+ /**
30953
+ * GET /.well-known/agent-services.json - Standard discovery endpoint
30954
+ */
30955
+ handleAgentServicesDiscovery(res) {
30956
+ const services = this.manifest.services.map((s) => ({
30957
+ id: s.id,
30958
+ name: s.name,
30959
+ description: s.description,
30960
+ price: s.price,
30961
+ currency: s.currency,
30962
+ input: s.input,
30963
+ output: s.output,
30964
+ available: this.skills.has(s.id)
30965
+ }));
30966
+ this.sendJson(res, 200, {
30967
+ version: "1.0",
30968
+ provider: {
30969
+ name: this.manifest.provider.name,
30970
+ description: this.manifest.provider.description,
30971
+ wallet: this.manifest.provider.wallet,
30972
+ chain: this.manifest.provider.chain || "base"
30973
+ },
30974
+ services,
30975
+ endpoints: {
30976
+ services: "/services",
30977
+ execute: "/execute",
30978
+ health: "/health"
30979
+ },
30980
+ payment: {
30981
+ protocol: "x402",
30982
+ version: X402_VERSION2,
30983
+ network: this.networkId,
30984
+ schemes: ["exact"],
30985
+ mainnet: this.useMainnet
30986
+ }
30987
+ });
30988
+ }
30949
30989
  /**
30950
30990
  * GET /services - List available services
30951
30991
  */
@@ -31032,10 +31072,16 @@ var MoltsPayServer = class {
31032
31072
  });
31033
31073
  }
31034
31074
  console.log(`[MoltsPay] Verified by ${verifyResult.facilitator}`);
31035
- console.log(`[MoltsPay] Executing skill: ${service}`);
31075
+ const timeoutSeconds = parseInt(process.env.SKILL_TIMEOUT_SECONDS || "1200");
31076
+ console.log(`[MoltsPay] Executing skill: ${service} (timeout: ${timeoutSeconds}s)`);
31036
31077
  let result;
31037
31078
  try {
31038
- result = await skill.handler(params || {});
31079
+ result = await Promise.race([
31080
+ skill.handler(params || {}),
31081
+ new Promise(
31082
+ (_, reject) => setTimeout(() => reject(new Error(`Skill timeout after ${timeoutSeconds}s`)), timeoutSeconds * 1e3)
31083
+ )
31084
+ ]);
31039
31085
  } catch (err) {
31040
31086
  console.error("[MoltsPay] Skill execution failed:", err.message);
31041
31087
  return this.sendJson(res, 500, {
@@ -31232,7 +31278,72 @@ var MoltsPayServer = class {
31232
31278
  });
31233
31279
  }
31234
31280
  console.log(`[MoltsPay] /proxy: Verified by ${verifyResult.facilitator}`);
31235
- console.log(`[MoltsPay] /proxy: Settling payment...`);
31281
+ const { execute, service, params } = body;
31282
+ if (execute && service) {
31283
+ console.log(`[MoltsPay] /proxy: Executing skill first (pay on success): ${service}`);
31284
+ const skill = this.skills.get(service);
31285
+ if (!skill) {
31286
+ console.log(`[MoltsPay] /proxy: Service not found: ${service} - NOT settling`);
31287
+ return this.sendJson(res, 404, {
31288
+ success: false,
31289
+ paymentSettled: false,
31290
+ error: `Service not found: ${service}`
31291
+ });
31292
+ }
31293
+ const timeoutSeconds = parseInt(process.env.SKILL_TIMEOUT_SECONDS || "1200");
31294
+ let result;
31295
+ try {
31296
+ result = await Promise.race([
31297
+ skill.handler(params || {}),
31298
+ new Promise(
31299
+ (_, reject) => setTimeout(() => reject(new Error(`Skill timeout after ${timeoutSeconds}s`)), timeoutSeconds * 1e3)
31300
+ )
31301
+ ]);
31302
+ console.log(`[MoltsPay] /proxy: Skill succeeded, now settling payment...`);
31303
+ } catch (err) {
31304
+ console.error(`[MoltsPay] /proxy: Skill failed: ${err.message} - NOT settling`);
31305
+ return this.sendJson(res, 500, {
31306
+ success: false,
31307
+ paymentSettled: false,
31308
+ error: `Service execution failed: ${err.message}`
31309
+ });
31310
+ }
31311
+ let settlement2 = null;
31312
+ try {
31313
+ settlement2 = await this.registry.settle(payment, requirements);
31314
+ console.log(`[MoltsPay] /proxy: Payment settled by ${settlement2.facilitator}: ${settlement2.transaction || "pending"}`);
31315
+ } catch (err) {
31316
+ console.error("[MoltsPay] /proxy: Settlement failed:", err.message);
31317
+ return this.sendJson(res, 200, {
31318
+ success: true,
31319
+ verified: true,
31320
+ settled: false,
31321
+ settlementError: err.message,
31322
+ from: payment.payload?.authorization?.from,
31323
+ // Buyer's wallet address
31324
+ paidTo: wallet,
31325
+ amount: amountNum,
31326
+ currency: currency || "USDC",
31327
+ memo,
31328
+ result
31329
+ });
31330
+ }
31331
+ return this.sendJson(res, 200, {
31332
+ success: true,
31333
+ verified: true,
31334
+ settled: settlement2?.success || false,
31335
+ txHash: settlement2?.transaction,
31336
+ from: payment.payload?.authorization?.from,
31337
+ // Buyer's wallet address
31338
+ paidTo: wallet,
31339
+ amount: amountNum,
31340
+ currency: currency || "USDC",
31341
+ facilitator: settlement2?.facilitator,
31342
+ memo,
31343
+ result
31344
+ });
31345
+ }
31346
+ console.log(`[MoltsPay] /proxy: Settling payment (no execution)...`);
31236
31347
  let settlement = null;
31237
31348
  try {
31238
31349
  settlement = await this.registry.settle(payment, requirements);
@@ -31249,6 +31360,8 @@ var MoltsPayServer = class {
31249
31360
  verified: true,
31250
31361
  settled: settlement?.success || false,
31251
31362
  txHash: settlement?.transaction,
31363
+ from: payment.payload?.authorization?.from,
31364
+ // Buyer's wallet address
31252
31365
  paidTo: wallet,
31253
31366
  amount: amountNum,
31254
31367
  currency: currency || "USDC",