@agent-score/commerce 2.6.2 → 2.6.4

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 (41) hide show
  1. package/dist/{checkout-C4RD7M0Z.d.ts → checkout-B-Z5_EQP.d.ts} +9 -0
  2. package/dist/{checkout-CzB9f_jf.d.mts → checkout-BtdHSF_w.d.mts} +9 -0
  3. package/dist/core.js +1 -1
  4. package/dist/core.mjs +1 -1
  5. package/dist/discovery/index.d.mts +1 -1
  6. package/dist/discovery/index.d.ts +1 -1
  7. package/dist/identity/express.js +1 -1
  8. package/dist/identity/express.js.map +1 -1
  9. package/dist/identity/express.mjs +1 -1
  10. package/dist/identity/express.mjs.map +1 -1
  11. package/dist/identity/fastify.js +1 -1
  12. package/dist/identity/fastify.js.map +1 -1
  13. package/dist/identity/fastify.mjs +1 -1
  14. package/dist/identity/fastify.mjs.map +1 -1
  15. package/dist/identity/hono.js +1 -1
  16. package/dist/identity/hono.js.map +1 -1
  17. package/dist/identity/hono.mjs +1 -1
  18. package/dist/identity/hono.mjs.map +1 -1
  19. package/dist/identity/nextjs.js +1 -1
  20. package/dist/identity/nextjs.js.map +1 -1
  21. package/dist/identity/nextjs.mjs +1 -1
  22. package/dist/identity/nextjs.mjs.map +1 -1
  23. package/dist/identity/web.js +1 -1
  24. package/dist/identity/web.js.map +1 -1
  25. package/dist/identity/web.mjs +1 -1
  26. package/dist/identity/web.mjs.map +1 -1
  27. package/dist/index.d.mts +35 -17
  28. package/dist/index.d.ts +35 -17
  29. package/dist/index.js +101 -18
  30. package/dist/index.js.map +1 -1
  31. package/dist/index.mjs +99 -18
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/{network_kind-BIJM2peR.d.ts → network_kind-BmbWKNud.d.ts} +23 -1
  34. package/dist/{network_kind-C0EMkdzz.d.mts → network_kind-D2xpo2Fj.d.mts} +23 -1
  35. package/dist/payment/index.d.mts +27 -20
  36. package/dist/payment/index.d.ts +27 -20
  37. package/dist/payment/index.js +35 -0
  38. package/dist/payment/index.js.map +1 -1
  39. package/dist/payment/index.mjs +34 -0
  40. package/dist/payment/index.mjs.map +1 -1
  41. package/package.json +2 -2
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.2"}`;
22577
+ const defaultUa = `@agent-score/commerce@${"2.6.4"}`;
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) {
@@ -25440,6 +25473,7 @@ var Checkout = class {
25440
25473
  * agent-supplied `payTo` to this set (anti funds-drain). Empty for pure per-order minting. */
25441
25474
  x402StaticRecipients;
25442
25475
  zeroSettleCarveOut;
25476
+ credentialPreCheck;
25443
25477
  gate;
25444
25478
  discoveryExtensions;
25445
25479
  resourceInfo;
@@ -25521,6 +25555,7 @@ var Checkout = class {
25521
25555
  this.isCachedAddress = opts.isCachedAddress;
25522
25556
  this.x402StaticRecipients = collectStaticX402Recipients(opts.rails);
25523
25557
  this.zeroSettleCarveOut = opts.zeroSettleCarveOut ?? false;
25558
+ this.credentialPreCheck = opts.credentialPreCheck ?? true;
25524
25559
  this.gate = opts.gate;
25525
25560
  this.discoveryExtensions = opts.discoveryExtensions;
25526
25561
  this.resourceInfo = opts.resourceInfo;
@@ -25658,6 +25693,27 @@ var Checkout = class {
25658
25693
  };
25659
25694
  }
25660
25695
  }
25696
+ if (this.credentialPreCheck) {
25697
+ const malformed = malformedPaymentCredential(request.headers);
25698
+ const enforced = malformed !== null && (malformed.channel === "x402" ? this.x402ServerAvailable() && this.x402BaseNetwork !== null : this.composeMppx !== void 0);
25699
+ if (enforced) {
25700
+ return {
25701
+ status: 400,
25702
+ body: buildValidationError({
25703
+ code: "payment_proof_invalid",
25704
+ message: malformed.message,
25705
+ nextSteps: {
25706
+ action: "regenerate_payment_credential",
25707
+ user_message: "The payment credential could not be decoded. Rebuild it from a fresh 402 challenge and retry."
25708
+ }
25709
+ }),
25710
+ headers: {},
25711
+ referenceId: ctx.referenceId,
25712
+ settled: false,
25713
+ settlePhase: "credential_malformed"
25714
+ };
25715
+ }
25716
+ }
25661
25717
  if (this.preValidate !== void 0) {
25662
25718
  try {
25663
25719
  const state = await this.preValidate(ctx);
@@ -25980,21 +26036,34 @@ var Checkout = class {
25980
26036
  if (ctx.pricing.amountUsd !== 0) return null;
25981
26037
  const headers = normalizeHeadersToLowercase(ctx.request.headers);
25982
26038
  let zero;
26039
+ let railKey;
25983
26040
  if (rail === "x402-base") {
25984
- const x402Header = headers["payment-signature"] ?? headers["x-payment"];
25985
- let payload = null;
25986
- if (typeof x402Header === "string" && x402Header.length > 0) {
25987
- try {
25988
- payload = JSON.parse(atob(x402Header));
25989
- } catch {
25990
- payload = null;
25991
- }
26041
+ const fakeRequest = new Request(ctx.request.url, {
26042
+ method: ctx.request.method,
26043
+ headers: ctx.request.headers
26044
+ });
26045
+ const verified = await verifyX402Request({
26046
+ request: fakeRequest,
26047
+ isCachedAddress: (addr) => this.asyncIsCachedAddress(addr, ctx),
26048
+ acceptedNetwork: this.x402BaseNetwork ?? ""
26049
+ });
26050
+ if (!verified.ok) {
26051
+ return {
26052
+ status: verified.status,
26053
+ body: verified.body,
26054
+ headers: {},
26055
+ referenceId: ctx.referenceId,
26056
+ settled: false,
26057
+ settlePhase: "verify_failed"
26058
+ };
25992
26059
  }
25993
- zero = zeroAmountCarveOut({ rail, payload });
26060
+ zero = zeroAmountCarveOut({ rail, payload: verified.payload });
26061
+ railKey = this.x402RailKey();
25994
26062
  } else {
25995
26063
  zero = zeroAmountCarveOut({ rail, authorizationHeader: headers["authorization"] });
26064
+ if (zero.signerNetwork !== "solana") return null;
26065
+ railKey = this.railsKeyForMppxMethod("solana") ?? this.mppRailKey();
25996
26066
  }
25997
- const railKey = rail === "x402-base" ? this.x402RailKey() : this.mppRailKey();
25998
26067
  const outcome = {
25999
26068
  rail: rail === "x402-base" ? "x402" : "mpp",
26000
26069
  paymentResponseHeader: null,
@@ -26790,11 +26859,11 @@ function canonicalize2(value) {
26790
26859
  }
26791
26860
  return value;
26792
26861
  }
26793
- function createQuoteCache(opts = {}) {
26862
+ function createResultCache(opts = {}) {
26794
26863
  const ttlMs = opts.ttlMs ?? 5 * 6e4;
26795
- const keyPrefix = opts.keyPrefix ?? "quote:";
26864
+ const keyPrefix = opts.keyPrefix ?? "result:";
26796
26865
  const memMap = /* @__PURE__ */ new Map();
26797
- const getRedis = memoizedRedis({ url: opts.redisUrl, label: "quote-cache" });
26866
+ const getRedis = memoizedRedis({ url: opts.redisUrl, label: "result-cache" });
26798
26867
  const evictExpired = () => {
26799
26868
  const now = Date.now();
26800
26869
  for (const [k, v] of memMap.entries()) {
@@ -26821,17 +26890,16 @@ function createQuoteCache(opts = {}) {
26821
26890
  const entry = memMap.get(key);
26822
26891
  return entry ? entry.entry : null;
26823
26892
  },
26824
- async write(key, body, priceCents, recipients = {}) {
26825
- const cached2 = { body, priceCents, recipients };
26893
+ async write(key, value) {
26826
26894
  const r = await getRedis();
26827
26895
  if (r) {
26828
26896
  try {
26829
- await r.set(`${keyPrefix}${key}`, JSON.stringify(cached2), "PX", ttlMs);
26897
+ await r.set(`${keyPrefix}${key}`, JSON.stringify(value), "PX", ttlMs);
26830
26898
  return;
26831
26899
  } catch {
26832
26900
  }
26833
26901
  }
26834
- memMap.set(key, { entry: cached2, expiresAt: Date.now() + ttlMs });
26902
+ memMap.set(key, { entry: value, expiresAt: Date.now() + ttlMs });
26835
26903
  },
26836
26904
  async clear() {
26837
26905
  memMap.clear();
@@ -26845,6 +26913,17 @@ function createQuoteCache(opts = {}) {
26845
26913
  }
26846
26914
  };
26847
26915
  }
26916
+ function createQuoteCache(opts = {}) {
26917
+ const cache = createResultCache({ ...opts, keyPrefix: opts.keyPrefix ?? "quote:" });
26918
+ return {
26919
+ bodyHashKey: cache.bodyHashKey,
26920
+ read: cache.read,
26921
+ async write(key, body, priceCents, recipients = {}) {
26922
+ await cache.write(key, { body, priceCents, recipients });
26923
+ },
26924
+ clear: cache.clear
26925
+ };
26926
+ }
26848
26927
 
26849
26928
  // src/checkout_compute_first.ts
26850
26929
  var DEFAULT_TTL_MS = 5 * 6e4;
@@ -27361,6 +27440,7 @@ export {
27361
27440
  computeFirstCheckout,
27362
27441
  createDefaultOnDenied,
27363
27442
  createQuoteCache,
27443
+ createResultCache,
27364
27444
  defaultReadOnlyOnDenied,
27365
27445
  denialReasonStatus,
27366
27446
  denialReasonToBody,
@@ -27385,6 +27465,7 @@ export {
27385
27465
  loadSolanaFeePayer,
27386
27466
  loadUCPSigningKeyFromEnv,
27387
27467
  makeMppxComposeHook,
27468
+ malformedPaymentCredential,
27388
27469
  mppPaymentHandler,
27389
27470
  normalizeAddress,
27390
27471
  pricingResult,