@bankofai/x402-evm 2.6.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +184 -0
  2. package/dist/cjs/exact/client/index.d.ts +53 -0
  3. package/dist/cjs/exact/client/index.js +661 -0
  4. package/dist/cjs/exact/client/index.js.map +1 -0
  5. package/dist/cjs/exact/facilitator/index.d.ts +121 -0
  6. package/dist/cjs/exact/facilitator/index.js +1458 -0
  7. package/dist/cjs/exact/facilitator/index.js.map +1 -0
  8. package/dist/cjs/exact/server/index.d.ts +125 -0
  9. package/dist/cjs/exact/server/index.js +229 -0
  10. package/dist/cjs/exact/server/index.js.map +1 -0
  11. package/dist/cjs/exact/v1/client/index.d.ts +37 -0
  12. package/dist/cjs/exact/v1/client/index.js +175 -0
  13. package/dist/cjs/exact/v1/client/index.js.map +1 -0
  14. package/dist/cjs/exact/v1/facilitator/index.d.ts +62 -0
  15. package/dist/cjs/exact/v1/facilitator/index.js +436 -0
  16. package/dist/cjs/exact/v1/facilitator/index.js.map +1 -0
  17. package/dist/cjs/exact_permit/client/index.d.ts +57 -0
  18. package/dist/cjs/exact_permit/client/index.js +223 -0
  19. package/dist/cjs/exact_permit/client/index.js.map +1 -0
  20. package/dist/cjs/exact_permit/facilitator/index.d.ts +82 -0
  21. package/dist/cjs/exact_permit/facilitator/index.js +437 -0
  22. package/dist/cjs/exact_permit/facilitator/index.js.map +1 -0
  23. package/dist/cjs/exact_permit/server/index.d.ts +71 -0
  24. package/dist/cjs/exact_permit/server/index.js +186 -0
  25. package/dist/cjs/exact_permit/server/index.js.map +1 -0
  26. package/dist/cjs/index.d.ts +76 -0
  27. package/dist/cjs/index.js +779 -0
  28. package/dist/cjs/index.js.map +1 -0
  29. package/dist/cjs/permit2-CpuKY2-F.d.ts +509 -0
  30. package/dist/cjs/permit2-D65OQ5yU.d.ts +517 -0
  31. package/dist/cjs/permit2-DvsTlAyj.d.ts +531 -0
  32. package/dist/cjs/signer-DC81R8wQ.d.ts +161 -0
  33. package/dist/cjs/v1/index.d.ts +39 -0
  34. package/dist/cjs/v1/index.js +181 -0
  35. package/dist/cjs/v1/index.js.map +1 -0
  36. package/dist/esm/chunk-4JGB4GDD.mjs +425 -0
  37. package/dist/esm/chunk-4JGB4GDD.mjs.map +1 -0
  38. package/dist/esm/chunk-TKN5V2BV.mjs +13 -0
  39. package/dist/esm/chunk-TKN5V2BV.mjs.map +1 -0
  40. package/dist/esm/chunk-UDKSVWSE.mjs +716 -0
  41. package/dist/esm/chunk-UDKSVWSE.mjs.map +1 -0
  42. package/dist/esm/exact/client/index.d.mts +53 -0
  43. package/dist/esm/exact/client/index.mjs +17 -0
  44. package/dist/esm/exact/client/index.mjs.map +1 -0
  45. package/dist/esm/exact/facilitator/index.d.mts +121 -0
  46. package/dist/esm/exact/facilitator/index.mjs +876 -0
  47. package/dist/esm/exact/facilitator/index.mjs.map +1 -0
  48. package/dist/esm/exact/server/index.d.mts +125 -0
  49. package/dist/esm/exact/server/index.mjs +201 -0
  50. package/dist/esm/exact/server/index.mjs.map +1 -0
  51. package/dist/esm/exact/v1/client/index.d.mts +37 -0
  52. package/dist/esm/exact/v1/client/index.mjs +7 -0
  53. package/dist/esm/exact/v1/client/index.mjs.map +1 -0
  54. package/dist/esm/exact/v1/facilitator/index.d.mts +62 -0
  55. package/dist/esm/exact/v1/facilitator/index.mjs +7 -0
  56. package/dist/esm/exact/v1/facilitator/index.mjs.map +1 -0
  57. package/dist/esm/index.d.mts +76 -0
  58. package/dist/esm/index.mjs +71 -0
  59. package/dist/esm/index.mjs.map +1 -0
  60. package/dist/esm/permit2-D7ni3g6s.d.mts +509 -0
  61. package/dist/esm/signer-DC81R8wQ.d.mts +161 -0
  62. package/dist/esm/v1/index.d.mts +39 -0
  63. package/dist/esm/v1/index.mjs +13 -0
  64. package/dist/esm/v1/index.mjs.map +1 -0
  65. package/package.json +128 -0
