uvd-x402-sdk 2.78.0 → 2.80.0

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.d.mts CHANGED
@@ -4,7 +4,7 @@ export { A as AlgorandPaymentPayload, C as CAIP2_IDENTIFIERS, a as CAIP2_TO_CHAI
4
4
  import { S as SigningWalletAdapter } from './wallet-0cX9Pw2F.mjs';
5
5
  export { E as EIP3009Authorization, a as EIP3009Params } from './wallet-0cX9Pw2F.mjs';
6
6
  export { E as EnvKeyAdapter, O as OWSWallet, a as OWSWalletAdapter } from './ows-CYIVd4xO.mjs';
7
- export { AMBIGUOUS_LEASE_REASONS, DEFAULT_FACILITATOR_RETRIES, DEFAULT_RETRY_AFTER_SECONDS, FacilitatorClient, FacilitatorClientOptions, FacilitatorErrorInfo, FacilitatorFailureFields, FacilitatorFetchOptions, HonoMiddlewareOptions, MAX_RETRY_AFTER_SECONDS, PaymentAcceptance, PaymentMiddlewareOptions, PaymentPayloadV2, PaymentRequirementsV2, REPLAYABLE_LEASE_REASONS, ResourceInfoV2, SettleRequestV2, VerifiedPaymentState, VerifyRequestV2, WRITER_LEASE_REASONS, WriterLeaseReason, X402_CORS_HEADERS, X402_HEADER_NAMES, buildPaymentRequirements, buildSettleRequest, buildSettleRequestForVersion, buildSettleRequestV2, buildVerifyRequest, buildVerifyRequestForVersion, buildVerifyRequestV2, create402Response, createHonoMiddleware, createPaymentMiddleware, extractPaymentFromHeaders, facilitatorFetch, getCorsHeaders, isAmbiguousLeaseReason, isReplayableLeaseReason, parseRetryAfterSeconds, readFacilitatorError, resolveEnvelopeVersion, toPaymentRequirementsV2, toResourceInfoV2 } from './backend/index.mjs';
7
+ export { AMBIGUOUS_LEASE_REASONS, DEFAULT_FACILITATOR_RETRIES, DEFAULT_RETRY_AFTER_SECONDS, FacilitatorClient, FacilitatorClientOptions, FacilitatorErrorInfo, FacilitatorFailureFields, FacilitatorFetchOptions, HonoMiddlewareOptions, MAX_RETRY_AFTER_SECONDS, ParsedFacilitatorErrorBody, PaymentAcceptance, PaymentMiddlewareOptions, PaymentPayloadV2, PaymentRequirementsV2, REPLAYABLE_LEASE_REASONS, ResourceInfoV2, SETTLEMENT_UNCONFIRMED, SettleRequestV2, VerifiedPaymentState, VerifyRequestV2, WRITER_LEASE_REASONS, WriterLeaseReason, X402_CORS_HEADERS, X402_HEADER_NAMES, buildPaymentRequirements, buildSettleRequest, buildSettleRequestForVersion, buildSettleRequestV2, buildVerifyRequest, buildVerifyRequestForVersion, buildVerifyRequestV2, create402Response, createHonoMiddleware, createPaymentMiddleware, extractPaymentFromHeaders, facilitatorFetch, getCorsHeaders, isAmbiguousLeaseReason, isReplayableLeaseReason, isSettlementUnconfirmed, parseFacilitatorErrorBody, parseRetryAfterSeconds, readFacilitatorError, resolveEnvelopeVersion, toPaymentRequirementsV2, toResourceInfoV2 } from './backend/index.mjs';
8
8
 
9
9
  /**
10
10
  * DX402 `durable-evidence`: recover a paid response after the fact.
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { A as AlgorandPaymentPayload, C as CAIP2_IDENTIFIERS, a as CAIP2_TO_CHAI
4
4
  import { S as SigningWalletAdapter } from './wallet-0cX9Pw2F.js';
5
5
  export { E as EIP3009Authorization, a as EIP3009Params } from './wallet-0cX9Pw2F.js';
6
6
  export { E as EnvKeyAdapter, O as OWSWallet, a as OWSWalletAdapter } from './ows-DTDixPzO.js';
7
- export { AMBIGUOUS_LEASE_REASONS, DEFAULT_FACILITATOR_RETRIES, DEFAULT_RETRY_AFTER_SECONDS, FacilitatorClient, FacilitatorClientOptions, FacilitatorErrorInfo, FacilitatorFailureFields, FacilitatorFetchOptions, HonoMiddlewareOptions, MAX_RETRY_AFTER_SECONDS, PaymentAcceptance, PaymentMiddlewareOptions, PaymentPayloadV2, PaymentRequirementsV2, REPLAYABLE_LEASE_REASONS, ResourceInfoV2, SettleRequestV2, VerifiedPaymentState, VerifyRequestV2, WRITER_LEASE_REASONS, WriterLeaseReason, X402_CORS_HEADERS, X402_HEADER_NAMES, buildPaymentRequirements, buildSettleRequest, buildSettleRequestForVersion, buildSettleRequestV2, buildVerifyRequest, buildVerifyRequestForVersion, buildVerifyRequestV2, create402Response, createHonoMiddleware, createPaymentMiddleware, extractPaymentFromHeaders, facilitatorFetch, getCorsHeaders, isAmbiguousLeaseReason, isReplayableLeaseReason, parseRetryAfterSeconds, readFacilitatorError, resolveEnvelopeVersion, toPaymentRequirementsV2, toResourceInfoV2 } from './backend/index.js';
7
+ export { AMBIGUOUS_LEASE_REASONS, DEFAULT_FACILITATOR_RETRIES, DEFAULT_RETRY_AFTER_SECONDS, FacilitatorClient, FacilitatorClientOptions, FacilitatorErrorInfo, FacilitatorFailureFields, FacilitatorFetchOptions, HonoMiddlewareOptions, MAX_RETRY_AFTER_SECONDS, ParsedFacilitatorErrorBody, PaymentAcceptance, PaymentMiddlewareOptions, PaymentPayloadV2, PaymentRequirementsV2, REPLAYABLE_LEASE_REASONS, ResourceInfoV2, SETTLEMENT_UNCONFIRMED, SettleRequestV2, VerifiedPaymentState, VerifyRequestV2, WRITER_LEASE_REASONS, WriterLeaseReason, X402_CORS_HEADERS, X402_HEADER_NAMES, buildPaymentRequirements, buildSettleRequest, buildSettleRequestForVersion, buildSettleRequestV2, buildVerifyRequest, buildVerifyRequestForVersion, buildVerifyRequestV2, create402Response, createHonoMiddleware, createPaymentMiddleware, extractPaymentFromHeaders, facilitatorFetch, getCorsHeaders, isAmbiguousLeaseReason, isReplayableLeaseReason, isSettlementUnconfirmed, parseFacilitatorErrorBody, parseRetryAfterSeconds, readFacilitatorError, resolveEnvelopeVersion, toPaymentRequirementsV2, toResourceInfoV2 } from './backend/index.js';
8
8
 
9
9
  /**
10
10
  * DX402 `durable-evidence`: recover a paid response after the fact.
package/dist/index.js CHANGED
@@ -3764,6 +3764,7 @@ var REPLAYABLE_LEASE_REASONS = [
3764
3764
  "body_unreadable"
3765
3765
  ];
3766
3766
  var AMBIGUOUS_LEASE_REASONS = ["forward_failed"];
3767
+ var SETTLEMENT_UNCONFIRMED = "settlement_unconfirmed";
3767
3768
  var MAX_RETRY_AFTER_SECONDS = 15;
3768
3769
  var DEFAULT_RETRY_AFTER_SECONDS = 5;
3769
3770
  var DEFAULT_FACILITATOR_RETRIES = 2;
@@ -3785,15 +3786,44 @@ function parseRetryAfterSeconds(response) {
3785
3786
  if (!Number.isFinite(seconds) || seconds < 0) return void 0;
3786
3787
  return Math.min(seconds, MAX_RETRY_AFTER_SECONDS);
3787
3788
  }
3788
- function reasonFrom(body) {
3789
+ function stringField(source, key) {
3790
+ const value = source[key];
3791
+ return typeof value === "string" && value !== "" ? value : void 0;
3792
+ }
3793
+ function transactionHashIn(source) {
3794
+ const tx = source.transaction;
3795
+ if (tx && typeof tx === "object") {
3796
+ const hash = tx.hash;
3797
+ if (typeof hash === "string" && hash !== "") return hash;
3798
+ }
3799
+ if (typeof tx === "string" && tx !== "") return tx;
3800
+ for (const key of ["txHash", "tx_hash", "transaction_hash"]) {
3801
+ const value = stringField(source, key);
3802
+ if (value !== void 0) return value;
3803
+ }
3804
+ return void 0;
3805
+ }
3806
+ function parseFacilitatorErrorBody(body) {
3807
+ let parsed;
3789
3808
  try {
3790
- const parsed = JSON.parse(body);
3791
- if (parsed && typeof parsed === "object" && typeof parsed.reason === "string") {
3792
- return parsed.reason;
3793
- }
3809
+ parsed = JSON.parse(body);
3794
3810
  } catch {
3811
+ return {};
3795
3812
  }
3796
- return void 0;
3813
+ if (!parsed || typeof parsed !== "object") return {};
3814
+ return {
3815
+ errorCode: stringField(parsed, "error"),
3816
+ reason: stringField(parsed, "reason"),
3817
+ transaction: transactionHashIn(parsed),
3818
+ paymentId: stringField(parsed, "paymentId"),
3819
+ retryable: typeof parsed.retryable === "boolean" ? parsed.retryable : void 0
3820
+ };
3821
+ }
3822
+ function isDeclaredUnretryable(parsed) {
3823
+ return parsed.retryable === false || parsed.errorCode === SETTLEMENT_UNCONFIRMED || parsed.transaction !== void 0;
3824
+ }
3825
+ function isSettlementUnconfirmed(failure) {
3826
+ return failure.errorCode === SETTLEMENT_UNCONFIRMED;
3797
3827
  }
3798
3828
  async function readFacilitatorError(response) {
3799
3829
  let body = "";
@@ -3803,14 +3833,19 @@ async function readFacilitatorError(response) {
3803
3833
  body = "";
3804
3834
  }
3805
3835
  const status = response.status;
3806
- const reason = reasonFrom(body);
3807
- const retryable = status === 429 || status === 502 || status === 503 || status === 504;
3836
+ const parsed = parseFacilitatorErrorBody(body);
3837
+ const reason = parsed.reason;
3838
+ const transportRetryable = status === 429 || status === 502 || status === 503 || status === 504;
3839
+ const retryable = transportRetryable && !isDeclaredUnretryable(parsed);
3808
3840
  const retryAfterSeconds = retryable ? parseRetryAfterSeconds(response) ?? DEFAULT_RETRY_AFTER_SECONDS : void 0;
3809
- const safeToReplay = status === 429 || status === 503 && isReplayableLeaseReason(reason);
3841
+ const safeToReplay = retryable && (status === 429 || status === 503 && isReplayableLeaseReason(reason));
3810
3842
  return {
3811
3843
  error: `Facilitator error: ${status} - ${body}`,
3812
3844
  status,
3813
3845
  reason,
3846
+ errorCode: parsed.errorCode,
3847
+ transaction: parsed.transaction,
3848
+ paymentId: parsed.paymentId,
3814
3849
  retryAfterSeconds,
3815
3850
  retryable,
3816
3851
  safeToReplay,
@@ -3823,6 +3858,11 @@ function failureFields(info) {
3823
3858
  retryable: info.retryable,
3824
3859
  safeToReplay: info.safeToReplay,
3825
3860
  ...info.reason !== void 0 ? { reason: info.reason } : {},
3861
+ ...info.errorCode !== void 0 ? { errorCode: info.errorCode } : {},
3862
+ // Without these the caller is told "do not retry" and given nothing to do
3863
+ // instead — an error that swallows the hash is the same defect one layer up.
3864
+ ...info.transaction !== void 0 ? { transaction: info.transaction } : {},
3865
+ ...info.paymentId !== void 0 ? { paymentId: info.paymentId } : {},
3826
3866
  ...info.retryAfterSeconds !== void 0 ? { retryAfterSeconds: info.retryAfterSeconds } : {}
3827
3867
  };
3828
3868
  }
@@ -3830,6 +3870,9 @@ function carryFailureFields(source) {
3830
3870
  return {
3831
3871
  ...source.status !== void 0 ? { status: source.status } : {},
3832
3872
  ...source.reason !== void 0 ? { reason: source.reason } : {},
3873
+ ...source.errorCode !== void 0 ? { errorCode: source.errorCode } : {},
3874
+ ...source.transaction !== void 0 ? { transaction: source.transaction } : {},
3875
+ ...source.paymentId !== void 0 ? { paymentId: source.paymentId } : {},
3833
3876
  ...source.retryable !== void 0 ? { retryable: source.retryable } : {},
3834
3877
  ...source.retryAfterSeconds !== void 0 ? { retryAfterSeconds: source.retryAfterSeconds } : {},
3835
3878
  ...source.safeToReplay !== void 0 ? { safeToReplay: source.safeToReplay } : {}
@@ -3917,7 +3960,23 @@ function buildPaymentRequirements(options) {
3917
3960
  }
3918
3961
  function buildVerifyRequest(paymentHeader, requirements) {
3919
3962
  return {
3920
- x402Version: paymentHeader.x402Version,
3963
+ // The literal `1` names THIS ENVELOPE, not the payer's header. Echoing
3964
+ // `paymentHeader.x402Version` here -- what this did until 2026-09-04 --
3965
+ // let a buyer who declared `2` produce a body that says "2" while carrying
3966
+ // `paymentRequirements`, which is the v1 shape. The facilitator serves it
3967
+ // anyway because its envelope enum is untagged and matches on shape, so
3968
+ // nothing broke; but it ALREADY picks the hint in its 400 off this marker:
3969
+ //
3970
+ // "This body declares `x402Version: 2`. x402 v2 is a JSON object with
3971
+ // `paymentPayload`, `resource` and `accepted`..."
3972
+ //
3973
+ // So the day that body fails for any other reason, the diagnosis sends the
3974
+ // integrator to document the wrong shape. That inversion -- being told to
3975
+ // fix the fields when the wrapper is what is wrong -- is what cost two
3976
+ // teams a day. The payer's own marker survives untouched inside
3977
+ // `paymentPayload`, where it belongs: it describes the payment, not the
3978
+ // envelope carrying it.
3979
+ x402Version: 1,
3921
3980
  paymentPayload: paymentHeader,
3922
3981
  paymentRequirements: requirements
3923
3982
  };
@@ -3940,13 +3999,21 @@ function buildSettleRequestV2(payload, resource, accepted) {
3940
3999
  }
3941
4000
  function buildSettleRequest(paymentHeader, requirements) {
3942
4001
  return {
3943
- x402Version: paymentHeader.x402Version,
4002
+ // `1` for the same reason as {@link buildVerifyRequest}: it names the
4003
+ // envelope, and `/settle` takes the same body as `/verify`.
4004
+ x402Version: 1,
3944
4005
  paymentPayload: paymentHeader,
3945
4006
  paymentRequirements: requirements
3946
4007
  };
3947
4008
  }
3948
4009
  function isCaip2Network(network) {
3949
- return network.includes(":");
4010
+ return typeof network === "string" && network.includes(":");
4011
+ }
4012
+ function networkOfPayload(payload) {
4013
+ const top = payload.network;
4014
+ if (typeof top === "string") return top;
4015
+ const accepted = payload.accepted;
4016
+ return typeof accepted?.network === "string" ? accepted.network : void 0;
3950
4017
  }
3951
4018
  function toResourceInfoV2(requirements) {
3952
4019
  return {
@@ -3956,9 +4023,15 @@ function toResourceInfoV2(requirements) {
3956
4023
  };
3957
4024
  }
3958
4025
  function toPaymentRequirementsV2(requirements) {
4026
+ const network = isCaip2Network(requirements.network) ? requirements.network : chainToCAIP2(requirements.network);
4027
+ if (!isCaip2Network(network)) {
4028
+ throw new Error(
4029
+ `Network '${requirements.network}' has no CAIP-2 form, so it cannot travel in the x402 v2 envelope. Use x402Version: 1 for this network.`
4030
+ );
4031
+ }
3959
4032
  return {
3960
4033
  scheme: requirements.scheme,
3961
- network: isCaip2Network(requirements.network) ? requirements.network : chainToCAIP2(requirements.network),
4034
+ network,
3962
4035
  asset: requirements.asset,
3963
4036
  amount: requirements.maxAmountRequired,
3964
4037
  payTo: requirements.payTo,
@@ -3971,7 +4044,7 @@ function resolveEnvelopeVersion(paymentHeader, requirements, requested = "auto")
3971
4044
  if (requested !== "auto") {
3972
4045
  return requested;
3973
4046
  }
3974
- return isCaip2Network(paymentHeader.network) || isCaip2Network(requirements.network) ? 2 : 1;
4047
+ return isCaip2Network(networkOfPayload(paymentHeader)) || isCaip2Network(requirements.network) ? 2 : 1;
3975
4048
  }
3976
4049
  function buildVerifyRequestForVersion(paymentHeader, requirements, version) {
3977
4050
  if (version === 2) {
@@ -4644,16 +4717,23 @@ function createPaymentMiddleware(getRequirements, options = {}) {
4644
4717
  respondUnavailable(res, "Payment settlement unavailable", settleResult);
4645
4718
  return;
4646
4719
  }
4647
- res.status(500).json({
4648
- error: "Payment settlement failed",
4649
- reason: settleResult.error || "Unknown settlement error"
4650
- });
4720
+ res.status(500).json(settlementFailureBody(settleResult, "Unknown settlement error"));
4651
4721
  return;
4652
4722
  }
4653
4723
  }
4654
4724
  next();
4655
4725
  };
4656
4726
  }
4727
+ function settlementFailureBody(failure, fallbackReason) {
4728
+ return {
4729
+ error: "Payment settlement failed",
4730
+ reason: failure.error || fallbackReason,
4731
+ retryable: false,
4732
+ ...failure.errorCode !== void 0 ? { errorCode: failure.errorCode } : {},
4733
+ ...failure.transaction !== void 0 ? { transaction: failure.transaction } : {},
4734
+ ...failure.paymentId !== void 0 ? { paymentId: failure.paymentId } : {}
4735
+ };
4736
+ }
4657
4737
  function honoUnavailable(c, message, failure) {
4658
4738
  const seconds = Math.max(1, Math.ceil(failure.retryAfterSeconds ?? DEFAULT_RETRY_AFTER_SECONDS));
4659
4739
  c.header?.("Retry-After", String(seconds));
@@ -4728,10 +4808,7 @@ function createHonoMiddleware(options) {
4728
4808
  if (settleResult.retryable) {
4729
4809
  return honoUnavailable(c, "Payment settlement unavailable", settleResult);
4730
4810
  }
4731
- return c.json({
4732
- error: "Payment settlement failed",
4733
- reason: settleResult.error || "Unknown error"
4734
- }, 500);
4811
+ return c.json(settlementFailureBody(settleResult, "Unknown error"), 500);
4735
4812
  }
4736
4813
  }
4737
4814
  await next();
@@ -4787,6 +4864,7 @@ exports.OPERATOR_FEE_BPS = OPERATOR_FEE_BPS;
4787
4864
  exports.OWSWalletAdapter = OWSWalletAdapter;
4788
4865
  exports.PAYMENT_HEADER_NAMES = PAYMENT_HEADER_NAMES;
4789
4866
  exports.REPLAYABLE_LEASE_REASONS = REPLAYABLE_LEASE_REASONS;
4867
+ exports.SETTLEMENT_UNCONFIRMED = SETTLEMENT_UNCONFIRMED;
4790
4868
  exports.SMA_WRAP_TARGETS = SMA_WRAP_TARGETS;
4791
4869
  exports.SSEParser = SSEParser;
4792
4870
  exports.SUPPORTED_CHAINS = SUPPORTED_CHAINS;
@@ -4867,12 +4945,14 @@ exports.isDelegated = isDelegated;
4867
4945
  exports.isEndToEnd = isEndToEnd;
4868
4946
  exports.isReplayableLeaseReason = isReplayableLeaseReason;
4869
4947
  exports.isSVMChain = isSVMChain;
4948
+ exports.isSettlementUnconfirmed = isSettlementUnconfirmed;
4870
4949
  exports.isSuiChain = isSuiChain;
4871
4950
  exports.isTokenSupported = isTokenSupported;
4872
4951
  exports.isXRPLChain = isXRPLChain;
4873
4952
  exports.matchesFilters = matchesFilters;
4874
4953
  exports.needsAccountWrap = needsAccountWrap;
4875
4954
  exports.parseEvidenceHeader = parseEvidenceHeader;
4955
+ exports.parseFacilitatorErrorBody = parseFacilitatorErrorBody;
4876
4956
  exports.parseNetworkIdentifier = parseNetworkIdentifier;
4877
4957
  exports.parseRetryAfterSeconds = parseRetryAfterSeconds;
4878
4958
  exports.parseSealed = parseSealed;