Sign In

@x402/core

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@x402/core - npm Package Compare versions

Comparing version
2.20.0
to
2.21.0
dist/cjs/x402Client-CzZlbbXy.d.ts

Sorry, the diff of this file is too big to display

+163
// src/utils/index.ts
function numberToDecimalString(n) {
const str = n.toString();
if (!/[eE]/.test(str)) return str;
const [significand, exponentStr] = str.split(/[eE]/);
const exp = parseInt(exponentStr, 10);
const negative = significand.startsWith("-");
const abs = negative ? significand.slice(1) : significand;
const [intDigits, fracDigits = ""] = abs.split(".");
const allDigits = intDigits + fracDigits;
const decimalPos = intDigits.length + exp;
let result;
if (decimalPos <= 0) {
result = "0." + "0".repeat(-decimalPos) + allDigits;
} else if (decimalPos >= allDigits.length) {
result = allDigits + "0".repeat(decimalPos - allDigits.length);
} else {
result = allDigits.slice(0, decimalPos) + "." + allDigits.slice(decimalPos);
}
return (negative ? "-" : "") + result;
}
function parseMoneyString(money) {
const cleaned = money.replace(/^\$/, "").trim();
if (!/^-?\d+(?:\.\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {
throw new Error(`Invalid money format: ${money}`);
}
const amount = Number(cleaned);
if (!Number.isFinite(amount) || amount < 0) {
throw new Error(`Invalid money format: ${money}`);
}
return amount;
}
function convertToTokenAmount(decimalAmount, decimals) {
if (/[eE]/.test(decimalAmount)) {
throw new Error(
`Invalid amount: ${decimalAmount} \u2014 use decimal notation, not scientific notation`
);
}
if (!/^-?\d+\.?\d*$/.test(decimalAmount)) {
throw new Error(`Invalid amount: ${decimalAmount}`);
}
const [intPart, decPart = ""] = decimalAmount.split(".");
const paddedDec = decPart.padEnd(decimals, "0").slice(0, decimals);
const tokenAmount = (intPart + paddedDec).replace(/^0+/, "") || "0";
if (tokenAmount === "0" && /[1-9]/.test(decimalAmount)) {
throw new Error(
`Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`
);
}
return tokenAmount;
}
var escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
var networkPatternToRegExp = (pattern) => {
const source = escapeRegExp(pattern).replace(/\\\*/g, ".*");
return new RegExp(`^${source}$`);
};
var networkMatchesPattern = (pattern, network) => {
return networkPatternToRegExp(pattern).test(network);
};
var findSchemesByNetwork = (map, network) => {
let implementationsByScheme = map.get(network);
if (!implementationsByScheme) {
for (const [registeredNetworkPattern, implementations] of map.entries()) {
if (networkMatchesPattern(registeredNetworkPattern, network)) {
implementationsByScheme = implementations;
break;
}
}
}
return implementationsByScheme;
};
var findByNetworkAndScheme = (map, scheme, network) => {
return findSchemesByNetwork(map, network)?.get(scheme);
};
var findFacilitatorBySchemeAndNetwork = (schemeMap, scheme, network) => {
const schemeData = schemeMap.get(scheme);
if (!schemeData) return void 0;
if (schemeData.networks.has(network)) {
return schemeData.facilitator;
}
if (networkMatchesPattern(schemeData.pattern, network)) {
return schemeData.facilitator;
}
return void 0;
};
var Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;
function safeBase64Encode(data) {
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
const bytes = new TextEncoder().encode(data);
const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join("");
return globalThis.btoa(binaryString);
}
return Buffer.from(data, "utf8").toString("base64");
}
function safeBase64Decode(data) {
if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") {
const binaryString = globalThis.atob(data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
const decoder = new TextDecoder("utf-8");
return decoder.decode(bytes);
}
return Buffer.from(data, "base64").toString("utf-8");
}
function deepEqual(obj1, obj2) {
const normalize = (obj) => {
if (obj === null || obj === void 0) return JSON.stringify(obj);
if (typeof obj !== "object") return JSON.stringify(obj);
if (Array.isArray(obj)) {
return JSON.stringify(
obj.map(
(item) => typeof item === "object" && item !== null ? JSON.parse(normalize(item)) : item
)
);
}
const sorted = {};
Object.keys(obj).sort().forEach((key) => {
const value = obj[key];
sorted[key] = typeof value === "object" && value !== null ? JSON.parse(normalize(value)) : value;
});
return JSON.stringify(sorted);
};
try {
return normalize(obj1) === normalize(obj2);
} catch {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
}
function toComparableArray(value) {
if (Array.isArray(value)) {
return value;
}
if (value === null || value === void 0 || typeof value === "object") {
return void 0;
}
return [value];
}
var ADDITIVE_ARRAY_INFO_FIELDS = {
"builder-code": /* @__PURE__ */ new Set(["s"])
};
var ADDITIVE_ARRAY_MAX_LENGTHS = {
"builder-code": { s: 10 }
};
export {
numberToDecimalString,
parseMoneyString,
convertToTokenAmount,
networkMatchesPattern,
findSchemesByNetwork,
findByNetworkAndScheme,
findFacilitatorBySchemeAndNetwork,
Base64EncodedRegex,
safeBase64Encode,
safeBase64Decode,
deepEqual,
toComparableArray,
ADDITIVE_ARRAY_INFO_FIELDS,
ADDITIVE_ARRAY_MAX_LENGTHS
};
//# sourceMappingURL=chunk-HX5GESJI.mjs.map
{"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Converts a JavaScript number to a plain decimal string, expanding scientific notation\n * via string manipulation rather than parseFloat round-tripping.\n *\n * e.g. 1e-7 → \"0.0000001\", 4.02 → \"4.02\"\n *\n * @param n - The number to convert\n * @returns A plain decimal string representation with no scientific notation\n */\nexport function numberToDecimalString(n: number): string {\n const str = n.toString();\n if (!/[eE]/.test(str)) return str;\n\n const [significand, exponentStr] = str.split(/[eE]/);\n const exp = parseInt(exponentStr, 10);\n const negative = significand.startsWith(\"-\");\n const abs = negative ? significand.slice(1) : significand;\n const [intDigits, fracDigits = \"\"] = abs.split(\".\");\n const allDigits = intDigits + fracDigits;\n const decimalPos = intDigits.length + exp;\n\n let result: string;\n if (decimalPos <= 0) {\n result = \"0.\" + \"0\".repeat(-decimalPos) + allDigits;\n } else if (decimalPos >= allDigits.length) {\n result = allDigits + \"0\".repeat(decimalPos - allDigits.length);\n } else {\n result = allDigits.slice(0, decimalPos) + \".\" + allDigits.slice(decimalPos);\n }\n return (negative ? \"-\" : \"\") + result;\n}\n\n/**\n * Parses a money string into a finite, non-negative decimal number.\n * Accepts plain decimal strings with an optional leading dollar sign.\n *\n * @param money - The money string to parse\n * @returns Decimal number\n */\nexport function parseMoneyString(money: string): number {\n const cleaned = money.replace(/^\\$/, \"\").trim();\n if (!/^-?\\d+(?:\\.\\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n const amount = Number(cleaned);\n if (!Number.isFinite(amount) || amount < 0) {\n throw new Error(`Invalid money format: ${money}`);\n }\n return amount;\n}\n\n/**\n * Convert a decimal amount to token smallest units.\n * Accepts only plain decimal strings — scientific notation is not allowed.\n * Throws if the amount is non-zero but too small to represent with the given decimal precision.\n *\n * @param decimalAmount - The decimal amount as a plain string (e.g., \"0.10\")\n * @param decimals - The number of decimals for the token (e.g., 6 for USDC)\n * @returns The amount in smallest units as a string\n */\nexport function convertToTokenAmount(decimalAmount: string, decimals: number): string {\n if (/[eE]/.test(decimalAmount)) {\n throw new Error(\n `Invalid amount: ${decimalAmount} — use decimal notation, not scientific notation`,\n );\n }\n if (!/^-?\\d+\\.?\\d*$/.test(decimalAmount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const [intPart, decPart = \"\"] = decimalAmount.split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n if (tokenAmount === \"0\" && /[1-9]/.test(decimalAmount)) {\n throw new Error(\n `Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`,\n );\n }\n return tokenAmount;\n}\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\nconst networkPatternToRegExp = (pattern: Network): RegExp => {\n const source = escapeRegExp(pattern).replace(/\\\\\\*/g, \".*\");\n return new RegExp(`^${source}$`);\n};\n\nexport const networkMatchesPattern = (pattern: Network, network: Network): boolean => {\n return networkPatternToRegExp(pattern).test(network);\n};\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n if (networkMatchesPattern(registeredNetworkPattern as Network, network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n\n/**\n * Coerces a value for array-aware merging/comparison: an array passes through\n * unchanged, while a bare scalar is wrapped as a single-element array so it can\n * merge or compare against an array declared on the other side (e.g. an\n * extension field documented as \"string or array of strings\"). Returns\n * undefined for values that cannot participate (null, undefined, objects).\n *\n * @param value - Value to coerce\n * @returns The value as an array, or undefined if it cannot be treated as one\n */\nexport function toComparableArray(value: unknown): unknown[] | undefined {\n if (Array.isArray(value)) {\n return value;\n }\n if (value === null || value === undefined || typeof value === \"object\") {\n return undefined;\n }\n return [value];\n}\n\n/**\n * Extension info fields, keyed by extension key, where a conflicting array\n * value declared by both server and client is additive rather than exclusive:\n * the client's merge concatenates both sides (client first, deduped, see\n * `mergeArraysUnique`), and the server's echo validation accepts any echo that\n * is a superset of the advertised value. Scoped narrowly per field (rather\n * than making all array fields additive) so unrelated extensions - e.g.\n * sign-in-with-x's `resources` - keep exact array matching in both directions.\n */\nexport const ADDITIVE_ARRAY_INFO_FIELDS: Record<string, ReadonlySet<string>> = {\n \"builder-code\": new Set([\"s\"]),\n};\n\n/**\n * Caps the combined echoed length of an additive array field (see\n * {@link ADDITIVE_ARRAY_INFO_FIELDS}) so a hand-crafted payload cannot pad the\n * field past the sum of every party's own reservation and later crowd out a\n * legitimately declared entry once truncated further downstream (e.g. by a\n * facilitator extension). Core has no dependency on extension packages, so\n * this value (builder-code's `MAX_CLIENT_SERVICE_CODES` +\n * `MAX_SERVER_SERVICE_CODES`) is duplicated from\n * `packages/extensions/src/builder-code/types.ts` and must be kept in sync by\n * hand.\n */\nexport const ADDITIVE_ARRAY_MAX_LENGTHS: Record<string, Record<string, number>> = {\n \"builder-code\": { s: 10 },\n};\n"],"mappings":";AAWO,SAAS,sBAAsB,GAAmB;AACvD,QAAM,MAAM,EAAE,SAAS;AACvB,MAAI,CAAC,OAAO,KAAK,GAAG,EAAG,QAAO;AAE9B,QAAM,CAAC,aAAa,WAAW,IAAI,IAAI,MAAM,MAAM;AACnD,QAAM,MAAM,SAAS,aAAa,EAAE;AACpC,QAAM,WAAW,YAAY,WAAW,GAAG;AAC3C,QAAM,M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AAU,MAAM;AAAA,EAC9B;AAEA,MAAI;AACF,WAAO,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC3C,QAAQ;AAEN,WAAO,KAAK,UAAU,IAAI,MAAM,KAAK,UAAU,IAAI;AAAA,EACrD;AACF;AAYO,SAAS,kBAAkB,OAAuC;AACvE,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO;AAAA,EACT;AACA,MAAI,UAAU,QAAQ,UAAU,UAAa,OAAO,UAAU,UAAU;AACtE,WAAO;AAAA,EACT;AACA,SAAO,CAAC,KAAK;AACf;AAWO,IAAM,6BAAkE;AAAA,EAC7E,gBAAgB,oBAAI,IAAI,CAAC,GAAG,CAAC;AAC/B;AAaO,IAAM,6BAAqE;AAAA,EAChF,gBAAgB,EAAE,GAAG,GAAG;AAC1B;","names":[]}
// src/types/facilitator.ts
var VerifyError = class extends Error {
/**
* Creates a VerifyError from a failed verification response.
*
* @param statusCode - HTTP status code from the facilitator
* @param response - The verify response containing failure details
*/
constructor(statusCode, response) {
const reason = response.invalidReason || "unknown reason";
const message = response.invalidMessage;
super(message ? `${reason}: ${message}` : reason);
this.name = "VerifyError";
this.statusCode = statusCode;
this.invalidReason = response.invalidReason;
this.invalidMessage = response.invalidMessage;
this.payer = response.payer;
}
};
var SettleError = class extends Error {
/**
* Creates a SettleError from a failed settlement response.
*
* @param statusCode - HTTP status code from the facilitator
* @param response - The settle response containing error details
*/
constructor(statusCode, response) {
const reason = response.errorReason || "unknown reason";
const message = response.errorMessage;
super(message ? `${reason}: ${message}` : reason);
this.name = "SettleError";
this.statusCode = statusCode;
this.errorReason = response.errorReason;
this.errorMessage = response.errorMessage;
this.payer = response.payer;
this.transaction = response.transaction;
this.network = response.network;
}
};
var FacilitatorResponseError = class extends Error {
/**
* Creates a FacilitatorResponseError for malformed facilitator responses.
*
* @param message - The boundary error message
*/
constructor(message) {
super(message);
this.name = "FacilitatorResponseError";
}
};
var FacilitatorTimeoutError = class extends FacilitatorResponseError {
/**
* Creates a FacilitatorTimeoutError.
*
* @param operation - The facilitator operation that timed out
* @param timeoutMs - The configured timeout in milliseconds
*/
constructor(operation, timeoutMs) {
super(`Facilitator ${operation} request timed out after ${timeoutMs}ms`);
this.name = "FacilitatorTimeoutError";
this.operation = operation;
this.timeoutMs = timeoutMs;
}
};
function getFacilitatorResponseError(error) {
let current = error;
while (current instanceof Error) {
if (current instanceof FacilitatorResponseError) {
return current;
}
current = current.cause;
}
return null;
}
export {
VerifyError,
SettleError,
FacilitatorResponseError,
FacilitatorTimeoutError,
getFacilitatorResponseError
};
//# sourceMappingURL=chunk-P3DFEIO7.mjs.map
{"version":3,"sources":["../../src/types/facilitator.ts"],"sourcesContent":["import { PaymentPayload, PaymentRequirements } from \"./payments\";\nimport { Network } from \"./\";\n\nexport type VerifyRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type VerifyResponse = {\n isValid: boolean;\n invalidReason?: string;\n invalidMessage?: string;\n payer?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SettleRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type SettleResponse = {\n success: boolean;\n errorReason?: string;\n errorMessage?: string;\n payer?: string;\n transaction: string;\n network: Network;\n /** Actual amount settled in atomic token units. Present for schemes like `upto` where settlement amount may differ from the authorized maximum. */\n amount?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedKind = {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedResponse = {\n kinds: SupportedKind[];\n extensions: string[];\n signers: Record<string, string[]>; // CAIP family pattern → Signer addresses\n};\n\n/**\n * Error thrown when payment verification fails.\n */\nexport class VerifyError extends Error {\n readonly invalidReason?: string;\n readonly invalidMessage?: string;\n readonly payer?: string;\n readonly statusCode: number;\n\n /**\n * Creates a VerifyError from a failed verification response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The verify response containing failure details\n */\n constructor(statusCode: number, response: VerifyResponse) {\n const reason = response.invalidReason || \"unknown reason\";\n const message = response.invalidMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"VerifyError\";\n this.statusCode = statusCode;\n this.invalidReason = response.invalidReason;\n this.invalidMessage = response.invalidMessage;\n this.payer = response.payer;\n }\n}\n\n/**\n * Error thrown when payment settlement fails.\n */\nexport class SettleError extends Error {\n readonly errorReason?: string;\n readonly errorMessage?: string;\n readonly payer?: string;\n readonly transaction: string;\n readonly network: Network;\n readonly statusCode: number;\n\n /**\n * Creates a SettleError from a failed settlement response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The settle response containing error details\n */\n constructor(statusCode: number, response: SettleResponse) {\n const reason = response.errorReason || \"unknown reason\";\n const message = response.errorMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"SettleError\";\n this.statusCode = statusCode;\n this.errorReason = response.errorReason;\n this.errorMessage = response.errorMessage;\n this.payer = response.payer;\n this.transaction = response.transaction;\n this.network = response.network;\n }\n}\n\n/**\n * Error thrown when a facilitator returns malformed success payload data.\n */\nexport class FacilitatorResponseError extends Error {\n /**\n * Creates a FacilitatorResponseError for malformed facilitator responses.\n *\n * @param message - The boundary error message\n */\n constructor(message: string) {\n super(message);\n this.name = \"FacilitatorResponseError\";\n }\n}\n\n/**\n * Error thrown when a facilitator HTTP request exceeds the client's configured timeout.\n *\n * Extends FacilitatorResponseError so HTTP middlewares surface it as a facilitator\n * boundary failure (502) rather than an unhandled runtime error.\n *\n * Note: for settle(), a client-side timeout is an indeterminate outcome — the\n * facilitator may have received and completed the settlement after the client\n * stopped waiting. Callers must not treat this error as proof that settlement\n * did not occur.\n */\nexport class FacilitatorTimeoutError extends FacilitatorResponseError {\n /** The facilitator operation that timed out (\"verify\", \"settle\", or \"supported\") */\n readonly operation: string;\n /** The timeout that elapsed, in milliseconds */\n readonly timeoutMs: number;\n\n /**\n * Creates a FacilitatorTimeoutError.\n *\n * @param operation - The facilitator operation that timed out\n * @param timeoutMs - The configured timeout in milliseconds\n */\n constructor(operation: string, timeoutMs: number) {\n super(`Facilitator ${operation} request timed out after ${timeoutMs}ms`);\n this.name = \"FacilitatorTimeoutError\";\n this.operation = operation;\n this.timeoutMs = timeoutMs;\n }\n}\n\n/**\n * Walks an error cause chain to find the first facilitator response error.\n *\n * @param error - The thrown value to inspect\n * @returns The nested facilitator response error, if present\n */\nexport function getFacilitatorResponseError(error: unknown): FacilitatorResponseError | null {\n let current = error;\n\n while (current instanceof Error) {\n if (current instanceof FacilitatorResponseError) {\n return current;\n }\n current = current.cause;\n }\n\n return null;\n}\n"],"mappings":";AAqDO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,iBAAiB;AACzC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,gBAAgB,SAAS;AAC9B,SAAK,iBAAiB,SAAS;AAC/B,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAKO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,eAAe;AACvC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,cAAc,SAAS;AAC5B,SAAK,eAAe,SAAS;AAC7B,SAAK,QAAQ,SAAS;AACtB,SAAK,cAAc,SAAS;AAC5B,SAAK,UAAU,SAAS;AAAA,EAC1B;AACF;AAKO,IAAM,2BAAN,cAAuC,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMlD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAaO,IAAM,0BAAN,cAAsC,yBAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYpE,YAAY,WAAmB,WAAmB;AAChD,UAAM,eAAe,SAAS,4BAA4B,SAAS,IAAI;AACvE,SAAK,OAAO;AACZ,SAAK,YAAY;AACjB,SAAK,YAAY;AAAA,EACnB;AACF;AAQO,SAAS,4BAA4B,OAAiD;AAC3F,MAAI,UAAU;AAEd,SAAO,mBAAmB,OAAO;AAC/B,QAAI,mBAAmB,0BAA0B;AAC/C,aAAO;AAAA,IACT;AACA,cAAU,QAAQ;AAAA,EACpB;AAEA,SAAO;AACT;","names":[]}
import {
z
} from "./chunk-N4QXZG2Z.mjs";
import {
x402Version
} from "./chunk-VE37GDG2.mjs";
import {
FacilitatorResponseError,
FacilitatorTimeoutError,
SettleError,
VerifyError
} from "./chunk-P3DFEIO7.mjs";
import {
Base64EncodedRegex,
safeBase64Decode,
safeBase64Encode
} from "./chunk-HX5GESJI.mjs";
import {
__require
} from "./chunk-BJTO5JO5.mjs";
// src/http/x402HTTPResourceServer.ts
var SETTLEMENT_OVERRIDES_HEADER = "Settlement-Overrides";
var PAYMENT_REQUIRED_CACHE_CONTROL = "no-store";
function withPrivateCacheControl(value) {
if (!value) {
return "private";
}
const directives = value.split(",").map((directive) => directive.trim().toLowerCase());
if (directives.includes("private")) {
return value;
}
return `${value}, private`;
}
function checkIfBazaarNeeded(routes) {
if ("accepts" in routes) {
return !!(routes.extensions && "bazaar" in routes.extensions);
}
return Object.values(routes).some((routeConfig) => {
return !!(routeConfig.extensions && "bazaar" in routeConfig.extensions);
});
}
var RouteConfigurationError = class extends Error {
/**
* Creates a new RouteConfigurationError with the given validation errors.
*
* @param errors - The validation errors that caused this exception.
*/
constructor(errors) {
const message = `x402 Route Configuration Errors:
${errors.map((e) => ` - ${e.message}`).join("\n")}`;
super(message);
this.name = "RouteConfigurationError";
this.errors = errors;
}
};
var FALLBACK_PAYWALL_HTML = `<!DOCTYPE html>
<html>
<head>
<title>Payment Required</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;">
<h1>Payment Required</h1>
<p>This resource is protected by the x402 payment protocol.</p>
<p style="margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;">
<strong>Note to developers:</strong> install <code>@x402/paywall</code> to enable
the in-browser wallet connection and payment UI. Programmatic clients should read
the payment requirements from the 402 response headers and JSON body.
</p>
</div>
</body>
</html>`;
var x402HTTPResourceServer = class {
/**
* Creates a new x402HTTPResourceServer instance.
*
* @param ResourceServer - The core x402ResourceServer instance to use
* @param routes - Route configuration for payment-protected endpoints
*/
constructor(ResourceServer, routes) {
this.compiledRoutes = [];
this.protectedRequestHooks = [];
this.ResourceServer = ResourceServer;
this.routesConfig = routes;
const normalizedRoutes = typeof routes === "object" && !("accepts" in routes) ? routes : { "*": routes };
for (const [pattern, config] of Object.entries(normalizedRoutes)) {
const parsed = this.parseRoutePattern(pattern);
this.compiledRoutes.push({
verb: parsed.verb,
regex: parsed.regex,
config,
pattern: parsed.path
});
}
}
/**
* Get the underlying x402ResourceServer instance.
*
* @returns The underlying x402ResourceServer instance
*/
get server() {
return this.ResourceServer;
}
/**
* Get the routes configuration.
*
* @returns The routes configuration
*/
get routes() {
return this.routesConfig;
}
/**
* Initialize the HTTP resource server.
*
* This method initializes the underlying resource server (fetching facilitator support)
* and then validates that all route payment configurations have corresponding
* registered schemes and facilitator support.
*
* @throws RouteConfigurationError if any route's payment options don't have
* corresponding registered schemes or facilitator support
*
* @example
* ```typescript
* const httpServer = new x402HTTPResourceServer(server, routes);
* await httpServer.initialize();
* ```
*/
async initialize() {
await this.ResourceServer.initialize();
const errors = this.validateRouteConfiguration();
if (errors.length > 0) {
throw new RouteConfigurationError(errors);
}
}
/**
* Register a custom paywall provider for generating HTML
*
* @param provider - PaywallProvider instance
* @returns This service instance for chaining
*/
registerPaywallProvider(provider) {
this.paywallProvider = provider;
return this;
}
/**
* Register a hook that runs on every request to a protected route, before payment processing.
* Hooks are executed in order of registration. The first hook to return a non-void result wins.
*
* @param hook - The request hook function
* @returns The x402HTTPResourceServer instance for chaining
*/
onProtectedRequest(hook) {
this.protectedRequestHooks.push(hook);
return this;
}
/**
* Process HTTP request and return response instructions
* This is the main entry point for framework middleware
*
* @param context - HTTP request context
* @param paywallConfig - Optional paywall configuration
* @returns Process result indicating next action for middleware
*/
async processHTTPRequest(context, paywallConfig) {
const method = context.method || context.adapter.getMethod();
context = { ...context, method };
const { adapter, path } = context;
const routeMatch = this.getRouteConfig(path, method);
if (!routeMatch) {
return { type: "no-payment-required" };
}
const { config: routeConfig, pattern: routePattern } = routeMatch;
const enrichedContext = { ...context, routePattern };
for (const hook of this.getProtectedRequestHooks(routeConfig)) {
const result = await hook(enrichedContext, routeConfig);
if (result && "grantAccess" in result) {
return { type: "no-payment-required" };
}
if (result && "abort" in result) {
return {
type: "payment-error",
response: {
status: 403,
headers: { "Content-Type": "application/json" },
body: { error: result.reason }
}
};
}
}
const paymentOptions = this.normalizePaymentOptions(routeConfig);
const paymentPayload = this.extractPayment(adapter);
const resourceInfo = {
url: routeConfig.resource || enrichedContext.adapter.getUrl(),
description: routeConfig.description || "",
mimeType: routeConfig.mimeType || "",
...routeConfig.serviceName !== void 0 && { serviceName: routeConfig.serviceName },
...routeConfig.tags !== void 0 && { tags: routeConfig.tags },
...routeConfig.iconUrl !== void 0 && { iconUrl: routeConfig.iconUrl }
};
let requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions(
paymentOptions,
enrichedContext
);
let extensions = routeConfig.extensions;
if (extensions) {
extensions = this.ResourceServer.enrichExtensions(extensions, enrichedContext);
}
const transportContext = { request: enrichedContext };
const paymentRequired = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
!paymentPayload ? "Payment required" : void 0,
extensions,
transportContext
);
if (!paymentPayload) {
const unpaidBody = routeConfig.unpaidResponseBody ? await routeConfig.unpaidResponseBody(enrichedContext) : void 0;
return {
type: "payment-error",
response: this.createHTTPResponse(
paymentRequired,
this.isWebBrowser(adapter),
paywallConfig,
routeConfig.customPaywallHtml,
unpaidBody
)
};
}
try {
const matchingRequirements = this.ResourceServer.findMatchingRequirements(
paymentRequired.accepts,
paymentPayload
);
if (!matchingRequirements) {
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
"No matching payment requirements",
extensions,
transportContext
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
const extensionResult = this.ResourceServer.validateExtensions(
paymentRequired,
paymentPayload
);
if (!extensionResult.valid) {
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
extensionResult.invalidReason,
extensions,
transportContext,
paymentPayload
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
const verifyResult = await this.ResourceServer.verifyPayment(
paymentPayload,
matchingRequirements,
extensions,
transportContext
);
if (!verifyResult.isValid) {
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
verifyResult.invalidReason,
extensions,
transportContext,
paymentPayload
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
if (verifyResult.skipHandler) {
return await this.processSkipHandlerSettlement(
paymentPayload,
matchingRequirements,
extensions,
transportContext,
verifyResult.skipHandler
);
}
const cancellationDispatcher = this.ResourceServer.createPaymentCancellationDispatcher(
paymentPayload,
matchingRequirements,
extensions,
transportContext
);
return {
type: "payment-verified",
cancellationDispatcher,
paymentPayload,
paymentRequirements: matchingRequirements,
declaredExtensions: extensions
};
} catch (error) {
if (error instanceof FacilitatorResponseError) {
throw error;
}
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
error instanceof Error ? error.message : "Payment verification failed",
extensions,
transportContext
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
}
/**
* Process settlement after successful response
*
* @param paymentPayload - The verified payment payload
* @param requirements - The matching payment requirements
* @param declaredExtensions - Optional declared extensions (for per-key enrichment)
* @param transportContext - Optional HTTP transport context
* @param settlementOverrides - Optional settlement overrides (e.g., partial settlement amount)
* @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure
*/
async processSettlement(paymentPayload, requirements, declaredExtensions, transportContext, settlementOverrides) {
if (transportContext?.request && !transportContext.request.method) {
transportContext = {
...transportContext,
request: {
...transportContext.request,
method: transportContext.request.adapter.getMethod()
}
};
}
try {
let resolvedOverrides = settlementOverrides;
if (!resolvedOverrides && transportContext?.responseHeaders) {
const overridesKey = SETTLEMENT_OVERRIDES_HEADER.toLowerCase();
const rawValue = Object.entries(transportContext.responseHeaders).find(
([key]) => key.toLowerCase() === overridesKey
)?.[1];
if (rawValue) {
try {
resolvedOverrides = JSON.parse(rawValue);
} catch {
}
}
}
const settleResponse = await this.ResourceServer.settlePayment(
paymentPayload,
requirements,
declaredExtensions,
transportContext,
resolvedOverrides
);
if (!settleResponse.success) {
const failure = {
...settleResponse,
success: false,
errorReason: settleResponse.errorReason || "Settlement failed",
errorMessage: settleResponse.errorMessage || settleResponse.errorReason || "Settlement failed",
headers: this.createSettlementHeaders(settleResponse)
};
const response = await this.buildSettlementFailureResponse(failure, transportContext);
return { ...failure, response };
}
return {
...settleResponse,
success: true,
headers: this.createSettlementHeaders(settleResponse),
requirements
};
} catch (error) {
if (error instanceof FacilitatorResponseError) {
throw error;
}
if (error instanceof SettleError) {
const errorReason2 = error.errorReason || error.message;
const settleResponse2 = {
success: false,
errorReason: errorReason2,
errorMessage: error.errorMessage || errorReason2,
payer: error.payer,
network: error.network,
transaction: error.transaction
};
const failure2 = {
...settleResponse2,
success: false,
errorReason: errorReason2,
headers: this.createSettlementHeaders(settleResponse2)
};
const response2 = await this.buildSettlementFailureResponse(failure2, transportContext);
return { ...failure2, response: response2 };
}
const errorReason = error instanceof Error ? error.message : "Settlement failed";
const settleResponse = {
success: false,
errorReason,
errorMessage: errorReason,
network: requirements.network,
transaction: ""
};
const failure = {
...settleResponse,
success: false,
errorReason,
headers: this.createSettlementHeaders(settleResponse)
};
const response = await this.buildSettlementFailureResponse(failure, transportContext);
return { ...failure, response };
}
}
/**
* Check if a request requires payment based on route configuration
*
* @param context - HTTP request context
* @returns True if the route requires payment, false otherwise
*/
requiresPayment(context) {
const method = context.method || context.adapter.getMethod();
return this.getRouteConfig(context.path, method) !== void 0;
}
/**
* Settle a verified payment that requested `skipHandler`, packaging the
* result as a `payment-error` HTTPProcessResult so framework adapters can
* write the response without invoking the route handler.
*
* - On success: status 200 + PAYMENT-RESPONSE header + configured body.
* - On failure: the standard 402 settlement-failure response.
*
* @param paymentPayload - Verified payment payload.
* @param requirements - Matched payment requirements.
* @param declaredExtensions - Optional declared extensions for the route.
* @param transportContext - Optional HTTP transport context.
* @param skipHandlerResponse - Optional content type + body to return on success.
* @returns A `payment-error` HTTPProcessResult carrying the final response.
*/
async processSkipHandlerSettlement(paymentPayload, requirements, declaredExtensions, transportContext, skipHandlerResponse) {
const settleResult = await this.processSettlement(
paymentPayload,
requirements,
declaredExtensions,
transportContext
);
if (!settleResult.success) {
return { type: "payment-error", response: settleResult.response };
}
const contentType = skipHandlerResponse?.contentType ?? "application/json";
const body = skipHandlerResponse?.body ?? {};
return {
type: "payment-error",
response: {
status: 200,
headers: {
"Content-Type": contentType,
...settleResult.headers,
"Cache-Control": withPrivateCacheControl(null)
},
body,
isHtml: contentType.includes("text/html")
}
};
}
/**
* Build HTTPResponseInstructions for settlement failure.
* Uses settlementFailedResponseBody hook if configured, otherwise defaults to empty body.
*
* @param failure - Settlement failure result with headers
* @param transportContext - Optional HTTP transport context for the request
* @returns HTTP response instructions for the 402 settlement failure response
*/
async buildSettlementFailureResponse(failure, transportContext) {
const settlementHeaders = failure.headers;
const routeConfig = transportContext ? this.getRouteConfig(transportContext.request.path, transportContext.request.method) : void 0;
const customBody = routeConfig?.config.settlementFailedResponseBody ? await routeConfig.config.settlementFailedResponseBody(transportContext.request, failure) : void 0;
const contentType = customBody ? customBody.contentType : "application/json";
const body = customBody ? customBody.body : {};
return {
status: 402,
headers: {
"Content-Type": contentType,
...settlementHeaders,
"Cache-Control": PAYMENT_REQUIRED_CACHE_CONTROL
},
body,
isHtml: contentType.includes("text/html")
};
}
/**
* Normalizes a RouteConfig's accepts field into an array of PaymentOptions
* Handles both single PaymentOption and array formats
*
* @param routeConfig - Route configuration
* @returns Array of payment options
*/
normalizePaymentOptions(routeConfig) {
return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts];
}
/**
* Manual request hooks run before extension transport hooks for declared extensions.
*
* @param routeConfig - Route configuration for the matched request
* @returns Hooks in invocation order
*/
getProtectedRequestHooks(routeConfig) {
const hooks = [...this.protectedRequestHooks];
const declaredExtensions = routeConfig.extensions;
if (!declaredExtensions) return hooks;
for (const extension of this.ResourceServer.getExtensions()) {
const hook = extension.transportHooks?.http?.onProtectedRequest;
if (!hook || !(extension.key in declaredExtensions)) continue;
hooks.push(
(context, routeConfig2) => hook(declaredExtensions[extension.key], context, routeConfig2)
);
}
return hooks;
}
/**
* Validates that all payment options in routes have corresponding registered schemes
* and facilitator support.
*
* @returns Array of validation errors (empty if all routes are valid)
*/
validateRouteConfiguration() {
const errors = [];
const normalizedRoutes = typeof this.routesConfig === "object" && !("accepts" in this.routesConfig) ? Object.entries(this.routesConfig) : [["*", this.routesConfig]];
for (const [pattern, config] of normalizedRoutes) {
const pathPart = pattern.includes(" ") ? pattern.split(/\s+/)[1] : pattern;
if (pathPart && pathPart.includes("*") && config.extensions && "bazaar" in config.extensions) {
console.warn(
`[x402] Route "${pattern}": Wildcard (*) patterns with bazaar discovery extensions will auto-generate parameter names (var1, var2, ...). Consider using named parameters instead (e.g. /weather/:city) for better discovery metadata.`
);
}
const paymentOptions = this.normalizePaymentOptions(config);
for (const option of paymentOptions) {
if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) {
errors.push({
routePattern: pattern,
scheme: option.scheme,
network: option.network,
reason: "missing_scheme",
message: `Route "${pattern}": No scheme implementation registered for "${option.scheme}" on network "${option.network}"`
});
continue;
}
const supportedKind = this.ResourceServer.getSupportedKind(
x402Version,
option.network,
option.scheme
);
if (!supportedKind) {
errors.push({
routePattern: pattern,
scheme: option.scheme,
network: option.network,
reason: "missing_facilitator",
message: `Route "${pattern}": Facilitator does not support scheme "${option.scheme}" on network "${option.network}"`
});
}
}
}
return errors;
}
/**
* Get route configuration for a request
*
* @param path - Request path
* @param method - HTTP method
* @returns Route configuration and pattern, or undefined if no match
*/
getRouteConfig(path, method) {
const normalizedPath = this.normalizePath(path);
const upperMethod = method.toUpperCase();
const matchingRoute = this.compiledRoutes.find(
(route) => route.regex.test(normalizedPath) && (route.verb === "*" || route.verb === upperMethod)
);
if (!matchingRoute) return void 0;
return { config: matchingRoute.config, pattern: matchingRoute.pattern };
}
/**
* Extract payment from HTTP headers (handles v1 and v2)
*
* @param adapter - HTTP adapter
* @returns Decoded payment payload or null
*/
extractPayment(adapter) {
const header = adapter.getHeader("payment-signature") || adapter.getHeader("PAYMENT-SIGNATURE");
if (header) {
try {
return decodePaymentSignatureHeader(header);
} catch (error) {
console.warn("Failed to decode PAYMENT-SIGNATURE header:", error);
}
}
return null;
}
/**
* Check if request is from a web browser
*
* @param adapter - HTTP adapter
* @returns True if request appears to be from a browser
*/
isWebBrowser(adapter) {
const accept = adapter.getAcceptHeader();
const userAgent = adapter.getUserAgent();
return accept.includes("text/html") && userAgent.includes("Mozilla");
}
/**
* Create HTTP response instructions from payment required
*
* @param paymentRequired - Payment requirements
* @param isWebBrowser - Whether request is from browser
* @param paywallConfig - Paywall configuration
* @param customHtml - Custom HTML template
* @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests
* @returns Response instructions
*/
createHTTPResponse(paymentRequired, isWebBrowser, paywallConfig, customHtml, unpaidResponse) {
const status = paymentRequired.error === "permit2_allowance_required" ? 412 : 402;
const response = this.createHTTPPaymentRequiredResponse(paymentRequired);
if (isWebBrowser) {
const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml);
return {
status,
headers: {
"Content-Type": "text/html",
...response.headers
},
body: html,
isHtml: true
};
}
const contentType = unpaidResponse ? unpaidResponse.contentType : "application/json";
const body = unpaidResponse ? unpaidResponse.body : {};
return {
status,
headers: {
"Content-Type": contentType,
...response.headers
},
body
};
}
/**
* Create HTTP payment required response (v1 puts in body, v2 puts in header)
*
* @param paymentRequired - Payment required object
* @returns Headers and body for the HTTP response
*/
createHTTPPaymentRequiredResponse(paymentRequired) {
return {
headers: {
"PAYMENT-REQUIRED": encodePaymentRequiredHeader(paymentRequired),
"Cache-Control": PAYMENT_REQUIRED_CACHE_CONTROL
}
};
}
/**
* Create settlement response headers
*
* @param settleResponse - Settlement response
* @returns Headers to add to response
*/
createSettlementHeaders(settleResponse) {
const encoded = encodePaymentResponseHeader(settleResponse);
return { "PAYMENT-RESPONSE": encoded };
}
/**
* Parse route pattern into verb and regex
*
* @param pattern - Route pattern like "GET /api/*", "/api/[id]", or "/api/:id"
* @returns Parsed pattern with verb and regex
*/
parseRoutePattern(pattern) {
const [verb, path] = pattern.includes(" ") ? pattern.split(/\s+/) : ["*", pattern];
const regex = new RegExp(
`^${path.replace(/\\/g, "\\\\").replace(/[$()+.?^{|}]/g, "\\$&").replace(/\*/g, ".*?").replace(/\[([^\]]+)\]/g, "[^/]+").replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, "[^/]+").replace(/\//g, "\\/")}$`,
// "s" (dotAll): without it, "." can't match LF/CR/U+2028/U+2029, so a wildcard segment containing one fails to match.
"is"
);
return { verb: verb.toUpperCase(), regex, path };
}
/**
* Normalize path for matching
*
* @param path - Raw path from request
* @returns Normalized path
*/
normalizePath(path) {
const pathWithoutQuery = path.split(/[?#]/)[0];
const parts = pathWithoutQuery.split(/(%2[fF]|%5[cC])/);
const decoded = parts.map((part, i) => {
if (i % 2 === 1) return part;
try {
return decodeURIComponent(part);
} catch {
return part;
}
}).join("");
return decoded.replace(/\\/g, "/").replace(/\/+/g, "/").replace(/(.+?)\/+$/, "$1");
}
/**
* Generate paywall HTML for browser requests
*
* @param paymentRequired - Payment required response
* @param paywallConfig - Optional paywall configuration
* @param customHtml - Optional custom HTML template
* @returns HTML string
*/
generatePaywallHTML(paymentRequired, paywallConfig, customHtml) {
if (customHtml) {
return customHtml;
}
if (this.paywallProvider) {
return this.paywallProvider.generateHtml(paymentRequired, paywallConfig);
}
try {
const paywall = __require("@x402/paywall");
const displayAmount = this.getDisplayAmount(paymentRequired);
const resource = paymentRequired.resource;
return paywall.getPaywallHtml({
amount: displayAmount,
paymentRequired,
currentUrl: resource?.url || paywallConfig?.currentUrl || "",
testnet: paywallConfig?.testnet ?? true,
appName: paywallConfig?.appName,
appLogo: paywallConfig?.appLogo,
sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint
});
} catch {
}
return FALLBACK_PAYWALL_HTML;
}
/**
* Extract display amount from payment requirements.
* Uses the registered scheme's decimal precision for the asset, falling back to 6.
*
* @param paymentRequired - The payment required object
* @returns The display amount in decimal format
*/
getDisplayAmount(paymentRequired) {
const accepts = paymentRequired.accepts;
if (accepts && accepts.length > 0) {
const firstReq = accepts[0];
if ("amount" in firstReq) {
const decimals = this.ResourceServer.getAssetDecimalsForRequirements(firstReq);
return parseFloat(firstReq.amount) / 10 ** decimals;
}
}
return 0;
}
};
// src/http/httpFacilitatorClient.ts
var DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator";
var DEFAULT_TIMEOUT_MS = 3e4;
var MAX_TIMEOUT_MS = 2147483647;
var GET_SUPPORTED_RETRIES = 3;
var GET_SUPPORTED_RETRY_DELAY_MS = 1e3;
var MAX_RETRY_DELAY_MS = 3e4;
function computeRetryDelay(retryAfter, attempt) {
let delay = null;
if (retryAfter !== null) {
const trimmedRetryAfter = retryAfter.trim();
if (/^\d+$/.test(trimmedRetryAfter)) {
delay = Number(trimmedRetryAfter) * 1e3;
} else {
const retryDate = Date.parse(retryAfter);
if (!isNaN(retryDate)) {
delay = retryDate - Date.now();
}
}
}
if (delay === null || delay <= 0) {
delay = GET_SUPPORTED_RETRY_DELAY_MS * Math.pow(2, attempt);
}
return Math.min(delay, MAX_RETRY_DELAY_MS);
}
var verifyResponseSchema = z.object({
isValid: z.boolean(),
invalidReason: z.string().nullish().transform((v) => v ?? void 0),
invalidMessage: z.string().nullish().transform((v) => v ?? void 0),
payer: z.string().nullish().transform((v) => v ?? void 0),
extensions: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0),
extra: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0)
});
var settleResponseSchema = z.object({
success: z.boolean(),
errorReason: z.string().nullish().transform((v) => v ?? void 0),
errorMessage: z.string().nullish().transform((v) => v ?? void 0),
payer: z.string().nullish().transform((v) => v ?? void 0),
transaction: z.string(),
network: z.custom((value) => typeof value === "string"),
amount: z.string().nullish().transform((v) => v ?? void 0),
extensions: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0),
extra: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0)
});
var supportedKindSchema = z.object({
x402Version: z.number(),
scheme: z.string(),
network: z.custom(
(value) => typeof value === "string"
),
extra: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0)
});
var supportedResponseSchema = z.object({
kinds: z.array(supportedKindSchema),
extensions: z.array(z.string()).default([]),
signers: z.record(z.string(), z.array(z.string())).default({})
});
function responseExcerpt(text, limit = 200) {
const compact = text.trim().replace(/\s+/g, " ");
if (!compact) {
return "<empty response>";
}
if (compact.length <= limit) {
return compact;
}
return `${compact.slice(0, limit - 3)}...`;
}
function isAbortOrTimeoutError(error) {
let current = error;
for (let depth = 0; depth < 10 && current !== null && typeof current === "object"; depth++) {
const name = current.name;
if (name === "TimeoutError" || name === "AbortError") {
return true;
}
current = current.cause;
}
return false;
}
var EXTENSION_RESPONSE_LOG_FIELD_ALLOWLIST = ["status", "rejectedReason", "reason", "code"];
function logExtensionResponsesHeader(response) {
const header = response.headers.get("EXTENSION-RESPONSES");
if (!header) return;
try {
const decoded = JSON.parse(safeBase64Decode(header));
if (!decoded || typeof decoded !== "object" || Array.isArray(decoded)) return;
const sanitized = {};
for (const [extensionKey, payload] of Object.entries(decoded)) {
const source = payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
const filtered = {};
for (const key of EXTENSION_RESPONSE_LOG_FIELD_ALLOWLIST) {
if (source[key] !== void 0) {
filtered[key] = source[key];
}
}
sanitized[extensionKey] = filtered;
}
console.log(`[x402] extension responses: ${JSON.stringify(sanitized)}`);
} catch {
}
}
async function parseSuccessResponse(response, schema, operation) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new FacilitatorResponseError(
`Facilitator ${operation} returned invalid JSON: ${responseExcerpt(text)}`
);
}
const parsed = schema.safeParse(data);
if (!parsed.success) {
throw new FacilitatorResponseError(
`Facilitator ${operation} returned invalid data: ${responseExcerpt(text)}`
);
}
return parsed.data;
}
var HTTPFacilitatorClient = class {
/**
* Creates a new HTTPFacilitatorClient instance.
*
* @param config - Configuration options for the facilitator client
*/
constructor(config) {
this.url = (config?.url || DEFAULT_FACILITATOR_URL).replace(/\/+$/, "");
const timeoutMs = config?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > MAX_TIMEOUT_MS) {
throw new RangeError(
`timeoutMs must be a positive integer number of milliseconds no greater than ${MAX_TIMEOUT_MS}, got ${timeoutMs}`
);
}
this.timeoutMs = timeoutMs;
this._createAuthHeaders = config?.createAuthHeaders;
}
/**
* Verify a payment with the facilitator
*
* @param paymentPayload - The payment to verify
* @param paymentRequirements - The requirements to verify against
* @returns Verification response
*/
async verify(paymentPayload, paymentRequirements) {
let headers = {
"Content-Type": "application/json"
};
if (this._createAuthHeaders) {
const authHeaders = await this.createAuthHeaders("verify");
headers = { ...headers, ...authHeaders.headers };
}
return this.withRequestTimeout("verify", async (signal) => {
const response = await fetch(`${this.url}/verify`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
}),
signal
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(
`Facilitator verify failed (${response.status}): ${responseExcerpt(text)}`
);
}
if (typeof data === "object" && data !== null && "isValid" in data) {
throw new VerifyError(response.status, data);
}
throw new Error(
`Facilitator verify failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const verifyResult = await parseSuccessResponse(response, verifyResponseSchema, "verify");
logExtensionResponsesHeader(response);
return verifyResult;
});
}
/**
* Settle a payment with the facilitator
*
* @param paymentPayload - The payment to settle
* @param paymentRequirements - The requirements for settlement
* @returns Settlement response
*/
async settle(paymentPayload, paymentRequirements) {
let headers = {
"Content-Type": "application/json"
};
if (this._createAuthHeaders) {
const authHeaders = await this.createAuthHeaders("settle");
headers = { ...headers, ...authHeaders.headers };
}
return this.withRequestTimeout("settle", async (signal) => {
const response = await fetch(`${this.url}/settle`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
}),
signal
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(
`Facilitator settle failed (${response.status}): ${responseExcerpt(text)}`
);
}
if (typeof data === "object" && data !== null && "success" in data) {
throw new SettleError(response.status, data);
}
throw new Error(
`Facilitator settle failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const settleResult = await parseSuccessResponse(response, settleResponseSchema, "settle");
logExtensionResponsesHeader(response);
return settleResult;
});
}
/**
* Get supported payment kinds and extensions from the facilitator.
* Retries with exponential backoff on 429 rate limit errors.
*
* @returns Supported payment kinds and extensions
*/
async getSupported() {
let headers = {
"Content-Type": "application/json"
};
if (this._createAuthHeaders) {
const authHeaders = await this.createAuthHeaders("supported");
headers = { ...headers, ...authHeaders.headers };
}
let lastError = null;
for (let attempt = 0; attempt < GET_SUPPORTED_RETRIES; attempt++) {
const outcome = await this.withRequestTimeout("supported", async (signal) => {
const response = await fetch(`${this.url}/supported`, {
method: "GET",
headers,
redirect: "follow",
signal
});
if (response.ok) {
return {
kind: "success",
value: await parseSuccessResponse(response, supportedResponseSchema, "supported")
};
}
const errorText = await response.text().catch((cause) => {
if (isAbortOrTimeoutError(cause)) {
throw cause;
}
return response.statusText;
});
return {
kind: "http-error",
status: response.status,
retryAfter: response.headers.get("Retry-After"),
error: new Error(
`Facilitator getSupported failed (${response.status}): ${responseExcerpt(errorText)}`
)
};
});
if (outcome.kind === "success") {
return outcome.value;
}
lastError = outcome.error;
if (outcome.status === 429 && attempt < GET_SUPPORTED_RETRIES - 1) {
const delay = computeRetryDelay(outcome.retryAfter, attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
throw lastError;
}
throw lastError ?? new Error("Facilitator getSupported failed after retries");
}
/**
* Creates authentication headers for a specific path.
*
* @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported")
* @returns An object containing the authentication headers for the specified path
*/
async createAuthHeaders(path) {
if (!this._createAuthHeaders) {
return { headers: {} };
}
const authHeaders = await this._createAuthHeaders();
const isHeaderObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
const hasPathKey = ["verify", "settle", "supported", "bazaar"].some(
(key) => isHeaderObject(authHeaders[key])
);
const looksFlat = !hasPathKey && Object.values(authHeaders).some((value) => !isHeaderObject(value));
if (looksFlat) {
throw new Error(
'createAuthHeaders must return an object keyed by facilitator path, e.g. { verify: { Authorization: "..." }, settle: { ... }, supported: { ... } }, but received a flat headers object. See https://github.com/x402-foundation/x402/issues/2762'
);
}
const headersForPath = authHeaders[path];
return {
headers: isHeaderObject(headersForPath) ? headersForPath : {}
};
}
/**
* Runs a single facilitator HTTP attempt under this client's request deadline.
* The provided signal must be passed to `fetch` so the deadline also covers
* response-body consumption.
*
* @param operation - The facilitator operation name ("verify", "settle", "supported")
* @param run - The attempt to execute with the deadline's AbortSignal
* @returns The attempt's result
* @throws FacilitatorTimeoutError when the deadline elapses before completion
*/
async withRequestTimeout(operation, run) {
const signal = AbortSignal.timeout(this.timeoutMs);
try {
return await run(signal);
} catch (error) {
if (signal.aborted && isAbortOrTimeoutError(error)) {
throw new FacilitatorTimeoutError(operation, this.timeoutMs);
}
throw error;
}
}
/**
* Helper to convert objects to JSON-safe format.
* Handles BigInt and other non-JSON types.
*
* @param obj - The object to convert
* @returns The JSON-safe representation of the object
*/
toJsonSafe(obj) {
return JSON.parse(
JSON.stringify(obj, (_, value) => typeof value === "bigint" ? value.toString() : value)
);
}
};
// src/http/x402HTTPClient.ts
var x402HTTPClient = class {
/**
* Creates a new x402HTTPClient instance.
*
* @param client - The underlying x402Client for payment logic
*/
constructor(client) {
this.client = client;
this.paymentRequiredHooks = [];
}
/**
* Register a hook to handle 402 responses before payment.
* Hooks run in order; first to return headers wins.
*
* @param hook - The hook function to register
* @returns This instance for chaining
*/
onPaymentRequired(hook) {
this.paymentRequiredHooks.push(hook);
return this;
}
/**
* Run hooks and return headers if any hook provides them.
*
* @param paymentRequired - The payment required response from the server
* @returns Headers to use for retry, or null to proceed to payment
*/
async handlePaymentRequired(paymentRequired) {
for (const hook of this.getPaymentRequiredHooks(paymentRequired)) {
const result = await hook({ paymentRequired });
if (result?.headers) {
return result.headers;
}
}
return null;
}
/**
* Encodes a payment payload into appropriate HTTP headers based on version.
*
* @param paymentPayload - The payment payload to encode
* @returns HTTP headers containing the encoded payment signature
*/
encodePaymentSignatureHeader(paymentPayload) {
switch (paymentPayload.x402Version) {
case 2:
return {
"PAYMENT-SIGNATURE": encodePaymentSignatureHeader(paymentPayload)
};
case 1:
return {
"X-PAYMENT": encodePaymentSignatureHeader(paymentPayload)
};
default:
throw new Error(
`Unsupported x402 version: ${paymentPayload.x402Version}`
);
}
}
/**
* Extracts payment required information from HTTP response.
*
* @param getHeader - Function to retrieve header value by name (case-insensitive)
* @param body - Optional response body for v1 compatibility
* @returns The payment required object
*/
getPaymentRequiredResponse(getHeader, body) {
const paymentRequired = getHeader("PAYMENT-REQUIRED");
if (paymentRequired) {
return decodePaymentRequiredHeader(paymentRequired);
}
if (body && body instanceof Object && "x402Version" in body && body.x402Version === 1) {
return body;
}
throw new Error("Invalid payment required response");
}
/**
* Extracts payment settlement response from HTTP headers.
*
* @param getHeader - Function to retrieve header value by name (case-insensitive)
* @returns The settlement response object
*/
getPaymentSettleResponse(getHeader) {
const paymentResponse = getHeader("PAYMENT-RESPONSE");
if (paymentResponse) {
return decodePaymentResponseHeader(paymentResponse);
}
const xPaymentResponse = getHeader("X-PAYMENT-RESPONSE");
if (xPaymentResponse) {
return decodePaymentResponseHeader(xPaymentResponse);
}
throw new Error("Payment response header not found");
}
/**
* Creates a payment payload for the given payment requirements.
* Delegates to the underlying x402Client.
*
* @param paymentRequired - The payment required response from the server
* @returns Promise resolving to the payment payload
*/
async createPaymentPayload(paymentRequired) {
return this.client.createPaymentPayload(paymentRequired);
}
/**
* Parses response headers into protocol types, fires payment response hooks (v2 only),
* and returns whether a hook signaled recovery.
*
* Called by transport wrappers (fetch, axios) after the paid request completes.
*
* @param paymentPayload - The payload that was sent with the request
* @param getHeader - Function to retrieve a response header by name
* @param status - The HTTP status code of the response
* @returns Whether a hook recovered and the parsed settle response (if any)
*/
async processPaymentResult(paymentPayload, getHeader, status) {
let settleResponse;
try {
settleResponse = this.getPaymentSettleResponse(getHeader);
} catch {
}
if (paymentPayload.x402Version === 1) {
return { recovered: false, settleResponse };
}
let paymentRequired;
if (!settleResponse && status === 402) {
try {
paymentRequired = this.getPaymentRequiredResponse(getHeader);
} catch {
}
}
const requirements = paymentPayload.accepted;
if (!requirements) {
throw new Error("Invalid x402 v2 payment payload: missing `accepted`");
}
const ctx = {
paymentPayload,
requirements,
...settleResponse ? { settleResponse } : {},
...paymentRequired ? { paymentRequired } : {}
};
const result = await this.client.handlePaymentResponse(ctx);
return { recovered: result?.recovered === true, settleResponse };
}
/**
* Parses HTTP status, headers, and body into an `HTTPResourceResponse`.
*
* Decodes the x402 payment header into `header`: the `PAYMENT-RESPONSE`
* settlement if present, otherwise the `PAYMENT-REQUIRED` declaration on
* 402 responses (whose `error` field carries the server's failure reason).
*
* @param args - Normalized response inputs from any HTTP transport
* @param args.status - HTTP response status code
* @param args.getHeader - Callback to read response headers by name
* @param args.body - Response body payload
* @returns The parsed status, body, and decoded payment header
*/
parsePaymentResult(args) {
const { status, getHeader, body } = args;
let header;
try {
header = this.getPaymentSettleResponse(getHeader);
} catch {
if (status === 402) {
try {
header = this.getPaymentRequiredResponse(getHeader, body);
} catch {
}
}
}
let paymentStatus = "none";
if (header && !("success" in header)) {
paymentStatus = "payment_required";
}
if (header && "success" in header) {
paymentStatus = header.success ? "settled" : "settle_failed";
}
return { status, paymentStatus, body, header };
}
/**
* Parses a fetch Response into an `HTTPResourceResponse` for app-level convenience.
*
* @param response - The fetch Response to process
* @returns The parsed status, body, and decoded payment header
*/
async processResponse(response) {
const getHeader = (name) => response.headers.get(name);
const contentType = response.headers.get("content-type") ?? "";
const body = contentType.includes("application/json") ? await response.json() : await response.text();
return this.parsePaymentResult({ status: response.status, getHeader, body });
}
/**
* Manual HTTP hooks run before extension hooks scoped to the 402 response.
*
* @param paymentRequired - The payment required response from the server
* @returns Hooks in invocation order
*/
getPaymentRequiredHooks(paymentRequired) {
const hooks = [...this.paymentRequiredHooks];
const declaredExtensions = paymentRequired.extensions;
if (!declaredExtensions) return hooks;
for (const extension of this.client.getExtensions()) {
const httpExtension = extension;
const hook = httpExtension.transportHooks?.http?.onPaymentRequired;
if (!hook || !(extension.key in declaredExtensions)) continue;
hooks.push((context) => hook(declaredExtensions[extension.key], context));
}
return hooks;
}
};
// src/http/index.ts
function encodePaymentSignatureHeader(paymentPayload) {
return safeBase64Encode(JSON.stringify(paymentPayload));
}
function decodePaymentSignatureHeader(paymentSignatureHeader) {
if (!Base64EncodedRegex.test(paymentSignatureHeader)) {
throw new Error("Invalid payment signature header");
}
return JSON.parse(safeBase64Decode(paymentSignatureHeader));
}
function encodePaymentRequiredHeader(paymentRequired) {
return safeBase64Encode(JSON.stringify(paymentRequired));
}
function decodePaymentRequiredHeader(paymentRequiredHeader) {
if (!Base64EncodedRegex.test(paymentRequiredHeader)) {
throw new Error("Invalid payment required header");
}
return JSON.parse(safeBase64Decode(paymentRequiredHeader));
}
function encodePaymentResponseHeader(paymentResponse) {
return safeBase64Encode(JSON.stringify(paymentResponse));
}
function decodePaymentResponseHeader(paymentResponseHeader) {
if (!Base64EncodedRegex.test(paymentResponseHeader)) {
throw new Error("Invalid payment response header");
}
return JSON.parse(safeBase64Decode(paymentResponseHeader));
}
export {
SETTLEMENT_OVERRIDES_HEADER,
PAYMENT_REQUIRED_CACHE_CONTROL,
withPrivateCacheControl,
checkIfBazaarNeeded,
RouteConfigurationError,
x402HTTPResourceServer,
HTTPFacilitatorClient,
encodePaymentSignatureHeader,
decodePaymentSignatureHeader,
encodePaymentRequiredHeader,
decodePaymentRequiredHeader,
encodePaymentResponseHeader,
decodePaymentResponseHeader,
x402HTTPClient
};
//# sourceMappingURL=chunk-VKJGPEW2.mjs.map

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

+2
-2

@@ -1,3 +0,3 @@

import { c as PaymentRequired, ae as x402Client, P as PaymentPayload, S as SettleResponse } from '../x402Client-0g4vl2En.js';
export { aj as AfterPaymentCreationHook, ai as BeforePaymentCreationHook, aq as ClientExtension, ao as ClientExtensionHooks, ap as ClientTransportExtensionHooks, ak as OnPaymentCreationFailureHook, am as OnPaymentResponseHook, ag as PaymentCreatedContext, af as PaymentCreationContext, ah as PaymentCreationFailureContext, ar as PaymentPolicy, al as PaymentResponseContext, as as SchemeRegistration, an as SelectPaymentRequirements, at as x402ClientConfig } from '../x402Client-0g4vl2En.js';
import { c as PaymentRequired, ah as x402Client, P as PaymentPayload, S as SettleResponse } from '../x402Client-CzZlbbXy.js';
export { am as AfterPaymentCreationHook, al as BeforePaymentCreationHook, at as ClientExtension, ar as ClientExtensionHooks, as as ClientTransportExtensionHooks, an as OnPaymentCreationFailureHook, ap as OnPaymentResponseHook, aj as PaymentCreatedContext, ai as PaymentCreationContext, ak as PaymentCreationFailureContext, au as PaymentPolicy, ao as PaymentResponseContext, av as SchemeRegistration, aq as SelectPaymentRequirements, aw as x402ClientConfig } from '../x402Client-CzZlbbXy.js';

@@ -4,0 +4,0 @@ /**

@@ -76,2 +76,38 @@ "use strict";

}
function deepEqual(obj1, obj2) {
const normalize = (obj) => {
if (obj === null || obj === void 0) return JSON.stringify(obj);
if (typeof obj !== "object") return JSON.stringify(obj);
if (Array.isArray(obj)) {
return JSON.stringify(
obj.map(
(item) => typeof item === "object" && item !== null ? JSON.parse(normalize(item)) : item
)
);
}
const sorted = {};
Object.keys(obj).sort().forEach((key) => {
const value = obj[key];
sorted[key] = typeof value === "object" && value !== null ? JSON.parse(normalize(value)) : value;
});
return JSON.stringify(sorted);
};
try {
return normalize(obj1) === normalize(obj2);
} catch {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
}
function toComparableArray(value) {
if (Array.isArray(value)) {
return value;
}
if (value === null || value === void 0 || typeof value === "object") {
return void 0;
}
return [value];
}
var ADDITIVE_ARRAY_INFO_FIELDS = {
"builder-code": /* @__PURE__ */ new Set(["s"])
};

