@x402/evm 2.4.0 → 2.5.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 (48) hide show
  1. package/dist/cjs/exact/client/index.d.ts +2 -2
  2. package/dist/cjs/exact/client/index.js +284 -192
  3. package/dist/cjs/exact/client/index.js.map +1 -1
  4. package/dist/cjs/exact/facilitator/index.d.ts +18 -18
  5. package/dist/cjs/exact/facilitator/index.js +454 -234
  6. package/dist/cjs/exact/facilitator/index.js.map +1 -1
  7. package/dist/cjs/exact/v1/client/index.d.ts +1 -1
  8. package/dist/cjs/exact/v1/client/index.js +17 -18
  9. package/dist/cjs/exact/v1/client/index.js.map +1 -1
  10. package/dist/cjs/exact/v1/facilitator/index.d.ts +1 -1
  11. package/dist/cjs/exact/v1/facilitator/index.js +7 -8
  12. package/dist/cjs/exact/v1/facilitator/index.js.map +1 -1
  13. package/dist/cjs/index.d.ts +3 -390
  14. package/dist/cjs/index.js +225 -132
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/permit2-CQbXqCMC.d.ts +517 -0
  17. package/dist/cjs/{signer-3KGwtdNT.d.ts → signer-DC81R8wQ.d.ts} +37 -0
  18. package/dist/cjs/v1/index.d.ts +11 -2
  19. package/dist/cjs/v1/index.js +14 -11
  20. package/dist/cjs/v1/index.js.map +1 -1
  21. package/dist/esm/{chunk-GSU4DHTC.mjs → chunk-7KHQD5KT.mjs} +72 -37
  22. package/dist/esm/chunk-7KHQD5KT.mjs.map +1 -0
  23. package/dist/esm/{chunk-CJHYX7BQ.mjs → chunk-GY6X5A3G.mjs} +119 -56
  24. package/dist/esm/chunk-GY6X5A3G.mjs.map +1 -0
  25. package/dist/esm/{chunk-PFULIQAE.mjs → chunk-TKN5V2BV.mjs} +1 -1
  26. package/dist/esm/chunk-TKN5V2BV.mjs.map +1 -0
  27. package/dist/esm/exact/client/index.d.mts +2 -2
  28. package/dist/esm/exact/client/index.mjs +4 -3
  29. package/dist/esm/exact/facilitator/index.d.mts +18 -18
  30. package/dist/esm/exact/facilitator/index.mjs +385 -176
  31. package/dist/esm/exact/facilitator/index.mjs.map +1 -1
  32. package/dist/esm/exact/v1/client/index.d.mts +1 -1
  33. package/dist/esm/exact/v1/client/index.mjs +2 -2
  34. package/dist/esm/exact/v1/facilitator/index.d.mts +1 -1
  35. package/dist/esm/exact/v1/facilitator/index.mjs +2 -2
  36. package/dist/esm/index.d.mts +3 -390
  37. package/dist/esm/index.mjs +18 -5
  38. package/dist/esm/index.mjs.map +1 -1
  39. package/dist/esm/permit2-CGOcN7Et.d.mts +517 -0
  40. package/dist/esm/{signer-3KGwtdNT.d.mts → signer-DC81R8wQ.d.mts} +37 -0
  41. package/dist/esm/v1/index.d.mts +11 -2
  42. package/dist/esm/v1/index.mjs +6 -4
  43. package/package.json +3 -3
  44. package/dist/cjs/permit2-CzKPU5By.d.ts +0 -130
  45. package/dist/esm/chunk-CJHYX7BQ.mjs.map +0 -1
  46. package/dist/esm/chunk-GSU4DHTC.mjs.map +0 -1
  47. package/dist/esm/chunk-PFULIQAE.mjs.map +0 -1
  48. package/dist/esm/permit2-C2FkNEHT.d.mts +0 -130
