{"version":3,"sources":["../../src/batch-settlement/constants.ts","../../src/batch-settlement/utils.ts","../../src/batch-settlement/errors.ts"],"sourcesContent":["import { keccak256, toBytes } from \"viem\";\n\n/** Scheme identifier for the batch-settlement payment scheme. */\nexport const BATCH_SETTLEMENT_SCHEME = \"batch-settlement\" as const;\n\n/** Deployed address of the x402BatchSettlement contract. */\nexport const BATCH_SETTLEMENT_ADDRESS = \"0x4020074e9dF2ce1deE5A9C1b5c3f541D02a10003\" as const;\n\n/** Deployed address of the ERC3009DepositCollector contract. */\nexport const ERC3009_DEPOSIT_COLLECTOR_ADDRESS =\n  \"0x4020806089470a89826cB9fB1f4059150b550004\" as const;\n\n/** Deployed address of the Permit2DepositCollector contract. */\nexport const PERMIT2_DEPOSIT_COLLECTOR_ADDRESS =\n  \"0x4020425FAf3B746C082C2f942b4E5159887B0005\" as const;\n\n/** Minimum withdraw delay in seconds (15 minutes), matching the onchain constant. */\nexport const MIN_WITHDRAW_DELAY = 900;\n\n/** Maximum withdraw delay in seconds (30 days), matching the onchain constant. */\nexport const MAX_WITHDRAW_DELAY = 2_592_000;\n\n/** EIP-712 domain fields shared across all batch-settlement typed-data signatures. */\nexport const BATCH_SETTLEMENT_DOMAIN = {\n  name: \"x402 Batch Settlement\",\n  version: \"1\",\n} as const;\n\n/** EIP-712 type hash for channel identity. */\nexport const CHANNEL_CONFIG_TYPEHASH = keccak256(\n  toBytes(\n    \"ChannelConfig(address payer,address payerAuthorizer,address receiver,address receiverAuthorizer,address token,uint40 withdrawDelay,bytes32 salt)\",\n  ),\n);\n\n/** EIP-712 type definition for a channel configuration. */\nexport const channelConfigTypes = {\n  ChannelConfig: [\n    { name: \"payer\", type: \"address\" },\n    { name: \"payerAuthorizer\", type: \"address\" },\n    { name: \"receiver\", type: \"address\" },\n    { name: \"receiverAuthorizer\", type: \"address\" },\n    { name: \"token\", type: \"address\" },\n    { name: \"withdrawDelay\", type: \"uint40\" },\n    { name: \"salt\", type: \"bytes32\" },\n  ],\n} as const;\n\n/** EIP-712 type definition for a cumulative voucher: `Voucher(bytes32 channelId, uint128 maxClaimableAmount)`. */\nexport const voucherTypes = {\n  Voucher: [\n    { name: \"channelId\", type: \"bytes32\" },\n    { name: \"maxClaimableAmount\", type: \"uint128\" },\n  ],\n} as const;\n\n/** EIP-712 type definition for cooperative refund: `Refund(bytes32 channelId, uint256 nonce, uint128 amount)`. */\nexport const refundTypes = {\n  Refund: [\n    { name: \"channelId\", type: \"bytes32\" },\n    { name: \"nonce\", type: \"uint256\" },\n    { name: \"amount\", type: \"uint128\" },\n  ],\n} as const;\n\n/** EIP-712 type definitions for a receiver-authorizer claim batch (nested ClaimEntry). */\nexport const claimBatchTypes = {\n  ClaimBatch: [{ name: \"claims\", type: \"ClaimEntry[]\" }],\n  ClaimEntry: [\n    { name: \"channelId\", type: \"bytes32\" },\n    { name: \"maxClaimableAmount\", type: \"uint128\" },\n    { name: \"totalClaimed\", type: \"uint128\" },\n  ],\n} as const;\n\n/** EIP-712 type definition for ERC-3009 `ReceiveWithAuthorization` (used for gasless deposits). */\nexport const receiveAuthorizationTypes = {\n  ReceiveWithAuthorization: [\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/** Permit2 typed data for channel-bound batch deposits. */\nexport const batchPermit2WitnessTypes = {\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: \"DepositWitness\" },\n  ],\n  TokenPermissions: [\n    { name: \"token\", type: \"address\" },\n    { name: \"amount\", type: \"uint256\" },\n  ],\n  DepositWitness: [{ name: \"channelId\", type: \"bytes32\" }],\n} as const;\n","import { getAddress, hashTypedData } from \"viem\";\nimport { BATCH_SETTLEMENT_ADDRESS, BATCH_SETTLEMENT_DOMAIN, channelConfigTypes } from \"./constants\";\nimport { ErrChannelIdMismatch, ErrInvalidChannelId } from \"./errors\";\nimport type { ChannelConfig } from \"./types\";\nimport { getEvmChainId } from \"../utils\";\n\n/** Canonical `bytes32` channel id: `0x` followed by exactly 64 hex digits. */\nconst CHANNEL_ID_RE = /^0x[0-9a-fA-F]{64}$/;\n\n/**\n * Narrows an untrusted value to a canonical `bytes32` channel id string.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a `0x`-prefixed 64-hex-digit string.\n */\nexport function isCanonicalChannelId(value: unknown): value is `0x${string}` {\n  return typeof value === \"string\" && CHANNEL_ID_RE.test(value);\n}\n\n/**\n * Validates canonical `bytes32` form and normalizes to lowercase.\n *\n * @param channelId - Untrusted channel identifier from a request payload.\n * @returns The lowercased channel id.\n * @throws When `channelId` is not a canonical `bytes32` string. The message is generic\n *   so untrusted input is never echoed into logs.\n */\nexport function normalizeChannelId(channelId: string): `0x${string}` {\n  if (!isCanonicalChannelId(channelId)) {\n    throw new Error(ErrInvalidChannelId);\n  }\n  return channelId.toLowerCase() as `0x${string}`;\n}\n\n/**\n * Binds a claimed channel id to a channel config and network.\n *\n * @param config - The immutable channel configuration from the payload.\n * @param claimedChannelId - The channel id the client claims the config resolves to.\n * @param networkOrChainId - CAIP-2 network identifier or numeric EVM chain id.\n * @returns An error code when the id is malformed or does not match the config, else `undefined`.\n */\nexport function channelIdBindingError(\n  config: ChannelConfig,\n  claimedChannelId: string,\n  networkOrChainId: string | number,\n): string | undefined {\n  if (!isCanonicalChannelId(claimedChannelId)) return ErrInvalidChannelId;\n  if (computeChannelId(config, networkOrChainId).toLowerCase() !== claimedChannelId.toLowerCase()) {\n    return ErrChannelIdMismatch;\n  }\n  return undefined;\n}\n\n/**\n * Computes the chain-bound channel id from a {@link ChannelConfig} struct.\n *\n * @param config - The immutable channel configuration.\n * @param networkOrChainId - CAIP-2 network identifier or numeric EVM chain id.\n * @returns The `bytes32` channel id as a hex string.\n */\nexport function computeChannelId(\n  config: ChannelConfig,\n  networkOrChainId: string | number,\n): `0x${string}` {\n  const chainId =\n    typeof networkOrChainId === \"number\" ? networkOrChainId : getEvmChainId(networkOrChainId);\n  return hashTypedData({\n    domain: getBatchSettlementEip712Domain(chainId),\n    types: channelConfigTypes,\n    primaryType: \"ChannelConfig\",\n    message: {\n      payer: config.payer,\n      payerAuthorizer: config.payerAuthorizer,\n      receiver: config.receiver,\n      receiverAuthorizer: config.receiverAuthorizer,\n      token: config.token,\n      withdrawDelay: config.withdrawDelay,\n      salt: config.salt,\n    },\n  });\n}\n\n/**\n * Returns the full EIP-712 domain for the batch-settlement contract on the given chain.\n *\n * @param chainId - Numeric EVM chain id.\n * @returns EIP-712 domain with `name`, `version`, `chainId`, and checksummed `verifyingContract`.\n */\nexport function getBatchSettlementEip712Domain(chainId: number) {\n  return {\n    ...BATCH_SETTLEMENT_DOMAIN,\n    chainId,\n    verifyingContract: getAddress(BATCH_SETTLEMENT_ADDRESS),\n  } as const;\n}\n","/** Error codes for the batch-settlement EVM scheme (see scheme_batch_settlement_evm2.md). */\n\nexport const ErrChannelNotFound = \"invalid_batch_settlement_evm_channel_not_found\";\nexport const ErrTokenMismatch = \"invalid_batch_settlement_evm_token_mismatch\";\nexport const ErrInvalidVoucherSignature = \"invalid_batch_settlement_evm_voucher_signature\";\nexport const ErrCumulativeExceedsBalance =\n  \"invalid_batch_settlement_evm_cumulative_exceeds_balance\";\nexport const ErrCumulativeAmountBelowClaimed =\n  \"invalid_batch_settlement_evm_cumulative_below_claimed\";\nexport const ErrInsufficientBalance = \"invalid_batch_settlement_evm_insufficient_balance\";\nexport const ErrDepositTransactionFailed =\n  \"invalid_batch_settlement_evm_deposit_transaction_failed\";\nexport const ErrClaimTransactionFailed = \"invalid_batch_settlement_evm_claim_transaction_failed\";\nexport const ErrSettleTransactionFailed = \"invalid_batch_settlement_evm_settle_transaction_failed\";\nexport const ErrInvalidScheme = \"invalid_batch_settlement_evm_scheme\";\nexport const ErrNetworkMismatch = \"invalid_batch_settlement_evm_network_mismatch\";\nexport const ErrMissingEip712Domain = \"invalid_batch_settlement_evm_missing_eip712_domain\";\nexport const ErrValidBeforeExpired =\n  \"invalid_batch_settlement_evm_payload_authorization_valid_before\";\nexport const ErrValidAfterInFuture =\n  \"invalid_batch_settlement_evm_payload_authorization_valid_after\";\nexport const ErrInvalidReceiveAuthorizationSignature =\n  \"invalid_batch_settlement_evm_receive_authorization_signature\";\nexport const ErrErc3009AuthorizationRequired =\n  \"invalid_batch_settlement_evm_erc3009_authorization_required\";\nexport const ErrRefundTransactionFailed = \"invalid_batch_settlement_evm_refund_transaction_failed\";\nexport const ErrInvalidPayloadType = \"invalid_batch_settlement_evm_payload_type\";\nexport const ErrWithdrawDelayOutOfRange =\n  \"invalid_batch_settlement_evm_withdraw_delay_out_of_range\";\nexport const ErrChannelIdMismatch = \"invalid_batch_settlement_evm_channel_id_mismatch\";\nexport const ErrInvalidChannelId = \"invalid_batch_settlement_evm_channel_id_invalid\";\nexport const ErrReceiverMismatch = \"invalid_batch_settlement_evm_receiver_mismatch\";\nexport const ErrReceiverAuthorizerMismatch =\n  \"invalid_batch_settlement_evm_receiver_authorizer_mismatch\";\nexport const ErrWithdrawDelayMismatch = \"invalid_batch_settlement_evm_withdraw_delay_mismatch\";\nexport const ErrAuthorizerAddressMismatch =\n  \"invalid_batch_settlement_evm_authorizer_address_mismatch\";\nexport const ErrAuthorizerNotConfigured = \"invalid_batch_settlement_evm_authorizer_not_configured\";\nexport const ErrDepositSimulationFailed = \"invalid_batch_settlement_evm_deposit_simulation_failed\";\n\n// ERC-6492 counterfactual deployment errors (ERC-3009 deposit path). Wire values keep the\n// scheme prefix to match the rest of this module's contract.\nexport const ErrFactoryNotAllowed = \"invalid_batch_settlement_evm_eip6492_factory_not_allowed\";\nexport const ErrSmartWalletDeploymentFailed =\n  \"invalid_batch_settlement_evm_smart_wallet_deployment_failed\";\nexport const ErrClaimSimulationFailed = \"invalid_batch_settlement_evm_claim_simulation_failed\";\nexport const ErrSettleSimulationFailed = \"invalid_batch_settlement_evm_settle_simulation_failed\";\nexport const ErrNothingToSettle = \"invalid_batch_settlement_evm_nothing_to_settle\";\nexport const ErrRefundPayload = \"invalid_batch_settlement_evm_refund_payload\";\nexport const ErrRefundSimulationFailed = \"invalid_batch_settlement_evm_refund_simulation_failed\";\nexport const ErrRpcReadFailed = \"invalid_batch_settlement_evm_rpc_read_failed\";\nexport const ErrPermit2AuthorizationRequired =\n  \"invalid_batch_settlement_evm_permit2_authorization_required\";\nexport const ErrPermit2InvalidSpender = \"invalid_batch_settlement_evm_permit2_invalid_spender\";\nexport const ErrPermit2AmountMismatch = \"invalid_batch_settlement_evm_permit2_amount_mismatch\";\nexport const ErrPermit2DeadlineExpired = \"invalid_batch_settlement_evm_permit2_deadline_expired\";\nexport const ErrPermit2InvalidSignature = \"invalid_batch_settlement_evm_permit2_invalid_signature\";\nexport const ErrPermit2AllowanceRequired =\n  \"invalid_batch_settlement_evm_permit2_allowance_required\";\nexport const ErrEip2612AmountMismatch = \"invalid_batch_settlement_evm_eip2612_amount_mismatch\";\nexport const ErrEip2612OwnerMismatch = \"invalid_batch_settlement_evm_eip2612_owner_mismatch\";\nexport const ErrEip2612AssetMismatch = \"invalid_batch_settlement_evm_eip2612_asset_mismatch\";\nexport const ErrEip2612SpenderMismatch = \"invalid_batch_settlement_evm_eip2612_spender_mismatch\";\nexport const ErrEip2612DeadlineExpired = \"invalid_batch_settlement_evm_eip2612_deadline_expired\";\nexport const ErrErc20ApprovalUnavailable =\n  \"invalid_batch_settlement_evm_erc20_approval_unavailable\";\n\n/** Resource server: 402 `error` and lifecycle `reason` (same strings as the spec). */\nexport const ErrCumulativeAmountMismatch =\n  \"invalid_batch_settlement_evm_cumulative_amount_mismatch\";\nexport const ErrChannelBusy = \"invalid_batch_settlement_evm_channel_busy\";\nexport const ErrVerificationStateUnavailable =\n  \"invalid_batch_settlement_evm_verification_state_unavailable\";\nexport const ErrChargeExceedsSignedCumulative =\n  \"invalid_batch_settlement_evm_charge_exceeds_signed_cumulative\";\nexport const ErrMissingChannel = \"invalid_batch_settlement_evm_missing_channel\";\nexport const ErrRefundNoBalance = \"invalid_batch_settlement_evm_refund_no_balance\";\nexport const ErrRefundAmountInvalid = \"invalid_batch_settlement_evm_refund_amount_invalid\";\n"],"mappings":";;;;;AAAA,SAAS,WAAW,eAAe;AAG5B,IAAM,0BAA0B;AAGhC,IAAM,2BAA2B;AAGjC,IAAM,oCACX;AAGK,IAAM,oCACX;AAGK,IAAM,qBAAqB;AAG3B,IAAM,qBAAqB;AAG3B,IAAM,0BAA0B;AAAA,EACrC,MAAM;AAAA,EACN,SAAS;AACX;AAGO,IAAM,0BAA0B;AAAA,EACrC;AAAA,IACE;AAAA,EACF;AACF;AAGO,IAAM,qBAAqB;AAAA,EAChC,eAAe;AAAA,IACb,EAAE,MAAM,SAAS,MAAM,UAAU;AAAA,IACjC,EAAE,MAAM,mBAAmB,MAAM,UAAU;AAAA,IAC3C,EAAE,MAAM,YAAY,MAAM,UAAU;AAAA,IACpC,EAAE,MAAM,sBAAsB,MAAM,UAAU;AAAA,IAC9C,EAAE,MAAM,SAAS,MAAM,UAAU;AAAA,IACjC,EAAE,MAAM,iBAAiB,MAAM,SAAS;AAAA,IACxC,EAAE,MAAM,QAAQ,MAAM,UAAU;AAAA,EAClC;AACF;AAGO,IAAM,eAAe;AAAA,EAC1B,SAAS;AAAA,IACP,EAAE,MAAM,aAAa,MAAM,UAAU;AAAA,IACrC,EAAE,MAAM,sBAAsB,MAAM,UAAU;AAAA,EAChD;AACF;AAGO,IAAM,cAAc;AAAA,EACzB,QAAQ;AAAA,IACN,EAAE,MAAM,aAAa,MAAM,UAAU;AAAA,IACrC,EAAE,MAAM,SAAS,MAAM,UAAU;AAAA,IACjC,EAAE,MAAM,UAAU,MAAM,UAAU;AAAA,EACpC;AACF;AAGO,IAAM,kBA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