@@ -166,5 +202,5 @@ // src/client/x402Client.ts

* Extensions are invoked after the scheme creates the base payload and the
* payload is wrapped with extensions/resource/accepted data. If the extension's
* key is present in `paymentRequired.extensions`, the extension's
* `enrichPaymentPayload` hook is called to modify the payload.
* payload is wrapped with extensions/resource/accepted data. Every registered
* extension's `enrichPaymentPayload` hook is called to modify the payload.
* Server-declared fields are preserved via merge after enrichment.
*

@@ -342,2 +378,7 @@ * @param extension - The client extension to register

* Client extension data may add fields, but server-declared fields remain intact.
* For fields listed in `ADDITIVE_ARRAY_INFO_FIELDS` (e.g. builder-code `s`), a
* conflicting array is concatenated with client entries first (so a downstream
* length cap trims server entries rather than the client's) and duplicates
* removed; a scalar on either side is treated as a single-element array. Every
* other conflicting array keeps the server's value, same as any other scalar.
*

@@ -360,2 +401,3 @@ * @param serverExtensions - Extensions declared by the server in the 402 response

const clientRecord = clientValue;
const additiveFields = ADDITIVE_ARRAY_INFO_FIELDS[key];
const extensionValue = { ...serverRecord };

@@ -375,2 +417,10 @@ const pending = [{ target: extensionValue, source: clientRecord }];

}
if (additiveFields?.has(fieldKey) && (Array.isArray(serverFieldValue) || Array.isArray(clientFieldValue))) {
const serverArray = toComparableArray(serverFieldValue);
const clientArray = toComparableArray(clientFieldValue);
if (serverArray && clientArray) {
item.target[fieldKey] = mergeArraysUnique(clientArray, serverArray);
continue;
}
}
if (!Object.prototype.hasOwnProperty.call(item.target, fieldKey)) {

@@ -387,4 +437,4 @@ item.target[fieldKey] = clientFieldValue;

* Enriches a payment payload by calling registered extension hooks.
* For each extension key present in the PaymentRequired response,
* invokes the corresponding extension's enrichPaymentPayload callback.
* Invokes enrichPaymentPayload for every registered extension, then merges
* server-declared extension fields back into the result.
*

@@ -396,8 +446,8 @@ * @param paymentPayload - The payment payload to enrich with extension data

async enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired) {
if (!paymentRequired.extensions || this.registeredExtensions.size === 0) {
if (this.registeredExtensions.size === 0) {
return paymentPayload;
}
let enriched = paymentPayload;
for (const [key, extension] of this.registeredExtensions) {
if (key in paymentRequired.extensions && extension.enrichPaymentPayload) {
for (const [, extension] of this.registeredExtensions) {
if (extension.enrichPaymentPayload) {
enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);

@@ -569,2 +619,11 @@ }

};
function mergeArraysUnique(client, server) {
const merged = [];
for (const item of [...client, ...server]) {
if (!merged.some((existing) => deepEqual(existing, item))) {
merged.push(item);
}
}
return merged;
}

@@ -571,0 +630,0 @@ // src/schemas/index.ts

@@ -1,2 +0,2 @@

import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../x402Client-0g4vl2En.js';
import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../x402Client-CzZlbbXy.js';

@@ -3,0 +3,0 @@ /**

@@ -1,1 +0,1 @@

{"version":3,"sources":["../../../src/facilitator/index.ts","../../../src/index.ts","../../../src/utils/index.ts","../../../src/facilitator/x402Facilitator.ts"],"sourcesContent":["export * from \"./x402Facilitator\";\n","export const x402Version = 2;\n","import { Network } from \"../types\";\n\n/**\n * Converts a JavaScript number to a plain decimal string, expanding scientific notation\n * via string manipulation rather than parseFloat round-tripping.\n *\n * e.g. 1e-7 → \"0.0000001\", 4.02 → \"4.02\"\n *\n * @param n - The number to convert\n * @returns A plain decimal string representation with no scientific notation\n */\nexport function numberToDecimalString(n: number): string {\n const str = n.toString();\n if (!/[eE]/.test(str)) return str;\n\n const [significand, exponentStr] = str.split(/[eE]/);\n const exp = parseInt(exponentStr, 10);\n const negative = significand.startsWith(\"-\");\n const abs = negative ? significand.slice(1) : significand;\n const [intDigits, fracDigits = \"\"] = abs.split(\".\");\n const allDigits = intDigits + fracDigits;\n const decimalPos = intDigits.length + exp;\n\n let result: string;\n if (decimalPos <= 0) {\n result = \"0.\" + \"0\".repeat(-decimalPos) + allDigits;\n } else if (decimalPos >= allDigits.length) {\n result = allDigits + \"0\".repeat(decimalPos - allDigits.length);\n } else {\n result = allDigits.slice(0, decimalPos) + \".\" + allDigits.slice(decimalPos);\n }\n return (negative ? \"-\" : \"\") + result;\n}\n\n/**\n * Parses a money string into a finite, non-negative decimal number.\n * Accepts plain decimal strings with an optional leading dollar sign.\n *\n * @param money - The money string to parse\n * @returns Decimal number\n */\nexport function parseMoneyString(money: string): number {\n const cleaned = money.replace(/^\\$/, \"\").trim();\n if (!/^-?\\d+(?:\\.\\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n const amount = Number(cleaned);\n if (!Number.isFinite(amount) || amount < 0) {\n throw new Error(`Invalid money format: ${money}`);\n }\n return amount;\n}\n\n/**\n * Convert a decimal amount to token smallest units.\n * Accepts only plain decimal strings — scientific notation is not allowed.\n * Throws if the amount is non-zero but too small to represent with the given decimal precision.\n *\n * @param decimalAmount - The decimal amount as a plain string (e.g., \"0.10\")\n * @param decimals - The number of decimals for the token (e.g., 6 for USDC)\n * @returns The amount in smallest units as a string\n */\nexport function convertToTokenAmount(decimalAmount: string, decimals: number): string {\n if (/[eE]/.test(decimalAmount)) {\n throw new Error(\n `Invalid amount: ${decimalAmount} — use decimal notation, not scientific notation`,\n );\n }\n if (!/^-?\\d+\\.?\\d*$/.test(decimalAmount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const [intPart, decPart = \"\"] = decimalAmount.split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n if (tokenAmount === \"0\" && /[1-9]/.test(decimalAmount)) {\n throw new Error(\n `Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`,\n );\n }\n return tokenAmount;\n}\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\nconst networkPatternToRegExp = (pattern: Network): RegExp => {\n const source = escapeRegExp(pattern).replace(/\\\\\\*/g, \".*\");\n return new RegExp(`^${source}$`);\n};\n\nexport const networkMatchesPattern = (pattern: Network, network: Network): boolean => {\n return networkPatternToRegExp(pattern).test(network);\n};\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n if (networkMatchesPattern(registeredNetworkPattern as Network, network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n","import { x402Version } from \"..\";\nimport { SettleResponse, VerifyResponse } from \"../types/facilitator\";\nimport { FacilitatorExtension } from \"../types/extensions\";\nimport { SchemeNetworkFacilitator, FacilitatorContext } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network } from \"../types\";\nimport { networkMatchesPattern, type SchemeData } from \"../utils\";\n\n/**\n * Facilitator Hook Context Interfaces\n */\n\nexport interface FacilitatorVerifyContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n}\n\nexport interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {\n result: VerifyResponse;\n}\n\nexport interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {\n error: Error;\n}\n\nexport interface FacilitatorSettleContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n}\n\nexport interface FacilitatorSettleResultContext extends FacilitatorSettleContext {\n result: SettleResponse;\n}\n\nexport interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {\n error: Error;\n}\n\n/**\n * Facilitator Hook Type Definitions\n */\n\nexport type FacilitatorBeforeVerifyHook = (\n context: FacilitatorVerifyContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;\n\nexport type FacilitatorOnVerifyFailureHook = (\n context: FacilitatorVerifyFailureContext,\n) => Promise<void | { recovered: true; result: VerifyResponse }>;\n\nexport type FacilitatorBeforeSettleHook = (\n context: FacilitatorSettleContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;\n\nexport type FacilitatorOnSettleFailureHook = (\n context: FacilitatorSettleFailureContext,\n) => Promise<void | { recovered: true; result: SettleResponse }>;\n\n/**\n * Facilitator client for the x402 payment protocol.\n * Manages payment scheme registration, verification, and settlement.\n */\nexport class x402Facilitator {\n private readonly registeredFacilitatorSchemes: Map<\n number,\n SchemeData<SchemeNetworkFacilitator>[] // Array to support multiple facilitators per version\n > = new Map();\n private readonly extensions: Map<string, FacilitatorExtension> = new Map();\n\n private beforeVerifyHooks: FacilitatorBeforeVerifyHook[] = [];\n private afterVerifyHooks: FacilitatorAfterVerifyHook[] = [];\n private onVerifyFailureHooks: FacilitatorOnVerifyFailureHook[] = [];\n private beforeSettleHooks: FacilitatorBeforeSettleHook[] = [];\n private afterSettleHooks: FacilitatorAfterSettleHook[] = [];\n private onSettleFailureHooks: FacilitatorOnSettleFailureHook[] = [];\n\n /**\n * Registers a scheme facilitator for the current x402 version.\n * Networks are stored and used for getSupported() - no need to specify them later.\n *\n * @param networks - Single network or array of networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator {\n const networksArray = Array.isArray(networks) ? networks : [networks];\n return this._registerScheme(x402Version, networksArray, facilitator);\n }\n\n /**\n * Registers a scheme facilitator for x402 version 1.\n * Networks are stored and used for getSupported() - no need to specify them later.\n *\n * @param networks - Single network or array of networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n registerV1(\n networks: Network | Network[],\n facilitator: SchemeNetworkFacilitator,\n ): x402Facilitator {\n const networksArray = Array.isArray(networks) ? networks : [networks];\n return this._registerScheme(1, networksArray, facilitator);\n }\n\n /**\n * Registers a protocol extension.\n *\n * @param extension - The extension object to register\n * @returns The x402Facilitator instance for chaining\n */\n registerExtension(extension: FacilitatorExtension): x402Facilitator {\n this.extensions.set(extension.key, extension);\n return this;\n }\n\n /**\n * Gets the list of registered extension keys.\n *\n * @returns Array of extension key strings\n */\n getExtensions(): string[] {\n return Array.from(this.extensions.keys());\n }\n\n /**\n * Gets a registered extension by key.\n *\n * @param key - The extension key to look up\n * @returns The extension object, or undefined if not registered\n */\n getExtension<T extends FacilitatorExtension = FacilitatorExtension>(key: string): T | undefined {\n return this.extensions.get(key) as T | undefined;\n }\n\n /**\n * Register a hook to execute before facilitator payment verification.\n * Can abort verification by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator {\n this.beforeVerifyHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful facilitator payment verification (isValid: true).\n * This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator {\n this.afterVerifyHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when facilitator payment verification fails.\n * Called when: verification returns isValid: false, or an exception is thrown during verification.\n * Can recover from failure by returning { recovered: true, result: VerifyResponse }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator {\n this.onVerifyFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute before facilitator payment settlement.\n * Can abort settlement by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator {\n this.beforeSettleHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful facilitator payment settlement.\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator {\n this.afterSettleHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when facilitator payment settlement fails.\n * Can recover from failure by returning { recovered: true, result: SettleResponse }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator {\n this.onSettleFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Gets supported payment kinds, extensions, and signers.\n * Uses networks registered during register() calls - no parameters needed.\n * Returns flat array format for backward compatibility with V1 clients.\n *\n * @returns Supported response with kinds as array (with version in each element), extensions, and signers\n */\n getSupported(): {\n kinds: Array<{\n x402Version: number;\n scheme: string;\n network: string;\n extra?: Record<string, unknown>;\n }>;\n extensions: string[];\n signers: Record<string, string[]>;\n } {\n const kinds: Array<{\n x402Version: number;\n scheme: string;\n network: string;\n extra?: Record<string, unknown>;\n }> = [];\n const signersByFamily: Record<string, Set<string>> = {};\n\n // Iterate over registered scheme data (array supports multiple facilitators per version)\n for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {\n for (const schemeData of schemeDataArray) {\n const { facilitator, networks } = schemeData;\n const scheme = facilitator.scheme;\n\n // Iterate over stored concrete networks\n for (const network of networks) {\n const extra = facilitator.getExtra(network);\n kinds.push({\n x402Version: version,\n scheme,\n network,\n ...(extra && { extra }),\n });\n\n // Collect signers by CAIP family for this network\n const family = facilitator.caipFamily;\n if (!signersByFamily[family]) {\n signersByFamily[family] = new Set();\n }\n facilitator.getSigners(network).forEach(signer => signersByFamily[family].add(signer));\n }\n }\n }\n\n // Convert signer sets to arrays\n const signers: Record<string, string[]> = {};\n for (const [family, signerSet] of Object.entries(signersByFamily)) {\n signers[family] = Array.from(signerSet);\n }\n\n return {\n kinds,\n extensions: this.getExtensions(),\n signers,\n };\n }\n\n /**\n * Verifies a payment payload against requirements.\n *\n * @param paymentPayload - The payment payload to verify\n * @param paymentRequirements - The payment requirements to verify against\n * @returns Promise resolving to the verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const context: FacilitatorVerifyContext = {\n paymentPayload,\n requirements: paymentRequirements,\n };\n\n // Execute beforeVerify hooks\n for (const hook of this.beforeVerifyHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n return {\n isValid: false,\n invalidReason: result.reason,\n };\n }\n }\n\n try {\n const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);\n if (!schemeDataArray) {\n throw new Error(\n `No facilitator registered for x402 version: ${paymentPayload.x402Version}`,\n );\n }\n\n // Find matching facilitator from array\n let schemeNetworkFacilitator: SchemeNetworkFacilitator | undefined;\n for (const schemeData of schemeDataArray) {\n if (schemeData.facilitator.scheme === paymentRequirements.scheme) {\n // Check if network matches\n if (schemeData.networks.has(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n }\n }\n\n if (!schemeNetworkFacilitator) {\n throw new Error(\n `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`,\n );\n }\n\n const facilitatorContext = this.buildFacilitatorContext();\n const verifyResult = await schemeNetworkFacilitator.verify(\n paymentPayload,\n paymentRequirements,\n facilitatorContext,\n );\n\n // Check if verification failed (isValid: false)\n if (!verifyResult.isValid) {\n const failureContext: FacilitatorVerifyFailureContext = {\n ...context,\n error: new Error(verifyResult.invalidReason || \"Verification failed\"),\n };\n\n // Execute onVerifyFailure hooks\n for (const hook of this.onVerifyFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n // If recovered, execute afterVerify hooks with recovered result\n const recoveredContext: FacilitatorVerifyResultContext = {\n ...context,\n result: result.result,\n };\n for (const hook of this.afterVerifyHooks) {\n await hook(recoveredContext);\n }\n return result.result;\n }\n }\n\n return verifyResult;\n }\n\n // Execute afterVerify hooks only for successful verification\n const resultContext: FacilitatorVerifyResultContext = {\n ...context,\n result: verifyResult,\n };\n\n for (const hook of this.afterVerifyHooks) {\n await hook(resultContext);\n }\n\n return verifyResult;\n } catch (error) {\n const failureContext: FacilitatorVerifyFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onVerifyFailure hooks\n for (const hook of this.onVerifyFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.result;\n }\n }\n\n throw error;\n }\n }\n\n /**\n * Settles a payment based on the payload and requirements.\n *\n * @param paymentPayload - The payment payload to settle\n * @param paymentRequirements - The payment requirements for settlement\n * @returns Promise resolving to the settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const context: FacilitatorSettleContext = {\n paymentPayload,\n requirements: paymentRequirements,\n };\n\n // Execute beforeSettle hooks\n for (const hook of this.beforeSettleHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Settlement aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);\n if (!schemeDataArray) {\n throw new Error(\n `No facilitator registered for x402 version: ${paymentPayload.x402Version}`,\n );\n }\n\n // Find matching facilitator from array\n let schemeNetworkFacilitator: SchemeNetworkFacilitator | undefined;\n for (const schemeData of schemeDataArray) {\n if (schemeData.facilitator.scheme === paymentRequirements.scheme) {\n // Check if network matches\n if (schemeData.networks.has(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n }\n }\n\n if (!schemeNetworkFacilitator) {\n throw new Error(\n `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`,\n );\n }\n\n const facilitatorContext = this.buildFacilitatorContext();\n const settleResult = await schemeNetworkFacilitator.settle(\n paymentPayload,\n paymentRequirements,\n facilitatorContext,\n );\n\n // Execute afterSettle hooks\n const resultContext: FacilitatorSettleResultContext = {\n ...context,\n result: settleResult,\n };\n\n for (const hook of this.afterSettleHooks) {\n await hook(resultContext);\n }\n\n return settleResult;\n } catch (error) {\n const failureContext: FacilitatorSettleFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onSettleFailure hooks\n for (const hook of this.onSettleFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.result;\n }\n }\n\n throw error;\n }\n }\n\n /**\n * Builds a FacilitatorContext from the registered extensions map.\n * Passed to mechanism verify/settle so they can access extension capabilities.\n *\n * @returns A FacilitatorContext backed by this facilitator's registered extensions\n */\n private buildFacilitatorContext(): FacilitatorContext {\n const extensionsMap = this.extensions;\n return {\n getExtension<T extends FacilitatorExtension = FacilitatorExtension>(\n key: string,\n ): T | undefined {\n return extensionsMap.get(key) as T | undefined;\n },\n };\n }\n\n /**\n * Internal method to register a scheme facilitator.\n *\n * @param x402Version - The x402 protocol version\n * @param networks - Array of concrete networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n private _registerScheme(\n x402Version: number,\n networks: Network[],\n facilitator: SchemeNetworkFacilitator,\n ): x402Facilitator {\n if (!this.registeredFacilitatorSchemes.has(x402Version)) {\n this.registeredFacilitatorSchemes.set(x402Version, []);\n }\n const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version)!;\n\n // Add new scheme data (supports multiple facilitators with same scheme name)\n schemeDataArray.push({\n facilitator,\n networks: new Set(networks),\n pattern: this.derivePattern(networks),\n });\n\n return this;\n }\n\n /**\n * Derives a wildcard pattern from an array of networks.\n * If all networks share the same namespace, returns wildcard pattern.\n * Otherwise returns the first network for exact matching.\n *\n * @param networks - Array of networks\n * @returns Derived pattern for matching\n */\n private derivePattern(networks: Network[]): Network {\n if (networks.length === 0) return \"\" as Network;\n if (networks.length === 1) return networks[0];\n\n // Extract namespaces (e.g., \"eip155\" from \"eip155:84532\")\n const namespaces = networks.map(n => n.split(\":\")[0]);\n const uniqueNamespaces = new Set(namespaces);\n\n // If all same namespace, use wildcard\n if (uniqueNamespaces.size === 1) {\n return `${namespaces[0]}:*` as Network;\n }\n\n // Mixed namespaces - use first network for exact matching\n return networks[0];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,cAAc;;;AC4F3B,IAAM,eAAe,CAAC,UAA0B,MAAM,QAAQ,uBAAuB,MAAM;AAE3F,IAAM,yBAAyB,CAAC,YAA6B;AAC3D,QAAM,SAAS,a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AAAA,MACF;AAEA,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,0BAA8C;AACpD,UAAM,gBAAgB,KAAK;AAC3B,WAAO;AAAA,MACL,aACE,KACe;AACf,eAAO,cAAc,IAAI,GAAG;AAAA,MAC9B;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,gBACNC,cACA,UACA,aACiB;AACjB,QAAI,CAAC,KAAK,6BAA6B,IAAIA,YAAW,GAAG;AACvD,WAAK,6BAA6B,IAAIA,cAAa,CAAC,CAAC;AAAA,IACvD;AACA,UAAM,kBAAkB,KAAK,6BAA6B,IAAIA,YAAW;AAGzE,oBAAgB,KAAK;AAAA,MACnB;AAAA,MACA,UAAU,IAAI,IAAI,QAAQ;AAAA,MAC1B,SAAS,KAAK,cAAc,QAAQ;AAAA,IACtC,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,cAAc,UAA8B;AAClD,QAAI,SAAS,WAAW,EAAG,QAAO;AAClC,QAAI,SAAS,WAAW,EAAG,QAAO,SAAS,CAAC;AAG5C,UAAM,aAAa,SAAS,IAAI,OAAK,EAAE,MAAM,GAAG,EAAE,CAAC,CAAC;AACpD,UAAM,mBAAmB,IAAI,IAAI,UAAU;AAG3C,QAAI,iBAAiB,SAAS,GAAG;AAC/B,aAAO,GAAG,WAAW,CAAC,CAAC;AAAA,IACzB;AAGA,WAAO,SAAS,CAAC;AAAA,EACnB;AACF;","names":["hook","x402Version"]}
{"version":3,"sources":["../../../src/facilitator/index.ts","../../../src/index.ts","../../../src/utils/index.ts","../../../src/facilitator/x402Facilitator.ts"],"sourcesContent":["export * from \"./x402Facilitator\";\n","export const x402Version = 2;\n","import { Network } from \"../types\";\n\n/**\n * Converts a JavaScript number to a plain decimal string, expanding scientific notation\n * via string manipulation rather than parseFloat round-tripping.\n *\n * e.g. 1e-7 → \"0.0000001\", 4.02 → \"4.02\"\n *\n * @param n - The number to convert\n * @returns A plain decimal string representation with no scientific notation\n */\nexport function numberToDecimalString(n: number): string {\n const str = n.toString();\n if (!/[eE]/.test(str)) return str;\n\n const [significand, exponentStr] = str.split(/[eE]/);\n const exp = parseInt(exponentStr, 10);\n const negative = significand.startsWith(\"-\");\n const abs = negative ? significand.slice(1) : significand;\n const [intDigits, fracDigits = \"\"] = abs.split(\".\");\n const allDigits = intDigits + fracDigits;\n const decimalPos = intDigits.length + exp;\n\n let result: string;\n if (decimalPos <= 0) {\n result = \"0.\" + \"0\".repeat(-decimalPos) + allDigits;\n } else if (decimalPos >= allDigits.length) {\n result = allDigits + \"0\".repeat(decimalPos - allDigits.length);\n } else {\n result = allDigits.slice(0, decimalPos) + \".\" + allDigits.slice(decimalPos);\n }\n return (negative ? \"-\" : \"\") + result;\n}\n\n/**\n * Parses a money string into a finite, non-negative decimal number.\n * Accepts plain decimal strings with an optional leading dollar sign.\n *\n * @param money - The money string to parse\n * @returns Decimal number\n */\nexport function parseMoneyString(money: string): number {\n const cleaned = money.replace(/^\\$/, \"\").trim();\n if (!/^-?\\d+(?:\\.\\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n const amount = Number(cleaned);\n if (!Number.isFinite(amount) || amount < 0) {\n throw new Error(`Invalid money format: ${money}`);\n }\n return amount;\n}\n\n/**\n * Convert a decimal amount to token smallest units.\n * Accepts only plain decimal strings — scientific notation is not allowed.\n * Throws if the amount is non-zero but too small to represent with the given decimal precision.\n *\n * @param decimalAmount - The decimal amount as a plain string (e.g., \"0.10\")\n * @param decimals - The number of decimals for the token (e.g., 6 for USDC)\n * @returns The amount in smallest units as a string\n */\nexport function convertToTokenAmount(decimalAmount: string, decimals: number): string {\n if (/[eE]/.test(decimalAmount)) {\n throw new Error(\n `Invalid amount: ${decimalAmount} — use decimal notation, not scientific notation`,\n );\n }\n if (!/^-?\\d+\\.?\\d*$/.test(decimalAmount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const [intPart, decPart = \"\"] = decimalAmount.split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n if (tokenAmount === \"0\" && /[1-9]/.test(decimalAmount)) {\n throw new Error(\n `Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`,\n );\n }\n return tokenAmount;\n}\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\nconst networkPatternToRegExp = (pattern: Network): RegExp => {\n const source = escapeRegExp(pattern).replace(/\\\\\\*/g, \".*\");\n return new RegExp(`^${source}$`);\n};\n\nexport const networkMatchesPattern = (pattern: Network, network: Network): boolean => {\n return networkPatternToRegExp(pattern).test(network);\n};\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n if (networkMatchesPattern(registeredNetworkPattern as Network, network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n\n/**\n * Coerces a value for array-aware merging/comparison: an array passes through\n * unchanged, while a bare scalar is wrapped as a single-element array so it can\n * merge or compare against an array declared on the other side (e.g. an\n * extension field documented as \"string or array of strings\"). Returns\n * undefined for values that cannot participate (null, undefined, objects).\n *\n * @param value - Value to coerce\n * @returns The value as an array, or undefined if it cannot be treated as one\n */\nexport function toComparableArray(value: unknown): unknown[] | undefined {\n if (Array.isArray(value)) {\n return value;\n }\n if (value === null || value === undefined || typeof value === \"object\") {\n return undefined;\n }\n return [value];\n}\n\n/**\n * Extension info fields, keyed by extension key, where a conflicting array\n * value declared by both server and client is additive rather than exclusive:\n * the client's merge concatenates both sides (client first, deduped, see\n * `mergeArraysUnique`), and the server's echo validation accepts any echo that\n * is a superset of the advertised value. Scoped narrowly per field (rather\n * than making all array fields additive) so unrelated extensions - e.g.\n * sign-in-with-x's `resources` - keep exact array matching in both directions.\n */\nexport const ADDITIVE_ARRAY_INFO_FIELDS: Record<string, ReadonlySet<string>> = {\n \"builder-code\": new Set([\"s\"]),\n};\n\n/**\n * Caps the combined echoed length of an additive array field (see\n * {@link ADDITIVE_ARRAY_INFO_FIELDS}) so a hand-crafted payload cannot pad the\n * field past the sum of every party's own reservation and later crowd out a\n * legitimately declared entry once truncated further downstream (e.g. by a\n * facilitator extension). Core has no dependency on extension packages, so\n * this value (builder-code's `MAX_CLIENT_SERVICE_CODES` +\n * `MAX_SERVER_SERVICE_CODES`) is duplicated from\n * `packages/extensions/src/builder-code/types.ts` and must be kept in sync by\n * hand.\n */\nexport const ADDITIVE_ARRAY_MAX_LENGTHS: Record<string, Record<string, number>> = {\n \"builder-code\": { s: 10 },\n};\n","import { x402Version } from \"..\";\nimport { SettleResponse, VerifyResponse } from \"../types/facilitator\";\nimport { FacilitatorExtension } from \"../types/extensions\";\nimport { SchemeNetworkFacilitator, FacilitatorContext } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network } from \"../types\";\nimport { networkMatchesPattern, type SchemeData } from \"../utils\";\n\n/**\n * Facilitator Hook Context Interfaces\n */\n\nexport interface FacilitatorVerifyContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n}\n\nexport interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {\n result: VerifyResponse;\n}\n\nexport interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {\n error: Error;\n}\n\nexport interface FacilitatorSettleContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n}\n\nexport interface FacilitatorSettleResultContext extends FacilitatorSettleContext {\n result: SettleResponse;\n}\n\nexport interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {\n error: Error;\n}\n\n/**\n * Facilitator Hook Type Definitions\n */\n\nexport type FacilitatorBeforeVerifyHook = (\n context: FacilitatorVerifyContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;\n\nexport type FacilitatorOnVerifyFailureHook = (\n context: FacilitatorVerifyFailureContext,\n) => Promise<void | { recovered: true; result: VerifyResponse }>;\n\nexport type FacilitatorBeforeSettleHook = (\n context: FacilitatorSettleContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;\n\nexport type FacilitatorOnSettleFailureHook = (\n context: FacilitatorSettleFailureContext,\n) => Promise<void | { recovered: true; result: SettleResponse }>;\n\n/**\n * Facilitator client for the x402 payment protocol.\n * Manages payment scheme registration, verification, and settlement.\n */\nexport class x402Facilitator {\n private readonly registeredFacilitatorSchemes: Map<\n number,\n SchemeData<SchemeNetworkFacilitator>[] // Array to support multiple facilitators per version\n > = new Map();\n private readonly extensions: Map<string, FacilitatorExtension> = new Map();\n\n private beforeVerifyHooks: FacilitatorBeforeVerifyHook[] = [];\n private afterVerifyHooks: FacilitatorAfterVerifyHook[] = [];\n private onVerifyFailureHooks: FacilitatorOnVerifyFailureHook[] = [];\n private beforeSettleHooks: FacilitatorBeforeSettleHook[] = [];\n private afterSettleHooks: FacilitatorAfterSettleHook[] = [];\n private onSettleFailureHooks: FacilitatorOnSettleFailureHook[] = [];\n\n /**\n * Registers a scheme facilitator for the current x402 version.\n * Networks are stored and used for getSupported() - no need to specify them later.\n *\n * @param networks - Single network or array of networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator {\n const networksArray = Array.isArray(networks) ? networks : [networks];\n return this._registerScheme(x402Version, networksArray, facilitator);\n }\n\n /**\n * Registers a scheme facilitator for x402 version 1.\n * Networks are stored and used for getSupported() - no need to specify them later.\n *\n * @param networks - Single network or array of networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n registerV1(\n networks: Network | Network[],\n facilitator: SchemeNetworkFacilitator,\n ): x402Facilitator {\n const networksArray = Array.isArray(networks) ? networks : [networks];\n return this._registerScheme(1, networksArray, facilitator);\n }\n\n /**\n * Registers a protocol extension.\n *\n * @param extension - The extension object to register\n * @returns The x402Facilitator instance for chaining\n */\n registerExtension(extension: FacilitatorExtension): x402Facilitator {\n this.extensions.set(extension.key, extension);\n return this;\n }\n\n /**\n * Gets the list of registered extension keys.\n *\n * @returns Array of extension key strings\n */\n getExtensions(): string[] {\n return Array.from(this.extensions.keys());\n }\n\n /**\n * Gets a registered extension by key.\n *\n * @param key - The extension key to look up\n * @returns The extension object, or undefined if not registered\n */\n getExtension<T extends FacilitatorExtension = FacilitatorExtension>(key: string): T | undefined {\n return this.extensions.get(key) as T | undefined;\n }\n\n /**\n * Register a hook to execute before facilitator payment verification.\n * Can abort verification by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator {\n this.beforeVerifyHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful facilitator payment verification (isValid: true).\n * This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator {\n this.afterVerifyHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when facilitator payment verification fails.\n * Called when: verification returns isValid: false, or an exception is thrown during verification.\n * Can recover from failure by returning { recovered: true, result: VerifyResponse }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator {\n this.onVerifyFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute before facilitator payment settlement.\n * Can abort settlement by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator {\n this.beforeSettleHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful facilitator payment settlement.\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator {\n this.afterSettleHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when facilitator payment settlement fails.\n * Can recover from failure by returning { recovered: true, result: SettleResponse }\n *\n * @param hook - The hook function to register\n * @returns The x402Facilitator instance for chaining\n */\n onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator {\n this.onSettleFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Gets supported payment kinds, extensions, and signers.\n * Uses networks registered during register() calls - no parameters needed.\n * Returns flat array format for backward compatibility with V1 clients.\n *\n * @returns Supported response with kinds as array (with version in each element), extensions, and signers\n */\n getSupported(): {\n kinds: Array<{\n x402Version: number;\n scheme: string;\n network: string;\n extra?: Record<string, unknown>;\n }>;\n extensions: string[];\n signers: Record<string, string[]>;\n } {\n const kinds: Array<{\n x402Version: number;\n scheme: string;\n network: string;\n extra?: Record<string, unknown>;\n }> = [];\n const signersByFamily: Record<string, Set<string>> = {};\n\n // Iterate over registered scheme data (array supports multiple facilitators per version)\n for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {\n for (const schemeData of schemeDataArray) {\n const { facilitator, networks } = schemeData;\n const scheme = facilitator.scheme;\n\n // Iterate over stored concrete networks\n for (const network of networks) {\n const extra = facilitator.getExtra(network);\n kinds.push({\n x402Version: version,\n scheme,\n network,\n ...(extra && { extra }),\n });\n\n // Collect signers by CAIP family for this network\n const family = facilitator.caipFamily;\n if (!signersByFamily[family]) {\n signersByFamily[family] = new Set();\n }\n facilitator.getSigners(network).forEach(signer => signersByFamily[family].add(signer));\n }\n }\n }\n\n // Convert signer sets to arrays\n const signers: Record<string, string[]> = {};\n for (const [family, signerSet] of Object.entries(signersByFamily)) {\n signers[family] = Array.from(signerSet);\n }\n\n return {\n kinds,\n extensions: this.getExtensions(),\n signers,\n };\n }\n\n /**\n * Verifies a payment payload against requirements.\n *\n * @param paymentPayload - The payment payload to verify\n * @param paymentRequirements - The payment requirements to verify against\n * @returns Promise resolving to the verification response\n */\n async verify(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const context: FacilitatorVerifyContext = {\n paymentPayload,\n requirements: paymentRequirements,\n };\n\n // Execute beforeVerify hooks\n for (const hook of this.beforeVerifyHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n return {\n isValid: false,\n invalidReason: result.reason,\n };\n }\n }\n\n try {\n const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);\n if (!schemeDataArray) {\n throw new Error(\n `No facilitator registered for x402 version: ${paymentPayload.x402Version}`,\n );\n }\n\n // Find matching facilitator from array\n let schemeNetworkFacilitator: SchemeNetworkFacilitator | undefined;\n for (const schemeData of schemeDataArray) {\n if (schemeData.facilitator.scheme === paymentRequirements.scheme) {\n // Check if network matches\n if (schemeData.networks.has(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n }\n }\n\n if (!schemeNetworkFacilitator) {\n throw new Error(\n `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`,\n );\n }\n\n const facilitatorContext = this.buildFacilitatorContext();\n const verifyResult = await schemeNetworkFacilitator.verify(\n paymentPayload,\n paymentRequirements,\n facilitatorContext,\n );\n\n // Check if verification failed (isValid: false)\n if (!verifyResult.isValid) {\n const failureContext: FacilitatorVerifyFailureContext = {\n ...context,\n error: new Error(verifyResult.invalidReason || \"Verification failed\"),\n };\n\n // Execute onVerifyFailure hooks\n for (const hook of this.onVerifyFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n // If recovered, execute afterVerify hooks with recovered result\n const recoveredContext: FacilitatorVerifyResultContext = {\n ...context,\n result: result.result,\n };\n for (const hook of this.afterVerifyHooks) {\n await hook(recoveredContext);\n }\n return result.result;\n }\n }\n\n return verifyResult;\n }\n\n // Execute afterVerify hooks only for successful verification\n const resultContext: FacilitatorVerifyResultContext = {\n ...context,\n result: verifyResult,\n };\n\n for (const hook of this.afterVerifyHooks) {\n await hook(resultContext);\n }\n\n return verifyResult;\n } catch (error) {\n const failureContext: FacilitatorVerifyFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onVerifyFailure hooks\n for (const hook of this.onVerifyFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.result;\n }\n }\n\n throw error;\n }\n }\n\n /**\n * Settles a payment based on the payload and requirements.\n *\n * @param paymentPayload - The payment payload to settle\n * @param paymentRequirements - The payment requirements for settlement\n * @returns Promise resolving to the settlement response\n */\n async settle(\n paymentPayload: PaymentPayload,\n paymentRequirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const context: FacilitatorSettleContext = {\n paymentPayload,\n requirements: paymentRequirements,\n };\n\n // Execute beforeSettle hooks\n for (const hook of this.beforeSettleHooks) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Settlement aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);\n if (!schemeDataArray) {\n throw new Error(\n `No facilitator registered for x402 version: ${paymentPayload.x402Version}`,\n );\n }\n\n // Find matching facilitator from array\n let schemeNetworkFacilitator: SchemeNetworkFacilitator | undefined;\n for (const schemeData of schemeDataArray) {\n if (schemeData.facilitator.scheme === paymentRequirements.scheme) {\n // Check if network matches\n if (schemeData.networks.has(paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, paymentRequirements.network)) {\n schemeNetworkFacilitator = schemeData.facilitator;\n break;\n }\n }\n }\n\n if (!schemeNetworkFacilitator) {\n throw new Error(\n `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`,\n );\n }\n\n const facilitatorContext = this.buildFacilitatorContext();\n const settleResult = await schemeNetworkFacilitator.settle(\n paymentPayload,\n paymentRequirements,\n facilitatorContext,\n );\n\n // Execute afterSettle hooks\n const resultContext: FacilitatorSettleResultContext = {\n ...context,\n result: settleResult,\n };\n\n for (const hook of this.afterSettleHooks) {\n await hook(resultContext);\n }\n\n return settleResult;\n } catch (error) {\n const failureContext: FacilitatorSettleFailureContext = {\n ...context,\n error: error as Error,\n };\n\n // Execute onSettleFailure hooks\n for (const hook of this.onSettleFailureHooks) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.result;\n }\n }\n\n throw error;\n }\n }\n\n /**\n * Builds a FacilitatorContext from the registered extensions map.\n * Passed to mechanism verify/settle so they can access extension capabilities.\n *\n * @returns A FacilitatorContext backed by this facilitator's registered extensions\n */\n private buildFacilitatorContext(): FacilitatorContext {\n const extensionsMap = this.extensions;\n return {\n getExtension<T extends FacilitatorExtension = FacilitatorExtension>(\n key: string,\n ): T | undefined {\n return extensionsMap.get(key) as T | undefined;\n },\n };\n }\n\n /**\n * Internal method to register a scheme facilitator.\n *\n * @param x402Version - The x402 protocol version\n * @param networks - Array of concrete networks this facilitator supports\n * @param facilitator - The scheme network facilitator to register\n * @returns The x402Facilitator instance for chaining\n */\n private _registerScheme(\n x402Version: number,\n networks: Network[],\n facilitator: SchemeNetworkFacilitator,\n ): x402Facilitator {\n if (!this.registeredFacilitatorSchemes.has(x402Version)) {\n this.registeredFacilitatorSchemes.set(x402Version, []);\n }\n const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version)!;\n\n // Add new scheme data (supports multiple facilitators with same scheme name)\n schemeDataArray.push({\n facilitator,\n networks: new Set(networks),\n pattern: this.derivePattern(networks),\n });\n\n return this;\n }\n\n /**\n * Derives a wildcard pattern from an array of networks.\n * If all networks share the same namespace, returns wildcard pattern.\n * Otherwise returns the first network for exact matching.\n *\n * @param networks - Array of networks\n * @returns Derived pattern for matching\n */\n private derivePattern(networks: Network[]): Network {\n if (networks.length === 0) return \"\" as Network;\n if (networks.length === 1) return networks[0];\n\n // Extract namespaces (e.g., \"eip155\" from \"eip155:84532\")\n const namespaces = networks.map(n => n.split(\":\")[0]);\n const uniqueNamespaces = new Set(namespaces);\n\n // If all same namespace, use wildcard\n if (uniqueNamespaces.size === 1) {\n return `${namespaces[0]}:*` as Network;\n }\n\n // Mixed namespaces - use first network for exact matching\n return networks[0];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,cAAc;;;AC4F3B,IAAM,eAAe,CAAC,UAA0B,MAAM,QAAQ,uBAAuB,MAAM;AAE3F,IAAM,yBAAyB,CAAC,YAA6B;AAC3D,QAAM,SAAS,aAAa,OAAO,EAAE,QAAQ,SAAS,IAAI;AAC1D,SAAO,IAAI,OAAO,IAAI,MAAM,GAAG;AACjC;AAEO,IAAM,wBAAwB,CAAC,SAAkB,YAA8B;AACpF,SAAO,uBAAuB,OAAO,EAAE,KAAK,OAAO;AACrD;;;ACnCO,IAAM,kBAAN,MAAsB;AAAA,EAAtB;AACL,SAAiB,+BAGb,oBAAI,IAAI;AACZ,SAAiB,aAAgD,oBAAI,IAAI;AAEzE,SAAQ,oBAAmD,CAAC;AAC5D,SAAQ,mBAAiD,CAAC;AAC1D,SAAQ,uBAAyD,CAAC;AAClE,SAAQ,oBAAmD,CAAC;AAC5D,SAAQ,mBAAiD,CAAC;AAC1D,SAAQ,uBAAyD,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUlE,SAAS,UAA+B,aAAwD;AAC9F,UAAM,gBAAgB,MAAM,QAAQ,QAAQ,IAAI,WAAW,CAAC,QAAQ;AACpE,WAAO,KAAK,gBAAgB,aAAa,eAAe,WAAW;AAAA,EACrE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,WACE,UACA,aACiB;AACjB,UAAM,gBAAgB,MAAM,QAAQ,QAAQ,IAAI,WAAW,CAAC,QAAQ;AACpE,WAAO,KAAK,gBAAgB,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C,CAAC;AACpD,UAAM,mBAAmB,IAAI,IAAI,UAAU;AAG3C,QAAI,iBAAiB,SAAS,GAAG;AAC/B,aAAO,GAAG,WAAW,CAAC,CAAC;AAAA,IACzB;AAGA,WAAO,SAAS,CAAC;AAAA,EACnB;AACF;","names":["hook","x402Version"]}

@@ -1,3 +0,3 @@

import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse } from '../x402Client-0g4vl2En.js';
export { C as CompiledRoute, D as DynamicPayTo, l as DynamicPrice, y as FacilitatorClient, z as FacilitatorConfig, A as FacilitatorResponseError, H as HTTPAdapter, w as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, u as HTTPResourceServerExtensionHooks, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, j as PaymentOption, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, v as ResourceServerTransportExtensionHooks, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, n as SettlementFailedResponseBody, U as UnpaidResponseBody, B as getFacilitatorResponseError, x as x402HTTPResourceServer } from '../x402Client-0g4vl2En.js';
import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse } from '../x402Client-CzZlbbXy.js';
export { C as CompiledRoute, D as DynamicPayTo, l as DynamicPrice, B as FacilitatorClient, E as FacilitatorConfig, G as FacilitatorResponseError, I as FacilitatorTimeoutError, H as HTTPAdapter, A as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, u as HTTPResourceServerExtensionHooks, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, y as PAYMENT_REQUIRED_CACHE_CONTROL, j as PaymentOption, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, v as ResourceServerTransportExtensionHooks, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, w as SETTLEMENT_OVERRIDES_HEADER, n as SettlementFailedResponseBody, U as UnpaidResponseBody, J as getFacilitatorResponseError, z as withPrivateCacheControl, x as x402HTTPResourceServer } from '../x402Client-CzZlbbXy.js';
export { HTTPClientExtensionHooks, HTTPPaymentStatus, HTTPResourceResponse, PaymentRequiredContext, PaymentRequiredHook, x402HTTPClient } from '../client/index.js';

@@ -4,0 +4,0 @@

@@ -24,4 +24,7 @@ "use strict";

FacilitatorResponseError: () => FacilitatorResponseError,
FacilitatorTimeoutError: () => FacilitatorTimeoutError,
HTTPFacilitatorClient: () => HTTPFacilitatorClient,
PAYMENT_REQUIRED_CACHE_CONTROL: () => PAYMENT_REQUIRED_CACHE_CONTROL,
RouteConfigurationError: () => RouteConfigurationError,
SETTLEMENT_OVERRIDES_HEADER: () => SETTLEMENT_OVERRIDES_HEADER,
decodePaymentRequiredHeader: () => decodePaymentRequiredHeader,

@@ -34,2 +37,3 @@ decodePaymentResponseHeader: () => decodePaymentResponseHeader,

getFacilitatorResponseError: () => getFacilitatorResponseError,
withPrivateCacheControl: () => withPrivateCacheControl,
x402HTTPClient: () => x402HTTPClient,

@@ -113,2 +117,16 @@ x402HTTPResourceServer: () => x402HTTPResourceServer

};
var FacilitatorTimeoutError = class extends FacilitatorResponseError {
/**
* Creates a FacilitatorTimeoutError.
*
* @param operation - The facilitator operation that timed out
* @param timeoutMs - The configured timeout in milliseconds
*/
constructor(operation, timeoutMs) {
super(`Facilitator ${operation} request timed out after ${timeoutMs}ms`);
this.name = "FacilitatorTimeoutError";
this.operation = operation;
this.timeoutMs = timeoutMs;
}
};
function getFacilitatorResponseError(error) {

@@ -130,2 +148,13 @@ let current = error;

var SETTLEMENT_OVERRIDES_HEADER = "Settlement-Overrides";
var PAYMENT_REQUIRED_CACHE_CONTROL = "no-store";
function withPrivateCacheControl(value) {
if (!value) {
return "private";
}
const directives = value.split(",").map((directive) => directive.trim().toLowerCase());
if (directives.includes("private")) {
return value;
}
return `${value}, private`;
}
var RouteConfigurationError = class extends Error {

@@ -557,3 +586,4 @@ /**

"Content-Type": contentType,
...settleResult.headers
...settleResult.headers,
"Cache-Control": withPrivateCacheControl(null)
},

@@ -583,3 +613,4 @@ body,

"Content-Type": contentType,
...settlementHeaders
...settlementHeaders,
"Cache-Control": PAYMENT_REQUIRED_CACHE_CONTROL
},

@@ -754,3 +785,4 @@ body,

headers: {
"PAYMENT-REQUIRED": encodePaymentRequiredHeader(paymentRequired)
"PAYMENT-REQUIRED": encodePaymentRequiredHeader(paymentRequired),
"Cache-Control": PAYMENT_REQUIRED_CACHE_CONTROL
}

@@ -779,3 +811,4 @@ };

`^${path.replace(/\\/g, "\\\\").replace(/[$()+.?^{|}]/g, "\\$&").replace(/\*/g, ".*?").replace(/\[([^\]]+)\]/g, "[^/]+").replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, "[^/]+").replace(/\//g, "\\/")}$`,
"i"
// "s" (dotAll): without it, "." can't match LF/CR/U+2028/U+2029, so a wildcard segment containing one fails to match.
"is"
);

@@ -938,2 +971,4 @@ return { verb: verb.toUpperCase(), regex, path };

var DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator";
var DEFAULT_TIMEOUT_MS = 3e4;
var MAX_TIMEOUT_MS = 2147483647;
var GET_SUPPORTED_RETRIES = 3;

@@ -1002,2 +1037,13 @@ var GET_SUPPORTED_RETRY_DELAY_MS = 1e3;

}
function isAbortOrTimeoutError(error) {
let current = error;
for (let depth = 0; depth < 10 && current !== null && typeof current === "object"; depth++) {
const name = current.name;
if (name === "TimeoutError" || name === "AbortError") {
return true;
}
current = current.cause;
}
return false;
}
var EXTENSION_RESPONSE_LOG_FIELD_ALLOWLIST = ["status", "rejectedReason", "reason", "code"];