package/dist/cjs/index.js CHANGED
@@ -39,7 +39,7 @@ __export(src_exports, {
39
39
  module.exports = __toCommonJS(src_exports);
40
40
 
41
41
  // src/exact/client/scheme.ts
42
- var import_extensions = require("@x402/extensions");
42
+ var import_extensions2 = require("@x402/extensions");
43
43
 
44
44
  // src/constants.ts
45
45
  var authorizationTypes = {
@@ -66,8 +66,7 @@ var permit2WitnessTypes = {
66
66
  ],
67
67
  Witness: [
68
68
  { name: "to", type: "address" },
69
- { name: "validAfter", type: "uint256" },
70
- { name: "extra", type: "bytes" }
69
+ { name: "validAfter", type: "uint256" }
71
70
  ]
72
71
  };
73
72
  var eip3009ABI = [
@@ -136,9 +135,40 @@ var eip2612NoncesAbi = [
136
135
  stateMutability: "view"
137
136
  }
138
137
  ];
138
+ var erc20ApproveAbi = [
139
+ {
140
+ type: "function",
141
+ name: "approve",
142
+ inputs: [
143
+ { name: "spender", type: "address" },
144
+ { name: "amount", type: "uint256" }
145
+ ],
146
+ outputs: [{ type: "bool" }],
147
+ stateMutability: "nonpayable"
148
+ }
149
+ ];
150
+ var erc20AllowanceAbi = [
151
+ {
152
+ type: "function",
153
+ name: "allowance",
154
+ inputs: [
155
+ { name: "owner", type: "address" },
156
+ { name: "spender", type: "address" }
157
+ ],
158
+ outputs: [{ type: "uint256" }],
159
+ stateMutability: "view"
160
+ }
161
+ ];
162
+ var ERC20_APPROVE_GAS_LIMIT = 70000n;
163
+ var DEFAULT_MAX_FEE_PER_GAS = 1000000000n;
164
+ var DEFAULT_MAX_PRIORITY_FEE_PER_GAS = 100000000n;
139
165
  var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
140
- var x402ExactPermit2ProxyAddress = "0x4020615294c913F045dc10f0a5cdEbd86c280001";
141
- var x402UptoPermit2ProxyAddress = "0x4020633461b2895a48930Ff97eE8fCdE8E520002";
166
+ var x402ExactPermit2ProxyAddress = "0x402085c248EeA27D92E8b30b2C58ed07f9E20001";
167
+ var x402UptoPermit2ProxyAddress = "0x402039b3d6E6BEC5A02c2C9fd937ac17A6940002";
168
+ var permit2WitnessABIComponents = [
169
+ { name: "to", type: "address", internalType: "address" },
170
+ { name: "validAfter", type: "uint256", internalType: "uint256" }
171
+ ];
142
172
  var x402ExactPermit2ProxyABI = [
143
173
  {
144
174
  type: "function",
@@ -161,13 +191,6 @@ var x402ExactPermit2ProxyABI = [
161
191
  outputs: [{ name: "", type: "string", internalType: "string" }],
162
192
  stateMutability: "view"
163
193
  },
164
- {
165
- type: "function",
166
- name: "initialize",
167
- inputs: [{ name: "_permit2", type: "address", internalType: "address" }],
168
- outputs: [],
169
- stateMutability: "nonpayable"
170
- },
171
194
  {
172
195
  type: "function",
173
196
  name: "settle",
@@ -194,12 +217,8 @@ var x402ExactPermit2ProxyABI = [
194
217
  {
195
218
  name: "witness",
196
219
  type: "tuple",
197
- internalType: "struct x402BasePermit2Proxy.Witness",
198
- components: [
199
- { name: "to", type: "address", internalType: "address" },
200
- { name: "validAfter", type: "uint256", internalType: "uint256" },
201
- { name: "extra", type: "bytes", internalType: "bytes" }
202
- ]
220
+ internalType: "struct x402ExactPermit2Proxy.Witness",
221
+ components: permit2WitnessABIComponents
203
222
  },
204
223
  { name: "signature", type: "bytes", internalType: "bytes" }
205
224
  ],
@@ -213,7 +232,7 @@ var x402ExactPermit2ProxyABI = [
213
232
  {
214
233
  name: "permit2612",
215
234
  type: "tuple",
216
- internalType: "struct x402BasePermit2Proxy.EIP2612Permit",
235
+ internalType: "struct x402ExactPermit2Proxy.EIP2612Permit",
217
236
  components: [
218
237
  { name: "value", type: "uint256", internalType: "uint256" },
219
238
  { name: "deadline", type: "uint256", internalType: "uint256" },
@@ -244,12 +263,8 @@ var x402ExactPermit2ProxyABI = [
244
263
  {
245
264
  name: "witness",
246
265
  type: "tuple",
247
- internalType: "struct x402BasePermit2Proxy.Witness",
248
- components: [
249
- { name: "to", type: "address", internalType: "address" },
250
- { name: "validAfter", type: "uint256", internalType: "uint256" },
251
- { name: "extra", type: "bytes", internalType: "bytes" }
252
- ]
266
+ internalType: "struct x402ExactPermit2Proxy.Witness",
267
+ components: permit2WitnessABIComponents
253
268
  },
254
269
  { name: "signature", type: "bytes", internalType: "bytes" }
255
270
  ],
@@ -258,53 +273,31 @@ var x402ExactPermit2ProxyABI = [
258
273
  },
259
274
  { type: "event", name: "Settled", inputs: [], anonymous: false },
260
275
  { type: "event", name: "SettledWithPermit", inputs: [], anonymous: false },
261
- { type: "error", name: "AlreadyInitialized", inputs: [] },
276
+ { type: "error", name: "InvalidAmount", inputs: [] },
262
277
  { type: "error", name: "InvalidDestination", inputs: [] },
263
278
  { type: "error", name: "InvalidOwner", inputs: [] },
264
279
  { type: "error", name: "InvalidPermit2Address", inputs: [] },
265
280
  { type: "error", name: "PaymentTooEarly", inputs: [] },
281
+ { type: "error", name: "Permit2612AmountMismatch", inputs: [] },
266
282
  { type: "error", name: "ReentrancyGuardReentrantCall", inputs: [] }
267
283
  ];
268
284
 
269
285
  // src/exact/client/scheme.ts
270
- var import_viem7 = require("viem");
271
-
272
- // src/exact/client/eip3009.ts
273
- var import_viem4 = require("viem");
286
+ var import_viem6 = require("viem");
274
287
 
275
288
  // src/utils.ts
276
- var import_viem3 = require("viem");
277
-
278
- // src/exact/v1/client/scheme.ts
279
289
  var import_viem = require("viem");
280
-
281
- // src/exact/v1/facilitator/scheme.ts
282
- var import_viem2 = require("viem");
283
-
284
- // src/v1/index.ts
285
- var EVM_NETWORK_CHAIN_ID_MAP = {
286
- ethereum: 1,
287
- sepolia: 11155111,
288
- abstract: 2741,
289
- "abstract-testnet": 11124,
290
- "base-sepolia": 84532,
291
- base: 8453,
292
- "avalanche-fuji": 43113,
293
- avalanche: 43114,
294
- iotex: 4689,
295
- sei: 1329,
296
- "sei-testnet": 1328,
297
- polygon: 137,
298
- "polygon-amoy": 80002,
299
- peaq: 3338,
300
- story: 1514,
301
- educhain: 41923,
302
- "skale-base-sepolia": 324705682,
303
- megaeth: 4326
304
- };
305
- var NETWORKS = Object.keys(EVM_NETWORK_CHAIN_ID_MAP);
306
-
307
- // src/utils.ts
290
+ function getEvmChainId(network) {
291
+ if (network.startsWith("eip155:")) {
292
+ const idStr = network.split(":")[1];
293
+ const chainId = parseInt(idStr, 10);
294
+ if (isNaN(chainId)) {
295
+ throw new Error(`Invalid CAIP-2 chain ID: ${network}`);
296
+ }
297
+ return chainId;
298
+ }
299
+ throw new Error(`Unsupported network format: ${network} (expected eip155:CHAIN_ID)`);
300
+ }
308
301
  function getCrypto() {
309
302
  const cryptoObj = globalThis.crypto;
310
303
  if (!cryptoObj) {
@@ -313,20 +306,21 @@ function getCrypto() {
313
306
  return cryptoObj;
314
307
  }
315
308
  function createNonce() {
316
- return (0, import_viem3.toHex)(getCrypto().getRandomValues(new Uint8Array(32)));
309
+ return (0, import_viem.toHex)(getCrypto().getRandomValues(new Uint8Array(32)));
317
310
  }
318
311
  function createPermit2Nonce() {
319
312
  const randomBytes = getCrypto().getRandomValues(new Uint8Array(32));
320
- return BigInt((0, import_viem3.toHex)(randomBytes)).toString();
313
+ return BigInt((0, import_viem.toHex)(randomBytes)).toString();
321
314
  }
322
315
 
323
316
  // src/exact/client/eip3009.ts
317
+ var import_viem2 = require("viem");
324
318
  async function createEIP3009Payload(signer, x402Version, paymentRequirements) {
325
319
  const nonce = createNonce();
326
320
  const now = Math.floor(Date.now() / 1e3);
327
321
  const authorization = {
328
322
  from: signer.address,
329
- to: (0, import_viem4.getAddress)(paymentRequirements.payTo),
323
+ to: (0, import_viem2.getAddress)(paymentRequirements.payTo),
330
324
  value: paymentRequirements.amount,
331
325
  validAfter: (now - 600).toString(),
332
326
  validBefore: (now + paymentRequirements.maxTimeoutSeconds).toString(),
@@ -343,7 +337,7 @@ async function createEIP3009Payload(signer, x402Version, paymentRequirements) {
343
337
  };
344
338
  }
345
339
  async function signEIP3009Authorization(signer, authorization, requirements) {
346
- const chainId = parseInt(requirements.network.split(":")[1]);
340
+ const chainId = getEvmChainId(requirements.network);
347
341
  if (!requirements.extra?.name || !requirements.extra?.version) {
348
342
  throw new Error(
349
343
  `EIP-712 domain parameters (name, version) are required in payment requirements for asset ${requirements.asset}`
@@ -354,11 +348,11 @@ async function signEIP3009Authorization(signer, authorization, requirements) {
354
348
  name,
355
349
  version,
356
350
  chainId,
357
- verifyingContract: (0, import_viem4.getAddress)(requirements.asset)
351
+ verifyingContract: (0, import_viem2.getAddress)(requirements.asset)
358
352
  };
359
353
  const message = {
360
- from: (0, import_viem4.getAddress)(authorization.from),
361
- to: (0, import_viem4.getAddress)(authorization.to),
354
+ from: (0, import_viem2.getAddress)(authorization.from),
355
+ to: (0, import_viem2.getAddress)(authorization.to),
362
356
  value: BigInt(authorization.value),
363
357
  validAfter: BigInt(authorization.validAfter),
364
358
  validBefore: BigInt(authorization.validBefore),
@@ -373,7 +367,7 @@ async function signEIP3009Authorization(signer, authorization, requirements) {
373
367
  }
374
368
 
375
369
  // src/exact/client/permit2.ts
376
- var import_viem5 = require("viem");
370
+ var import_viem3 = require("viem");
377
371
  var MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
378
372
  async function createPermit2Payload(signer, x402Version, paymentRequirements) {
379
373
  const now = Math.floor(Date.now() / 1e3);
@@ -383,16 +377,15 @@ async function createPermit2Payload(signer, x402Version, paymentRequirements) {
383
377
  const permit2Authorization = {
384
378
  from: signer.address,
385
379
  permitted: {
386
- token: (0, import_viem5.getAddress)(paymentRequirements.asset),
380
+ token: (0, import_viem3.getAddress)(paymentRequirements.asset),
387
381
  amount: paymentRequirements.amount
388
382
  },
389
383
  spender: x402ExactPermit2ProxyAddress,
390
384
  nonce,
391
385
  deadline,
392
386
  witness: {
393
- to: (0, import_viem5.getAddress)(paymentRequirements.payTo),
394
- validAfter,
395
- extra: "0x"
387
+ to: (0, import_viem3.getAddress)(paymentRequirements.payTo),
388
+ validAfter
396
389
  }
397
390
  };
398
391
  const signature = await signPermit2Authorization(
@@ -410,7 +403,7 @@ async function createPermit2Payload(signer, x402Version, paymentRequirements) {
410
403
  };
411
404
  }
412
405
  async function signPermit2Authorization(signer, permit2Authorization, requirements) {
413
- const chainId = parseInt(requirements.network.split(":")[1]);
406
+ const chainId = getEvmChainId(requirements.network);
414
407
  const domain = {
415
408
  name: "Permit2",
416
409
  chainId,
@@ -418,16 +411,15 @@ async function signPermit2Authorization(signer, permit2Authorization, requiremen
418
411
  };
419
412
  const message = {
420
413
  permitted: {
421
- token: (0, import_viem5.getAddress)(permit2Authorization.permitted.token),
414
+ token: (0, import_viem3.getAddress)(permit2Authorization.permitted.token),
422
415
  amount: BigInt(permit2Authorization.permitted.amount)
423
416
  },
424
- spender: (0, import_viem5.getAddress)(permit2Authorization.spender),
417
+ spender: (0, import_viem3.getAddress)(permit2Authorization.spender),
425
418
  nonce: BigInt(permit2Authorization.nonce),
426
419
  deadline: BigInt(permit2Authorization.deadline),
427
420
  witness: {
428
- to: (0, import_viem5.getAddress)(permit2Authorization.witness.to),
429
- validAfter: BigInt(permit2Authorization.witness.validAfter),
430
- extra: permit2Authorization.witness.extra
421
+ to: (0, import_viem3.getAddress)(permit2Authorization.witness.to),
422
+ validAfter: BigInt(permit2Authorization.witness.validAfter)
431
423
  }
432
424
  };
433
425
  return await signer.signTypedData({
@@ -437,56 +429,31 @@ async function signPermit2Authorization(signer, permit2Authorization, requiremen
437
429
  message
438
430
  });
439
431
  }
440
- var erc20ApproveAbi = [
441
- {
442
- type: "function",
443
- name: "approve",
444
- inputs: [
445
- { name: "spender", type: "address" },
446
- { name: "amount", type: "uint256" }
447
- ],
448
- outputs: [{ type: "bool" }],
449
- stateMutability: "nonpayable"
450
- }
451
- ];
452
- var erc20AllowanceAbi = [
453
- {
454
- type: "function",
455
- name: "allowance",
456
- inputs: [
457
- { name: "owner", type: "address" },
458
- { name: "spender", type: "address" }
459
- ],
460
- outputs: [{ type: "uint256" }],
461
- stateMutability: "view"
462
- }
463
- ];
464
432
  function createPermit2ApprovalTx(tokenAddress) {
465
- const data = (0, import_viem5.encodeFunctionData)({
433
+ const data = (0, import_viem3.encodeFunctionData)({
466
434
  abi: erc20ApproveAbi,
467
435
  functionName: "approve",
468
436
  args: [PERMIT2_ADDRESS, MAX_UINT256]
469
437
  });
470
438
  return {
471
- to: (0, import_viem5.getAddress)(tokenAddress),
439
+ to: (0, import_viem3.getAddress)(tokenAddress),
472
440
  data
473
441
  };
474
442
  }
475
443
  function getPermit2AllowanceReadParams(params) {
476
444
  return {
477
- address: (0, import_viem5.getAddress)(params.tokenAddress),
445
+ address: (0, import_viem3.getAddress)(params.tokenAddress),
478
446
  abi: erc20AllowanceAbi,
479
447
  functionName: "allowance",
480
- args: [(0, import_viem5.getAddress)(params.ownerAddress), PERMIT2_ADDRESS]
448
+ args: [(0, import_viem3.getAddress)(params.ownerAddress), PERMIT2_ADDRESS]
481
449
  };
482
450
  }
483
451
 
484
452
  // src/exact/client/eip2612.ts
485
- var import_viem6 = require("viem");
486
- var MAX_UINT2562 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
487
- async function signEip2612Permit(signer, tokenAddress, tokenName, tokenVersion, chainId, deadline) {
453
+ var import_viem4 = require("viem");
454
+ async function signEip2612Permit(signer, tokenAddress, tokenName, tokenVersion, chainId, deadline, permittedAmount) {
488
455
  const owner = signer.address;
489
- const spender = (0, import_viem6.getAddress)(PERMIT2_ADDRESS);
456
+ const spender = (0, import_viem4.getAddress)(PERMIT2_ADDRESS);
490
457
  const nonce = await signer.readContract({
491
458
  address: tokenAddress,
492
459
  abi: eip2612NoncesAbi,
@@ -499,10 +466,11 @@ async function signEip2612Permit(signer, tokenAddress, tokenName, tokenVersion,
499
466
  chainId,
500
467
  verifyingContract: tokenAddress
501
468
  };
469
+ const approvalAmount = BigInt(permittedAmount);
502
470
  const message = {
503
471
  owner,
504
472
  spender,
505
- value: MAX_UINT2562,
473
+ value: approvalAmount,
506
474
  nonce,
507
475
  deadline: BigInt(deadline)
508
476
  };
@@ -516,7 +484,7 @@ async function signEip2612Permit(signer, tokenAddress, tokenName, tokenVersion,
516
484
  from: owner,
517
485
  asset: tokenAddress,
518
486
  spender,
519
- amount: MAX_UINT2562.toString(),
487
+ amount: approvalAmount.toString(),
520
488
  nonce: nonce.toString(),
521
489
  deadline,
522
490
  signature,
@@ -524,19 +492,48 @@ async function signEip2612Permit(signer, tokenAddress, tokenName, tokenVersion,
524
492
  };
525
493
  }
526
494
 
527
- // src/exact/client/scheme.ts
528
- var erc20AllowanceAbi2 = [
529
- {
530
- type: "function",
531
- name: "allowance",
532
- inputs: [
533
- { name: "owner", type: "address" },
534
- { name: "spender", type: "address" }
535
- ],
536
- outputs: [{ type: "uint256" }],
537
- stateMutability: "view"
495
+ // src/exact/client/erc20approval.ts
496
+ var import_viem5 = require("viem");
497
+ var import_extensions = require("@x402/extensions");
498
+ async function signErc20ApprovalTransaction(signer, tokenAddress, chainId) {
499
+ const from = signer.address;
500
+ const spender = (0, import_viem5.getAddress)(PERMIT2_ADDRESS);
501
+ const data = (0, import_viem5.encodeFunctionData)({
502
+ abi: erc20ApproveAbi,
503
+ functionName: "approve",
504
+ args: [spender, import_viem5.maxUint256]
505
+ });
506
+ const nonce = await signer.getTransactionCount({ address: from });
507
+ let maxFeePerGas;
508
+ let maxPriorityFeePerGas;
509
+ try {
510
+ const fees = await signer.estimateFeesPerGas();
511
+ maxFeePerGas = fees.maxFeePerGas;
512
+ maxPriorityFeePerGas = fees.maxPriorityFeePerGas;
513
+ } catch {
514
+ maxFeePerGas = DEFAULT_MAX_FEE_PER_GAS;
515
+ maxPriorityFeePerGas = DEFAULT_MAX_PRIORITY_FEE_PER_GAS;
538
516
  }
539
- ];
517
+ const signedTransaction = await signer.signTransaction({
518
+ to: tokenAddress,
519
+ data,
520
+ nonce,
521
+ gas: ERC20_APPROVE_GAS_LIMIT,
522
+ maxFeePerGas,
523
+ maxPriorityFeePerGas,
524
+ chainId
525
+ });
526
+ return {
527
+ from,
528
+ asset: tokenAddress,
529
+ spender,
530
+ amount: import_viem5.maxUint256.toString(),
531
+ signedTransaction,
532
+ version: import_extensions.ERC20_APPROVAL_GAS_SPONSORING_VERSION
533
+ };
534
+ }
535
+
536
+ // src/exact/client/scheme.ts
540
537
  var ExactEvmScheme = class {
541
538
  /**
542
539
  * Creates a new ExactEvmClient instance.
@@ -577,6 +574,13 @@ var ExactEvmScheme = class {
577
574
  extensions: eip2612Extensions
578
575
  };
579
576
  }
577
+ const erc20Extensions = await this.trySignErc20Approval(paymentRequirements, result, context);
578
+ if (erc20Extensions) {
579
+ return {
580
+ ...result,
581
+ extensions: erc20Extensions
582
+ };
583
+ }
580
584
  return result;
581
585
  }
582
586
  return createEIP3009Payload(this.signer, x402Version, paymentRequirements);
@@ -597,7 +601,7 @@ var ExactEvmScheme = class {
597
601
  * @returns Extension data for EIP-2612 gas sponsoring, or undefined if not applicable
598
602
  */
599
603
  async trySignEip2612Permit(requirements, result, context) {
600
- if (!context?.extensions?.[import_extensions.EIP2612_GAS_SPONSORING.key]) {
604
+ if (!context?.extensions?.[import_extensions2.EIP2612_GAS_SPONSORING.key]) {
601
605
  return void 0;
602
606
  }
603
607
  const tokenName = requirements.extra?.name;
@@ -605,12 +609,12 @@ var ExactEvmScheme = class {
605
609
  if (!tokenName || !tokenVersion) {
606
610
  return void 0;
607
611
  }
608
- const chainId = parseInt(requirements.network.split(":")[1]);
609
- const tokenAddress = (0, import_viem7.getAddress)(requirements.asset);
612
+ const chainId = getEvmChainId(requirements.network);
613
+ const tokenAddress = (0, import_viem6.getAddress)(requirements.asset);
610
614
  try {
611
615
  const allowance = await this.signer.readContract({
612
616
  address: tokenAddress,
613
- abi: erc20AllowanceAbi2,
617
+ abi: erc20AllowanceAbi,
614
618
  functionName: "allowance",
615
619
  args: [this.signer.address, PERMIT2_ADDRESS]
616
620
  });
@@ -627,13 +631,89 @@ var ExactEvmScheme = class {
627
631
  tokenName,
628
632
  tokenVersion,
629
633
  chainId,
630
- deadline
634
+ deadline,
635
+ requirements.amount
631
636
  );
632
637
  return {
633
- [import_extensions.EIP2612_GAS_SPONSORING.key]: { info }
638
+ [import_extensions2.EIP2612_GAS_SPONSORING.key]: { info }
634
639
  };
635
640
  }
641
+ /**
642
+ * Attempts to sign an ERC-20 approval transaction for gasless Permit2 approval.
643
+ *
644
+ * This is the fallback path when the token does not support EIP-2612. The client
645
+ * signs (but does not broadcast) a raw `approve(Permit2, MaxUint256)` transaction.
646
+ * The facilitator broadcasts it atomically before settling.
647
+ *
648
+ * Returns extension data if:
649
+ * 1. Server advertises erc20ApprovalGasSponsoring
650
+ * 2. Signer has signTransaction + getTransactionCount capabilities
651
+ * 3. Current Permit2 allowance is insufficient
652
+ *
653
+ * Returns undefined if the extension should not be used.
654
+ *
655
+ * @param requirements - The payment requirements from the server
656
+ * @param _result - The payment payload result from the scheme (unused)
657
+ * @param context - Optional context containing server extensions and metadata
658
+ * @returns Extension data for ERC-20 approval gas sponsoring, or undefined if not applicable
659
+ */
660
+ async trySignErc20Approval(requirements, _result, context) {
661
+ if (!context?.extensions?.[import_extensions2.ERC20_APPROVAL_GAS_SPONSORING.key]) {
662
+ return void 0;
663
+ }
664
+ if (!this.signer.signTransaction || !this.signer.getTransactionCount) {
665
+ return void 0;
666
+ }
667
+ const chainId = getEvmChainId(requirements.network);
668
+ const tokenAddress = (0, import_viem6.getAddress)(requirements.asset);
669
+ try {
670
+ const allowance = await this.signer.readContract({
671
+ address: tokenAddress,
672
+ abi: erc20AllowanceAbi,
673
+ functionName: "allowance",
674
+ args: [this.signer.address, PERMIT2_ADDRESS]
675
+ });
676
+ if (allowance >= BigInt(requirements.amount)) {
677
+ return void 0;
678
+ }
679
+ } catch {
680
+ }
681
+ const info = await signErc20ApprovalTransaction(this.signer, tokenAddress, chainId);
682
+ return {
683
+ [import_extensions2.ERC20_APPROVAL_GAS_SPONSORING.key]: { info }
684
+ };
685
+ }
686
+ };
687
+
688
+ // src/exact/v1/client/scheme.ts
689
+ var import_viem8 = require("viem");
690
+
691
+ // src/exact/v1/facilitator/scheme.ts
692
+ var import_viem7 = require("viem");
693
+
694
+ // src/v1/index.ts
695
+ var EVM_NETWORK_CHAIN_ID_MAP = {
696
+ ethereum: 1,
697
+ sepolia: 11155111,
698
+ abstract: 2741,
699
+ "abstract-testnet": 11124,
700
+ "base-sepolia": 84532,
701
+ base: 8453,
702
+ "avalanche-fuji": 43113,
703
+ avalanche: 43114,
704
+ iotex: 4689,
705
+ sei: 1329,
706
+ "sei-testnet": 1328,
707
+ polygon: 137,
708
+ "polygon-amoy": 80002,
709
+ peaq: 3338,
710
+ story: 1514,
711
+ educhain: 41923,
712
+ "skale-base-sepolia": 324705682,
713
+ megaeth: 4326,
714
+ monad: 143
636
715
  };
716
+ var NETWORKS = Object.keys(EVM_NETWORK_CHAIN_ID_MAP);
637
717
 
638
718
  // src/signer.ts
639
719
  function toClientEvmSigner(signer, publicClient) {
@@ -643,11 +723,24 @@ function toClientEvmSigner(signer, publicClient) {
643
723
  "toClientEvmSigner requires either a signer with readContract or a publicClient. Use createWalletClient(...).extend(publicActions) or pass a publicClient."
644
724
  );
645
725
  }
646
- return {
726
+ const result = {
647
727
  address: signer.address,
648
728
  signTypedData: (msg) => signer.signTypedData(msg),
649
729
  readContract
650
730
  };
731
+ const signTransaction = signer.signTransaction;
732
+ if (signTransaction) {
733
+ result.signTransaction = (args) => signTransaction(args);
734
+ }
735
+ const getTransactionCount = signer.getTransactionCount ?? publicClient?.getTransactionCount?.bind(publicClient);
736
+ if (getTransactionCount) {
737
+ result.getTransactionCount = (args) => getTransactionCount(args);
738
+ }
739
+ const estimateFeesPerGas = signer.estimateFeesPerGas ?? publicClient?.estimateFeesPerGas?.bind(publicClient);
740
+ if (estimateFeesPerGas) {
741
+ result.estimateFeesPerGas = () => estimateFeesPerGas();
742
+ }
743
+ return result;
651
744
  }
652
745
  function toFacilitatorEvmSigner(client) {
653
746
  return {