@payai/x402-evm 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/exact/client/index.d.ts +2 -2
- package/dist/cjs/exact/server/index.js +19 -4
- package/dist/cjs/exact/server/index.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/{permit2-CQbXqCMC.d.ts → permit2-DHAq6FTe.d.ts} +1 -1
- package/dist/esm/exact/client/index.d.mts +2 -2
- package/dist/esm/exact/server/index.mjs +19 -4
- package/dist/esm/exact/server/index.mjs.map +1 -1
- package/dist/esm/index.d.mts +1 -1
- package/dist/esm/{permit2-CGOcN7Et.d.mts → permit2-BuAhWvNC.d.mts} +1 -1
- package/package.json +5 -5
- package/dist/cjs/permit2-BYv82va2.d.ts +0 -103
- package/dist/cjs/signer-5OVDxViv.d.ts +0 -79
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { E as ExactEvmScheme,
|
|
2
|
-
import { SelectPaymentRequirements, PaymentPolicy
|
|
1
|
+
export { E as ExactEvmScheme, P as Permit2AllowanceParams, c as createPermit2ApprovalTx, e as erc20AllowanceAbi, g as getPermit2AllowanceReadParams } from '../../permit2-DHAq6FTe.js';
|
|
2
|
+
import { x402Client, SelectPaymentRequirements, PaymentPolicy } from '@x402/core/client';
|
|
3
3
|
import { Network } from '@x402/core/types';
|
|
4
4
|
import { C as ClientEvmSigner } from '../../signer-DC81R8wQ.js';
|
|
5
5
|
|
|
@@ -131,12 +131,18 @@ var ExactEvmScheme = class {
|
|
|
131
131
|
defaultMoneyConversion(amount, network) {
|
|
132
132
|
const assetInfo = this.getDefaultAsset(network);
|
|
133
133
|
const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);
|
|
134
|
+
const includeEip712Domain = !assetInfo.assetTransferMethod || assetInfo.supportsEip2612;
|
|
134
135
|
return {
|
|
135
136
|
amount: tokenAmount,
|
|
136
137
|
asset: assetInfo.address,
|
|
137
138
|
extra: {
|
|
138
|
-
|
|
139
|
-
|
|
139
|
+
...includeEip712Domain && {
|
|
140
|
+
name: assetInfo.name,
|
|
141
|
+
version: assetInfo.version
|
|
142
|
+
},
|
|
143
|
+
...assetInfo.assetTransferMethod && {
|
|
144
|
+
assetTransferMethod: assetInfo.assetTransferMethod
|
|
145
|
+
}
|
|
140
146
|
}
|
|
141
147
|
};
|
|
142
148
|
}
|
|
@@ -183,9 +189,18 @@ var ExactEvmScheme = class {
|
|
|
183
189
|
address: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
184
190
|
name: "MegaUSD",
|
|
185
191
|
version: "1",
|
|
186
|
-
decimals: 18
|
|
192
|
+
decimals: 18,
|
|
193
|
+
assetTransferMethod: "permit2",
|
|
194
|
+
supportsEip2612: true
|
|
195
|
+
},
|
|
196
|
+
// MegaETH mainnet MegaUSD (no EIP-3009, supports EIP-2612)
|
|
197
|
+
"eip155:143": {
|
|
198
|
+
address: "0x754704Bc059F8C67012fEd69BC8A327a5aafb603",
|
|
199
|
+
name: "USD Coin",
|
|
200
|
+
version: "2",
|
|
201
|
+
decimals: 6
|
|
187
202
|
}
|
|
188
|
-
//
|
|
203
|
+
// Monad mainnet USDC
|
|
189
204
|
};
|
|
190
205
|
const assetInfo = stablecoins[network];
|
|
191
206
|
if (!assetInfo) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/exact/server/index.ts","../../../../src/exact/server/scheme.ts","../../../../src/exact/server/register.ts"],"sourcesContent":["export { ExactEvmScheme } from \"./scheme\";\nexport { registerExactEvmScheme } from \"./register\";\nexport type { EvmResourceServerConfig } from \"./register\";\n","import {\n AssetAmount,\n Network,\n PaymentRequirements,\n Price,\n SchemeNetworkServer,\n MoneyParser,\n} from \"@x402/core/types\";\n\n/**\n * EVM server implementation for the Exact payment scheme.\n */\nexport class ExactEvmScheme implements SchemeNetworkServer {\n readonly scheme = \"exact\";\n private moneyParsers: MoneyParser[] = [];\n\n /**\n * Register a custom money parser in the parser chain.\n * Multiple parsers can be registered - they will be tried in registration order.\n * Each parser receives a decimal amount (e.g., 1.50 for $1.50).\n * If a parser returns null, the next parser in the chain will be tried.\n * The default parser is always the final fallback.\n *\n * @param parser - Custom function to convert amount to AssetAmount (or null to skip)\n * @returns The server instance for chaining\n *\n * @example\n * evmServer.registerMoneyParser(async (amount, network) => {\n * // Custom conversion logic\n * if (amount > 100) {\n * // Use different token for large amounts\n * return { amount: (amount * 1e18).toString(), asset: \"0xCustomToken\" };\n * }\n * return null; // Use next parser\n * });\n */\n registerMoneyParser(parser: MoneyParser): ExactEvmScheme {\n this.moneyParsers.push(parser);\n return this;\n }\n\n /**\n * Parses a price into an asset amount.\n * If price is already an AssetAmount, returns it directly.\n * If price is Money (string | number), parses to decimal and tries custom parsers.\n * Falls back to default conversion if all custom parsers return null.\n *\n * @param price - The price to parse\n * @param network - The network to use\n * @returns Promise that resolves to the parsed asset amount\n */\n async parsePrice(price: Price, network: Network): Promise<AssetAmount> {\n // If already an AssetAmount, return it directly\n if (typeof price === \"object\" && price !== null && \"amount\" in price) {\n if (!price.asset) {\n throw new Error(`Asset address must be specified for AssetAmount on network ${network}`);\n }\n return {\n amount: price.amount,\n asset: price.asset,\n extra: price.extra || {},\n };\n }\n\n // Parse Money to decimal number\n const amount = this.parseMoneyToDecimal(price);\n\n // Try each custom money parser in order\n for (const parser of this.moneyParsers) {\n const result = await parser(amount, network);\n if (result !== null) {\n return result;\n }\n }\n\n // All custom parsers returned null, use default conversion\n return this.defaultMoneyConversion(amount, network);\n }\n\n /**\n * Build payment requirements for this scheme/network combination\n *\n * @param paymentRequirements - The base payment requirements\n * @param supportedKind - The supported kind from facilitator (unused)\n * @param supportedKind.x402Version - The x402 version\n * @param supportedKind.scheme - The logical payment scheme\n * @param supportedKind.network - The network identifier in CAIP-2 format\n * @param supportedKind.extra - Optional extra metadata regarding scheme/network implementation details\n * @param extensionKeys - Extension keys supported by the facilitator (unused)\n * @returns Payment requirements ready to be sent to clients\n */\n enhancePaymentRequirements(\n paymentRequirements: PaymentRequirements,\n supportedKind: {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n },\n extensionKeys: string[],\n ): Promise<PaymentRequirements> {\n // Mark unused parameters to satisfy linter\n void supportedKind;\n void extensionKeys;\n return Promise.resolve(paymentRequirements);\n }\n\n /**\n * Parse Money (string | number) to a decimal number.\n * Handles formats like \"$1.50\", \"1.50\", 1.50, etc.\n *\n * @param money - The money value to parse\n * @returns Decimal number\n */\n private parseMoneyToDecimal(money: string | number): number {\n if (typeof money === \"number\") {\n return money;\n }\n\n // Remove $ sign and whitespace, then parse\n const cleanMoney = money.replace(/^\\$/, \"\").trim();\n const amount = parseFloat(cleanMoney);\n\n if (isNaN(amount)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n return amount;\n }\n\n /**\n * Default money conversion implementation.\n * Converts decimal amount to the default stablecoin on the specified network.\n *\n * @param amount - The decimal amount (e.g., 1.50)\n * @param network - The network to use\n * @returns The parsed asset amount in the default stablecoin\n */\n private defaultMoneyConversion(amount: number, network: Network): AssetAmount {\n const assetInfo = this.getDefaultAsset(network);\n const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);\n\n return {\n amount: tokenAmount,\n asset: assetInfo.address,\n extra: {\n name: assetInfo.name,\n version: assetInfo.version,\n },\n };\n }\n\n /**\n * Convert decimal amount to token units (e.g., 0.10 -> 100000 for 6-decimal tokens)\n *\n * @param decimalAmount - The decimal amount to convert\n * @param decimals - The number of decimals for the token\n * @returns The token amount as a string\n */\n private convertToTokenAmount(decimalAmount: string, decimals: number): string {\n const amount = parseFloat(decimalAmount);\n if (isNaN(amount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n // Convert to smallest unit (e.g., for USDC with 6 decimals: 0.10 * 10^6 = 100000)\n const [intPart, decPart = \"\"] = String(amount).split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n return tokenAmount;\n }\n\n /**\n * Get the default asset info for a network (typically USDC)\n *\n * @param network - The network to get asset info for\n * @returns The asset information including address, name, version, and decimals\n */\n private getDefaultAsset(network: Network): {\n address: string;\n name: string;\n version: string;\n decimals: number;\n } {\n // Map of network to USDC info including EIP-712 domain parameters\n // Each network has the right to determine its own default stablecoin that can be expressed as a USD string by calling servers\n // NOTE: Currently only EIP-3009 supporting stablecoins can be used with this scheme\n // Generic ERC20 support via EIP-2612/permit2 is planned, but not yet implemented.\n const stablecoins: Record<\n string,\n { address: string; name: string; version: string; decimals: number }\n > = {\n \"eip155:8453\": {\n address: \"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913\",\n name: \"USD Coin\",\n version: \"2\",\n decimals: 6,\n }, // Base mainnet USDC\n \"eip155:84532\": {\n address: \"0x036CbD53842c5426634e7929541eC2318f3dCF7e\",\n name: \"USDC\",\n version: \"2\",\n decimals: 6,\n }, // Base Sepolia USDC\n \"eip155:4326\": {\n address: \"0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7\",\n name: \"MegaUSD\",\n version: \"1\",\n decimals: 18,\n }, // MegaETH mainnet USDM\n };\n\n const assetInfo = stablecoins[network];\n if (!assetInfo) {\n throw new Error(`No default asset configured for network ${network}`);\n }\n\n return assetInfo;\n }\n}\n","import { x402ResourceServer } from \"@x402/core/server\";\nimport { Network } from \"@x402/core/types\";\nimport { ExactEvmScheme } from \"./scheme\";\n\n/**\n * Configuration options for registering EVM schemes to an x402ResourceServer\n */\nexport interface EvmResourceServerConfig {\n /**\n * Optional specific networks to register\n * If not provided, registers wildcard support (eip155:*)\n */\n networks?: Network[];\n}\n\n/**\n * Registers EVM exact payment schemes to an x402ResourceServer instance.\n *\n * This function registers:\n * - V2: eip155:* wildcard scheme with ExactEvmScheme (or specific networks if provided)\n *\n * @param server - The x402ResourceServer instance to register schemes to\n * @param config - Configuration for EVM resource server registration\n * @returns The server instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactEvmScheme } from \"@x402/evm/exact/server/register\";\n * import { x402ResourceServer } from \"@x402/core/server\";\n *\n * const server = new x402ResourceServer(facilitatorClient);\n * registerExactEvmScheme(server, {});\n * ```\n */\nexport function registerExactEvmScheme(\n server: x402ResourceServer,\n config: EvmResourceServerConfig = {},\n): x402ResourceServer {\n // Register V2 scheme\n if (config.networks && config.networks.length > 0) {\n // Register specific networks\n config.networks.forEach(network => {\n server.register(network, new ExactEvmScheme());\n });\n } else {\n // Register wildcard for all EVM chains\n server.register(\"eip155:*\", new ExactEvmScheme());\n }\n\n return server;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYO,IAAM,iBAAN,MAAoD;AAAA,EAApD;AACL,SAAS,SAAS;AAClB,SAAQ,eAA8B,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBvC,oBAAoB,QAAqC;AACvD,SAAK,aAAa,KAAK,MAAM;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,MAAM,WAAW,OAAc,SAAwC;AAErE,QAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,YAAY,OAAO;AACpE,UAAI,CAAC,MAAM,OAAO;AAChB,cAAM,IAAI,MAAM,8DAA8D,OAAO,EAAE;AAAA,MACzF;AACA,aAAO;AAAA,QACL,QAAQ,MAAM;AAAA,QACd,OAAO,MAAM;AAAA,QACb,OAAO,MAAM,SAAS,CAAC;AAAA,MACzB;AAAA,IACF;AAGA,UAAM,SAAS,KAAK,oBAAoB,KAAK;AAG7C,eAAW,UAAU,KAAK,cAAc;AACtC,YAAM,SAAS,MAAM,OAAO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AG;AACxD,UAAM,YAAY,QAAQ,OAAO,UAAU,GAAG,EAAE,MAAM,GAAG,QAAQ;AACjE,UAAM,eAAe,UAAU,WAAW,QAAQ,OAAO,EAAE,KAAK;AAChE,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,gBAAgB,SAKtB;AAKA,UAAM,cAGF;AAAA,MACF,eAAe;AAAA,QACb,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,MACA,gBAAgB;AAAA,QACd,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,MACA,eAAe;AAAA,QACb,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,IACF;AAEA,UAAM,YAAY,YAAY,OAAO;AACrC,QAAI,CAAC,WAAW;AACd,YAAM,IAAI,MAAM,2CAA2C,OAAO,EAAE;AAAA,IACtE;AAEA,WAAO;AAAA,EACT;AACF;;;ACxLO,SAAS,uBACd,QACA,SAAkC,CAAC,GACf;AAEpB,MAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAO,SAAS,QAAQ,aAAW;AACjC,aAAO,SAAS,SAAS,IAAI,eAAe,CAAC;AAAA,IAC/C,CAAC;AAAA,EACH,OAAO;AAEL,WAAO,SAAS,YAAY,IAAI,eAAe,CAAC;AAAA,EAClD;AAEA,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact/server/index.ts","../../../../src/exact/server/scheme.ts","../../../../src/exact/server/register.ts"],"sourcesContent":["export { ExactEvmScheme } from \"./scheme\";\nexport { registerExactEvmScheme } from \"./register\";\nexport type { EvmResourceServerConfig } from \"./register\";\n","import {\n AssetAmount,\n Network,\n PaymentRequirements,\n Price,\n SchemeNetworkServer,\n MoneyParser,\n} from \"@x402/core/types\";\n\n/**\n * EVM server implementation for the Exact payment scheme.\n */\nexport class ExactEvmScheme implements SchemeNetworkServer {\n readonly scheme = \"exact\";\n private moneyParsers: MoneyParser[] = [];\n\n /**\n * Register a custom money parser in the parser chain.\n * Multiple parsers can be registered - they will be tried in registration order.\n * Each parser receives a decimal amount (e.g., 1.50 for $1.50).\n * If a parser returns null, the next parser in the chain will be tried.\n * The default parser is always the final fallback.\n *\n * @param parser - Custom function to convert amount to AssetAmount (or null to skip)\n * @returns The server instance for chaining\n *\n * @example\n * evmServer.registerMoneyParser(async (amount, network) => {\n * // Custom conversion logic\n * if (amount > 100) {\n * // Use different token for large amounts\n * return { amount: (amount * 1e18).toString(), asset: \"0xCustomToken\" };\n * }\n * return null; // Use next parser\n * });\n */\n registerMoneyParser(parser: MoneyParser): ExactEvmScheme {\n this.moneyParsers.push(parser);\n return this;\n }\n\n /**\n * Parses a price into an asset amount.\n * If price is already an AssetAmount, returns it directly.\n * If price is Money (string | number), parses to decimal and tries custom parsers.\n * Falls back to default conversion if all custom parsers return null.\n *\n * @param price - The price to parse\n * @param network - The network to use\n * @returns Promise that resolves to the parsed asset amount\n */\n async parsePrice(price: Price, network: Network): Promise<AssetAmount> {\n // If already an AssetAmount, return it directly\n if (typeof price === \"object\" && price !== null && \"amount\" in price) {\n if (!price.asset) {\n throw new Error(`Asset address must be specified for AssetAmount on network ${network}`);\n }\n return {\n amount: price.amount,\n asset: price.asset,\n extra: price.extra || {},\n };\n }\n\n // Parse Money to decimal number\n const amount = this.parseMoneyToDecimal(price);\n\n // Try each custom money parser in order\n for (const parser of this.moneyParsers) {\n const result = await parser(amount, network);\n if (result !== null) {\n return result;\n }\n }\n\n // All custom parsers returned null, use default conversion\n return this.defaultMoneyConversion(amount, network);\n }\n\n /**\n * Build payment requirements for this scheme/network combination\n *\n * @param paymentRequirements - The base payment requirements\n * @param supportedKind - The supported kind from facilitator (unused)\n * @param supportedKind.x402Version - The x402 version\n * @param supportedKind.scheme - The logical payment scheme\n * @param supportedKind.network - The network identifier in CAIP-2 format\n * @param supportedKind.extra - Optional extra metadata regarding scheme/network implementation details\n * @param extensionKeys - Extension keys supported by the facilitator (unused)\n * @returns Payment requirements ready to be sent to clients\n */\n enhancePaymentRequirements(\n paymentRequirements: PaymentRequirements,\n supportedKind: {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n },\n extensionKeys: string[],\n ): Promise<PaymentRequirements> {\n // Mark unused parameters to satisfy linter\n void supportedKind;\n void extensionKeys;\n return Promise.resolve(paymentRequirements);\n }\n\n /**\n * Parse Money (string | number) to a decimal number.\n * Handles formats like \"$1.50\", \"1.50\", 1.50, etc.\n *\n * @param money - The money value to parse\n * @returns Decimal number\n */\n private parseMoneyToDecimal(money: string | number): number {\n if (typeof money === \"number\") {\n return money;\n }\n\n // Remove $ sign and whitespace, then parse\n const cleanMoney = money.replace(/^\\$/, \"\").trim();\n const amount = parseFloat(cleanMoney);\n\n if (isNaN(amount)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n return amount;\n }\n\n /**\n * Default money conversion implementation.\n * Converts decimal amount to the default stablecoin on the specified network.\n *\n * @param amount - The decimal amount (e.g., 1.50)\n * @param network - The network to use\n * @returns The parsed asset amount in the default stablecoin\n */\n private defaultMoneyConversion(amount: number, network: Network): AssetAmount {\n const assetInfo = this.getDefaultAsset(network);\n const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);\n\n // EIP-3009 tokens always need name/version for their transferWithAuthorization domain.\n // Permit2 tokens only need them if the token supports EIP-2612 (for gasless permit signing).\n // Omitting name/version for permit2 tokens signals the client to skip EIP-2612 and use\n // ERC-20 approval gas sponsoring instead.\n const includeEip712Domain = !assetInfo.assetTransferMethod || assetInfo.supportsEip2612;\n\n return {\n amount: tokenAmount,\n asset: assetInfo.address,\n extra: {\n ...(includeEip712Domain && {\n name: assetInfo.name,\n version: assetInfo.version,\n }),\n ...(assetInfo.assetTransferMethod && {\n assetTransferMethod: assetInfo.assetTransferMethod,\n }),\n },\n };\n }\n\n /**\n * Convert decimal amount to token units (e.g., 0.10 -> 100000 for 6-decimal tokens)\n *\n * @param decimalAmount - The decimal amount to convert\n * @param decimals - The number of decimals for the token\n * @returns The token amount as a string\n */\n private convertToTokenAmount(decimalAmount: string, decimals: number): string {\n const amount = parseFloat(decimalAmount);\n if (isNaN(amount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n // Convert to smallest unit (e.g., for USDC with 6 decimals: 0.10 * 10^6 = 100000)\n const [intPart, decPart = \"\"] = String(amount).split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n return tokenAmount;\n }\n\n /**\n * Get the default asset info for a network (typically USDC)\n *\n * @param network - The network to get asset info for\n * @returns The asset information including address, name, version, and decimals\n */\n private getDefaultAsset(network: Network): {\n address: string;\n name: string;\n version: string;\n decimals: number;\n assetTransferMethod?: string;\n supportsEip2612?: boolean;\n } {\n // Map of network to stablecoin info including EIP-712 domain parameters.\n // Each network has the right to determine its own default stablecoin that can be expressed as a USD string by calling servers.\n // Tokens that don't support EIP-3009 should set assetTransferMethod: \"permit2\".\n // For permit2 tokens, set supportsEip2612: true if the token implements EIP-2612 permit().\n // When supportsEip2612 is false/absent on a permit2 token, name/version are omitted from\n // extra so the client skips the EIP-2612 path and falls back to ERC-20 approval gas sponsoring.\n const stablecoins: Record<\n string,\n {\n address: string;\n name: string;\n version: string;\n decimals: number;\n assetTransferMethod?: string;\n supportsEip2612?: boolean;\n }\n > = {\n \"eip155:8453\": {\n address: \"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913\",\n name: \"USD Coin\",\n version: \"2\",\n decimals: 6,\n }, // Base mainnet USDC\n \"eip155:84532\": {\n address: \"0x036CbD53842c5426634e7929541eC2318f3dCF7e\",\n name: \"USDC\",\n version: \"2\",\n decimals: 6,\n }, // Base Sepolia USDC\n \"eip155:4326\": {\n address: \"0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7\",\n name: \"MegaUSD\",\n version: \"1\",\n decimals: 18,\n assetTransferMethod: \"permit2\",\n supportsEip2612: true,\n }, // MegaETH mainnet MegaUSD (no EIP-3009, supports EIP-2612)\n \"eip155:143\": {\n address: \"0x754704Bc059F8C67012fEd69BC8A327a5aafb603\",\n name: \"USD Coin\",\n version: \"2\",\n decimals: 6,\n }, // Monad mainnet USDC\n };\n\n const assetInfo = stablecoins[network];\n if (!assetInfo) {\n throw new Error(`No default asset configured for network ${network}`);\n }\n\n return assetInfo;\n }\n}\n","import { x402ResourceServer } from \"@x402/core/server\";\nimport { Network } from \"@x402/core/types\";\nimport { ExactEvmScheme } from \"./scheme\";\n\n/**\n * Configuration options for registering EVM schemes to an x402ResourceServer\n */\nexport interface EvmResourceServerConfig {\n /**\n * Optional specific networks to register\n * If not provided, registers wildcard support (eip155:*)\n */\n networks?: Network[];\n}\n\n/**\n * Registers EVM exact payment schemes to an x402ResourceServer instance.\n *\n * This function registers:\n * - V2: eip155:* wildcard scheme with ExactEvmScheme (or specific networks if provided)\n *\n * @param server - The x402ResourceServer instance to register schemes to\n * @param config - Configuration for EVM resource server registration\n * @returns The server instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactEvmScheme } from \"@x402/evm/exact/server/register\";\n * import { x402ResourceServer } from \"@x402/core/server\";\n *\n * const server = new x402ResourceServer(facilitatorClient);\n * registerExactEvmScheme(server, {});\n * ```\n */\nexport function registerExactEvmScheme(\n server: x402ResourceServer,\n config: EvmResourceServerConfig = {},\n): x402ResourceServer {\n // Register V2 scheme\n if (config.networks && config.networks.length > 0) {\n // Register specific networks\n config.networks.forEach(network => {\n server.register(network, new ExactEvmScheme());\n });\n } else {\n // Register wildcard for all EVM chains\n server.register(\"eip155:*\", new ExactEvmScheme());\n }\n\n return server;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;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AA;AAAA,MACA,gBAAgB;AAAA,QACd,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,MACA,eAAe;AAAA,QACb,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,QACV,qBAAqB;AAAA,QACrB,iBAAiB;AAAA,MACnB;AAAA;AAAA,MACA,cAAc;AAAA,QACZ,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,IACF;AAEA,UAAM,YAAY,YAAY,OAAO;AACrC,QAAI,CAAC,WAAW;AACd,YAAM,IAAI,MAAM,2CAA2C,OAAO,EAAE;AAAA,IACtE;AAEA,WAAO;AAAA,EACT;AACF;;;ACtNO,SAAS,uBACd,QACA,SAAkC,CAAC,GACf;AAEpB,MAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAO,SAAS,QAAQ,aAAW;AACjC,aAAO,SAAS,SAAS,IAAI,eAAe,CAAC;AAAA,IAC/C,CAAC;AAAA,EACH,OAAO;AAEL,WAAO,SAAS,YAAY,IAAI,eAAe,CAAC;AAAA,EAClD;AAEA,SAAO;AACT;","names":[]}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { E as ExactEvmScheme,
|
|
1
|
+
export { E as ExactEvmScheme, a as PERMIT2_ADDRESS, P as Permit2AllowanceParams, d as authorizationTypes, c as createPermit2ApprovalTx, f as eip3009ABI, e as erc20AllowanceAbi, g as getPermit2AllowanceReadParams, p as permit2WitnessTypes, h as x402ExactPermit2ProxyABI, x as x402ExactPermit2ProxyAddress, b as x402UptoPermit2ProxyAddress } from './permit2-DHAq6FTe.js';
|
|
2
2
|
export { C as ClientEvmSigner, F as FacilitatorEvmSigner, t as toClientEvmSigner, a as toFacilitatorEvmSigner } from './signer-DC81R8wQ.js';
|
|
3
3
|
import '@x402/core/types';
|
|
4
4
|
|
|
@@ -514,4 +514,4 @@ declare function getPermit2AllowanceReadParams(params: Permit2AllowanceParams):
|
|
|
514
514
|
args: [`0x${string}`, `0x${string}`];
|
|
515
515
|
};
|
|
516
516
|
|
|
517
|
-
export { ExactEvmScheme as E,
|
|
517
|
+
export { ExactEvmScheme as E, type Permit2AllowanceParams as P, PERMIT2_ADDRESS as a, x402UptoPermit2ProxyAddress as b, createPermit2ApprovalTx as c, authorizationTypes as d, erc20AllowanceAbi as e, eip3009ABI as f, getPermit2AllowanceReadParams as g, x402ExactPermit2ProxyABI as h, permit2WitnessTypes as p, x402ExactPermit2ProxyAddress as x };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { E as ExactEvmScheme,
|
|
2
|
-
import { SelectPaymentRequirements, PaymentPolicy
|
|
1
|
+
export { E as ExactEvmScheme, P as Permit2AllowanceParams, c as createPermit2ApprovalTx, e as erc20AllowanceAbi, g as getPermit2AllowanceReadParams } from '../../permit2-BuAhWvNC.mjs';
|
|
2
|
+
import { x402Client, SelectPaymentRequirements, PaymentPolicy } from '@x402/core/client';
|
|
3
3
|
import { Network } from '@x402/core/types';
|
|
4
4
|
import { C as ClientEvmSigner } from '../../signer-DC81R8wQ.mjs';
|
|
5
5
|
|
|
@@ -104,12 +104,18 @@ var ExactEvmScheme = class {
|
|
|
104
104
|
defaultMoneyConversion(amount, network) {
|
|
105
105
|
const assetInfo = this.getDefaultAsset(network);
|
|
106
106
|
const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);
|
|
107
|
+
const includeEip712Domain = !assetInfo.assetTransferMethod || assetInfo.supportsEip2612;
|
|
107
108
|
return {
|
|
108
109
|
amount: tokenAmount,
|
|
109
110
|
asset: assetInfo.address,
|
|
110
111
|
extra: {
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
...includeEip712Domain && {
|
|
113
|
+
name: assetInfo.name,
|
|
114
|
+
version: assetInfo.version
|
|
115
|
+
},
|
|
116
|
+
...assetInfo.assetTransferMethod && {
|
|
117
|
+
assetTransferMethod: assetInfo.assetTransferMethod
|
|
118
|
+
}
|
|
113
119
|
}
|
|
114
120
|
};
|
|
115
121
|
}
|
|
@@ -156,9 +162,18 @@ var ExactEvmScheme = class {
|
|
|
156
162
|
address: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
157
163
|
name: "MegaUSD",
|
|
158
164
|
version: "1",
|
|
159
|
-
decimals: 18
|
|
165
|
+
decimals: 18,
|
|
166
|
+
assetTransferMethod: "permit2",
|
|
167
|
+
supportsEip2612: true
|
|
168
|
+
},
|
|
169
|
+
// MegaETH mainnet MegaUSD (no EIP-3009, supports EIP-2612)
|
|
170
|
+
"eip155:143": {
|
|
171
|
+
address: "0x754704Bc059F8C67012fEd69BC8A327a5aafb603",
|
|
172
|
+
name: "USD Coin",
|
|
173
|
+
version: "2",
|
|
174
|
+
decimals: 6
|
|
160
175
|
}
|
|
161
|
-
//
|
|
176
|
+
// Monad mainnet USDC
|
|
162
177
|
};
|
|
163
178
|
const assetInfo = stablecoins[network];
|
|
164
179
|
if (!assetInfo) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/exact/server/scheme.ts","../../../../src/exact/server/register.ts"],"sourcesContent":["import {\n AssetAmount,\n Network,\n PaymentRequirements,\n Price,\n SchemeNetworkServer,\n MoneyParser,\n} from \"@x402/core/types\";\n\n/**\n * EVM server implementation for the Exact payment scheme.\n */\nexport class ExactEvmScheme implements SchemeNetworkServer {\n readonly scheme = \"exact\";\n private moneyParsers: MoneyParser[] = [];\n\n /**\n * Register a custom money parser in the parser chain.\n * Multiple parsers can be registered - they will be tried in registration order.\n * Each parser receives a decimal amount (e.g., 1.50 for $1.50).\n * If a parser returns null, the next parser in the chain will be tried.\n * The default parser is always the final fallback.\n *\n * @param parser - Custom function to convert amount to AssetAmount (or null to skip)\n * @returns The server instance for chaining\n *\n * @example\n * evmServer.registerMoneyParser(async (amount, network) => {\n * // Custom conversion logic\n * if (amount > 100) {\n * // Use different token for large amounts\n * return { amount: (amount * 1e18).toString(), asset: \"0xCustomToken\" };\n * }\n * return null; // Use next parser\n * });\n */\n registerMoneyParser(parser: MoneyParser): ExactEvmScheme {\n this.moneyParsers.push(parser);\n return this;\n }\n\n /**\n * Parses a price into an asset amount.\n * If price is already an AssetAmount, returns it directly.\n * If price is Money (string | number), parses to decimal and tries custom parsers.\n * Falls back to default conversion if all custom parsers return null.\n *\n * @param price - The price to parse\n * @param network - The network to use\n * @returns Promise that resolves to the parsed asset amount\n */\n async parsePrice(price: Price, network: Network): Promise<AssetAmount> {\n // If already an AssetAmount, return it directly\n if (typeof price === \"object\" && price !== null && \"amount\" in price) {\n if (!price.asset) {\n throw new Error(`Asset address must be specified for AssetAmount on network ${network}`);\n }\n return {\n amount: price.amount,\n asset: price.asset,\n extra: price.extra || {},\n };\n }\n\n // Parse Money to decimal number\n const amount = this.parseMoneyToDecimal(price);\n\n // Try each custom money parser in order\n for (const parser of this.moneyParsers) {\n const result = await parser(amount, network);\n if (result !== null) {\n return result;\n }\n }\n\n // All custom parsers returned null, use default conversion\n return this.defaultMoneyConversion(amount, network);\n }\n\n /**\n * Build payment requirements for this scheme/network combination\n *\n * @param paymentRequirements - The base payment requirements\n * @param supportedKind - The supported kind from facilitator (unused)\n * @param supportedKind.x402Version - The x402 version\n * @param supportedKind.scheme - The logical payment scheme\n * @param supportedKind.network - The network identifier in CAIP-2 format\n * @param supportedKind.extra - Optional extra metadata regarding scheme/network implementation details\n * @param extensionKeys - Extension keys supported by the facilitator (unused)\n * @returns Payment requirements ready to be sent to clients\n */\n enhancePaymentRequirements(\n paymentRequirements: PaymentRequirements,\n supportedKind: {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n },\n extensionKeys: string[],\n ): Promise<PaymentRequirements> {\n // Mark unused parameters to satisfy linter\n void supportedKind;\n void extensionKeys;\n return Promise.resolve(paymentRequirements);\n }\n\n /**\n * Parse Money (string | number) to a decimal number.\n * Handles formats like \"$1.50\", \"1.50\", 1.50, etc.\n *\n * @param money - The money value to parse\n * @returns Decimal number\n */\n private parseMoneyToDecimal(money: string | number): number {\n if (typeof money === \"number\") {\n return money;\n }\n\n // Remove $ sign and whitespace, then parse\n const cleanMoney = money.replace(/^\\$/, \"\").trim();\n const amount = parseFloat(cleanMoney);\n\n if (isNaN(amount)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n return amount;\n }\n\n /**\n * Default money conversion implementation.\n * Converts decimal amount to the default stablecoin on the specified network.\n *\n * @param amount - The decimal amount (e.g., 1.50)\n * @param network - The network to use\n * @returns The parsed asset amount in the default stablecoin\n */\n private defaultMoneyConversion(amount: number, network: Network): AssetAmount {\n const assetInfo = this.getDefaultAsset(network);\n const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);\n\n return {\n amount: tokenAmount,\n asset: assetInfo.address,\n extra: {\n name: assetInfo.name,\n version: assetInfo.version,\n },\n };\n }\n\n /**\n * Convert decimal amount to token units (e.g., 0.10 -> 100000 for 6-decimal tokens)\n *\n * @param decimalAmount - The decimal amount to convert\n * @param decimals - The number of decimals for the token\n * @returns The token amount as a string\n */\n private convertToTokenAmount(decimalAmount: string, decimals: number): string {\n const amount = parseFloat(decimalAmount);\n if (isNaN(amount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n // Convert to smallest unit (e.g., for USDC with 6 decimals: 0.10 * 10^6 = 100000)\n const [intPart, decPart = \"\"] = String(amount).split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n return tokenAmount;\n }\n\n /**\n * Get the default asset info for a network (typically USDC)\n *\n * @param network - The network to get asset info for\n * @returns The asset information including address, name, version, and decimals\n */\n private getDefaultAsset(network: Network): {\n address: string;\n name: string;\n version: string;\n decimals: number;\n } {\n // Map of network to USDC info including EIP-712 domain parameters\n // Each network has the right to determine its own default stablecoin that can be expressed as a USD string by calling servers\n // NOTE: Currently only EIP-3009 supporting stablecoins can be used with this scheme\n // Generic ERC20 support via EIP-2612/permit2 is planned, but not yet implemented.\n const stablecoins: Record<\n string,\n { address: string; name: string; version: string; decimals: number }\n > = {\n \"eip155:8453\": {\n address: \"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913\",\n name: \"USD Coin\",\n version: \"2\",\n decimals: 6,\n }, // Base mainnet USDC\n \"eip155:84532\": {\n address: \"0x036CbD53842c5426634e7929541eC2318f3dCF7e\",\n name: \"USDC\",\n version: \"2\",\n decimals: 6,\n }, // Base Sepolia USDC\n \"eip155:4326\": {\n address: \"0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7\",\n name: \"MegaUSD\",\n version: \"1\",\n decimals: 18,\n }, // MegaETH mainnet USDM\n };\n\n const assetInfo = stablecoins[network];\n if (!assetInfo) {\n throw new Error(`No default asset configured for network ${network}`);\n }\n\n return assetInfo;\n }\n}\n","import { x402ResourceServer } from \"@x402/core/server\";\nimport { Network } from \"@x402/core/types\";\nimport { ExactEvmScheme } from \"./scheme\";\n\n/**\n * Configuration options for registering EVM schemes to an x402ResourceServer\n */\nexport interface EvmResourceServerConfig {\n /**\n * Optional specific networks to register\n * If not provided, registers wildcard support (eip155:*)\n */\n networks?: Network[];\n}\n\n/**\n * Registers EVM exact payment schemes to an x402ResourceServer instance.\n *\n * This function registers:\n * - V2: eip155:* wildcard scheme with ExactEvmScheme (or specific networks if provided)\n *\n * @param server - The x402ResourceServer instance to register schemes to\n * @param config - Configuration for EVM resource server registration\n * @returns The server instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactEvmScheme } from \"@x402/evm/exact/server/register\";\n * import { x402ResourceServer } from \"@x402/core/server\";\n *\n * const server = new x402ResourceServer(facilitatorClient);\n * registerExactEvmScheme(server, {});\n * ```\n */\nexport function registerExactEvmScheme(\n server: x402ResourceServer,\n config: EvmResourceServerConfig = {},\n): x402ResourceServer {\n // Register V2 scheme\n if (config.networks && config.networks.length > 0) {\n // Register specific networks\n config.networks.forEach(network => {\n server.register(network, new ExactEvmScheme());\n });\n } else {\n // Register wildcard for all EVM chains\n server.register(\"eip155:*\", new ExactEvmScheme());\n }\n\n return server;\n}\n"],"mappings":";AAYO,IAAM,iBAAN,MAAoD;AAAA,EAApD;AACL,SAAS,SAAS;AAClB,SAAQ,eAA8B,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBvC,oBAAoB,QAAqC;AACvD,SAAK,aAAa,KAAK,MAAM;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,MAAM,WAAW,OAAc,SAAwC;AAErE,QAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,YAAY,OAAO;AACpE,UAAI,CAAC,MAAM,OAAO;AAChB,cAAM,IAAI,MAAM,8DAA8D,OAAO,EAAE;AAAA,MACzF;AACA,aAAO;AAAA,QACL,QAAQ,MAAM;AAAA,QACd,OAAO,MAAM;AAAA,QACb,OAAO,MAAM,SAAS,CAAC;AAAA,MACzB;AAAA,IACF;AAGA,UAAM,SAAS,KAAK,oBAAoB,KAAK;AAG7C,eAAW,UAAU,KAAK,cAAc;AACtC,YAAM,SAAS,MAAM,OAAO,QAAQ,OAAO;AAC3C,UAAI,WAAW,MAAM;AACnB,eAAO;AAAA,MACT;AAAA,IACF;AAGA,WAAO,KAAK,uBAAuB,QAAQ,OAAO;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,2BACE,qBACA,eAMA,eAC8B;AAE9B,SAAK;AACL,SAAK;AACL,WAAO,QAAQ,QAAQ,mBAAmB;AAAA,EAC5C;AAAA;A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AAU;AAAA,MACZ;AAAA;AAAA,MACA,gBAAgB;AAAA,QACd,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,MACA,eAAe;AAAA,QACb,SAAS;AAAA,QACT,MAAM;AAAA,QACN,SAAS;AAAA,QACT,UAAU;AAAA,MACZ;AAAA;AAAA,IACF;AAEA,UAAM,YAAY,YAAY,OAAO;AACrC,QAAI,CAAC,WAAW;AACd,YAAM,IAAI,MAAM,2CAA2C,OAAO,EAAE;AAAA,IACtE;AAEA,WAAO;AAAA,EACT;AACF;;;ACxLO,SAAS,uBACd,QACA,SAAkC,CAAC,GACf;AAEpB,MAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAO,SAAS,QAAQ,aAAW;AACjC,aAAO,SAAS,SAAS,IAAI,eAAe,CAAC;AAAA,IAC/C,CAAC;AAAA,EACH,OAAO;AAEL,WAAO,SAAS,YAAY,IAAI,eAAe,CAAC;AAAA,EAClD;AAEA,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact/server/scheme.ts","../../../../src/exact/server/register.ts"],"sourcesContent":["import {\n AssetAmount,\n Network,\n PaymentRequirements,\n Price,\n SchemeNetworkServer,\n MoneyParser,\n} from \"@x402/core/types\";\n\n/**\n * EVM server implementation for the Exact payment scheme.\n */\nexport class ExactEvmScheme implements SchemeNetworkServer {\n readonly scheme = \"exact\";\n private moneyParsers: MoneyParser[] = [];\n\n /**\n * Register a custom money parser in the parser chain.\n * Multiple parsers can be registered - they will be tried in registration order.\n * Each parser receives a decimal amount (e.g., 1.50 for $1.50).\n * If a parser returns null, the next parser in the chain will be tried.\n * The default parser is always the final fallback.\n *\n * @param parser - Custom function to convert amount to AssetAmount (or null to skip)\n * @returns The server instance for chaining\n *\n * @example\n * evmServer.registerMoneyParser(async (amount, network) => {\n * // Custom conversion logic\n * if (amount > 100) {\n * // Use different token for large amounts\n * return { amount: (amount * 1e18).toString(), asset: \"0xCustomToken\" };\n * }\n * return null; // Use next parser\n * });\n */\n registerMoneyParser(parser: MoneyParser): ExactEvmScheme {\n this.moneyParsers.push(parser);\n return this;\n }\n\n /**\n * Parses a price into an asset amount.\n * If price is already an AssetAmount, returns it directly.\n * If price is Money (string | number), parses to decimal and tries custom parsers.\n * Falls back to default conversion if all custom parsers return null.\n *\n * @param price - The price to parse\n * @param network - The network to use\n * @returns Promise that resolves to the parsed asset amount\n */\n async parsePrice(price: Price, network: Network): Promise<AssetAmount> {\n // If already an AssetAmount, return it directly\n if (typeof price === \"object\" && price !== null && \"amount\" in price) {\n if (!price.asset) {\n throw new Error(`Asset address must be specified for AssetAmount on network ${network}`);\n }\n return {\n amount: price.amount,\n asset: price.asset,\n extra: price.extra || {},\n };\n }\n\n // Parse Money to decimal number\n const amount = this.parseMoneyToDecimal(price);\n\n // Try each custom money parser in order\n for (const parser of this.moneyParsers) {\n const result = await parser(amount, network);\n if (result !== null) {\n return result;\n }\n }\n\n // All custom parsers returned null, use default conversion\n return this.defaultMoneyConversion(amount, network);\n }\n\n /**\n * Build payment requirements for this scheme/network combination\n *\n * @param paymentRequirements - The base payment requirements\n * @param supportedKind - The supported kind from facilitator (unused)\n * @param supportedKind.x402Version - The x402 version\n * @param supportedKind.scheme - The logical payment scheme\n * @param supportedKind.network - The network identifier in CAIP-2 format\n * @param supportedKind.extra - Optional extra metadata regarding scheme/network implementation details\n * @param extensionKeys - Extension keys supported by the facilitator (unused)\n * @returns Payment requirements ready to be sent to clients\n */\n enhancePaymentRequirements(\n paymentRequirements: PaymentRequirements,\n supportedKind: {\n x402Version: number;\n scheme: string;\n network: Network;\n extra?: Record<string, unknown>;\n },\n extensionKeys: string[],\n ): Promise<PaymentRequirements> {\n // Mark unused parameters to satisfy linter\n void supportedKind;\n void extensionKeys;\n return Promise.resolve(paymentRequirements);\n }\n\n /**\n * Parse Money (string | number) to a decimal number.\n * Handles formats like \"$1.50\", \"1.50\", 1.50, etc.\n *\n * @param money - The money value to parse\n * @returns Decimal number\n */\n private parseMoneyToDecimal(money: string | number): number {\n if (typeof money === \"number\") {\n return money;\n }\n\n // Remove $ sign and whitespace, then parse\n const cleanMoney = money.replace(/^\\$/, \"\").trim();\n const amount = parseFloat(cleanMoney);\n\n if (isNaN(amount)) {\n throw new Error(`Invalid money format: ${money}`);\n }\n\n return amount;\n }\n\n /**\n * Default money conversion implementation.\n * Converts decimal amount to the default stablecoin on the specified network.\n *\n * @param amount - The decimal amount (e.g., 1.50)\n * @param network - The network to use\n * @returns The parsed asset amount in the default stablecoin\n */\n private defaultMoneyConversion(amount: number, network: Network): AssetAmount {\n const assetInfo = this.getDefaultAsset(network);\n const tokenAmount = this.convertToTokenAmount(amount.toString(), assetInfo.decimals);\n\n // EIP-3009 tokens always need name/version for their transferWithAuthorization domain.\n // Permit2 tokens only need them if the token supports EIP-2612 (for gasless permit signing).\n // Omitting name/version for permit2 tokens signals the client to skip EIP-2612 and use\n // ERC-20 approval gas sponsoring instead.\n const includeEip712Domain = !assetInfo.assetTransferMethod || assetInfo.supportsEip2612;\n\n return {\n amount: tokenAmount,\n asset: assetInfo.address,\n extra: {\n ...(includeEip712Domain && {\n name: assetInfo.name,\n version: assetInfo.version,\n }),\n ...(assetInfo.assetTransferMethod && {\n assetTransferMethod: assetInfo.assetTransferMethod,\n }),\n },\n };\n }\n\n /**\n * Convert decimal amount to token units (e.g., 0.10 -> 100000 for 6-decimal tokens)\n *\n * @param decimalAmount - The decimal amount to convert\n * @param decimals - The number of decimals for the token\n * @returns The token amount as a string\n */\n private convertToTokenAmount(decimalAmount: string, decimals: number): string {\n const amount = parseFloat(decimalAmount);\n if (isNaN(amount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n // Convert to smallest unit (e.g., for USDC with 6 decimals: 0.10 * 10^6 = 100000)\n const [intPart, decPart = \"\"] = String(amount).split(\".\");\n const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n const tokenAmount = (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n return tokenAmount;\n }\n\n /**\n * Get the default asset info for a network (typically USDC)\n *\n * @param network - The network to get asset info for\n * @returns The asset information including address, name, version, and decimals\n */\n private getDefaultAsset(network: Network): {\n address: string;\n name: string;\n version: string;\n decimals: number;\n assetTransferMethod?: string;\n supportsEip2612?: boolean;\n } {\n // Map of network to stablecoin info including EIP-712 domain parameters.\n // Each network has the right to determine its own default stablecoin that can be expressed as a USD string by calling servers.\n // Tokens that don't support EIP-3009 should set assetTransferMethod: \"permit2\".\n // For permit2 tokens, set supportsEip2612: true if the token implements EIP-2612 permit().\n // When supportsEip2612 is false/absent on a permit2 token, name/version are omitted from\n // extra so the client skips the EIP-2612 path and falls back to ERC-20 approval gas sponsoring.\n const stablecoins: Record<\n string,\n {\n address: string;\n name: string;\n version: string;\n decimals: number;\n assetTransferMethod?: string;\n supportsEip2612?: boolean;\n }\n > = {\n \"eip155:8453\": {\n address: \"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913\",\n name: \"USD Coin\",\n version: \"2\",\n decimals: 6,\n }, // Base mainnet USDC\n \"eip155:84532\": {\n address: \"0x036CbD53842c5426634e7929541eC2318f3dCF7e\",\n name: \"USDC\",\n version: \"2\",\n decimals: 6,\n }, // Base Sepolia USDC\n \"eip155:4326\": {\n address: \"0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7\",\n name: \"MegaUSD\",\n version: \"1\",\n decimals: 18,\n assetTransferMethod: \"permit2\",\n supportsEip2612: true,\n }, // MegaETH mainnet MegaUSD (no EIP-3009, supports EIP-2612)\n \"eip155:143\": {\n address: \"0x754704Bc059F8C67012fEd69BC8A327a5aafb603\",\n name: \"USD Coin\",\n version: \"2\",\n decimals: 6,\n }, // Monad mainnet USDC\n };\n\n const assetInfo = stablecoins[network];\n if (!assetInfo) {\n throw new Error(`No default asset configured for network ${network}`);\n }\n\n return assetInfo;\n }\n}\n","import { x402ResourceServer } from \"@x402/core/server\";\nimport { Network } from \"@x402/core/types\";\nimport { ExactEvmScheme } from \"./scheme\";\n\n/**\n * Configuration options for registering EVM schemes to an x402ResourceServer\n */\nexport interface EvmResourceServerConfig {\n /**\n * Optional specific networks to register\n * If not provided, registers wildcard support (eip155:*)\n */\n networks?: Network[];\n}\n\n/**\n * Registers EVM exact payment schemes to an x402ResourceServer instance.\n *\n * This function registers:\n * - V2: eip155:* wildcard scheme with ExactEvmScheme (or specific networks if provided)\n *\n * @param server - The x402ResourceServer instance to register schemes to\n * @param config - Configuration for EVM resource server registration\n * @returns The server instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactEvmScheme } from \"@x402/evm/exact/server/register\";\n * import { x402ResourceServer } from \"@x402/core/server\";\n *\n * const server = new x402ResourceServer(facilitatorClient);\n * registerExactEvmScheme(server, {});\n * ```\n */\nexport function registerExactEvmScheme(\n server: x402ResourceServer,\n config: EvmResourceServerConfig = {},\n): x402ResourceServer {\n // Register V2 scheme\n if (config.networks && config.networks.length > 0) {\n // Register specific networks\n config.networks.forEach(network => {\n server.register(network, new ExactEvmScheme());\n });\n } else {\n // Register wildcard for all EVM chains\n server.register(\"eip155:*\", new ExactEvmScheme());\n }\n\n return server;\n}\n"],"mappings":";AAYO,IAAM,iBAAN,MAAoD;AAAA,EAApD;AACL,SAAS,SAAS;AAClB,SAAQ,eAA8B,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBvC,oBAAoB,QAAqC;AACvD,SAAK,aAAa,KAAK,MAAM;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,MAAM,WAAW,OAAc,SAAwC;AAErE,QAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,YAAY,OAAO;AACpE,UAAI,CAAC,MAAM,OAAO;AAChB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WAAO;AAAA,EACT;AACF;;;ACtNO,SAAS,uBACd,QACA,SAAkC,CAAC,GACf;AAEpB,MAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAO,SAAS,QAAQ,aAAW;AACjC,aAAO,SAAS,SAAS,IAAI,eAAe,CAAC;AAAA,IAC/C,CAAC;AAAA,EACH,OAAO;AAEL,WAAO,SAAS,YAAY,IAAI,eAAe,CAAC;AAAA,EAClD;AAEA,SAAO;AACT;","names":[]}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { E as ExactEvmScheme,
|
|
1
|
+
export { E as ExactEvmScheme, a as PERMIT2_ADDRESS, P as Permit2AllowanceParams, d as authorizationTypes, c as createPermit2ApprovalTx, f as eip3009ABI, e as erc20AllowanceAbi, g as getPermit2AllowanceReadParams, p as permit2WitnessTypes, h as x402ExactPermit2ProxyABI, x as x402ExactPermit2ProxyAddress, b as x402UptoPermit2ProxyAddress } from './permit2-BuAhWvNC.mjs';
|
|
2
2
|
export { C as ClientEvmSigner, F as FacilitatorEvmSigner, t as toClientEvmSigner, a as toFacilitatorEvmSigner } from './signer-DC81R8wQ.mjs';
|
|
3
3
|
import '@x402/core/types';
|
|
4
4
|
|
|
@@ -514,4 +514,4 @@ declare function getPermit2AllowanceReadParams(params: Permit2AllowanceParams):
|
|
|
514
514
|
args: [`0x${string}`, `0x${string}`];
|
|
515
515
|
};
|
|
516
516
|
|
|
517
|
-
export { ExactEvmScheme as E,
|
|
517
|
+
export { ExactEvmScheme as E, type Permit2AllowanceParams as P, PERMIT2_ADDRESS as a, x402UptoPermit2ProxyAddress as b, createPermit2ApprovalTx as c, authorizationTypes as d, erc20AllowanceAbi as e, eip3009ABI as f, getPermit2AllowanceReadParams as g, x402ExactPermit2ProxyABI as h, permit2WitnessTypes as p, x402ExactPermit2ProxyAddress as x };
|
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payai/x402-evm",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"main": "./dist/cjs/index.js",
|
|
5
5
|
"module": "./dist/esm/index.js",
|
|
6
6
|
"types": "./dist/cjs/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build": "node ./scripts/build.js"
|
|
9
|
-
},
|
|
10
7
|
"keywords": [
|
|
11
8
|
"x402",
|
|
12
9
|
"payment",
|
|
@@ -103,5 +100,8 @@
|
|
|
103
100
|
],
|
|
104
101
|
"publishConfig": {
|
|
105
102
|
"access": "public"
|
|
103
|
+
},
|
|
104
|
+
"scripts": {
|
|
105
|
+
"build": "node ./scripts/build.js"
|
|
106
106
|
}
|
|
107
|
-
}
|
|
107
|
+
}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { SchemeNetworkClient, PaymentRequirements, PaymentPayloadResult } from '@x402/core/types';
|
|
2
|
-
import { C as ClientEvmSigner } from './signer-5OVDxViv.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* EVM client implementation for the Exact payment scheme.
|
|
6
|
-
* Supports both EIP-3009 (transferWithAuthorization) and Permit2 flows.
|
|
7
|
-
*
|
|
8
|
-
* Routes to the appropriate authorization method based on
|
|
9
|
-
* `requirements.extra.assetTransferMethod`. Defaults to EIP-3009
|
|
10
|
-
* for backward compatibility with older facilitators.
|
|
11
|
-
*/
|
|
12
|
-
declare class ExactEvmScheme implements SchemeNetworkClient {
|
|
13
|
-
private readonly signer;
|
|
14
|
-
readonly scheme = "exact";
|
|
15
|
-
/**
|
|
16
|
-
* Creates a new ExactEvmClient instance.
|
|
17
|
-
*
|
|
18
|
-
* @param signer - The EVM signer for client operations
|
|
19
|
-
*/
|
|
20
|
-
constructor(signer: ClientEvmSigner);
|
|
21
|
-
/**
|
|
22
|
-
* Creates a payment payload for the Exact scheme.
|
|
23
|
-
* Routes to EIP-3009 or Permit2 based on requirements.extra.assetTransferMethod.
|
|
24
|
-
*
|
|
25
|
-
* @param x402Version - The x402 protocol version
|
|
26
|
-
* @param paymentRequirements - The payment requirements
|
|
27
|
-
* @returns Promise resolving to a payment payload result
|
|
28
|
-
*/
|
|
29
|
-
createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<PaymentPayloadResult>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* ERC20 allowance ABI for checking approval status.
|
|
34
|
-
*/
|
|
35
|
-
declare const erc20AllowanceAbi: readonly [{
|
|
36
|
-
readonly type: "function";
|
|
37
|
-
readonly name: "allowance";
|
|
38
|
-
readonly inputs: readonly [{
|
|
39
|
-
readonly name: "owner";
|
|
40
|
-
readonly type: "address";
|
|
41
|
-
}, {
|
|
42
|
-
readonly name: "spender";
|
|
43
|
-
readonly type: "address";
|
|
44
|
-
}];
|
|
45
|
-
readonly outputs: readonly [{
|
|
46
|
-
readonly type: "uint256";
|
|
47
|
-
}];
|
|
48
|
-
readonly stateMutability: "view";
|
|
49
|
-
}];
|
|
50
|
-
/**
|
|
51
|
-
* Creates transaction data to approve Permit2 to spend tokens.
|
|
52
|
-
* The user sends this transaction (paying gas) before using Permit2 flow.
|
|
53
|
-
*
|
|
54
|
-
* @param tokenAddress - The ERC20 token contract address
|
|
55
|
-
* @returns Transaction data to send for approval
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* const tx = createPermit2ApprovalTx("0x...");
|
|
60
|
-
* await walletClient.sendTransaction({
|
|
61
|
-
* to: tx.to,
|
|
62
|
-
* data: tx.data,
|
|
63
|
-
* });
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
declare function createPermit2ApprovalTx(tokenAddress: `0x${string}`): {
|
|
67
|
-
to: `0x${string}`;
|
|
68
|
-
data: `0x${string}`;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Parameters for checking Permit2 allowance.
|
|
72
|
-
* Application provides these to check if approval is needed.
|
|
73
|
-
*/
|
|
74
|
-
interface Permit2AllowanceParams {
|
|
75
|
-
tokenAddress: `0x${string}`;
|
|
76
|
-
ownerAddress: `0x${string}`;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Returns contract read parameters for checking Permit2 allowance.
|
|
80
|
-
* Use with a public client to check if the user has approved Permit2.
|
|
81
|
-
*
|
|
82
|
-
* @param params - The allowance check parameters
|
|
83
|
-
* @returns Contract read parameters for checking allowance
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```typescript
|
|
87
|
-
* const readParams = getPermit2AllowanceReadParams({
|
|
88
|
-
* tokenAddress: "0x...",
|
|
89
|
-
* ownerAddress: "0x...",
|
|
90
|
-
* });
|
|
91
|
-
*
|
|
92
|
-
* const allowance = await publicClient.readContract(readParams);
|
|
93
|
-
* const needsApproval = allowance < requiredAmount;
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare function getPermit2AllowanceReadParams(params: Permit2AllowanceParams): {
|
|
97
|
-
address: `0x${string}`;
|
|
98
|
-
abi: typeof erc20AllowanceAbi;
|
|
99
|
-
functionName: "allowance";
|
|
100
|
-
args: [`0x${string}`, `0x${string}`];
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export { ExactEvmScheme as E, type Permit2AllowanceParams as P, createPermit2ApprovalTx as c, erc20AllowanceAbi as e, getPermit2AllowanceReadParams as g };
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ClientEvmSigner - Used by x402 clients to sign payment authorizations
|
|
3
|
-
* This is typically a LocalAccount or wallet that holds private keys
|
|
4
|
-
* and can sign EIP-712 typed data for payment authorizations
|
|
5
|
-
*/
|
|
6
|
-
type ClientEvmSigner = {
|
|
7
|
-
readonly address: `0x${string}`;
|
|
8
|
-
signTypedData(message: {
|
|
9
|
-
domain: Record<string, unknown>;
|
|
10
|
-
types: Record<string, unknown>;
|
|
11
|
-
primaryType: string;
|
|
12
|
-
message: Record<string, unknown>;
|
|
13
|
-
}): Promise<`0x${string}`>;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* FacilitatorEvmSigner - Used by x402 facilitators to verify and settle payments
|
|
17
|
-
* This is typically a viem PublicClient + WalletClient combination that can
|
|
18
|
-
* read contract state, verify signatures, write transactions, and wait for receipts
|
|
19
|
-
*
|
|
20
|
-
* Supports multiple addresses for load balancing, key rotation, and high availability
|
|
21
|
-
*/
|
|
22
|
-
type FacilitatorEvmSigner = {
|
|
23
|
-
/**
|
|
24
|
-
* Get all addresses this facilitator can use for signing
|
|
25
|
-
* Enables dynamic address selection for load balancing and key rotation
|
|
26
|
-
*/
|
|
27
|
-
getAddresses(): readonly `0x${string}`[];
|
|
28
|
-
readContract(args: {
|
|
29
|
-
address: `0x${string}`;
|
|
30
|
-
abi: readonly unknown[];
|
|
31
|
-
functionName: string;
|
|
32
|
-
args?: readonly unknown[];
|
|
33
|
-
}): Promise<unknown>;
|
|
34
|
-
verifyTypedData(args: {
|
|
35
|
-
address: `0x${string}`;
|
|
36
|
-
domain: Record<string, unknown>;
|
|
37
|
-
types: Record<string, unknown>;
|
|
38
|
-
primaryType: string;
|
|
39
|
-
message: Record<string, unknown>;
|
|
40
|
-
signature: `0x${string}`;
|
|
41
|
-
}): Promise<boolean>;
|
|
42
|
-
writeContract(args: {
|
|
43
|
-
address: `0x${string}`;
|
|
44
|
-
abi: readonly unknown[];
|
|
45
|
-
functionName: string;
|
|
46
|
-
args: readonly unknown[];
|
|
47
|
-
}): Promise<`0x${string}`>;
|
|
48
|
-
sendTransaction(args: {
|
|
49
|
-
to: `0x${string}`;
|
|
50
|
-
data: `0x${string}`;
|
|
51
|
-
}): Promise<`0x${string}`>;
|
|
52
|
-
waitForTransactionReceipt(args: {
|
|
53
|
-
hash: `0x${string}`;
|
|
54
|
-
}): Promise<{
|
|
55
|
-
status: string;
|
|
56
|
-
}>;
|
|
57
|
-
getCode(args: {
|
|
58
|
-
address: `0x${string}`;
|
|
59
|
-
}): Promise<`0x${string}` | undefined>;
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* Converts a signer to a ClientEvmSigner
|
|
63
|
-
*
|
|
64
|
-
* @param signer - The signer to convert to a ClientEvmSigner
|
|
65
|
-
* @returns The converted signer
|
|
66
|
-
*/
|
|
67
|
-
declare function toClientEvmSigner(signer: ClientEvmSigner): ClientEvmSigner;
|
|
68
|
-
/**
|
|
69
|
-
* Converts a viem client with single address to a FacilitatorEvmSigner
|
|
70
|
-
* Wraps the single address in a getAddresses() function for compatibility
|
|
71
|
-
*
|
|
72
|
-
* @param client - The client to convert (must have 'address' property)
|
|
73
|
-
* @returns FacilitatorEvmSigner with getAddresses() support
|
|
74
|
-
*/
|
|
75
|
-
declare function toFacilitatorEvmSigner(client: Omit<FacilitatorEvmSigner, "getAddresses"> & {
|
|
76
|
-
address: `0x${string}`;
|
|
77
|
-
}): FacilitatorEvmSigner;
|
|
78
|
-
|
|
79
|
-
export { type ClientEvmSigner as C, type FacilitatorEvmSigner as F, toFacilitatorEvmSigner as a, toClientEvmSigner as t };
|