@@ -1051,2 +1097,9 @@ function logExtensionResponsesHeader(response) {

this.url = (config?.url || DEFAULT_FACILITATOR_URL).replace(/\/+$/, "");
const timeoutMs = config?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > MAX_TIMEOUT_MS) {
throw new RangeError(
`timeoutMs must be a positive integer number of milliseconds no greater than ${MAX_TIMEOUT_MS}, got ${timeoutMs}`
);
}
this.timeoutMs = timeoutMs;
this._createAuthHeaders = config?.createAuthHeaders;

@@ -1069,30 +1122,35 @@ }

}
const response = await fetch(`${this.url}/verify`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
})
return this.withRequestTimeout("verify", async (signal) => {
const response = await fetch(`${this.url}/verify`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
}),
signal
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(
`Facilitator verify failed (${response.status}): ${responseExcerpt(text)}`
);
}
if (typeof data === "object" && data !== null && "isValid" in data) {
throw new VerifyError(response.status, data);
}
throw new Error(
`Facilitator verify failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const verifyResult = await parseSuccessResponse(response, verifyResponseSchema, "verify");
logExtensionResponsesHeader(response);
return verifyResult;
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(`Facilitator verify failed (${response.status}): ${responseExcerpt(text)}`);
}
if (typeof data === "object" && data !== null && "isValid" in data) {
throw new VerifyError(response.status, data);
}
throw new Error(
`Facilitator verify failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const verifyResult = await parseSuccessResponse(response, verifyResponseSchema, "verify");
logExtensionResponsesHeader(response);
return verifyResult;
}

