@agent-score/commerce 2.6.3 → 2.6.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.
Files changed (42) hide show
  1. package/README.md +2 -2
  2. package/dist/{checkout-CzB9f_jf.d.mts → checkout-DOd9GDmt.d.mts} +15 -3
  3. package/dist/{checkout-C4RD7M0Z.d.ts → checkout-GcDNDzSK.d.ts} +15 -3
  4. package/dist/core.js +1 -1
  5. package/dist/core.mjs +1 -1
  6. package/dist/discovery/index.d.mts +1 -1
  7. package/dist/discovery/index.d.ts +1 -1
  8. package/dist/identity/express.js +1 -1
  9. package/dist/identity/express.js.map +1 -1
  10. package/dist/identity/express.mjs +1 -1
  11. package/dist/identity/express.mjs.map +1 -1
  12. package/dist/identity/fastify.js +1 -1
  13. package/dist/identity/fastify.js.map +1 -1
  14. package/dist/identity/fastify.mjs +1 -1
  15. package/dist/identity/fastify.mjs.map +1 -1
  16. package/dist/identity/hono.js +1 -1
  17. package/dist/identity/hono.js.map +1 -1
  18. package/dist/identity/hono.mjs +1 -1
  19. package/dist/identity/hono.mjs.map +1 -1
  20. package/dist/identity/nextjs.js +1 -1
  21. package/dist/identity/nextjs.js.map +1 -1
  22. package/dist/identity/nextjs.mjs +1 -1
  23. package/dist/identity/nextjs.mjs.map +1 -1
  24. package/dist/identity/web.js +1 -1
  25. package/dist/identity/web.js.map +1 -1
  26. package/dist/identity/web.mjs +1 -1
  27. package/dist/identity/web.mjs.map +1 -1
  28. package/dist/index.d.mts +35 -17
  29. package/dist/index.d.ts +35 -17
  30. package/dist/index.js +99 -9
  31. package/dist/index.js.map +1 -1
  32. package/dist/index.mjs +97 -9
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/{network_kind-BIJM2peR.d.ts → network_kind-BmbWKNud.d.ts} +23 -1
  35. package/dist/{network_kind-C0EMkdzz.d.mts → network_kind-D2xpo2Fj.d.mts} +23 -1
  36. package/dist/payment/index.d.mts +42 -21
  37. package/dist/payment/index.d.ts +42 -21
  38. package/dist/payment/index.js +53 -0
  39. package/dist/payment/index.js.map +1 -1
  40. package/dist/payment/index.mjs +51 -0
  41. package/dist/payment/index.mjs.map +1 -1
  42. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -22574,7 +22574,7 @@ function createAgentScoreCore(options) {
22574
22574
  } = options;
22575
22575
  const baseUrl = stripTrailingSlashes(rawBaseUrl);
22576
22576
  const agentMemoryHint = buildAgentMemoryHint(aipTrustedIssuers);
22577
- const defaultUa = `@agent-score/commerce@${"2.6.3"}`;
22577
+ const defaultUa = `@agent-score/commerce@${"2.6.5"}`;
22578
22578
  const userAgentHeader = userAgent ? `${userAgent} (${defaultUa})` : defaultUa;
22579
22579
  const sdk = new AgentScore({ apiKey, baseUrl, userAgent: userAgentHeader });
22580
22580
  const sessionSdkCache = /* @__PURE__ */ new Map();
@@ -23201,6 +23201,39 @@ function hasMppxHeader(input) {
23201
23201
  const headers = asHeaders(input);
23202
23202
  return Boolean(readHeader(headers, "authorization")?.startsWith("Payment "));
23203
23203
  }
23204
+ var JWT_SHAPE_RE = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
23205
+ function decodesToJsonObject(token) {
23206
+ try {
23207
+ const parsed = JSON.parse(Buffer.from(token, "base64").toString("utf8"));
23208
+ return typeof parsed === "object" && parsed !== null;
23209
+ } catch {
23210
+ return false;
23211
+ }
23212
+ }
23213
+ function malformedPaymentCredential(input) {
23214
+ const headers = asHeaders(input);
23215
+ const x402Token = readHeader(headers, "payment-signature") ?? readHeader(headers, "x-payment");
23216
+ if (x402Token !== null && x402Token.length > 0) {
23217
+ if (!decodesToJsonObject(x402Token)) {
23218
+ return {
23219
+ channel: "x402",
23220
+ message: "X-Payment header is not decodable base64 JSON."
23221
+ };
23222
+ }
23223
+ return null;
23224
+ }
23225
+ const auth = readHeader(headers, "authorization");
23226
+ if (auth !== null && auth.startsWith("Payment ")) {
23227
+ const token = auth.slice("Payment ".length).trim();
23228
+ if (token.length === 0 || !decodesToJsonObject(token) && !JWT_SHAPE_RE.test(token)) {
23229
+ return {
23230
+ channel: "mpp",
23231
+ message: "Authorization: Payment credential is neither base64-encoded JSON nor a token-shaped value."
23232
+ };
23233
+ }
23234
+ }
23235
+ return null;
23236
+ }
23204
23237
 
