@payai/x402-evm 2.3.5 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/cjs/exact/client/index.d.ts +3 -2
  2. package/dist/cjs/exact/client/index.js +175 -194
  3. package/dist/cjs/exact/client/index.js.map +1 -1
  4. package/dist/cjs/exact/facilitator/index.js +201 -185
  5. package/dist/cjs/exact/facilitator/index.js.map +1 -1
  6. package/dist/cjs/exact/server/index.d.ts +18 -17
  7. package/dist/cjs/exact/server/index.js +100 -55
  8. package/dist/cjs/exact/server/index.js.map +1 -1
  9. package/dist/cjs/exact/v1/client/index.js +5 -1
  10. package/dist/cjs/exact/v1/client/index.js.map +1 -1
  11. package/dist/cjs/exact/v1/facilitator/index.js +5 -1
  12. package/dist/cjs/exact/v1/facilitator/index.js.map +1 -1
  13. package/dist/cjs/index.d.ts +38 -2
  14. package/dist/cjs/index.js +443 -191
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/{permit2-U9Zolx3O.d.ts → permit2-CyZxwngN.d.ts} +278 -87
  17. package/dist/cjs/scheme-CXDF0D2A.d.ts +47 -0
  18. package/dist/cjs/upto/client/index.d.ts +32 -0
  19. package/dist/cjs/upto/client/index.js +507 -0
  20. package/dist/cjs/upto/client/index.js.map +1 -0
  21. package/dist/cjs/upto/facilitator/index.d.ts +52 -0
  22. package/dist/cjs/upto/facilitator/index.js +1233 -0
  23. package/dist/cjs/upto/facilitator/index.js.map +1 -0
  24. package/dist/cjs/upto/server/index.d.ts +77 -0
  25. package/dist/cjs/upto/server/index.js +246 -0
  26. package/dist/cjs/upto/server/index.js.map +1 -0
  27. package/dist/cjs/v1/index.d.ts +4 -0
  28. package/dist/cjs/v1/index.js +5 -1
  29. package/dist/cjs/v1/index.js.map +1 -1
  30. package/dist/esm/chunk-C4ZQMS77.mjs +629 -0
  31. package/dist/esm/chunk-C4ZQMS77.mjs.map +1 -0
  32. package/dist/esm/chunk-CRT6YNY5.mjs +529 -0
  33. package/dist/esm/chunk-CRT6YNY5.mjs.map +1 -0
  34. package/dist/esm/chunk-D6RXZXOS.mjs +158 -0
  35. package/dist/esm/chunk-D6RXZXOS.mjs.map +1 -0
  36. package/dist/esm/chunk-GJ57SZGI.mjs +121 -0
  37. package/dist/esm/chunk-GJ57SZGI.mjs.map +1 -0
  38. package/dist/esm/chunk-JII456TS.mjs +34 -0
  39. package/dist/esm/chunk-JII456TS.mjs.map +1 -0
  40. package/dist/esm/chunk-NSFLAANF.mjs +80 -0
  41. package/dist/esm/chunk-NSFLAANF.mjs.map +1 -0
  42. package/dist/esm/{chunk-IZEI7JTG.mjs → chunk-RYT6M3PA.mjs} +31 -501
  43. package/dist/esm/chunk-RYT6M3PA.mjs.map +1 -0
  44. package/dist/esm/chunk-WKBC5YMI.mjs +291 -0
  45. package/dist/esm/chunk-WKBC5YMI.mjs.map +1 -0
  46. package/dist/esm/exact/client/index.d.mts +3 -2
  47. package/dist/esm/exact/client/index.mjs +8 -5
  48. package/dist/esm/exact/facilitator/index.mjs +84 -430
  49. package/dist/esm/exact/facilitator/index.mjs.map +1 -1
  50. package/dist/esm/exact/server/index.d.mts +18 -17
  51. package/dist/esm/exact/server/index.mjs +28 -55
  52. package/dist/esm/exact/server/index.mjs.map +1 -1
  53. package/dist/esm/exact/v1/client/index.mjs +2 -1
  54. package/dist/esm/exact/v1/facilitator/index.mjs +2 -1
  55. package/dist/esm/index.d.mts +38 -2
  56. package/dist/esm/index.mjs +21 -8
  57. package/dist/esm/index.mjs.map +1 -1
  58. package/dist/esm/{permit2-Bbh3a8_h.d.mts → permit2-CyZxwngN.d.mts} +278 -87
  59. package/dist/esm/scheme-DCR7hsa3.d.mts +47 -0
  60. package/dist/esm/upto/client/index.d.mts +32 -0
  61. package/dist/esm/upto/client/index.mjs +18 -0
  62. package/dist/esm/upto/client/index.mjs.map +1 -0
  63. package/dist/esm/upto/facilitator/index.d.mts +52 -0
  64. package/dist/esm/upto/facilitator/index.mjs +473 -0
  65. package/dist/esm/upto/facilitator/index.mjs.map +1 -0
  66. package/dist/esm/upto/server/index.d.mts +77 -0
  67. package/dist/esm/upto/server/index.mjs +145 -0
  68. package/dist/esm/upto/server/index.mjs.map +1 -0
  69. package/dist/esm/v1/index.d.mts +4 -0
  70. package/dist/esm/v1/index.mjs +2 -1
  71. package/package.json +31 -1
  72. package/dist/esm/chunk-GD4MKCN7.mjs +0 -57
  73. package/dist/esm/chunk-GD4MKCN7.mjs.map +0 -1
  74. package/dist/esm/chunk-IZEI7JTG.mjs.map +0 -1
  75. package/dist/esm/chunk-TKN5V2BV.mjs +0 -13
  76. package/dist/esm/chunk-TKN5V2BV.mjs.map +0 -1
  77. package/dist/esm/chunk-WJWNS4G4.mjs +0 -518
  78. package/dist/esm/chunk-WJWNS4G4.mjs.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/upto/facilitator/index.ts","../../../../src/types.ts","../../../../src/exact/extensions.ts","../../../../src/upto/facilitator/permit2.ts","../../../../src/constants.ts","../../../../src/exact/facilitator/errors.ts","../../../../src/upto/facilitator/errors.ts","../../../../src/utils.ts","../../../../src/shared/erc20approval.ts","../../../../src/shared/permit2.ts","../../../../src/multicall.ts","../../../../src/upto/facilitator/scheme.ts"],"sourcesContent":["// Note: No register.ts helper — V1 backward compatibility is not needed for upto.\n// Use direct class instantiation: facilitator.register(\"eip155:*\", new UptoEvmScheme(signer))\nexport { UptoEvmScheme } from \"./scheme\";\n","/**\n * Asset transfer methods for the exact EVM scheme.\n * - eip3009: Uses transferWithAuthorization (USDC, etc.) - recommended for compatible tokens\n * - permit2: Uses Permit2 + x402Permit2Proxy - universal fallback for any ERC-20\n */\nexport type AssetTransferMethod = \"eip3009\" | \"permit2\";\n\n/**\n * EIP-3009 payload for tokens with native transferWithAuthorization support.\n */\nexport type ExactEIP3009Payload = {\n signature?: `0x${string}`;\n authorization: {\n from: `0x${string}`;\n to: `0x${string}`;\n value: string;\n validAfter: string;\n validBefore: string;\n nonce: `0x${string}`;\n };\n};\n\n/**\n * Permit2 witness data structure.\n * Matches the Witness struct in x402Permit2Proxy contract.\n * Note: Upper time bound is enforced by Permit2's `deadline` field, not a witness field.\n */\nexport type Permit2Witness = {\n to: `0x${string}`;\n validAfter: string;\n};\n\n/**\n * Permit2 authorization parameters.\n * Used to reconstruct the signed message for verification.\n */\nexport type Permit2Authorization = {\n permitted: {\n token: `0x${string}`;\n amount: string;\n };\n spender: `0x${string}`;\n nonce: string;\n deadline: string;\n witness: Permit2Witness;\n};\n\n/**\n * Permit2 payload for tokens using the Permit2 + x402Permit2Proxy flow.\n */\nexport type ExactPermit2Payload = {\n signature: `0x${string}`;\n permit2Authorization: Permit2Authorization & {\n from: `0x${string}`;\n };\n};\n\nexport type ExactEvmPayloadV1 = ExactEIP3009Payload;\n\nexport type ExactEvmPayloadV2 = ExactEIP3009Payload | ExactPermit2Payload;\n\n/**\n * Type guard to check if a payload is a Permit2 payload.\n * Permit2 payloads have a `permit2Authorization` field.\n *\n * @param payload - The payload to check.\n * @returns True if the payload is a Permit2 payload, false otherwise.\n */\nexport function isPermit2Payload(payload: ExactEvmPayloadV2): payload is ExactPermit2Payload {\n return \"permit2Authorization\" in payload;\n}\n\n/**\n * Type guard to check if a payload is an EIP-3009 payload.\n * EIP-3009 payloads have an `authorization` field.\n *\n * @param payload - The payload to check.\n * @returns True if the payload is an EIP-3009 payload, false otherwise.\n */\nexport function isEIP3009Payload(payload: ExactEvmPayloadV2): payload is ExactEIP3009Payload {\n return \"authorization\" in payload;\n}\n\n/**\n * Upto Permit2 witness — includes `facilitator` field absent from exact witness.\n * Only the address matching `witness.facilitator` can call settle() on-chain.\n */\nexport type UptoPermit2Witness = {\n to: `0x${string}`;\n facilitator: `0x${string}`;\n validAfter: string;\n};\n\nexport type UptoPermit2Authorization = {\n permitted: {\n token: `0x${string}`;\n amount: string;\n };\n spender: `0x${string}`;\n nonce: string;\n deadline: string;\n witness: UptoPermit2Witness;\n};\n\nexport type UptoPermit2Payload = {\n signature: `0x${string}`;\n permit2Authorization: UptoPermit2Authorization & {\n from: `0x${string}`;\n };\n};\n\n/**\n * Type guard to check if a payload is an upto Permit2 payload.\n * Validates structural presence of all required fields: signature, permit2Authorization\n * (with from, permitted, spender, nonce, deadline), and a witness containing facilitator.\n *\n * @param payload - The payload to check\n * @returns True if the payload is an upto Permit2 payload, false otherwise\n */\nexport function isUptoPermit2Payload(\n payload: Record<string, unknown>,\n): payload is UptoPermit2Payload {\n if (typeof payload.signature !== \"string\") return false;\n if (!(\"permit2Authorization\" in payload)) return false;\n\n const auth = payload.permit2Authorization;\n if (typeof auth !== \"object\" || auth === null) return false;\n\n const a = auth as Record<string, unknown>;\n if (typeof a.from !== \"string\") return false;\n if (typeof a.spender !== \"string\") return false;\n if (typeof a.nonce !== \"string\") return false;\n if (typeof a.deadline !== \"string\") return false;\n\n const permitted = a.permitted;\n if (typeof permitted !== \"object\" || permitted === null) return false;\n const p = permitted as Record<string, unknown>;\n if (typeof p.token !== \"string\") return false;\n if (typeof p.amount !== \"string\") return false;\n\n const witness = a.witness;\n if (typeof witness !== \"object\" || witness === null) return false;\n const w = witness as Record<string, unknown>;\n return (\n typeof w.facilitator === \"string\" &&\n typeof w.to === \"string\" &&\n typeof w.validAfter === \"string\"\n );\n}\n","import type { PaymentPayload } from \"@payai/x402/types\";\nimport type { FacilitatorEvmSigner } from \"../signer\";\n\nexport const EIP2612_GAS_SPONSORING_KEY = \"eip2612GasSponsoring\" as const;\nexport const ERC20_APPROVAL_GAS_SPONSORING_KEY = \"erc20ApprovalGasSponsoring\" as const;\nexport const ERC20_APPROVAL_GAS_SPONSORING_VERSION = \"1\" as const;\n\nexport interface Eip2612GasSponsoringInfo {\n [key: string]: unknown;\n from: string;\n asset: string;\n spender: string;\n amount: string;\n nonce: string;\n deadline: string;\n signature: string;\n version: string;\n}\n\nexport interface Erc20ApprovalGasSponsoringInfo {\n [key: string]: unknown;\n from: `0x${string}`;\n asset: `0x${string}`;\n spender: `0x${string}`;\n amount: string;\n signedTransaction: `0x${string}`;\n version: string;\n}\n\n/**\n * A single transaction to be executed by the signer.\n * - `0x${string}`: a pre-signed serialized transaction (broadcast as-is via sendRawTransaction)\n * - `{ to, data, gas? }`: an unsigned call intent (signer signs and broadcasts)\n */\nexport type TransactionRequest =\n | `0x${string}`\n | { to: `0x${string}`; data: `0x${string}`; gas?: bigint };\n\nexport type Erc20ApprovalGasSponsoringSigner = FacilitatorEvmSigner & {\n sendTransactions(transactions: TransactionRequest[]): Promise<`0x${string}`[]>;\n simulateTransactions?(transactions: TransactionRequest[]): Promise<boolean>;\n};\n\nexport interface Erc20ApprovalGasSponsoringFacilitatorExtension {\n key: typeof ERC20_APPROVAL_GAS_SPONSORING_KEY;\n signer?: Erc20ApprovalGasSponsoringSigner;\n signerForNetwork?: (network: string) => Erc20ApprovalGasSponsoringSigner | undefined;\n}\n\n/**\n * Extracts a typed `info` payload from an extension entry.\n *\n * @param payload - Payment payload containing optional extensions.\n * @param extensionKey - Extension key to extract.\n * @returns The extension `info` object when present; otherwise null.\n */\nfunction _extractInfo(\n payload: PaymentPayload,\n extensionKey: string,\n): Record<string, unknown> | null {\n const extensions = payload.extensions;\n if (!extensions) return null;\n const extension = extensions[extensionKey] as { info?: Record<string, unknown> } | undefined;\n if (!extension?.info) return null;\n return extension.info;\n}\n\n/**\n * Extracts and validates required EIP-2612 gas sponsoring fields.\n *\n * @param payload - Payment payload returned by the client scheme.\n * @returns Parsed EIP-2612 gas sponsoring info when available and complete.\n */\nexport function extractEip2612GasSponsoringInfo(\n payload: PaymentPayload,\n): Eip2612GasSponsoringInfo | null {\n const info = _extractInfo(payload, EIP2612_GAS_SPONSORING_KEY);\n if (!info) return null;\n if (\n !info.from ||\n !info.asset ||\n !info.spender ||\n !info.amount ||\n !info.nonce ||\n !info.deadline ||\n !info.signature ||\n !info.version\n ) {\n return null;\n }\n return info as unknown as Eip2612GasSponsoringInfo;\n}\n\n/**\n * Validates the structure and formatting of EIP-2612 sponsoring info.\n *\n * @param info - EIP-2612 extension info to validate.\n * @returns True when all required fields match expected patterns.\n */\nexport function validateEip2612GasSponsoringInfo(info: Eip2612GasSponsoringInfo): boolean {\n const addressPattern = /^0x[a-fA-F0-9]{40}$/;\n const numericPattern = /^[0-9]+$/;\n const hexPattern = /^0x[a-fA-F0-9]+$/;\n const versionPattern = /^[0-9]+(\\.[0-9]+)*$/;\n return (\n addressPattern.test(info.from) &&\n addressPattern.test(info.asset) &&\n addressPattern.test(info.spender) &&\n numericPattern.test(info.amount) &&\n numericPattern.test(info.nonce) &&\n numericPattern.test(info.deadline) &&\n hexPattern.test(info.signature) &&\n versionPattern.test(info.version)\n );\n}\n\n/**\n * Extracts and validates required ERC-20 approval sponsoring fields.\n *\n * @param payload - Payment payload returned by the client scheme.\n * @returns Parsed ERC-20 approval sponsoring info when available and complete.\n */\nexport function extractErc20ApprovalGasSponsoringInfo(\n payload: PaymentPayload,\n): Erc20ApprovalGasSponsoringInfo | null {\n const info = _extractInfo(payload, ERC20_APPROVAL_GAS_SPONSORING_KEY);\n if (!info) return null;\n if (\n !info.from ||\n !info.asset ||\n !info.spender ||\n !info.amount ||\n !info.signedTransaction ||\n !info.version\n ) {\n return null;\n }\n return info as unknown as Erc20ApprovalGasSponsoringInfo;\n}\n\n/**\n * Validates the structure and formatting of ERC-20 approval sponsoring info.\n *\n * @param info - ERC-20 approval extension info to validate.\n * @returns True when all required fields match expected patterns.\n */\nexport function validateErc20ApprovalGasSponsoringInfo(\n info: Erc20ApprovalGasSponsoringInfo,\n): boolean {\n const addressPattern = /^0x[a-fA-F0-9]{40}$/;\n const numericPattern = /^[0-9]+$/;\n const hexPattern = /^0x[a-fA-F0-9]+$/;\n const versionPattern = /^[0-9]+(\\.[0-9]+)*$/;\n return (\n addressPattern.test(info.from) &&\n addressPattern.test(info.asset) &&\n addressPattern.test(info.spender) &&\n numericPattern.test(info.amount) &&\n hexPattern.test(info.signedTransaction) &&\n versionPattern.test(info.version)\n );\n}\n\n/**\n * Resolves the ERC-20 approval extension signer for a specific network.\n *\n * @param extension - Optional facilitator extension config.\n * @param network - CAIP-2 network identifier.\n * @returns A network-specific signer when available, else the default signer.\n */\nexport function resolveErc20ApprovalExtensionSigner(\n extension: Erc20ApprovalGasSponsoringFacilitatorExtension | undefined,\n network: string,\n): Erc20ApprovalGasSponsoringSigner | undefined {\n if (!extension) return undefined;\n return extension.signerForNetwork?.(network) ?? extension.signer;\n}\n","import {\n PaymentPayload,\n PaymentRequirements,\n FacilitatorContext,\n SettleResponse,\n VerifyResponse,\n} from \"@payai/x402/types\";\nimport {\n extractEip2612GasSponsoringInfo,\n extractErc20ApprovalGasSponsoringInfo,\n ERC20_APPROVAL_GAS_SPONSORING_KEY,\n resolveErc20ApprovalExtensionSigner,\n type Erc20ApprovalGasSponsoringFacilitatorExtension,\n type Erc20ApprovalGasSponsoringSigner,\n} from \"../../exact/extensions\";\nimport { getAddress, encodeFunctionData } from \"viem\";\nimport {\n PERMIT2_ADDRESS,\n uptoPermit2WitnessTypes,\n x402UptoPermit2ProxyABI,\n x402UptoPermit2ProxyAddress,\n} from \"../../constants\";\nimport {\n ErrPermit2AmountMismatch,\n ErrUptoSettlementExceedsAmount,\n ErrUptoFacilitatorMismatch,\n ErrUptoInvalidScheme,\n ErrUptoNetworkMismatch,\n} from \"./errors\";\nimport { FacilitatorEvmSigner } from \"../../signer\";\nimport { UptoPermit2Payload } from \"../../types\";\nimport { getEvmChainId } from \"../../utils\";\nimport { validateErc20ApprovalForPayment } from \"../../shared/erc20approval\";\nimport {\n buildUptoPermit2SettleArgs,\n waitAndReturnSettleResponse,\n mapSettleError,\n splitEip2612Signature,\n simulatePermit2Settle,\n simulatePermit2SettleWithPermit,\n simulatePermit2SettleWithErc20Approval,\n diagnosePermit2SimulationFailure,\n checkPermit2Prerequisites,\n validateEip2612PermitForPayment,\n type Permit2ProxyConfig,\n} from \"../../shared/permit2\";\nimport type { Eip2612GasSponsoringInfo } from \"../../exact/extensions\";\n\nconst uptoProxyConfig: Permit2ProxyConfig = {\n proxyAddress: x402UptoPermit2ProxyAddress,\n proxyABI: x402UptoPermit2ProxyABI,\n};\n\nexport interface VerifyUptoPermit2Options {\n simulate?: boolean;\n}\n\nexport interface UptoPermit2FacilitatorConfig {\n simulateInSettle?: boolean;\n}\n\n/**\n * Verifies an upto Permit2 payment payload against the given requirements.\n *\n * Validates scheme, network, spender, recipient, facilitator, deadline, amount,\n * token, signature, Permit2 allowance, and payer balance.\n *\n * @param signer - The facilitator signer for contract reads and signature verification\n * @param payload - The payment payload to verify\n * @param requirements - The payment requirements to verify against\n * @param permit2Payload - The upto Permit2 specific payload with witness data\n * @param context - Optional facilitator context for extension-provided capabilities\n * @param options - Optional verification options (e.g., skip simulation)\n * @returns Promise resolving to a verification response indicating validity\n */\nexport async function verifyUptoPermit2(\n signer: FacilitatorEvmSigner,\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n permit2Payload: UptoPermit2Payload,\n context?: FacilitatorContext,\n options?: VerifyUptoPermit2Options,\n): Promise<VerifyResponse> {\n const payer = permit2Payload.permit2Authorization.from;\n\n if (payload.accepted.scheme !== \"upto\" || requirements.scheme !== \"upto\") {\n return {\n isValid: false,\n invalidReason: ErrUptoInvalidScheme,\n payer,\n };\n }\n\n if (payload.accepted.network !== requirements.network) {\n return {\n isValid: false,\n invalidReason: ErrUptoNetworkMismatch,\n payer,\n };\n }\n\n const chainId = getEvmChainId(requirements.network);\n const tokenAddress = getAddress(requirements.asset);\n\n if (\n getAddress(permit2Payload.permit2Authorization.spender) !==\n getAddress(x402UptoPermit2ProxyAddress)\n ) {\n return {\n isValid: false,\n invalidReason: \"invalid_permit2_spender\",\n payer,\n };\n }\n\n if (\n getAddress(permit2Payload.permit2Authorization.witness.to) !== getAddress(requirements.payTo)\n ) {\n return {\n isValid: false,\n invalidReason: \"invalid_permit2_recipient_mismatch\",\n payer,\n };\n }\n\n // Verify the facilitator address in the witness matches our own address\n const facilitatorAddresses = signer.getAddresses();\n const witnessFacilitator = getAddress(permit2Payload.permit2Authorization.witness.facilitator);\n const isFacilitatorMatch = facilitatorAddresses.some(\n addr => getAddress(addr) === witnessFacilitator,\n );\n if (!isFacilitatorMatch) {\n return {\n isValid: false,\n invalidReason: ErrUptoFacilitatorMismatch,\n payer,\n };\n }\n\n const now = Math.floor(Date.now() / 1000);\n if (BigInt(permit2Payload.permit2Authorization.deadline) < BigInt(now + 6)) {\n return {\n isValid: false,\n invalidReason: \"permit2_deadline_expired\",\n payer,\n };\n }\n\n if (BigInt(permit2Payload.permit2Authorization.witness.validAfter) > BigInt(now)) {\n return {\n isValid: false,\n invalidReason: \"permit2_not_yet_valid\",\n payer,\n };\n }\n\n if (\n BigInt(permit2Payload.permit2Authorization.permitted.amount) !== BigInt(requirements.amount)\n ) {\n return {\n isValid: false,\n invalidReason: ErrPermit2AmountMismatch,\n payer,\n };\n }\n\n if (getAddress(permit2Payload.permit2Authorization.permitted.token) !== tokenAddress) {\n return {\n isValid: false,\n invalidReason: \"permit2_token_mismatch\",\n payer,\n };\n }\n\n // Verify signature using upto-specific witness types (includes facilitator)\n const permit2TypedData = {\n types: uptoPermit2WitnessTypes,\n primaryType: \"PermitWitnessTransferFrom\" as const,\n domain: {\n name: \"Permit2\",\n chainId,\n verifyingContract: PERMIT2_ADDRESS,\n },\n message: {\n permitted: {\n token: getAddress(permit2Payload.permit2Authorization.permitted.token),\n amount: BigInt(permit2Payload.permit2Authorization.permitted.amount),\n },\n spender: getAddress(permit2Payload.permit2Authorization.spender),\n nonce: BigInt(permit2Payload.permit2Authorization.nonce),\n deadline: BigInt(permit2Payload.permit2Authorization.deadline),\n witness: {\n to: getAddress(permit2Payload.permit2Authorization.witness.to),\n facilitator: getAddress(permit2Payload.permit2Authorization.witness.facilitator),\n validAfter: BigInt(permit2Payload.permit2Authorization.witness.validAfter),\n },\n },\n };\n\n // Verify signature\n // Note: verifyTypedData is implementation-dependent and pluggable on FacilitatorEvmSigner\n // Some implementations only do EOA-style ECDSA recovery (e.g. viem/utils verifyTypedData, ethers.verifyTypedData)\n // Viem's publicClient.verifyTypedData supports EOA and Smart Contract Account (ERC-1271 / ERC-6492) signature verification\n let signatureValid = false;\n try {\n signatureValid = await signer.verifyTypedData({\n address: payer,\n ...permit2TypedData,\n signature: permit2Payload.signature,\n });\n } catch {\n signatureValid = false;\n }\n\n if (!signatureValid) {\n // Check if the payer is a deployed smart contract (ERC-1271 / ERC-6492)\n const bytecode = await signer.getCode({ address: payer });\n const isDeployedContract = bytecode && bytecode !== \"0x\";\n\n if (!isDeployedContract) {\n return {\n isValid: false,\n invalidReason: \"invalid_permit2_signature\",\n payer,\n };\n }\n // Deployed smart contract: fall through to simulation\n }\n\n // If simulation is disabled, return early\n if (options?.simulate === false) {\n return { isValid: true, invalidReason: undefined, payer };\n }\n\n const facilitatorAddress = getAddress(permit2Payload.permit2Authorization.witness.facilitator);\n // Per spec §Phase 3 Step 7: simulate with requirements.amount (the worst-case charge).\n // At verify time, requirements.amount = max authorized amount.\n // At settle time, requirements.amount = actual settlement amount (≤ max).\n const uptoSettleArgs = buildUptoPermit2SettleArgs(\n permit2Payload,\n BigInt(requirements.amount),\n facilitatorAddress,\n );\n\n const eip2612InfoForSim = extractEip2612GasSponsoringInfo(payload);\n if (eip2612InfoForSim) {\n const fieldResult = validateEip2612PermitForPayment(eip2612InfoForSim, payer, tokenAddress);\n if (!fieldResult.isValid) {\n return { isValid: false, invalidReason: fieldResult.invalidReason!, payer };\n }\n\n const simOk = await simulatePermit2SettleWithPermit(\n uptoProxyConfig,\n signer,\n uptoSettleArgs,\n eip2612InfoForSim,\n );\n if (!simOk) {\n return diagnosePermit2SimulationFailure(\n uptoProxyConfig,\n signer,\n tokenAddress,\n permit2Payload,\n requirements.amount,\n );\n }\n\n return { isValid: true, invalidReason: undefined, payer };\n }\n\n const erc20GasSponsorshipExtension =\n context?.getExtension<Erc20ApprovalGasSponsoringFacilitatorExtension>(\n ERC20_APPROVAL_GAS_SPONSORING_KEY,\n );\n if (erc20GasSponsorshipExtension) {\n const erc20Info = extractErc20ApprovalGasSponsoringInfo(payload);\n if (erc20Info) {\n const fieldResult = await validateErc20ApprovalForPayment(erc20Info, payer, tokenAddress);\n if (!fieldResult.isValid) {\n return { isValid: false, invalidReason: fieldResult.invalidReason!, payer };\n }\n\n const extensionSigner = resolveErc20ApprovalExtensionSigner(\n erc20GasSponsorshipExtension,\n requirements.network,\n );\n\n if (extensionSigner?.simulateTransactions) {\n const simOk = await simulatePermit2SettleWithErc20Approval(\n uptoProxyConfig,\n extensionSigner,\n uptoSettleArgs,\n erc20Info,\n );\n if (!simOk) {\n return diagnosePermit2SimulationFailure(\n uptoProxyConfig,\n signer,\n tokenAddress,\n permit2Payload,\n requirements.amount,\n );\n }\n return { isValid: true, invalidReason: undefined, payer };\n }\n\n return checkPermit2Prerequisites(\n uptoProxyConfig,\n signer,\n tokenAddress,\n payer,\n requirements.amount,\n );\n }\n }\n\n const simOk = await simulatePermit2Settle(uptoProxyConfig, signer, uptoSettleArgs);\n if (!simOk) {\n return diagnosePermit2SimulationFailure(\n uptoProxyConfig,\n signer,\n tokenAddress,\n permit2Payload,\n requirements.amount,\n );\n }\n\n return {\n isValid: true,\n invalidReason: undefined,\n payer,\n };\n}\n\n/**\n * Settles an upto Permit2 payment on-chain.\n *\n * Verifies the payment first, then selects the appropriate settlement path:\n * EIP-2612 atomic permit, ERC-20 approval extension, or direct settlement.\n *\n * @param signer - The facilitator signer for contract writes\n * @param payload - The payment payload to settle\n * @param requirements - The payment requirements\n * @param permit2Payload - The upto Permit2 specific payload with witness data\n * @param context - Optional facilitator context for extension-provided capabilities\n * @param config - Optional facilitator configuration (e.g., simulation settings for settle)\n * @returns Promise resolving to a settlement response indicating success or failure\n */\nexport async function settleUptoPermit2(\n signer: FacilitatorEvmSigner,\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n permit2Payload: UptoPermit2Payload,\n context?: FacilitatorContext,\n config?: UptoPermit2FacilitatorConfig,\n): Promise<SettleResponse> {\n const payer = permit2Payload.permit2Authorization.from;\n const settlementAmount = BigInt(requirements.amount);\n\n // Re-verify the signature before settling. We override `requirements.amount`\n // with the *authorized maximum* (`permitted.amount`) — NOT the actual\n // settlement amount — because `verifyUptoPermit2` performs strict equality\n // (`permitted.amount === requirements.amount`) to confirm the payload matches\n // what the client signed. The actual settlement amount, which may be lower\n // than the authorized maximum, is validated separately in the guard below\n // (`settlementAmount > permitted.amount`).\n const verifyRequirements: PaymentRequirements = {\n ...requirements,\n amount: permit2Payload.permit2Authorization.permitted.amount,\n };\n\n const valid = await verifyUptoPermit2(\n signer,\n payload,\n verifyRequirements,\n permit2Payload,\n context,\n { simulate: config?.simulateInSettle ?? true },\n );\n if (!valid.isValid) {\n return {\n success: false,\n network: payload.accepted.network,\n transaction: \"\",\n errorReason: valid.invalidReason ?? \"invalid_scheme\",\n payer,\n };\n }\n\n // Zero settlement — no on-chain tx needed\n if (settlementAmount === 0n) {\n return {\n success: true,\n transaction: \"\",\n network: payload.accepted.network,\n payer,\n amount: \"0\",\n };\n }\n\n if (settlementAmount > BigInt(permit2Payload.permit2Authorization.permitted.amount)) {\n return {\n success: false,\n network: payload.accepted.network,\n transaction: \"\",\n errorReason: ErrUptoSettlementExceedsAmount,\n payer,\n };\n }\n\n const facilitatorAddress = getAddress(permit2Payload.permit2Authorization.witness.facilitator);\n\n // Branch: EIP-2612 gas sponsoring (atomic settleWithPermit via contract)\n const eip2612Info = extractEip2612GasSponsoringInfo(payload);\n if (eip2612Info) {\n return settleUptoWithEIP2612(\n signer,\n payload,\n permit2Payload,\n eip2612Info,\n settlementAmount,\n facilitatorAddress,\n );\n }\n\n // Branch: ERC-20 approval gas sponsoring (broadcast approval + settle via extension signer)\n const erc20Info = extractErc20ApprovalGasSponsoringInfo(payload);\n if (erc20Info) {\n const erc20GasSponsorshipExtension =\n context?.getExtension<Erc20ApprovalGasSponsoringFacilitatorExtension>(\n ERC20_APPROVAL_GAS_SPONSORING_KEY,\n );\n const extensionSigner = resolveErc20ApprovalExtensionSigner(\n erc20GasSponsorshipExtension,\n payload.accepted.network,\n );\n if (extensionSigner) {\n return settleUptoWithERC20Approval(\n extensionSigner,\n payload,\n permit2Payload,\n erc20Info,\n settlementAmount,\n facilitatorAddress,\n );\n }\n }\n\n // Branch: standard settle (allowance already on-chain)\n return settleUptoDirect(signer, payload, permit2Payload, settlementAmount, facilitatorAddress);\n}\n\n/**\n * Settles an upto Permit2 payment via settleWithPermit, including the EIP-2612 permit atomically.\n *\n * @param signer - The facilitator signer for contract writes\n * @param payload - The payment payload for network info\n * @param permit2Payload - The upto Permit2 specific payload with authorization and signature\n * @param eip2612Info - The EIP-2612 gas sponsoring info from the payload extension\n * @param settlementAmount - The amount to settle on-chain\n * @param facilitatorAddress - The facilitator address authorized in the witness\n * @returns Promise resolving to a settlement response\n */\nasync function settleUptoWithEIP2612(\n signer: FacilitatorEvmSigner,\n payload: PaymentPayload,\n permit2Payload: UptoPermit2Payload,\n eip2612Info: Eip2612GasSponsoringInfo,\n settlementAmount: bigint,\n facilitatorAddress: `0x${string}`,\n): Promise<SettleResponse> {\n const payer = permit2Payload.permit2Authorization.from;\n try {\n const { v, r, s } = splitEip2612Signature(eip2612Info.signature);\n\n const tx = await signer.writeContract({\n address: uptoProxyConfig.proxyAddress,\n abi: uptoProxyConfig.proxyABI,\n functionName: \"settleWithPermit\",\n args: [\n {\n value: BigInt(eip2612Info.amount),\n deadline: BigInt(eip2612Info.deadline),\n r,\n s,\n v,\n },\n ...buildUptoPermit2SettleArgs(permit2Payload, settlementAmount, facilitatorAddress),\n ],\n });\n\n const response = await waitAndReturnSettleResponse(signer, tx, payload, payer);\n return { ...response, amount: settlementAmount.toString() };\n } catch (error) {\n return mapSettleError(error, payload, payer);\n }\n}\n\n/**\n * Settles an upto Permit2 payment using an ERC-20 approval gas sponsoring extension.\n *\n * Broadcasts the pre-signed approval transaction followed by the settle transaction\n * via the extension signer.\n *\n * @param extensionSigner - The extension signer with sendTransactions capability\n * @param payload - The payment payload for network info\n * @param permit2Payload - The upto Permit2 specific payload with authorization and signature\n * @param erc20Info - Object containing the signed approval transaction\n * @param erc20Info.signedTransaction - The RLP-encoded signed ERC-20 approve transaction hex string\n * @param settlementAmount - The amount to settle on-chain\n * @param facilitatorAddress - The facilitator address authorized in the witness\n * @returns Promise resolving to a settlement response\n */\nasync function settleUptoWithERC20Approval(\n extensionSigner: Erc20ApprovalGasSponsoringSigner,\n payload: PaymentPayload,\n permit2Payload: UptoPermit2Payload,\n erc20Info: { signedTransaction: string },\n settlementAmount: bigint,\n facilitatorAddress: `0x${string}`,\n): Promise<SettleResponse> {\n const payer = permit2Payload.permit2Authorization.from;\n\n try {\n const settleData = encodeFunctionData({\n abi: uptoProxyConfig.proxyABI,\n functionName: \"settle\",\n args: buildUptoPermit2SettleArgs(permit2Payload, settlementAmount, facilitatorAddress),\n });\n\n const txHashes = await extensionSigner.sendTransactions([\n erc20Info.signedTransaction as `0x${string}`,\n { to: uptoProxyConfig.proxyAddress, data: settleData, gas: BigInt(300_000) },\n ]);\n\n const settleTxHash = txHashes[txHashes.length - 1];\n const response = await waitAndReturnSettleResponse(\n extensionSigner,\n settleTxHash,\n payload,\n payer,\n );\n return { ...response, amount: settlementAmount.toString() };\n } catch (error) {\n return mapSettleError(error, payload, payer);\n }\n}\n\n/**\n * Settles an upto Permit2 payment directly when Permit2 allowance is already on-chain.\n *\n * @param signer - The facilitator signer for contract writes\n * @param payload - The payment payload for network info\n * @param permit2Payload - The upto Permit2 specific payload with authorization and signature\n * @param settlementAmount - The amount to settle on-chain\n * @param facilitatorAddress - The facilitator address authorized in the witness\n * @returns Promise resolving to a settlement response\n */\nasync function settleUptoDirect(\n signer: FacilitatorEvmSigner,\n payload: PaymentPayload,\n permit2Payload: UptoPermit2Payload,\n settlementAmount: bigint,\n facilitatorAddress: `0x${string}`,\n): Promise<SettleResponse> {\n const payer = permit2Payload.permit2Authorization.from;\n try {\n const tx = await signer.writeContract({\n address: uptoProxyConfig.proxyAddress,\n abi: uptoProxyConfig.proxyABI,\n functionName: \"settle\",\n args: buildUptoPermit2SettleArgs(permit2Payload, settlementAmount, facilitatorAddress),\n });\n\n const response = await waitAndReturnSettleResponse(signer, tx, payload, payer);\n return { ...response, amount: settlementAmount.toString() };\n } catch (error) {\n return mapSettleError(error, payload, payer);\n }\n}\n","// EIP-3009 TransferWithAuthorization types for EIP-712 signing\nexport const authorizationTypes = {\n TransferWithAuthorization: [\n { name: \"from\", type: \"address\" },\n { name: \"to\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"validAfter\", type: \"uint256\" },\n { name: \"validBefore\", type: \"uint256\" },\n { name: \"nonce\", type: \"bytes32\" },\n ],\n} as const;\n\n/**\n * Permit2 EIP-712 types for signing PermitWitnessTransferFrom (exact scheme).\n * Must match the exact format expected by the Permit2 contract.\n * Note: Types must be in ALPHABETICAL order after the primary type (TokenPermissions < Witness).\n */\nexport const permit2WitnessTypes = {\n PermitWitnessTransferFrom: [\n { name: \"permitted\", type: \"TokenPermissions\" },\n { name: \"spender\", type: \"address\" },\n { name: \"nonce\", type: \"uint256\" },\n { name: \"deadline\", type: \"uint256\" },\n { name: \"witness\", type: \"Witness\" },\n ],\n TokenPermissions: [\n { name: \"token\", type: \"address\" },\n { name: \"amount\", type: \"uint256\" },\n ],\n Witness: [\n { name: \"to\", type: \"address\" },\n { name: \"validAfter\", type: \"uint256\" },\n ],\n} as const;\n\n/**\n * Permit2 EIP-712 types for signing PermitWitnessTransferFrom (upto scheme).\n * The upto witness includes a `facilitator` field that the exact witness does not.\n * This ensures only the authorized facilitator can settle the payment.\n * Must match: Witness(address to,address facilitator,uint256 validAfter)\n */\nexport const uptoPermit2WitnessTypes = {\n PermitWitnessTransferFrom: [\n { name: \"permitted\", type: \"TokenPermissions\" },\n { name: \"spender\", type: \"address\" },\n { name: \"nonce\", type: \"uint256\" },\n { name: \"deadline\", type: \"uint256\" },\n { name: \"witness\", type: \"Witness\" },\n ],\n TokenPermissions: [\n { name: \"token\", type: \"address\" },\n { name: \"amount\", type: \"uint256\" },\n ],\n Witness: [\n { name: \"to\", type: \"address\" },\n { name: \"facilitator\", type: \"address\" },\n { name: \"validAfter\", type: \"uint256\" },\n ],\n} as const;\n\n// EIP3009 ABI for transferWithAuthorization function\nexport const eip3009ABI = [\n {\n inputs: [\n { name: \"from\", type: \"address\" },\n { name: \"to\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"validAfter\", type: \"uint256\" },\n { name: \"validBefore\", type: \"uint256\" },\n { name: \"nonce\", type: \"bytes32\" },\n { name: \"v\", type: \"uint8\" },\n { name: \"r\", type: \"bytes32\" },\n { name: \"s\", type: \"bytes32\" },\n ],\n name: \"transferWithAuthorization\",\n outputs: [],\n stateMutability: \"nonpayable\",\n type: \"function\",\n },\n {\n inputs: [\n { name: \"from\", type: \"address\" },\n { name: \"to\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"validAfter\", type: \"uint256\" },\n { name: \"validBefore\", type: \"uint256\" },\n { name: \"nonce\", type: \"bytes32\" },\n { name: \"signature\", type: \"bytes\" },\n ],\n name: \"transferWithAuthorization\",\n outputs: [],\n stateMutability: \"nonpayable\",\n type: \"function\",\n },\n {\n inputs: [{ name: \"account\", type: \"address\" }],\n name: \"balanceOf\",\n outputs: [{ name: \"\", type: \"uint256\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n {\n inputs: [],\n name: \"version\",\n outputs: [{ name: \"\", type: \"string\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n {\n inputs: [],\n name: \"name\",\n outputs: [{ name: \"\", type: \"string\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n {\n inputs: [\n { name: \"authorizer\", type: \"address\" },\n { name: \"nonce\", type: \"bytes32\" },\n ],\n name: \"authorizationState\",\n outputs: [{ name: \"\", type: \"bool\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n] as const;\n\n/**\n * EIP-2612 Permit EIP-712 types for signing token.permit().\n */\nexport const eip2612PermitTypes = {\n Permit: [\n { name: \"owner\", type: \"address\" },\n { name: \"spender\", type: \"address\" },\n { name: \"value\", type: \"uint256\" },\n { name: \"nonce\", type: \"uint256\" },\n { name: \"deadline\", type: \"uint256\" },\n ],\n} as const;\n\n/**\n * EIP-2612 nonces ABI for querying current nonce.\n */\nexport const eip2612NoncesAbi = [\n {\n type: \"function\",\n name: \"nonces\",\n inputs: [{ name: \"owner\", type: \"address\" }],\n outputs: [{ type: \"uint256\" }],\n stateMutability: \"view\",\n },\n] as const;\n\n/** ERC-20 approve(address,uint256) ABI for encoding/decoding approval calldata. */\nexport const erc20ApproveAbi = [\n {\n type: \"function\",\n name: \"approve\",\n inputs: [\n { name: \"spender\", type: \"address\" },\n { name: \"amount\", type: \"uint256\" },\n ],\n outputs: [{ type: \"bool\" }],\n stateMutability: \"nonpayable\",\n },\n] as const;\n\n/** ERC-20 allowance(address,address) ABI for checking spender approval. */\nexport const erc20AllowanceAbi = [\n {\n type: \"function\",\n name: \"allowance\",\n inputs: [\n { name: \"owner\", type: \"address\" },\n { name: \"spender\", type: \"address\" },\n ],\n outputs: [{ type: \"uint256\" }],\n stateMutability: \"view\",\n },\n] as const;\n\n/** Gas limit for a standard ERC-20 approve() transaction. */\nexport const ERC20_APPROVE_GAS_LIMIT = 70_000n;\n\n/** Fallback max fee per gas (1 gwei) when fee estimation fails. */\nexport const DEFAULT_MAX_FEE_PER_GAS = 1_000_000_000n;\n\n/** Fallback max priority fee per gas (0.1 gwei) when fee estimation fails. */\nexport const DEFAULT_MAX_PRIORITY_FEE_PER_GAS = 100_000_000n;\n\n/**\n * Canonical Permit2 contract address.\n * Same address on all EVM chains via CREATE2 deployment.\n *\n * @see https://github.com/Uniswap/permit2\n */\nexport const PERMIT2_ADDRESS = \"0x000000000022D473030F116dDEE9F6B43aC78BA3\" as const;\n\n/**\n * x402ExactPermit2Proxy contract address.\n * Vanity address: 0x4020...0001 for easy recognition.\n * This address is deterministic based on:\n * - Arachnid's deterministic deployer (0x4e59b44847b379578588920cA78FbF26c0B4956C)\n * - Vanity-mined salt for prefix 0x4020 and suffix 0001\n * - Contract bytecode + constructor args (PERMIT2_ADDRESS)\n */\nexport const x402ExactPermit2ProxyAddress = \"0x402085c248EeA27D92E8b30b2C58ed07f9E20001\" as const;\n\n/**\n * x402UptoPermit2Proxy contract address.\n * Vanity address: 0x4020...0002 for easy recognition.\n * This address is deterministic based on:\n * - Arachnid's deterministic deployer (0x4e59b44847b379578588920cA78FbF26c0B4956C)\n * - Vanity-mined salt for prefix 0x4020 and suffix 0002\n * - Contract bytecode + constructor args (PERMIT2_ADDRESS)\n */\nexport const x402UptoPermit2ProxyAddress = \"0x4020A4f3b7b90ccA423B9fabCc0CE57C6C240002\" as const;\n\n/**\n * ABI components for the exact Permit2 witness tuple: Witness(address to, uint256 validAfter).\n */\nconst permit2WitnessABIComponents = [\n { name: \"to\", type: \"address\", internalType: \"address\" },\n { name: \"validAfter\", type: \"uint256\", internalType: \"uint256\" },\n] as const;\n\n/**\n * ABI components for the upto Permit2 witness tuple:\n * Witness(address to, address facilitator, uint256 validAfter).\n */\nconst uptoPermit2WitnessABIComponents = [\n { name: \"to\", type: \"address\", internalType: \"address\" },\n { name: \"facilitator\", type: \"address\", internalType: \"address\" },\n { name: \"validAfter\", type: \"uint256\", internalType: \"uint256\" },\n] as const;\n\n/**\n * x402UptoPermit2Proxy ABI — settle/settleWithPermit for the upto payment scheme.\n * Key differences from exact: settle() takes a `uint256 amount` parameter, and the\n * Witness struct includes an `address facilitator` field.\n */\nexport const x402UptoPermit2ProxyABI = [\n {\n type: \"function\",\n name: \"PERMIT2\",\n inputs: [],\n outputs: [{ name: \"\", type: \"address\", internalType: \"contract ISignatureTransfer\" }],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"WITNESS_TYPEHASH\",\n inputs: [],\n outputs: [{ name: \"\", type: \"bytes32\", internalType: \"bytes32\" }],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"WITNESS_TYPE_STRING\",\n inputs: [],\n outputs: [{ name: \"\", type: \"string\", internalType: \"string\" }],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"settle\",\n inputs: [\n {\n name: \"permit\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.PermitTransferFrom\",\n components: [\n {\n name: \"permitted\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.TokenPermissions\",\n components: [\n { name: \"token\", type: \"address\", internalType: \"address\" },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"nonce\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"deadline\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"owner\", type: \"address\", internalType: \"address\" },\n {\n name: \"witness\",\n type: \"tuple\",\n internalType: \"struct x402UptoPermit2Proxy.Witness\",\n components: uptoPermit2WitnessABIComponents,\n },\n { name: \"signature\", type: \"bytes\", internalType: \"bytes\" },\n ],\n outputs: [],\n stateMutability: \"nonpayable\",\n },\n {\n type: \"function\",\n name: \"settleWithPermit\",\n inputs: [\n {\n name: \"permit2612\",\n type: \"tuple\",\n internalType: \"struct x402UptoPermit2Proxy.EIP2612Permit\",\n components: [\n { name: \"value\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"deadline\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"r\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"s\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"v\", type: \"uint8\", internalType: \"uint8\" },\n ],\n },\n {\n name: \"permit\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.PermitTransferFrom\",\n components: [\n {\n name: \"permitted\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.TokenPermissions\",\n components: [\n { name: \"token\", type: \"address\", internalType: \"address\" },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"nonce\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"deadline\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"owner\", type: \"address\", internalType: \"address\" },\n {\n name: \"witness\",\n type: \"tuple\",\n internalType: \"struct x402UptoPermit2Proxy.Witness\",\n components: uptoPermit2WitnessABIComponents,\n },\n { name: \"signature\", type: \"bytes\", internalType: \"bytes\" },\n ],\n outputs: [],\n stateMutability: \"nonpayable\",\n },\n { type: \"event\", name: \"Settled\", inputs: [], anonymous: false },\n { type: \"event\", name: \"SettledWithPermit\", inputs: [], anonymous: false },\n { type: \"error\", name: \"AmountExceedsPermitted\", inputs: [] },\n { type: \"error\", name: \"InvalidDestination\", inputs: [] },\n { type: \"error\", name: \"InvalidOwner\", inputs: [] },\n { type: \"error\", name: \"InvalidPermit2Address\", inputs: [] },\n { type: \"error\", name: \"PaymentTooEarly\", inputs: [] },\n { type: \"error\", name: \"Permit2612AmountMismatch\", inputs: [] },\n { type: \"error\", name: \"ReentrancyGuardReentrantCall\", inputs: [] },\n { type: \"error\", name: \"UnauthorizedFacilitator\", inputs: [] },\n] as const;\n\n/**\n * x402ExactPermit2Proxy ABI - settle function for exact payment scheme.\n */\nexport const x402ExactPermit2ProxyABI = [\n {\n type: \"function\",\n name: \"PERMIT2\",\n inputs: [],\n outputs: [{ name: \"\", type: \"address\", internalType: \"contract ISignatureTransfer\" }],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"WITNESS_TYPEHASH\",\n inputs: [],\n outputs: [{ name: \"\", type: \"bytes32\", internalType: \"bytes32\" }],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"WITNESS_TYPE_STRING\",\n inputs: [],\n outputs: [{ name: \"\", type: \"string\", internalType: \"string\" }],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"settle\",\n inputs: [\n {\n name: \"permit\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.PermitTransferFrom\",\n components: [\n {\n name: \"permitted\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.TokenPermissions\",\n components: [\n { name: \"token\", type: \"address\", internalType: \"address\" },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"nonce\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"deadline\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"owner\", type: \"address\", internalType: \"address\" },\n {\n name: \"witness\",\n type: \"tuple\",\n internalType: \"struct x402ExactPermit2Proxy.Witness\",\n components: permit2WitnessABIComponents,\n },\n { name: \"signature\", type: \"bytes\", internalType: \"bytes\" },\n ],\n outputs: [],\n stateMutability: \"nonpayable\",\n },\n {\n type: \"function\",\n name: \"settleWithPermit\",\n inputs: [\n {\n name: \"permit2612\",\n type: \"tuple\",\n internalType: \"struct x402ExactPermit2Proxy.EIP2612Permit\",\n components: [\n { name: \"value\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"deadline\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"r\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"s\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"v\", type: \"uint8\", internalType: \"uint8\" },\n ],\n },\n {\n name: \"permit\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.PermitTransferFrom\",\n components: [\n {\n name: \"permitted\",\n type: \"tuple\",\n internalType: \"struct ISignatureTransfer.TokenPermissions\",\n components: [\n { name: \"token\", type: \"address\", internalType: \"address\" },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"nonce\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"deadline\", type: \"uint256\", internalType: \"uint256\" },\n ],\n },\n { name: \"owner\", type: \"address\", internalType: \"address\" },\n {\n name: \"witness\",\n type: \"tuple\",\n internalType: \"struct x402ExactPermit2Proxy.Witness\",\n components: permit2WitnessABIComponents,\n },\n { name: \"signature\", type: \"bytes\", internalType: \"bytes\" },\n ],\n outputs: [],\n stateMutability: \"nonpayable\",\n },\n { type: \"event\", name: \"Settled\", inputs: [], anonymous: false },\n { type: \"event\", name: \"SettledWithPermit\", inputs: [], anonymous: false },\n { type: \"error\", name: \"InvalidAmount\", inputs: [] },\n { type: \"error\", name: \"InvalidDestination\", inputs: [] },\n { type: \"error\", name: \"InvalidOwner\", inputs: [] },\n { type: \"error\", name: \"InvalidPermit2Address\", inputs: [] },\n { type: \"error\", name: \"PaymentTooEarly\", inputs: [] },\n { type: \"error\", name: \"Permit2612AmountMismatch\", inputs: [] },\n { type: \"error\", name: \"ReentrancyGuardReentrantCall\", inputs: [] },\n] as const;\n","/**\n * Named error reason constants for the exact EVM facilitator.\n *\n * These strings must be character-for-character identical to the Go constants in\n * go/mechanisms/evm/exact/facilitator/errors.go to maintain cross-SDK parity.\n */\n\nexport const ErrInvalidScheme = \"invalid_exact_evm_scheme\";\nexport const ErrNetworkMismatch = \"invalid_exact_evm_network_mismatch\";\nexport const ErrMissingEip712Domain = \"invalid_exact_evm_missing_eip712_domain\";\nexport const ErrRecipientMismatch = \"invalid_exact_evm_recipient_mismatch\";\nexport const ErrInvalidSignature = \"invalid_exact_evm_signature\";\nexport const ErrValidBeforeExpired = \"invalid_exact_evm_payload_authorization_valid_before\";\nexport const ErrValidAfterInFuture = \"invalid_exact_evm_payload_authorization_valid_after\";\nexport const ErrInvalidAuthorizationValue = \"invalid_exact_evm_authorization_value\";\nexport const ErrUndeployedSmartWallet = \"invalid_exact_evm_payload_undeployed_smart_wallet\";\nexport const ErrTransactionFailed = \"invalid_exact_evm_transaction_failed\";\n\n// EIP-3009 verify errors\nexport const ErrEip3009TokenNameMismatch = \"invalid_exact_evm_token_name_mismatch\";\nexport const ErrEip3009TokenVersionMismatch = \"invalid_exact_evm_token_version_mismatch\";\nexport const ErrEip3009NotSupported = \"invalid_exact_evm_eip3009_not_supported\";\nexport const ErrEip3009NonceAlreadyUsed = \"invalid_exact_evm_nonce_already_used\";\nexport const ErrEip3009InsufficientBalance = \"invalid_exact_evm_insufficient_balance\";\nexport const ErrEip3009SimulationFailed = \"invalid_exact_evm_transaction_simulation_failed\";\n\n// Permit2 verify errors\nexport const ErrPermit2InvalidSpender = \"invalid_permit2_spender\";\nexport const ErrPermit2RecipientMismatch = \"invalid_permit2_recipient_mismatch\";\nexport const ErrPermit2DeadlineExpired = \"permit2_deadline_expired\";\nexport const ErrPermit2NotYetValid = \"permit2_not_yet_valid\";\nexport const ErrPermit2AmountMismatch = \"permit2_amount_mismatch\";\nexport const ErrPermit2TokenMismatch = \"permit2_token_mismatch\";\nexport const ErrPermit2InvalidSignature = \"invalid_permit2_signature\";\nexport const ErrPermit2AllowanceRequired = \"permit2_allowance_required\";\nexport const ErrPermit2SimulationFailed = \"permit2_simulation_failed\";\nexport const ErrPermit2InsufficientBalance = \"permit2_insufficient_balance\";\nexport const ErrPermit2ProxyNotDeployed = \"permit2_proxy_not_deployed\";\n\n// Permit2 settle errors (from contract reverts)\nexport const ErrPermit2InvalidAmount = \"permit2_invalid_amount\";\nexport const ErrPermit2InvalidDestination = \"permit2_invalid_destination\";\nexport const ErrPermit2InvalidOwner = \"permit2_invalid_owner\";\nexport const ErrPermit2PaymentTooEarly = \"permit2_payment_too_early\";\nexport const ErrPermit2InvalidNonce = \"permit2_invalid_nonce\";\nexport const ErrPermit2612AmountMismatch = \"permit2_2612_amount_mismatch\";\n\n// ERC-20 approval gas sponsoring verify errors\nexport const ErrErc20ApprovalInvalidFormat = \"invalid_erc20_approval_extension_format\";\nexport const ErrErc20ApprovalFromMismatch = \"erc20_approval_from_mismatch\";\nexport const ErrErc20ApprovalAssetMismatch = \"erc20_approval_asset_mismatch\";\nexport const ErrErc20ApprovalSpenderNotPermit2 = \"erc20_approval_spender_not_permit2\";\nexport const ErrErc20ApprovalTxWrongTarget = \"erc20_approval_tx_wrong_target\";\nexport const ErrErc20ApprovalTxWrongSelector = \"erc20_approval_tx_wrong_selector\";\nexport const ErrErc20ApprovalTxWrongSpender = \"erc20_approval_tx_wrong_spender\";\nexport const ErrErc20ApprovalTxInvalidCalldata = \"erc20_approval_tx_invalid_calldata\";\nexport const ErrErc20ApprovalTxSignerMismatch = \"erc20_approval_tx_signer_mismatch\";\nexport const ErrErc20ApprovalTxInvalidSignature = \"erc20_approval_tx_invalid_signature\";\nexport const ErrErc20ApprovalTxParseFailed = \"erc20_approval_tx_parse_failed\";\nexport const ErrErc20ApprovalTxFailed = \"erc20_approval_tx_failed\";\n\n// EIP-2612 gas sponsoring verify errors\nexport const ErrInvalidEip2612ExtensionFormat = \"invalid_eip2612_extension_format\";\nexport const ErrEip2612FromMismatch = \"eip2612_from_mismatch\";\nexport const ErrEip2612AssetMismatch = \"eip2612_asset_mismatch\";\nexport const ErrEip2612SpenderNotPermit2 = \"eip2612_spender_not_permit2\";\nexport const ErrEip2612DeadlineExpired = \"eip2612_deadline_expired\";\n\n// Shared settle errors\nexport const ErrUnsupportedPayloadType = \"unsupported_payload_type\";\nexport const ErrInvalidTransactionState = \"invalid_transaction_state\";\n","/**\n * Named error reason constants for the upto EVM facilitator.\n *\n * Shared Permit2 errors are re-exported from exact/facilitator/errors.ts.\n * Upto-specific errors are defined here.\n *\n * These strings must be character-for-character identical to the Go constants\n * to maintain cross-SDK parity.\n */\n\n// Re-export shared Permit2 errors\nexport {\n ErrPermit2InvalidSpender,\n ErrPermit2RecipientMismatch,\n ErrPermit2DeadlineExpired,\n ErrPermit2NotYetValid,\n ErrPermit2AmountMismatch,\n ErrPermit2TokenMismatch,\n ErrPermit2InvalidSignature,\n ErrPermit2AllowanceRequired,\n ErrPermit2InvalidAmount,\n ErrPermit2InvalidDestination,\n ErrPermit2InvalidOwner,\n ErrPermit2PaymentTooEarly,\n ErrPermit2InvalidNonce,\n ErrPermit2612AmountMismatch,\n ErrErc20ApprovalInvalidFormat,\n ErrErc20ApprovalFromMismatch,\n ErrErc20ApprovalAssetMismatch,\n ErrErc20ApprovalSpenderNotPermit2,\n ErrErc20ApprovalTxWrongTarget,\n ErrErc20ApprovalTxWrongSelector,\n ErrErc20ApprovalTxWrongSpender,\n ErrErc20ApprovalTxInvalidCalldata,\n ErrErc20ApprovalTxSignerMismatch,\n ErrErc20ApprovalTxInvalidSignature,\n ErrErc20ApprovalTxParseFailed,\n} from \"../../exact/facilitator/errors\";\n\n// Upto-specific errors\nexport const ErrUptoInvalidScheme = \"invalid_upto_evm_scheme\";\nexport const ErrUptoNetworkMismatch = \"invalid_upto_evm_network_mismatch\";\nexport const ErrUptoSettlementExceedsAmount = \"invalid_upto_evm_payload_settlement_exceeds_amount\";\nexport const ErrUptoAmountExceedsPermitted = \"upto_amount_exceeds_permitted\";\nexport const ErrUptoUnauthorizedFacilitator = \"upto_unauthorized_facilitator\";\nexport const ErrUptoFacilitatorMismatch = \"upto_facilitator_mismatch\";\n","import { toHex } from \"viem\";\n\n/**\n * Extract chain ID from a CAIP-2 network identifier (eip155:CHAIN_ID).\n *\n * @param network - The network identifier in CAIP-2 format (e.g., \"eip155:8453\")\n * @returns The numeric chain ID\n * @throws Error if the network format is invalid\n */\nexport function getEvmChainId(network: string): number {\n if (network.startsWith(\"eip155:\")) {\n const idStr = network.split(\":\")[1];\n const chainId = parseInt(idStr, 10);\n if (isNaN(chainId)) {\n throw new Error(`Invalid CAIP-2 chain ID: ${network}`);\n }\n return chainId;\n }\n\n throw new Error(`Unsupported network format: ${network} (expected eip155:CHAIN_ID)`);\n}\n\n/**\n * Get the crypto object from the global scope.\n *\n * @returns The crypto object\n * @throws Error if crypto API is not available\n */\nfunction getCrypto(): Crypto {\n const cryptoObj = globalThis.crypto as Crypto | undefined;\n if (!cryptoObj) {\n throw new Error(\"Crypto API not available\");\n }\n return cryptoObj;\n}\n\n/**\n * Create a random 32-byte nonce for EIP-3009 authorization.\n *\n * @returns A hex-encoded 32-byte nonce\n */\nexport function createNonce(): `0x${string}` {\n return toHex(getCrypto().getRandomValues(new Uint8Array(32)));\n}\n\n/**\n * Creates a random 256-bit nonce for Permit2.\n * Permit2 uses uint256 nonces (not bytes32 like EIP-3009).\n *\n * @returns A string representation of the random nonce\n */\nexport function createPermit2Nonce(): string {\n const randomBytes = getCrypto().getRandomValues(new Uint8Array(32));\n return BigInt(toHex(randomBytes)).toString();\n}\n","import {\n getAddress,\n parseTransaction,\n decodeFunctionData,\n recoverTransactionAddress,\n type TransactionSerialized,\n} from \"viem\";\nimport type { VerifyResponse } from \"@payai/x402/types\";\nimport {\n validateErc20ApprovalGasSponsoringInfo,\n type Erc20ApprovalGasSponsoringInfo,\n} from \"../exact/extensions\";\nimport { PERMIT2_ADDRESS, erc20ApproveAbi } from \"../constants\";\nimport {\n ErrErc20ApprovalInvalidFormat,\n ErrErc20ApprovalFromMismatch,\n ErrErc20ApprovalAssetMismatch,\n ErrErc20ApprovalSpenderNotPermit2,\n ErrErc20ApprovalTxWrongTarget,\n ErrErc20ApprovalTxWrongSelector,\n ErrErc20ApprovalTxWrongSpender,\n ErrErc20ApprovalTxInvalidCalldata,\n ErrErc20ApprovalTxSignerMismatch,\n ErrErc20ApprovalTxInvalidSignature,\n ErrErc20ApprovalTxParseFailed,\n} from \"../exact/facilitator/errors\";\n\n/** The approve(address,uint256) function selector */\nconst APPROVE_SELECTOR = \"0x095ea7b3\";\n\n/**\n * Validates ERC-20 approval extension data for a Permit2 payment.\n *\n * Performs comprehensive validation:\n * - Format validation via validateErc20ApprovalGasSponsoringInfo (JSON Schema)\n * - `from` matches payer\n * - `asset` matches token\n * - `spender` is PERMIT2_ADDRESS\n * - Transaction `to` matches token address\n * - Transaction calldata is a valid approve(PERMIT2_ADDRESS, ...) call\n * - Recovered transaction signer matches `from`\n *\n * @param info - The ERC-20 approval gas sponsoring info\n * @param payer - The expected payer address\n * @param tokenAddress - The expected token address\n * @returns Validation result with invalidReason and invalidMessage on failure\n */\nexport async function validateErc20ApprovalForPayment(\n info: Erc20ApprovalGasSponsoringInfo,\n payer: `0x${string}`,\n tokenAddress: `0x${string}`,\n): Promise<Pick<VerifyResponse, \"isValid\" | \"invalidReason\" | \"invalidMessage\">> {\n if (!validateErc20ApprovalGasSponsoringInfo(info)) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalInvalidFormat,\n invalidMessage: \"ERC-20 approval extension info failed schema validation\",\n };\n }\n\n if (getAddress(info.from) !== getAddress(payer)) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalFromMismatch,\n invalidMessage: `Expected from=${payer}, got ${info.from}`,\n };\n }\n\n if (getAddress(info.asset) !== tokenAddress) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalAssetMismatch,\n invalidMessage: `Expected asset=${tokenAddress}, got ${info.asset}`,\n };\n }\n\n if (getAddress(info.spender) !== getAddress(PERMIT2_ADDRESS)) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalSpenderNotPermit2,\n invalidMessage: `Expected spender=${PERMIT2_ADDRESS}, got ${info.spender}`,\n };\n }\n\n try {\n const serializedTx = info.signedTransaction as TransactionSerialized;\n const tx = parseTransaction(serializedTx);\n\n if (!tx.to || getAddress(tx.to) !== tokenAddress) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxWrongTarget,\n invalidMessage: `Transaction targets ${tx.to ?? \"null\"}, expected ${tokenAddress}`,\n };\n }\n\n const data = tx.data ?? \"0x\";\n if (!data.startsWith(APPROVE_SELECTOR)) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxWrongSelector,\n invalidMessage: `Transaction calldata does not start with approve() selector ${APPROVE_SELECTOR}`,\n };\n }\n\n try {\n const decoded = decodeFunctionData({\n abi: erc20ApproveAbi,\n data: data as `0x${string}`,\n });\n const calldataSpender = getAddress(decoded.args[0] as `0x${string}`);\n if (calldataSpender !== getAddress(PERMIT2_ADDRESS)) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxWrongSpender,\n invalidMessage: `approve() spender is ${calldataSpender}, expected Permit2 ${PERMIT2_ADDRESS}`,\n };\n }\n } catch {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxInvalidCalldata,\n invalidMessage: \"Failed to decode approve() calldata from the signed transaction\",\n };\n }\n\n try {\n const recoveredAddress = await recoverTransactionAddress({\n serializedTransaction: serializedTx,\n });\n if (getAddress(recoveredAddress) !== getAddress(payer)) {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxSignerMismatch,\n invalidMessage: `Transaction signed by ${recoveredAddress}, expected payer ${payer}`,\n };\n }\n } catch {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxInvalidSignature,\n invalidMessage: \"Failed to recover signer from the signed transaction\",\n };\n }\n } catch {\n return {\n isValid: false,\n invalidReason: ErrErc20ApprovalTxParseFailed,\n invalidMessage: \"Failed to parse the signed transaction\",\n };\n }\n\n return { isValid: true };\n}\n","import {\n PaymentPayload,\n PaymentPayloadResult,\n PaymentRequirements,\n FacilitatorContext,\n SettleResponse,\n VerifyResponse,\n} from \"@payai/x402/types\";\nimport {\n extractEip2612GasSponsoringInfo,\n validateEip2612GasSponsoringInfo,\n extractErc20ApprovalGasSponsoringInfo,\n ERC20_APPROVAL_GAS_SPONSORING_KEY,\n type Eip2612GasSponsoringInfo,\n type Erc20ApprovalGasSponsoringFacilitatorExtension,\n type Erc20ApprovalGasSponsoringSigner,\n} from \"../exact/extensions\";\nimport { getAddress, encodeFunctionData } from \"viem\";\nimport { PERMIT2_ADDRESS, eip3009ABI, erc20AllowanceAbi, permit2WitnessTypes } from \"../constants\";\nimport { multicall, ContractCall } from \"../multicall\";\nimport { createPermit2Nonce, getEvmChainId } from \"../utils\";\nimport {\n ErrPermit2612AmountMismatch,\n ErrPermit2InvalidAmount,\n ErrPermit2InvalidDestination,\n ErrPermit2InvalidNonce,\n ErrPermit2InvalidOwner,\n ErrPermit2InvalidSignature,\n ErrPermit2PaymentTooEarly,\n ErrPermit2AllowanceRequired,\n ErrPermit2SimulationFailed,\n ErrPermit2InsufficientBalance,\n ErrPermit2ProxyNotDeployed,\n ErrInvalidTransactionState,\n ErrTransactionFailed,\n ErrInvalidEip2612ExtensionFormat,\n ErrEip2612FromMismatch,\n ErrEip2612AssetMismatch,\n ErrEip2612SpenderNotPermit2,\n ErrEip2612DeadlineExpired,\n ErrErc20ApprovalTxFailed,\n} from \"../exact/facilitator/errors\";\nimport { ClientEvmSigner, FacilitatorEvmSigner } from \"../signer\";\nimport { ExactPermit2Payload, Permit2Authorization, UptoPermit2Payload } from \"../types\";\nimport { validateErc20ApprovalForPayment } from \"./erc20approval\";\nimport {\n ErrUptoAmountExceedsPermitted,\n ErrUptoUnauthorizedFacilitator,\n} from \"../upto/facilitator/errors\";\n\n/**\n * Base type for Permit2 payloads shared between exact and upto schemes.\n * Both {@link ExactPermit2Payload} and {@link UptoPermit2Payload} satisfy this type.\n */\nexport type Permit2PayloadBase = ExactPermit2Payload | UptoPermit2Payload;\n\n/**\n * Configuration for the Permit2 proxy contract used during settlement.\n * The exact and upto schemes use different proxy addresses and ABIs.\n */\nexport type Permit2ProxyConfig = {\n /** The deployed proxy contract address. */\n proxyAddress: `0x${string}`;\n /** The proxy contract ABI (must include settle and settleWithPermit functions). */\n proxyABI: readonly Record<string, unknown>[];\n};\n\n/**\n * Checks Permit2 allowance and validates gas sponsoring extensions if allowance is insufficient.\n *\n * When the on-chain ERC-20 allowance to the Permit2 contract is below the required amount,\n * this function falls back to validating EIP-2612 or ERC-20 approval gas sponsoring extensions\n * attached to the payment payload.\n *\n * @param signer - The facilitator signer for on-chain reads\n * @param payload - The payment payload\n * @param requirements - The payment requirements\n * @param payer - The payer address\n * @param tokenAddress - The token contract address\n * @param context - Optional facilitator context for extension lookup\n * @returns A VerifyResponse if verification should stop (failure), or null to continue\n */\nexport async function verifyPermit2Allowance(\n signer: FacilitatorEvmSigner,\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n payer: `0x${string}`,\n tokenAddress: `0x${string}`,\n context?: FacilitatorContext,\n): Promise<VerifyResponse | null> {\n try {\n const allowance = (await signer.readContract({\n address: tokenAddress,\n abi: erc20AllowanceAbi,\n functionName: \"allowance\",\n args: [payer, PERMIT2_ADDRESS],\n })) as bigint;\n\n if (allowance >= BigInt(requirements.amount)) {\n return null; // Sufficient allowance, continue verification\n }\n\n // Allowance insufficient — try EIP-2612 gas sponsoring first\n const eip2612Info = extractEip2612GasSponsoringInfo(payload);\n if (eip2612Info) {\n const result = validateEip2612PermitForPayment(eip2612Info, payer, tokenAddress);\n if (!result.isValid) {\n return { isValid: false, invalidReason: result.invalidReason!, payer };\n }\n return null; // EIP-2612 is valid, allowance will be set atomically during settlement\n }\n\n // Try ERC-20 approval gas sponsoring as fallback\n const erc20GasSponsorshipExtension =\n context?.getExtension<Erc20ApprovalGasSponsoringFacilitatorExtension>(\n ERC20_APPROVAL_GAS_SPONSORING_KEY,\n );\n if (erc20GasSponsorshipExtension) {\n const erc20Info = extractErc20ApprovalGasSponsoringInfo(payload);\n if (erc20Info) {\n const result = await validateErc20ApprovalForPayment(erc20Info, payer, tokenAddress);\n if (!result.isValid) {\n return { isValid: false, invalidReason: result.invalidReason!, payer };\n }\n return null; // ERC-20 approval is valid, will be broadcast before settlement\n }\n }\n\n return { isValid: false, invalidReason: \"permit2_allowance_required\", payer };\n } catch {\n // Allowance check failed — validate extensions if present; fail closed if none valid\n const eip2612Info = extractEip2612GasSponsoringInfo(payload);\n if (eip2612Info) {\n const result = validateEip2612PermitForPayment(eip2612Info, payer, tokenAddress);\n if (!result.isValid) {\n return { isValid: false, invalidReason: result.invalidReason!, payer };\n }\n return null;\n }\n\n const erc20GasSponsorshipExtension =\n context?.getExtension<Erc20ApprovalGasSponsoringFacilitatorExtension>(\n ERC20_APPROVAL_GAS_SPONSORING_KEY,\n );\n if (erc20GasSponsorshipExtension) {\n const erc20Info = extractErc20ApprovalGasSponsoringInfo(payload);\n if (erc20Info) {\n const result = await validateErc20ApprovalForPayment(erc20Info, payer, tokenAddress);\n if (!result.isValid) {\n return { isValid: false, invalidReason: result.invalidReason!, payer };\n }\n return null;\n }\n }\n\n return { isValid: false, invalidReason: \"permit2_allowance_required\", payer };\n }\n}\n\n/**\n * Waits for a transaction receipt and returns the appropriate SettleResponse.\n *\n * @param signer - Signer with waitForTransactionReceipt capability\n * @param tx - The transaction hash to wait for\n * @param payload - The payment payload (for network info)\n * @param payer - The payer address\n * @returns Promise resolving to a settlement response indicating success or failure\n */\nexport async function waitAndReturnSettleResponse(\n signer: Pick<FacilitatorEvmSigner, \"waitForTransactionReceipt\">,\n tx: `0x${string}`,\n payload: PaymentPayload,\n payer: `0x${string}`,\n): Promise<SettleResponse> {\n const receipt = await signer.waitForTransactionReceipt({ hash: tx });\n\n if (receipt.status !== \"success\") {\n return {\n success: false,\n errorReason: ErrInvalidTransactionState,\n transaction: tx,\n network: payload.accepted.network,\n payer,\n };\n }\n\n return {\n success: true,\n transaction: tx,\n network: payload.accepted.network,\n payer,\n };\n}\n\n/**\n * Maps contract revert errors to structured SettleResponse error reasons.\n *\n * Inspects the error message for known contract revert strings and maps them\n * to the corresponding error reason constants. Falls back to a generic\n * \"transaction_failed\" reason with truncated message for unrecognized errors.\n *\n * @param error - The caught error (typically from a contract write)\n * @param payload - The payment payload (for network info)\n * @param payer - The payer address\n * @returns A failed SettleResponse with the mapped error reason\n */\nexport function mapSettleError(\n error: unknown,\n payload: PaymentPayload,\n payer: `0x${string}`,\n): SettleResponse {\n let errorReason: string = ErrTransactionFailed;\n if (error instanceof Error) {\n const message = error.message;\n if (message.includes(\"Permit2612AmountMismatch\")) {\n errorReason = ErrPermit2612AmountMismatch;\n } else if (message.includes(\"InvalidAmount\")) {\n errorReason = ErrPermit2InvalidAmount;\n } else if (message.includes(\"InvalidDestination\")) {\n errorReason = ErrPermit2InvalidDestination;\n } else if (message.includes(\"InvalidOwner\")) {\n errorReason = ErrPermit2InvalidOwner;\n } else if (message.includes(\"PaymentTooEarly\")) {\n errorReason = ErrPermit2PaymentTooEarly;\n } else if (message.includes(\"InvalidSignature\") || message.includes(\"SignatureExpired\")) {\n errorReason = ErrPermit2InvalidSignature;\n } else if (message.includes(\"InvalidNonce\")) {\n errorReason = ErrPermit2InvalidNonce;\n } else if (message.includes(\"erc20_approval_tx_failed\")) {\n errorReason = ErrErc20ApprovalTxFailed;\n } else if (message.includes(\"AmountExceedsPermitted\")) {\n errorReason = ErrUptoAmountExceedsPermitted;\n } else if (message.includes(\"UnauthorizedFacilitator\")) {\n errorReason = ErrUptoUnauthorizedFacilitator;\n } else {\n errorReason = `${ErrTransactionFailed}: ${message.slice(0, 500)}`;\n }\n }\n return {\n success: false,\n errorReason,\n transaction: \"\",\n network: payload.accepted.network,\n payer,\n };\n}\n\n/**\n * Validates EIP-2612 permit extension data for a Permit2 payment.\n *\n * Checks that the permit extension has a valid format and that the from address,\n * asset address, spender address, and deadline all match expectations.\n *\n * @param info - The EIP-2612 gas sponsoring info extracted from the payment payload\n * @param payer - The expected payer address\n * @param tokenAddress - The expected token address\n * @returns Validation result with isValid flag and optional invalidReason string\n */\nexport function validateEip2612PermitForPayment(\n info: Eip2612GasSponsoringInfo,\n payer: `0x${string}`,\n tokenAddress: `0x${string}`,\n): { isValid: boolean; invalidReason?: string } {\n if (!validateEip2612GasSponsoringInfo(info)) {\n return { isValid: false, invalidReason: ErrInvalidEip2612ExtensionFormat };\n }\n\n if (getAddress(info.from as `0x${string}`) !== getAddress(payer)) {\n return { isValid: false, invalidReason: ErrEip2612FromMismatch };\n }\n\n if (getAddress(info.asset as `0x${string}`) !== tokenAddress) {\n return { isValid: false, invalidReason: ErrEip2612AssetMismatch };\n }\n\n if (getAddress(info.spender as `0x${string}`) !== getAddress(PERMIT2_ADDRESS)) {\n return { isValid: false, invalidReason: ErrEip2612SpenderNotPermit2 };\n }\n\n const now = Math.floor(Date.now() / 1000);\n if (BigInt(info.deadline) < BigInt(now + 6)) {\n return { isValid: false, invalidReason: ErrEip2612DeadlineExpired };\n }\n\n return { isValid: true };\n}\n\n// ---------------------------------------------------------------------------\n// Simulation helpers (shared across exact and upto)\n// ---------------------------------------------------------------------------\n\n/**\n * Simulates settle() via eth_call (readContract).\n * Returns true if simulation succeeded, false if it failed.\n *\n * @param config - The proxy contract configuration (address and ABI)\n * @param signer - EVM signer for contract reads\n * @param settleArgs - Pre-built settle function arguments (scheme-specific)\n * @returns true if simulation succeeded, false if it failed\n */\nexport async function simulatePermit2Settle(\n config: Permit2ProxyConfig,\n signer: FacilitatorEvmSigner,\n settleArgs: readonly unknown[],\n): Promise<boolean> {\n try {\n await signer.readContract({\n address: config.proxyAddress,\n abi: config.proxyABI,\n functionName: \"settle\",\n args: settleArgs,\n });\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Simulates settleWithPermit() via eth_call (readContract).\n * The contract atomically calls token.permit() then PERMIT2.permitTransferFrom(),\n * so simulation covers allowance + balance + nonces.\n *\n * @param config - The proxy contract configuration (address and ABI)\n * @param signer - EVM signer for contract reads\n * @param settleArgs - Pre-built settle function arguments (scheme-specific)\n * @param eip2612Info - EIP-2612 gas sponsoring info from the payload extension\n * @returns true if simulation succeeded, false if it failed\n */\nexport async function simulatePermit2SettleWithPermit(\n config: Permit2ProxyConfig,\n signer: FacilitatorEvmSigner,\n settleArgs: readonly unknown[],\n eip2612Info: Eip2612GasSponsoringInfo,\n): Promise<boolean> {\n try {\n const { v, r, s } = splitEip2612Signature(eip2612Info.signature);\n\n await signer.readContract({\n address: config.proxyAddress,\n abi: config.proxyABI,\n functionName: \"settleWithPermit\",\n args: [\n {\n value: BigInt(eip2612Info.amount),\n deadline: BigInt(eip2612Info.deadline),\n r,\n s,\n v,\n },\n ...settleArgs,\n ],\n });\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Delegates the full approve+settle simulation flow to the extension signer via simulateTransactions.\n * The signer owns execution strategy.\n *\n * @param config - The proxy contract configuration (address and ABI)\n * @param extensionSigner - The extension signer with simulateTransactions capability\n * @param settleArgs - Pre-built settle function arguments (scheme-specific)\n * @param erc20Info - Object containing the signed approval transaction\n * @param erc20Info.signedTransaction - The RLP-encoded signed ERC-20 approve transaction hex string\n * @returns true if the bundle simulation succeeded, false otherwise\n */\nexport async function simulatePermit2SettleWithErc20Approval(\n config: Permit2ProxyConfig,\n extensionSigner: Erc20ApprovalGasSponsoringSigner,\n settleArgs: readonly unknown[],\n erc20Info: { signedTransaction: string },\n): Promise<boolean> {\n if (!extensionSigner.simulateTransactions) {\n return false;\n }\n\n try {\n const settleData = encodeFunctionData({\n abi: config.proxyABI,\n functionName: \"settle\",\n args: settleArgs,\n });\n\n return await extensionSigner.simulateTransactions([\n erc20Info.signedTransaction as `0x${string}`,\n { to: config.proxyAddress, data: settleData, gas: BigInt(300_000) },\n ]);\n } catch {\n return false;\n }\n}\n\n/**\n * Diagnoses a Permit2 simulation failure by performing a multicall to check the proxy deployment, balance and allowance.\n *\n * @param config - The proxy contract configuration (address and ABI)\n * @param signer - EVM signer for contract reads\n * @param tokenAddress - ERC-20 token contract address\n * @param permit2Payload - The Permit2 authorization payload\n * @param amountRequired - Required payment amount (as string)\n * @returns VerifyResponse with the most specific failure reason\n */\nexport async function diagnosePermit2SimulationFailure(\n config: Permit2ProxyConfig,\n signer: FacilitatorEvmSigner,\n tokenAddress: `0x${string}`,\n permit2Payload: Permit2PayloadBase,\n amountRequired: string,\n): Promise<VerifyResponse> {\n const payer = permit2Payload.permit2Authorization.from;\n\n const diagnosticCalls: ContractCall[] = [\n {\n address: config.proxyAddress,\n abi: config.proxyABI,\n functionName: \"PERMIT2\",\n },\n {\n address: tokenAddress,\n abi: eip3009ABI,\n functionName: \"balanceOf\",\n args: [payer],\n },\n {\n address: tokenAddress,\n abi: erc20AllowanceAbi,\n functionName: \"allowance\",\n args: [payer, PERMIT2_ADDRESS],\n },\n ];\n\n try {\n const results = await multicall(signer.readContract.bind(signer), diagnosticCalls);\n\n const [proxyResult, balanceResult, allowanceResult] = results;\n\n if (proxyResult.status === \"failure\") {\n return { isValid: false, invalidReason: ErrPermit2ProxyNotDeployed, payer };\n }\n\n if (balanceResult.status === \"success\") {\n const balance = balanceResult.result as bigint;\n if (balance < BigInt(amountRequired)) {\n return { isValid: false, invalidReason: ErrPermit2InsufficientBalance, payer };\n }\n }\n\n if (allowanceResult.status === \"success\") {\n const allowance = allowanceResult.result as bigint;\n if (allowance < BigInt(amountRequired)) {\n return { isValid: false, invalidReason: ErrPermit2AllowanceRequired, payer };\n }\n }\n } catch {\n // Diagnostic multicall itself failed — fall through to generic error\n }\n\n return { isValid: false, invalidReason: ErrPermit2SimulationFailed, payer };\n}\n\n/**\n * Targeted multicall for the ERC-20 approval path where simulation cannot be used\n * (the approval hasn't been broadcast yet, so settle() would fail for expected reasons).\n * Checks proxy deployment, payer token balance and payer ETH balance for gas.\n *\n * @param config - The proxy contract configuration (address and ABI)\n * @param signer - EVM signer for contract reads\n * @param tokenAddress - ERC-20 token contract address\n * @param payer - The payer address\n * @param amountRequired - Required payment amount (as string)\n * @returns VerifyResponse — valid if checks pass, otherwise the most specific failure\n */\nexport async function checkPermit2Prerequisites(\n config: Permit2ProxyConfig,\n signer: FacilitatorEvmSigner,\n tokenAddress: `0x${string}`,\n payer: `0x${string}`,\n amountRequired: string,\n): Promise<VerifyResponse> {\n const diagnosticCalls: ContractCall[] = [\n {\n address: config.proxyAddress,\n abi: config.proxyABI,\n functionName: \"PERMIT2\",\n },\n {\n address: tokenAddress,\n abi: eip3009ABI,\n functionName: \"balanceOf\",\n args: [payer],\n },\n ];\n\n try {\n const results = await multicall(signer.readContract.bind(signer), diagnosticCalls);\n\n const [proxyResult, balanceResult] = results;\n\n if (proxyResult.status === \"failure\") {\n return { isValid: false, invalidReason: ErrPermit2ProxyNotDeployed, payer };\n }\n\n if (balanceResult.status === \"success\") {\n const balance = balanceResult.result as bigint;\n if (balance < BigInt(amountRequired)) {\n return { isValid: false, invalidReason: ErrPermit2InsufficientBalance, payer };\n }\n }\n } catch {\n // Multicall failed — fall through to valid (fail open for prerequisites-only check)\n }\n\n return { isValid: true, invalidReason: undefined, payer };\n}\n\n/**\n * Builds args for exact settle(permit, owner, witness, signature).\n *\n * @param permit2Payload - The Permit2 payload containing authorization and signature data\n * @returns Tuple of contract call arguments for the exact settle function\n */\nexport function buildExactPermit2SettleArgs(permit2Payload: Permit2PayloadBase) {\n return [\n {\n permitted: {\n token: getAddress(permit2Payload.permit2Authorization.permitted.token),\n amount: BigInt(permit2Payload.permit2Authorization.permitted.amount),\n },\n nonce: BigInt(permit2Payload.permit2Authorization.nonce),\n deadline: BigInt(permit2Payload.permit2Authorization.deadline),\n },\n getAddress(permit2Payload.permit2Authorization.from),\n {\n to: getAddress(permit2Payload.permit2Authorization.witness.to),\n validAfter: BigInt(permit2Payload.permit2Authorization.witness.validAfter),\n },\n permit2Payload.signature,\n ] as const;\n}\n\n/**\n * Builds args for upto settle(permit, amount, owner, witness, signature).\n * The upto contract's settle() takes an additional `amount` parameter and the witness\n * includes a `facilitator` field.\n *\n * @param permit2Payload - The upto Permit2 payload containing authorization and signature data\n * @param settlementAmount - The amount to settle on-chain\n * @param facilitatorAddress - The facilitator address authorized in the witness\n * @returns Tuple of contract call arguments for the upto settle function\n */\nexport function buildUptoPermit2SettleArgs(\n permit2Payload: UptoPermit2Payload,\n settlementAmount: bigint,\n facilitatorAddress: `0x${string}`,\n) {\n return [\n {\n permitted: {\n token: getAddress(permit2Payload.permit2Authorization.permitted.token),\n amount: BigInt(permit2Payload.permit2Authorization.permitted.amount),\n },\n nonce: BigInt(permit2Payload.permit2Authorization.nonce),\n deadline: BigInt(permit2Payload.permit2Authorization.deadline),\n },\n settlementAmount,\n getAddress(permit2Payload.permit2Authorization.from),\n {\n to: getAddress(permit2Payload.permit2Authorization.witness.to),\n facilitator: getAddress(facilitatorAddress),\n validAfter: BigInt(permit2Payload.permit2Authorization.witness.validAfter),\n },\n permit2Payload.signature,\n ] as const;\n}\n\n/**\n * Splits a 65-byte EIP-2612 signature into v, r, s components.\n *\n * @param signature - The hex-encoded 65-byte signature (with or without 0x prefix)\n * @returns Object with v (uint8), r (bytes32 hex), s (bytes32 hex)\n * @throws Error if the signature is not exactly 65 bytes (130 hex chars)\n */\nexport function splitEip2612Signature(signature: string): {\n v: number;\n r: `0x${string}`;\n s: `0x${string}`;\n} {\n const sig = signature.startsWith(\"0x\") ? signature.slice(2) : signature;\n\n if (sig.length !== 130) {\n throw new Error(\n `invalid EIP-2612 signature length: expected 65 bytes (130 hex chars), got ${sig.length / 2} bytes`,\n );\n }\n\n const r = `0x${sig.slice(0, 64)}` as `0x${string}`;\n const s = `0x${sig.slice(64, 128)}` as `0x${string}`;\n const v = parseInt(sig.slice(128, 130), 16);\n\n return { v, r, s };\n}\n\n// ---------------------------------------------------------------------------\n// Client-side helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Creates a Permit2 payload for any scheme (exact or upto).\n * The only scheme-specific input is the proxy address used as the spender.\n *\n * @param proxyAddress - The x402 proxy contract address to set as spender\n * @param signer - The EVM client signer\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - The payment requirements\n * @returns Promise resolving to a payment payload result\n */\nexport async function createPermit2PayloadForProxy(\n proxyAddress: `0x${string}`,\n signer: ClientEvmSigner,\n x402Version: number,\n paymentRequirements: PaymentRequirements,\n): Promise<PaymentPayloadResult> {\n const now = Math.floor(Date.now() / 1000);\n const nonce = createPermit2Nonce();\n\n // Lower time bound - allow some clock skew\n const validAfter = (now - 600).toString();\n // Upper time bound is enforced by Permit2's deadline field\n const deadline = (now + paymentRequirements.maxTimeoutSeconds).toString();\n\n const permit2Authorization: Permit2Authorization & { from: `0x${string}` } = {\n from: signer.address,\n permitted: {\n token: getAddress(paymentRequirements.asset),\n amount: paymentRequirements.amount,\n },\n spender: proxyAddress,\n nonce,\n deadline,\n witness: {\n to: getAddress(paymentRequirements.payTo),\n validAfter,\n },\n };\n\n const signature = await signPermit2Authorization(\n signer,\n permit2Authorization,\n paymentRequirements,\n );\n\n return {\n x402Version,\n payload: { signature, permit2Authorization },\n };\n}\n\n/**\n * Signs a Permit2 authorization using EIP-712 with witness data.\n * The signature authorizes the proxy contract to transfer tokens on behalf of the signer.\n *\n * @param signer - The EVM client signer\n * @param permit2Authorization - The Permit2 authorization parameters\n * @param requirements - The payment requirements\n * @returns Promise resolving to the hex-encoded signature\n */\nasync function signPermit2Authorization(\n signer: ClientEvmSigner,\n permit2Authorization: Permit2Authorization & { from: `0x${string}` },\n requirements: PaymentRequirements,\n): Promise<`0x${string}`> {\n const chainId = getEvmChainId(requirements.network);\n\n return await signer.signTypedData({\n domain: { name: \"Permit2\", chainId, verifyingContract: PERMIT2_ADDRESS },\n types: permit2WitnessTypes,\n primaryType: \"PermitWitnessTransferFrom\",\n message: {\n permitted: {\n token: getAddress(permit2Authorization.permitted.token),\n amount: BigInt(permit2Authorization.permitted.amount),\n },\n spender: getAddress(permit2Authorization.spender),\n nonce: BigInt(permit2Authorization.nonce),\n deadline: BigInt(permit2Authorization.deadline),\n witness: {\n to: getAddress(permit2Authorization.witness.to),\n validAfter: BigInt(permit2Authorization.witness.validAfter),\n },\n },\n });\n}\n","import { encodeFunctionData, decodeFunctionResult } from \"viem\";\n\n/**\n * Multicall3 contract address.\n * Same address on all EVM chains via CREATE2 deployment.\n *\n * @see https://github.com/mds1/multicall\n */\nexport const MULTICALL3_ADDRESS = \"0xcA11bde05977b3631167028862bE2a173976CA11\" as const;\n\n/** Multicall3 getEthBalance ABI for querying native token balance. */\nexport const multicall3GetEthBalanceAbi = [\n {\n name: \"getEthBalance\",\n inputs: [{ name: \"addr\", type: \"address\" }],\n outputs: [{ name: \"balance\", type: \"uint256\" }],\n stateMutability: \"view\",\n type: \"function\",\n },\n] as const;\n\n/** Multicall3 tryAggregate ABI for batching calls. */\nconst multicall3ABI = [\n {\n inputs: [\n { name: \"requireSuccess\", type: \"bool\" },\n {\n name: \"calls\",\n type: \"tuple[]\",\n components: [\n { name: \"target\", type: \"address\" },\n { name: \"callData\", type: \"bytes\" },\n ],\n },\n ],\n name: \"tryAggregate\",\n outputs: [\n {\n name: \"returnData\",\n type: \"tuple[]\",\n components: [\n { name: \"success\", type: \"bool\" },\n { name: \"returnData\", type: \"bytes\" },\n ],\n },\n ],\n stateMutability: \"payable\",\n type: \"function\",\n },\n] as const;\n\nexport type ContractCall = {\n address: `0x${string}`;\n abi: readonly unknown[];\n functionName: string;\n args?: readonly unknown[];\n};\n\nexport type RawContractCall = {\n address: `0x${string}`;\n callData: `0x${string}`;\n};\n\nexport type MulticallSuccess = { status: \"success\"; result: unknown };\nexport type MulticallFailure = { status: \"failure\"; error: Error };\nexport type MulticallResult = MulticallSuccess | MulticallFailure;\n\n/**\n * Batches contract calls via Multicall3 `tryAggregate(false, ...)`.\n *\n * Accepts a mix of typed ContractCall (ABI-encoded + decoded) and\n * RawContractCall (pre-encoded calldata, no decoding) entries.\n * Raw calls are useful for the EIP-6492 factory deployment case\n * where calldata is pre-encoded with no ABI available.\n */\ntype ReadContractFn = (args: {\n address: `0x${string}`;\n abi: readonly unknown[];\n functionName: string;\n args?: readonly unknown[];\n}) => Promise<unknown>;\n\n/**\n * Executes multiple contract read calls in a single RPC round-trip using Multicall3.\n *\n * @param readContract - Function that performs a single contract read (e.g. viem readContract)\n * @param calls - Array of contract calls to batch (ContractCall or RawContractCall)\n * @returns A promise that resolves to an array of decoded results, one per call\n */\nexport async function multicall(\n readContract: ReadContractFn,\n calls: ReadonlyArray<ContractCall | RawContractCall>,\n): Promise<MulticallResult[]> {\n const aggregateCalls = calls.map(call => {\n if (\"callData\" in call) {\n return { target: call.address, callData: call.callData };\n }\n const callData = encodeFunctionData({\n abi: call.abi,\n functionName: call.functionName,\n args: call.args as unknown[],\n });\n return { target: call.address, callData };\n });\n\n const rawResults = (await readContract({\n address: MULTICALL3_ADDRESS,\n abi: multicall3ABI,\n functionName: \"tryAggregate\",\n args: [false, aggregateCalls],\n })) as { success: boolean; returnData: `0x${string}` }[];\n\n return rawResults.map((raw, i) => {\n if (!raw.success) {\n return {\n status: \"failure\" as const,\n error: new Error(`multicall: call reverted (returnData: ${raw.returnData})`),\n };\n }\n\n const call = calls[i];\n if (\"callData\" in call) {\n return { status: \"success\" as const, result: undefined };\n }\n\n try {\n const decoded = decodeFunctionResult({\n abi: call.abi,\n functionName: call.functionName,\n data: raw.returnData,\n });\n return { status: \"success\" as const, result: decoded };\n } catch (err) {\n return {\n status: \"failure\" as const,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n });\n}\n","import {\n PaymentPayload,\n PaymentRequirements,\n SchemeNetworkFacilitator,\n FacilitatorContext,\n SettleResponse,\n VerifyResponse,\n} from \"@payai/x402/types\";\nimport { FacilitatorEvmSigner } from \"../../signer\";\nimport { UptoPermit2Payload, isUptoPermit2Payload } from \"../../types\";\nimport { verifyUptoPermit2, settleUptoPermit2 } from \"./permit2\";\n\n/**\n * EVM facilitator implementation for the Upto payment scheme.\n * Handles verification and settlement of Permit2-based payments.\n */\nexport class UptoEvmScheme implements SchemeNetworkFacilitator {\n readonly scheme = \"upto\";\n readonly caipFamily = \"eip155:*\";\n\n /**\n * Creates a new UptoEvmScheme facilitator instance.\n *\n * @param signer - The EVM signer for facilitator operations\n */\n constructor(private readonly signer: FacilitatorEvmSigner) {}\n\n /**\n * Returns extra metadata required by the upto scheme, including the facilitator address.\n *\n * @param _ - The network identifier (unused)\n * @returns Object with facilitatorAddress, or undefined if no signer addresses are available\n */\n getExtra(_: string): Record<string, unknown> | undefined {\n const addresses = this.signer.getAddresses();\n if (addresses.length === 0) {\n return undefined;\n }\n return { facilitatorAddress: addresses[Math.floor(Math.random() * addresses.length)] };\n }\n\n /**\n * Returns the list of facilitator signer addresses for the upto scheme.\n *\n * @param _ - The network identifier (unused)\n * @returns Array of facilitator signer addresses\n */\n getSigners(_: string): string[] {\n return [...this.signer.getAddresses()];\n }\n\n /**\n * Verifies an upto Permit2 payment payload against the given requirements.\n *\n * @param payload - The payment payload to verify\n * @param requirements - The payment requirements to verify against\n * @param context - Optional facilitator context\n * @returns Promise resolving to a verification response\n */\n async verify(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n context?: FacilitatorContext,\n ): Promise<VerifyResponse> {\n const rawPayload = payload.payload as Record<string, unknown>;\n if (!isUptoPermit2Payload(rawPayload)) {\n return { isValid: false, invalidReason: \"unsupported_payload_type\", payer: \"\" };\n }\n return verifyUptoPermit2(\n this.signer,\n payload,\n requirements,\n rawPayload as UptoPermit2Payload,\n context,\n );\n }\n\n /**\n * Settles an upto Permit2 payment on-chain.\n *\n * @param payload - The payment payload to settle\n * @param requirements - The payment requirements\n * @param context - Optional facilitator context\n * @returns Promise resolving to a settlement response\n */\n async settle(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n context?: FacilitatorContext,\n ): Promise<SettleResponse> {\n const rawPayload = payload.payload as Record<string, unknown>;\n if (!isUptoPermit2Payload(rawPayload)) {\n return {\n success: false,\n network: payload.accepted.network,\n transaction: \"\",\n errorReason: \"unsupported_payload_type\",\n payer: \"\",\n };\n }\n return settleUptoPermit2(\n this.signer,\n payload,\n requirements,\n rawPayload as UptoPermit2Payload,\n context,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACuHO,SAAS,qBACd,SAC+B;AAC/B,MAAI,OAAO,QAAQ,cAAc,SAAU,QAAO;AAClD,MAAI,EAAE,0BAA0B,SAAU,QAAO;AAEjD,QAAM,OAAO,QAAQ;AACrB,MAAI,OAAO,SAAS,YAAY,SAAS,KAAM,QAAO;AAEtD,QAAM,IAAI;AACV,MAAI,OAAO,EAAE,SAAS,SAAU,QAAO;AACvC,MAAI,OAAO,EAAE,YAAY,SAAU,QAAO;AAC1C,MAAI,OAAO,EAAE,UAAU,SAAU,QAAO;AACxC,MAAI,OAAO,EAAE,aAAa,SAAU,QAAO;AAE3C,QAAM,YAAY,EAAE;AACpB,MAAI,OAAO,cAAc,YAAY,cAAc,KAAM,QAAO;AAChE,QAAM,IAAI;AACV,MAAI,OAAO,EAAE,UAAU,SAAU,QAAO;AACxC,MAAI,OAAO,EAAE,WAAW,SAAU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,MACA;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,OACJ,SACA,cACA,SACyB;AACzB,UAAM,aAAa,QAAQ;AAC3B,QAAI,CAAC,qBAAqB,UAAU,GAAG;AACrC,aAAO;AAAA,QACL,SAAS;AAAA,QACT,SAAS,QAAQ,SAAS;AAAA,QAC1B,aAAa;AAAA,QACb,aAAa;AAAA,QACb,OAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,MACL,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;","names":["import_viem","import_viem","import_viem","import_viem","simOk"]}
