x402-evm-mantle 2.1.1-mantle
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/README.md +183 -0
- package/dist/cjs/exact/client/index.d.ts +53 -0
- package/dist/cjs/exact/client/index.js +270 -0
- package/dist/cjs/exact/client/index.js.map +1 -0
- package/dist/cjs/exact/facilitator/index.d.ts +118 -0
- package/dist/cjs/exact/facilitator/index.js +735 -0
- package/dist/cjs/exact/facilitator/index.js.map +1 -0
- package/dist/cjs/exact/server/index.d.ts +140 -0
- package/dist/cjs/exact/server/index.js +247 -0
- package/dist/cjs/exact/server/index.js.map +1 -0
- package/dist/cjs/exact/v1/client/index.d.ts +37 -0
- package/dist/cjs/exact/v1/client/index.js +147 -0
- package/dist/cjs/exact/v1/client/index.js.map +1 -0
- package/dist/cjs/exact/v1/facilitator/index.d.ts +62 -0
- package/dist/cjs/exact/v1/facilitator/index.js +401 -0
- package/dist/cjs/exact/v1/facilitator/index.js.map +1 -0
- package/dist/cjs/index.d.ts +36 -0
- package/dist/cjs/index.js +148 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/signer-5OVDxViv.d.ts +79 -0
- package/dist/cjs/v1/index.d.ts +7 -0
- package/dist/cjs/v1/index.js +171 -0
- package/dist/cjs/v1/index.js.map +1 -0
- package/dist/esm/chunk-CYGMVQCG.mjs +88 -0
- package/dist/esm/chunk-CYGMVQCG.mjs.map +1 -0
- package/dist/esm/chunk-DI3EK7PR.mjs +305 -0
- package/dist/esm/chunk-DI3EK7PR.mjs.map +1 -0
- package/dist/esm/chunk-QLXM7BIB.mjs +23 -0
- package/dist/esm/chunk-QLXM7BIB.mjs.map +1 -0
- package/dist/esm/chunk-S5OEANGR.mjs +92 -0
- package/dist/esm/chunk-S5OEANGR.mjs.map +1 -0
- package/dist/esm/chunk-T3FPOH7F.mjs +88 -0
- package/dist/esm/chunk-T3FPOH7F.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +53 -0
- package/dist/esm/exact/client/index.mjs +36 -0
- package/dist/esm/exact/client/index.mjs.map +1 -0
- package/dist/esm/exact/facilitator/index.d.mts +118 -0
- package/dist/esm/exact/facilitator/index.mjs +324 -0
- package/dist/esm/exact/facilitator/index.mjs.map +1 -0
- package/dist/esm/exact/server/index.d.mts +140 -0
- package/dist/esm/exact/server/index.mjs +219 -0
- package/dist/esm/exact/server/index.mjs.map +1 -0
- package/dist/esm/exact/v1/client/index.d.mts +37 -0
- package/dist/esm/exact/v1/client/index.mjs +8 -0
- package/dist/esm/exact/v1/client/index.mjs.map +1 -0
- package/dist/esm/exact/v1/facilitator/index.d.mts +62 -0
- package/dist/esm/exact/v1/facilitator/index.mjs +8 -0
- package/dist/esm/exact/v1/facilitator/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +36 -0
- package/dist/esm/index.mjs +21 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/signer-5OVDxViv.d.mts +79 -0
- package/dist/esm/v1/index.d.mts +7 -0
- package/dist/esm/v1/index.mjs +13 -0
- package/dist/esm/v1/index.mjs.map +1 -0
- package/package.json +127 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exact/client/scheme.ts"],"sourcesContent":["import { PaymentPayload, PaymentRequirements, SchemeNetworkClient } from \"x402-core-mantle/types\";\nimport { getAddress } from \"viem\";\nimport { authorizationTypes } from \"../../constants\";\nimport { ClientEvmSigner } from \"../../signer\";\nimport { ExactEvmPayloadV2 } from \"../../types\";\nimport { createNonce } from \"../../utils\";\n\n/**\n * EVM client implementation for the Exact payment scheme.\n *\n */\nexport class ExactEvmScheme implements SchemeNetworkClient {\n readonly scheme = \"exact\";\n\n /**\n * Creates a new ExactEvmClient instance.\n *\n * @param signer - The EVM signer for client operations\n */\n constructor(private readonly signer: ClientEvmSigner) {}\n\n /**\n * Creates a payment payload for the Exact scheme.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - The payment requirements\n * @returns Promise resolving to a payment payload\n */\n async createPaymentPayload(\n x402Version: number,\n paymentRequirements: PaymentRequirements,\n ): Promise<Pick<PaymentPayload, \"x402Version\" | \"payload\">> {\n const nonce = createNonce();\n const now = Math.floor(Date.now() / 1000);\n\n const authorization: ExactEvmPayloadV2[\"authorization\"] = {\n from: this.signer.address,\n to: getAddress(paymentRequirements.payTo),\n value: paymentRequirements.amount,\n validAfter: (now - 600).toString(), // 10 minutes before\n validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),\n nonce,\n };\n\n // Sign the authorization\n const signature = await this.signAuthorization(authorization, paymentRequirements);\n\n const payload: ExactEvmPayloadV2 = {\n authorization,\n signature,\n };\n\n return {\n x402Version,\n payload,\n };\n }\n\n /**\n * Sign the EIP-3009 authorization using EIP-712\n *\n * @param authorization - The authorization to sign\n * @param requirements - The payment requirements\n * @returns Promise resolving to the signature\n */\n private async signAuthorization(\n authorization: ExactEvmPayloadV2[\"authorization\"],\n requirements: PaymentRequirements,\n ): Promise<`0x${string}`> {\n const chainId = parseInt(requirements.network.split(\":\")[1]);\n\n if (!requirements.extra?.name || !requirements.extra?.version) {\n throw new Error(\n `EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`,\n );\n }\n\n const { name, version } = requirements.extra;\n\n const domain = {\n name,\n version,\n chainId,\n verifyingContract: getAddress(requirements.asset),\n };\n\n const message = {\n from: getAddress(authorization.from),\n to: getAddress(authorization.to),\n value: BigInt(authorization.value),\n validAfter: BigInt(authorization.validAfter),\n validBefore: BigInt(authorization.validBefore),\n nonce: authorization.nonce,\n };\n\n return await this.signer.signTypedData({\n domain,\n types: authorizationTypes,\n primaryType: \"TransferWithAuthorization\",\n message,\n });\n }\n}\n"],"mappings":";;;;;;AACA,SAAS,kBAAkB;AAUpB,IAAM,iBAAN,MAAoD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQzD,YAA6B,QAAyB;AAAzB;AAP7B,SAAS,SAAS;AAAA,EAOqC;AAAA;AAA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AO,cAAc;AAAA,MACrC;AAAA,MACA,OAAO;AAAA,MACP,aAAa;AAAA,MACb;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export { ExactEvmScheme } from '../../index.mjs';
|
|
2
|
+
import { x402Client, SelectPaymentRequirements, PaymentPolicy } from 'x402-core-mantle/client';
|
|
3
|
+
import { Network } from 'x402-core-mantle/types';
|
|
4
|
+
import { C as ClientEvmSigner } from '../../signer-5OVDxViv.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for registering EVM schemes to an x402Client
|
|
8
|
+
*/
|
|
9
|
+
interface EvmClientConfig {
|
|
10
|
+
/**
|
|
11
|
+
* The EVM signer to use for creating payment payloads
|
|
12
|
+
*/
|
|
13
|
+
signer: ClientEvmSigner;
|
|
14
|
+
/**
|
|
15
|
+
* Optional payment requirements selector function
|
|
16
|
+
* If not provided, uses the default selector (first available option)
|
|
17
|
+
*/
|
|
18
|
+
paymentRequirementsSelector?: SelectPaymentRequirements;
|
|
19
|
+
/**
|
|
20
|
+
* Optional policies to apply to the client
|
|
21
|
+
*/
|
|
22
|
+
policies?: PaymentPolicy[];
|
|
23
|
+
/**
|
|
24
|
+
* Optional specific networks to register
|
|
25
|
+
* If not provided, registers wildcard support (eip155:*)
|
|
26
|
+
*/
|
|
27
|
+
networks?: Network[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Registers EVM exact payment schemes to an x402Client instance.
|
|
31
|
+
*
|
|
32
|
+
* This function registers:
|
|
33
|
+
* - V2: eip155:* wildcard scheme with ExactEvmScheme (or specific networks if provided)
|
|
34
|
+
* - V1: All supported EVM networks with ExactEvmSchemeV1
|
|
35
|
+
*
|
|
36
|
+
* @param client - The x402Client instance to register schemes to
|
|
37
|
+
* @param config - Configuration for EVM client registration
|
|
38
|
+
* @returns The client instance for chaining
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { registerExactEvmScheme } from "@x402/evm/exact/client/register";
|
|
43
|
+
* import { x402Client } from "x402-core-mantle/client";
|
|
44
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
45
|
+
*
|
|
46
|
+
* const account = privateKeyToAccount("0x...");
|
|
47
|
+
* const client = new x402Client();
|
|
48
|
+
* registerExactEvmScheme(client, { signer: account });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function registerExactEvmScheme(client: x402Client, config: EvmClientConfig): x402Client;
|
|
52
|
+
|
|
53
|
+
export { type EvmClientConfig, registerExactEvmScheme };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExactEvmScheme
|
|
3
|
+
} from "../../chunk-T3FPOH7F.mjs";
|
|
4
|
+
import {
|
|
5
|
+
NETWORKS
|
|
6
|
+
} from "../../chunk-QLXM7BIB.mjs";
|
|
7
|
+
import {
|
|
8
|
+
ExactEvmSchemeV1
|
|
9
|
+
} from "../../chunk-S5OEANGR.mjs";
|
|
10
|
+
import "../../chunk-DI3EK7PR.mjs";
|
|
11
|
+
import "../../chunk-CYGMVQCG.mjs";
|
|
12
|
+
|
|
13
|
+
// src/exact/client/register.ts
|
|
14
|
+
function registerExactEvmScheme(client, config) {
|
|
15
|
+
if (config.networks && config.networks.length > 0) {
|
|
16
|
+
config.networks.forEach((network) => {
|
|
17
|
+
client.register(network, new ExactEvmScheme(config.signer));
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
client.register("eip155:*", new ExactEvmScheme(config.signer));
|
|
21
|
+
}
|
|
22
|
+
NETWORKS.forEach((network) => {
|
|
23
|
+
client.registerV1(network, new ExactEvmSchemeV1(config.signer));
|
|
24
|
+
});
|
|
25
|
+
if (config.policies) {
|
|
26
|
+
config.policies.forEach((policy) => {
|
|
27
|
+
client.registerPolicy(policy);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return client;
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
ExactEvmScheme,
|
|
34
|
+
registerExactEvmScheme
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact/client/register.ts"],"sourcesContent":["import { x402Client, SelectPaymentRequirements, PaymentPolicy } from \"x402-core-mantle/client\";\nimport { Network } from \"x402-core-mantle/types\";\nimport { ClientEvmSigner } from \"../../signer\";\nimport { ExactEvmScheme } from \"./scheme\";\nimport { ExactEvmSchemeV1 } from \"../v1/client/scheme\";\nimport { NETWORKS } from \"../../v1\";\n\n/**\n * Configuration options for registering EVM schemes to an x402Client\n */\nexport interface EvmClientConfig {\n /**\n * The EVM signer to use for creating payment payloads\n */\n signer: ClientEvmSigner;\n\n /**\n * Optional payment requirements selector function\n * If not provided, uses the default selector (first available option)\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n\n /**\n * Optional policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\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 x402Client instance.\n *\n * This function registers:\n * - V2: eip155:* wildcard scheme with ExactEvmScheme (or specific networks if provided)\n * - V1: All supported EVM networks with ExactEvmSchemeV1\n *\n * @param client - The x402Client instance to register schemes to\n * @param config - Configuration for EVM client registration\n * @returns The client instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactEvmScheme } from \"@x402/evm/exact/client/register\";\n * import { x402Client } from \"x402-core-mantle/client\";\n * import { privateKeyToAccount } from \"viem/accounts\";\n *\n * const account = privateKeyToAccount(\"0x...\");\n * const client = new x402Client();\n * registerExactEvmScheme(client, { signer: account });\n * ```\n */\nexport function registerExactEvmScheme(client: x402Client, config: EvmClientConfig): x402Client {\n // Register V2 scheme\n if (config.networks && config.networks.length > 0) {\n // Register specific networks\n config.networks.forEach(network => {\n client.register(network, new ExactEvmScheme(config.signer));\n });\n } else {\n // Register wildcard for all EVM chains\n client.register(\"eip155:*\", new ExactEvmScheme(config.signer));\n }\n\n // Register all V1 networks\n NETWORKS.forEach(network => {\n client.registerV1(network as Network, new ExactEvmSchemeV1(config.signer));\n });\n\n // Apply policies if provided\n if (config.policies) {\n config.policies.forEach(policy => {\n client.registerPolicy(policy);\n });\n }\n\n return client;\n}\n"],"mappings":";;;;;;;;;;;;;AAwDO,SAAS,uBAAuB,QAAoB,QAAqC;AAE9F,MAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAO,SAAS,QAAQ,aAAW;AACjC,aAAO,SAAS,SAAS,IAAI,eAAe,OAAO,MAAM,CAAC;AAAA,IAC5D,CAAC;AAAA,EACH,OAAO;AAEL,WAAO,SAAS,YAAY,IAAI,eAAe,OAAO,MAAM,CAAC;AAAA,EAC/D;AAGA,WAAS,QAAQ,aAAW;AAC1B,WAAO,WAAW,SAAoB,IAAI,iBAAiB,OAAO,MAAM,CAAC;AAAA,EAC3E,CAAC;AAGD,MAAI,OAAO,UAAU;AACnB,WAAO,SAAS,QAAQ,YAAU;AAChC,aAAO,eAAe,MAAM;AAAA,IAC9B,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { SchemeNetworkFacilitator, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse, Network } from 'x402-core-mantle/types';
|
|
2
|
+
import { F as FacilitatorEvmSigner } from '../../signer-5OVDxViv.mjs';
|
|
3
|
+
import { x402Facilitator } from 'x402-core-mantle/facilitator';
|
|
4
|
+
|
|
5
|
+
interface ExactEvmSchemeConfig {
|
|
6
|
+
/**
|
|
7
|
+
* If enabled, the facilitator will deploy ERC-4337 smart wallets
|
|
8
|
+
* via EIP-6492 when encountering undeployed contract signatures.
|
|
9
|
+
*
|
|
10
|
+
* @default false
|
|
11
|
+
*/
|
|
12
|
+
deployERC4337WithEIP6492?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* EVM facilitator implementation for the Exact payment scheme.
|
|
16
|
+
*/
|
|
17
|
+
declare class ExactEvmScheme implements SchemeNetworkFacilitator {
|
|
18
|
+
private readonly signer;
|
|
19
|
+
readonly scheme = "exact";
|
|
20
|
+
readonly caipFamily = "eip155:*";
|
|
21
|
+
private readonly config;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new ExactEvmFacilitator instance.
|
|
24
|
+
*
|
|
25
|
+
* @param signer - The EVM signer for facilitator operations
|
|
26
|
+
* @param config - Optional configuration for the facilitator
|
|
27
|
+
*/
|
|
28
|
+
constructor(signer: FacilitatorEvmSigner, config?: ExactEvmSchemeConfig);
|
|
29
|
+
/**
|
|
30
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
31
|
+
* For EVM, no extra data is needed.
|
|
32
|
+
*
|
|
33
|
+
* @param _ - The network identifier (unused for EVM)
|
|
34
|
+
* @returns undefined (EVM has no extra data)
|
|
35
|
+
*/
|
|
36
|
+
getExtra(_: string): Record<string, unknown> | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Get signer addresses used by this facilitator.
|
|
39
|
+
* Returns all addresses this facilitator can use for signing/settling transactions.
|
|
40
|
+
*
|
|
41
|
+
* @param _ - The network identifier (unused for EVM, addresses are network-agnostic)
|
|
42
|
+
* @returns Array of facilitator wallet addresses
|
|
43
|
+
*/
|
|
44
|
+
getSigners(_: string): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Verifies a payment payload.
|
|
47
|
+
*
|
|
48
|
+
* @param payload - The payment payload to verify
|
|
49
|
+
* @param requirements - The payment requirements
|
|
50
|
+
* @returns Promise resolving to verification response
|
|
51
|
+
*/
|
|
52
|
+
verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
53
|
+
/**
|
|
54
|
+
* Settles a payment by executing the transfer.
|
|
55
|
+
*
|
|
56
|
+
* @param payload - The payment payload to settle
|
|
57
|
+
* @param requirements - The payment requirements
|
|
58
|
+
* @returns Promise resolving to settlement response
|
|
59
|
+
*/
|
|
60
|
+
settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Configuration options for registering EVM schemes to an x402Facilitator
|
|
65
|
+
*/
|
|
66
|
+
interface EvmFacilitatorConfig {
|
|
67
|
+
/**
|
|
68
|
+
* The EVM signer for facilitator operations (verify and settle)
|
|
69
|
+
*/
|
|
70
|
+
signer: FacilitatorEvmSigner;
|
|
71
|
+
/**
|
|
72
|
+
* Networks to register (single network or array of networks)
|
|
73
|
+
* Examples: "eip155:84532", ["eip155:84532", "eip155:1"]
|
|
74
|
+
*/
|
|
75
|
+
networks: Network | Network[];
|
|
76
|
+
/**
|
|
77
|
+
* If enabled, the facilitator will deploy ERC-4337 smart wallets
|
|
78
|
+
* via EIP-6492 when encountering undeployed contract signatures.
|
|
79
|
+
*
|
|
80
|
+
* @default false
|
|
81
|
+
*/
|
|
82
|
+
deployERC4337WithEIP6492?: boolean;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Registers EVM exact payment schemes to an x402Facilitator instance.
|
|
86
|
+
*
|
|
87
|
+
* This function registers:
|
|
88
|
+
* - V2: Specified networks with ExactEvmScheme
|
|
89
|
+
* - V1: All supported EVM networks with ExactEvmSchemeV1
|
|
90
|
+
*
|
|
91
|
+
* @param facilitator - The x402Facilitator instance to register schemes to
|
|
92
|
+
* @param config - Configuration for EVM facilitator registration
|
|
93
|
+
* @returns The facilitator instance for chaining
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* import { registerExactEvmScheme } from "@x402/evm/exact/facilitator/register";
|
|
98
|
+
* import { x402Facilitator } from "x402-core-mantle/facilitator";
|
|
99
|
+
* import { createPublicClient, createWalletClient } from "viem";
|
|
100
|
+
*
|
|
101
|
+
* const facilitator = new x402Facilitator();
|
|
102
|
+
*
|
|
103
|
+
* // Single network
|
|
104
|
+
* registerExactEvmScheme(facilitator, {
|
|
105
|
+
* signer: combinedClient,
|
|
106
|
+
* networks: "eip155:84532" // Base Sepolia
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* // Multiple networks (will auto-derive eip155:* pattern)
|
|
110
|
+
* registerExactEvmScheme(facilitator, {
|
|
111
|
+
* signer: combinedClient,
|
|
112
|
+
* networks: ["eip155:84532", "eip155:1"] // Base Sepolia and Mainnet
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function registerExactEvmScheme(facilitator: x402Facilitator, config: EvmFacilitatorConfig): x402Facilitator;
|
|
117
|
+
|
|
118
|
+
export { type EvmFacilitatorConfig, ExactEvmScheme, type ExactEvmSchemeConfig, registerExactEvmScheme };
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NETWORKS
|
|
3
|
+
} from "../../chunk-QLXM7BIB.mjs";
|
|
4
|
+
import "../../chunk-S5OEANGR.mjs";
|
|
5
|
+
import {
|
|
6
|
+
ExactEvmSchemeV1
|
|
7
|
+
} from "../../chunk-DI3EK7PR.mjs";
|
|
8
|
+
import {
|
|
9
|
+
authorizationTypes,
|
|
10
|
+
eip3009ABI
|
|
11
|
+
} from "../../chunk-CYGMVQCG.mjs";
|
|
12
|
+
|
|
13
|
+
// src/exact/facilitator/scheme.ts
|
|
14
|
+
import { getAddress, isAddressEqual, parseErc6492Signature, parseSignature } from "viem";
|
|
15
|
+
var ExactEvmScheme = class {
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new ExactEvmFacilitator instance.
|
|
18
|
+
*
|
|
19
|
+
* @param signer - The EVM signer for facilitator operations
|
|
20
|
+
* @param config - Optional configuration for the facilitator
|
|
21
|
+
*/
|
|
22
|
+
constructor(signer, config) {
|
|
23
|
+
this.signer = signer;
|
|
24
|
+
this.scheme = "exact";
|
|
25
|
+
this.caipFamily = "eip155:*";
|
|
26
|
+
this.config = {
|
|
27
|
+
deployERC4337WithEIP6492: config?.deployERC4337WithEIP6492 ?? false
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
32
|
+
* For EVM, no extra data is needed.
|
|
33
|
+
*
|
|
34
|
+
* @param _ - The network identifier (unused for EVM)
|
|
35
|
+
* @returns undefined (EVM has no extra data)
|
|
36
|
+
*/
|
|
37
|
+
getExtra(_) {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get signer addresses used by this facilitator.
|
|
42
|
+
* Returns all addresses this facilitator can use for signing/settling transactions.
|
|
43
|
+
*
|
|
44
|
+
* @param _ - The network identifier (unused for EVM, addresses are network-agnostic)
|
|
45
|
+
* @returns Array of facilitator wallet addresses
|
|
46
|
+
*/
|
|
47
|
+
getSigners(_) {
|
|
48
|
+
return [...this.signer.getAddresses()];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Verifies a payment payload.
|
|
52
|
+
*
|
|
53
|
+
* @param payload - The payment payload to verify
|
|
54
|
+
* @param requirements - The payment requirements
|
|
55
|
+
* @returns Promise resolving to verification response
|
|
56
|
+
*/
|
|
57
|
+
async verify(payload, requirements) {
|
|
58
|
+
const exactEvmPayload = payload.payload;
|
|
59
|
+
if (payload.accepted.scheme !== "exact" || requirements.scheme !== "exact") {
|
|
60
|
+
return {
|
|
61
|
+
isValid: false,
|
|
62
|
+
invalidReason: "unsupported_scheme",
|
|
63
|
+
payer: exactEvmPayload.authorization.from
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (!requirements.extra?.name || !requirements.extra?.version) {
|
|
67
|
+
return {
|
|
68
|
+
isValid: false,
|
|
69
|
+
invalidReason: "missing_eip712_domain",
|
|
70
|
+
payer: exactEvmPayload.authorization.from
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const { name, version } = requirements.extra;
|
|
74
|
+
const erc20Address = getAddress(requirements.asset);
|
|
75
|
+
if (payload.accepted.network !== requirements.network) {
|
|
76
|
+
return {
|
|
77
|
+
isValid: false,
|
|
78
|
+
invalidReason: "network_mismatch",
|
|
79
|
+
payer: exactEvmPayload.authorization.from
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const permitTypedData = {
|
|
83
|
+
types: authorizationTypes,
|
|
84
|
+
primaryType: "TransferWithAuthorization",
|
|
85
|
+
domain: {
|
|
86
|
+
name,
|
|
87
|
+
version,
|
|
88
|
+
chainId: parseInt(requirements.network.split(":")[1]),
|
|
89
|
+
verifyingContract: erc20Address
|
|
90
|
+
},
|
|
91
|
+
message: {
|
|
92
|
+
from: exactEvmPayload.authorization.from,
|
|
93
|
+
to: exactEvmPayload.authorization.to,
|
|
94
|
+
value: BigInt(exactEvmPayload.authorization.value),
|
|
95
|
+
validAfter: BigInt(exactEvmPayload.authorization.validAfter),
|
|
96
|
+
validBefore: BigInt(exactEvmPayload.authorization.validBefore),
|
|
97
|
+
nonce: exactEvmPayload.authorization.nonce
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
try {
|
|
101
|
+
const recoveredAddress = await this.signer.verifyTypedData({
|
|
102
|
+
address: exactEvmPayload.authorization.from,
|
|
103
|
+
...permitTypedData,
|
|
104
|
+
signature: exactEvmPayload.signature
|
|
105
|
+
});
|
|
106
|
+
if (!recoveredAddress) {
|
|
107
|
+
return {
|
|
108
|
+
isValid: false,
|
|
109
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
110
|
+
payer: exactEvmPayload.authorization.from
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
const signature = exactEvmPayload.signature;
|
|
115
|
+
const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
|
|
116
|
+
const isSmartWallet = signatureLength > 130;
|
|
117
|
+
if (isSmartWallet) {
|
|
118
|
+
const payerAddress = exactEvmPayload.authorization.from;
|
|
119
|
+
const bytecode = await this.signer.getCode({ address: payerAddress });
|
|
120
|
+
if (!bytecode || bytecode === "0x") {
|
|
121
|
+
const erc6492Data = parseErc6492Signature(signature);
|
|
122
|
+
const hasDeploymentInfo = erc6492Data.address && erc6492Data.data && !isAddressEqual(erc6492Data.address, "0x0000000000000000000000000000000000000000");
|
|
123
|
+
if (!hasDeploymentInfo) {
|
|
124
|
+
return {
|
|
125
|
+
isValid: false,
|
|
126
|
+
invalidReason: "invalid_exact_evm_payload_undeployed_smart_wallet",
|
|
127
|
+
payer: payerAddress
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
return {
|
|
132
|
+
isValid: false,
|
|
133
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
134
|
+
payer: exactEvmPayload.authorization.from
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
return {
|
|
139
|
+
isValid: false,
|
|
140
|
+
invalidReason: "invalid_exact_evm_payload_signature",
|
|
141
|
+
payer: exactEvmPayload.authorization.from
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (getAddress(exactEvmPayload.authorization.to) !== getAddress(requirements.payTo)) {
|
|
146
|
+
return {
|
|
147
|
+
isValid: false,
|
|
148
|
+
invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
|
|
149
|
+
payer: exactEvmPayload.authorization.from
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
153
|
+
if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(now + 6)) {
|
|
154
|
+
return {
|
|
155
|
+
isValid: false,
|
|
156
|
+
invalidReason: "invalid_exact_evm_payload_authorization_valid_before",
|
|
157
|
+
payer: exactEvmPayload.authorization.from
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(now)) {
|
|
161
|
+
return {
|
|
162
|
+
isValid: false,
|
|
163
|
+
invalidReason: "invalid_exact_evm_payload_authorization_valid_after",
|
|
164
|
+
payer: exactEvmPayload.authorization.from
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const balance = await this.signer.readContract({
|
|
169
|
+
address: erc20Address,
|
|
170
|
+
abi: eip3009ABI,
|
|
171
|
+
functionName: "balanceOf",
|
|
172
|
+
args: [exactEvmPayload.authorization.from]
|
|
173
|
+
});
|
|
174
|
+
if (BigInt(balance) < BigInt(requirements.amount)) {
|
|
175
|
+
return {
|
|
176
|
+
isValid: false,
|
|
177
|
+
invalidReason: "insufficient_funds",
|
|
178
|
+
payer: exactEvmPayload.authorization.from
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
} catch {
|
|
182
|
+
}
|
|
183
|
+
if (BigInt(exactEvmPayload.authorization.value) < BigInt(requirements.amount)) {
|
|
184
|
+
return {
|
|
185
|
+
isValid: false,
|
|
186
|
+
invalidReason: "invalid_exact_evm_payload_authorization_value",
|
|
187
|
+
payer: exactEvmPayload.authorization.from
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
isValid: true,
|
|
192
|
+
invalidReason: void 0,
|
|
193
|
+
payer: exactEvmPayload.authorization.from
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Settles a payment by executing the transfer.
|
|
198
|
+
*
|
|
199
|
+
* @param payload - The payment payload to settle
|
|
200
|
+
* @param requirements - The payment requirements
|
|
201
|
+
* @returns Promise resolving to settlement response
|
|
202
|
+
*/
|
|
203
|
+
async settle(payload, requirements) {
|
|
204
|
+
const exactEvmPayload = payload.payload;
|
|
205
|
+
const valid = await this.verify(payload, requirements);
|
|
206
|
+
if (!valid.isValid) {
|
|
207
|
+
return {
|
|
208
|
+
success: false,
|
|
209
|
+
network: payload.accepted.network,
|
|
210
|
+
transaction: "",
|
|
211
|
+
errorReason: valid.invalidReason ?? "invalid_scheme",
|
|
212
|
+
payer: exactEvmPayload.authorization.from
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const parseResult = parseErc6492Signature(exactEvmPayload.signature);
|
|
217
|
+
const { signature, address: factoryAddress, data: factoryCalldata } = parseResult;
|
|
218
|
+
if (this.config.deployERC4337WithEIP6492 && factoryAddress && factoryCalldata && !isAddressEqual(factoryAddress, "0x0000000000000000000000000000000000000000")) {
|
|
219
|
+
const payerAddress = exactEvmPayload.authorization.from;
|
|
220
|
+
const bytecode = await this.signer.getCode({ address: payerAddress });
|
|
221
|
+
if (!bytecode || bytecode === "0x") {
|
|
222
|
+
try {
|
|
223
|
+
console.log(`Deploying ERC-4337 smart wallet for ${payerAddress} via EIP-6492`);
|
|
224
|
+
const deployTx = await this.signer.sendTransaction({
|
|
225
|
+
to: factoryAddress,
|
|
226
|
+
data: factoryCalldata
|
|
227
|
+
});
|
|
228
|
+
await this.signer.waitForTransactionReceipt({ hash: deployTx });
|
|
229
|
+
console.log(`Successfully deployed smart wallet for ${payerAddress}`);
|
|
230
|
+
} catch (deployError) {
|
|
231
|
+
console.error("Smart wallet deployment failed:", deployError);
|
|
232
|
+
throw deployError;
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
console.log(`Smart wallet for ${payerAddress} already deployed, skipping deployment`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
|
|
239
|
+
const isECDSA = signatureLength === 130;
|
|
240
|
+
let tx;
|
|
241
|
+
if (isECDSA) {
|
|
242
|
+
const parsedSig = parseSignature(signature);
|
|
243
|
+
tx = await this.signer.writeContract({
|
|
244
|
+
address: getAddress(requirements.asset),
|
|
245
|
+
abi: eip3009ABI,
|
|
246
|
+
functionName: "transferWithAuthorization",
|
|
247
|
+
args: [
|
|
248
|
+
getAddress(exactEvmPayload.authorization.from),
|
|
249
|
+
getAddress(exactEvmPayload.authorization.to),
|
|
250
|
+
BigInt(exactEvmPayload.authorization.value),
|
|
251
|
+
BigInt(exactEvmPayload.authorization.validAfter),
|
|
252
|
+
BigInt(exactEvmPayload.authorization.validBefore),
|
|
253
|
+
exactEvmPayload.authorization.nonce,
|
|
254
|
+
parsedSig.v || parsedSig.yParity,
|
|
255
|
+
parsedSig.r,
|
|
256
|
+
parsedSig.s
|
|
257
|
+
]
|
|
258
|
+
});
|
|
259
|
+
} else {
|
|
260
|
+
tx = await this.signer.writeContract({
|
|
261
|
+
address: getAddress(requirements.asset),
|
|
262
|
+
abi: eip3009ABI,
|
|
263
|
+
functionName: "transferWithAuthorization",
|
|
264
|
+
args: [
|
|
265
|
+
getAddress(exactEvmPayload.authorization.from),
|
|
266
|
+
getAddress(exactEvmPayload.authorization.to),
|
|
267
|
+
BigInt(exactEvmPayload.authorization.value),
|
|
268
|
+
BigInt(exactEvmPayload.authorization.validAfter),
|
|
269
|
+
BigInt(exactEvmPayload.authorization.validBefore),
|
|
270
|
+
exactEvmPayload.authorization.nonce,
|
|
271
|
+
signature
|
|
272
|
+
]
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
const receipt = await this.signer.waitForTransactionReceipt({ hash: tx });
|
|
276
|
+
if (receipt.status !== "success") {
|
|
277
|
+
return {
|
|
278
|
+
success: false,
|
|
279
|
+
errorReason: "invalid_transaction_state",
|
|
280
|
+
transaction: tx,
|
|
281
|
+
network: payload.accepted.network,
|
|
282
|
+
payer: exactEvmPayload.authorization.from
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
success: true,
|
|
287
|
+
transaction: tx,
|
|
288
|
+
network: payload.accepted.network,
|
|
289
|
+
payer: exactEvmPayload.authorization.from
|
|
290
|
+
};
|
|
291
|
+
} catch (error) {
|
|
292
|
+
console.error("Failed to settle transaction:", error);
|
|
293
|
+
return {
|
|
294
|
+
success: false,
|
|
295
|
+
errorReason: "transaction_failed",
|
|
296
|
+
transaction: "",
|
|
297
|
+
network: payload.accepted.network,
|
|
298
|
+
payer: exactEvmPayload.authorization.from
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// src/exact/facilitator/register.ts
|
|
305
|
+
function registerExactEvmScheme(facilitator, config) {
|
|
306
|
+
facilitator.register(
|
|
307
|
+
config.networks,
|
|
308
|
+
new ExactEvmScheme(config.signer, {
|
|
309
|
+
deployERC4337WithEIP6492: config.deployERC4337WithEIP6492
|
|
310
|
+
})
|
|
311
|
+
);
|
|
312
|
+
facilitator.registerV1(
|
|
313
|
+
NETWORKS,
|
|
314
|
+
new ExactEvmSchemeV1(config.signer, {
|
|
315
|
+
deployERC4337WithEIP6492: config.deployERC4337WithEIP6492
|
|
316
|
+
})
|
|
317
|
+
);
|
|
318
|
+
return facilitator;
|
|
319
|
+
}
|
|
320
|
+
export {
|
|
321
|
+
ExactEvmScheme,
|
|
322
|
+
registerExactEvmScheme
|
|
323
|
+
};
|
|
324
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact/facilitator/scheme.ts","../../../../src/exact/facilitator/register.ts"],"sourcesContent":["import {\n PaymentPayload,\n PaymentRequirements,\n SchemeNetworkFacilitator,\n SettleResponse,\n VerifyResponse,\n} from \"x402-core-mantle/types\";\nimport { getAddress, Hex, isAddressEqual, parseErc6492Signature, parseSignature } from \"viem\";\nimport { authorizationTypes, eip3009ABI } from \"../../constants\";\nimport { FacilitatorEvmSigner } from \"../../signer\";\nimport { ExactEvmPayloadV2 } from \"../../types\";\n\nexport interface ExactEvmSchemeConfig {\n /**\n * If enabled, the facilitator will deploy ERC-4337 smart wallets\n * via EIP-6492 when encountering undeployed contract signatures.\n *\n * @default false\n */\n deployERC4337WithEIP6492?: boolean;\n}\n\n/**\n * EVM facilitator implementation for the Exact payment scheme.\n */\nexport class ExactEvmScheme implements SchemeNetworkFacilitator {\n readonly scheme = \"exact\";\n readonly caipFamily = \"eip155:*\";\n private readonly config: Required<ExactEvmSchemeConfig>;\n\n /**\n * Creates a new ExactEvmFacilitator instance.\n *\n * @param signer - The EVM signer for facilitator operations\n * @param config - Optional configuration for the facilitator\n */\n constructor(\n private readonly signer: FacilitatorEvmSigner,\n config?: ExactEvmSchemeConfig,\n ) {\n this.config = {\n deployERC4337WithEIP6492: config?.deployERC4337WithEIP6492 ?? false,\n };\n }\n\n /**\n * Get mechanism-specific extra data for the supported kinds endpoint.\n * For EVM, no extra data is needed.\n *\n * @param _ - The network identifier (unused for EVM)\n * @returns undefined (EVM has no extra data)\n */\n getExtra(_: string): Record<string, unknown> | undefined {\n return undefined;\n }\n\n /**\n * Get signer addresses used by this facilitator.\n * Returns all addresses this facilitator can use for signing/settling transactions.\n *\n * @param _ - The network identifier (unused for EVM, addresses are network-agnostic)\n * @returns Array of facilitator wallet addresses\n */\n getSigners(_: string): string[] {\n return [...this.signer.getAddresses()];\n }\n\n /**\n * Verifies a payment payload.\n *\n * @param payload - The payment payload to verify\n * @param requirements - The payment requirements\n * @returns Promise resolving to verification response\n */\n async verify(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const exactEvmPayload = payload.payload as ExactEvmPayloadV2;\n\n // Verify scheme matches\n if (payload.accepted.scheme !== \"exact\" || requirements.scheme !== \"exact\") {\n return {\n isValid: false,\n invalidReason: \"unsupported_scheme\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Get chain configuration\n if (!requirements.extra?.name || !requirements.extra?.version) {\n return {\n isValid: false,\n invalidReason: \"missing_eip712_domain\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n const { name, version } = requirements.extra;\n const erc20Address = getAddress(requirements.asset);\n\n // Verify network matches\n if (payload.accepted.network !== requirements.network) {\n return {\n isValid: false,\n invalidReason: \"network_mismatch\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Build typed data for signature verification\n const permitTypedData = {\n types: authorizationTypes,\n primaryType: \"TransferWithAuthorization\" as const,\n domain: {\n name,\n version,\n chainId: parseInt(requirements.network.split(\":\")[1]),\n verifyingContract: erc20Address,\n },\n message: {\n from: exactEvmPayload.authorization.from,\n to: exactEvmPayload.authorization.to,\n value: BigInt(exactEvmPayload.authorization.value),\n validAfter: BigInt(exactEvmPayload.authorization.validAfter),\n validBefore: BigInt(exactEvmPayload.authorization.validBefore),\n nonce: exactEvmPayload.authorization.nonce,\n },\n };\n\n // Verify signature\n try {\n const recoveredAddress = await this.signer.verifyTypedData({\n address: exactEvmPayload.authorization.from,\n ...permitTypedData,\n signature: exactEvmPayload.signature!,\n });\n\n if (!recoveredAddress) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_signature\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n } catch {\n // Signature verification failed - could be an undeployed smart wallet\n // Check if smart wallet is deployed\n const signature = exactEvmPayload.signature!;\n const signatureLength = signature.startsWith(\"0x\") ? signature.length - 2 : signature.length;\n const isSmartWallet = signatureLength > 130; // 65 bytes = 130 hex chars for EOA\n\n if (isSmartWallet) {\n const payerAddress = exactEvmPayload.authorization.from;\n const bytecode = await this.signer.getCode({ address: payerAddress });\n\n if (!bytecode || bytecode === \"0x\") {\n // Wallet is not deployed. Check if it's EIP-6492 with deployment info.\n // EIP-6492 signatures contain factory address and calldata needed for deployment.\n // Non-EIP-6492 undeployed wallets cannot succeed (no way to deploy them).\n const erc6492Data = parseErc6492Signature(signature);\n const hasDeploymentInfo =\n erc6492Data.address &&\n erc6492Data.data &&\n !isAddressEqual(erc6492Data.address, \"0x0000000000000000000000000000000000000000\");\n\n if (!hasDeploymentInfo) {\n // Non-EIP-6492 undeployed smart wallet - will always fail at settlement\n // since EIP-3009 requires on-chain EIP-1271 validation\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_undeployed_smart_wallet\",\n payer: payerAddress,\n };\n }\n // EIP-6492 signature with deployment info - allow through\n // Facilitators with sponsored deployment support can handle this in settle()\n } else {\n // Wallet is deployed but signature still failed - invalid signature\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_signature\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n } else {\n // EOA signature failed\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_signature\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n }\n\n // Verify payment recipient matches\n if (getAddress(exactEvmPayload.authorization.to) !== getAddress(requirements.payTo)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_recipient_mismatch\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Verify validBefore is in the future (with 6 second buffer for block time)\n const now = Math.floor(Date.now() / 1000);\n if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(now + 6)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_authorization_valid_before\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Verify validAfter is not in the future\n if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(now)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_authorization_valid_after\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n // Check balance\n try {\n const balance = (await this.signer.readContract({\n address: erc20Address,\n abi: eip3009ABI,\n functionName: \"balanceOf\",\n args: [exactEvmPayload.authorization.from],\n })) as bigint;\n\n if (BigInt(balance) < BigInt(requirements.amount)) {\n return {\n isValid: false,\n invalidReason: \"insufficient_funds\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n } catch {\n // If we can't check balance, continue with other validations\n }\n\n // Verify amount is sufficient\n if (BigInt(exactEvmPayload.authorization.value) < BigInt(requirements.amount)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_evm_payload_authorization_value\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n return {\n isValid: true,\n invalidReason: undefined,\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n /**\n * Settles a payment by executing the transfer.\n *\n * @param payload - The payment payload to settle\n * @param requirements - The payment requirements\n * @returns Promise resolving to settlement response\n */\n async settle(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const exactEvmPayload = payload.payload as ExactEvmPayloadV2;\n\n // Re-verify before settling\n const valid = await this.verify(payload, requirements);\n if (!valid.isValid) {\n return {\n success: false,\n network: payload.accepted.network,\n transaction: \"\",\n errorReason: valid.invalidReason ?? \"invalid_scheme\",\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n try {\n // Parse ERC-6492 signature if applicable\n const parseResult = parseErc6492Signature(exactEvmPayload.signature!);\n const { signature, address: factoryAddress, data: factoryCalldata } = parseResult;\n\n // Deploy ERC-4337 smart wallet via EIP-6492 if configured and needed\n if (\n this.config.deployERC4337WithEIP6492 &&\n factoryAddress &&\n factoryCalldata &&\n !isAddressEqual(factoryAddress, \"0x0000000000000000000000000000000000000000\")\n ) {\n // Check if smart wallet is already deployed\n const payerAddress = exactEvmPayload.authorization.from;\n const bytecode = await this.signer.getCode({ address: payerAddress });\n\n if (!bytecode || bytecode === \"0x\") {\n // Wallet not deployed - attempt deployment\n try {\n console.log(`Deploying ERC-4337 smart wallet for ${payerAddress} via EIP-6492`);\n\n // Send the factory calldata directly as a transaction\n // The factoryCalldata already contains the complete encoded function call\n const deployTx = await this.signer.sendTransaction({\n to: factoryAddress as Hex,\n data: factoryCalldata as Hex,\n });\n\n // Wait for deployment transaction\n await this.signer.waitForTransactionReceipt({ hash: deployTx });\n console.log(`Successfully deployed smart wallet for ${payerAddress}`);\n } catch (deployError) {\n console.error(\"Smart wallet deployment failed:\", deployError);\n // Deployment failed - cannot proceed\n throw deployError;\n }\n } else {\n console.log(`Smart wallet for ${payerAddress} already deployed, skipping deployment`);\n }\n }\n\n // Determine if this is an ECDSA signature (EOA) or smart wallet signature\n // ECDSA signatures are exactly 65 bytes (130 hex chars without 0x)\n const signatureLength = signature.startsWith(\"0x\") ? signature.length - 2 : signature.length;\n const isECDSA = signatureLength === 130;\n\n let tx: Hex;\n if (isECDSA) {\n // For EOA wallets, parse signature into v, r, s and use that overload\n const parsedSig = parseSignature(signature);\n\n tx = await this.signer.writeContract({\n address: getAddress(requirements.asset),\n abi: eip3009ABI,\n functionName: \"transferWithAuthorization\",\n args: [\n getAddress(exactEvmPayload.authorization.from),\n getAddress(exactEvmPayload.authorization.to),\n BigInt(exactEvmPayload.authorization.value),\n BigInt(exactEvmPayload.authorization.validAfter),\n BigInt(exactEvmPayload.authorization.validBefore),\n exactEvmPayload.authorization.nonce,\n (parsedSig.v as number | undefined) || parsedSig.yParity,\n parsedSig.r,\n parsedSig.s,\n ],\n });\n } else {\n // For smart wallets, use the bytes signature overload\n // The signature contains WebAuthn/P256 or other ERC-1271 compatible signature data\n tx = await this.signer.writeContract({\n address: getAddress(requirements.asset),\n abi: eip3009ABI,\n functionName: \"transferWithAuthorization\",\n args: [\n getAddress(exactEvmPayload.authorization.from),\n getAddress(exactEvmPayload.authorization.to),\n BigInt(exactEvmPayload.authorization.value),\n BigInt(exactEvmPayload.authorization.validAfter),\n BigInt(exactEvmPayload.authorization.validBefore),\n exactEvmPayload.authorization.nonce,\n signature,\n ],\n });\n }\n\n // Wait for transaction confirmation\n const receipt = await this.signer.waitForTransactionReceipt({ hash: tx });\n\n if (receipt.status !== \"success\") {\n return {\n success: false,\n errorReason: \"invalid_transaction_state\",\n transaction: tx,\n network: payload.accepted.network,\n payer: exactEvmPayload.authorization.from,\n };\n }\n\n return {\n success: true,\n transaction: tx,\n network: payload.accepted.network,\n payer: exactEvmPayload.authorization.from,\n };\n } catch (error) {\n console.error(\"Failed to settle transaction:\", error);\n return {\n success: false,\n errorReason: \"transaction_failed\",\n transaction: \"\",\n network: payload.accepted.network,\n payer: exactEvmPayload.authorization.from,\n };\n }\n }\n}\n","import { x402Facilitator } from \"x402-core-mantle/facilitator\";\nimport { Network } from \"x402-core-mantle/types\";\nimport { FacilitatorEvmSigner } from \"../../signer\";\nimport { ExactEvmScheme } from \"./scheme\";\nimport { ExactEvmSchemeV1 } from \"../v1/facilitator/scheme\";\nimport { NETWORKS } from \"../../v1\";\n\n/**\n * Configuration options for registering EVM schemes to an x402Facilitator\n */\nexport interface EvmFacilitatorConfig {\n /**\n * The EVM signer for facilitator operations (verify and settle)\n */\n signer: FacilitatorEvmSigner;\n\n /**\n * Networks to register (single network or array of networks)\n * Examples: \"eip155:84532\", [\"eip155:84532\", \"eip155:1\"]\n */\n networks: Network | Network[];\n\n /**\n * If enabled, the facilitator will deploy ERC-4337 smart wallets\n * via EIP-6492 when encountering undeployed contract signatures.\n *\n * @default false\n */\n deployERC4337WithEIP6492?: boolean;\n}\n\n/**\n * Registers EVM exact payment schemes to an x402Facilitator instance.\n *\n * This function registers:\n * - V2: Specified networks with ExactEvmScheme\n * - V1: All supported EVM networks with ExactEvmSchemeV1\n *\n * @param facilitator - The x402Facilitator instance to register schemes to\n * @param config - Configuration for EVM facilitator registration\n * @returns The facilitator instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactEvmScheme } from \"@x402/evm/exact/facilitator/register\";\n * import { x402Facilitator } from \"x402-core-mantle/facilitator\";\n * import { createPublicClient, createWalletClient } from \"viem\";\n *\n * const facilitator = new x402Facilitator();\n *\n * // Single network\n * registerExactEvmScheme(facilitator, {\n * signer: combinedClient,\n * networks: \"eip155:84532\" // Base Sepolia\n * });\n *\n * // Multiple networks (will auto-derive eip155:* pattern)\n * registerExactEvmScheme(facilitator, {\n * signer: combinedClient,\n * networks: [\"eip155:84532\", \"eip155:1\"] // Base Sepolia and Mainnet\n * });\n * ```\n */\nexport function registerExactEvmScheme(\n facilitator: x402Facilitator,\n config: EvmFacilitatorConfig,\n): x402Facilitator {\n // Register V2 scheme with specified networks\n facilitator.register(\n config.networks,\n new ExactEvmScheme(config.signer, {\n deployERC4337WithEIP6492: config.deployERC4337WithEIP6492,\n }),\n );\n\n // Register all V1 networks\n facilitator.registerV1(\n NETWORKS as Network[],\n new ExactEvmSchemeV1(config.signer, {\n deployERC4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