@@ -1114,30 +1172,35 @@ /**

}
const response = await fetch(`${this.url}/settle`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
})
return this.withRequestTimeout("settle", async (signal) => {
const response = await fetch(`${this.url}/settle`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
}),
signal
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(
`Facilitator settle failed (${response.status}): ${responseExcerpt(text)}`
);
}
if (typeof data === "object" && data !== null && "success" in data) {
throw new SettleError(response.status, data);
}
throw new Error(
`Facilitator settle failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const settleResult = await parseSuccessResponse(response, settleResponseSchema, "settle");
logExtensionResponsesHeader(response);
return settleResult;
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(`Facilitator settle failed (${response.status}): ${responseExcerpt(text)}`);
}
if (typeof data === "object" && data !== null && "success" in data) {
throw new SettleError(response.status, data);
}
throw new Error(
`Facilitator settle failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const settleResult = await parseSuccessResponse(response, settleResponseSchema, "settle");
logExtensionResponsesHeader(response);
return settleResult;
}

@@ -1160,16 +1223,36 @@ /**

for (let attempt = 0; attempt < GET_SUPPORTED_RETRIES; attempt++) {
const response = await fetch(`${this.url}/supported`, {
method: "GET",
headers,
redirect: "follow"
const outcome = await this.withRequestTimeout("supported", async (signal) => {
const response = await fetch(`${this.url}/supported`, {
method: "GET",
headers,
redirect: "follow",
signal
});
if (response.ok) {
return {
kind: "success",
value: await parseSuccessResponse(response, supportedResponseSchema, "supported")
};
}
const errorText = await response.text().catch((cause) => {
if (isAbortOrTimeoutError(cause)) {
throw cause;
}
return response.statusText;
});
return {
kind: "http-error",
status: response.status,
retryAfter: response.headers.get("Retry-After"),
error: new Error(
`Facilitator getSupported failed (${response.status}): ${responseExcerpt(errorText)}`
)
};
});
if (response.ok) {
return parseSuccessResponse(response, supportedResponseSchema, "supported");
if (outcome.kind === "success") {
return outcome.value;
}
const errorText = await response.text().catch(() => response.statusText);
lastError = new Error(
`Facilitator getSupported failed (${response.status}): ${responseExcerpt(errorText)}`
);
if (response.status === 429 && attempt < GET_SUPPORTED_RETRIES - 1) {
const delay = computeRetryDelay(response.headers.get("Retry-After"), attempt);
lastError = outcome.error;
if (outcome.status === 429 && attempt < GET_SUPPORTED_RETRIES - 1) {
const delay = computeRetryDelay(outcome.retryAfter, attempt);
await new Promise((resolve) => setTimeout(resolve, delay));

@@ -1209,2 +1292,23 @@ continue;

/**
* Runs a single facilitator HTTP attempt under this client's request deadline.
* The provided signal must be passed to `fetch` so the deadline also covers
* response-body consumption.
*
* @param operation - The facilitator operation name ("verify", "settle", "supported")
* @param run - The attempt to execute with the deadline's AbortSignal
* @returns The attempt's result
* @throws FacilitatorTimeoutError when the deadline elapses before completion
*/
async withRequestTimeout(operation, run) {
const signal = AbortSignal.timeout(this.timeoutMs);
try {
return await run(signal);
} catch (error) {
if (signal.aborted && isAbortOrTimeoutError(error)) {
throw new FacilitatorTimeoutError(operation, this.timeoutMs);
}
throw error;
}
}
/**
* Helper to convert objects to JSON-safe format.

@@ -1464,4 +1568,7 @@ * Handles BigInt and other non-JSON types.

FacilitatorResponseError,
FacilitatorTimeoutError,
HTTPFacilitatorClient,
PAYMENT_REQUIRED_CACHE_CONTROL,
RouteConfigurationError,
SETTLEMENT_OVERRIDES_HEADER,
decodePaymentRequiredHeader,

@@ -1474,2 +1581,3 @@ decodePaymentResponseHeader,

getFacilitatorResponseError,
withPrivateCacheControl,
x402HTTPClient,

@@ -1476,0 +1584,0 @@ x402HTTPResourceServer

@@ -1,3 +0,3 @@

import { a as PaymentRequirements, S as SettleResponse } from '../x402Client-0g4vl2En.js';
export { a5 as AfterSettleHook, a2 as AfterVerifyHook, a4 as BeforeSettleHook, a1 as BeforeVerifyHook, C as CompiledRoute, _ as ExtensionValidationResult, y as FacilitatorClient, z as FacilitatorConfig, A as FacilitatorResponseError, H as HTTPAdapter, w as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, a6 as OnSettleFailureHook, a7 as OnVerifiedPaymentCanceledHook, a3 as OnVerifyFailureHook, Y as PaymentCancellationDispatcher, I as PaymentRequiredContext, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, G as ResourceConfig, a0 as ResourceVerifyRespone, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, ac as SETTLEMENT_OVERRIDES_HEADER, a8 as SchemeEnrichPaymentRequiredResponseHook, aa as SchemeEnrichSettlementPayloadHook, ab as SchemeEnrichSettlementResponseHook, a9 as SchemePaymentRequiredContext, M as SettleContext, Q as SettleFailureContext, O as SettleResultContext, n as SettlementFailedResponseBody, Z as SettlementOverrides, $ as SkipHandlerDirective, U as UnpaidResponseBody, X as VerifiedPaymentCancelOptions, T as VerifiedPaymentCanceledContext, W as VerifiedPaymentCancellationReason, J as VerifyContext, L as VerifyFailureContext, K as VerifyResultContext, ad as checkIfBazaarNeeded, B as getFacilitatorResponseError, x as x402HTTPResourceServer, E as x402ResourceServer } from '../x402Client-0g4vl2En.js';
import { a as PaymentRequirements, S as SettleResponse } from '../x402Client-CzZlbbXy.js';
export { a9 as AfterSettleHook, a6 as AfterVerifyHook, a8 as BeforeSettleHook, a5 as BeforeVerifyHook, C as CompiledRoute, a2 as ExtensionValidationResult, B as FacilitatorClient, E as FacilitatorConfig, G as FacilitatorResponseError, I as FacilitatorTimeoutError, H as HTTPAdapter, A as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, aa as OnSettleFailureHook, ab as OnVerifiedPaymentCanceledHook, a7 as OnVerifyFailureHook, y as PAYMENT_REQUIRED_CACHE_CONTROL, a0 as PaymentCancellationDispatcher, M as PaymentRequiredContext, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, L as ResourceConfig, a4 as ResourceVerifyRespone, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, w as SETTLEMENT_OVERRIDES_HEADER, ac as SchemeEnrichPaymentRequiredResponseHook, ae as SchemeEnrichSettlementPayloadHook, af as SchemeEnrichSettlementResponseHook, ad as SchemePaymentRequiredContext, W as SettleContext, Y as SettleFailureContext, X as SettleResultContext, n as SettlementFailedResponseBody, a1 as SettlementOverrides, a3 as SkipHandlerDirective, U as UnpaidResponseBody, $ as VerifiedPaymentCancelOptions, Z as VerifiedPaymentCanceledContext, _ as VerifiedPaymentCancellationReason, O as VerifyContext, T as VerifyFailureContext, Q as VerifyResultContext, ag as checkIfBazaarNeeded, J as getFacilitatorResponseError, z as withPrivateCacheControl, x as x402HTTPResourceServer, K as x402ResourceServer } from '../x402Client-CzZlbbXy.js';

@@ -4,0 +4,0 @@ /**

@@ -1,1 +0,1 @@

export { aC as AssetAmount, aV as DeepReadonly, aS as FacilitatorContext, F as FacilitatorExtension, A as FacilitatorResponseError, u as HTTPResourceServerExtensionHooks, aB as Money, aP as MoneyParser, N as Network, P as PaymentPayload, aR as PaymentPayloadContext, aQ as PaymentPayloadResult, aw as PaymentPayloadV1, c as PaymentRequired, I as PaymentRequiredContext, av as PaymentRequiredV1, a as PaymentRequirements, au as PaymentRequirementsV1, aD as Price, aK as ResourceInfo, aT as ResourceServerExtension, aU as ResourceServerExtensionHooks, v as ResourceServerTransportExtensionHooks, aM as SchemeClientHooks, a8 as SchemeEnrichPaymentRequiredResponseHook, aL as SchemeNetworkClient, b as SchemeNetworkFacilitator, aN as SchemeNetworkServer, a9 as SchemePaymentRequiredContext, aO as SchemeServerHooks, M as SettleContext, aJ as SettleError, Q as SettleFailureContext, aF as SettleRequest, S as SettleResponse, O as SettleResultContext, aH as SupportedKind, aG as SupportedResponse, T as VerifiedPaymentCanceledContext, J as VerifyContext, aI as VerifyError, L as VerifyFailureContext, aE as VerifyRequest, V as VerifyResponse, K as VerifyResultContext, B as getFacilitatorResponseError } from '../x402Client-0g4vl2En.js';
export { aF as AssetAmount, aY as DeepReadonly, aV as FacilitatorContext, F as FacilitatorExtension, G as FacilitatorResponseError, I as FacilitatorTimeoutError, u as HTTPResourceServerExtensionHooks, aE as Money, aS as MoneyParser, N as Network, P as PaymentPayload, aU as PaymentPayloadContext, aT as PaymentPayloadResult, az as PaymentPayloadV1, c as PaymentRequired, M as PaymentRequiredContext, ay as PaymentRequiredV1, a as PaymentRequirements, ax as PaymentRequirementsV1, aG as Price, aN as ResourceInfo, aW as ResourceServerExtension, aX as ResourceServerExtensionHooks, v as ResourceServerTransportExtensionHooks, aP as SchemeClientHooks, ac as SchemeEnrichPaymentRequiredResponseHook, aO as SchemeNetworkClient, b as SchemeNetworkFacilitator, aQ as SchemeNetworkServer, ad as SchemePaymentRequiredContext, aR as SchemeServerHooks, W as SettleContext, aM as SettleError, Y as SettleFailureContext, aI as SettleRequest, S as SettleResponse, X as SettleResultContext, aK as SupportedKind, aJ as SupportedResponse, Z as VerifiedPaymentCanceledContext, O as VerifyContext, aL as VerifyError, T as VerifyFailureContext, aH as VerifyRequest, V as VerifyResponse, Q as VerifyResultContext, J as getFacilitatorResponseError } from '../x402Client-CzZlbbXy.js';

@@ -24,2 +24,3 @@ "use strict";

FacilitatorResponseError: () => FacilitatorResponseError,
FacilitatorTimeoutError: () => FacilitatorTimeoutError,
SettleError: () => SettleError,

@@ -81,2 +82,16 @@ VerifyError: () => VerifyError,

};
var FacilitatorTimeoutError = class extends FacilitatorResponseError {
/**
* Creates a FacilitatorTimeoutError.
*
* @param operation - The facilitator operation that timed out
* @param timeoutMs - The configured timeout in milliseconds
*/
constructor(operation, timeoutMs) {
super(`Facilitator ${operation} request timed out after ${timeoutMs}ms`);
this.name = "FacilitatorTimeoutError";
this.operation = operation;
this.timeoutMs = timeoutMs;
}
};
function getFacilitatorResponseError(error) {

@@ -95,2 +110,3 @@ let current = error;

FacilitatorResponseError,
FacilitatorTimeoutError,
SettleError,

@@ -97,0 +113,0 @@ VerifyError,

@@ -1,1 +0,1 @@

{"version":3,"sources":["../../../src/types/index.ts","../../../src/types/facilitator.ts"],"sourcesContent":["export type {\n VerifyRequest,\n VerifyResponse,\n SettleRequest,\n SettleResponse,\n SupportedResponse,\n SupportedKind,\n} from \"./facilitator\";\nexport {\n VerifyError,\n SettleError,\n FacilitatorResponseError,\n getFacilitatorResponseError,\n} from \"./facilitator\";\nexport type {\n PaymentRequirements,\n PaymentPayload,\n PaymentRequired,\n ResourceInfo,\n} from \"./payments\";\nexport type {\n SchemeNetworkClient,\n SchemeClientHooks,\n SchemeNetworkFacilitator,\n SchemeNetworkServer,\n SchemeServerHooks,\n MoneyParser,\n PaymentPayloadResult,\n PaymentPayloadContext,\n FacilitatorContext,\n SchemePaymentRequiredContext,\n SchemeEnrichPaymentRequiredResponseHook,\n} from \"./mechanisms\";\nexport type { PaymentRequirementsV1, PaymentRequiredV1, PaymentPayloadV1 } from \"./v1\";\nexport type {\n FacilitatorExtension,\n ResourceServerExtension,\n ResourceServerExtensionHooks,\n PaymentRequiredContext,\n ResourceServerTransportExtensionHooks,\n HTTPResourceServerExtensionHooks,\n SettleResultContext,\n VerifyContext,\n VerifyResultContext,\n VerifyFailureContext,\n SettleContext,\n SettleFailureContext,\n VerifiedPaymentCanceledContext,\n} from \"./extensions\";\n\nexport type { DeepReadonly } from \"./readonly\";\n\nexport type Network = `${string}:${string}`;\n\nexport type Money = string | number;\nexport type AssetAmount = {\n asset: string;\n amount: string;\n extra?: Record<string, unknown>;\n};\nexport type Price = Money | AssetAmount;\n","import { PaymentPayload, PaymentRequirements } from \"./payments\";\nimport { Network } from \"./\";\n\nexport type VerifyRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type VerifyResponse = {\n isValid: boolean;\n invalidReason?: string;\n invalidMessage?: string;\n payer?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SettleRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type SettleResponse = {\n success: boolean;\n errorReason?: string;\n errorMessage?: string;\n payer?: string;\n transaction: string;\n network: Network;\n /** Actual amount settled in atomic token units. Present for schemes like `upto` where settlement amount may differ from the authorized maximum. */\n amount?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedKind = {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedResponse = {\n kinds: SupportedKind[];\n extensions: string[];\n signers: Record<string, string[]>; // CAIP family pattern → Signer addresses\n};\n\n/**\n * Error thrown when payment verification fails.\n */\nexport class VerifyError extends Error {\n readonly invalidReason?: string;\n readonly invalidMessage?: string;\n readonly payer?: string;\n readonly statusCode: number;\n\n /**\n * Creates a VerifyError from a failed verification response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The verify response containing failure details\n */\n constructor(statusCode: number, response: VerifyResponse) {\n const reason = response.invalidReason || \"unknown reason\";\n const message = response.invalidMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"VerifyError\";\n this.statusCode = statusCode;\n this.invalidReason = response.invalidReason;\n this.invalidMessage = response.invalidMessage;\n this.payer = response.payer;\n }\n}\n\n/**\n * Error thrown when payment settlement fails.\n */\nexport class SettleError extends Error {\n readonly errorReason?: string;\n readonly errorMessage?: string;\n readonly payer?: string;\n readonly transaction: string;\n readonly network: Network;\n readonly statusCode: number;\n\n /**\n * Creates a SettleError from a failed settlement response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The settle response containing error details\n */\n constructor(statusCode: number, response: SettleResponse) {\n const reason = response.errorReason || \"unknown reason\";\n const message = response.errorMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"SettleError\";\n this.statusCode = statusCode;\n this.errorReason = response.errorReason;\n this.errorMessage = response.errorMessage;\n this.payer = response.payer;\n this.transaction = response.transaction;\n this.network = response.network;\n }\n}\n\n/**\n * Error thrown when a facilitator returns malformed success payload data.\n */\nexport class FacilitatorResponseError extends Error {\n /**\n * Creates a FacilitatorResponseError for malformed facilitator responses.\n *\n * @param message - The boundary error message\n */\n constructor(message: string) {\n super(message);\n this.name = \"FacilitatorResponseError\";\n }\n}\n\n/**\n * Walks an error cause chain to find the first facilitator response error.\n *\n * @param error - The thrown value to inspect\n * @returns The nested facilitator response error, if present\n */\nexport function getFacilitatorResponseError(error: unknown): FacilitatorResponseError | null {\n let current = error;\n\n while (current instanceof Error) {\n if (current instanceof FacilitatorResponseError) {\n return current;\n }\n current = current.cause;\n }\n\n return null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqDO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,iBAAiB;AACzC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,gBAAgB,SAAS;AAC9B,SAAK,iBAAiB,SAAS;AAC/B,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAKO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,eAAe;AACvC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,cAAc,SAAS;AAC5B,SAAK,eAAe,SAAS;AAC7B,SAAK,QAAQ,SAAS;AACtB,SAAK,cAAc,SAAS;AAC5B,SAAK,UAAU,SAAS;AAAA,EAC1B;AACF;AAKO,IAAM,2BAAN,cAAuC,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMlD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAQO,SAAS,4BAA4B,OAAiD;AAC3F,MAAI,UAAU;AAEd,SAAO,mBAAmB,OAAO;AAC/B,QAAI,mBAAmB,0BAA0B;AAC/C,aAAO;AAAA,IACT;AACA,cAAU,QAAQ;AAAA,EACpB;AAEA,SAAO;AACT;","names":[]}
{"version":3,"sources":["../../../src/types/index.ts","../../../src/types/facilitator.ts"],"sourcesContent":["export type {\n VerifyRequest,\n VerifyResponse,\n SettleRequest,\n SettleResponse,\n SupportedResponse,\n SupportedKind,\n} from \"./facilitator\";\nexport {\n VerifyError,\n SettleError,\n FacilitatorResponseError,\n FacilitatorTimeoutError,\n getFacilitatorResponseError,\n} from \"./facilitator\";\nexport type {\n PaymentRequirements,\n PaymentPayload,\n PaymentRequired,\n ResourceInfo,\n} from \"./payments\";\nexport type {\n SchemeNetworkClient,\n SchemeClientHooks,\n SchemeNetworkFacilitator,\n SchemeNetworkServer,\n SchemeServerHooks,\n MoneyParser,\n PaymentPayloadResult,\n PaymentPayloadContext,\n FacilitatorContext,\n SchemePaymentRequiredContext,\n SchemeEnrichPaymentRequiredResponseHook,\n} from \"./mechanisms\";\nexport type { PaymentRequirementsV1, PaymentRequiredV1, PaymentPayloadV1 } from \"./v1\";\nexport type {\n FacilitatorExtension,\n ResourceServerExtension,\n ResourceServerExtensionHooks,\n PaymentRequiredContext,\n ResourceServerTransportExtensionHooks,\n HTTPResourceServerExtensionHooks,\n SettleResultContext,\n VerifyContext,\n VerifyResultContext,\n VerifyFailureContext,\n SettleContext,\n SettleFailureContext,\n VerifiedPaymentCanceledContext,\n} from \"./extensions\";\n\nexport type { DeepReadonly } from \"./readonly\";\n\nexport type Network = `${string}:${string}`;\n\nexport type Money = string | number;\nexport type AssetAmount = {\n asset: string;\n amount: string;\n extra?: Record<string, unknown>;\n};\nexport type Price = Money | AssetAmount;\n","import { PaymentPayload, PaymentRequirements } from \"./payments\";\nimport { Network } from \"./\";\n\nexport type VerifyRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type VerifyResponse = {\n isValid: boolean;\n invalidReason?: string;\n invalidMessage?: string;\n payer?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SettleRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type SettleResponse = {\n success: boolean;\n errorReason?: string;\n errorMessage?: string;\n payer?: string;\n transaction: string;\n network: Network;\n /** Actual amount settled in atomic token units. Present for schemes like `upto` where settlement amount may differ from the authorized maximum. */\n amount?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedKind = {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedResponse = {\n kinds: SupportedKind[];\n extensions: string[];\n signers: Record<string, string[]>; // CAIP family pattern → Signer addresses\n};\n\n/**\n * Error thrown when payment verification fails.\n */\nexport class VerifyError extends Error {\n readonly invalidReason?: string;\n readonly invalidMessage?: string;\n readonly payer?: string;\n readonly statusCode: number;\n\n /**\n * Creates a VerifyError from a failed verification response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The verify response containing failure details\n */\n constructor(statusCode: number, response: VerifyResponse) {\n const reason = response.invalidReason || \"unknown reason\";\n const message = response.invalidMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"VerifyError\";\n this.statusCode = statusCode;\n this.invalidReason = response.invalidReason;\n this.invalidMessage = response.invalidMessage;\n this.payer = response.payer;\n }\n}\n\n/**\n * Error thrown when payment settlement fails.\n */\nexport class SettleError extends Error {\n readonly errorReason?: string;\n readonly errorMessage?: string;\n readonly payer?: string;\n readonly transaction: string;\n readonly network: Network;\n readonly statusCode: number;\n\n /**\n * Creates a SettleError from a failed settlement response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The settle response containing error details\n */\n constructor(statusCode: number, response: SettleResponse) {\n const reason = response.errorReason || \"unknown reason\";\n const message = response.errorMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"SettleError\";\n this.statusCode = statusCode;\n this.errorReason = response.errorReason;\n this.errorMessage = response.errorMessage;\n this.payer = response.payer;\n this.transaction = response.transaction;\n this.network = response.network;\n }\n}\n\n/**\n * Error thrown when a facilitator returns malformed success payload data.\n */\nexport class FacilitatorResponseError extends Error {\n /**\n * Creates a FacilitatorResponseError for malformed facilitator responses.\n *\n * @param message - The boundary error message\n */\n constructor(message: string) {\n super(message);\n this.name = \"FacilitatorResponseError\";\n }\n}\n\n/**\n * Error thrown when a facilitator HTTP request exceeds the client's configured timeout.\n *\n * Extends FacilitatorResponseError so HTTP middlewares surface it as a facilitator\n * boundary failure (502) rather than an unhandled runtime error.\n *\n * Note: for settle(), a client-side timeout is an indeterminate outcome — the\n * facilitator may have received and completed the settlement after the client\n * stopped waiting. Callers must not treat this error as proof that settlement\n * did not occur.\n */\nexport class FacilitatorTimeoutError extends FacilitatorResponseError {\n /** The facilitator operation that timed out (\"verify\", \"settle\", or \"supported\") */\n readonly operation: string;\n /** The timeout that elapsed, in milliseconds */\n readonly timeoutMs: number;\n\n /**\n * Creates a FacilitatorTimeoutError.\n *\n * @param operation - The facilitator operation that timed out\n * @param timeoutMs - The configured timeout in milliseconds\n */\n constructor(operation: string, timeoutMs: number) {\n super(`Facilitator ${operation} request timed out after ${timeoutMs}ms`);\n this.name = \"FacilitatorTimeoutError\";\n this.operation = operation;\n this.timeoutMs = timeoutMs;\n }\n}\n\n/**\n * Walks an error cause chain to find the first facilitator response error.\n *\n * @param error - The thrown value to inspect\n * @returns The nested facilitator response error, if present\n */\nexport function getFacilitatorResponseError(error: unknown): FacilitatorResponseError | null {\n let current = error;\n\n while (current instanceof Error) {\n if (current instanceof FacilitatorResponseError) {\n return current;\n }\n current = current.cause;\n }\n\n return null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqDO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,iBAAiB;AACzC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,gBAAgB,SAAS;AAC9B,SAAK,iBAAiB,SAAS;AAC/B,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAKO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,eAAe;AACvC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,cAAc,SAAS;AAC5B,SAAK,eAAe,SAAS;AAC7B,SAAK,QAAQ,SAAS;AACtB,SAAK,cAAc,SAAS;AAC5B,SAAK,UAAU,SAAS;AAAA,EAC1B;AACF;AAKO,IAAM,2BAAN,cAAuC,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMlD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAaO,IAAM,0BAAN,cAAsC,yBAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYpE,YAAY,WAAmB,WAAmB;AAChD,UAAM,eAAe,SAAS,4BAA4B,SAAS,IAAI;AACvE,SAAK,OAAO;AACZ,SAAK,YAAY;AACjB,SAAK,YAAY;AAAA,EACnB;AACF;AAQO,SAAS,4BAA4B,OAAiD;AAC3F,MAAI,UAAU;AAEd,SAAO,mBAAmB,OAAO;AAC/B,QAAI,mBAAmB,0BAA0B;AAC/C,aAAO;AAAA,IACT;AACA,cAAU,QAAQ;AAAA,EACpB;AAEA,SAAO;AACT;","names":[]}

@@ -1,1 +0,1 @@

export { aw as PaymentPayloadV1, av as PaymentRequiredV1, au as PaymentRequirementsV1, ay as SettleRequestV1, az as SettleResponseV1, aA as SupportedResponseV1, ax as VerifyRequestV1 } from '../../x402Client-0g4vl2En.js';
export { az as PaymentPayloadV1, ay as PaymentRequiredV1, ax as PaymentRequirementsV1, aB as SettleRequestV1, aC as SettleResponseV1, aD as SupportedResponseV1, aA as VerifyRequestV1 } from '../../x402Client-CzZlbbXy.js';

@@ -1,2 +0,2 @@

import { N as Network } from '../x402Client-0g4vl2En.js';
import { N as Network } from '../x402Client-CzZlbbXy.js';

@@ -76,3 +76,36 @@ /**

declare function deepEqual(obj1: unknown, obj2: unknown): boolean;
/**
* Coerces a value for array-aware merging/comparison: an array passes through
* unchanged, while a bare scalar is wrapped as a single-element array so it can
* merge or compare against an array declared on the other side (e.g. an
* extension field documented as "string or array of strings"). Returns
* undefined for values that cannot participate (null, undefined, objects).
*
* @param value - Value to coerce
* @returns The value as an array, or undefined if it cannot be treated as one
*/
declare function toComparableArray(value: unknown): unknown[] | undefined;
/**
* Extension info fields, keyed by extension key, where a conflicting array
* value declared by both server and client is additive rather than exclusive:
* the client's merge concatenates both sides (client first, deduped, see
* `mergeArraysUnique`), and the server's echo validation accepts any echo that
* is a superset of the advertised value. Scoped narrowly per field (rather
* than making all array fields additive) so unrelated extensions - e.g.
* sign-in-with-x's `resources` - keep exact array matching in both directions.
*/
declare const ADDITIVE_ARRAY_INFO_FIELDS: Record<string, ReadonlySet<string>>;
/**
* Caps the combined echoed length of an additive array field (see
* {@link ADDITIVE_ARRAY_INFO_FIELDS}) so a hand-crafted payload cannot pad the
* field past the sum of every party's own reservation and later crowd out a
* legitimately declared entry once truncated further downstream (e.g. by a
* facilitator extension). Core has no dependency on extension packages, so
* this value (builder-code's `MAX_CLIENT_SERVICE_CODES` +
* `MAX_SERVER_SERVICE_CODES`) is duplicated from
* `packages/extensions/src/builder-code/types.ts` and must be kept in sync by
* hand.
*/
declare const ADDITIVE_ARRAY_MAX_LENGTHS: Record<string, Record<string, number>>;
export { Base64EncodedRegex, type SchemeData, convertToTokenAmount, deepEqual, findByNetworkAndScheme, findFacilitatorBySchemeAndNetwork, findSchemesByNetwork, networkMatchesPattern, numberToDecimalString, parseMoneyString, safeBase64Decode, safeBase64Encode };
export { ADDITIVE_ARRAY_INFO_FIELDS, ADDITIVE_ARRAY_MAX_LENGTHS, Base64EncodedRegex, type SchemeData, convertToTokenAmount, deepEqual, findByNetworkAndScheme, findFacilitatorBySchemeAndNetwork, findSchemesByNetwork, networkMatchesPattern, numberToDecimalString, parseMoneyString, safeBase64Decode, safeBase64Encode, toComparableArray };

@@ -23,2 +23,4 @@ "use strict";

__export(utils_exports, {
ADDITIVE_ARRAY_INFO_FIELDS: () => ADDITIVE_ARRAY_INFO_FIELDS,
ADDITIVE_ARRAY_MAX_LENGTHS: () => ADDITIVE_ARRAY_MAX_LENGTHS,
Base64EncodedRegex: () => Base64EncodedRegex,

@@ -34,3 +36,4 @@ convertToTokenAmount: () => convertToTokenAmount,

safeBase64Decode: () => safeBase64Decode,
safeBase64Encode: () => safeBase64Encode
safeBase64Encode: () => safeBase64Encode,
toComparableArray: () => toComparableArray
});

@@ -167,4 +170,21 @@ module.exports = __toCommonJS(utils_exports);

}
function toComparableArray(value) {
if (Array.isArray(value)) {
return value;
}
if (value === null || value === void 0 || typeof value === "object") {
return void 0;
}
return [value];
}
var ADDITIVE_ARRAY_INFO_FIELDS = {
"builder-code": /* @__PURE__ */ new Set(["s"])
};
var ADDITIVE_ARRAY_MAX_LENGTHS = {
"builder-code": { s: 10 }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ADDITIVE_ARRAY_INFO_FIELDS,
ADDITIVE_ARRAY_MAX_LENGTHS,
Base64EncodedRegex,

@@ -180,4 +200,5 @@ convertToTokenAmount,

safeBase64Decode,
safeBase64Encode
safeBase64Encode,
toComparableArray
});
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"sources":["../../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Converts a JavaScript number to a plain decimal string, expanding scientific notation\n * via string manipulation rather than parseFloat round-tripping.\n *\n * e.g. 1e-7 → \"0.0000001\", 4.02 → \"4.02\"\n *\n * @param n - The number to convert\n * @returns A plain decimal string representation with no scientific notation\n */\nexport function numberToDecimalString(n: number): string {\n const str = n.toString();\n if (!/[eE]/.test(str)) return str;\n\n const [significand, exponentStr] = str.split(/[eE]/);\n const exp = parseInt(exponentStr, 10);\n const negative = significand.startsWith(\"-\");\n const abs = negative ? significand.slice(1) : significand;\n const [intDigits, fracDigits = \"\"] = abs.split(\".\");\n const allDigits = intDigits + fracDigits;\n const decimalPos = intDigits.length + exp;\n\n let result: string;\n if (decimalPos <= 0) {\n result = \"0.\" + \"0\".repeat(-decimalPos) + allDigits;\n } else if (decimalPos >= allDigits.length) {\n result = allDigits + \"0\".repeat(decimalPos - allDigits.length);\n } else {\n result = allDigits.slice(0, decimalPos) + \".\" + allDigits.slice(decimalPos);\n }\n return (negative ? \"-\" : \"\") + result;\n}\n\n/**\n * Parses a money string into a finite, non-negative decimal number.\n * Accepts plain decimal strings with an optional leading dollar sign.\n *\n * @param money - The money string to parse\n * @returns Decimal number\n */\nexport function parseMoneyString(money: string): number {\n const cleaned = money.replace(/^\\$/, \"\").trim();\n if (!/^-?\\d+(?:\\.\\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n const amount = Number(cleaned);\n if (!Number.isFinite(amount) || amount < 0) {\n throw new Error(`Invalid money format: ${money}`);\n }\n return amount;\n}\n\n/**\n * Convert a decimal amount to token smallest units.\n * Accepts only plain decimal strings — scientific notation is not allowed.\n * Throws if the amount is non-zero but too small to represent with the given decimal precision.\n *\n * @param decimalAmount - The decimal amount as a plain string (e.g., \"0.10\")\n * @param decimals - The number of decimals for the token (e.g., 6 for USDC)\n * @returns The amount in smallest units as a string\n */\nexport function convertToTokenAmount(decimalAmount: string, decimals: number): string {\n if (/[eE]/.test(decimalAmount)) {\n throw new Error(\n `Invalid amount: ${decimalAmount} — use decimal notation, not scientific notation`,\n );\n }\n if (!/^-?\\d+\\.?\\d*$/.test(decimalAmount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const [intPart, decPart = \"\"] = decimalAmount.split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n if (tokenAmount === \"0\" && /[1-9]/.test(decimalAmount)) {\n throw new Error(\n `Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`,\n );\n }\n return tokenAmount;\n}\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\nconst networkPatternToRegExp = (pattern: Network): RegExp => {\n const source = escapeRegExp(pattern).replace(/\\\\\\*/g, \".*\");\n return new RegExp(`^${source}$`);\n};\n\nexport const networkMatchesPattern = (pattern: Network, network: Network): boolean => {\n return networkPatternToRegExp(pattern).test(network);\n};\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n if (networkMatchesPattern(registeredNetworkPattern as Network, network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,SAAS,sBAAsB,GAAmB;AACvD,QAAM,MAAM,EAAE,SAAS;AACvB,MAAI,CAAC,OAAO,KAAK,GAAG,EAAG,QAAO;AAE9B,QAAM,CAAC,aAAa,WAAW,IAAI,IAAI,MAAM,MAAM;AACnD,QAAM,MAAM,SAA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AAM,UAAU,KAAK,CAAC,IAAI;AAAA,IACjF,CAAC;AACH,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AAEA,MAAI;AACF,WAAO,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC3C,QAAQ;AAEN,WAAO,KAAK,UAAU,IAAI,MAAM,KAAK,UAAU,IAAI;AAAA,EACrD;AACF;","names":[]}
{"version":3,"sources":["../../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Converts a JavaScript number to a plain decimal string, expanding scientific notation\n * via string manipulation rather than parseFloat round-tripping.\n *\n * e.g. 1e-7 → \"0.0000001\", 4.02 → \"4.02\"\n *\n * @param n - The number to convert\n * @returns A plain decimal string representation with no scientific notation\n */\nexport function numberToDecimalString(n: number): string {\n const str = n.toString();\n if (!/[eE]/.test(str)) return str;\n\n const [significand, exponentStr] = str.split(/[eE]/);\n const exp = parseInt(exponentStr, 10);\n const negative = significand.startsWith(\"-\");\n const abs = negative ? significand.slice(1) : significand;\n const [intDigits, fracDigits = \"\"] = abs.split(\".\");\n const allDigits = intDigits + fracDigits;\n const decimalPos = intDigits.length + exp;\n\n let result: string;\n if (decimalPos <= 0) {\n result = \"0.\" + \"0\".repeat(-decimalPos) + allDigits;\n } else if (decimalPos >= allDigits.length) {\n result = allDigits + \"0\".repeat(decimalPos - allDigits.length);\n } else {\n result = allDigits.slice(0, decimalPos) + \".\" + allDigits.slice(decimalPos);\n }\n return (negative ? \"-\" : \"\") + result;\n}\n\n/**\n * Parses a money string into a finite, non-negative decimal number.\n * Accepts plain decimal strings with an optional leading dollar sign.\n *\n * @param money - The money string to parse\n * @returns Decimal number\n */\nexport function parseMoneyString(money: string): number {\n const cleaned = money.replace(/^\\$/, \"\").trim();\n if (!/^-?\\d+(?:\\.\\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n const amount = Number(cleaned);\n if (!Number.isFinite(amount) || amount < 0) {\n throw new Error(`Invalid money format: ${money}`);\n }\n return amount;\n}\n\n/**\n * Convert a decimal amount to token smallest units.\n * Accepts only plain decimal strings — scientific notation is not allowed.\n * Throws if the amount is non-zero but too small to represent with the given decimal precision.\n *\n * @param decimalAmount - The decimal amount as a plain string (e.g., \"0.10\")\n * @param decimals - The number of decimals for the token (e.g., 6 for USDC)\n * @returns The amount in smallest units as a string\n */\nexport function convertToTokenAmount(decimalAmount: string, decimals: number): string {\n if (/[eE]/.test(decimalAmount)) {\n throw new Error(\n `Invalid amount: ${decimalAmount} — use decimal notation, not scientific notation`,\n );\n }\n if (!/^-?\\d+\\.?\\d*$/.test(decimalAmount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const [intPart, decPart = \"\"] = decimalAmount.split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n if (tokenAmount === \"0\" && /[1-9]/.test(decimalAmount)) {\n throw new Error(\n `Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`,\n );\n }\n return tokenAmount;\n}\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\nconst networkPatternToRegExp = (pattern: Network): RegExp => {\n const source = escapeRegExp(pattern).replace(/\\\\\\*/g, \".*\");\n return new RegExp(`^${source}$`);\n};\n\nexport const networkMatchesPattern = (pattern: Network, network: Network): boolean => {\n return networkPatternToRegExp(pattern).test(network);\n};\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n if (networkMatchesPattern(registeredNetworkPattern as Network, network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if normalization fails\n return JSON.stringify(obj1) === JSON.stringify(obj2);\n }\n}\n\n/**\n * Coerces a value for array-aware merging/comparison: an array passes through\n * unchanged, while a bare scalar is wrapped as a single-element array so it can\n * merge or compare against an array declared on the other side (e.g. an\n * extension field documented as \"string or array of strings\"). Returns\n * undefined for values that cannot participate (null, undefined, objects).\n *\n * @param value - Value to coerce\n * @returns The value as an array, or undefined if it cannot be treated as one\n */\nexport function toComparableArray(value: unknown): unknown[] | undefined {\n if (Array.isArray(value)) {\n return value;\n }\n if (value === null || value === undefined || typeof value === \"object\") {\n return undefined;\n }\n return [value];\n}\n\n/**\n * Extension info fields, keyed by extension key, where a conflicting array\n * value declared by both server and client is additive rather than exclusive:\n * the client's merge concatenates both sides (client first, deduped, see\n * `mergeArraysUnique`), and the server's echo validation accepts any echo that\n * is a superset of the advertised value. Scoped narrowly per field (rather\n * than making all array fields additive) so unrelated extensions - e.g.\n * sign-in-with-x's `resources` - keep exact array matching in both directions.\n */\nexport const ADDITIVE_ARRAY_INFO_FIELDS: Record<string, ReadonlySet<string>> = {\n \"builder-code\": new Set([\"s\"]),\n};\n\n/**\n * Caps the combined echoed length of an additive array field (see\n * {@link ADDITIVE_ARRAY_INFO_FIELDS}) so a hand-crafted payload cannot pad the\n * field past the sum of every party's own reservation and later crowd out a\n * legitimately declared entry once truncated further downstream (e.g. by a\n * facilitator extension). Core has no dependency on extension packages, so\n * this value (builder-code's `MAX_CLIENT_SERVICE_CODES` +\n * `MAX_SERVER_SERVICE_CODES`) is duplicated from\n * `packages/extensions/src/builder-code/types.ts` and must be kept in sync by\n * hand.\n */\nexport const ADDITIVE_ARRAY_MAX_LENGTHS: Record<string, Record<string, number>> = {\n \"builder-code\": { s: 10 },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,SAAS,sBAAsB,GAAmB;AACvD,QAAM,MAAM,EAAE,SAAS;AACvB,MAAI,CAAC,OAAO,KAAK,GAAG,EAAG,QAAO;AAE9B,QAAM,CAAC,aAAa,WAAW,IAAI,IAAI,MAAM,MAAM;AACnD,QAAM,MAAM,SAAS,aAAa,EAAE;AACpC,QAAM,WAAW,YAAY,WAAW,GAAG;AAC3C,QAAM,MAAM,WAAW,YAAY,MAAM,CAAC,IAAI;AAC9C,QAAM,CAAC,WAAW,aAAa,EAAE,IAAI,IAAI,MAAM,GAAG;AAClD,QAAM,YAAY,YAAY;AAC9B,QAAM,aAAa,UAAU,SAAS;AAEtC,MAAI;AACJ,MAAI,cAAc,GAAG;AACnB,aAAS,OAAO,IAAI,OAAO,CAAC,UAAU,IAAI;AAAA,EAC5C,WAAW,cAAc,UAAU,QAAQ;AACz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,UAAU,UAAa,OAAO,UAAU,UAAU;AACtE,WAAO;AAAA,EACT;AACA,SAAO,CAAC,KAAK;AACf;AAWO,IAAM,6BAAkE;AAAA,EAC7E,gBAAgB,oBAAI,IAAI,CAAC,GAAG,CAAC;AAC/B;AAaO,IAAM,6BAAqE;AAAA,EAChF,gBAAgB,EAAE,GAAG,GAAG;AAC1B;","names":[]}

@@ -1,3 +0,3 @@

import { c as PaymentRequired, ae as x402Client, P as PaymentPayload, S as SettleResponse } from '../x402Client-0g4vl2En.mjs';
export { aj as AfterPaymentCreationHook, ai as BeforePaymentCreationHook, aq as ClientExtension, ao as ClientExtensionHooks, ap as ClientTransportExtensionHooks, ak as OnPaymentCreationFailureHook, am as OnPaymentResponseHook, ag as PaymentCreatedContext, af as PaymentCreationContext, ah as PaymentCreationFailureContext, ar as PaymentPolicy, al as PaymentResponseContext, as as SchemeRegistration, an as SelectPaymentRequirements, at as x402ClientConfig } from '../x402Client-0g4vl2En.mjs';
import { c as PaymentRequired, ah as x402Client, P as PaymentPayload, S as SettleResponse } from '../x402Client-CzZlbbXy.mjs';
export { am as AfterPaymentCreationHook, al as BeforePaymentCreationHook, at as ClientExtension, ar as ClientExtensionHooks, as as ClientTransportExtensionHooks, an as OnPaymentCreationFailureHook, ap as OnPaymentResponseHook, aj as PaymentCreatedContext, ai as PaymentCreationContext, ak as PaymentCreationFailureContext, au as PaymentPolicy, ao as PaymentResponseContext, av as SchemeRegistration, aq as SelectPaymentRequirements, aw as x402ClientConfig } from '../x402Client-CzZlbbXy.mjs';

@@ -4,0 +4,0 @@ /**

import {
x402HTTPClient
} from "../chunk-4Y6I6537.mjs";
} from "../chunk-VKJGPEW2.mjs";
import "../chunk-N4QXZG2Z.mjs";

@@ -8,7 +8,10 @@ import {

} from "../chunk-VE37GDG2.mjs";
import "../chunk-AGOUMC4P.mjs";
import "../chunk-P3DFEIO7.mjs";
import {
ADDITIVE_ARRAY_INFO_FIELDS,
deepEqual,
findByNetworkAndScheme,
findSchemesByNetwork
} from "../chunk-ABS7D6VX.mjs";
findSchemesByNetwork,
toComparableArray
} from "../chunk-HX5GESJI.mjs";
import "../chunk-BJTO5JO5.mjs";

@@ -104,5 +107,5 @@

* Extensions are invoked after the scheme creates the base payload and the
* payload is wrapped with extensions/resource/accepted data. If the extension's
* key is present in `paymentRequired.extensions`, the extension's
* `enrichPaymentPayload` hook is called to modify the payload.
* payload is wrapped with extensions/resource/accepted data. Every registered
* extension's `enrichPaymentPayload` hook is called to modify the payload.
* Server-declared fields are preserved via merge after enrichment.
*

@@ -280,2 +283,7 @@ * @param extension - The client extension to register

* Client extension data may add fields, but server-declared fields remain intact.
* For fields listed in `ADDITIVE_ARRAY_INFO_FIELDS` (e.g. builder-code `s`), a
* conflicting array is concatenated with client entries first (so a downstream
* length cap trims server entries rather than the client's) and duplicates
* removed; a scalar on either side is treated as a single-element array. Every
* other conflicting array keeps the server's value, same as any other scalar.
*

@@ -298,2 +306,3 @@ * @param serverExtensions - Extensions declared by the server in the 402 response

const clientRecord = clientValue;
const additiveFields = ADDITIVE_ARRAY_INFO_FIELDS[key];
const extensionValue = { ...serverRecord };

@@ -313,2 +322,10 @@ const pending = [{ target: extensionValue, source: clientRecord }];

}
if (additiveFields?.has(fieldKey) && (Array.isArray(serverFieldValue) || Array.isArray(clientFieldValue))) {
const serverArray = toComparableArray(serverFieldValue);
const clientArray = toComparableArray(clientFieldValue);
if (serverArray && clientArray) {
item.target[fieldKey] = mergeArraysUnique(clientArray, serverArray);
continue;
}
}
if (!Object.prototype.hasOwnProperty.call(item.target, fieldKey)) {

@@ -325,4 +342,4 @@ item.target[fieldKey] = clientFieldValue;

* Enriches a payment payload by calling registered extension hooks.
* For each extension key present in the PaymentRequired response,
* invokes the corresponding extension's enrichPaymentPayload callback.
* Invokes enrichPaymentPayload for every registered extension, then merges
* server-declared extension fields back into the result.
*

@@ -334,8 +351,8 @@ * @param paymentPayload - The payment payload to enrich with extension data

async enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired) {
if (!paymentRequired.extensions || this.registeredExtensions.size === 0) {
if (this.registeredExtensions.size === 0) {
return paymentPayload;
}
let enriched = paymentPayload;
for (const [key, extension] of this.registeredExtensions) {
if (key in paymentRequired.extensions && extension.enrichPaymentPayload) {
for (const [, extension] of this.registeredExtensions) {
if (extension.enrichPaymentPayload) {
enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);

@@ -507,2 +524,11 @@ }

};
function mergeArraysUnique(client, server) {
const merged = [];
for (const item of [...client, ...server]) {
if (!merged.some((existing) => deepEqual(existing, item))) {
merged.push(item);
}
}
return merged;
}
export {

@@ -509,0 +535,0 @@ x402Client,

@@ -1,1 +0,1 @@

{"version":3,"sources":["../../../src/client/x402Client.ts"],"sourcesContent":["import { x402Version } from \"..\";\nimport { SchemeNetworkClient } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network, PaymentRequired, SettleResponse } from \"../types\";\nimport { findByNetworkAndScheme, findSchemesByNetwork } from \"../utils\";\n\n/**\n * Client Hook Context Interfaces\n */\n\nexport interface PaymentCreationContext {\n paymentRequired: PaymentRequired;\n selectedRequirements: PaymentRequirements;\n}\n\nexport interface PaymentCreatedContext extends PaymentCreationContext {\n paymentPayload: PaymentPayload;\n}\n\nexport interface PaymentCreationFailureContext extends PaymentCreationContext {\n error: Error;\n}\n\n/**\n * Client Hook Type Definitions\n */\n\nexport type BeforePaymentCreationHook = (\n context: PaymentCreationContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type AfterPaymentCreationHook = (context: PaymentCreatedContext) => Promise<void>;\n\nexport type OnPaymentCreationFailureHook = (\n context: PaymentCreationFailureContext,\n) => Promise<void | { recovered: true; payload: PaymentPayload }>;\n\n/**\n * Context provided to payment response hooks after the paid request completes.\n *\n * Discriminate by what's present:\n * - `settleResponse` with `success: true` → settle succeeded\n * - `settleResponse` with `success: false` → settle failed\n * - `paymentRequired` (no `settleResponse`) → verify failed\n * - `error` → transport or parse error\n */\nexport interface PaymentResponseContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n settleResponse?: SettleResponse;\n paymentRequired?: PaymentRequired;\n error?: Error;\n}\n\n/**\n * Hook fired after a paid request completes.\n * Return `{ recovered: true }` to signal the transport should retry with a fresh payload.\n */\nexport type OnPaymentResponseHook = (\n ctx: PaymentResponseContext,\n) => Promise<void | { recovered: true }>;\n\nexport type SelectPaymentRequirements = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements;\n\ntype ClientHookAdapterHandles = {\n beforePaymentCreation?: BeforePaymentCreationHook;\n afterPaymentCreation?: AfterPaymentCreationHook;\n onPaymentCreationFailure?: OnPaymentCreationFailureHook;\n onPaymentResponse?: OnPaymentResponseHook;\n};\n\ntype ClientHookPhase = keyof ClientHookAdapterHandles;\n\nexport interface ClientExtensionHooks {\n onBeforePaymentCreation?: (\n declaration: unknown,\n context: PaymentCreationContext,\n ) => Promise<void | { abort: true; reason: string }>;\n onAfterPaymentCreation?: (\n declaration: unknown,\n context: PaymentCreatedContext,\n ) => Promise<void>;\n onPaymentCreationFailure?: (\n declaration: unknown,\n context: PaymentCreationFailureContext,\n ) => Promise<void | { recovered: true; payload: PaymentPayload }>;\n onPaymentResponse?: (\n declaration: unknown,\n context: PaymentResponseContext,\n ) => Promise<void | { recovered: true }>;\n}\n\nexport interface ClientTransportExtensionHooks {\n [transport: string]: unknown;\n}\n\n/**\n * Extension that can enrich payment payloads on the client side.\n *\n * Client extensions are invoked after the scheme creates the base payment payload\n * but before it is returned. This allows mechanism-specific logic (e.g., EVM EIP-2612\n * permit signing) to enrich the payload's extensions data.\n */\nexport interface ClientExtension {\n /**\n * Unique key identifying this extension (e.g., \"eip2612GasSponsoring\").\n * Must match the extension key used in PaymentRequired.extensions.\n */\n key: string;\n\n /**\n * Called after payload creation when the extension key is present in\n * paymentRequired.extensions. Allows the extension to enrich the payload\n * with extension-specific data (e.g., signing an EIP-2612 permit).\n *\n * @param paymentPayload - The payment payload to enrich\n * @param paymentRequired - The original PaymentRequired response\n * @returns The enriched payment payload\n */\n enrichPaymentPayload?: (\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ) => Promise<PaymentPayload>;\n\n hooks?: ClientExtensionHooks;\n transportHooks?: ClientTransportExtensionHooks;\n}\n\n/**\n * A policy function that filters or transforms payment requirements.\n * Policies are applied in order before the selector chooses the final option.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of payment requirements to filter/transform\n * @returns Filtered array of payment requirements\n */\nexport type PaymentPolicy = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];\n\n\n/**\n * Configuration for registering a payment scheme with a specific network\n */\nexport interface SchemeRegistration {\n /**\n * The network identifier (e.g., 'eip155:8453', 'solana:mainnet')\n */\n network: Network;\n\n /**\n * The scheme client implementation for this network\n */\n client: SchemeNetworkClient;\n\n /**\n * The x402 protocol version to use for this scheme\n *\n * @default 2\n */\n x402Version?: number;\n}\n\n/**\n * Configuration options for the fetch wrapper\n */\nexport interface x402ClientConfig {\n /**\n * Array of scheme registrations defining which payment methods are supported\n */\n schemes: SchemeRegistration[];\n\n /**\n * Policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Custom payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n}\n\n/**\n * Core client for managing x402 payment schemes and creating payment payloads.\n *\n * Handles registration of payment schemes, policy-based filtering of payment requirements,\n * and creation of payment payloads based on server requirements.\n */\nexport class x402Client {\n private readonly paymentRequirementsSelector: SelectPaymentRequirements;\n private readonly registeredClientSchemes: Map<number, Map<string, Map<string, SchemeNetworkClient>>> = new Map();\n private readonly schemeClientHookAdapters: Map<number, Map<string, Map<string, ClientHookAdapterHandles>>> = new Map();\n private readonly policies: PaymentPolicy[] = [];\n private readonly registeredExtensions: Map<string, ClientExtension> = new Map();\n\n private beforePaymentCreationHooks: BeforePaymentCreationHook[] = [];\n private afterPaymentCreationHooks: AfterPaymentCreationHook[] = [];\n private onPaymentCreationFailureHooks: OnPaymentCreationFailureHook[] = [];\n private paymentResponseHooks: OnPaymentResponseHook[] = [];\n\n /**\n * Creates a new x402Client instance.\n *\n * @param paymentRequirementsSelector - Function to select payment requirements from available options\n */\n constructor(paymentRequirementsSelector?: SelectPaymentRequirements) {\n this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version, accepts) => accepts[0]);\n }\n\n /**\n * Creates a new x402Client instance from a configuration object.\n *\n * @param config - The client configuration including schemes, policies, and payment requirements selector\n * @returns A configured x402Client instance\n */\n static fromConfig(config: x402ClientConfig): x402Client {\n const client = new x402Client(config.paymentRequirementsSelector);\n config.schemes.forEach(scheme => {\n if (scheme.x402Version === 1) {\n client.registerV1(scheme.network, scheme.client);\n } else {\n client.register(scheme.network, scheme.client);\n }\n });\n config.policies?.forEach(policy => {\n client.registerPolicy(policy);\n });\n return client;\n }\n\n /**\n * Registers a scheme client for the current x402 version.\n *\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n register(network: Network, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(x402Version, network, client);\n }\n\n /**\n * Registers a scheme client for x402 version 1.\n *\n * @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n registerV1(network: string, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(1, network as Network, client);\n }\n\n /**\n * Registers a policy to filter or transform payment requirements.\n *\n * Policies are applied in order after filtering by registered schemes\n * and before the selector chooses the final payment requirement.\n *\n * @param policy - Function to filter/transform payment requirements\n * @returns The x402Client instance for chaining\n *\n * @example\n * ```typescript\n * // Prefer cheaper options\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))\n * );\n *\n * // Prefer specific networks\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => r.network.startsWith('eip155:'))\n * );\n * ```\n */\n registerPolicy(policy: PaymentPolicy): x402Client {\n this.policies.push(policy);\n return this;\n }\n\n /**\n * Registers a client extension that can enrich payment payloads.\n *\n * Extensions are invoked after the scheme creates the base payload and the\n * payload is wrapped with extensions/resource/accepted data. If the extension's\n * key is present in `paymentRequired.extensions`, the extension's\n * `enrichPaymentPayload` hook is called to modify the payload.\n *\n * @param extension - The client extension to register\n * @returns The x402Client instance for chaining\n */\n registerExtension(extension: ClientExtension): x402Client {\n this.registeredExtensions.set(extension.key, extension);\n return this;\n }\n\n /**\n * Get all registered client extensions.\n *\n * @returns Array of registered extensions\n */\n getExtensions(): ClientExtension[] {\n return Array.from(this.registeredExtensions.values());\n }\n\n /**\n * Register a hook to execute before payment payload creation.\n * Can abort creation by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client {\n this.beforePaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful payment payload creation.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client {\n this.afterPaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when payment payload creation fails.\n * Can recover from failure by returning { recovered: true, payload: PaymentPayload }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client {\n this.onPaymentCreationFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after a paid request completes.\n * Can signal recovery by returning { recovered: true }, causing the transport to retry.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentResponse(hook: OnPaymentResponseHook): x402Client {\n this.paymentResponseHooks.push(hook);\n return this;\n }\n\n /**\n * Fires all registered payment response hooks in order.\n * Returns `{ recovered: true }` if any hook signals recovery (first wins).\n *\n * @param ctx - The payment response context\n * @returns Recovery signal or undefined\n */\n async handlePaymentResponse(\n ctx: PaymentResponseContext,\n ): Promise<{ recovered: true } | undefined> {\n for (const hook of this.getLabeledHooks(\n \"onPaymentResponse\",\n ctx.paymentPayload.x402Version,\n ctx.requirements,\n ctx.paymentRequired?.extensions ?? ctx.paymentPayload.extensions,\n )) {\n const result = await hook(ctx);\n if (result && \"recovered\" in result && result.recovered) {\n return { recovered: true };\n }\n }\n return undefined;\n }\n\n /**\n * Creates a payment payload based on a PaymentRequired response.\n *\n * Automatically extracts x402Version, resource, and extensions from the PaymentRequired\n * response and constructs a complete PaymentPayload with the accepted requirements.\n *\n * @param paymentRequired - The PaymentRequired response from the server\n * @returns Promise resolving to the complete payment payload\n */\n async createPaymentPayload(\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);\n }\n\n const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);\n\n const context: PaymentCreationContext = {\n paymentRequired,\n selectedRequirements: requirements,\n };\n\n for (const hook of this.getLabeledHooks(\n \"beforePaymentCreation\",\n paymentRequired.x402Version,\n requirements,\n paymentRequired.extensions,\n )) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Payment creation aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);\n if (!schemeNetworkClient) {\n throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);\n }\n\n const partialPayload = await schemeNetworkClient.createPaymentPayload(\n paymentRequired.x402Version,\n requirements,\n { extensions: paymentRequired.extensions },\n );\n\n let paymentPayload: PaymentPayload;\n if (partialPayload.x402Version == 1) {\n paymentPayload = partialPayload as PaymentPayload;\n } else {\n // Merge server-declared extensions with any scheme-provided extensions.\n // Scheme extensions overlay on top (e.g., EIP-2612 info enriches server declaration).\n const mergedExtensions = this.mergeExtensions(\n paymentRequired.extensions,\n partialPayload.extensions,\n );\n\n paymentPayload = {\n x402Version: partialPayload.x402Version,\n payload: partialPayload.payload,\n extensions: mergedExtensions,\n resource: paymentRequired.resource,\n accepted: requirements,\n };\n }\n\n // Enrich payload via registered client extensions (for non-scheme extensions)\n paymentPayload = await this.enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired);\n\n const createdContext: PaymentCreatedContext = {\n ...context,\n paymentPayload,\n };\n\n for (const hook of this.getLabeledHooks(\n \"afterPaymentCreation\",\n paymentRequired.x402Version,\n requirements,\n paymentRequired.extensions,\n )) {\n await hook(createdContext);\n }\n\n return paymentPayload;\n } catch (error) {\n const failureContext: PaymentCreationFailureContext = {\n ...context,\n error: error as Error,\n };\n\n for (const hook of this.getLabeledHooks(\n \"onPaymentCreationFailure\",\n paymentRequired.x402Version,\n requirements,\n paymentRequired.extensions,\n )) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.payload;\n }\n }\n\n throw error;\n }\n }\n\n\n\n /**\n * Merges server-declared extensions with client extension echoes.\n * Client extension data may add fields, but server-declared fields remain intact.\n *\n * @param serverExtensions - Extensions declared by the server in the 402 response\n * @param clientExtensions - Extensions provided by the client or scheme\n * @returns The merged extensions object, or undefined if both inputs are undefined\n */\n private mergeExtensions(\n serverExtensions?: Record<string, unknown>,\n clientExtensions?: Record<string, unknown>,\n ): Record<string, unknown> | undefined {\n if (!clientExtensions) return serverExtensions;\n if (!serverExtensions) return clientExtensions;\n\n const merged = { ...serverExtensions };\n for (const [key, clientValue] of Object.entries(clientExtensions)) {\n const serverValue = merged[key];\n if (\n serverValue === null ||\n typeof serverValue !== \"object\" ||\n Array.isArray(serverValue) ||\n clientValue === null ||\n typeof clientValue !== \"object\" ||\n Array.isArray(clientValue)\n ) {\n merged[key] = clientValue;\n continue;\n }\n\n const serverRecord = serverValue as Record<string, unknown>;\n const clientRecord = clientValue as Record<string, unknown>;\n const extensionValue = { ...serverRecord };\n const pending = [{ target: extensionValue, source: clientRecord }];\n for (const item of pending) {\n for (const [fieldKey, clientFieldValue] of Object.entries(item.source)) {\n const serverFieldValue = item.target[fieldKey];\n if (\n serverFieldValue !== null &&\n typeof serverFieldValue === \"object\" &&\n !Array.isArray(serverFieldValue) &&\n clientFieldValue !== null &&\n typeof clientFieldValue === \"object\" &&\n !Array.isArray(clientFieldValue)\n ) {\n const nestedValue = { ...(serverFieldValue as Record<string, unknown>) };\n item.target[fieldKey] = nestedValue;\n pending.push({\n target: nestedValue,\n source: clientFieldValue as Record<string, unknown>,\n });\n continue;\n }\n\n if (!Object.prototype.hasOwnProperty.call(item.target, fieldKey)) {\n item.target[fieldKey] = clientFieldValue;\n }\n }\n }\n\n merged[key] = extensionValue;\n }\n return merged;\n }\n\n /**\n * Enriches a payment payload by calling registered extension hooks.\n * For each extension key present in the PaymentRequired response,\n * invokes the corresponding extension's enrichPaymentPayload callback.\n *\n * @param paymentPayload - The payment payload to enrich with extension data\n * @param paymentRequired - The PaymentRequired response containing extension declarations\n * @returns The enriched payment payload with extension data applied\n */\n private async enrichPaymentPayloadWithExtensions(\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n if (!paymentRequired.extensions || this.registeredExtensions.size === 0) {\n return paymentPayload;\n }\n\n let enriched = paymentPayload;\n for (const [key, extension] of this.registeredExtensions) {\n if (key in paymentRequired.extensions && extension.enrichPaymentPayload) {\n enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);\n }\n }\n\n return {\n ...enriched,\n extensions: this.mergeExtensions(paymentRequired.extensions, enriched.extensions),\n };\n }\n\n /**\n * Selects appropriate payment requirements based on registered clients and policies.\n *\n * Selection process:\n * 1. Filter by registered schemes (network + scheme support)\n * 2. Apply all registered policies in order\n * 3. Use selector to choose final requirement\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of available payment requirements\n * @returns The selected payment requirements\n */\n private selectPaymentRequirements(x402Version: number, paymentRequirements: PaymentRequirements[]): PaymentRequirements {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${x402Version}`);\n }\n\n // Step 1: Filter by registered schemes\n const supportedPaymentRequirements = paymentRequirements.filter(requirement => {\n let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);\n if (!clientSchemes) {\n return false;\n }\n\n return clientSchemes.has(requirement.scheme);\n })\n\n if (supportedPaymentRequirements.length === 0) {\n throw new Error(`No network/scheme registered for x402 version: ${x402Version} which comply with the payment requirements. ${JSON.stringify({\n x402Version,\n paymentRequirements,\n x402Versions: Array.from(this.registeredClientSchemes.keys()),\n networks: Array.from(clientSchemesByNetwork.keys()),\n schemes: Array.from(clientSchemesByNetwork.values()).map(schemes => Array.from(schemes.keys())).flat(),\n })}`);\n }\n\n // Step 2: Apply all policies in order\n let filteredRequirements = supportedPaymentRequirements;\n for (const policy of this.policies) {\n filteredRequirements = policy(x402Version, filteredRequirements);\n\n if (filteredRequirements.length === 0) {\n throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version}`);\n }\n }\n\n // Step 3: Use selector to choose final requirement\n return this.paymentRequirementsSelector(x402Version, filteredRequirements);\n }\n\n /**\n * Internal method to register a scheme client.\n *\n * @param x402Version - The x402 protocol version\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n private _registerScheme(x402Version: number, network: Network, client: SchemeNetworkClient): x402Client {\n if (!this.registeredClientSchemes.has(x402Version)) {\n this.registeredClientSchemes.set(x402Version, new Map());\n }\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version)!;\n if (!clientSchemesByNetwork.has(network)) {\n clientSchemesByNetwork.set(network, new Map());\n }\n\n const clientByScheme = clientSchemesByNetwork.get(network)!;\n clientByScheme.set(client.scheme, client);\n\n if (!this.schemeClientHookAdapters.has(x402Version)) {\n this.schemeClientHookAdapters.set(x402Version, new Map());\n }\n const adaptersByNetwork = this.schemeClientHookAdapters.get(x402Version)!;\n if (!adaptersByNetwork.has(network)) {\n adaptersByNetwork.set(network, new Map());\n }\n\n const adaptersByScheme = adaptersByNetwork.get(network)!;\n const hooks = client.schemeHooks;\n if (!hooks) {\n adaptersByScheme.delete(client.scheme);\n return this;\n }\n\n const handles: ClientHookAdapterHandles = {};\n if (hooks.onBeforePaymentCreation) {\n handles.beforePaymentCreation = hooks.onBeforePaymentCreation;\n }\n if (hooks.onAfterPaymentCreation) {\n handles.afterPaymentCreation = hooks.onAfterPaymentCreation;\n }\n if (hooks.onPaymentCreationFailure) {\n handles.onPaymentCreationFailure = hooks.onPaymentCreationFailure;\n }\n if (hooks.onPaymentResponse) {\n handles.onPaymentResponse = hooks.onPaymentResponse;\n }\n\n if (Object.keys(handles).length > 0) {\n adaptersByScheme.set(client.scheme, handles);\n } else {\n adaptersByScheme.delete(client.scheme);\n }\n\n return this;\n }\n\n /**\n * Returns manual hooks followed by the selected scheme hook and declared extension hooks.\n *\n * @param phase - Hook slot to collect\n * @param x402Version - Protocol version for the selected requirement\n * @param requirements - Selected payment requirement\n * @param declaredExtensions - Extension declarations that scope extension hooks\n * @returns Hooks in invocation order\n */\n private getLabeledHooks<P extends ClientHookPhase>(\n phase: P,\n x402Version: number,\n requirements: PaymentRequirements,\n declaredExtensions?: Record<string, unknown>,\n ): Array<NonNullable<ClientHookAdapterHandles[P]>> {\n let manual: Array<NonNullable<ClientHookAdapterHandles[P]>>;\n switch (phase) {\n case \"beforePaymentCreation\":\n manual = this.beforePaymentCreationHooks as Array<\n NonNullable<ClientHookAdapterHandles[P]>\n >;\n break;\n case \"afterPaymentCreation\":\n manual = this.afterPaymentCreationHooks as Array<\n NonNullable<ClientHookAdapterHandles[P]>\n >;\n break;\n case \"onPaymentCreationFailure\":\n manual = this.onPaymentCreationFailureHooks as Array<\n NonNullable<ClientHookAdapterHandles[P]>\n >;\n break;\n case \"onPaymentResponse\":\n manual = this.paymentResponseHooks as Array<NonNullable<ClientHookAdapterHandles[P]>>;\n break;\n }\n\n const out: Array<NonNullable<ClientHookAdapterHandles[P]>> = [...manual];\n const adaptersByNetwork = this.schemeClientHookAdapters.get(x402Version);\n const schemeAdapter = adaptersByNetwork\n ? findByNetworkAndScheme(adaptersByNetwork, requirements.scheme, requirements.network)\n : undefined;\n const hook = schemeAdapter?.[phase];\n if (hook !== undefined) {\n out.push(hook);\n }\n if (!declaredExtensions) {\n return out;\n }\n\n const extensionHookKey = this.getClientExtensionHookKey(phase);\n for (const [extensionKey, extension] of this.registeredExtensions) {\n if (!(extensionKey in declaredExtensions)) continue;\n\n const extensionHook = extension.hooks?.[extensionHookKey];\n if (!extensionHook) continue;\n\n type HookFn = NonNullable<ClientHookAdapterHandles[P]>;\n type HookContext = Parameters<HookFn>[0];\n out.push((async (ctx: HookContext) => {\n return (\n extensionHook as (\n declaration: unknown,\n context: HookContext,\n ) => ReturnType<HookFn>\n )(declaredExtensions[extensionKey], ctx);\n }) as HookFn);\n }\n return out;\n }\n\n /**\n * Maps internal hook phases to extension hook names.\n *\n * @param phase - Internal hook phase\n * @returns Extension hook key for the phase\n */\n private getClientExtensionHookKey<P extends ClientHookPhase>(\n phase: P,\n ): keyof ClientExtensionHooks {\n switch (phase) {\n case \"beforePaymentCreation\":\n return \"onBeforePaymentCreation\";\n case \"afterPaymentCreation\":\n return \"onAfterPaymentCreation\";\n case \"onPaymentCreationFailure\":\n return \"onPaymentCreationFailure\";\n case \"onPaymentResponse\":\n return \"onPaymentResponse\";\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA4LO,IAAM,aAAN,MAAM,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBtB,YAAY,6BAAyD;AAfrE,SAAiB,0BAAsF,oBAAI,IAAI;AAC/G,SAAiB,2BAA4F,oBAAI,IAAI;AACrH,SAAiB,WAA4B,CAAC;AAC9C,SA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J,UAAM,OAAO,gBAAgB,KAAK;AAClC,QAAI,SAAS,QAAW;AACtB,UAAI,KAAK,IAAI;AAAA,IACf;AACA,QAAI,CAAC,oBAAoB;AACvB,aAAO;AAAA,IACT;AAEA,UAAM,mBAAmB,KAAK,0BAA0B,KAAK;AAC7D,eAAW,CAAC,cAAc,SAAS,KAAK,KAAK,sBAAsB;AACjE,UAAI,EAAE,gBAAgB,oBAAqB;AAE3C,YAAM,gBAAgB,UAAU,QAAQ,gBAAgB;AACxD,UAAI,CAAC,cAAe;AAIpB,UAAI,MAAM,OAAO,QAAqB;AACpC,eACE,cAIA,mBAAmB,YAAY,GAAG,GAAG;AAAA,MACzC,EAAY;AAAA,IACd;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,0BACN,OAC4B;AAC5B,YAAQ,OAAO;AAAA,MACb,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,IACX;AAAA,EACF;AACF;","names":["x402Version"]}
{"version":3,"sources":["../../../src/client/x402Client.ts"],"sourcesContent":["import { x402Version } from \"..\";\nimport { SchemeNetworkClient } from \"../types/mechanisms\";\nimport { PaymentPayload, PaymentRequirements } from \"../types/payments\";\nimport { Network, PaymentRequired, SettleResponse } from \"../types\";\nimport {\n ADDITIVE_ARRAY_INFO_FIELDS,\n deepEqual,\n findByNetworkAndScheme,\n findSchemesByNetwork,\n toComparableArray,\n} from \"../utils\";\n\n/**\n * Client Hook Context Interfaces\n */\n\nexport interface PaymentCreationContext {\n paymentRequired: PaymentRequired;\n selectedRequirements: PaymentRequirements;\n}\n\nexport interface PaymentCreatedContext extends PaymentCreationContext {\n paymentPayload: PaymentPayload;\n}\n\nexport interface PaymentCreationFailureContext extends PaymentCreationContext {\n error: Error;\n}\n\n/**\n * Client Hook Type Definitions\n */\n\nexport type BeforePaymentCreationHook = (\n context: PaymentCreationContext,\n) => Promise<void | { abort: true; reason: string }>;\n\nexport type AfterPaymentCreationHook = (context: PaymentCreatedContext) => Promise<void>;\n\nexport type OnPaymentCreationFailureHook = (\n context: PaymentCreationFailureContext,\n) => Promise<void | { recovered: true; payload: PaymentPayload }>;\n\n/**\n * Context provided to payment response hooks after the paid request completes.\n *\n * Discriminate by what's present:\n * - `settleResponse` with `success: true` → settle succeeded\n * - `settleResponse` with `success: false` → settle failed\n * - `paymentRequired` (no `settleResponse`) → verify failed\n * - `error` → transport or parse error\n */\nexport interface PaymentResponseContext {\n paymentPayload: PaymentPayload;\n requirements: PaymentRequirements;\n settleResponse?: SettleResponse;\n paymentRequired?: PaymentRequired;\n error?: Error;\n}\n\n/**\n * Hook fired after a paid request completes.\n * Return `{ recovered: true }` to signal the transport should retry with a fresh payload.\n */\nexport type OnPaymentResponseHook = (\n ctx: PaymentResponseContext,\n) => Promise<void | { recovered: true }>;\n\nexport type SelectPaymentRequirements = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements;\n\ntype ClientHookAdapterHandles = {\n beforePaymentCreation?: BeforePaymentCreationHook;\n afterPaymentCreation?: AfterPaymentCreationHook;\n onPaymentCreationFailure?: OnPaymentCreationFailureHook;\n onPaymentResponse?: OnPaymentResponseHook;\n};\n\ntype ClientHookPhase = keyof ClientHookAdapterHandles;\n\nexport interface ClientExtensionHooks {\n onBeforePaymentCreation?: (\n declaration: unknown,\n context: PaymentCreationContext,\n ) => Promise<void | { abort: true; reason: string }>;\n onAfterPaymentCreation?: (\n declaration: unknown,\n context: PaymentCreatedContext,\n ) => Promise<void>;\n onPaymentCreationFailure?: (\n declaration: unknown,\n context: PaymentCreationFailureContext,\n ) => Promise<void | { recovered: true; payload: PaymentPayload }>;\n onPaymentResponse?: (\n declaration: unknown,\n context: PaymentResponseContext,\n ) => Promise<void | { recovered: true }>;\n}\n\nexport interface ClientTransportExtensionHooks {\n [transport: string]: unknown;\n}\n\n/**\n * Extension that can enrich payment payloads on the client side.\n *\n * Client extensions are invoked after the scheme creates the base payment payload\n * but before it is returned. This allows mechanism-specific logic (e.g., EVM EIP-2612\n * permit signing) to enrich the payload's extensions data.\n */\nexport interface ClientExtension {\n /**\n * Unique key identifying this extension (e.g., \"eip2612GasSponsoring\").\n * Must match the extension key used in PaymentRequired.extensions.\n */\n key: string;\n\n /**\n * Called after payload creation for every registered extension. Allows the\n * extension to enrich the payload with extension-specific data. Extensions\n * that require a server declaration must no-op internally when the server\n * did not advertise them in paymentRequired.extensions.\n *\n * @param paymentPayload - The payment payload to enrich\n * @param paymentRequired - The original PaymentRequired response\n * @returns The enriched payment payload\n */\n enrichPaymentPayload?: (\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ) => Promise<PaymentPayload>;\n\n hooks?: ClientExtensionHooks;\n transportHooks?: ClientTransportExtensionHooks;\n}\n\n/**\n * A policy function that filters or transforms payment requirements.\n * Policies are applied in order before the selector chooses the final option.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of payment requirements to filter/transform\n * @returns Filtered array of payment requirements\n */\nexport type PaymentPolicy = (x402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];\n\n\n/**\n * Configuration for registering a payment scheme with a specific network\n */\nexport interface SchemeRegistration {\n /**\n * The network identifier (e.g., 'eip155:8453', 'solana:mainnet')\n */\n network: Network;\n\n /**\n * The scheme client implementation for this network\n */\n client: SchemeNetworkClient;\n\n /**\n * The x402 protocol version to use for this scheme\n *\n * @default 2\n */\n x402Version?: number;\n}\n\n/**\n * Configuration options for the fetch wrapper\n */\nexport interface x402ClientConfig {\n /**\n * Array of scheme registrations defining which payment methods are supported\n */\n schemes: SchemeRegistration[];\n\n /**\n * Policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Custom payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n}\n\n/**\n * Core client for managing x402 payment schemes and creating payment payloads.\n *\n * Handles registration of payment schemes, policy-based filtering of payment requirements,\n * and creation of payment payloads based on server requirements.\n */\nexport class x402Client {\n private readonly paymentRequirementsSelector: SelectPaymentRequirements;\n private readonly registeredClientSchemes: Map<number, Map<string, Map<string, SchemeNetworkClient>>> = new Map();\n private readonly schemeClientHookAdapters: Map<number, Map<string, Map<string, ClientHookAdapterHandles>>> = new Map();\n private readonly policies: PaymentPolicy[] = [];\n private readonly registeredExtensions: Map<string, ClientExtension> = new Map();\n\n private beforePaymentCreationHooks: BeforePaymentCreationHook[] = [];\n private afterPaymentCreationHooks: AfterPaymentCreationHook[] = [];\n private onPaymentCreationFailureHooks: OnPaymentCreationFailureHook[] = [];\n private paymentResponseHooks: OnPaymentResponseHook[] = [];\n\n /**\n * Creates a new x402Client instance.\n *\n * @param paymentRequirementsSelector - Function to select payment requirements from available options\n */\n constructor(paymentRequirementsSelector?: SelectPaymentRequirements) {\n this.paymentRequirementsSelector = paymentRequirementsSelector || ((x402Version, accepts) => accepts[0]);\n }\n\n /**\n * Creates a new x402Client instance from a configuration object.\n *\n * @param config - The client configuration including schemes, policies, and payment requirements selector\n * @returns A configured x402Client instance\n */\n static fromConfig(config: x402ClientConfig): x402Client {\n const client = new x402Client(config.paymentRequirementsSelector);\n config.schemes.forEach(scheme => {\n if (scheme.x402Version === 1) {\n client.registerV1(scheme.network, scheme.client);\n } else {\n client.register(scheme.network, scheme.client);\n }\n });\n config.policies?.forEach(policy => {\n client.registerPolicy(policy);\n });\n return client;\n }\n\n /**\n * Registers a scheme client for the current x402 version.\n *\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n register(network: Network, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(x402Version, network, client);\n }\n\n /**\n * Registers a scheme client for x402 version 1.\n *\n * @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n registerV1(network: string, client: SchemeNetworkClient): x402Client {\n return this._registerScheme(1, network as Network, client);\n }\n\n /**\n * Registers a policy to filter or transform payment requirements.\n *\n * Policies are applied in order after filtering by registered schemes\n * and before the selector chooses the final payment requirement.\n *\n * @param policy - Function to filter/transform payment requirements\n * @returns The x402Client instance for chaining\n *\n * @example\n * ```typescript\n * // Prefer cheaper options\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => BigInt(r.value) < BigInt('1000000'))\n * );\n *\n * // Prefer specific networks\n * client.registerPolicy((version, reqs) =>\n * reqs.filter(r => r.network.startsWith('eip155:'))\n * );\n * ```\n */\n registerPolicy(policy: PaymentPolicy): x402Client {\n this.policies.push(policy);\n return this;\n }\n\n /**\n * Registers a client extension that can enrich payment payloads.\n *\n * Extensions are invoked after the scheme creates the base payload and the\n * payload is wrapped with extensions/resource/accepted data. Every registered\n * extension's `enrichPaymentPayload` hook is called to modify the payload.\n * Server-declared fields are preserved via merge after enrichment.\n *\n * @param extension - The client extension to register\n * @returns The x402Client instance for chaining\n */\n registerExtension(extension: ClientExtension): x402Client {\n this.registeredExtensions.set(extension.key, extension);\n return this;\n }\n\n /**\n * Get all registered client extensions.\n *\n * @returns Array of registered extensions\n */\n getExtensions(): ClientExtension[] {\n return Array.from(this.registeredExtensions.values());\n }\n\n /**\n * Register a hook to execute before payment payload creation.\n * Can abort creation by returning { abort: true, reason: string }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onBeforePaymentCreation(hook: BeforePaymentCreationHook): x402Client {\n this.beforePaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after successful payment payload creation.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onAfterPaymentCreation(hook: AfterPaymentCreationHook): x402Client {\n this.afterPaymentCreationHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute when payment payload creation fails.\n * Can recover from failure by returning { recovered: true, payload: PaymentPayload }\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): x402Client {\n this.onPaymentCreationFailureHooks.push(hook);\n return this;\n }\n\n /**\n * Register a hook to execute after a paid request completes.\n * Can signal recovery by returning { recovered: true }, causing the transport to retry.\n *\n * @param hook - The hook function to register\n * @returns The x402Client instance for chaining\n */\n onPaymentResponse(hook: OnPaymentResponseHook): x402Client {\n this.paymentResponseHooks.push(hook);\n return this;\n }\n\n /**\n * Fires all registered payment response hooks in order.\n * Returns `{ recovered: true }` if any hook signals recovery (first wins).\n *\n * @param ctx - The payment response context\n * @returns Recovery signal or undefined\n */\n async handlePaymentResponse(\n ctx: PaymentResponseContext,\n ): Promise<{ recovered: true } | undefined> {\n for (const hook of this.getLabeledHooks(\n \"onPaymentResponse\",\n ctx.paymentPayload.x402Version,\n ctx.requirements,\n ctx.paymentRequired?.extensions ?? ctx.paymentPayload.extensions,\n )) {\n const result = await hook(ctx);\n if (result && \"recovered\" in result && result.recovered) {\n return { recovered: true };\n }\n }\n return undefined;\n }\n\n /**\n * Creates a payment payload based on a PaymentRequired response.\n *\n * Automatically extracts x402Version, resource, and extensions from the PaymentRequired\n * response and constructs a complete PaymentPayload with the accepted requirements.\n *\n * @param paymentRequired - The PaymentRequired response from the server\n * @returns Promise resolving to the complete payment payload\n */\n async createPaymentPayload(\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(paymentRequired.x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${paymentRequired.x402Version}`);\n }\n\n const requirements = this.selectPaymentRequirements(paymentRequired.x402Version, paymentRequired.accepts);\n\n const context: PaymentCreationContext = {\n paymentRequired,\n selectedRequirements: requirements,\n };\n\n for (const hook of this.getLabeledHooks(\n \"beforePaymentCreation\",\n paymentRequired.x402Version,\n requirements,\n paymentRequired.extensions,\n )) {\n const result = await hook(context);\n if (result && \"abort\" in result && result.abort) {\n throw new Error(`Payment creation aborted: ${result.reason}`);\n }\n }\n\n try {\n const schemeNetworkClient = findByNetworkAndScheme(clientSchemesByNetwork, requirements.scheme, requirements.network);\n if (!schemeNetworkClient) {\n throw new Error(`No client registered for scheme: ${requirements.scheme} and network: ${requirements.network}`);\n }\n\n const partialPayload = await schemeNetworkClient.createPaymentPayload(\n paymentRequired.x402Version,\n requirements,\n { extensions: paymentRequired.extensions },\n );\n\n let paymentPayload: PaymentPayload;\n if (partialPayload.x402Version == 1) {\n paymentPayload = partialPayload as PaymentPayload;\n } else {\n // Merge server-declared extensions with any scheme-provided extensions.\n // Scheme extensions overlay on top (e.g., EIP-2612 info enriches server declaration).\n const mergedExtensions = this.mergeExtensions(\n paymentRequired.extensions,\n partialPayload.extensions,\n );\n\n paymentPayload = {\n x402Version: partialPayload.x402Version,\n payload: partialPayload.payload,\n extensions: mergedExtensions,\n resource: paymentRequired.resource,\n accepted: requirements,\n };\n }\n\n // Enrich payload via registered client extensions (for non-scheme extensions)\n paymentPayload = await this.enrichPaymentPayloadWithExtensions(paymentPayload, paymentRequired);\n\n const createdContext: PaymentCreatedContext = {\n ...context,\n paymentPayload,\n };\n\n for (const hook of this.getLabeledHooks(\n \"afterPaymentCreation\",\n paymentRequired.x402Version,\n requirements,\n paymentRequired.extensions,\n )) {\n await hook(createdContext);\n }\n\n return paymentPayload;\n } catch (error) {\n const failureContext: PaymentCreationFailureContext = {\n ...context,\n error: error as Error,\n };\n\n for (const hook of this.getLabeledHooks(\n \"onPaymentCreationFailure\",\n paymentRequired.x402Version,\n requirements,\n paymentRequired.extensions,\n )) {\n const result = await hook(failureContext);\n if (result && \"recovered\" in result && result.recovered) {\n return result.payload;\n }\n }\n\n throw error;\n }\n }\n\n\n\n /**\n * Merges server-declared extensions with client extension echoes.\n * Client extension data may add fields, but server-declared fields remain intact.\n * For fields listed in `ADDITIVE_ARRAY_INFO_FIELDS` (e.g. builder-code `s`), a\n * conflicting array is concatenated with client entries first (so a downstream\n * length cap trims server entries rather than the client's) and duplicates\n * removed; a scalar on either side is treated as a single-element array. Every\n * other conflicting array keeps the server's value, same as any other scalar.\n *\n * @param serverExtensions - Extensions declared by the server in the 402 response\n * @param clientExtensions - Extensions provided by the client or scheme\n * @returns The merged extensions object, or undefined if both inputs are undefined\n */\n private mergeExtensions(\n serverExtensions?: Record<string, unknown>,\n clientExtensions?: Record<string, unknown>,\n ): Record<string, unknown> | undefined {\n if (!clientExtensions) return serverExtensions;\n if (!serverExtensions) return clientExtensions;\n\n const merged = { ...serverExtensions };\n for (const [key, clientValue] of Object.entries(clientExtensions)) {\n const serverValue = merged[key];\n if (\n serverValue === null ||\n typeof serverValue !== \"object\" ||\n Array.isArray(serverValue) ||\n clientValue === null ||\n typeof clientValue !== \"object\" ||\n Array.isArray(clientValue)\n ) {\n merged[key] = clientValue;\n continue;\n }\n\n const serverRecord = serverValue as Record<string, unknown>;\n const clientRecord = clientValue as Record<string, unknown>;\n const additiveFields = ADDITIVE_ARRAY_INFO_FIELDS[key];\n const extensionValue = { ...serverRecord };\n const pending = [{ target: extensionValue, source: clientRecord }];\n for (const item of pending) {\n for (const [fieldKey, clientFieldValue] of Object.entries(item.source)) {\n const serverFieldValue = item.target[fieldKey];\n if (\n serverFieldValue !== null &&\n typeof serverFieldValue === \"object\" &&\n !Array.isArray(serverFieldValue) &&\n clientFieldValue !== null &&\n typeof clientFieldValue === \"object\" &&\n !Array.isArray(clientFieldValue)\n ) {\n const nestedValue = { ...(serverFieldValue as Record<string, unknown>) };\n item.target[fieldKey] = nestedValue;\n pending.push({\n target: nestedValue,\n source: clientFieldValue as Record<string, unknown>,\n });\n continue;\n }\n\n // A scalar on one side (e.g. builder-code `s` sent as a bare string)\n // merges as a single-element array against an array on the other side.\n // Only additive fields concatenate; other conflicting arrays keep the\n // server's value below, same as any other scalar leaf field.\n if (\n additiveFields?.has(fieldKey) &&\n (Array.isArray(serverFieldValue) || Array.isArray(clientFieldValue))\n ) {\n const serverArray = toComparableArray(serverFieldValue);\n const clientArray = toComparableArray(clientFieldValue);\n if (serverArray && clientArray) {\n item.target[fieldKey] = mergeArraysUnique(clientArray, serverArray);\n continue;\n }\n }\n\n if (!Object.prototype.hasOwnProperty.call(item.target, fieldKey)) {\n item.target[fieldKey] = clientFieldValue;\n }\n }\n }\n\n merged[key] = extensionValue;\n }\n return merged;\n }\n\n /**\n * Enriches a payment payload by calling registered extension hooks.\n * Invokes enrichPaymentPayload for every registered extension, then merges\n * server-declared extension fields back into the result.\n *\n * @param paymentPayload - The payment payload to enrich with extension data\n * @param paymentRequired - The PaymentRequired response containing extension declarations\n * @returns The enriched payment payload with extension data applied\n */\n private async enrichPaymentPayloadWithExtensions(\n paymentPayload: PaymentPayload,\n paymentRequired: PaymentRequired,\n ): Promise<PaymentPayload> {\n if (this.registeredExtensions.size === 0) {\n return paymentPayload;\n }\n\n let enriched = paymentPayload;\n for (const [, extension] of this.registeredExtensions) {\n if (extension.enrichPaymentPayload) {\n enriched = await extension.enrichPaymentPayload(enriched, paymentRequired);\n }\n }\n\n return {\n ...enriched,\n extensions: this.mergeExtensions(paymentRequired.extensions, enriched.extensions),\n };\n }\n\n /**\n * Selects appropriate payment requirements based on registered clients and policies.\n *\n * Selection process:\n * 1. Filter by registered schemes (network + scheme support)\n * 2. Apply all registered policies in order\n * 3. Use selector to choose final requirement\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - Array of available payment requirements\n * @returns The selected payment requirements\n */\n private selectPaymentRequirements(x402Version: number, paymentRequirements: PaymentRequirements[]): PaymentRequirements {\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version);\n if (!clientSchemesByNetwork) {\n throw new Error(`No client registered for x402 version: ${x402Version}`);\n }\n\n // Step 1: Filter by registered schemes\n const supportedPaymentRequirements = paymentRequirements.filter(requirement => {\n let clientSchemes = findSchemesByNetwork(clientSchemesByNetwork, requirement.network);\n if (!clientSchemes) {\n return false;\n }\n\n return clientSchemes.has(requirement.scheme);\n })\n\n if (supportedPaymentRequirements.length === 0) {\n throw new Error(`No network/scheme registered for x402 version: ${x402Version} which comply with the payment requirements. ${JSON.stringify({\n x402Version,\n paymentRequirements,\n x402Versions: Array.from(this.registeredClientSchemes.keys()),\n networks: Array.from(clientSchemesByNetwork.keys()),\n schemes: Array.from(clientSchemesByNetwork.values()).map(schemes => Array.from(schemes.keys())).flat(),\n })}`);\n }\n\n // Step 2: Apply all policies in order\n let filteredRequirements = supportedPaymentRequirements;\n for (const policy of this.policies) {\n filteredRequirements = policy(x402Version, filteredRequirements);\n\n if (filteredRequirements.length === 0) {\n throw new Error(`All payment requirements were filtered out by policies for x402 version: ${x402Version}`);\n }\n }\n\n // Step 3: Use selector to choose final requirement\n return this.paymentRequirementsSelector(x402Version, filteredRequirements);\n }\n\n /**\n * Internal method to register a scheme client.\n *\n * @param x402Version - The x402 protocol version\n * @param network - The network to register the client for\n * @param client - The scheme network client to register\n * @returns The x402Client instance for chaining\n */\n private _registerScheme(x402Version: number, network: Network, client: SchemeNetworkClient): x402Client {\n if (!this.registeredClientSchemes.has(x402Version)) {\n this.registeredClientSchemes.set(x402Version, new Map());\n }\n const clientSchemesByNetwork = this.registeredClientSchemes.get(x402Version)!;\n if (!clientSchemesByNetwork.has(network)) {\n clientSchemesByNetwork.set(network, new Map());\n }\n\n const clientByScheme = clientSchemesByNetwork.get(network)!;\n clientByScheme.set(client.scheme, client);\n\n if (!this.schemeClientHookAdapters.has(x402Version)) {\n this.schemeClientHookAdapters.set(x402Version, new Map());\n }\n const adaptersByNetwork = this.schemeClientHookAdapters.get(x402Version)!;\n if (!adaptersByNetwork.has(network)) {\n adaptersByNetwork.set(network, new Map());\n }\n\n const adaptersByScheme = adaptersByNetwork.get(network)!;\n const hooks = client.schemeHooks;\n if (!hooks) {\n adaptersByScheme.delete(client.scheme);\n return this;\n }\n\n const handles: ClientHookAdapterHandles = {};\n if (hooks.onBeforePaymentCreation) {\n handles.beforePaymentCreation = hooks.onBeforePaymentCreation;\n }\n if (hooks.onAfterPaymentCreation) {\n handles.afterPaymentCreation = hooks.onAfterPaymentCreation;\n }\n if (hooks.onPaymentCreationFailure) {\n handles.onPaymentCreationFailure = hooks.onPaymentCreationFailure;\n }\n if (hooks.onPaymentResponse) {\n handles.onPaymentResponse = hooks.onPaymentResponse;\n }\n\n if (Object.keys(handles).length > 0) {\n adaptersByScheme.set(client.scheme, handles);\n } else {\n adaptersByScheme.delete(client.scheme);\n }\n\n return this;\n }\n\n /**\n * Returns manual hooks followed by the selected scheme hook and declared extension hooks.\n *\n * @param phase - Hook slot to collect\n * @param x402Version - Protocol version for the selected requirement\n * @param requirements - Selected payment requirement\n * @param declaredExtensions - Extension declarations that scope extension hooks\n * @returns Hooks in invocation order\n */\n private getLabeledHooks<P extends ClientHookPhase>(\n phase: P,\n x402Version: number,\n requirements: PaymentRequirements,\n declaredExtensions?: Record<string, unknown>,\n ): Array<NonNullable<ClientHookAdapterHandles[P]>> {\n let manual: Array<NonNullable<ClientHookAdapterHandles[P]>>;\n switch (phase) {\n case \"beforePaymentCreation\":\n manual = this.beforePaymentCreationHooks as Array<\n NonNullable<ClientHookAdapterHandles[P]>\n >;\n break;\n case \"afterPaymentCreation\":\n manual = this.afterPaymentCreationHooks as Array<\n NonNullable<ClientHookAdapterHandles[P]>\n >;\n break;\n case \"onPaymentCreationFailure\":\n manual = this.onPaymentCreationFailureHooks as Array<\n NonNullable<ClientHookAdapterHandles[P]>\n >;\n break;\n case \"onPaymentResponse\":\n manual = this.paymentResponseHooks as Array<NonNullable<ClientHookAdapterHandles[P]>>;\n break;\n }\n\n const out: Array<NonNullable<ClientHookAdapterHandles[P]>> = [...manual];\n const adaptersByNetwork = this.schemeClientHookAdapters.get(x402Version);\n const schemeAdapter = adaptersByNetwork\n ? findByNetworkAndScheme(adaptersByNetwork, requirements.scheme, requirements.network)\n : undefined;\n const hook = schemeAdapter?.[phase];\n if (hook !== undefined) {\n out.push(hook);\n }\n if (!declaredExtensions) {\n return out;\n }\n\n const extensionHookKey = this.getClientExtensionHookKey(phase);\n for (const [extensionKey, extension] of this.registeredExtensions) {\n if (!(extensionKey in declaredExtensions)) continue;\n\n const extensionHook = extension.hooks?.[extensionHookKey];\n if (!extensionHook) continue;\n\n type HookFn = NonNullable<ClientHookAdapterHandles[P]>;\n type HookContext = Parameters<HookFn>[0];\n out.push((async (ctx: HookContext) => {\n return (\n extensionHook as (\n declaration: unknown,\n context: HookContext,\n ) => ReturnType<HookFn>\n )(declaredExtensions[extensionKey], ctx);\n }) as HookFn);\n }\n return out;\n }\n\n /**\n * Maps internal hook phases to extension hook names.\n *\n * @param phase - Internal hook phase\n * @returns Extension hook key for the phase\n */\n private getClientExtensionHookKey<P extends ClientHookPhase>(\n phase: P,\n ): keyof ClientExtensionHooks {\n switch (phase) {\n case \"beforePaymentCreation\":\n return \"onBeforePaymentCreation\";\n case \"afterPaymentCreation\":\n return \"onAfterPaymentCreation\";\n case \"onPaymentCreationFailure\":\n return \"onPaymentCreationFailure\";\n case \"onPaymentResponse\":\n return \"onPaymentResponse\";\n }\n }\n}\n\n/**\n * Concatenates two arrays, keeping client entries first (so a downstream length\n * cap trims server entries rather than the client's) and dropping duplicates\n * (deep equality) wherever they occur, including within either input array.\n *\n * @param client - Client-provided array values\n * @param server - Server-declared array values\n * @returns Deduplicated concatenation\n */\nfunction mergeArraysUnique(client: unknown[], server: unknown[]): unknown[] {\n const merged: unknown[] = [];\n for (const item of [...client, ...server]) {\n if (!merged.some(existing => deepEqual(existing, item))) {\n merged.push(item);\n }\n }\n return merged;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmMO,IAAM,aAAN,MAAM,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBtB,YAAY,6BAAyD;AAfrE,SAAiB,0BAAsF,oBAAI,IAAI;AAC/G,SAAiB,2BAA4F,oBAAI,IAAI;AACrH,SAAiB,WAA4B,CAAC;AAC9C,SAAiB,uBAAqD,oBAAI,IAAI;AAE9E,SAAQ,6BAA0D,CAAC;AACnE,SAAQ,4BAAwD,CAAC;AACjE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,MACF,KAAK;AACH,iBAAS,KAAK;AACd;AAAA,IACJ;AAEA,UAAM,MAAuD,CAAC,GAAG,MAAM;AACvE,UAAM,oBAAoB,KAAK,yBAAyB,IAAIA,YAAW;AACvE,UAAM,gBAAgB,oBAClB,uBAAuB,mBAAmB,aAAa,QAAQ,aAAa,OAAO,IACnF;AACJ,UAAM,OAAO,gBAAgB,KAAK;AAClC,QAAI,SAAS,QAAW;AACtB,UAAI,KAAK,IAAI;AAAA,IACf;AACA,QAAI,CAAC,oBAAoB;AACvB,aAAO;AAAA,IACT;AAEA,UAAM,mBAAmB,KAAK,0BAA0B,KAAK;AAC7D,eAAW,CAAC,cAAc,SAAS,KAAK,KAAK,sBAAsB;AACjE,UAAI,EAAE,gBAAgB,oBAAqB;AAE3C,YAAM,gBAAgB,UAAU,QAAQ,gBAAgB;AACxD,UAAI,CAAC,cAAe;AAIpB,UAAI,MAAM,OAAO,QAAqB;AACpC,eACE,cAIA,mBAAmB,YAAY,GAAG,GAAG;AAAA,MACzC,EAAY;AAAA,IACd;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,0BACN,OAC4B;AAC5B,YAAQ,OAAO;AAAA,MACb,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,IACX;AAAA,EACF;AACF;AAWA,SAAS,kBAAkB,QAAmB,QAA8B;AAC1E,QAAM,SAAoB,CAAC;AAC3B,aAAW,QAAQ,CAAC,GAAG,QAAQ,GAAG,MAAM,GAAG;AACzC,QAAI,CAAC,OAAO,KAAK,cAAY,UAAU,UAAU,IAAI,CAAC,GAAG;AACvD,aAAO,KAAK,IAAI;AAAA,IAClB;AAAA,EACF;AACA,SAAO;AACT;","names":["x402Version"]}

@@ -1,2 +0,2 @@

import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../x402Client-0g4vl2En.mjs';
import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator, F as FacilitatorExtension } from '../x402Client-CzZlbbXy.mjs';

@@ -3,0 +3,0 @@ /**

@@ -6,3 +6,3 @@ import {

networkMatchesPattern
} from "../chunk-ABS7D6VX.mjs";
} from "../chunk-HX5GESJI.mjs";
import "../chunk-BJTO5JO5.mjs";

@@ -9,0 +9,0 @@

@@ -1,3 +0,3 @@

import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse } from '../x402Client-0g4vl2En.mjs';
export { C as CompiledRoute, D as DynamicPayTo, l as DynamicPrice, y as FacilitatorClient, z as FacilitatorConfig, A as FacilitatorResponseError, H as HTTPAdapter, w as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, u as HTTPResourceServerExtensionHooks, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, j as PaymentOption, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, v as ResourceServerTransportExtensionHooks, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, n as SettlementFailedResponseBody, U as UnpaidResponseBody, B as getFacilitatorResponseError, x as x402HTTPResourceServer } from '../x402Client-0g4vl2En.mjs';
import { P as PaymentPayload, c as PaymentRequired, S as SettleResponse } from '../x402Client-CzZlbbXy.mjs';
export { C as CompiledRoute, D as DynamicPayTo, l as DynamicPrice, B as FacilitatorClient, E as FacilitatorConfig, G as FacilitatorResponseError, I as FacilitatorTimeoutError, H as HTTPAdapter, A as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, u as HTTPResourceServerExtensionHooks, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, y as PAYMENT_REQUIRED_CACHE_CONTROL, j as PaymentOption, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, v as ResourceServerTransportExtensionHooks, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, w as SETTLEMENT_OVERRIDES_HEADER, n as SettlementFailedResponseBody, U as UnpaidResponseBody, J as getFacilitatorResponseError, z as withPrivateCacheControl, x as x402HTTPResourceServer } from '../x402Client-CzZlbbXy.mjs';
export { HTTPClientExtensionHooks, HTTPPaymentStatus, HTTPResourceResponse, PaymentRequiredContext, PaymentRequiredHook, x402HTTPClient } from '../client/index.mjs';

@@ -4,0 +4,0 @@

import {
HTTPFacilitatorClient,
PAYMENT_REQUIRED_CACHE_CONTROL,
RouteConfigurationError,
SETTLEMENT_OVERRIDES_HEADER,
decodePaymentRequiredHeader,

@@ -10,5 +12,6 @@ decodePaymentResponseHeader,

encodePaymentSignatureHeader,
withPrivateCacheControl,
x402HTTPClient,
x402HTTPResourceServer
} from "../chunk-4Y6I6537.mjs";
} from "../chunk-VKJGPEW2.mjs";
import "../chunk-N4QXZG2Z.mjs";

@@ -18,10 +21,14 @@ import "../chunk-VE37GDG2.mjs";

FacilitatorResponseError,
FacilitatorTimeoutError,
getFacilitatorResponseError
} from "../chunk-AGOUMC4P.mjs";
import "../chunk-ABS7D6VX.mjs";
} from "../chunk-P3DFEIO7.mjs";
import "../chunk-HX5GESJI.mjs";
import "../chunk-BJTO5JO5.mjs";
export {
FacilitatorResponseError,
FacilitatorTimeoutError,
HTTPFacilitatorClient,
PAYMENT_REQUIRED_CACHE_CONTROL,
RouteConfigurationError,
SETTLEMENT_OVERRIDES_HEADER,
decodePaymentRequiredHeader,

@@ -34,2 +41,3 @@ decodePaymentResponseHeader,

getFacilitatorResponseError,
withPrivateCacheControl,
x402HTTPClient,

@@ -36,0 +44,0 @@ x402HTTPResourceServer

@@ -1,3 +0,3 @@

import { a as PaymentRequirements, S as SettleResponse } from '../x402Client-0g4vl2En.mjs';
export { a5 as AfterSettleHook, a2 as AfterVerifyHook, a4 as BeforeSettleHook, a1 as BeforeVerifyHook, C as CompiledRoute, _ as ExtensionValidationResult, y as FacilitatorClient, z as FacilitatorConfig, A as FacilitatorResponseError, H as HTTPAdapter, w as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, a6 as OnSettleFailureHook, a7 as OnVerifiedPaymentCanceledHook, a3 as OnVerifyFailureHook, Y as PaymentCancellationDispatcher, I as PaymentRequiredContext, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, G as ResourceConfig, a0 as ResourceVerifyRespone, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, ac as SETTLEMENT_OVERRIDES_HEADER, a8 as SchemeEnrichPaymentRequiredResponseHook, aa as SchemeEnrichSettlementPayloadHook, ab as SchemeEnrichSettlementResponseHook, a9 as SchemePaymentRequiredContext, M as SettleContext, Q as SettleFailureContext, O as SettleResultContext, n as SettlementFailedResponseBody, Z as SettlementOverrides, $ as SkipHandlerDirective, U as UnpaidResponseBody, X as VerifiedPaymentCancelOptions, T as VerifiedPaymentCanceledContext, W as VerifiedPaymentCancellationReason, J as VerifyContext, L as VerifyFailureContext, K as VerifyResultContext, ad as checkIfBazaarNeeded, B as getFacilitatorResponseError, x as x402HTTPResourceServer, E as x402ResourceServer } from '../x402Client-0g4vl2En.mjs';
import { a as PaymentRequirements, S as SettleResponse } from '../x402Client-CzZlbbXy.mjs';
export { a9 as AfterSettleHook, a6 as AfterVerifyHook, a8 as BeforeSettleHook, a5 as BeforeVerifyHook, C as CompiledRoute, a2 as ExtensionValidationResult, B as FacilitatorClient, E as FacilitatorConfig, G as FacilitatorResponseError, I as FacilitatorTimeoutError, H as HTTPAdapter, A as HTTPFacilitatorClient, g as HTTPProcessResult, d as HTTPRequestContext, m as HTTPResponseBody, f as HTTPResponseInstructions, e as HTTPTransportContext, aa as OnSettleFailureHook, ab as OnVerifiedPaymentCanceledHook, a7 as OnVerifyFailureHook, y as PAYMENT_REQUIRED_CACHE_CONTROL, a0 as PaymentCancellationDispatcher, M as PaymentRequiredContext, h as PaywallConfig, i as PaywallProvider, q as ProcessSettleFailureResponse, o as ProcessSettleResultResponse, p as ProcessSettleSuccessResponse, t as ProtectedRequestHook, L as ResourceConfig, a4 as ResourceVerifyRespone, R as RouteConfig, s as RouteConfigurationError, r as RouteValidationError, k as RoutesConfig, w as SETTLEMENT_OVERRIDES_HEADER, ac as SchemeEnrichPaymentRequiredResponseHook, ae as SchemeEnrichSettlementPayloadHook, af as SchemeEnrichSettlementResponseHook, ad as SchemePaymentRequiredContext, W as SettleContext, Y as SettleFailureContext, X as SettleResultContext, n as SettlementFailedResponseBody, a1 as SettlementOverrides, a3 as SkipHandlerDirective, U as UnpaidResponseBody, $ as VerifiedPaymentCancelOptions, Z as VerifiedPaymentCanceledContext, _ as VerifiedPaymentCancellationReason, O as VerifyContext, T as VerifyFailureContext, Q as VerifyResultContext, ag as checkIfBazaarNeeded, J as getFacilitatorResponseError, z as withPrivateCacheControl, x as x402HTTPResourceServer, K as x402ResourceServer } from '../x402Client-CzZlbbXy.mjs';

@@ -4,0 +4,0 @@ /**

import {
HTTPFacilitatorClient,
PAYMENT_REQUIRED_CACHE_CONTROL,
RouteConfigurationError,
SETTLEMENT_OVERRIDES_HEADER,
checkIfBazaarNeeded,
withPrivateCacheControl,
x402HTTPResourceServer
} from "../chunk-4Y6I6537.mjs";
} from "../chunk-VKJGPEW2.mjs";
import "../chunk-N4QXZG2Z.mjs";

@@ -14,9 +16,13 @@ import {

FacilitatorResponseError,
FacilitatorTimeoutError,
SettleError,
getFacilitatorResponseError
} from "../chunk-AGOUMC4P.mjs";
} from "../chunk-P3DFEIO7.mjs";
import {
ADDITIVE_ARRAY_INFO_FIELDS,
ADDITIVE_ARRAY_MAX_LENGTHS,
deepEqual,
findByNetworkAndScheme
} from "../chunk-ABS7D6VX.mjs";
findByNetworkAndScheme,
toComparableArray
} from "../chunk-HX5GESJI.mjs";
import "../chunk-BJTO5JO5.mjs";

@@ -1045,5 +1051,9 @@

const dynamicFields = this.registeredExtensions.get(key)?.dynamicInfoFields;
const additiveFields = ADDITIVE_ARRAY_INFO_FIELDS[key];
const maxLengths = ADDITIVE_ARRAY_MAX_LENGTHS[key];
if (!extensionInfoMatchesAdvertised(
omitFields(advertisedInfo, dynamicFields),
omitFields(echoedInfo, dynamicFields)
omitFields(echoedInfo, dynamicFields),
additiveFields,
maxLengths
)) {

@@ -1341,4 +1351,4 @@ return {

}
function extensionInfoMatchesAdvertised(advertised, echoed) {
return objectContainsSubset(advertised, echoed);
function extensionInfoMatchesAdvertised(advertised, echoed, additiveFields, maxLengths) {
return objectContainsSubset(advertised, echoed, additiveFields, maxLengths);
}

@@ -1356,3 +1366,15 @@ function paymentRequirementsMatchAccepted(required, accepted) {

}
function objectContainsSubset(expected, actual) {
function objectContainsSubset(expected, actual, additiveFields, maxLengths, fieldKey) {
if (fieldKey !== void 0 && additiveFields?.has(fieldKey) && (Array.isArray(expected) || Array.isArray(actual))) {
const expectedArray = toComparableArray(expected);
const actualArray = toComparableArray(actual);
if (!expectedArray || !actualArray) {
return false;
}
const maxLength = maxLengths?.[fieldKey];
if (maxLength !== void 0 && actualArray.length > maxLength) {
return false;
}
return expectedArray.every((expItem) => actualArray.some((actItem) => deepEqual(expItem, actItem)));
}
if (expected === null || typeof expected !== "object" || Array.isArray(expected)) {

@@ -1370,3 +1392,3 @@ return deepEqual(expected, actual);

}
return objectContainsSubset(value, actualRecord[key]);
return objectContainsSubset(value, actualRecord[key], additiveFields, maxLengths, key);
});

@@ -1376,3 +1398,5 @@ }

FacilitatorResponseError,
FacilitatorTimeoutError,
HTTPFacilitatorClient,
PAYMENT_REQUIRED_CACHE_CONTROL,
RouteConfigurationError,

@@ -1390,2 +1414,3 @@ SETTLEMENT_OVERRIDES_HEADER,

snapshotSettleResponseCore,
withPrivateCacheControl,
x402HTTPResourceServer,

@@ -1392,0 +1417,0 @@ x402ResourceServer

@@ -1,1 +0,1 @@

export { aC as AssetAmount, aV as DeepReadonly, aS as FacilitatorContext, F as FacilitatorExtension, A as FacilitatorResponseError, u as HTTPResourceServerExtensionHooks, aB as Money, aP as MoneyParser, N as Network, P as PaymentPayload, aR as PaymentPayloadContext, aQ as PaymentPayloadResult, aw as PaymentPayloadV1, c as PaymentRequired, I as PaymentRequiredContext, av as PaymentRequiredV1, a as PaymentRequirements, au as PaymentRequirementsV1, aD as Price, aK as ResourceInfo, aT as ResourceServerExtension, aU as ResourceServerExtensionHooks, v as ResourceServerTransportExtensionHooks, aM as SchemeClientHooks, a8 as SchemeEnrichPaymentRequiredResponseHook, aL as SchemeNetworkClient, b as SchemeNetworkFacilitator, aN as SchemeNetworkServer, a9 as SchemePaymentRequiredContext, aO as SchemeServerHooks, M as SettleContext, aJ as SettleError, Q as SettleFailureContext, aF as SettleRequest, S as SettleResponse, O as SettleResultContext, aH as SupportedKind, aG as SupportedResponse, T as VerifiedPaymentCanceledContext, J as VerifyContext, aI as VerifyError, L as VerifyFailureContext, aE as VerifyRequest, V as VerifyResponse, K as VerifyResultContext, B as getFacilitatorResponseError } from '../x402Client-0g4vl2En.mjs';
export { aF as AssetAmount, aY as DeepReadonly, aV as FacilitatorContext, F as FacilitatorExtension, G as FacilitatorResponseError, I as FacilitatorTimeoutError, u as HTTPResourceServerExtensionHooks, aE as Money, aS as MoneyParser, N as Network, P as PaymentPayload, aU as PaymentPayloadContext, aT as PaymentPayloadResult, az as PaymentPayloadV1, c as PaymentRequired, M as PaymentRequiredContext, ay as PaymentRequiredV1, a as PaymentRequirements, ax as PaymentRequirementsV1, aG as Price, aN as ResourceInfo, aW as ResourceServerExtension, aX as ResourceServerExtensionHooks, v as ResourceServerTransportExtensionHooks, aP as SchemeClientHooks, ac as SchemeEnrichPaymentRequiredResponseHook, aO as SchemeNetworkClient, b as SchemeNetworkFacilitator, aQ as SchemeNetworkServer, ad as SchemePaymentRequiredContext, aR as SchemeServerHooks, W as SettleContext, aM as SettleError, Y as SettleFailureContext, aI as SettleRequest, S as SettleResponse, X as SettleResultContext, aK as SupportedKind, aJ as SupportedResponse, Z as VerifiedPaymentCanceledContext, O as VerifyContext, aL as VerifyError, T as VerifyFailureContext, aH as VerifyRequest, V as VerifyResponse, Q as VerifyResultContext, J as getFacilitatorResponseError } from '../x402Client-CzZlbbXy.mjs';
import {
FacilitatorResponseError,
FacilitatorTimeoutError,
SettleError,
VerifyError,
getFacilitatorResponseError
} from "../chunk-AGOUMC4P.mjs";
} from "../chunk-P3DFEIO7.mjs";
import "../chunk-BJTO5JO5.mjs";
export {
FacilitatorResponseError,
FacilitatorTimeoutError,
SettleError,

@@ -11,0 +13,0 @@ VerifyError,

@@ -1,1 +0,1 @@

export { aw as PaymentPayloadV1, av as PaymentRequiredV1, au as PaymentRequirementsV1, ay as SettleRequestV1, az as SettleResponseV1, aA as SupportedResponseV1, ax as VerifyRequestV1 } from '../../x402Client-0g4vl2En.mjs';
export { az as PaymentPayloadV1, ay as PaymentRequiredV1, ax as PaymentRequirementsV1, aB as SettleRequestV1, aC as SettleResponseV1, aD as SupportedResponseV1, aA as VerifyRequestV1 } from '../../x402Client-CzZlbbXy.mjs';

@@ -1,2 +0,2 @@

import { N as Network } from '../x402Client-0g4vl2En.mjs';
import { N as Network } from '../x402Client-CzZlbbXy.mjs';

@@ -76,3 +76,36 @@ /**

declare function deepEqual(obj1: unknown, obj2: unknown): boolean;
/**
* Coerces a value for array-aware merging/comparison: an array passes through
* unchanged, while a bare scalar is wrapped as a single-element array so it can
* merge or compare against an array declared on the other side (e.g. an
* extension field documented as "string or array of strings"). Returns
* undefined for values that cannot participate (null, undefined, objects).
*
* @param value - Value to coerce
* @returns The value as an array, or undefined if it cannot be treated as one
*/
declare function toComparableArray(value: unknown): unknown[] | undefined;
/**
* Extension info fields, keyed by extension key, where a conflicting array
* value declared by both server and client is additive rather than exclusive:
* the client's merge concatenates both sides (client first, deduped, see
* `mergeArraysUnique`), and the server's echo validation accepts any echo that
* is a superset of the advertised value. Scoped narrowly per field (rather
* than making all array fields additive) so unrelated extensions - e.g.
* sign-in-with-x's `resources` - keep exact array matching in both directions.
*/
declare const ADDITIVE_ARRAY_INFO_FIELDS: Record<string, ReadonlySet<string>>;
/**
* Caps the combined echoed length of an additive array field (see
* {@link ADDITIVE_ARRAY_INFO_FIELDS}) so a hand-crafted payload cannot pad the
* field past the sum of every party's own reservation and later crowd out a
* legitimately declared entry once truncated further downstream (e.g. by a
* facilitator extension). Core has no dependency on extension packages, so
* this value (builder-code's `MAX_CLIENT_SERVICE_CODES` +
* `MAX_SERVER_SERVICE_CODES`) is duplicated from
* `packages/extensions/src/builder-code/types.ts` and must be kept in sync by
* hand.
*/
declare const ADDITIVE_ARRAY_MAX_LENGTHS: Record<string, Record<string, number>>;
export { Base64EncodedRegex, type SchemeData, convertToTokenAmount, deepEqual, findByNetworkAndScheme, findFacilitatorBySchemeAndNetwork, findSchemesByNetwork, networkMatchesPattern, numberToDecimalString, parseMoneyString, safeBase64Decode, safeBase64Encode };
export { ADDITIVE_ARRAY_INFO_FIELDS, ADDITIVE_ARRAY_MAX_LENGTHS, Base64EncodedRegex, type SchemeData, convertToTokenAmount, deepEqual, findByNetworkAndScheme, findFacilitatorBySchemeAndNetwork, findSchemesByNetwork, networkMatchesPattern, numberToDecimalString, parseMoneyString, safeBase64Decode, safeBase64Encode, toComparableArray };
import {
ADDITIVE_ARRAY_INFO_FIELDS,
ADDITIVE_ARRAY_MAX_LENGTHS,
Base64EncodedRegex,

@@ -12,6 +14,9 @@ convertToTokenAmount,

safeBase64Decode,
safeBase64Encode
} from "../chunk-ABS7D6VX.mjs";
safeBase64Encode,
toComparableArray
} from "../chunk-HX5GESJI.mjs";
import "../chunk-BJTO5JO5.mjs";
export {
ADDITIVE_ARRAY_INFO_FIELDS,
ADDITIVE_ARRAY_MAX_LENGTHS,
Base64EncodedRegex,

@@ -27,4 +32,5 @@ convertToTokenAmount,

safeBase64Decode,
safeBase64Encode
safeBase64Encode,
toComparableArray
};
//# sourceMappingURL=index.mjs.map
{
"name": "@x402/core",
"version": "2.20.0",
"version": "2.21.0",
"main": "./dist/cjs/index.js",

@@ -5,0 +5,0 @@ "module": "./dist/esm/index.js",

Sorry, the diff of this file is too big to display

import {
z
} from "./chunk-N4QXZG2Z.mjs";
import {
x402Version
} from "./chunk-VE37GDG2.mjs";
import {
FacilitatorResponseError,
SettleError,
VerifyError
} from "./chunk-AGOUMC4P.mjs";
import {
Base64EncodedRegex,
safeBase64Decode,
safeBase64Encode
} from "./chunk-ABS7D6VX.mjs";
import {
__require
} from "./chunk-BJTO5JO5.mjs";
// src/http/x402HTTPResourceServer.ts
var SETTLEMENT_OVERRIDES_HEADER = "Settlement-Overrides";
function checkIfBazaarNeeded(routes) {
if ("accepts" in routes) {
return !!(routes.extensions && "bazaar" in routes.extensions);
}
return Object.values(routes).some((routeConfig) => {
return !!(routeConfig.extensions && "bazaar" in routeConfig.extensions);
});
}
var RouteConfigurationError = class extends Error {
/**
* Creates a new RouteConfigurationError with the given validation errors.
*
* @param errors - The validation errors that caused this exception.
*/
constructor(errors) {
const message = `x402 Route Configuration Errors:
${errors.map((e) => ` - ${e.message}`).join("\n")}`;
super(message);
this.name = "RouteConfigurationError";
this.errors = errors;
}
};
var FALLBACK_PAYWALL_HTML = `<!DOCTYPE html>
<html>
<head>
<title>Payment Required</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="max-width: 600px; margin: 50px auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;">
<h1>Payment Required</h1>
<p>This resource is protected by the x402 payment protocol.</p>
<p style="margin-top: 2rem; padding: 1rem; background: #fef3c7; border-radius: 0.5rem;">
<strong>Note to developers:</strong> install <code>@x402/paywall</code> to enable
the in-browser wallet connection and payment UI. Programmatic clients should read
the payment requirements from the 402 response headers and JSON body.
</p>
</div>
</body>
</html>`;
var x402HTTPResourceServer = class {
/**
* Creates a new x402HTTPResourceServer instance.
*
* @param ResourceServer - The core x402ResourceServer instance to use
* @param routes - Route configuration for payment-protected endpoints
*/
constructor(ResourceServer, routes) {
this.compiledRoutes = [];
this.protectedRequestHooks = [];
this.ResourceServer = ResourceServer;
this.routesConfig = routes;
const normalizedRoutes = typeof routes === "object" && !("accepts" in routes) ? routes : { "*": routes };
for (const [pattern, config] of Object.entries(normalizedRoutes)) {
const parsed = this.parseRoutePattern(pattern);
this.compiledRoutes.push({
verb: parsed.verb,
regex: parsed.regex,
config,
pattern: parsed.path
});
}
}
/**
* Get the underlying x402ResourceServer instance.
*
* @returns The underlying x402ResourceServer instance
*/
get server() {
return this.ResourceServer;
}
/**
* Get the routes configuration.
*
* @returns The routes configuration
*/
get routes() {
return this.routesConfig;
}
/**
* Initialize the HTTP resource server.
*
* This method initializes the underlying resource server (fetching facilitator support)
* and then validates that all route payment configurations have corresponding
* registered schemes and facilitator support.
*
* @throws RouteConfigurationError if any route's payment options don't have
* corresponding registered schemes or facilitator support
*
* @example
* ```typescript
* const httpServer = new x402HTTPResourceServer(server, routes);
* await httpServer.initialize();
* ```
*/
async initialize() {
await this.ResourceServer.initialize();
const errors = this.validateRouteConfiguration();
if (errors.length > 0) {
throw new RouteConfigurationError(errors);
}
}
/**
* Register a custom paywall provider for generating HTML
*
* @param provider - PaywallProvider instance
* @returns This service instance for chaining
*/
registerPaywallProvider(provider) {
this.paywallProvider = provider;
return this;
}
/**
* Register a hook that runs on every request to a protected route, before payment processing.
* Hooks are executed in order of registration. The first hook to return a non-void result wins.
*
* @param hook - The request hook function
* @returns The x402HTTPResourceServer instance for chaining
*/
onProtectedRequest(hook) {
this.protectedRequestHooks.push(hook);
return this;
}
/**
* Process HTTP request and return response instructions
* This is the main entry point for framework middleware
*
* @param context - HTTP request context
* @param paywallConfig - Optional paywall configuration
* @returns Process result indicating next action for middleware
*/
async processHTTPRequest(context, paywallConfig) {
const method = context.method || context.adapter.getMethod();
context = { ...context, method };
const { adapter, path } = context;
const routeMatch = this.getRouteConfig(path, method);
if (!routeMatch) {
return { type: "no-payment-required" };
}
const { config: routeConfig, pattern: routePattern } = routeMatch;
const enrichedContext = { ...context, routePattern };
for (const hook of this.getProtectedRequestHooks(routeConfig)) {
const result = await hook(enrichedContext, routeConfig);
if (result && "grantAccess" in result) {
return { type: "no-payment-required" };
}
if (result && "abort" in result) {
return {
type: "payment-error",
response: {
status: 403,
headers: { "Content-Type": "application/json" },
body: { error: result.reason }
}
};
}
}
const paymentOptions = this.normalizePaymentOptions(routeConfig);
const paymentPayload = this.extractPayment(adapter);
const resourceInfo = {
url: routeConfig.resource || enrichedContext.adapter.getUrl(),
description: routeConfig.description || "",
mimeType: routeConfig.mimeType || "",
...routeConfig.serviceName !== void 0 && { serviceName: routeConfig.serviceName },
...routeConfig.tags !== void 0 && { tags: routeConfig.tags },
...routeConfig.iconUrl !== void 0 && { iconUrl: routeConfig.iconUrl }
};
let requirements = await this.ResourceServer.buildPaymentRequirementsFromOptions(
paymentOptions,
enrichedContext
);
let extensions = routeConfig.extensions;
if (extensions) {
extensions = this.ResourceServer.enrichExtensions(extensions, enrichedContext);
}
const transportContext = { request: enrichedContext };
const paymentRequired = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
!paymentPayload ? "Payment required" : void 0,
extensions,
transportContext
);
if (!paymentPayload) {
const unpaidBody = routeConfig.unpaidResponseBody ? await routeConfig.unpaidResponseBody(enrichedContext) : void 0;
return {
type: "payment-error",
response: this.createHTTPResponse(
paymentRequired,
this.isWebBrowser(adapter),
paywallConfig,
routeConfig.customPaywallHtml,
unpaidBody
)
};
}
try {
const matchingRequirements = this.ResourceServer.findMatchingRequirements(
paymentRequired.accepts,
paymentPayload
);
if (!matchingRequirements) {
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
"No matching payment requirements",
extensions,
transportContext
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
const extensionResult = this.ResourceServer.validateExtensions(
paymentRequired,
paymentPayload
);
if (!extensionResult.valid) {
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
extensionResult.invalidReason,
extensions,
transportContext,
paymentPayload
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
const verifyResult = await this.ResourceServer.verifyPayment(
paymentPayload,
matchingRequirements,
extensions,
transportContext
);
if (!verifyResult.isValid) {
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
verifyResult.invalidReason,
extensions,
transportContext,
paymentPayload
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
if (verifyResult.skipHandler) {
return await this.processSkipHandlerSettlement(
paymentPayload,
matchingRequirements,
extensions,
transportContext,
verifyResult.skipHandler
);
}
const cancellationDispatcher = this.ResourceServer.createPaymentCancellationDispatcher(
paymentPayload,
matchingRequirements,
extensions,
transportContext
);
return {
type: "payment-verified",
cancellationDispatcher,
paymentPayload,
paymentRequirements: matchingRequirements,
declaredExtensions: extensions
};
} catch (error) {
if (error instanceof FacilitatorResponseError) {
throw error;
}
const errorResponse = await this.ResourceServer.createPaymentRequiredResponse(
requirements,
resourceInfo,
error instanceof Error ? error.message : "Payment verification failed",
extensions,
transportContext
);
return {
type: "payment-error",
response: this.createHTTPResponse(errorResponse, false, paywallConfig)
};
}
}
/**
* Process settlement after successful response
*
* @param paymentPayload - The verified payment payload
* @param requirements - The matching payment requirements
* @param declaredExtensions - Optional declared extensions (for per-key enrichment)
* @param transportContext - Optional HTTP transport context
* @param settlementOverrides - Optional settlement overrides (e.g., partial settlement amount)
* @returns ProcessSettleResultResponse - SettleResponse with headers if success or errorReason if failure
*/
async processSettlement(paymentPayload, requirements, declaredExtensions, transportContext, settlementOverrides) {
if (transportContext?.request && !transportContext.request.method) {
transportContext = {
...transportContext,
request: {
...transportContext.request,
method: transportContext.request.adapter.getMethod()
}
};
}
try {
let resolvedOverrides = settlementOverrides;
if (!resolvedOverrides && transportContext?.responseHeaders) {
const overridesKey = SETTLEMENT_OVERRIDES_HEADER.toLowerCase();
const rawValue = Object.entries(transportContext.responseHeaders).find(
([key]) => key.toLowerCase() === overridesKey
)?.[1];
if (rawValue) {
try {
resolvedOverrides = JSON.parse(rawValue);
} catch {
}
}
}
const settleResponse = await this.ResourceServer.settlePayment(
paymentPayload,
requirements,
declaredExtensions,
transportContext,
resolvedOverrides
);
if (!settleResponse.success) {
const failure = {
...settleResponse,
success: false,
errorReason: settleResponse.errorReason || "Settlement failed",
errorMessage: settleResponse.errorMessage || settleResponse.errorReason || "Settlement failed",
headers: this.createSettlementHeaders(settleResponse)
};
const response = await this.buildSettlementFailureResponse(failure, transportContext);
return { ...failure, response };
}
return {
...settleResponse,
success: true,
headers: this.createSettlementHeaders(settleResponse),
requirements
};
} catch (error) {
if (error instanceof FacilitatorResponseError) {
throw error;
}
if (error instanceof SettleError) {
const errorReason2 = error.errorReason || error.message;
const settleResponse2 = {
success: false,
errorReason: errorReason2,
errorMessage: error.errorMessage || errorReason2,
payer: error.payer,
network: error.network,
transaction: error.transaction
};
const failure2 = {
...settleResponse2,
success: false,
errorReason: errorReason2,
headers: this.createSettlementHeaders(settleResponse2)
};
const response2 = await this.buildSettlementFailureResponse(failure2, transportContext);
return { ...failure2, response: response2 };
}
const errorReason = error instanceof Error ? error.message : "Settlement failed";
const settleResponse = {
success: false,
errorReason,
errorMessage: errorReason,
network: requirements.network,
transaction: ""
};
const failure = {
...settleResponse,
success: false,
errorReason,
headers: this.createSettlementHeaders(settleResponse)
};
const response = await this.buildSettlementFailureResponse(failure, transportContext);
return { ...failure, response };
}
}
/**
* Check if a request requires payment based on route configuration
*
* @param context - HTTP request context
* @returns True if the route requires payment, false otherwise
*/
requiresPayment(context) {
const method = context.method || context.adapter.getMethod();
return this.getRouteConfig(context.path, method) !== void 0;
}
/**
* Settle a verified payment that requested `skipHandler`, packaging the
* result as a `payment-error` HTTPProcessResult so framework adapters can
* write the response without invoking the route handler.
*
* - On success: status 200 + PAYMENT-RESPONSE header + configured body.
* - On failure: the standard 402 settlement-failure response.
*
* @param paymentPayload - Verified payment payload.
* @param requirements - Matched payment requirements.
* @param declaredExtensions - Optional declared extensions for the route.
* @param transportContext - Optional HTTP transport context.
* @param skipHandlerResponse - Optional content type + body to return on success.
* @returns A `payment-error` HTTPProcessResult carrying the final response.
*/
async processSkipHandlerSettlement(paymentPayload, requirements, declaredExtensions, transportContext, skipHandlerResponse) {
const settleResult = await this.processSettlement(
paymentPayload,
requirements,
declaredExtensions,
transportContext
);
if (!settleResult.success) {
return { type: "payment-error", response: settleResult.response };
}
const contentType = skipHandlerResponse?.contentType ?? "application/json";
const body = skipHandlerResponse?.body ?? {};
return {
type: "payment-error",
response: {
status: 200,
headers: {
"Content-Type": contentType,
...settleResult.headers
},
body,
isHtml: contentType.includes("text/html")
}
};
}
/**
* Build HTTPResponseInstructions for settlement failure.
* Uses settlementFailedResponseBody hook if configured, otherwise defaults to empty body.
*
* @param failure - Settlement failure result with headers
* @param transportContext - Optional HTTP transport context for the request
* @returns HTTP response instructions for the 402 settlement failure response
*/
async buildSettlementFailureResponse(failure, transportContext) {
const settlementHeaders = failure.headers;
const routeConfig = transportContext ? this.getRouteConfig(transportContext.request.path, transportContext.request.method) : void 0;
const customBody = routeConfig?.config.settlementFailedResponseBody ? await routeConfig.config.settlementFailedResponseBody(transportContext.request, failure) : void 0;
const contentType = customBody ? customBody.contentType : "application/json";
const body = customBody ? customBody.body : {};
return {
status: 402,
headers: {
"Content-Type": contentType,
...settlementHeaders
},
body,
isHtml: contentType.includes("text/html")
};
}
/**
* Normalizes a RouteConfig's accepts field into an array of PaymentOptions
* Handles both single PaymentOption and array formats
*
* @param routeConfig - Route configuration
* @returns Array of payment options
*/
normalizePaymentOptions(routeConfig) {
return Array.isArray(routeConfig.accepts) ? routeConfig.accepts : [routeConfig.accepts];
}
/**
* Manual request hooks run before extension transport hooks for declared extensions.
*
* @param routeConfig - Route configuration for the matched request
* @returns Hooks in invocation order
*/
getProtectedRequestHooks(routeConfig) {
const hooks = [...this.protectedRequestHooks];
const declaredExtensions = routeConfig.extensions;
if (!declaredExtensions) return hooks;
for (const extension of this.ResourceServer.getExtensions()) {
const hook = extension.transportHooks?.http?.onProtectedRequest;
if (!hook || !(extension.key in declaredExtensions)) continue;
hooks.push(
(context, routeConfig2) => hook(declaredExtensions[extension.key], context, routeConfig2)
);
}
return hooks;
}
/**
* Validates that all payment options in routes have corresponding registered schemes
* and facilitator support.
*
* @returns Array of validation errors (empty if all routes are valid)
*/
validateRouteConfiguration() {
const errors = [];
const normalizedRoutes = typeof this.routesConfig === "object" && !("accepts" in this.routesConfig) ? Object.entries(this.routesConfig) : [["*", this.routesConfig]];
for (const [pattern, config] of normalizedRoutes) {
const pathPart = pattern.includes(" ") ? pattern.split(/\s+/)[1] : pattern;
if (pathPart && pathPart.includes("*") && config.extensions && "bazaar" in config.extensions) {
console.warn(
`[x402] Route "${pattern}": Wildcard (*) patterns with bazaar discovery extensions will auto-generate parameter names (var1, var2, ...). Consider using named parameters instead (e.g. /weather/:city) for better discovery metadata.`
);
}
const paymentOptions = this.normalizePaymentOptions(config);
for (const option of paymentOptions) {
if (!this.ResourceServer.hasRegisteredScheme(option.network, option.scheme)) {
errors.push({
routePattern: pattern,
scheme: option.scheme,
network: option.network,
reason: "missing_scheme",
message: `Route "${pattern}": No scheme implementation registered for "${option.scheme}" on network "${option.network}"`
});
continue;
}
const supportedKind = this.ResourceServer.getSupportedKind(
x402Version,
option.network,
option.scheme
);
if (!supportedKind) {
errors.push({
routePattern: pattern,
scheme: option.scheme,
network: option.network,
reason: "missing_facilitator",
message: `Route "${pattern}": Facilitator does not support scheme "${option.scheme}" on network "${option.network}"`
});
}
}
}
return errors;
}
/**
* Get route configuration for a request
*
* @param path - Request path
* @param method - HTTP method
* @returns Route configuration and pattern, or undefined if no match
*/
getRouteConfig(path, method) {
const normalizedPath = this.normalizePath(path);
const upperMethod = method.toUpperCase();
const matchingRoute = this.compiledRoutes.find(
(route) => route.regex.test(normalizedPath) && (route.verb === "*" || route.verb === upperMethod)
);
if (!matchingRoute) return void 0;
return { config: matchingRoute.config, pattern: matchingRoute.pattern };
}
/**
* Extract payment from HTTP headers (handles v1 and v2)
*
* @param adapter - HTTP adapter
* @returns Decoded payment payload or null
*/
extractPayment(adapter) {
const header = adapter.getHeader("payment-signature") || adapter.getHeader("PAYMENT-SIGNATURE");
if (header) {
try {
return decodePaymentSignatureHeader(header);
} catch (error) {
console.warn("Failed to decode PAYMENT-SIGNATURE header:", error);
}
}
return null;
}
/**
* Check if request is from a web browser
*
* @param adapter - HTTP adapter
* @returns True if request appears to be from a browser
*/
isWebBrowser(adapter) {
const accept = adapter.getAcceptHeader();
const userAgent = adapter.getUserAgent();
return accept.includes("text/html") && userAgent.includes("Mozilla");
}
/**
* Create HTTP response instructions from payment required
*
* @param paymentRequired - Payment requirements
* @param isWebBrowser - Whether request is from browser
* @param paywallConfig - Paywall configuration
* @param customHtml - Custom HTML template
* @param unpaidResponse - Optional custom response (content type and body) for unpaid API requests
* @returns Response instructions
*/
createHTTPResponse(paymentRequired, isWebBrowser, paywallConfig, customHtml, unpaidResponse) {
const status = paymentRequired.error === "permit2_allowance_required" ? 412 : 402;
const response = this.createHTTPPaymentRequiredResponse(paymentRequired);
if (isWebBrowser) {
const html = this.generatePaywallHTML(paymentRequired, paywallConfig, customHtml);
return {
status,
headers: {
"Content-Type": "text/html",
...response.headers
},
body: html,
isHtml: true
};
}
const contentType = unpaidResponse ? unpaidResponse.contentType : "application/json";
const body = unpaidResponse ? unpaidResponse.body : {};
return {
status,
headers: {
"Content-Type": contentType,
...response.headers
},
body
};
}
/**
* Create HTTP payment required response (v1 puts in body, v2 puts in header)
*
* @param paymentRequired - Payment required object
* @returns Headers and body for the HTTP response
*/
createHTTPPaymentRequiredResponse(paymentRequired) {
return {
headers: {
"PAYMENT-REQUIRED": encodePaymentRequiredHeader(paymentRequired)
}
};
}
/**
* Create settlement response headers
*
* @param settleResponse - Settlement response
* @returns Headers to add to response
*/
createSettlementHeaders(settleResponse) {
const encoded = encodePaymentResponseHeader(settleResponse);
return { "PAYMENT-RESPONSE": encoded };
}
/**
* Parse route pattern into verb and regex
*
* @param pattern - Route pattern like "GET /api/*", "/api/[id]", or "/api/:id"
* @returns Parsed pattern with verb and regex
*/
parseRoutePattern(pattern) {
const [verb, path] = pattern.includes(" ") ? pattern.split(/\s+/) : ["*", pattern];
const regex = new RegExp(
`^${path.replace(/\\/g, "\\\\").replace(/[$()+.?^{|}]/g, "\\$&").replace(/\*/g, ".*?").replace(/\[([^\]]+)\]/g, "[^/]+").replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, "[^/]+").replace(/\//g, "\\/")}$`,
"i"
);
return { verb: verb.toUpperCase(), regex, path };
}
/**
* Normalize path for matching
*
* @param path - Raw path from request
* @returns Normalized path
*/
normalizePath(path) {
const pathWithoutQuery = path.split(/[?#]/)[0];
const parts = pathWithoutQuery.split(/(%2[fF]|%5[cC])/);
const decoded = parts.map((part, i) => {
if (i % 2 === 1) return part;
try {
return decodeURIComponent(part);
} catch {
return part;
}
}).join("");
return decoded.replace(/\\/g, "/").replace(/\/+/g, "/").replace(/(.+?)\/+$/, "$1");
}
/**
* Generate paywall HTML for browser requests
*
* @param paymentRequired - Payment required response
* @param paywallConfig - Optional paywall configuration
* @param customHtml - Optional custom HTML template
* @returns HTML string
*/
generatePaywallHTML(paymentRequired, paywallConfig, customHtml) {
if (customHtml) {
return customHtml;
}
if (this.paywallProvider) {
return this.paywallProvider.generateHtml(paymentRequired, paywallConfig);
}
try {
const paywall = __require("@x402/paywall");
const displayAmount = this.getDisplayAmount(paymentRequired);
const resource = paymentRequired.resource;
return paywall.getPaywallHtml({
amount: displayAmount,
paymentRequired,
currentUrl: resource?.url || paywallConfig?.currentUrl || "",
testnet: paywallConfig?.testnet ?? true,
appName: paywallConfig?.appName,
appLogo: paywallConfig?.appLogo,
sessionTokenEndpoint: paywallConfig?.sessionTokenEndpoint
});
} catch {
}
return FALLBACK_PAYWALL_HTML;
}
/**
* Extract display amount from payment requirements.
* Uses the registered scheme's decimal precision for the asset, falling back to 6.
*
* @param paymentRequired - The payment required object
* @returns The display amount in decimal format
*/
getDisplayAmount(paymentRequired) {
const accepts = paymentRequired.accepts;
if (accepts && accepts.length > 0) {
const firstReq = accepts[0];
if ("amount" in firstReq) {
const decimals = this.ResourceServer.getAssetDecimalsForRequirements(firstReq);
return parseFloat(firstReq.amount) / 10 ** decimals;
}
}
return 0;
}
};
// src/http/httpFacilitatorClient.ts
var DEFAULT_FACILITATOR_URL = "https://x402.org/facilitator";
var GET_SUPPORTED_RETRIES = 3;
var GET_SUPPORTED_RETRY_DELAY_MS = 1e3;
var MAX_RETRY_DELAY_MS = 3e4;
function computeRetryDelay(retryAfter, attempt) {
let delay = null;
if (retryAfter !== null) {
const trimmedRetryAfter = retryAfter.trim();
if (/^\d+$/.test(trimmedRetryAfter)) {
delay = Number(trimmedRetryAfter) * 1e3;
} else {
const retryDate = Date.parse(retryAfter);
if (!isNaN(retryDate)) {
delay = retryDate - Date.now();
}
}
}
if (delay === null || delay <= 0) {
delay = GET_SUPPORTED_RETRY_DELAY_MS * Math.pow(2, attempt);
}
return Math.min(delay, MAX_RETRY_DELAY_MS);
}
var verifyResponseSchema = z.object({
isValid: z.boolean(),
invalidReason: z.string().nullish().transform((v) => v ?? void 0),
invalidMessage: z.string().nullish().transform((v) => v ?? void 0),
payer: z.string().nullish().transform((v) => v ?? void 0),
extensions: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0),
extra: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0)
});
var settleResponseSchema = z.object({
success: z.boolean(),
errorReason: z.string().nullish().transform((v) => v ?? void 0),
errorMessage: z.string().nullish().transform((v) => v ?? void 0),
payer: z.string().nullish().transform((v) => v ?? void 0),
transaction: z.string(),
network: z.custom((value) => typeof value === "string"),
amount: z.string().nullish().transform((v) => v ?? void 0),
extensions: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0),
extra: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0)
});
var supportedKindSchema = z.object({
x402Version: z.number(),
scheme: z.string(),
network: z.custom(
(value) => typeof value === "string"
),
extra: z.record(z.string(), z.unknown()).nullish().transform((v) => v ?? void 0)
});
var supportedResponseSchema = z.object({
kinds: z.array(supportedKindSchema),
extensions: z.array(z.string()).default([]),
signers: z.record(z.string(), z.array(z.string())).default({})
});
function responseExcerpt(text, limit = 200) {
const compact = text.trim().replace(/\s+/g, " ");
if (!compact) {
return "<empty response>";
}
if (compact.length <= limit) {
return compact;
}
return `${compact.slice(0, limit - 3)}...`;
}
var EXTENSION_RESPONSE_LOG_FIELD_ALLOWLIST = ["status", "rejectedReason", "reason", "code"];
function logExtensionResponsesHeader(response) {
const header = response.headers.get("EXTENSION-RESPONSES");
if (!header) return;
try {
const decoded = JSON.parse(safeBase64Decode(header));
if (!decoded || typeof decoded !== "object" || Array.isArray(decoded)) return;
const sanitized = {};
for (const [extensionKey, payload] of Object.entries(decoded)) {
const source = payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
const filtered = {};
for (const key of EXTENSION_RESPONSE_LOG_FIELD_ALLOWLIST) {
if (source[key] !== void 0) {
filtered[key] = source[key];
}
}
sanitized[extensionKey] = filtered;
}
console.log(`[x402] extension responses: ${JSON.stringify(sanitized)}`);
} catch {
}
}
async function parseSuccessResponse(response, schema, operation) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new FacilitatorResponseError(
`Facilitator ${operation} returned invalid JSON: ${responseExcerpt(text)}`
);
}
const parsed = schema.safeParse(data);
if (!parsed.success) {
throw new FacilitatorResponseError(
`Facilitator ${operation} returned invalid data: ${responseExcerpt(text)}`
);
}
return parsed.data;
}
var HTTPFacilitatorClient = class {
/**
* Creates a new HTTPFacilitatorClient instance.
*
* @param config - Configuration options for the facilitator client
*/
constructor(config) {
this.url = (config?.url || DEFAULT_FACILITATOR_URL).replace(/\/+$/, "");
this._createAuthHeaders = config?.createAuthHeaders;
}
/**
* Verify a payment with the facilitator
*
* @param paymentPayload - The payment to verify
* @param paymentRequirements - The requirements to verify against
* @returns Verification response
*/
async verify(paymentPayload, paymentRequirements) {
let headers = {
"Content-Type": "application/json"
};
if (this._createAuthHeaders) {
const authHeaders = await this.createAuthHeaders("verify");
headers = { ...headers, ...authHeaders.headers };
}
const response = await fetch(`${this.url}/verify`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
})
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(`Facilitator verify failed (${response.status}): ${responseExcerpt(text)}`);
}
if (typeof data === "object" && data !== null && "isValid" in data) {
throw new VerifyError(response.status, data);
}
throw new Error(
`Facilitator verify failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const verifyResult = await parseSuccessResponse(response, verifyResponseSchema, "verify");
logExtensionResponsesHeader(response);
return verifyResult;
}
/**
* Settle a payment with the facilitator
*
* @param paymentPayload - The payment to settle
* @param paymentRequirements - The requirements for settlement
* @returns Settlement response
*/
async settle(paymentPayload, paymentRequirements) {
let headers = {
"Content-Type": "application/json"
};
if (this._createAuthHeaders) {
const authHeaders = await this.createAuthHeaders("settle");
headers = { ...headers, ...authHeaders.headers };
}
const response = await fetch(`${this.url}/settle`, {
method: "POST",
headers,
redirect: "follow",
body: JSON.stringify({
x402Version: paymentPayload.x402Version,
paymentPayload: this.toJsonSafe(paymentPayload),
paymentRequirements: this.toJsonSafe(paymentRequirements)
})
});
if (!response.ok) {
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(`Facilitator settle failed (${response.status}): ${responseExcerpt(text)}`);
}
if (typeof data === "object" && data !== null && "success" in data) {
throw new SettleError(response.status, data);
}
throw new Error(
`Facilitator settle failed (${response.status}): ${responseExcerpt(JSON.stringify(data))}`
);
}
const settleResult = await parseSuccessResponse(response, settleResponseSchema, "settle");
logExtensionResponsesHeader(response);
return settleResult;
}
/**
* Get supported payment kinds and extensions from the facilitator.
* Retries with exponential backoff on 429 rate limit errors.
*
* @returns Supported payment kinds and extensions
*/
async getSupported() {
let headers = {
"Content-Type": "application/json"
};
if (this._createAuthHeaders) {
const authHeaders = await this.createAuthHeaders("supported");
headers = { ...headers, ...authHeaders.headers };
}
let lastError = null;
for (let attempt = 0; attempt < GET_SUPPORTED_RETRIES; attempt++) {
const response = await fetch(`${this.url}/supported`, {
method: "GET",
headers,
redirect: "follow"
});
if (response.ok) {
return parseSuccessResponse(response, supportedResponseSchema, "supported");
}
const errorText = await response.text().catch(() => response.statusText);
lastError = new Error(
`Facilitator getSupported failed (${response.status}): ${responseExcerpt(errorText)}`
);
if (response.status === 429 && attempt < GET_SUPPORTED_RETRIES - 1) {
const delay = computeRetryDelay(response.headers.get("Retry-After"), attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
throw lastError;
}
throw lastError ?? new Error("Facilitator getSupported failed after retries");
}
/**
* Creates authentication headers for a specific path.
*
* @param path - The path to create authentication headers for (e.g., "verify", "settle", "supported")
* @returns An object containing the authentication headers for the specified path
*/
async createAuthHeaders(path) {
if (!this._createAuthHeaders) {
return { headers: {} };
}
const authHeaders = await this._createAuthHeaders();
const isHeaderObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
const hasPathKey = ["verify", "settle", "supported", "bazaar"].some(
(key) => isHeaderObject(authHeaders[key])
);
const looksFlat = !hasPathKey && Object.values(authHeaders).some((value) => !isHeaderObject(value));
if (looksFlat) {
throw new Error(
'createAuthHeaders must return an object keyed by facilitator path, e.g. { verify: { Authorization: "..." }, settle: { ... }, supported: { ... } }, but received a flat headers object. See https://github.com/x402-foundation/x402/issues/2762'
);
}
const headersForPath = authHeaders[path];
return {
headers: isHeaderObject(headersForPath) ? headersForPath : {}
};
}
/**
* Helper to convert objects to JSON-safe format.
* Handles BigInt and other non-JSON types.
*
* @param obj - The object to convert
* @returns The JSON-safe representation of the object
*/
toJsonSafe(obj) {
return JSON.parse(
JSON.stringify(obj, (_, value) => typeof value === "bigint" ? value.toString() : value)
);
}
};
// src/http/x402HTTPClient.ts
var x402HTTPClient = class {
/**
* Creates a new x402HTTPClient instance.
*
* @param client - The underlying x402Client for payment logic
*/
constructor(client) {
this.client = client;
this.paymentRequiredHooks = [];
}
/**
* Register a hook to handle 402 responses before payment.
* Hooks run in order; first to return headers wins.
*
* @param hook - The hook function to register
* @returns This instance for chaining
*/
onPaymentRequired(hook) {
this.paymentRequiredHooks.push(hook);
return this;
}
/**
* Run hooks and return headers if any hook provides them.
*
* @param paymentRequired - The payment required response from the server
* @returns Headers to use for retry, or null to proceed to payment
*/
async handlePaymentRequired(paymentRequired) {
for (const hook of this.getPaymentRequiredHooks(paymentRequired)) {
const result = await hook({ paymentRequired });
if (result?.headers) {
return result.headers;
}
}
return null;
}
/**
* Encodes a payment payload into appropriate HTTP headers based on version.
*
* @param paymentPayload - The payment payload to encode
* @returns HTTP headers containing the encoded payment signature
*/
encodePaymentSignatureHeader(paymentPayload) {
switch (paymentPayload.x402Version) {
case 2:
return {
"PAYMENT-SIGNATURE": encodePaymentSignatureHeader(paymentPayload)
};
case 1:
return {
"X-PAYMENT": encodePaymentSignatureHeader(paymentPayload)
};
default:
throw new Error(
`Unsupported x402 version: ${paymentPayload.x402Version}`
);
}
}
/**
* Extracts payment required information from HTTP response.
*
* @param getHeader - Function to retrieve header value by name (case-insensitive)
* @param body - Optional response body for v1 compatibility
* @returns The payment required object
*/
getPaymentRequiredResponse(getHeader, body) {
const paymentRequired = getHeader("PAYMENT-REQUIRED");
if (paymentRequired) {
return decodePaymentRequiredHeader(paymentRequired);
}
if (body && body instanceof Object && "x402Version" in body && body.x402Version === 1) {
return body;
}
throw new Error("Invalid payment required response");
}
/**
* Extracts payment settlement response from HTTP headers.
*
* @param getHeader - Function to retrieve header value by name (case-insensitive)
* @returns The settlement response object
*/
getPaymentSettleResponse(getHeader) {
const paymentResponse = getHeader("PAYMENT-RESPONSE");
if (paymentResponse) {
return decodePaymentResponseHeader(paymentResponse);
}
const xPaymentResponse = getHeader("X-PAYMENT-RESPONSE");
if (xPaymentResponse) {
return decodePaymentResponseHeader(xPaymentResponse);
}
throw new Error("Payment response header not found");
}
/**
* Creates a payment payload for the given payment requirements.
* Delegates to the underlying x402Client.
*
* @param paymentRequired - The payment required response from the server
* @returns Promise resolving to the payment payload
*/
async createPaymentPayload(paymentRequired) {
return this.client.createPaymentPayload(paymentRequired);
}
/**
* Parses response headers into protocol types, fires payment response hooks (v2 only),
* and returns whether a hook signaled recovery.
*
* Called by transport wrappers (fetch, axios) after the paid request completes.
*
* @param paymentPayload - The payload that was sent with the request
* @param getHeader - Function to retrieve a response header by name
* @param status - The HTTP status code of the response
* @returns Whether a hook recovered and the parsed settle response (if any)
*/
async processPaymentResult(paymentPayload, getHeader, status) {
let settleResponse;
try {
settleResponse = this.getPaymentSettleResponse(getHeader);
} catch {
}
if (paymentPayload.x402Version === 1) {
return { recovered: false, settleResponse };
}
let paymentRequired;
if (!settleResponse && status === 402) {
try {
paymentRequired = this.getPaymentRequiredResponse(getHeader);
} catch {
}
}
const requirements = paymentPayload.accepted;
if (!requirements) {
throw new Error("Invalid x402 v2 payment payload: missing `accepted`");
}
const ctx = {
paymentPayload,
requirements,
...settleResponse ? { settleResponse } : {},
...paymentRequired ? { paymentRequired } : {}
};
const result = await this.client.handlePaymentResponse(ctx);
return { recovered: result?.recovered === true, settleResponse };
}
/**
* Parses HTTP status, headers, and body into an `HTTPResourceResponse`.
*
* Decodes the x402 payment header into `header`: the `PAYMENT-RESPONSE`
* settlement if present, otherwise the `PAYMENT-REQUIRED` declaration on
* 402 responses (whose `error` field carries the server's failure reason).
*
* @param args - Normalized response inputs from any HTTP transport
* @param args.status - HTTP response status code
* @param args.getHeader - Callback to read response headers by name
* @param args.body - Response body payload
* @returns The parsed status, body, and decoded payment header
*/
parsePaymentResult(args) {
const { status, getHeader, body } = args;
let header;
try {
header = this.getPaymentSettleResponse(getHeader);
} catch {
if (status === 402) {
try {
header = this.getPaymentRequiredResponse(getHeader, body);
} catch {
}
}
}
let paymentStatus = "none";
if (header && !("success" in header)) {
paymentStatus = "payment_required";
}
if (header && "success" in header) {
paymentStatus = header.success ? "settled" : "settle_failed";
}
return { status, paymentStatus, body, header };
}
/**
* Parses a fetch Response into an `HTTPResourceResponse` for app-level convenience.
*
* @param response - The fetch Response to process
* @returns The parsed status, body, and decoded payment header
*/
async processResponse(response) {
const getHeader = (name) => response.headers.get(name);
const contentType = response.headers.get("content-type") ?? "";
const body = contentType.includes("application/json") ? await response.json() : await response.text();
return this.parsePaymentResult({ status: response.status, getHeader, body });
}
/**
* Manual HTTP hooks run before extension hooks scoped to the 402 response.
*
* @param paymentRequired - The payment required response from the server
* @returns Hooks in invocation order
*/
getPaymentRequiredHooks(paymentRequired) {
const hooks = [...this.paymentRequiredHooks];
const declaredExtensions = paymentRequired.extensions;
if (!declaredExtensions) return hooks;
for (const extension of this.client.getExtensions()) {
const httpExtension = extension;
const hook = httpExtension.transportHooks?.http?.onPaymentRequired;
if (!hook || !(extension.key in declaredExtensions)) continue;
hooks.push((context) => hook(declaredExtensions[extension.key], context));
}
return hooks;
}
};
// src/http/index.ts
function encodePaymentSignatureHeader(paymentPayload) {
return safeBase64Encode(JSON.stringify(paymentPayload));
}
function decodePaymentSignatureHeader(paymentSignatureHeader) {
if (!Base64EncodedRegex.test(paymentSignatureHeader)) {
throw new Error("Invalid payment signature header");
}
return JSON.parse(safeBase64Decode(paymentSignatureHeader));
}
function encodePaymentRequiredHeader(paymentRequired) {
return safeBase64Encode(JSON.stringify(paymentRequired));
}
function decodePaymentRequiredHeader(paymentRequiredHeader) {
if (!Base64EncodedRegex.test(paymentRequiredHeader)) {
throw new Error("Invalid payment required header");
}
return JSON.parse(safeBase64Decode(paymentRequiredHeader));
}
function encodePaymentResponseHeader(paymentResponse) {
return safeBase64Encode(JSON.stringify(paymentResponse));
}
function decodePaymentResponseHeader(paymentResponseHeader) {
if (!Base64EncodedRegex.test(paymentResponseHeader)) {
throw new Error("Invalid payment response header");
}
return JSON.parse(safeBase64Decode(paymentResponseHeader));
}
export {
SETTLEMENT_OVERRIDES_HEADER,
checkIfBazaarNeeded,
RouteConfigurationError,
x402HTTPResourceServer,
HTTPFacilitatorClient,
encodePaymentSignatureHeader,
decodePaymentSignatureHeader,
encodePaymentRequiredHeader,
decodePaymentRequiredHeader,
encodePaymentResponseHeader,
decodePaymentResponseHeader,
x402HTTPClient
};
//# sourceMappingURL=chunk-4Y6I6537.mjs.map

Sorry, the diff of this file is too big to display

// src/utils/index.ts
function numberToDecimalString(n) {
const str = n.toString();
if (!/[eE]/.test(str)) return str;
const [significand, exponentStr] = str.split(/[eE]/);
const exp = parseInt(exponentStr, 10);
const negative = significand.startsWith("-");
const abs = negative ? significand.slice(1) : significand;
const [intDigits, fracDigits = ""] = abs.split(".");
const allDigits = intDigits + fracDigits;
const decimalPos = intDigits.length + exp;
let result;
if (decimalPos <= 0) {
result = "0." + "0".repeat(-decimalPos) + allDigits;
} else if (decimalPos >= allDigits.length) {
result = allDigits + "0".repeat(decimalPos - allDigits.length);
} else {
result = allDigits.slice(0, decimalPos) + "." + allDigits.slice(decimalPos);
}
return (negative ? "-" : "") + result;
}
function parseMoneyString(money) {
const cleaned = money.replace(/^\$/, "").trim();
if (!/^-?\d+(?:\.\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {
throw new Error(`Invalid money format: ${money}`);
}
const amount = Number(cleaned);
if (!Number.isFinite(amount) || amount < 0) {
throw new Error(`Invalid money format: ${money}`);
}
return amount;
}
function convertToTokenAmount(decimalAmount, decimals) {
if (/[eE]/.test(decimalAmount)) {
throw new Error(
`Invalid amount: ${decimalAmount} \u2014 use decimal notation, not scientific notation`
);
}
if (!/^-?\d+\.?\d*$/.test(decimalAmount)) {
throw new Error(`Invalid amount: ${decimalAmount}`);
}
const [intPart, decPart = ""] = decimalAmount.split(".");
const paddedDec = decPart.padEnd(decimals, "0").slice(0, decimals);
const tokenAmount = (intPart + paddedDec).replace(/^0+/, "") || "0";
if (tokenAmount === "0" && /[1-9]/.test(decimalAmount)) {
throw new Error(
`Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`
);
}
return tokenAmount;
}
var escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
var networkPatternToRegExp = (pattern) => {
const source = escapeRegExp(pattern).replace(/\\\*/g, ".*");
return new RegExp(`^${source}$`);
};
var networkMatchesPattern = (pattern, network) => {
return networkPatternToRegExp(pattern).test(network);
};
var findSchemesByNetwork = (map, network) => {
let implementationsByScheme = map.get(network);
if (!implementationsByScheme) {
for (const [registeredNetworkPattern, implementations] of map.entries()) {
if (networkMatchesPattern(registeredNetworkPattern, network)) {
implementationsByScheme = implementations;
break;
}
}
}
return implementationsByScheme;
};
var findByNetworkAndScheme = (map, scheme, network) => {
return findSchemesByNetwork(map, network)?.get(scheme);
};
var findFacilitatorBySchemeAndNetwork = (schemeMap, scheme, network) => {
const schemeData = schemeMap.get(scheme);
if (!schemeData) return void 0;
if (schemeData.networks.has(network)) {
return schemeData.facilitator;
}
if (networkMatchesPattern(schemeData.pattern, network)) {
return schemeData.facilitator;
}
return void 0;
};
var Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;
function safeBase64Encode(data) {
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
const bytes = new TextEncoder().encode(data);
const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join("");
return globalThis.btoa(binaryString);
}
return Buffer.from(data, "utf8").toString("base64");
}
function safeBase64Decode(data) {
if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") {
const binaryString = globalThis.atob(data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
const decoder = new TextDecoder("utf-8");
return decoder.decode(bytes);
}
return Buffer.from(data, "base64").toString("utf-8");
}
function deepEqual(obj1, obj2) {
const normalize = (obj) => {
if (obj === null || obj === void 0) return JSON.stringify(obj);
if (typeof obj !== "object") return JSON.stringify(obj);
if (Array.isArray(obj)) {
return JSON.stringify(
obj.map(
(item) => typeof item === "object" && item !== null ? JSON.parse(normalize(item)) : item
)
);
}
const sorted = {};
Object.keys(obj).sort().forEach((key) => {
const value = obj[key];
sorted[key] = typeof value === "object" && value !== null ? JSON.parse(normalize(value)) : value;
});
return JSON.stringify(sorted);
};
try {
return normalize(obj1) === normalize(obj2);
} catch {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
}
export {
numberToDecimalString,
parseMoneyString,
convertToTokenAmount,
networkMatchesPattern,
findSchemesByNetwork,
findByNetworkAndScheme,
findFacilitatorBySchemeAndNetwork,
Base64EncodedRegex,
safeBase64Encode,
safeBase64Decode,
deepEqual
};
//# sourceMappingURL=chunk-ABS7D6VX.mjs.map
{"version":3,"sources":["../../src/utils/index.ts"],"sourcesContent":["import { Network } from \"../types\";\n\n/**\n * Converts a JavaScript number to a plain decimal string, expanding scientific notation\n * via string manipulation rather than parseFloat round-tripping.\n *\n * e.g. 1e-7 → \"0.0000001\", 4.02 → \"4.02\"\n *\n * @param n - The number to convert\n * @returns A plain decimal string representation with no scientific notation\n */\nexport function numberToDecimalString(n: number): string {\n const str = n.toString();\n if (!/[eE]/.test(str)) return str;\n\n const [significand, exponentStr] = str.split(/[eE]/);\n const exp = parseInt(exponentStr, 10);\n const negative = significand.startsWith(\"-\");\n const abs = negative ? significand.slice(1) : significand;\n const [intDigits, fracDigits = \"\"] = abs.split(\".\");\n const allDigits = intDigits + fracDigits;\n const decimalPos = intDigits.length + exp;\n\n let result: string;\n if (decimalPos <= 0) {\n result = \"0.\" + \"0\".repeat(-decimalPos) + allDigits;\n } else if (decimalPos >= allDigits.length) {\n result = allDigits + \"0\".repeat(decimalPos - allDigits.length);\n } else {\n result = allDigits.slice(0, decimalPos) + \".\" + allDigits.slice(decimalPos);\n }\n return (negative ? \"-\" : \"\") + result;\n}\n\n/**\n * Parses a money string into a finite, non-negative decimal number.\n * Accepts plain decimal strings with an optional leading dollar sign.\n *\n * @param money - The money string to parse\n * @returns Decimal number\n */\nexport function parseMoneyString(money: string): number {\n const cleaned = money.replace(/^\\$/, \"\").trim();\n if (!/^-?\\d+(?:\\.\\d+)?$/.test(cleaned) || /[eE]/.test(cleaned)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n const amount = Number(cleaned);\n if (!Number.isFinite(amount) || amount < 0) {\n throw new Error(`Invalid money format: ${money}`);\n }\n return amount;\n}\n\n/**\n * Convert a decimal amount to token smallest units.\n * Accepts only plain decimal strings — scientific notation is not allowed.\n * Throws if the amount is non-zero but too small to represent with the given decimal precision.\n *\n * @param decimalAmount - The decimal amount as a plain string (e.g., \"0.10\")\n * @param decimals - The number of decimals for the token (e.g., 6 for USDC)\n * @returns The amount in smallest units as a string\n */\nexport function convertToTokenAmount(decimalAmount: string, decimals: number): string {\n if (/[eE]/.test(decimalAmount)) {\n throw new Error(\n `Invalid amount: ${decimalAmount} — use decimal notation, not scientific notation`,\n );\n }\n if (!/^-?\\d+\\.?\\d*$/.test(decimalAmount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const [intPart, decPart = \"\"] = decimalAmount.split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n if (tokenAmount === \"0\" && /[1-9]/.test(decimalAmount)) {\n throw new Error(\n `Amount ${decimalAmount} is too small to represent with ${decimals} decimal places`,\n );\n }\n return tokenAmount;\n}\n\n/**\n * Scheme data structure for facilitator storage\n */\nexport interface SchemeData<T> {\n facilitator: T;\n networks: Set<Network>;\n pattern: Network;\n}\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\nconst networkPatternToRegExp = (pattern: Network): RegExp => {\n const source = escapeRegExp(pattern).replace(/\\\\\\*/g, \".*\");\n return new RegExp(`^${source}$`);\n};\n\nexport const networkMatchesPattern = (pattern: Network, network: Network): boolean => {\n return networkPatternToRegExp(pattern).test(network);\n};\n\nexport const findSchemesByNetwork = <T>(\n map: Map<string, Map<string, T>>,\n network: Network,\n): Map<string, T> | undefined => {\n // Direct match first\n let implementationsByScheme = map.get(network);\n\n if (!implementationsByScheme) {\n // Try pattern matching for registered network patterns\n for (const [registeredNetworkPattern, implementations] of map.entries()) {\n if (networkMatchesPattern(registeredNetworkPattern as Network, network)) {\n implementationsByScheme = implementations;\n break;\n }\n }\n }\n\n return implementationsByScheme;\n};\n\nexport const findByNetworkAndScheme = <T>(\n map: Map<string, Map<string, T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n return findSchemesByNetwork(map, network)?.get(scheme);\n};\n\n/**\n * Finds a facilitator by scheme and network using pattern matching.\n * Works with new SchemeData storage structure.\n *\n * @param schemeMap - Map of scheme names to SchemeData\n * @param scheme - The scheme to find\n * @param network - The network to match against\n * @returns The facilitator if found, undefined otherwise\n */\nexport const findFacilitatorBySchemeAndNetwork = <T>(\n schemeMap: Map<string, SchemeData<T>>,\n scheme: string,\n network: Network,\n): T | undefined => {\n const schemeData = schemeMap.get(scheme);\n if (!schemeData) return undefined;\n\n // Check if network is in the stored networks set\n if (schemeData.networks.has(network)) {\n return schemeData.facilitator;\n }\n\n // Try pattern matching\n if (networkMatchesPattern(schemeData.pattern, network)) {\n return schemeData.facilitator;\n }\n\n return undefined;\n};\n\nexport const Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;\n\n/**\n * Encodes a string to base64 format\n *\n * @param data - The string to be encoded to base64\n * @returns The base64 encoded string\n */\nexport function safeBase64Encode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.btoa === \"function\") {\n const bytes = new TextEncoder().encode(data);\n const binaryString = Array.from(bytes, byte => String.fromCharCode(byte)).join(\"\");\n return globalThis.btoa(binaryString);\n }\n return Buffer.from(data, \"utf8\").toString(\"base64\");\n}\n\n/**\n * Decodes a base64 string back to its original format\n *\n * @param data - The base64 encoded string to be decoded\n * @returns The decoded string in UTF-8 format\n */\nexport function safeBase64Decode(data: string): string {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.atob === \"function\") {\n const binaryString = globalThis.atob(data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n const decoder = new TextDecoder(\"utf-8\");\n return decoder.decode(bytes);\n }\n return Buffer.from(data, \"base64\").toString(\"utf-8\");\n}\n\n/**\n * Deep equality comparison for payment requirements\n * Uses a normalized JSON.stringify for consistent comparison\n *\n * @param obj1 - First object to compare\n * @param obj2 - Second object to compare\n * @returns True if objects are deeply equal\n */\nexport function deepEqual(obj1: unknown, obj2: unknown): boolean {\n // Normalize and stringify both objects for comparison\n // This handles nested objects, arrays, and different property orders\n const normalize = (obj: unknown): string => {\n // Handle primitives and null/undefined\n if (obj === null || obj === undefined) return JSON.stringify(obj);\n if (typeof obj !== \"object\") return JSON.stringify(obj);\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return JSON.stringify(\n obj.map(item =>\n typeof item === \"object\" && item !== null ? JSON.parse(normalize(item)) : item,\n ),\n );\n }\n\n // Handle objects - sort keys and recursively normalize values\n const sorted: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach(key => {\n const value = (obj as Record<string, unknown>)[key];\n sorted[key] =\n typeof value === \"object\" && value !== null ? JSON.parse(normalize(value)) : value;\n });\n return JSON.stringify(sorted);\n };\n\n try {\n return normalize(obj1) === normalize(obj2);\n } catch {\n // Fallback to simple comparison if norm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AAK,MAAM,UAAU,IAAI,CAAC,IAAI;AAAA,QAC5E;AAAA,MACF;AAAA,IACF;AAGA,UAAM,SAAkC,CAAC;AACzC,WAAO,KAAK,GAA8B,EACvC,KAAK,EACL,QAAQ,SAAO;AACd,YAAM,QAAS,IAAgC,GAAG;AAClD,aAAO,GAAG,IACR,OAAO,UAAU,YAAY,UAAU,OAAO,KAAK,MAAM,UAAU,KAAK,CAAC,IAAI;AAAA,IACjF,CAAC;AACH,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AAEA,MAAI;AACF,WAAO,UAAU,IAAI,MAAM,UAAU,IAAI;AAAA,EAC3C,QAAQ;AAEN,WAAO,KAAK,UAAU,IAAI,MAAM,KAAK,UAAU,IAAI;AAAA,EACrD;AACF;","names":[]}
// src/types/facilitator.ts
var VerifyError = class extends Error {
/**
* Creates a VerifyError from a failed verification response.
*
* @param statusCode - HTTP status code from the facilitator
* @param response - The verify response containing failure details
*/
constructor(statusCode, response) {
const reason = response.invalidReason || "unknown reason";
const message = response.invalidMessage;
super(message ? `${reason}: ${message}` : reason);
this.name = "VerifyError";
this.statusCode = statusCode;
this.invalidReason = response.invalidReason;
this.invalidMessage = response.invalidMessage;
this.payer = response.payer;
}
};
var SettleError = class extends Error {
/**
* Creates a SettleError from a failed settlement response.
*
* @param statusCode - HTTP status code from the facilitator
* @param response - The settle response containing error details
*/
constructor(statusCode, response) {
const reason = response.errorReason || "unknown reason";
const message = response.errorMessage;
super(message ? `${reason}: ${message}` : reason);
this.name = "SettleError";
this.statusCode = statusCode;
this.errorReason = response.errorReason;
this.errorMessage = response.errorMessage;
this.payer = response.payer;
this.transaction = response.transaction;
this.network = response.network;
}
};
var FacilitatorResponseError = class extends Error {
/**
* Creates a FacilitatorResponseError for malformed facilitator responses.
*
* @param message - The boundary error message
*/
constructor(message) {
super(message);
this.name = "FacilitatorResponseError";
}
};
function getFacilitatorResponseError(error) {
let current = error;
while (current instanceof Error) {
if (current instanceof FacilitatorResponseError) {
return current;
}
current = current.cause;
}
return null;
}
export {
VerifyError,
SettleError,
FacilitatorResponseError,
getFacilitatorResponseError
};
//# sourceMappingURL=chunk-AGOUMC4P.mjs.map
{"version":3,"sources":["../../src/types/facilitator.ts"],"sourcesContent":["import { PaymentPayload, PaymentRequirements } from \"./payments\";\nimport { Network } from \"./\";\n\nexport type VerifyRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type VerifyResponse = {\n isValid: boolean;\n invalidReason?: string;\n invalidMessage?: string;\n payer?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SettleRequest = {\n x402Version: number;\n paymentPayload: PaymentPayload;\n paymentRequirements: PaymentRequirements;\n};\n\nexport type SettleResponse = {\n success: boolean;\n errorReason?: string;\n errorMessage?: string;\n payer?: string;\n transaction: string;\n network: Network;\n /** Actual amount settled in atomic token units. Present for schemes like `upto` where settlement amount may differ from the authorized maximum. */\n amount?: string;\n extensions?: Record<string, unknown>;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedKind = {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n};\n\nexport type SupportedResponse = {\n kinds: SupportedKind[];\n extensions: string[];\n signers: Record<string, string[]>; // CAIP family pattern → Signer addresses\n};\n\n/**\n * Error thrown when payment verification fails.\n */\nexport class VerifyError extends Error {\n readonly invalidReason?: string;\n readonly invalidMessage?: string;\n readonly payer?: string;\n readonly statusCode: number;\n\n /**\n * Creates a VerifyError from a failed verification response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The verify response containing failure details\n */\n constructor(statusCode: number, response: VerifyResponse) {\n const reason = response.invalidReason || \"unknown reason\";\n const message = response.invalidMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"VerifyError\";\n this.statusCode = statusCode;\n this.invalidReason = response.invalidReason;\n this.invalidMessage = response.invalidMessage;\n this.payer = response.payer;\n }\n}\n\n/**\n * Error thrown when payment settlement fails.\n */\nexport class SettleError extends Error {\n readonly errorReason?: string;\n readonly errorMessage?: string;\n readonly payer?: string;\n readonly transaction: string;\n readonly network: Network;\n readonly statusCode: number;\n\n /**\n * Creates a SettleError from a failed settlement response.\n *\n * @param statusCode - HTTP status code from the facilitator\n * @param response - The settle response containing error details\n */\n constructor(statusCode: number, response: SettleResponse) {\n const reason = response.errorReason || \"unknown reason\";\n const message = response.errorMessage;\n super(message ? `${reason}: ${message}` : reason);\n this.name = \"SettleError\";\n this.statusCode = statusCode;\n this.errorReason = response.errorReason;\n this.errorMessage = response.errorMessage;\n this.payer = response.payer;\n this.transaction = response.transaction;\n this.network = response.network;\n }\n}\n\n/**\n * Error thrown when a facilitator returns malformed success payload data.\n */\nexport class FacilitatorResponseError extends Error {\n /**\n * Creates a FacilitatorResponseError for malformed facilitator responses.\n *\n * @param message - The boundary error message\n */\n constructor(message: string) {\n super(message);\n this.name = \"FacilitatorResponseError\";\n }\n}\n\n/**\n * Walks an error cause chain to find the first facilitator response error.\n *\n * @param error - The thrown value to inspect\n * @returns The nested facilitator response error, if present\n */\nexport function getFacilitatorResponseError(error: unknown): FacilitatorResponseError | null {\n let current = error;\n\n while (current instanceof Error) {\n if (current instanceof FacilitatorResponseError) {\n return current;\n }\n current = current.cause;\n }\n\n return null;\n}\n"],"mappings":";AAqDO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,iBAAiB;AACzC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,gBAAgB,SAAS;AAC9B,SAAK,iBAAiB,SAAS;AAC/B,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAKO,IAAM,cAAN,cAA0B,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcrC,YAAY,YAAoB,UAA0B;AACxD,UAAM,SAAS,SAAS,eAAe;AACvC,UAAM,UAAU,SAAS;AACzB,UAAM,UAAU,GAAG,MAAM,KAAK,OAAO,KAAK,MAAM;AAChD,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,cAAc,SAAS;AAC5B,SAAK,eAAe,SAAS;AAC7B,SAAK,QAAQ,SAAS;AACtB,SAAK,cAAc,SAAS;AAC5B,SAAK,UAAU,SAAS;AAAA,EAC1B;AACF;AAKO,IAAM,2BAAN,cAAuC,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMlD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAQO,SAAS,4BAA4B,OAAiD;AAC3F,MAAI,UAAU;AAEd,SAAO,mBAAmB,OAAO;AAC/B,QAAI,mBAAmB,0BAA0B;AAC/C,aAAO;AAAA,IACT;AACA,cAAU,QAAQ;AAAA,EACpB;AAEA,SAAO;AACT;","names":[]}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display