@@ -0,0 +1,77 @@
1
+ import { SchemeNetworkServer, MoneyParser, Network, Price, AssetAmount, PaymentRequirements } from '@payai/x402/types';
2
+
3
+ /**
4
+ * EVM server implementation for the Upto payment scheme.
5
+ * Handles price parsing, payment requirements enhancement, and default asset resolution.
6
+ */
7
+ declare class UptoEvmScheme implements SchemeNetworkServer {
8
+ readonly scheme = "upto";
9
+ private moneyParsers;
10
+ /**
11
+ * Registers a custom money parser for converting prices to asset amounts.
12
+ *
13
+ * @param parser - The money parser function to register
14
+ * @returns This instance for chaining
15
+ */
16
+ registerMoneyParser(parser: MoneyParser): UptoEvmScheme;
17
+ /**
18
+ * Returns the decimal precision of the default stablecoin for the given network.
19
+ * Implements the optional AssetDecimalsProvider interface used by resolveSettlementOverrideAmount.
20
+ *
21
+ * @param _asset - The asset symbol (unused; defaults to the network's default stablecoin)
22
+ * @param network - The network to look up the default asset for
23
+ * @returns The number of decimal places for the asset
24
+ */
25
+ getAssetDecimals(_asset: string, network: Network): number;
26
+ /**
27
+ * Parses a price into an asset amount for the given network.
28
+ *
29
+ * @param price - The price to parse (string, number, or AssetAmount)
30
+ * @param network - The target network
31
+ * @returns Promise resolving to an asset amount
32
+ */
33
+ parsePrice(price: Price, network: Network): Promise<AssetAmount>;
34
+ /**
35
+ * Enhances payment requirements with upto-specific metadata.
36
+ *
37
+ * @param paymentRequirements - The base payment requirements
38
+ * @param supportedKind - The supported scheme/network kind
39
+ * @param supportedKind.x402Version - The x402 protocol version
40
+ * @param supportedKind.scheme - The payment scheme name
41
+ * @param supportedKind.network - The target network
42
+ * @param supportedKind.extra - Optional extra metadata
43
+ * @param extensionKeys - Extension keys to include
44
+ * @returns Promise resolving to enhanced payment requirements
45
+ */
46
+ enhancePaymentRequirements(paymentRequirements: PaymentRequirements, supportedKind: {
47
+ x402Version: number;
48
+ scheme: string;
49
+ network: Network;
50
+ extra?: Record<string, unknown>;
51
+ }, extensionKeys: string[]): Promise<PaymentRequirements>;
52
+ /**
53
+ * Parses a money string or number into a decimal value.
54
+ *
55
+ * @param money - The money value to parse
56
+ * @returns The parsed decimal amount
57
+ */
58
+ private parseMoneyToDecimal;
59
+ /**
60
+ * Converts a numeric dollar amount to an AssetAmount using the default token for the network.
61
+ *
62
+ * @param amount - The dollar amount as a number
63
+ * @param network - The target network
64
+ * @returns The converted asset amount with token metadata
65
+ */
66
+ private defaultMoneyConversion;
67
+ /**
68
+ * Converts a decimal string amount to an integer token amount using the given decimals.
69
+ *
70
+ * @param decimalAmount - The amount as a decimal string (e.g. "1.5")
71
+ * @param decimals - The number of decimal places for the token
72
+ * @returns The token amount as an integer string in smallest units
73
+ */
74
+ private convertToTokenAmount;
75
+ }
76
+
77
+ export { UptoEvmScheme };
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/upto/server/index.ts
21
+ var server_exports = {};
22
+ __export(server_exports, {
23
+ UptoEvmScheme: () => UptoEvmScheme
24
+ });
25
+ module.exports = __toCommonJS(server_exports);
26
+
27
+ // src/upto/server/scheme.ts
28
+ var import_viem = require("viem");
29
+
30
+ // src/shared/defaultAssets.ts
31
+ var DEFAULT_STABLECOINS = {
32
+ "eip155:8453": {
33
+ address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
34
+ name: "USD Coin",
35
+ version: "2",
36
+ decimals: 6
37
+ },
38
+ // Base mainnet USDC
39
+ "eip155:84532": {
40
+ address: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
41
+ name: "USDC",
42
+ version: "2",
43
+ decimals: 6
44
+ },
45
+ // Base Sepolia USDC
46
+ "eip155:4326": {
47
+ address: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
48
+ name: "MegaUSD",
49
+ version: "1",
50
+ decimals: 18,
51
+ assetTransferMethod: "permit2",
52
+ supportsEip2612: true
53
+ },
54
+ // MegaETH mainnet MegaUSD (no EIP-3009, supports EIP-2612)
55
+ "eip155:143": {
56
+ address: "0x754704Bc059F8C67012fEd69BC8A327a5aafb603",
57
+ name: "USD Coin",
58
+ version: "2",
59
+ decimals: 6
60
+ },
61
+ // Monad mainnet USDC
62
+ "eip155:988": {
63
+ address: "0x779Ded0c9e1022225f8E0630b35a9b54bE713736",
64
+ name: "USDT0",
65
+ version: "1",
66
+ decimals: 6
67
+ },
68
+ // Stable mainnet USDT0
69
+ "eip155:2201": {
70
+ address: "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9",
71
+ name: "USDT0",
72
+ version: "1",
73
+ decimals: 6
74
+ },
75
+ // Stable testnet USDT0
76
+ "eip155:137": {
77
+ address: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
78
+ name: "USD Coin",
79
+ version: "2",
80
+ decimals: 6
81
+ },
82
+ // Polygon mainnet USDC
83
+ "eip155:42161": {
84
+ address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
85
+ name: "USD Coin",
86
+ version: "2",
87
+ decimals: 6
88
+ },
89
+ // Arbitrum One USDC
90
+ "eip155:421614": {
91
+ address: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
92
+ name: "USD Coin",
93
+ version: "2",
94
+ decimals: 6
95
+ }
96
+ // Arbitrum Sepolia USDC
97
+ };
98
+ function getDefaultAsset(network) {
99
+ const info = DEFAULT_STABLECOINS[network];
100
+ if (!info) {
101
+ throw new Error(`No default asset configured for network ${network}`);
102
+ }
103
+ return info;
104
+ }
105
+
106
+ // src/upto/server/scheme.ts
107
+ var UptoEvmScheme = class {
108
+ constructor() {
109
+ this.scheme = "upto";
110
+ this.moneyParsers = [];
111
+ }
112
+ /**
113
+ * Registers a custom money parser for converting prices to asset amounts.
114
+ *
115
+ * @param parser - The money parser function to register
116
+ * @returns This instance for chaining
117
+ */
118
+ registerMoneyParser(parser) {
119
+ this.moneyParsers.push(parser);
120
+ return this;
121
+ }
122
+ /**
123
+ * Returns the decimal precision of the default stablecoin for the given network.
124
+ * Implements the optional AssetDecimalsProvider interface used by resolveSettlementOverrideAmount.
125
+ *
126
+ * @param _asset - The asset symbol (unused; defaults to the network's default stablecoin)
127
+ * @param network - The network to look up the default asset for
128
+ * @returns The number of decimal places for the asset
129
+ */
130
+ getAssetDecimals(_asset, network) {
131
+ try {
132
+ return getDefaultAsset(network).decimals;
133
+ } catch {
134
+ return 6;
135
+ }
136
+ }
137
+ /**
138
+ * Parses a price into an asset amount for the given network.
139
+ *
140
+ * @param price - The price to parse (string, number, or AssetAmount)
141
+ * @param network - The target network
142
+ * @returns Promise resolving to an asset amount
143
+ */
144
+ async parsePrice(price, network) {
145
+ if (typeof price === "object" && price !== null && "amount" in price) {
146
+ if (!price.asset) {
147
+ throw new Error(`Asset address must be specified for AssetAmount on network ${network}`);
148
+ }
149
+ return {
150
+ amount: price.amount,
151
+ asset: price.asset,
152
+ extra: price.extra || {}
153
+ };
154
+ }
155
+ const amount = this.parseMoneyToDecimal(price);
156
+ for (const parser of this.moneyParsers) {
157
+ const result = await parser(amount, network);
158
+ if (result !== null) {
159
+ return result;
160
+ }
161
+ }
162
+ return this.defaultMoneyConversion(amount, network);
163
+ }
164
+ /**
165
+ * Enhances payment requirements with upto-specific metadata.
166
+ *
167
+ * @param paymentRequirements - The base payment requirements
168
+ * @param supportedKind - The supported scheme/network kind
169
+ * @param supportedKind.x402Version - The x402 protocol version
170
+ * @param supportedKind.scheme - The payment scheme name
171
+ * @param supportedKind.network - The target network
172
+ * @param supportedKind.extra - Optional extra metadata
173
+ * @param extensionKeys - Extension keys to include
174
+ * @returns Promise resolving to enhanced payment requirements
175
+ */
176
+ enhancePaymentRequirements(paymentRequirements, supportedKind, extensionKeys) {
177
+ void extensionKeys;
178
+ return Promise.resolve({
179
+ ...paymentRequirements,
180
+ extra: {
181
+ ...paymentRequirements.extra,
182
+ assetTransferMethod: "permit2",
183
+ ...supportedKind.extra?.facilitatorAddress ? { facilitatorAddress: (0, import_viem.getAddress)(supportedKind.extra.facilitatorAddress) } : {}
184
+ }
185
+ });
186
+ }
187
+ /**
188
+ * Parses a money string or number into a decimal value.
189
+ *
190
+ * @param money - The money value to parse
191
+ * @returns The parsed decimal amount
192
+ */
193
+ parseMoneyToDecimal(money) {
194
+ if (typeof money === "number") {
195
+ return money;
196
+ }
197
+ const cleanMoney = money.replace(/^\$/, "").trim();
198
+ const amount = parseFloat(cleanMoney);
199
+ if (isNaN(amount)) {
200
+ throw new Error(`Invalid money format: ${money}`);
201
+ }
202
+ return amount;
203
+ }
204
+ /**
205
+ * Converts a numeric dollar amount to an AssetAmount using the default token for the network.
206
+ *
207
+ * @param amount - The dollar amount as a number
208
+ * @param network - The target network
209
+ * @returns The converted asset amount with token metadata
210
+ */
211
+ defaultMoneyConversion(amount, network) {
212
+ const assetInfo = getDefaultAsset(network);
213
+ const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);
214
+ return {
215
+ amount: tokenAmount,
216
+ asset: assetInfo.address,
217
+ extra: {
218
+ name: assetInfo.name,
219
+ version: assetInfo.version,
220
+ assetTransferMethod: "permit2"
221
+ }
222
+ };
223
+ }
224
+ /**
225
+ * Converts a decimal string amount to an integer token amount using the given decimals.
226
+ *
227
+ * @param decimalAmount - The amount as a decimal string (e.g. "1.5")
228
+ * @param decimals - The number of decimal places for the token
229
+ * @returns The token amount as an integer string in smallest units
230
+ */
231
+ convertToTokenAmount(decimalAmount, decimals) {
232
+ const amount = parseFloat(decimalAmount);
233
+ if (isNaN(amount)) {
234
+ throw new Error(`Invalid amount: ${decimalAmount}`);
235
+ }
236
+ const [intPart, decPart = ""] = String(amount).split(".");
237
+ const paddedDec = decPart.padEnd(decimals, "0").slice(0, decimals);
238
+ const tokenAmount = (intPart + paddedDec).replace(/^0+/, "") || "0";
239
+ return tokenAmount;
240
+ }
241
+ };
242
+ // Annotate the CommonJS export names for ESM import in node:
243
+ 0 && (module.exports = {
244
+ UptoEvmScheme
245
+ });
246
+ //# sourceMappingURL=index.js.map