23205
23238
  // src/identity/tokens.ts
23206
23239
  function hashOperatorToken(plaintext) {
@@ -25188,6 +25221,22 @@ function x402SignerFromPayload(payload) {
25188
25221
  txHash: null
25189
25222
  };
25190
25223
  }
25224
+ function mppCredentialPayloadType(authorizationHeader) {
25225
+ if (typeof authorizationHeader !== "string") return null;
25226
+ if (!authorizationHeader.toLowerCase().startsWith("payment ")) return null;
25227
+ const token = authorizationHeader.slice("payment ".length).trim();
25228
+ if (!token) return null;
25229
+ try {
25230
+ const credential = JSON.parse(atob(token));
25231
+ if (!credential || typeof credential !== "object") return null;
25232
+ const payload = credential.payload;
25233
+ if (!payload || typeof payload !== "object") return null;
25234
+ const type = payload.type;
25235
+ return typeof type === "string" ? type : null;
25236
+ } catch {
25237
+ return null;
25238
+ }
25239
+ }
25191
25240
  function mppSignerFromAuth(authorizationHeader) {
25192
25241
  if (typeof authorizationHeader !== "string") return NULL_RESULT;
25193
25242
  if (!authorizationHeader.toLowerCase().startsWith("payment ")) return NULL_RESULT;
@@ -25440,6 +25489,7 @@ var Checkout = class {
25440
25489
  * agent-supplied `payTo` to this set (anti funds-drain). Empty for pure per-order minting. */
25441
25490
  x402StaticRecipients;
25442
25491
  zeroSettleCarveOut;
25492
+ credentialPreCheck;
25443
25493
  gate;
25444
25494
  discoveryExtensions;
25445
25495
  resourceInfo;
@@ -25521,6 +25571,7 @@ var Checkout = class {
25521
25571
  this.isCachedAddress = opts.isCachedAddress;
25522
25572
  this.x402StaticRecipients = collectStaticX402Recipients(opts.rails);
25523
25573
  this.zeroSettleCarveOut = opts.zeroSettleCarveOut ?? false;
25574
+ this.credentialPreCheck = opts.credentialPreCheck ?? true;
25524
25575
  this.gate = opts.gate;
25525
25576
  this.discoveryExtensions = opts.discoveryExtensions;
25526
25577
  this.resourceInfo = opts.resourceInfo;
@@ -25658,6 +25709,27 @@ var Checkout = class {
25658
25709
  };
25659
25710
  }
25660
25711
  }
25712
+ if (this.credentialPreCheck) {
25713
+ const malformed = malformedPaymentCredential(request.headers);
25714
+ const enforced = malformed !== null && (malformed.channel === "x402" ? this.x402ServerAvailable() && this.x402BaseNetwork !== null : this.composeMppx !== void 0);
25715
+ if (enforced) {
25716
+ return {
25717
+ status: 400,
25718
+ body: buildValidationError({
25719
+ code: "payment_proof_invalid",
25720
+ message: malformed.message,
25721
+ nextSteps: {
25722
+ action: "regenerate_payment_credential",
25723
+ user_message: "The payment credential could not be decoded. Rebuild it from a fresh 402 challenge and retry."
25724
+ }
25725
+ }),
25726
+ headers: {},
25727
+ referenceId: ctx.referenceId,
25728
+ settled: false,
25729
+ settlePhase: "credential_malformed"
25730
+ };
25731
+ }
25732
+ }
25661
25733
  if (this.preValidate !== void 0) {
25662
25734
  try {
25663
25735
  const state = await this.preValidate(ctx);
@@ -26004,7 +26076,11 @@ var Checkout = class {
26004
26076
  zero = zeroAmountCarveOut({ rail, payload: verified.payload });
26005
26077
  railKey = this.x402RailKey();
26006
26078
  } else {
26007
- zero = zeroAmountCarveOut({ rail, authorizationHeader: headers["authorization"] });
26079
+ const authHeader = headers["authorization"] ?? null;
26080
+ if (mppCredentialPayloadType(authHeader) === "proof") return null;
26081
+ const inline = zeroAmountCarveOut({ rail, authorizationHeader: authHeader });
26082
+ const extracted = await extractPaymentSignerFromAuth(authHeader);
26083
+ zero = extracted !== null ? { signerAddress: extracted.address, signerNetwork: extracted.network, txHash: null } : inline;
26008
26084
  railKey = (zero.signerNetwork !== null ? this.railsKeyForMppxMethod(zero.signerNetwork === "solana" ? "solana" : "tempo") : void 0) ?? this.mppRailKey();
26009
26085
  }
26010
26086
  const outcome = {
@@ -26802,11 +26878,11 @@ function canonicalize2(value) {
26802
26878
  }
26803
26879
  return value;
26804
26880
  }
26805
- function createQuoteCache(opts = {}) {
26881
+ function createResultCache(opts = {}) {
26806
26882
  const ttlMs = opts.ttlMs ?? 5 * 6e4;
26807
- const keyPrefix = opts.keyPrefix ?? "quote:";
26883
+ const keyPrefix = opts.keyPrefix ?? "result:";
26808
26884
  const memMap = /* @__PURE__ */ new Map();
26809
- const getRedis = memoizedRedis({ url: opts.redisUrl, label: "quote-cache" });
26885
+ const getRedis = memoizedRedis({ url: opts.redisUrl, label: "result-cache" });
26810
26886
  const evictExpired = () => {
26811
26887
  const now = Date.now();
26812
26888
  for (const [k, v] of memMap.entries()) {
@@ -26833,17 +26909,16 @@ function createQuoteCache(opts = {}) {
26833
26909
  const entry = memMap.get(key);
26834
26910
  return entry ? entry.entry : null;
26835
26911
  },
26836
- async write(key, body, priceCents, recipients = {}) {
26837
- const cached2 = { body, priceCents, recipients };
26912
+ async write(key, value) {
26838
26913
  const r = await getRedis();
26839
26914
  if (r) {
26840
26915
  try {
26841
- await r.set(`${keyPrefix}${key}`, JSON.stringify(cached2), "PX", ttlMs);
26916
+ await r.set(`${keyPrefix}${key}`, JSON.stringify(value), "PX", ttlMs);
26842
26917
  return;
26843
26918
  } catch {
26844
26919
  }
26845
26920
  }
26846
- memMap.set(key, { entry: cached2, expiresAt: Date.now() + ttlMs });
26921
+ memMap.set(key, { entry: value, expiresAt: Date.now() + ttlMs });
26847
26922
  },
26848
26923
  async clear() {
26849
26924
  memMap.clear();
@@ -26857,6 +26932,17 @@ function createQuoteCache(opts = {}) {
26857
26932
  }
26858
26933
  };
26859
26934
  }
26935
+ function createQuoteCache(opts = {}) {
26936
+ const cache = createResultCache({ ...opts, keyPrefix: opts.keyPrefix ?? "quote:" });
26937
+ return {
26938
+ bodyHashKey: cache.bodyHashKey,
26939
+ read: cache.read,
26940
+ async write(key, body, priceCents, recipients = {}) {
26941
+ await cache.write(key, { body, priceCents, recipients });
26942
+ },
26943
+ clear: cache.clear
26944
+ };
26945
+ }
26860
26946
 
26861
26947
  // src/checkout_compute_first.ts
26862
26948
  var DEFAULT_TTL_MS = 5 * 6e4;
@@ -27373,6 +27459,7 @@ export {
27373
27459
  computeFirstCheckout,
27374
27460
  createDefaultOnDenied,
27375
27461
  createQuoteCache,
27462
+ createResultCache,
27376
27463
  defaultReadOnlyOnDenied,
27377
27464
  denialReasonStatus,
27378
27465
  denialReasonToBody,
@@ -27397,6 +27484,7 @@ export {
27397
27484
  loadSolanaFeePayer,
27398
27485
  loadUCPSigningKeyFromEnv,
27399
27486
  makeMppxComposeHook,
27487
+ malformedPaymentCredential,
27400
27488
  mppPaymentHandler,
27401
27489
  normalizeAddress,
27402
27490
  pricingResult,