@@ -0,0 +1,716 @@
1
+ // src/exact/v1/client/scheme.ts
2
+ import { getAddress } from "viem";
3
+
4
+ // src/constants.ts
5
+ var authorizationTypes = {
6
+ TransferWithAuthorization: [
7
+ { name: "from", type: "address" },
8
+ { name: "to", type: "address" },
9
+ { name: "value", type: "uint256" },
10
+ { name: "validAfter", type: "uint256" },
11
+ { name: "validBefore", type: "uint256" },
12
+ { name: "nonce", type: "bytes32" }
13
+ ]
14
+ };
15
+ var permit2WitnessTypes = {
16
+ PermitWitnessTransferFrom: [
17
+ { name: "permitted", type: "TokenPermissions" },
18
+ { name: "spender", type: "address" },
19
+ { name: "nonce", type: "uint256" },
20
+ { name: "deadline", type: "uint256" },
21
+ { name: "witness", type: "Witness" }
22
+ ],
23
+ TokenPermissions: [
24
+ { name: "token", type: "address" },
25
+ { name: "amount", type: "uint256" }
26
+ ],
27
+ Witness: [
28
+ { name: "to", type: "address" },
29
+ { name: "validAfter", type: "uint256" }
30
+ ]
31
+ };
32
+ var eip3009ABI = [
33
+ {
34
+ inputs: [
35
+ { name: "from", type: "address" },
36
+ { name: "to", type: "address" },
37
+ { name: "value", type: "uint256" },
38
+ { name: "validAfter", type: "uint256" },
39
+ { name: "validBefore", type: "uint256" },
40
+ { name: "nonce", type: "bytes32" },
41
+ { name: "v", type: "uint8" },
42
+ { name: "r", type: "bytes32" },
43
+ { name: "s", type: "bytes32" }
44
+ ],
45
+ name: "transferWithAuthorization",
46
+ outputs: [],
47
+ stateMutability: "nonpayable",
48
+ type: "function"
49
+ },
50
+ {
51
+ inputs: [
52
+ { name: "from", type: "address" },
53
+ { name: "to", type: "address" },
54
+ { name: "value", type: "uint256" },
55
+ { name: "validAfter", type: "uint256" },
56
+ { name: "validBefore", type: "uint256" },
57
+ { name: "nonce", type: "bytes32" },
58
+ { name: "signature", type: "bytes" }
59
+ ],
60
+ name: "transferWithAuthorization",
61
+ outputs: [],
62
+ stateMutability: "nonpayable",
63
+ type: "function"
64
+ },
65
+ {
66
+ inputs: [{ name: "account", type: "address" }],
67
+ name: "balanceOf",
68
+ outputs: [{ name: "", type: "uint256" }],
69
+ stateMutability: "view",
70
+ type: "function"
71
+ },
72
+ {
73
+ inputs: [],
74
+ name: "version",
75
+ outputs: [{ name: "", type: "string" }],
76
+ stateMutability: "view",
77
+ type: "function"
78
+ }
79
+ ];
80
+ var eip2612PermitTypes = {
81
+ Permit: [
82
+ { name: "owner", type: "address" },
83
+ { name: "spender", type: "address" },
84
+ { name: "value", type: "uint256" },
85
+ { name: "nonce", type: "uint256" },
86
+ { name: "deadline", type: "uint256" }
87
+ ]
88
+ };
89
+ var eip2612NoncesAbi = [
90
+ {
91
+ type: "function",
92
+ name: "nonces",
93
+ inputs: [{ name: "owner", type: "address" }],
94
+ outputs: [{ type: "uint256" }],
95
+ stateMutability: "view"
96
+ }
97
+ ];
98
+ var erc20ApproveAbi = [
99
+ {
100
+ type: "function",
101
+ name: "approve",
102
+ inputs: [
103
+ { name: "spender", type: "address" },
104
+ { name: "amount", type: "uint256" }
105
+ ],
106
+ outputs: [{ type: "bool" }],
107
+ stateMutability: "nonpayable"
108
+ }
109
+ ];
110
+ var erc20AllowanceAbi = [
111
+ {
112
+ type: "function",
113
+ name: "allowance",
114
+ inputs: [
115
+ { name: "owner", type: "address" },
116
+ { name: "spender", type: "address" }
117
+ ],
118
+ outputs: [{ type: "uint256" }],
119
+ stateMutability: "view"
120
+ }
121
+ ];
122
+ var ERC20_APPROVE_GAS_LIMIT = 70000n;
123
+ var DEFAULT_MAX_FEE_PER_GAS = 1000000000n;
124
+ var DEFAULT_MAX_PRIORITY_FEE_PER_GAS = 100000000n;
125
+ var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
126
+ var x402ExactPermit2ProxyAddress = "0xEe38Ec718255fe78e9D16aCC0e1183C731679b23";
127
+ var x402UptoPermit2ProxyAddress = "0x2b30Ed9F37c7C21ae8779c5753B1cCf264DfD63C";
128
+ var permit2WitnessABIComponents = [
129
+ { name: "to", type: "address", internalType: "address" },
130
+ { name: "validAfter", type: "uint256", internalType: "uint256" }
131
+ ];
132
+ var x402ExactPermit2ProxyABI = [
133
+ {
134
+ type: "function",
135
+ name: "PERMIT2",
136
+ inputs: [],
137
+ outputs: [{ name: "", type: "address", internalType: "contract ISignatureTransfer" }],
138
+ stateMutability: "view"
139
+ },
140
+ {
141
+ type: "function",
142
+ name: "WITNESS_TYPEHASH",
143
+ inputs: [],
144
+ outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
145
+ stateMutability: "view"
146
+ },
147
+ {
148
+ type: "function",
149
+ name: "WITNESS_TYPE_STRING",
150
+ inputs: [],
151
+ outputs: [{ name: "", type: "string", internalType: "string" }],
152
+ stateMutability: "view"
153
+ },
154
+ {
155
+ type: "function",
156
+ name: "settle",
157
+ inputs: [
158
+ {
159
+ name: "permit",
160
+ type: "tuple",
161
+ internalType: "struct ISignatureTransfer.PermitTransferFrom",
162
+ components: [
163
+ {
164
+ name: "permitted",
165
+ type: "tuple",
166
+ internalType: "struct ISignatureTransfer.TokenPermissions",
167
+ components: [
168
+ { name: "token", type: "address", internalType: "address" },
169
+ { name: "amount", type: "uint256", internalType: "uint256" }
170
+ ]
171
+ },
172
+ { name: "nonce", type: "uint256", internalType: "uint256" },
173
+ { name: "deadline", type: "uint256", internalType: "uint256" }
174
+ ]
175
+ },
176
+ { name: "owner", type: "address", internalType: "address" },
177
+ {
178
+ name: "witness",
179
+ type: "tuple",
180
+ internalType: "struct x402ExactPermit2Proxy.Witness",
181
+ components: permit2WitnessABIComponents
182
+ },
183
+ { name: "signature", type: "bytes", internalType: "bytes" }
184
+ ],
185
+ outputs: [],
186
+ stateMutability: "nonpayable"
187
+ },
188
+ {
189
+ type: "function",
190
+ name: "settleWithPermit",
191
+ inputs: [
192
+ {
193
+ name: "permit2612",
194
+ type: "tuple",
195
+ internalType: "struct x402ExactPermit2Proxy.EIP2612Permit",
196
+ components: [
197
+ { name: "value", type: "uint256", internalType: "uint256" },
198
+ { name: "deadline", type: "uint256", internalType: "uint256" },
199
+ { name: "r", type: "bytes32", internalType: "bytes32" },
200
+ { name: "s", type: "bytes32", internalType: "bytes32" },
201
+ { name: "v", type: "uint8", internalType: "uint8" }
202
+ ]
203
+ },
204
+ {
205
+ name: "permit",
206
+ type: "tuple",
207
+ internalType: "struct ISignatureTransfer.PermitTransferFrom",
208
+ components: [
209
+ {
210
+ name: "permitted",
211
+ type: "tuple",
212
+ internalType: "struct ISignatureTransfer.TokenPermissions",
213
+ components: [
214
+ { name: "token", type: "address", internalType: "address" },
215
+ { name: "amount", type: "uint256", internalType: "uint256" }
216
+ ]
217
+ },
218
+ { name: "nonce", type: "uint256", internalType: "uint256" },
219
+ { name: "deadline", type: "uint256", internalType: "uint256" }
220
+ ]
221
+ },
222
+ { name: "owner", type: "address", internalType: "address" },
223
+ {
224
+ name: "witness",
225
+ type: "tuple",
226
+ internalType: "struct x402ExactPermit2Proxy.Witness",
227
+ components: permit2WitnessABIComponents
228
+ },
229
+ { name: "signature", type: "bytes", internalType: "bytes" }
230
+ ],
231
+ outputs: [],
232
+ stateMutability: "nonpayable"
233
+ },
234
+ { type: "event", name: "Settled", inputs: [], anonymous: false },
235
+ { type: "event", name: "SettledWithPermit", inputs: [], anonymous: false },
236
+ { type: "error", name: "InvalidAmount", inputs: [] },
237
+ { type: "error", name: "InvalidDestination", inputs: [] },
238
+ { type: "error", name: "InvalidOwner", inputs: [] },
239
+ { type: "error", name: "InvalidPermit2Address", inputs: [] },
240
+ { type: "error", name: "PaymentTooEarly", inputs: [] },
241
+ { type: "error", name: "Permit2612AmountMismatch", inputs: [] },
242
+ { type: "error", name: "ReentrancyGuardReentrantCall", inputs: [] }
243
+ ];
244
+
245
+ // src/utils.ts
246
+ import { toHex } from "viem";
247
+ function getEvmChainId(network) {
248
+ if (network.startsWith("eip155:")) {
249
+ const idStr = network.split(":")[1];
250
+ const chainId = parseInt(idStr, 10);
251
+ if (isNaN(chainId)) {
252
+ throw new Error(`Invalid CAIP-2 chain ID: ${network}`);
253
+ }
254
+ return chainId;
255
+ }
256
+ throw new Error(`Unsupported network format: ${network} (expected eip155:CHAIN_ID)`);
257
+ }
258
+ function getCrypto() {
259
+ const cryptoObj = globalThis.crypto;
260
+ if (!cryptoObj) {
261
+ throw new Error("Crypto API not available");
262
+ }
263
+ return cryptoObj;
264
+ }
265
+ function createNonce() {
266
+ return toHex(getCrypto().getRandomValues(new Uint8Array(32)));
267
+ }
268
+ function createPermit2Nonce() {
269
+ const randomBytes = getCrypto().getRandomValues(new Uint8Array(32));
270
+ return BigInt(toHex(randomBytes)).toString();
271
+ }
272
+
273
+ // src/exact/v1/client/scheme.ts
274
+ var ExactEvmSchemeV1 = class {
275
+ /**
276
+ * Creates a new ExactEvmClientV1 instance.
277
+ *
278
+ * @param signer - The EVM signer for client operations
279
+ */
280
+ constructor(signer) {
281
+ this.signer = signer;
282
+ this.scheme = "exact";
283
+ }
284
+ /**
285
+ * Creates a payment payload for the Exact scheme (V1).
286
+ *
287
+ * @param x402Version - The x402 protocol version
288
+ * @param paymentRequirements - The payment requirements
289
+ * @returns Promise resolving to a payment payload
290
+ */
291
+ async createPaymentPayload(x402Version, paymentRequirements) {
292
+ const selectedV1 = paymentRequirements;
293
+ const nonce = createNonce();
294
+ const now = Math.floor(Date.now() / 1e3);
295
+ const authorization = {
296
+ from: this.signer.address,
297
+ to: getAddress(selectedV1.payTo),
298
+ value: selectedV1.maxAmountRequired,
299
+ validAfter: (now - 600).toString(),
300
+ // 10 minutes before
301
+ validBefore: (now + selectedV1.maxTimeoutSeconds).toString(),
302
+ nonce
303
+ };
304
+ const signature = await this.signAuthorization(authorization, selectedV1);
305
+ const payload = {
306
+ authorization,
307
+ signature
308
+ };
309
+ return {
310
+ x402Version,
311
+ scheme: selectedV1.scheme,
312
+ network: selectedV1.network,
313
+ payload
314
+ };
315
+ }
316
+ /**
317
+ * Sign the EIP-3009 authorization using EIP-712
318
+ *
319
+ * @param authorization - The authorization to sign
320
+ * @param requirements - The payment requirements
321
+ * @returns Promise resolving to the signature
322
+ */
323
+ async signAuthorization(authorization, requirements) {
324
+ const chainId = getEvmChainIdV1(requirements.network);
325
+ if (!requirements.extra?.name || !requirements.extra?.version) {
326
+ throw new Error(
327
+ `EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
328
+ );
329
+ }
330
+ const { name, version } = requirements.extra;
331
+ const domain = {
332
+ name,
333
+ version,
334
+ chainId,
335
+ verifyingContract: getAddress(requirements.asset)
336
+ };
337
+ const message = {
338
+ from: getAddress(authorization.from),
339
+ to: getAddress(authorization.to),
340
+ value: BigInt(authorization.value),
341
+ validAfter: BigInt(authorization.validAfter),
342
+ validBefore: BigInt(authorization.validBefore),
343
+ nonce: authorization.nonce
344
+ };
345
+ return await this.signer.signTypedData({
346
+ domain,
347
+ types: authorizationTypes,
348
+ primaryType: "TransferWithAuthorization",
349
+ message
350
+ });
351
+ }
352
+ };
353
+
354
+ // src/exact/v1/facilitator/scheme.ts
355
+ import { getAddress as getAddress2, isAddressEqual, parseErc6492Signature, parseSignature } from "viem";
356
+ var ExactEvmSchemeV12 = class {
357
+ /**
358
+ * Creates a new ExactEvmFacilitatorV1 instance.
359
+ *
360
+ * @param signer - The EVM signer for facilitator operations
361
+ * @param config - Optional configuration for the facilitator
362
+ */
363
+ constructor(signer, config) {
364
+ this.signer = signer;
365
+ this.scheme = "exact";
366
+ this.caipFamily = "eip155:*";
367
+ this.config = {
368
+ deployERC4337WithEIP6492: config?.deployERC4337WithEIP6492 ?? false
369
+ };
370
+ }
371
+ /**
372
+ * Get mechanism-specific extra data for the supported kinds endpoint.
373
+ * For EVM, no extra data is needed.
374
+ *
375
+ * @param _ - The network identifier (unused for EVM)
376
+ * @returns undefined (EVM has no extra data)
377
+ */
378
+ getExtra(_) {
379
+ return void 0;
380
+ }
381
+ /**
382
+ * Get signer addresses used by this facilitator.
383
+ * Returns all addresses this facilitator can use for signing/settling transactions.
384
+ *
385
+ * @param _ - The network identifier (unused for EVM, addresses are network-agnostic)
386
+ * @returns Array of facilitator wallet addresses
387
+ */
388
+ getSigners(_) {
389
+ return [...this.signer.getAddresses()];
390
+ }
391
+ /**
392
+ * Verifies a payment payload (V1).
393
+ *
394
+ * @param payload - The payment payload to verify
395
+ * @param requirements - The payment requirements
396
+ * @returns Promise resolving to verification response
397
+ */
398
+ async verify(payload, requirements) {
399
+ const requirementsV1 = requirements;
400
+ const payloadV1 = payload;
401
+ const exactEvmPayload = payload.payload;
402
+ if (payloadV1.scheme !== "exact" || requirements.scheme !== "exact") {
403
+ return {
404
+ isValid: false,
405
+ invalidReason: "unsupported_scheme",
406
+ payer: exactEvmPayload.authorization.from
407
+ };
408
+ }
409
+ let chainId;
410
+ try {
411
+ chainId = getEvmChainIdV1(payloadV1.network);
412
+ } catch {
413
+ return {
414
+ isValid: false,
415
+ invalidReason: `invalid_network`,
416
+ payer: exactEvmPayload.authorization.from
417
+ };
418
+ }
419
+ if (!requirements.extra?.name || !requirements.extra?.version) {
420
+ return {
421
+ isValid: false,
422
+ invalidReason: "missing_eip712_domain",
423
+ payer: exactEvmPayload.authorization.from
424
+ };
425
+ }
426
+ const { name, version } = requirements.extra;
427
+ const erc20Address = getAddress2(requirements.asset);
428
+ if (payloadV1.network !== requirements.network) {
429
+ return {
430
+ isValid: false,
431
+ invalidReason: "network_mismatch",
432
+ payer: exactEvmPayload.authorization.from
433
+ };
434
+ }
435
+ const permitTypedData = {
436
+ types: authorizationTypes,
437
+ primaryType: "TransferWithAuthorization",
438
+ domain: {
439
+ name,
440
+ version,
441
+ chainId,
442
+ verifyingContract: erc20Address
443
+ },
444
+ message: {
445
+ from: exactEvmPayload.authorization.from,
446
+ to: exactEvmPayload.authorization.to,
447
+ value: BigInt(exactEvmPayload.authorization.value),
448
+ validAfter: BigInt(exactEvmPayload.authorization.validAfter),
449
+ validBefore: BigInt(exactEvmPayload.authorization.validBefore),
450
+ nonce: exactEvmPayload.authorization.nonce
451
+ }
452
+ };
453
+ try {
454
+ const recoveredAddress = await this.signer.verifyTypedData({
455
+ address: exactEvmPayload.authorization.from,
456
+ ...permitTypedData,
457
+ signature: exactEvmPayload.signature
458
+ });
459
+ if (!recoveredAddress) {
460
+ return {
461
+ isValid: false,
462
+ invalidReason: "invalid_exact_evm_payload_signature",
463
+ payer: exactEvmPayload.authorization.from
464
+ };
465
+ }
466
+ } catch {
467
+ const signature = exactEvmPayload.signature;
468
+ const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
469
+ const isSmartWallet = signatureLength > 130;
470
+ if (isSmartWallet) {
471
+ const payerAddress = exactEvmPayload.authorization.from;
472
+ const bytecode = await this.signer.getCode({ address: payerAddress });
473
+ if (!bytecode || bytecode === "0x") {
474
+ const erc6492Data = parseErc6492Signature(signature);
475
+ const hasDeploymentInfo = erc6492Data.address && erc6492Data.data && !isAddressEqual(erc6492Data.address, "0x0000000000000000000000000000000000000000");
476
+ if (!hasDeploymentInfo) {
477
+ return {
478
+ isValid: false,
479
+ invalidReason: "invalid_exact_evm_payload_undeployed_smart_wallet",
480
+ payer: payerAddress
481
+ };
482
+ }
483
+ } else {
484
+ return {
485
+ isValid: false,
486
+ invalidReason: "invalid_exact_evm_payload_signature",
487
+ payer: exactEvmPayload.authorization.from
488
+ };
489
+ }
490
+ } else {
491
+ return {
492
+ isValid: false,
493
+ invalidReason: "invalid_exact_evm_payload_signature",
494
+ payer: exactEvmPayload.authorization.from
495
+ };
496
+ }
497
+ }
498
+ if (getAddress2(exactEvmPayload.authorization.to) !== getAddress2(requirements.payTo)) {
499
+ return {
500
+ isValid: false,
501
+ invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
502
+ payer: exactEvmPayload.authorization.from
503
+ };
504
+ }
505
+ const now = Math.floor(Date.now() / 1e3);
506
+ if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(now + 6)) {
507
+ return {
508
+ isValid: false,
509
+ invalidReason: "invalid_exact_evm_payload_authorization_valid_before",
510
+ payer: exactEvmPayload.authorization.from
511
+ };
512
+ }
513
+ if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(now)) {
514
+ return {
515
+ isValid: false,
516
+ invalidReason: "invalid_exact_evm_payload_authorization_valid_after",
517
+ payer: exactEvmPayload.authorization.from
518
+ };
519
+ }
520
+ try {
521
+ const balance = await this.signer.readContract({
522
+ address: erc20Address,
523
+ abi: eip3009ABI,
524
+ functionName: "balanceOf",
525
+ args: [exactEvmPayload.authorization.from]
526
+ });
527
+ if (BigInt(balance) < BigInt(requirementsV1.maxAmountRequired)) {
528
+ return {
529
+ isValid: false,
530
+ invalidReason: "insufficient_funds",
531
+ invalidMessage: `Insufficient funds to complete the payment. Required: ${requirementsV1.maxAmountRequired} ${requirements.asset}, Available: ${balance.toString()} ${requirements.asset}. Please add funds to your wallet and try again.`,
532
+ payer: exactEvmPayload.authorization.from
533
+ };
534
+ }
535
+ } catch {
536
+ }
537
+ if (BigInt(exactEvmPayload.authorization.value) !== BigInt(requirementsV1.maxAmountRequired)) {
538
+ return {
539
+ isValid: false,
540
+ invalidReason: "invalid_exact_evm_payload_authorization_value_mismatch",
541
+ payer: exactEvmPayload.authorization.from
542
+ };
543
+ }
544
+ return {
545
+ isValid: true,
546
+ invalidReason: void 0,
547
+ payer: exactEvmPayload.authorization.from
548
+ };
549
+ }
550
+ /**
551
+ * Settles a payment by executing the transfer (V1).
552
+ *
553
+ * @param payload - The payment payload to settle
554
+ * @param requirements - The payment requirements
555
+ * @returns Promise resolving to settlement response
556
+ */
557
+ async settle(payload, requirements) {
558
+ const payloadV1 = payload;
559
+ const exactEvmPayload = payload.payload;
560
+ const valid = await this.verify(payload, requirements);
561
+ if (!valid.isValid) {
562
+ return {
563
+ success: false,
564
+ network: payloadV1.network,
565
+ transaction: "",
566
+ errorReason: valid.invalidReason ?? "invalid_scheme",
567
+ payer: exactEvmPayload.authorization.from
568
+ };
569
+ }
570
+ try {
571
+ const parseResult = parseErc6492Signature(exactEvmPayload.signature);
572
+ const { signature, address: factoryAddress, data: factoryCalldata } = parseResult;
573
+ if (this.config.deployERC4337WithEIP6492 && factoryAddress && factoryCalldata && !isAddressEqual(factoryAddress, "0x0000000000000000000000000000000000000000")) {
574
+ const payerAddress = exactEvmPayload.authorization.from;
575
+ const bytecode = await this.signer.getCode({ address: payerAddress });
576
+ if (!bytecode || bytecode === "0x") {
577
+ try {
578
+ console.log(`Deploying ERC-4337 smart wallet for ${payerAddress} via EIP-6492`);
579
+ const deployTx = await this.signer.sendTransaction({
580
+ to: factoryAddress,
581
+ data: factoryCalldata
582
+ });
583
+ await this.signer.waitForTransactionReceipt({ hash: deployTx });
584
+ console.log(`Successfully deployed smart wallet for ${payerAddress}`);
585
+ } catch (deployError) {
586
+ console.error("Smart wallet deployment failed:", deployError);
587
+ throw deployError;
588
+ }
589
+ } else {
590
+ console.log(`Smart wallet for ${payerAddress} already deployed, skipping deployment`);
591
+ }
592
+ }
593
+ const signatureLength = signature.startsWith("0x") ? signature.length - 2 : signature.length;
594
+ const isECDSA = signatureLength === 130;
595
+ let tx;
596
+ if (isECDSA) {
597
+ const parsedSig = parseSignature(signature);
598
+ tx = await this.signer.writeContract({
599
+ address: getAddress2(requirements.asset),
600
+ abi: eip3009ABI,
601
+ functionName: "transferWithAuthorization",
602
+ args: [
603
+ getAddress2(exactEvmPayload.authorization.from),
604
+ getAddress2(exactEvmPayload.authorization.to),
605
+ BigInt(exactEvmPayload.authorization.value),
606
+ BigInt(exactEvmPayload.authorization.validAfter),
607
+ BigInt(exactEvmPayload.authorization.validBefore),
608
+ exactEvmPayload.authorization.nonce,
609
+ parsedSig.v || parsedSig.yParity,
610
+ parsedSig.r,
611
+ parsedSig.s
612
+ ]
613
+ });
614
+ } else {
615
+ tx = await this.signer.writeContract({
616
+ address: getAddress2(requirements.asset),
617
+ abi: eip3009ABI,
618
+ functionName: "transferWithAuthorization",
619
+ args: [
620
+ getAddress2(exactEvmPayload.authorization.from),
621
+ getAddress2(exactEvmPayload.authorization.to),
622
+ BigInt(exactEvmPayload.authorization.value),
623
+ BigInt(exactEvmPayload.authorization.validAfter),
624
+ BigInt(exactEvmPayload.authorization.validBefore),
625
+ exactEvmPayload.authorization.nonce,
626
+ signature
627
+ ]
628
+ });
629
+ }
630
+ const receipt = await this.signer.waitForTransactionReceipt({ hash: tx });
631
+ if (receipt.status !== "success") {
632
+ return {
633
+ success: false,
634
+ errorReason: "invalid_transaction_state",
635
+ transaction: tx,
636
+ network: payloadV1.network,
637
+ payer: exactEvmPayload.authorization.from
638
+ };
639
+ }
640
+ return {
641
+ success: true,
642
+ transaction: tx,
643
+ network: payloadV1.network,
644
+ payer: exactEvmPayload.authorization.from
645
+ };
646
+ } catch (error) {
647
+ console.error("Failed to settle transaction:", error);
648
+ return {
649
+ success: false,
650
+ errorReason: "transaction_failed",
651
+ transaction: "",
652
+ network: payloadV1.network,
653
+ payer: exactEvmPayload.authorization.from
654
+ };
655
+ }
656
+ }
657
+ };
658
+
659
+ // src/v1/index.ts
660
+ var EVM_NETWORK_CHAIN_ID_MAP = {
661
+ ethereum: 1,
662
+ sepolia: 11155111,
663
+ abstract: 2741,
664
+ "abstract-testnet": 11124,
665
+ "base-sepolia": 84532,
666
+ base: 8453,
667
+ "avalanche-fuji": 43113,
668
+ avalanche: 43114,
669
+ iotex: 4689,
670
+ sei: 1329,
671
+ "sei-testnet": 1328,
672
+ polygon: 137,
673
+ "polygon-amoy": 80002,
674
+ peaq: 3338,
675
+ story: 1514,
676
+ educhain: 41923,
677
+ "skale-base-sepolia": 324705682,
678
+ megaeth: 4326,
679
+ monad: 143,
680
+ bsc: 56,
681
+ "bsc-testnet": 97
682
+ };
683
+ var NETWORKS = Object.keys(EVM_NETWORK_CHAIN_ID_MAP);
684
+ function getEvmChainIdV1(network) {
685
+ const chainId = EVM_NETWORK_CHAIN_ID_MAP[network];
686
+ if (!chainId) {
687
+ throw new Error(`Unsupported v1 network: ${network}`);
688
+ }
689
+ return chainId;
690
+ }
691
+
692
+ export {
693
+ authorizationTypes,
694
+ permit2WitnessTypes,
695
+ eip3009ABI,
696
+ eip2612PermitTypes,
697
+ eip2612NoncesAbi,
698
+ erc20ApproveAbi,
699
+ erc20AllowanceAbi,
700
+ ERC20_APPROVE_GAS_LIMIT,
701
+ DEFAULT_MAX_FEE_PER_GAS,
702
+ DEFAULT_MAX_PRIORITY_FEE_PER_GAS,
703
+ PERMIT2_ADDRESS,
704
+ x402ExactPermit2ProxyAddress,
705
+ x402UptoPermit2ProxyAddress,
706
+ x402ExactPermit2ProxyABI,
707
+ getEvmChainId,
708
+ createNonce,
709
+ createPermit2Nonce,
710
+ ExactEvmSchemeV12 as ExactEvmSchemeV1,
711
+ EVM_NETWORK_CHAIN_ID_MAP,
712
+ NETWORKS,
713
+ getEvmChainIdV1,
714
+ ExactEvmSchemeV1 as ExactEvmSchemeV12
715
+ };
716
+ //# sourceMappingURL=chunk-UDKSVWSE.mjs.map