@wtflabs/x402 0.0.1-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 (96) hide show
  1. package/README.md +60 -0
  2. package/dist/cjs/client/index.d.ts +64 -0
  3. package/dist/cjs/client/index.js +832 -0
  4. package/dist/cjs/client/index.js.map +1 -0
  5. package/dist/cjs/config-CFBSAuxW.d.ts +10 -0
  6. package/dist/cjs/config-Dfuvno71.d.ts +19 -0
  7. package/dist/cjs/facilitator/index.d.ts +42 -0
  8. package/dist/cjs/facilitator/index.js +2574 -0
  9. package/dist/cjs/facilitator/index.js.map +1 -0
  10. package/dist/cjs/index.d.ts +16 -0
  11. package/dist/cjs/index.js +2974 -0
  12. package/dist/cjs/index.js.map +1 -0
  13. package/dist/cjs/middleware-6_1ApcJn.d.ts +93 -0
  14. package/dist/cjs/middleware-B_ewwsQp.d.ts +93 -0
  15. package/dist/cjs/middleware-Brgsx32F.d.ts +93 -0
  16. package/dist/cjs/middleware-BwfW7mAs.d.ts +93 -0
  17. package/dist/cjs/middleware-CQb61c1k.d.ts +93 -0
  18. package/dist/cjs/middleware-DB9lqy9f.d.ts +93 -0
  19. package/dist/cjs/middleware-DcHctwQV.d.ts +93 -0
  20. package/dist/cjs/middleware-De0jD3Bp.d.ts +93 -0
  21. package/dist/cjs/middleware-HoFOmpgv.d.ts +93 -0
  22. package/dist/cjs/middleware-Y8AiAfYw.d.ts +93 -0
  23. package/dist/cjs/middleware-pnres9YM.d.ts +93 -0
  24. package/dist/cjs/network-FrFmmiyj.d.ts +11 -0
  25. package/dist/cjs/network-RtNddYQk.d.ts +11 -0
  26. package/dist/cjs/paywall/index.d.ts +30 -0
  27. package/dist/cjs/paywall/index.js +131 -0
  28. package/dist/cjs/paywall/index.js.map +1 -0
  29. package/dist/cjs/rpc-BMvnNNHd.d.ts +35 -0
  30. package/dist/cjs/rpc-Ca8eHCWz.d.ts +35 -0
  31. package/dist/cjs/schemes/index.d.ts +533 -0
  32. package/dist/cjs/schemes/index.js +3144 -0
  33. package/dist/cjs/schemes/index.js.map +1 -0
  34. package/dist/cjs/shared/evm/index.d.ts +71 -0
  35. package/dist/cjs/shared/evm/index.js +947 -0
  36. package/dist/cjs/shared/evm/index.js.map +1 -0
  37. package/dist/cjs/shared/index.d.ts +182 -0
  38. package/dist/cjs/shared/index.js +674 -0
  39. package/dist/cjs/shared/index.js.map +1 -0
  40. package/dist/cjs/types/index.d.ts +1515 -0
  41. package/dist/cjs/types/index.js +1645 -0
  42. package/dist/cjs/types/index.js.map +1 -0
  43. package/dist/cjs/verify/index.d.ts +7 -0
  44. package/dist/cjs/verify/index.js +438 -0
  45. package/dist/cjs/verify/index.js.map +1 -0
  46. package/dist/cjs/wallet-BRWfOM5D.d.ts +153 -0
  47. package/dist/cjs/wallet-BTqCm9Zp.d.ts +27 -0
  48. package/dist/cjs/wallet-BYRAGtOB.d.ts +153 -0
  49. package/dist/cjs/wallet-BmEtlgEf.d.ts +48 -0
  50. package/dist/cjs/wallet-CNOAmyZ6.d.ts +48 -0
  51. package/dist/cjs/wallet-CQ0Fe_M5.d.ts +88 -0
  52. package/dist/cjs/wallet-D1SoxFTw.d.ts +48 -0
  53. package/dist/cjs/wallet-SJ-hbjm9.d.ts +153 -0
  54. package/dist/cjs/wallet-SJKJpUgQ.d.ts +167 -0
  55. package/dist/cjs/wallet-ecnda4Aj.d.ts +48 -0
  56. package/dist/cjs/wallet-gP8Qoi-c.d.ts +74 -0
  57. package/dist/cjs/x402Specs-B7InXo2L.d.ts +1065 -0
  58. package/dist/cjs/x402Specs-BLH3j34O.d.ts +1696 -0
  59. package/dist/cjs/x402Specs-C7LipAZg.d.ts +1715 -0
  60. package/dist/cjs/x402Specs-CYq5tSY1.d.ts +1745 -0
  61. package/dist/cjs/x402Specs-CeajqonG.d.ts +1696 -0
  62. package/dist/cjs/x402Specs-qMujgEV5.d.ts +1715 -0
  63. package/dist/cjs/x402Specs-qUBCpcuz.d.ts +1715 -0
  64. package/dist/esm/chunk-34YNR4LY.mjs +106 -0
  65. package/dist/esm/chunk-34YNR4LY.mjs.map +1 -0
  66. package/dist/esm/chunk-57UEJN5U.mjs +1416 -0
  67. package/dist/esm/chunk-57UEJN5U.mjs.map +1 -0
  68. package/dist/esm/chunk-5LTKIVOA.mjs +858 -0
  69. package/dist/esm/chunk-5LTKIVOA.mjs.map +1 -0
  70. package/dist/esm/chunk-AQQR4PXH.mjs +80 -0
  71. package/dist/esm/chunk-AQQR4PXH.mjs.map +1 -0
  72. package/dist/esm/chunk-LGXWNXCO.mjs +76 -0
  73. package/dist/esm/chunk-LGXWNXCO.mjs.map +1 -0
  74. package/dist/esm/chunk-RX2JKK4O.mjs +349 -0
  75. package/dist/esm/chunk-RX2JKK4O.mjs.map +1 -0
  76. package/dist/esm/chunk-UCBE7FDY.mjs +1041 -0
  77. package/dist/esm/chunk-UCBE7FDY.mjs.map +1 -0
  78. package/dist/esm/client/index.mjs +17 -0
  79. package/dist/esm/client/index.mjs.map +1 -0
  80. package/dist/esm/facilitator/index.mjs +14 -0
  81. package/dist/esm/facilitator/index.mjs.map +1 -0
  82. package/dist/esm/index.mjs +28 -0
  83. package/dist/esm/index.mjs.map +1 -0
  84. package/dist/esm/paywall/index.mjs +46 -0
  85. package/dist/esm/paywall/index.mjs.map +1 -0
  86. package/dist/esm/schemes/index.mjs +16 -0
  87. package/dist/esm/schemes/index.mjs.map +1 -0
  88. package/dist/esm/shared/evm/index.mjs +20 -0
  89. package/dist/esm/shared/evm/index.mjs.map +1 -0
  90. package/dist/esm/shared/index.mjs +31 -0
  91. package/dist/esm/shared/index.mjs.map +1 -0
  92. package/dist/esm/types/index.mjs +87 -0
  93. package/dist/esm/types/index.mjs.map +1 -0
  94. package/dist/esm/verify/index.mjs +105 -0
  95. package/dist/esm/verify/index.mjs.map +1 -0
  96. package/package.json +148 -0
@@ -0,0 +1,3144 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/schemes/index.ts
21
+ var schemes_exports = {};
22
+ __export(schemes_exports, {
23
+ decodePayment: () => decodePayment,
24
+ encodePayment: () => encodePayment,
25
+ exact: () => exact_exports
26
+ });
27
+ module.exports = __toCommonJS(schemes_exports);
28
+
29
+ // src/schemes/exact/index.ts
30
+ var exact_exports = {};
31
+ __export(exact_exports, {
32
+ SCHEME: () => SCHEME,
33
+ evm: () => evm_exports2,
34
+ svm: () => svm_exports3
35
+ });
36
+
37
+ // src/schemes/exact/evm/index.ts
38
+ var evm_exports2 = {};
39
+ __export(evm_exports2, {
40
+ decodePayment: () => decodePayment,
41
+ eip3009: () => eip3009_exports,
42
+ encodePayment: () => encodePayment,
43
+ permit: () => permit_exports,
44
+ permit2: () => permit2_exports,
45
+ settle: () => settle4,
46
+ verify: () => verify4
47
+ });
48
+
49
+ // src/schemes/exact/evm/eip3009/facilitator.ts
50
+ var import_viem2 = require("viem");
51
+
52
+ // src/shared/base64.ts
53
+ var Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;
54
+ function safeBase64Encode(data) {
55
+ if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
56
+ return globalThis.btoa(data);
57
+ }
58
+ return Buffer.from(data).toString("base64");
59
+ }
60
+ function safeBase64Decode(data) {
61
+ if (typeof globalThis !== "undefined" && typeof globalThis.atob === "function") {
62
+ return globalThis.atob(data);
63
+ }
64
+ return Buffer.from(data, "base64").toString("utf-8");
65
+ }
66
+
67
+ // src/types/shared/money.ts
68
+ var import_zod = require("zod");
69
+ var moneySchema = import_zod.z.union([import_zod.z.string().transform((x) => x.replace(/[^0-9.-]+/g, "")), import_zod.z.number()]).pipe(import_zod.z.coerce.number().min(1e-4).max(999999999));
70
+
71
+ // src/types/shared/network.ts
72
+ var import_zod2 = require("zod");
73
+ var NetworkSchema = import_zod2.z.enum([
74
+ "base-sepolia",
75
+ "base",
76
+ "avalanche-fuji",
77
+ "avalanche",
78
+ "iotex",
79
+ "solana-devnet",
80
+ "solana",
81
+ "sei",
82
+ "sei-testnet",
83
+ "polygon",
84
+ "polygon-amoy",
85
+ "peaq",
86
+ "bsc",
87
+ "bsc-testnet"
88
+ ]);
89
+ var SupportedEVMNetworks = [
90
+ "base-sepolia",
91
+ "base",
92
+ "avalanche-fuji",
93
+ "avalanche",
94
+ "iotex",
95
+ "sei",
96
+ "sei-testnet",
97
+ "polygon",
98
+ "polygon-amoy",
99
+ "peaq",
100
+ "bsc",
101
+ "bsc-testnet"
102
+ ];
103
+ var EvmNetworkToChainId = /* @__PURE__ */ new Map([
104
+ ["base-sepolia", 84532],
105
+ ["base", 8453],
106
+ ["avalanche-fuji", 43113],
107
+ ["avalanche", 43114],
108
+ ["iotex", 4689],
109
+ ["sei", 1329],
110
+ ["sei-testnet", 1328],
111
+ ["polygon", 137],
112
+ ["polygon-amoy", 80002],
113
+ ["peaq", 3338],
114
+ ["bsc", 56],
115
+ ["bsc-testnet", 97]
116
+ ]);
117
+ var SupportedSVMNetworks = ["solana-devnet", "solana"];
118
+ var SvmNetworkToChainId = /* @__PURE__ */ new Map([
119
+ ["solana-devnet", 103],
120
+ ["solana", 101]
121
+ ]);
122
+ var ChainIdToNetwork = Object.fromEntries(
123
+ [...SupportedEVMNetworks, ...SupportedSVMNetworks].map((network) => [
124
+ EvmNetworkToChainId.get(network),
125
+ network
126
+ ])
127
+ );
128
+
129
+ // src/types/shared/evm/wallet.ts
130
+ var import_viem = require("viem");
131
+ var import_chains = require("viem/chains");
132
+ var import_accounts = require("viem/accounts");
133
+ function isSignerWallet(wallet) {
134
+ return typeof wallet === "object" && wallet !== null && "chain" in wallet && "transport" in wallet;
135
+ }
136
+ function isAccount(wallet) {
137
+ const w = wallet;
138
+ return typeof wallet === "object" && wallet !== null && typeof w.address === "string" && typeof w.type === "string" && // Check for essential signing capabilities
139
+ typeof w.sign === "function" && typeof w.signMessage === "function" && typeof w.signTypedData === "function" && // Check for transaction signing (required by LocalAccount)
140
+ typeof w.signTransaction === "function";
141
+ }
142
+
143
+ // src/shared/svm/wallet.ts
144
+ var import_kit2 = require("@solana/kit");
145
+ var import_base = require("@scure/base");
146
+
147
+ // src/shared/svm/rpc.ts
148
+ var import_kit = require("@solana/kit");
149
+ var DEVNET_RPC_URL = "https://api.devnet.solana.com";
150
+ var MAINNET_RPC_URL = "https://api.mainnet-beta.solana.com";
151
+ var DEVNET_WS_URL = "wss://api.devnet.solana.com";
152
+ var MAINNET_WS_URL = "wss://api.mainnet-beta.solana.com";
153
+ function createDevnetRpcClient(url) {
154
+ return (0, import_kit.createSolanaRpc)(
155
+ url ? (0, import_kit.devnet)(url) : (0, import_kit.devnet)(DEVNET_RPC_URL)
156
+ );
157
+ }
158
+ function createMainnetRpcClient(url) {
159
+ return (0, import_kit.createSolanaRpc)(
160
+ url ? (0, import_kit.mainnet)(url) : (0, import_kit.mainnet)(MAINNET_RPC_URL)
161
+ );
162
+ }
163
+ function getRpcClient(network, url) {
164
+ if (network === "solana-devnet") {
165
+ return createDevnetRpcClient(url);
166
+ } else if (network === "solana") {
167
+ return createMainnetRpcClient(url);
168
+ } else {
169
+ throw new Error("Invalid network");
170
+ }
171
+ }
172
+ function getRpcSubscriptions(network, url) {
173
+ if (network === "solana-devnet") {
174
+ return (0, import_kit.createSolanaRpcSubscriptions)((0, import_kit.devnet)(url ? httpToWs(url) : DEVNET_WS_URL));
175
+ } else if (network === "solana") {
176
+ return (0, import_kit.createSolanaRpcSubscriptions)((0, import_kit.mainnet)(url ? httpToWs(url) : MAINNET_WS_URL));
177
+ } else {
178
+ throw new Error("Invalid network");
179
+ }
180
+ }
181
+ function httpToWs(url) {
182
+ if (url.startsWith("http")) {
183
+ return url.replace("http", "ws");
184
+ }
185
+ return url;
186
+ }
187
+
188
+ // src/types/shared/evm/config.ts
189
+ var config = {
190
+ "84532": {
191
+ usdcAddress: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
192
+ usdcName: "USDC"
193
+ },
194
+ "8453": {
195
+ usdcAddress: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
196
+ usdcName: "USD Coin"
197
+ },
198
+ "43113": {
199
+ usdcAddress: "0x5425890298aed601595a70AB815c96711a31Bc65",
200
+ usdcName: "USD Coin"
201
+ },
202
+ "43114": {
203
+ usdcAddress: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
204
+ usdcName: "USD Coin"
205
+ },
206
+ "4689": {
207
+ usdcAddress: "0xcdf79194c6c285077a58da47641d4dbe51f63542",
208
+ usdcName: "Bridged USDC"
209
+ },
210
+ // solana devnet
211
+ "103": {
212
+ usdcAddress: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
213
+ usdcName: "USDC"
214
+ },
215
+ // solana mainnet
216
+ "101": {
217
+ usdcAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
218
+ usdcName: "USDC"
219
+ },
220
+ "1328": {
221
+ usdcAddress: "0x4fcf1784b31630811181f670aea7a7bef803eaed",
222
+ usdcName: "USDC"
223
+ },
224
+ "1329": {
225
+ usdcAddress: "0xe15fc38f6d8c56af07bbcbe3baf5708a2bf42392",
226
+ usdcName: "USDC"
227
+ },
228
+ "137": {
229
+ usdcAddress: "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359",
230
+ usdcName: "USD Coin"
231
+ },
232
+ "80002": {
233
+ usdcAddress: "0x41E94Eb019C0762f9Bfcf9Fb1E58725BfB0e7582",
234
+ usdcName: "USDC"
235
+ },
236
+ "3338": {
237
+ usdcAddress: "0xbbA60da06c2c5424f03f7434542280FCAd453d10",
238
+ usdcName: "USDC"
239
+ },
240
+ "56": {
241
+ usdcAddress: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
242
+ usdcName: "USDC"
243
+ },
244
+ "97": {
245
+ usdcAddress: "0x64544969ed7EBf5f083679233325356EbE738930",
246
+ usdcName: "USDC"
247
+ }
248
+ };
249
+
250
+ // src/types/shared/evm/eip3009.ts
251
+ var authorizationTypes = {
252
+ TransferWithAuthorization: [
253
+ { name: "from", type: "address" },
254
+ { name: "to", type: "address" },
255
+ { name: "value", type: "uint256" },
256
+ { name: "validAfter", type: "uint256" },
257
+ { name: "validBefore", type: "uint256" },
258
+ { name: "nonce", type: "bytes32" }
259
+ ]
260
+ };
261
+
262
+ // src/types/shared/evm/eip2612.ts
263
+ var permitTypes = {
264
+ Permit: [
265
+ { name: "owner", type: "address" },
266
+ { name: "spender", type: "address" },
267
+ { name: "value", type: "uint256" },
268
+ { name: "nonce", type: "uint256" },
269
+ { name: "deadline", type: "uint256" }
270
+ ]
271
+ };
272
+ var erc20PermitABI = [
273
+ {
274
+ inputs: [
275
+ { internalType: "address", name: "owner", type: "address" },
276
+ { internalType: "address", name: "spender", type: "address" },
277
+ { internalType: "uint256", name: "value", type: "uint256" },
278
+ { internalType: "uint256", name: "deadline", type: "uint256" },
279
+ { internalType: "uint8", name: "v", type: "uint8" },
280
+ { internalType: "bytes32", name: "r", type: "bytes32" },
281
+ { internalType: "bytes32", name: "s", type: "bytes32" }
282
+ ],
283
+ name: "permit",
284
+ outputs: [],
285
+ stateMutability: "nonpayable",
286
+ type: "function"
287
+ },
288
+ {
289
+ inputs: [{ internalType: "address", name: "owner", type: "address" }],
290
+ name: "nonces",
291
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
292
+ stateMutability: "view",
293
+ type: "function"
294
+ },
295
+ {
296
+ inputs: [],
297
+ name: "name",
298
+ outputs: [{ internalType: "string", name: "", type: "string" }],
299
+ stateMutability: "view",
300
+ type: "function"
301
+ },
302
+ {
303
+ inputs: [],
304
+ name: "DOMAIN_SEPARATOR",
305
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
306
+ stateMutability: "view",
307
+ type: "function"
308
+ },
309
+ {
310
+ inputs: [{ internalType: "address", name: "account", type: "address" }],
311
+ name: "balanceOf",
312
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
313
+ stateMutability: "view",
314
+ type: "function"
315
+ },
316
+ {
317
+ inputs: [
318
+ { internalType: "address", name: "from", type: "address" },
319
+ { internalType: "address", name: "to", type: "address" },
320
+ { internalType: "uint256", name: "amount", type: "uint256" }
321
+ ],
322
+ name: "transferFrom",
323
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
324
+ stateMutability: "nonpayable",
325
+ type: "function"
326
+ },
327
+ {
328
+ inputs: [
329
+ { internalType: "address", name: "owner", type: "address" },
330
+ { internalType: "address", name: "spender", type: "address" }
331
+ ],
332
+ name: "allowance",
333
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
334
+ stateMutability: "view",
335
+ type: "function"
336
+ },
337
+ {
338
+ inputs: [],
339
+ name: "version",
340
+ outputs: [{ internalType: "string", name: "", type: "string" }],
341
+ stateMutability: "view",
342
+ type: "function"
343
+ }
344
+ ];
345
+
346
+ // src/types/shared/evm/permit2.ts
347
+ var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
348
+ var permit2Types = {
349
+ PermitTransferFrom: [
350
+ { name: "permitted", type: "TokenPermissions" },
351
+ { name: "spender", type: "address" },
352
+ { name: "nonce", type: "uint256" },
353
+ { name: "deadline", type: "uint256" }
354
+ ],
355
+ TokenPermissions: [
356
+ { name: "token", type: "address" },
357
+ { name: "amount", type: "uint256" }
358
+ ]
359
+ };
360
+ var permit2ABI = [
361
+ {
362
+ inputs: [
363
+ {
364
+ components: [
365
+ {
366
+ components: [
367
+ { internalType: "address", name: "token", type: "address" },
368
+ { internalType: "uint256", name: "amount", type: "uint256" }
369
+ ],
370
+ internalType: "struct ISignatureTransfer.TokenPermissions",
371
+ name: "permitted",
372
+ type: "tuple"
373
+ },
374
+ { internalType: "uint256", name: "nonce", type: "uint256" },
375
+ { internalType: "uint256", name: "deadline", type: "uint256" }
376
+ ],
377
+ internalType: "struct ISignatureTransfer.PermitTransferFrom",
378
+ name: "permit",
379
+ type: "tuple"
380
+ },
381
+ {
382
+ components: [
383
+ { internalType: "address", name: "to", type: "address" },
384
+ { internalType: "uint256", name: "requestedAmount", type: "uint256" }
385
+ ],
386
+ internalType: "struct ISignatureTransfer.SignatureTransferDetails",
387
+ name: "transferDetails",
388
+ type: "tuple"
389
+ },
390
+ { internalType: "address", name: "owner", type: "address" },
391
+ { internalType: "bytes", name: "signature", type: "bytes" }
392
+ ],
393
+ name: "permitTransferFrom",
394
+ outputs: [],
395
+ stateMutability: "nonpayable",
396
+ type: "function"
397
+ },
398
+ {
399
+ inputs: [
400
+ { internalType: "address", name: "owner", type: "address" },
401
+ { internalType: "address", name: "token", type: "address" },
402
+ { internalType: "address", name: "spender", type: "address" }
403
+ ],
404
+ name: "allowance",
405
+ outputs: [
406
+ { internalType: "uint160", name: "amount", type: "uint160" },
407
+ { internalType: "uint48", name: "expiration", type: "uint48" },
408
+ { internalType: "uint48", name: "nonce", type: "uint48" }
409
+ ],
410
+ stateMutability: "view",
411
+ type: "function"
412
+ },
413
+ {
414
+ inputs: [],
415
+ name: "DOMAIN_SEPARATOR",
416
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
417
+ stateMutability: "view",
418
+ type: "function"
419
+ },
420
+ {
421
+ inputs: [
422
+ { internalType: "address", name: "owner", type: "address" },
423
+ { internalType: "uint256", name: "wordPos", type: "uint256" }
424
+ ],
425
+ name: "nonceBitmap",
426
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
427
+ stateMutability: "view",
428
+ type: "function"
429
+ }
430
+ ];
431
+
432
+ // src/types/shared/evm/erc20PermitABI.ts
433
+ var usdcABI = [
434
+ {
435
+ anonymous: false,
436
+ inputs: [
437
+ {
438
+ indexed: true,
439
+ internalType: "address",
440
+ name: "owner",
441
+ type: "address"
442
+ },
443
+ {
444
+ indexed: true,
445
+ internalType: "address",
446
+ name: "spender",
447
+ type: "address"
448
+ },
449
+ {
450
+ indexed: false,
451
+ internalType: "uint256",
452
+ name: "value",
453
+ type: "uint256"
454
+ }
455
+ ],
456
+ name: "Approval",
457
+ type: "event"
458
+ },
459
+ {
460
+ anonymous: false,
461
+ inputs: [
462
+ {
463
+ indexed: true,
464
+ internalType: "address",
465
+ name: "authorizer",
466
+ type: "address"
467
+ },
468
+ {
469
+ indexed: true,
470
+ internalType: "bytes32",
471
+ name: "nonce",
472
+ type: "bytes32"
473
+ }
474
+ ],
475
+ name: "AuthorizationCanceled",
476
+ type: "event"
477
+ },
478
+ {
479
+ anonymous: false,
480
+ inputs: [
481
+ {
482
+ indexed: true,
483
+ internalType: "address",
484
+ name: "authorizer",
485
+ type: "address"
486
+ },
487
+ {
488
+ indexed: true,
489
+ internalType: "bytes32",
490
+ name: "nonce",
491
+ type: "bytes32"
492
+ }
493
+ ],
494
+ name: "AuthorizationUsed",
495
+ type: "event"
496
+ },
497
+ {
498
+ anonymous: false,
499
+ inputs: [
500
+ {
501
+ indexed: true,
502
+ internalType: "address",
503
+ name: "_account",
504
+ type: "address"
505
+ }
506
+ ],
507
+ name: "Blacklisted",
508
+ type: "event"
509
+ },
510
+ {
511
+ anonymous: false,
512
+ inputs: [
513
+ {
514
+ indexed: true,
515
+ internalType: "address",
516
+ name: "newBlacklister",
517
+ type: "address"
518
+ }
519
+ ],
520
+ name: "BlacklisterChanged",
521
+ type: "event"
522
+ },
523
+ {
524
+ anonymous: false,
525
+ inputs: [
526
+ {
527
+ indexed: true,
528
+ internalType: "address",
529
+ name: "burner",
530
+ type: "address"
531
+ },
532
+ {
533
+ indexed: false,
534
+ internalType: "uint256",
535
+ name: "amount",
536
+ type: "uint256"
537
+ }
538
+ ],
539
+ name: "Burn",
540
+ type: "event"
541
+ },
542
+ {
543
+ anonymous: false,
544
+ inputs: [
545
+ {
546
+ indexed: true,
547
+ internalType: "address",
548
+ name: "newMasterMinter",
549
+ type: "address"
550
+ }
551
+ ],
552
+ name: "MasterMinterChanged",
553
+ type: "event"
554
+ },
555
+ {
556
+ anonymous: false,
557
+ inputs: [
558
+ {
559
+ indexed: true,
560
+ internalType: "address",
561
+ name: "minter",
562
+ type: "address"
563
+ },
564
+ { indexed: true, internalType: "address", name: "to", type: "address" },
565
+ {
566
+ indexed: false,
567
+ internalType: "uint256",
568
+ name: "amount",
569
+ type: "uint256"
570
+ }
571
+ ],
572
+ name: "Mint",
573
+ type: "event"
574
+ },
575
+ {
576
+ anonymous: false,
577
+ inputs: [
578
+ {
579
+ indexed: true,
580
+ internalType: "address",
581
+ name: "minter",
582
+ type: "address"
583
+ },
584
+ {
585
+ indexed: false,
586
+ internalType: "uint256",
587
+ name: "minterAllowedAmount",
588
+ type: "uint256"
589
+ }
590
+ ],
591
+ name: "MinterConfigured",
592
+ type: "event"
593
+ },
594
+ {
595
+ anonymous: false,
596
+ inputs: [
597
+ {
598
+ indexed: true,
599
+ internalType: "address",
600
+ name: "oldMinter",
601
+ type: "address"
602
+ }
603
+ ],
604
+ name: "MinterRemoved",
605
+ type: "event"
606
+ },
607
+ {
608
+ anonymous: false,
609
+ inputs: [
610
+ {
611
+ indexed: false,
612
+ internalType: "address",
613
+ name: "previousOwner",
614
+ type: "address"
615
+ },
616
+ {
617
+ indexed: false,
618
+ internalType: "address",
619
+ name: "newOwner",
620
+ type: "address"
621
+ }
622
+ ],
623
+ name: "OwnershipTransferred",
624
+ type: "event"
625
+ },
626
+ { anonymous: false, inputs: [], name: "Pause", type: "event" },
627
+ {
628
+ anonymous: false,
629
+ inputs: [
630
+ {
631
+ indexed: true,
632
+ internalType: "address",
633
+ name: "newAddress",
634
+ type: "address"
635
+ }
636
+ ],
637
+ name: "PauserChanged",
638
+ type: "event"
639
+ },
640
+ {
641
+ anonymous: false,
642
+ inputs: [
643
+ {
644
+ indexed: true,
645
+ internalType: "address",
646
+ name: "newRescuer",
647
+ type: "address"
648
+ }
649
+ ],
650
+ name: "RescuerChanged",
651
+ type: "event"
652
+ },
653
+ {
654
+ anonymous: false,
655
+ inputs: [
656
+ { indexed: true, internalType: "address", name: "from", type: "address" },
657
+ { indexed: true, internalType: "address", name: "to", type: "address" },
658
+ {
659
+ indexed: false,
660
+ internalType: "uint256",
661
+ name: "value",
662
+ type: "uint256"
663
+ }
664
+ ],
665
+ name: "Transfer",
666
+ type: "event"
667
+ },
668
+ {
669
+ anonymous: false,
670
+ inputs: [
671
+ {
672
+ indexed: true,
673
+ internalType: "address",
674
+ name: "_account",
675
+ type: "address"
676
+ }
677
+ ],
678
+ name: "UnBlacklisted",
679
+ type: "event"
680
+ },
681
+ { anonymous: false, inputs: [], name: "Unpause", type: "event" },
682
+ {
683
+ inputs: [],
684
+ name: "CANCEL_AUTHORIZATION_TYPEHASH",
685
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
686
+ stateMutability: "view",
687
+ type: "function"
688
+ },
689
+ {
690
+ inputs: [],
691
+ name: "DOMAIN_SEPARATOR",
692
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
693
+ stateMutability: "view",
694
+ type: "function"
695
+ },
696
+ {
697
+ inputs: [],
698
+ name: "PERMIT_TYPEHASH",
699
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
700
+ stateMutability: "view",
701
+ type: "function"
702
+ },
703
+ {
704
+ inputs: [],
705
+ name: "RECEIVE_WITH_AUTHORIZATION_TYPEHASH",
706
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
707
+ stateMutability: "view",
708
+ type: "function"
709
+ },
710
+ {
711
+ inputs: [],
712
+ name: "TRANSFER_WITH_AUTHORIZATION_TYPEHASH",
713
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
714
+ stateMutability: "view",
715
+ type: "function"
716
+ },
717
+ {
718
+ inputs: [
719
+ { internalType: "address", name: "owner", type: "address" },
720
+ { internalType: "address", name: "spender", type: "address" }
721
+ ],
722
+ name: "allowance",
723
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
724
+ stateMutability: "view",
725
+ type: "function"
726
+ },
727
+ {
728
+ inputs: [
729
+ { internalType: "address", name: "spender", type: "address" },
730
+ { internalType: "uint256", name: "value", type: "uint256" }
731
+ ],
732
+ name: "approve",
733
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
734
+ stateMutability: "nonpayable",
735
+ type: "function"
736
+ },
737
+ {
738
+ inputs: [
739
+ { internalType: "address", name: "authorizer", type: "address" },
740
+ { internalType: "bytes32", name: "nonce", type: "bytes32" }
741
+ ],
742
+ name: "authorizationState",
743
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
744
+ stateMutability: "view",
745
+ type: "function"
746
+ },
747
+ {
748
+ inputs: [{ internalType: "address", name: "account", type: "address" }],
749
+ name: "balanceOf",
750
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
751
+ stateMutability: "view",
752
+ type: "function"
753
+ },
754
+ {
755
+ inputs: [{ internalType: "address", name: "_account", type: "address" }],
756
+ name: "blacklist",
757
+ outputs: [],
758
+ stateMutability: "nonpayable",
759
+ type: "function"
760
+ },
761
+ {
762
+ inputs: [],
763
+ name: "blacklister",
764
+ outputs: [{ internalType: "address", name: "", type: "address" }],
765
+ stateMutability: "view",
766
+ type: "function"
767
+ },
768
+ {
769
+ inputs: [{ internalType: "uint256", name: "_amount", type: "uint256" }],
770
+ name: "burn",
771
+ outputs: [],
772
+ stateMutability: "nonpayable",
773
+ type: "function"
774
+ },
775
+ {
776
+ inputs: [
777
+ { internalType: "address", name: "authorizer", type: "address" },
778
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
779
+ { internalType: "uint8", name: "v", type: "uint8" },
780
+ { internalType: "bytes32", name: "r", type: "bytes32" },
781
+ { internalType: "bytes32", name: "s", type: "bytes32" }
782
+ ],
783
+ name: "cancelAuthorization",
784
+ outputs: [],
785
+ stateMutability: "nonpayable",
786
+ type: "function"
787
+ },
788
+ {
789
+ inputs: [
790
+ { internalType: "address", name: "authorizer", type: "address" },
791
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
792
+ { internalType: "bytes", name: "signature", type: "bytes" }
793
+ ],
794
+ name: "cancelAuthorization",
795
+ outputs: [],
796
+ stateMutability: "nonpayable",
797
+ type: "function"
798
+ },
799
+ {
800
+ inputs: [
801
+ { internalType: "address", name: "minter", type: "address" },
802
+ { internalType: "uint256", name: "minterAllowedAmount", type: "uint256" }
803
+ ],
804
+ name: "configureMinter",
805
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
806
+ stateMutability: "nonpayable",
807
+ type: "function"
808
+ },
809
+ {
810
+ inputs: [],
811
+ name: "currency",
812
+ outputs: [{ internalType: "string", name: "", type: "string" }],
813
+ stateMutability: "view",
814
+ type: "function"
815
+ },
816
+ {
817
+ inputs: [],
818
+ name: "decimals",
819
+ outputs: [{ internalType: "uint8", name: "", type: "uint8" }],
820
+ stateMutability: "view",
821
+ type: "function"
822
+ },
823
+ {
824
+ inputs: [
825
+ { internalType: "address", name: "spender", type: "address" },
826
+ { internalType: "uint256", name: "decrement", type: "uint256" }
827
+ ],
828
+ name: "decreaseAllowance",
829
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
830
+ stateMutability: "nonpayable",
831
+ type: "function"
832
+ },
833
+ {
834
+ inputs: [
835
+ { internalType: "address", name: "spender", type: "address" },
836
+ { internalType: "uint256", name: "increment", type: "uint256" }
837
+ ],
838
+ name: "increaseAllowance",
839
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
840
+ stateMutability: "nonpayable",
841
+ type: "function"
842
+ },
843
+ {
844
+ inputs: [
845
+ { internalType: "string", name: "tokenName", type: "string" },
846
+ { internalType: "string", name: "tokenSymbol", type: "string" },
847
+ { internalType: "string", name: "tokenCurrency", type: "string" },
848
+ { internalType: "uint8", name: "tokenDecimals", type: "uint8" },
849
+ { internalType: "address", name: "newMasterMinter", type: "address" },
850
+ { internalType: "address", name: "newPauser", type: "address" },
851
+ { internalType: "address", name: "newBlacklister", type: "address" },
852
+ { internalType: "address", name: "newOwner", type: "address" }
853
+ ],
854
+ name: "initialize",
855
+ outputs: [],
856
+ stateMutability: "nonpayable",
857
+ type: "function"
858
+ },
859
+ {
860
+ inputs: [{ internalType: "string", name: "newName", type: "string" }],
861
+ name: "initializeV2",
862
+ outputs: [],
863
+ stateMutability: "nonpayable",
864
+ type: "function"
865
+ },
866
+ {
867
+ inputs: [{ internalType: "address", name: "lostAndFound", type: "address" }],
868
+ name: "initializeV2_1",
869
+ outputs: [],
870
+ stateMutability: "nonpayable",
871
+ type: "function"
872
+ },
873
+ {
874
+ inputs: [
875
+ {
876
+ internalType: "address[]",
877
+ name: "accountsToBlacklist",
878
+ type: "address[]"
879
+ },
880
+ { internalType: "string", name: "newSymbol", type: "string" }
881
+ ],
882
+ name: "initializeV2_2",
883
+ outputs: [],
884
+ stateMutability: "nonpayable",
885
+ type: "function"
886
+ },
887
+ {
888
+ inputs: [{ internalType: "address", name: "_account", type: "address" }],
889
+ name: "isBlacklisted",
890
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
891
+ stateMutability: "view",
892
+ type: "function"
893
+ },
894
+ {
895
+ inputs: [{ internalType: "address", name: "account", type: "address" }],
896
+ name: "isMinter",
897
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
898
+ stateMutability: "view",
899
+ type: "function"
900
+ },
901
+ {
902
+ inputs: [],
903
+ name: "masterMinter",
904
+ outputs: [{ internalType: "address", name: "", type: "address" }],
905
+ stateMutability: "view",
906
+ type: "function"
907
+ },
908
+ {
909
+ inputs: [
910
+ { internalType: "address", name: "_to", type: "address" },
911
+ { internalType: "uint256", name: "_amount", type: "uint256" }
912
+ ],
913
+ name: "mint",
914
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
915
+ stateMutability: "nonpayable",
916
+ type: "function"
917
+ },
918
+ {
919
+ inputs: [{ internalType: "address", name: "minter", type: "address" }],
920
+ name: "minterAllowance",
921
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
922
+ stateMutability: "view",
923
+ type: "function"
924
+ },
925
+ {
926
+ inputs: [],
927
+ name: "name",
928
+ outputs: [{ internalType: "string", name: "", type: "string" }],
929
+ stateMutability: "view",
930
+ type: "function"
931
+ },
932
+ {
933
+ inputs: [{ internalType: "address", name: "owner", type: "address" }],
934
+ name: "nonces",
935
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
936
+ stateMutability: "view",
937
+ type: "function"
938
+ },
939
+ {
940
+ inputs: [],
941
+ name: "owner",
942
+ outputs: [{ internalType: "address", name: "", type: "address" }],
943
+ stateMutability: "view",
944
+ type: "function"
945
+ },
946
+ {
947
+ inputs: [],
948
+ name: "pause",
949
+ outputs: [],
950
+ stateMutability: "nonpayable",
951
+ type: "function"
952
+ },
953
+ {
954
+ inputs: [],
955
+ name: "paused",
956
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
957
+ stateMutability: "view",
958
+ type: "function"
959
+ },
960
+ {
961
+ inputs: [],
962
+ name: "pauser",
963
+ outputs: [{ internalType: "address", name: "", type: "address" }],
964
+ stateMutability: "view",
965
+ type: "function"
966
+ },
967
+ {
968
+ inputs: [
969
+ { internalType: "address", name: "owner", type: "address" },
970
+ { internalType: "address", name: "spender", type: "address" },
971
+ { internalType: "uint256", name: "value", type: "uint256" },
972
+ { internalType: "uint256", name: "deadline", type: "uint256" },
973
+ { internalType: "bytes", name: "signature", type: "bytes" }
974
+ ],
975
+ name: "permit",
976
+ outputs: [],
977
+ stateMutability: "nonpayable",
978
+ type: "function"
979
+ },
980
+ {
981
+ inputs: [
982
+ { internalType: "address", name: "owner", type: "address" },
983
+ { internalType: "address", name: "spender", type: "address" },
984
+ { internalType: "uint256", name: "value", type: "uint256" },
985
+ { internalType: "uint256", name: "deadline", type: "uint256" },
986
+ { internalType: "uint8", name: "v", type: "uint8" },
987
+ { internalType: "bytes32", name: "r", type: "bytes32" },
988
+ { internalType: "bytes32", name: "s", type: "bytes32" }
989
+ ],
990
+ name: "permit",
991
+ outputs: [],
992
+ stateMutability: "nonpayable",
993
+ type: "function"
994
+ },
995
+ {
996
+ inputs: [
997
+ { internalType: "address", name: "from", type: "address" },
998
+ { internalType: "address", name: "to", type: "address" },
999
+ { internalType: "uint256", name: "value", type: "uint256" },
1000
+ { internalType: "uint256", name: "validAfter", type: "uint256" },
1001
+ { internalType: "uint256", name: "validBefore", type: "uint256" },
1002
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
1003
+ { internalType: "bytes", name: "signature", type: "bytes" }
1004
+ ],
1005
+ name: "receiveWithAuthorization",
1006
+ outputs: [],
1007
+ stateMutability: "nonpayable",
1008
+ type: "function"
1009
+ },
1010
+ {
1011
+ inputs: [
1012
+ { internalType: "address", name: "from", type: "address" },
1013
+ { internalType: "address", name: "to", type: "address" },
1014
+ { internalType: "uint256", name: "value", type: "uint256" },
1015
+ { internalType: "uint256", name: "validAfter", type: "uint256" },
1016
+ { internalType: "uint256", name: "validBefore", type: "uint256" },
1017
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
1018
+ { internalType: "uint8", name: "v", type: "uint8" },
1019
+ { internalType: "bytes32", name: "r", type: "bytes32" },
1020
+ { internalType: "bytes32", name: "s", type: "bytes32" }
1021
+ ],
1022
+ name: "receiveWithAuthorization",
1023
+ outputs: [],
1024
+ stateMutability: "nonpayable",
1025
+ type: "function"
1026
+ },
1027
+ {
1028
+ inputs: [{ internalType: "address", name: "minter", type: "address" }],
1029
+ name: "removeMinter",
1030
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
1031
+ stateMutability: "nonpayable",
1032
+ type: "function"
1033
+ },
1034
+ {
1035
+ inputs: [
1036
+ {
1037
+ internalType: "contract IERC20",
1038
+ name: "tokenContract",
1039
+ type: "address"
1040
+ },
1041
+ { internalType: "address", name: "to", type: "address" },
1042
+ { internalType: "uint256", name: "amount", type: "uint256" }
1043
+ ],
1044
+ name: "rescueERC20",
1045
+ outputs: [],
1046
+ stateMutability: "nonpayable",
1047
+ type: "function"
1048
+ },
1049
+ {
1050
+ inputs: [],
1051
+ name: "rescuer",
1052
+ outputs: [{ internalType: "address", name: "", type: "address" }],
1053
+ stateMutability: "view",
1054
+ type: "function"
1055
+ },
1056
+ {
1057
+ inputs: [],
1058
+ name: "symbol",
1059
+ outputs: [{ internalType: "string", name: "", type: "string" }],
1060
+ stateMutability: "view",
1061
+ type: "function"
1062
+ },
1063
+ {
1064
+ inputs: [],
1065
+ name: "totalSupply",
1066
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
1067
+ stateMutability: "view",
1068
+ type: "function"
1069
+ },
1070
+ {
1071
+ inputs: [
1072
+ { internalType: "address", name: "to", type: "address" },
1073
+ { internalType: "uint256", name: "value", type: "uint256" }
1074
+ ],
1075
+ name: "transfer",
1076
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
1077
+ stateMutability: "nonpayable",
1078
+ type: "function"
1079
+ },
1080
+ {
1081
+ inputs: [
1082
+ { internalType: "address", name: "from", type: "address" },
1083
+ { internalType: "address", name: "to", type: "address" },
1084
+ { internalType: "uint256", name: "value", type: "uint256" }
1085
+ ],
1086
+ name: "transferFrom",
1087
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
1088
+ stateMutability: "nonpayable",
1089
+ type: "function"
1090
+ },
1091
+ {
1092
+ inputs: [{ internalType: "address", name: "newOwner", type: "address" }],
1093
+ name: "transferOwnership",
1094
+ outputs: [],
1095
+ stateMutability: "nonpayable",
1096
+ type: "function"
1097
+ },
1098
+ {
1099
+ inputs: [
1100
+ { internalType: "address", name: "from", type: "address" },
1101
+ { internalType: "address", name: "to", type: "address" },
1102
+ { internalType: "uint256", name: "value", type: "uint256" },
1103
+ { internalType: "uint256", name: "validAfter", type: "uint256" },
1104
+ { internalType: "uint256", name: "validBefore", type: "uint256" },
1105
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
1106
+ { internalType: "bytes", name: "signature", type: "bytes" }
1107
+ ],
1108
+ name: "transferWithAuthorization",
1109
+ outputs: [],
1110
+ stateMutability: "nonpayable",
1111
+ type: "function"
1112
+ },
1113
+ {
1114
+ inputs: [
1115
+ { internalType: "address", name: "from", type: "address" },
1116
+ { internalType: "address", name: "to", type: "address" },
1117
+ { internalType: "uint256", name: "value", type: "uint256" },
1118
+ { internalType: "uint256", name: "validAfter", type: "uint256" },
1119
+ { internalType: "uint256", name: "validBefore", type: "uint256" },
1120
+ { internalType: "bytes32", name: "nonce", type: "bytes32" },
1121
+ { internalType: "uint8", name: "v", type: "uint8" },
1122
+ { internalType: "bytes32", name: "r", type: "bytes32" },
1123
+ { internalType: "bytes32", name: "s", type: "bytes32" }
1124
+ ],
1125
+ name: "transferWithAuthorization",
1126
+ outputs: [],
1127
+ stateMutability: "nonpayable",
1128
+ type: "function"
1129
+ },
1130
+ {
1131
+ inputs: [{ internalType: "address", name: "_account", type: "address" }],
1132
+ name: "unBlacklist",
1133
+ outputs: [],
1134
+ stateMutability: "nonpayable",
1135
+ type: "function"
1136
+ },
1137
+ {
1138
+ inputs: [],
1139
+ name: "unpause",
1140
+ outputs: [],
1141
+ stateMutability: "nonpayable",
1142
+ type: "function"
1143
+ },
1144
+ {
1145
+ inputs: [{ internalType: "address", name: "_newBlacklister", type: "address" }],
1146
+ name: "updateBlacklister",
1147
+ outputs: [],
1148
+ stateMutability: "nonpayable",
1149
+ type: "function"
1150
+ },
1151
+ {
1152
+ inputs: [{ internalType: "address", name: "_newMasterMinter", type: "address" }],
1153
+ name: "updateMasterMinter",
1154
+ outputs: [],
1155
+ stateMutability: "nonpayable",
1156
+ type: "function"
1157
+ },
1158
+ {
1159
+ inputs: [{ internalType: "address", name: "_newPauser", type: "address" }],
1160
+ name: "updatePauser",
1161
+ outputs: [],
1162
+ stateMutability: "nonpayable",
1163
+ type: "function"
1164
+ },
1165
+ {
1166
+ inputs: [{ internalType: "address", name: "newRescuer", type: "address" }],
1167
+ name: "updateRescuer",
1168
+ outputs: [],
1169
+ stateMutability: "nonpayable",
1170
+ type: "function"
1171
+ },
1172
+ {
1173
+ inputs: [],
1174
+ name: "version",
1175
+ outputs: [{ internalType: "string", name: "", type: "string" }],
1176
+ stateMutability: "pure",
1177
+ type: "function"
1178
+ }
1179
+ ];
1180
+
1181
+ // src/types/shared/svm/regex.ts
1182
+ var SvmAddressRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
1183
+
1184
+ // src/shared/network.ts
1185
+ function getNetworkId(network) {
1186
+ if (EvmNetworkToChainId.has(network)) {
1187
+ return EvmNetworkToChainId.get(network);
1188
+ }
1189
+ if (SvmNetworkToChainId.has(network)) {
1190
+ return SvmNetworkToChainId.get(network);
1191
+ }
1192
+ throw new Error(`Unsupported network: ${network}`);
1193
+ }
1194
+
1195
+ // src/types/verify/x402Specs.ts
1196
+ var import_zod3 = require("zod");
1197
+ var EvmMaxAtomicUnits = 40;
1198
+ var EvmAddressRegex = /^0x[0-9a-fA-F]{40}$/;
1199
+ var MixedAddressRegex = /^0x[a-fA-F0-9]{40}|[A-Za-z0-9][A-Za-z0-9-]{0,34}[A-Za-z0-9]$/;
1200
+ var HexEncoded64ByteRegex = /^0x[0-9a-fA-F]{64}$/;
1201
+ var EvmSignatureRegex = /^0x[0-9a-fA-F]+$/;
1202
+ var schemes = ["exact"];
1203
+ var x402Versions = [1];
1204
+ var ErrorReasons = [
1205
+ "insufficient_funds",
1206
+ "invalid_exact_evm_payload_authorization_valid_after",
1207
+ "invalid_exact_evm_payload_authorization_valid_before",
1208
+ "invalid_exact_evm_payload_authorization_value",
1209
+ "invalid_exact_evm_payload_signature",
1210
+ "invalid_exact_evm_payload_recipient_mismatch",
1211
+ "invalid_exact_svm_payload_transaction",
1212
+ "invalid_exact_svm_payload_transaction_amount_mismatch",
1213
+ "invalid_exact_svm_payload_transaction_create_ata_instruction",
1214
+ "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee",
1215
+ "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset",
1216
+ "invalid_exact_svm_payload_transaction_instructions",
1217
+ "invalid_exact_svm_payload_transaction_instructions_length",
1218
+ "invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction",
1219
+ "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction",
1220
+ "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high",
1221
+ "invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked",
1222
+ "invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked",
1223
+ "invalid_exact_svm_payload_transaction_not_a_transfer_instruction",
1224
+ "invalid_exact_svm_payload_transaction_receiver_ata_not_found",
1225
+ "invalid_exact_svm_payload_transaction_sender_ata_not_found",
1226
+ "invalid_exact_svm_payload_transaction_simulation_failed",
1227
+ "invalid_exact_svm_payload_transaction_transfer_to_incorrect_ata",
1228
+ "invalid_network",
1229
+ "invalid_payload",
1230
+ "invalid_payment_requirements",
1231
+ "invalid_scheme",
1232
+ "invalid_payment",
1233
+ "payment_expired",
1234
+ "unsupported_scheme",
1235
+ "invalid_x402_version",
1236
+ "invalid_transaction_state",
1237
+ "settle_exact_svm_block_height_exceeded",
1238
+ "settle_exact_svm_transaction_confirmation_timed_out",
1239
+ "unexpected_settle_error",
1240
+ "unexpected_verify_error",
1241
+ // New error reasons for Permit and Permit2
1242
+ "unsupported_authorization_type",
1243
+ "invalid_authorization_type",
1244
+ "invalid_permit_signature",
1245
+ "invalid_permit2_signature",
1246
+ "permit_expired",
1247
+ "permit2_expired",
1248
+ "permit2_not_approved",
1249
+ "invalid_token_address",
1250
+ "invalid_spender_address",
1251
+ "token_mismatch",
1252
+ "insufficient_payment_amount",
1253
+ "transaction_failed",
1254
+ "settlement_failed"
1255
+ ];
1256
+ var isInteger = (value) => Number.isInteger(Number(value)) && Number(value) >= 0;
1257
+ var hasMaxLength = (maxLength) => (value) => value.length <= maxLength;
1258
+ var EvmOrSvmAddress = import_zod3.z.string().regex(EvmAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
1259
+ var mixedAddressOrSvmAddress = import_zod3.z.string().regex(MixedAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
1260
+ var PaymentRequirementsSchema = import_zod3.z.object({
1261
+ scheme: import_zod3.z.enum(schemes),
1262
+ network: NetworkSchema,
1263
+ maxAmountRequired: import_zod3.z.string().refine(isInteger),
1264
+ resource: import_zod3.z.string().url(),
1265
+ description: import_zod3.z.string(),
1266
+ mimeType: import_zod3.z.string(),
1267
+ outputSchema: import_zod3.z.record(import_zod3.z.any()).optional(),
1268
+ payTo: EvmOrSvmAddress,
1269
+ maxTimeoutSeconds: import_zod3.z.number().int(),
1270
+ asset: mixedAddressOrSvmAddress,
1271
+ paymentType: import_zod3.z.enum(["eip3009", "permit", "permit2"]).optional(),
1272
+ extra: import_zod3.z.record(import_zod3.z.any()).optional()
1273
+ });
1274
+ var ExactEvmPayloadAuthorizationSchema = import_zod3.z.object({
1275
+ from: import_zod3.z.string().regex(EvmAddressRegex),
1276
+ to: import_zod3.z.string().regex(EvmAddressRegex),
1277
+ value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1278
+ validAfter: import_zod3.z.string().refine(isInteger),
1279
+ validBefore: import_zod3.z.string().refine(isInteger),
1280
+ nonce: import_zod3.z.string().regex(HexEncoded64ByteRegex)
1281
+ });
1282
+ var PermitEvmPayloadAuthorizationSchema = import_zod3.z.object({
1283
+ owner: import_zod3.z.string().regex(EvmAddressRegex),
1284
+ spender: import_zod3.z.string().regex(EvmAddressRegex),
1285
+ value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1286
+ deadline: import_zod3.z.string().refine(isInteger),
1287
+ nonce: import_zod3.z.string().refine(isInteger)
1288
+ });
1289
+ var Permit2EvmPayloadAuthorizationSchema = import_zod3.z.object({
1290
+ owner: import_zod3.z.string().regex(EvmAddressRegex),
1291
+ spender: import_zod3.z.string().regex(EvmAddressRegex),
1292
+ token: import_zod3.z.string().regex(EvmAddressRegex),
1293
+ amount: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1294
+ deadline: import_zod3.z.string().refine(isInteger),
1295
+ nonce: import_zod3.z.string().refine(isInteger)
1296
+ });
1297
+ var ExactEvmPayloadSchema = import_zod3.z.discriminatedUnion("authorizationType", [
1298
+ import_zod3.z.object({
1299
+ authorizationType: import_zod3.z.literal("eip3009"),
1300
+ signature: import_zod3.z.string().regex(EvmSignatureRegex),
1301
+ authorization: ExactEvmPayloadAuthorizationSchema
1302
+ }),
1303
+ import_zod3.z.object({
1304
+ authorizationType: import_zod3.z.literal("permit"),
1305
+ signature: import_zod3.z.string().regex(EvmSignatureRegex),
1306
+ authorization: PermitEvmPayloadAuthorizationSchema
1307
+ }),
1308
+ import_zod3.z.object({
1309
+ authorizationType: import_zod3.z.literal("permit2"),
1310
+ signature: import_zod3.z.string().regex(EvmSignatureRegex),
1311
+ authorization: Permit2EvmPayloadAuthorizationSchema
1312
+ })
1313
+ ]);
1314
+ var ExactSvmPayloadSchema = import_zod3.z.object({
1315
+ transaction: import_zod3.z.string().regex(Base64EncodedRegex)
1316
+ });
1317
+ var PaymentPayloadSchema = import_zod3.z.object({
1318
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1319
+ scheme: import_zod3.z.enum(schemes),
1320
+ network: NetworkSchema,
1321
+ payload: import_zod3.z.union([ExactEvmPayloadSchema, ExactSvmPayloadSchema])
1322
+ });
1323
+ var x402ResponseSchema = import_zod3.z.object({
1324
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1325
+ error: import_zod3.z.enum(ErrorReasons).optional(),
1326
+ accepts: import_zod3.z.array(PaymentRequirementsSchema).optional(),
1327
+ payer: import_zod3.z.string().regex(MixedAddressRegex).optional()
1328
+ });
1329
+ var HTTPVerbsSchema = import_zod3.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]);
1330
+ var HTTPRequestStructureSchema = import_zod3.z.object({
1331
+ type: import_zod3.z.literal("http"),
1332
+ method: HTTPVerbsSchema,
1333
+ queryParams: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).optional(),
1334
+ bodyType: import_zod3.z.enum(["json", "form-data", "multipart-form-data", "text", "binary"]).optional(),
1335
+ bodyFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional(),
1336
+ headerFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional()
1337
+ });
1338
+ var RequestStructureSchema = import_zod3.z.discriminatedUnion("type", [
1339
+ HTTPRequestStructureSchema
1340
+ // MCPRequestStructureSchema,
1341
+ // OpenAPIRequestStructureSchema,
1342
+ ]);
1343
+ var DiscoveredResourceSchema = import_zod3.z.object({
1344
+ resource: import_zod3.z.string(),
1345
+ type: import_zod3.z.enum(["http"]),
1346
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1347
+ accepts: import_zod3.z.array(PaymentRequirementsSchema),
1348
+ lastUpdated: import_zod3.z.date(),
1349
+ metadata: import_zod3.z.record(import_zod3.z.any()).optional()
1350
+ });
1351
+ var SettleRequestSchema = import_zod3.z.object({
1352
+ paymentPayload: PaymentPayloadSchema,
1353
+ paymentRequirements: PaymentRequirementsSchema
1354
+ });
1355
+ var VerifyRequestSchema = import_zod3.z.object({
1356
+ paymentPayload: PaymentPayloadSchema,
1357
+ paymentRequirements: PaymentRequirementsSchema
1358
+ });
1359
+ var VerifyResponseSchema = import_zod3.z.object({
1360
+ isValid: import_zod3.z.boolean(),
1361
+ invalidReason: import_zod3.z.enum(ErrorReasons).optional(),
1362
+ payer: EvmOrSvmAddress.optional()
1363
+ });
1364
+ var SettleResponseSchema = import_zod3.z.object({
1365
+ success: import_zod3.z.boolean(),
1366
+ errorReason: import_zod3.z.enum(ErrorReasons).optional(),
1367
+ payer: EvmOrSvmAddress.optional(),
1368
+ transaction: import_zod3.z.string().regex(MixedAddressRegex),
1369
+ network: NetworkSchema
1370
+ });
1371
+ var ListDiscoveryResourcesRequestSchema = import_zod3.z.object({
1372
+ type: import_zod3.z.string().optional(),
1373
+ limit: import_zod3.z.number().optional(),
1374
+ offset: import_zod3.z.number().optional()
1375
+ });
1376
+ var ListDiscoveryResourcesResponseSchema = import_zod3.z.object({
1377
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1378
+ items: import_zod3.z.array(DiscoveredResourceSchema),
1379
+ pagination: import_zod3.z.object({
1380
+ limit: import_zod3.z.number(),
1381
+ offset: import_zod3.z.number(),
1382
+ total: import_zod3.z.number()
1383
+ })
1384
+ });
1385
+ var SupportedPaymentKindSchema = import_zod3.z.object({
1386
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1387
+ scheme: import_zod3.z.enum(schemes),
1388
+ network: NetworkSchema,
1389
+ extra: import_zod3.z.record(import_zod3.z.any()).optional()
1390
+ });
1391
+ var SupportedPaymentKindsResponseSchema = import_zod3.z.object({
1392
+ kinds: import_zod3.z.array(SupportedPaymentKindSchema)
1393
+ });
1394
+
1395
+ // src/types/verify/facilitator.ts
1396
+ var import_zod4 = require("zod");
1397
+ var facilitatorRequestSchema = import_zod4.z.object({
1398
+ paymentHeader: import_zod4.z.string(),
1399
+ paymentRequirements: PaymentRequirementsSchema
1400
+ });
1401
+
1402
+ // src/shared/evm/usdc.ts
1403
+ function getUsdcAddress(client) {
1404
+ return config[client.chain.id.toString()].usdcAddress;
1405
+ }
1406
+ var versionCache = /* @__PURE__ */ new Map();
1407
+ async function getVersion(client, tokenAddress) {
1408
+ const address = tokenAddress ?? getUsdcAddress(client);
1409
+ const cacheKey = `${client.chain.id}-${address.toLowerCase()}`;
1410
+ if (versionCache.has(cacheKey)) {
1411
+ return versionCache.get(cacheKey);
1412
+ }
1413
+ let version = "1";
1414
+ try {
1415
+ const eip712DomainABI = [
1416
+ {
1417
+ inputs: [],
1418
+ name: "eip712Domain",
1419
+ outputs: [
1420
+ { name: "fields", type: "bytes1" },
1421
+ { name: "name", type: "string" },
1422
+ { name: "version", type: "string" },
1423
+ { name: "chainId", type: "uint256" },
1424
+ { name: "verifyingContract", type: "address" },
1425
+ { name: "salt", type: "bytes32" },
1426
+ { name: "extensions", type: "uint256[]" }
1427
+ ],
1428
+ stateMutability: "view",
1429
+ type: "function"
1430
+ }
1431
+ ];
1432
+ const result = await client.readContract({
1433
+ address,
1434
+ abi: eip712DomainABI,
1435
+ functionName: "eip712Domain"
1436
+ });
1437
+ version = result[2];
1438
+ } catch {
1439
+ try {
1440
+ const result = await client.readContract({
1441
+ address,
1442
+ abi: usdcABI,
1443
+ functionName: "version"
1444
+ });
1445
+ version = result;
1446
+ } catch {
1447
+ console.warn(
1448
+ `Neither eip712Domain() nor version() available for token ${address}, using default: ${version}`
1449
+ );
1450
+ }
1451
+ }
1452
+ versionCache.set(cacheKey, version);
1453
+ return version;
1454
+ }
1455
+
1456
+ // src/shared/evm/erc20.ts
1457
+ async function getERC20Balance(client, erc20Address, address) {
1458
+ const balance = await client.readContract({
1459
+ address: erc20Address,
1460
+ abi: usdcABI,
1461
+ functionName: "balanceOf",
1462
+ args: [address]
1463
+ });
1464
+ return balance;
1465
+ }
1466
+ async function getERC20Allowance(client, erc20Address, owner, spender) {
1467
+ const allowance = await client.readContract({
1468
+ address: erc20Address,
1469
+ abi: usdcABI,
1470
+ functionName: "allowance",
1471
+ args: [owner, spender]
1472
+ });
1473
+ return allowance;
1474
+ }
1475
+
1476
+ // src/shared/svm/transaction.ts
1477
+ var import_kit3 = require("@solana/kit");
1478
+ var import_token = require("@solana-program/token");
1479
+ var import_token_2022 = require("@solana-program/token-2022");
1480
+ function decodeTransactionFromPayload(svmPayload) {
1481
+ try {
1482
+ const base64Encoder = (0, import_kit3.getBase64Encoder)();
1483
+ const transactionBytes = base64Encoder.encode(svmPayload.transaction);
1484
+ const transactionDecoder = (0, import_kit3.getTransactionDecoder)();
1485
+ return transactionDecoder.decode(transactionBytes);
1486
+ } catch (error) {
1487
+ console.error("error", error);
1488
+ throw new Error("invalid_exact_svm_payload_transaction");
1489
+ }
1490
+ }
1491
+ function getTokenPayerFromTransaction(transaction) {
1492
+ const compiled = (0, import_kit3.getCompiledTransactionMessageDecoder)().decode(
1493
+ transaction.messageBytes
1494
+ );
1495
+ const staticAccounts = compiled.staticAccounts ?? [];
1496
+ const instructions = compiled.instructions ?? [];
1497
+ for (const ix of instructions) {
1498
+ const programIndex = ix.programAddressIndex;
1499
+ const programAddress = staticAccounts[programIndex].toString();
1500
+ if (programAddress === import_token.TOKEN_PROGRAM_ADDRESS.toString() || programAddress === import_token_2022.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
1501
+ const accountIndices = ix.accountIndices ?? [];
1502
+ if (accountIndices.length >= 4) {
1503
+ const ownerIndex = accountIndices[3];
1504
+ const ownerAddress = staticAccounts[ownerIndex].toString();
1505
+ if (ownerAddress) return ownerAddress;
1506
+ }
1507
+ }
1508
+ }
1509
+ return "";
1510
+ }
1511
+ async function signAndSimulateTransaction(signer, transaction, rpc) {
1512
+ const signedTransaction = await (0, import_kit3.partiallySignTransaction)([signer.keyPair], transaction);
1513
+ const base64EncodedTransaction = (0, import_kit3.getBase64EncodedWireTransaction)(signedTransaction);
1514
+ const simulateTxConfig = {
1515
+ sigVerify: true,
1516
+ replaceRecentBlockhash: false,
1517
+ commitment: "confirmed",
1518
+ encoding: "base64",
1519
+ accounts: void 0,
1520
+ innerInstructions: void 0,
1521
+ minContextSlot: void 0
1522
+ };
1523
+ const simulateResult = await rpc.simulateTransaction(base64EncodedTransaction, simulateTxConfig).send();
1524
+ return simulateResult;
1525
+ }
1526
+
1527
+ // src/schemes/exact/evm/eip3009/facilitator.ts
1528
+ async function verify(client, payload, paymentRequirements) {
1529
+ const exactEvmPayload = payload.payload;
1530
+ if (exactEvmPayload.authorizationType !== "eip3009") {
1531
+ return {
1532
+ isValid: false,
1533
+ invalidReason: "unsupported_authorization_type",
1534
+ payer: ""
1535
+ };
1536
+ }
1537
+ if (payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
1538
+ return {
1539
+ isValid: false,
1540
+ invalidReason: `unsupported_scheme`,
1541
+ payer: exactEvmPayload.authorization.from
1542
+ };
1543
+ }
1544
+ let name;
1545
+ let chainId;
1546
+ let erc20Address;
1547
+ let version;
1548
+ try {
1549
+ chainId = getNetworkId(payload.network);
1550
+ name = paymentRequirements.extra?.name ?? config[chainId.toString()].usdcName;
1551
+ erc20Address = paymentRequirements.asset;
1552
+ version = paymentRequirements.extra?.version ?? await getVersion(client, erc20Address);
1553
+ } catch {
1554
+ return {
1555
+ isValid: false,
1556
+ invalidReason: `invalid_network`,
1557
+ payer: exactEvmPayload.authorization.from
1558
+ };
1559
+ }
1560
+ const permitTypedData = {
1561
+ types: authorizationTypes,
1562
+ primaryType: "TransferWithAuthorization",
1563
+ domain: {
1564
+ name,
1565
+ version,
1566
+ chainId,
1567
+ verifyingContract: erc20Address
1568
+ },
1569
+ message: {
1570
+ from: exactEvmPayload.authorization.from,
1571
+ to: exactEvmPayload.authorization.to,
1572
+ value: exactEvmPayload.authorization.value,
1573
+ validAfter: exactEvmPayload.authorization.validAfter,
1574
+ validBefore: exactEvmPayload.authorization.validBefore,
1575
+ nonce: exactEvmPayload.authorization.nonce
1576
+ }
1577
+ };
1578
+ const recoveredAddress = await client.verifyTypedData({
1579
+ address: exactEvmPayload.authorization.from,
1580
+ ...permitTypedData,
1581
+ signature: exactEvmPayload.signature
1582
+ });
1583
+ if (!recoveredAddress) {
1584
+ return {
1585
+ isValid: false,
1586
+ invalidReason: "invalid_exact_evm_payload_signature",
1587
+ //"Invalid permit signature",
1588
+ payer: exactEvmPayload.authorization.from
1589
+ };
1590
+ }
1591
+ if ((0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.payTo)) {
1592
+ return {
1593
+ isValid: false,
1594
+ invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
1595
+ payer: exactEvmPayload.authorization.from
1596
+ };
1597
+ }
1598
+ if (BigInt(exactEvmPayload.authorization.validBefore) < BigInt(Math.floor(Date.now() / 1e3) + 6)) {
1599
+ return {
1600
+ isValid: false,
1601
+ invalidReason: "invalid_exact_evm_payload_authorization_valid_before",
1602
+ //"Deadline on permit isn't far enough in the future",
1603
+ payer: exactEvmPayload.authorization.from
1604
+ };
1605
+ }
1606
+ if (BigInt(exactEvmPayload.authorization.validAfter) > BigInt(Math.floor(Date.now() / 1e3))) {
1607
+ return {
1608
+ isValid: false,
1609
+ invalidReason: "invalid_exact_evm_payload_authorization_valid_after",
1610
+ //"Deadline on permit is in the future",
1611
+ payer: exactEvmPayload.authorization.from
1612
+ };
1613
+ }
1614
+ const balance = await getERC20Balance(
1615
+ client,
1616
+ erc20Address,
1617
+ exactEvmPayload.authorization.from
1618
+ );
1619
+ if (balance < BigInt(paymentRequirements.maxAmountRequired)) {
1620
+ return {
1621
+ isValid: false,
1622
+ invalidReason: "insufficient_funds",
1623
+ //"Client does not have enough funds",
1624
+ payer: exactEvmPayload.authorization.from
1625
+ };
1626
+ }
1627
+ if (BigInt(exactEvmPayload.authorization.value) < BigInt(paymentRequirements.maxAmountRequired)) {
1628
+ return {
1629
+ isValid: false,
1630
+ invalidReason: "invalid_exact_evm_payload_authorization_value",
1631
+ //"Value in payload is not enough to cover paymentRequirements.maxAmountRequired",
1632
+ payer: exactEvmPayload.authorization.from
1633
+ };
1634
+ }
1635
+ return {
1636
+ isValid: true,
1637
+ invalidReason: void 0,
1638
+ payer: exactEvmPayload.authorization.from
1639
+ };
1640
+ }
1641
+ async function settle(wallet, paymentPayload, paymentRequirements) {
1642
+ const payload = paymentPayload.payload;
1643
+ if (payload.authorizationType !== "eip3009") {
1644
+ return {
1645
+ success: false,
1646
+ errorReason: "unsupported_authorization_type",
1647
+ transaction: "",
1648
+ network: paymentPayload.network,
1649
+ payer: ""
1650
+ };
1651
+ }
1652
+ const valid = await verify(wallet, paymentPayload, paymentRequirements);
1653
+ if (!valid.isValid) {
1654
+ return {
1655
+ success: false,
1656
+ network: paymentPayload.network,
1657
+ transaction: "",
1658
+ errorReason: valid.invalidReason ?? "invalid_scheme",
1659
+ //`Payment is no longer valid: ${valid.invalidReason}`,
1660
+ payer: payload.authorization.from
1661
+ };
1662
+ }
1663
+ const { signature } = (0, import_viem2.parseErc6492Signature)(payload.signature);
1664
+ const tx = await wallet.writeContract({
1665
+ address: paymentRequirements.asset,
1666
+ abi: usdcABI,
1667
+ functionName: "transferWithAuthorization",
1668
+ args: [
1669
+ payload.authorization.from,
1670
+ payload.authorization.to,
1671
+ BigInt(payload.authorization.value),
1672
+ BigInt(payload.authorization.validAfter),
1673
+ BigInt(payload.authorization.validBefore),
1674
+ payload.authorization.nonce,
1675
+ signature
1676
+ ],
1677
+ chain: wallet.chain
1678
+ });
1679
+ const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
1680
+ if (receipt.status !== "success") {
1681
+ return {
1682
+ success: false,
1683
+ errorReason: "invalid_transaction_state",
1684
+ //`Transaction failed`,
1685
+ transaction: tx,
1686
+ network: paymentPayload.network,
1687
+ payer: payload.authorization.from
1688
+ };
1689
+ }
1690
+ return {
1691
+ success: true,
1692
+ transaction: tx,
1693
+ network: paymentPayload.network,
1694
+ payer: payload.authorization.from
1695
+ };
1696
+ }
1697
+
1698
+ // src/schemes/exact/evm/permit/facilitator.ts
1699
+ var import_viem4 = require("viem");
1700
+
1701
+ // src/schemes/exact/evm/permit/sign.ts
1702
+ var import_viem3 = require("viem");
1703
+ async function signPermit(walletClient, { owner, spender, value, deadline }, { asset, network }) {
1704
+ const chainId = getNetworkId(network);
1705
+ const tokenAddress = (0, import_viem3.getAddress)(asset);
1706
+ let nonce;
1707
+ let name;
1708
+ let version;
1709
+ if (isSignerWallet(walletClient)) {
1710
+ [nonce, name, version] = await Promise.all([
1711
+ walletClient.readContract({
1712
+ address: tokenAddress,
1713
+ abi: erc20PermitABI,
1714
+ functionName: "nonces",
1715
+ args: [(0, import_viem3.getAddress)(owner)]
1716
+ }),
1717
+ walletClient.readContract({
1718
+ address: tokenAddress,
1719
+ abi: erc20PermitABI,
1720
+ functionName: "name"
1721
+ }),
1722
+ walletClient.readContract({
1723
+ address: tokenAddress,
1724
+ abi: erc20PermitABI,
1725
+ functionName: "version"
1726
+ })
1727
+ ]);
1728
+ } else {
1729
+ throw new Error("Local account signing for permit requires a connected client");
1730
+ }
1731
+ const data = {
1732
+ types: permitTypes,
1733
+ domain: {
1734
+ name,
1735
+ version,
1736
+ chainId,
1737
+ verifyingContract: tokenAddress
1738
+ },
1739
+ primaryType: "Permit",
1740
+ message: {
1741
+ owner: (0, import_viem3.getAddress)(owner),
1742
+ spender: (0, import_viem3.getAddress)(spender),
1743
+ value,
1744
+ nonce,
1745
+ deadline
1746
+ }
1747
+ };
1748
+ if (isSignerWallet(walletClient)) {
1749
+ const signature = await walletClient.signTypedData(data);
1750
+ return {
1751
+ signature,
1752
+ nonce: nonce.toString()
1753
+ };
1754
+ }
1755
+ const account = walletClient;
1756
+ if (account.signTypedData) {
1757
+ const signature = await account.signTypedData(data);
1758
+ return {
1759
+ signature,
1760
+ nonce: nonce.toString()
1761
+ };
1762
+ }
1763
+ throw new Error("Invalid wallet client provided does not support signTypedData");
1764
+ }
1765
+ function splitSignature(signature) {
1766
+ const sig = signature.slice(2);
1767
+ const r = `0x${sig.slice(0, 64)}`;
1768
+ const s = `0x${sig.slice(64, 128)}`;
1769
+ let v = parseInt(sig.slice(128, 130), 16);
1770
+ if (v < 27) {
1771
+ v += 27;
1772
+ }
1773
+ return { v, r, s };
1774
+ }
1775
+
1776
+ // src/types/shared/evm/permitProxyABI.ts
1777
+ var permitProxyContractABI = [
1778
+ {
1779
+ inputs: [
1780
+ {
1781
+ internalType: "address",
1782
+ name: "token",
1783
+ type: "address"
1784
+ },
1785
+ {
1786
+ internalType: "address",
1787
+ name: "owner",
1788
+ type: "address"
1789
+ },
1790
+ {
1791
+ internalType: "address",
1792
+ name: "spender",
1793
+ type: "address"
1794
+ },
1795
+ {
1796
+ internalType: "uint256",
1797
+ name: "value",
1798
+ type: "uint256"
1799
+ },
1800
+ {
1801
+ internalType: "uint256",
1802
+ name: "deadline",
1803
+ type: "uint256"
1804
+ },
1805
+ {
1806
+ internalType: "uint8",
1807
+ name: "v",
1808
+ type: "uint8"
1809
+ },
1810
+ {
1811
+ internalType: "bytes32",
1812
+ name: "r",
1813
+ type: "bytes32"
1814
+ },
1815
+ {
1816
+ internalType: "bytes32",
1817
+ name: "s",
1818
+ type: "bytes32"
1819
+ },
1820
+ {
1821
+ internalType: "address",
1822
+ name: "to",
1823
+ type: "address"
1824
+ }
1825
+ ],
1826
+ name: "permitAndTransfer",
1827
+ outputs: [],
1828
+ stateMutability: "nonpayable",
1829
+ type: "function"
1830
+ }
1831
+ ];
1832
+
1833
+ // src/schemes/exact/evm/permit/facilitator.ts
1834
+ async function verify2(client, payload, paymentRequirements) {
1835
+ if (payload.payload.authorizationType !== "permit" || payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
1836
+ return {
1837
+ isValid: false,
1838
+ invalidReason: "unsupported_scheme"
1839
+ };
1840
+ }
1841
+ const permitPayload = payload.payload;
1842
+ const { owner, spender, value, deadline, nonce } = permitPayload.authorization;
1843
+ let name;
1844
+ let version;
1845
+ let erc20Address;
1846
+ let chainId;
1847
+ try {
1848
+ chainId = getNetworkId(payload.network);
1849
+ erc20Address = paymentRequirements.asset;
1850
+ name = paymentRequirements.extra?.name ?? await client.readContract({
1851
+ address: erc20Address,
1852
+ abi: erc20PermitABI,
1853
+ functionName: "name"
1854
+ });
1855
+ version = paymentRequirements.extra?.version ?? await getVersion(client, erc20Address);
1856
+ } catch {
1857
+ return {
1858
+ isValid: false,
1859
+ invalidReason: "invalid_network",
1860
+ payer: owner
1861
+ };
1862
+ }
1863
+ const permitTypedData = {
1864
+ types: permitTypes,
1865
+ domain: {
1866
+ name,
1867
+ version,
1868
+ chainId,
1869
+ verifyingContract: erc20Address
1870
+ },
1871
+ primaryType: "Permit",
1872
+ message: {
1873
+ owner: (0, import_viem4.getAddress)(owner),
1874
+ spender: (0, import_viem4.getAddress)(spender),
1875
+ value,
1876
+ nonce,
1877
+ deadline
1878
+ }
1879
+ };
1880
+ const recoveredAddress = await client.verifyTypedData({
1881
+ address: owner,
1882
+ ...permitTypedData,
1883
+ signature: permitPayload.signature
1884
+ });
1885
+ if (!recoveredAddress) {
1886
+ return {
1887
+ isValid: false,
1888
+ invalidReason: "invalid_permit_signature",
1889
+ payer: owner
1890
+ };
1891
+ }
1892
+ const now = Math.floor(Date.now() / 1e3);
1893
+ if (BigInt(deadline) < now) {
1894
+ return {
1895
+ isValid: false,
1896
+ invalidReason: "permit_expired",
1897
+ payer: owner
1898
+ };
1899
+ }
1900
+ if (paymentRequirements.extra?.proxyAddress) {
1901
+ if ((0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(paymentRequirements.extra?.proxyAddress)) {
1902
+ return {
1903
+ isValid: false,
1904
+ invalidReason: "invalid_spender_address",
1905
+ payer: owner
1906
+ };
1907
+ }
1908
+ } else {
1909
+ if (client.account && (0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(client.account.address)) {
1910
+ return {
1911
+ isValid: false,
1912
+ invalidReason: "invalid_spender_address",
1913
+ payer: owner
1914
+ };
1915
+ }
1916
+ }
1917
+ const balance = await getERC20Balance(client, erc20Address, owner);
1918
+ if (balance < BigInt(paymentRequirements.maxAmountRequired)) {
1919
+ return {
1920
+ isValid: false,
1921
+ invalidReason: "insufficient_funds",
1922
+ payer: owner
1923
+ };
1924
+ }
1925
+ if (BigInt(value) < BigInt(paymentRequirements.maxAmountRequired)) {
1926
+ return {
1927
+ isValid: false,
1928
+ invalidReason: "invalid_exact_evm_payload_authorization_value",
1929
+ payer: owner
1930
+ };
1931
+ }
1932
+ return {
1933
+ isValid: true,
1934
+ payer: owner
1935
+ };
1936
+ }
1937
+ async function settle2(wallet, paymentPayload, paymentRequirements) {
1938
+ const permitPayload = paymentPayload.payload;
1939
+ if (permitPayload.authorizationType !== "permit") {
1940
+ return {
1941
+ success: false,
1942
+ errorReason: "invalid_authorization_type",
1943
+ transaction: "",
1944
+ network: paymentPayload.network,
1945
+ payer: ""
1946
+ };
1947
+ }
1948
+ const valid = await verify2(wallet, paymentPayload, paymentRequirements);
1949
+ if (!valid.isValid) {
1950
+ return {
1951
+ success: false,
1952
+ network: paymentPayload.network,
1953
+ transaction: "",
1954
+ errorReason: valid.invalidReason ?? "invalid_payment",
1955
+ payer: permitPayload.authorization.owner
1956
+ };
1957
+ }
1958
+ const { owner, spender, value, deadline } = permitPayload.authorization;
1959
+ const { v, r, s } = splitSignature(permitPayload.signature);
1960
+ const tokenAddress = paymentRequirements.asset;
1961
+ const txNonce = await wallet.getTransactionCount({
1962
+ address: wallet.account.address
1963
+ });
1964
+ let transactionHash;
1965
+ if (paymentRequirements.extra?.proxyAddress) {
1966
+ transactionHash = await wallet.writeContract({
1967
+ address: paymentRequirements.extra.proxyAddress,
1968
+ abi: permitProxyContractABI,
1969
+ functionName: "permitAndTransfer",
1970
+ args: [
1971
+ tokenAddress,
1972
+ owner,
1973
+ spender,
1974
+ BigInt(value),
1975
+ BigInt(deadline),
1976
+ v,
1977
+ r,
1978
+ s,
1979
+ paymentRequirements.payTo
1980
+ ],
1981
+ chain: wallet.chain,
1982
+ nonce: txNonce
1983
+ });
1984
+ const receipt = await wallet.waitForTransactionReceipt({ hash: transactionHash });
1985
+ if (receipt.status !== "success") {
1986
+ return {
1987
+ success: false,
1988
+ errorReason: "transaction_failed",
1989
+ transaction: transactionHash,
1990
+ network: paymentPayload.network,
1991
+ payer: owner
1992
+ };
1993
+ }
1994
+ } else {
1995
+ const [permitTx, transferTx] = await Promise.all([
1996
+ // Call permit to approve the spender
1997
+ wallet.writeContract({
1998
+ address: tokenAddress,
1999
+ abi: erc20PermitABI,
2000
+ functionName: "permit",
2001
+ args: [owner, spender, BigInt(value), BigInt(deadline), v, r, s],
2002
+ chain: wallet.chain,
2003
+ nonce: txNonce
2004
+ }),
2005
+ // Call transferFrom to transfer tokens to payTo address
2006
+ wallet.writeContract({
2007
+ address: tokenAddress,
2008
+ abi: erc20PermitABI,
2009
+ functionName: "transferFrom",
2010
+ args: [owner, paymentRequirements.payTo, BigInt(value)],
2011
+ chain: wallet.chain,
2012
+ nonce: txNonce + 1
2013
+ })
2014
+ ]);
2015
+ const [, receipt] = await Promise.all([
2016
+ wallet.waitForTransactionReceipt({ hash: permitTx }),
2017
+ wallet.waitForTransactionReceipt({ hash: transferTx })
2018
+ ]);
2019
+ if (receipt.status !== "success") {
2020
+ return {
2021
+ success: false,
2022
+ errorReason: "transaction_failed",
2023
+ transaction: transferTx,
2024
+ network: paymentPayload.network,
2025
+ payer: owner
2026
+ };
2027
+ }
2028
+ transactionHash = transferTx;
2029
+ }
2030
+ return {
2031
+ success: true,
2032
+ transaction: transactionHash,
2033
+ network: paymentPayload.network,
2034
+ payer: owner
2035
+ };
2036
+ }
2037
+
2038
+ // src/schemes/exact/evm/permit2/facilitator.ts
2039
+ var import_viem5 = require("viem");
2040
+ async function verify3(client, payload, paymentRequirements) {
2041
+ if (payload.payload.authorizationType !== "permit2" || payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
2042
+ return {
2043
+ isValid: false,
2044
+ invalidReason: "unsupported_scheme"
2045
+ };
2046
+ }
2047
+ const permit2Payload = payload.payload;
2048
+ const { owner, spender, token, amount, deadline, nonce } = permit2Payload.authorization;
2049
+ const chainId = getNetworkId(payload.network);
2050
+ const tokenAddress = (0, import_viem5.getAddress)(token);
2051
+ const ownerAddress = (0, import_viem5.getAddress)(owner);
2052
+ const permit2TypedData = {
2053
+ types: permit2Types,
2054
+ domain: {
2055
+ name: "Permit2",
2056
+ chainId,
2057
+ verifyingContract: PERMIT2_ADDRESS
2058
+ },
2059
+ primaryType: "PermitTransferFrom",
2060
+ message: {
2061
+ permitted: {
2062
+ token: tokenAddress,
2063
+ amount
2064
+ },
2065
+ spender: (0, import_viem5.getAddress)(spender),
2066
+ nonce,
2067
+ deadline
2068
+ }
2069
+ };
2070
+ const recoveredAddress = await client.verifyTypedData({
2071
+ address: ownerAddress,
2072
+ ...permit2TypedData,
2073
+ signature: permit2Payload.signature
2074
+ });
2075
+ if (!recoveredAddress) {
2076
+ return {
2077
+ isValid: false,
2078
+ invalidReason: "invalid_permit2_signature",
2079
+ payer: owner
2080
+ };
2081
+ }
2082
+ const now = Math.floor(Date.now() / 1e3);
2083
+ if (BigInt(deadline) < now) {
2084
+ return {
2085
+ isValid: false,
2086
+ invalidReason: "permit2_expired",
2087
+ payer: owner
2088
+ };
2089
+ }
2090
+ if (client.account && (0, import_viem5.getAddress)(spender) !== (0, import_viem5.getAddress)(client.account.address)) {
2091
+ return {
2092
+ isValid: false,
2093
+ invalidReason: "invalid_spender_address",
2094
+ payer: owner
2095
+ };
2096
+ }
2097
+ if (tokenAddress.toLowerCase() !== paymentRequirements.asset.toLowerCase()) {
2098
+ return {
2099
+ isValid: false,
2100
+ invalidReason: "token_mismatch",
2101
+ payer: owner
2102
+ };
2103
+ }
2104
+ const allowance = await getERC20Allowance(client, tokenAddress, ownerAddress, PERMIT2_ADDRESS);
2105
+ if (allowance < BigInt(paymentRequirements.maxAmountRequired)) {
2106
+ return {
2107
+ isValid: false,
2108
+ invalidReason: "permit2_not_approved",
2109
+ payer: owner
2110
+ };
2111
+ }
2112
+ const balance = await getERC20Balance(client, tokenAddress, ownerAddress);
2113
+ if (balance < BigInt(paymentRequirements.maxAmountRequired)) {
2114
+ return {
2115
+ isValid: false,
2116
+ invalidReason: "insufficient_funds",
2117
+ payer: owner
2118
+ };
2119
+ }
2120
+ if (BigInt(amount) < BigInt(paymentRequirements.maxAmountRequired)) {
2121
+ return {
2122
+ isValid: false,
2123
+ invalidReason: "invalid_exact_evm_payload_authorization_value",
2124
+ payer: owner
2125
+ };
2126
+ }
2127
+ return {
2128
+ isValid: true,
2129
+ payer: owner
2130
+ };
2131
+ }
2132
+ async function settle3(wallet, paymentPayload, paymentRequirements) {
2133
+ const permit2Payload = paymentPayload.payload;
2134
+ if (permit2Payload.authorizationType !== "permit2") {
2135
+ return {
2136
+ success: false,
2137
+ errorReason: "invalid_authorization_type",
2138
+ transaction: "",
2139
+ network: paymentPayload.network,
2140
+ payer: ""
2141
+ };
2142
+ }
2143
+ const valid = await verify3(wallet, paymentPayload, paymentRequirements);
2144
+ if (!valid.isValid) {
2145
+ return {
2146
+ success: false,
2147
+ network: paymentPayload.network,
2148
+ transaction: "",
2149
+ errorReason: valid.invalidReason ?? "invalid_payment",
2150
+ payer: permit2Payload.authorization.owner
2151
+ };
2152
+ }
2153
+ const { owner, token, amount, deadline, nonce } = permit2Payload.authorization;
2154
+ const tokenAddress = (0, import_viem5.getAddress)(token);
2155
+ const ownerAddress = (0, import_viem5.getAddress)(owner);
2156
+ const tx = await wallet.writeContract({
2157
+ address: PERMIT2_ADDRESS,
2158
+ abi: permit2ABI,
2159
+ functionName: "permitTransferFrom",
2160
+ args: [
2161
+ {
2162
+ permitted: {
2163
+ token: tokenAddress,
2164
+ amount: BigInt(amount)
2165
+ },
2166
+ nonce: BigInt(nonce),
2167
+ deadline: BigInt(deadline)
2168
+ },
2169
+ {
2170
+ to: paymentRequirements.payTo,
2171
+ requestedAmount: BigInt(amount)
2172
+ },
2173
+ ownerAddress,
2174
+ permit2Payload.signature
2175
+ ],
2176
+ chain: wallet.chain
2177
+ });
2178
+ const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
2179
+ if (receipt.status !== "success") {
2180
+ return {
2181
+ success: false,
2182
+ errorReason: "transaction_failed",
2183
+ transaction: tx,
2184
+ network: paymentPayload.network,
2185
+ payer: owner
2186
+ };
2187
+ }
2188
+ return {
2189
+ success: true,
2190
+ transaction: tx,
2191
+ network: paymentPayload.network,
2192
+ payer: owner
2193
+ };
2194
+ }
2195
+
2196
+ // src/schemes/exact/evm/eip3009/index.ts
2197
+ var eip3009_exports = {};
2198
+ __export(eip3009_exports, {
2199
+ createNonce: () => createNonce,
2200
+ createPayment: () => createPayment,
2201
+ createPaymentHeader: () => createPaymentHeader,
2202
+ preparePaymentHeader: () => preparePaymentHeader,
2203
+ settle: () => settle,
2204
+ signAuthorization: () => signAuthorization,
2205
+ signPaymentHeader: () => signPaymentHeader,
2206
+ verify: () => verify
2207
+ });
2208
+
2209
+ // src/schemes/exact/evm/eip3009/sign.ts
2210
+ var import_viem6 = require("viem");
2211
+ async function signAuthorization(walletClient, { from, to, value, validAfter, validBefore, nonce }, { asset, network, extra }) {
2212
+ const chainId = getNetworkId(network);
2213
+ const name = extra?.name;
2214
+ const version = extra?.version;
2215
+ const data = {
2216
+ types: authorizationTypes,
2217
+ domain: {
2218
+ name,
2219
+ version,
2220
+ chainId,
2221
+ verifyingContract: (0, import_viem6.getAddress)(asset)
2222
+ },
2223
+ primaryType: "TransferWithAuthorization",
2224
+ message: {
2225
+ from: (0, import_viem6.getAddress)(from),
2226
+ to: (0, import_viem6.getAddress)(to),
2227
+ value,
2228
+ validAfter,
2229
+ validBefore,
2230
+ nonce
2231
+ }
2232
+ };
2233
+ if (isSignerWallet(walletClient)) {
2234
+ const signature = await walletClient.signTypedData(data);
2235
+ return {
2236
+ signature
2237
+ };
2238
+ } else if (isAccount(walletClient) && walletClient.signTypedData) {
2239
+ const signature = await walletClient.signTypedData(data);
2240
+ return {
2241
+ signature
2242
+ };
2243
+ } else {
2244
+ throw new Error("Invalid wallet client provided does not support signTypedData");
2245
+ }
2246
+ }
2247
+ function createNonce() {
2248
+ const cryptoObj = typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.getRandomValues === "function" ? globalThis.crypto : (
2249
+ // Dynamic require is needed to support node.js
2250
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
2251
+ require("crypto").webcrypto
2252
+ );
2253
+ return (0, import_viem6.toHex)(cryptoObj.getRandomValues(new Uint8Array(32)));
2254
+ }
2255
+
2256
+ // src/schemes/exact/evm/utils/paymentUtils.ts
2257
+ function encodePayment(payment) {
2258
+ let safe;
2259
+ if (SupportedEVMNetworks.includes(payment.network)) {
2260
+ const evmPayload = payment.payload;
2261
+ let processedPayload;
2262
+ if (evmPayload.authorizationType === "eip3009") {
2263
+ processedPayload = {
2264
+ ...evmPayload,
2265
+ authorization: {
2266
+ ...evmPayload.authorization,
2267
+ validAfter: evmPayload.authorization.validAfter.toString(),
2268
+ validBefore: evmPayload.authorization.validBefore.toString()
2269
+ }
2270
+ };
2271
+ } else if (evmPayload.authorizationType === "permit") {
2272
+ processedPayload = {
2273
+ ...evmPayload,
2274
+ authorization: {
2275
+ ...evmPayload.authorization,
2276
+ deadline: evmPayload.authorization.deadline.toString(),
2277
+ nonce: evmPayload.authorization.nonce.toString()
2278
+ }
2279
+ };
2280
+ } else {
2281
+ processedPayload = {
2282
+ ...evmPayload,
2283
+ authorization: {
2284
+ ...evmPayload.authorization,
2285
+ deadline: evmPayload.authorization.deadline.toString(),
2286
+ nonce: evmPayload.authorization.nonce.toString()
2287
+ }
2288
+ };
2289
+ }
2290
+ safe = {
2291
+ ...payment,
2292
+ payload: processedPayload
2293
+ };
2294
+ return safeBase64Encode(JSON.stringify(safe));
2295
+ }
2296
+ if (SupportedSVMNetworks.includes(payment.network)) {
2297
+ safe = { ...payment, payload: payment.payload };
2298
+ return safeBase64Encode(JSON.stringify(safe));
2299
+ }
2300
+ throw new Error("Invalid network");
2301
+ }
2302
+ function decodePayment(payment) {
2303
+ const decoded = safeBase64Decode(payment);
2304
+ const parsed = JSON.parse(decoded);
2305
+ let obj;
2306
+ if (SupportedEVMNetworks.includes(parsed.network)) {
2307
+ obj = {
2308
+ ...parsed,
2309
+ payload: parsed.payload
2310
+ };
2311
+ } else if (SupportedSVMNetworks.includes(parsed.network)) {
2312
+ obj = {
2313
+ ...parsed,
2314
+ payload: parsed.payload
2315
+ };
2316
+ } else {
2317
+ throw new Error("Invalid network");
2318
+ }
2319
+ const validated = PaymentPayloadSchema.parse(obj);
2320
+ return validated;
2321
+ }
2322
+
2323
+ // src/schemes/exact/evm/eip3009/client.ts
2324
+ function preparePaymentHeader(from, x402Version, paymentRequirements) {
2325
+ const nonce = createNonce();
2326
+ const validAfter = BigInt(
2327
+ Math.floor(Date.now() / 1e3) - 600
2328
+ // 10 minutes before
2329
+ ).toString();
2330
+ const validBefore = BigInt(
2331
+ Math.floor(Date.now() / 1e3 + paymentRequirements.maxTimeoutSeconds)
2332
+ ).toString();
2333
+ return {
2334
+ x402Version,
2335
+ scheme: paymentRequirements.scheme,
2336
+ network: paymentRequirements.network,
2337
+ payload: {
2338
+ authorizationType: "eip3009",
2339
+ signature: void 0,
2340
+ authorization: {
2341
+ from,
2342
+ to: paymentRequirements.payTo,
2343
+ value: paymentRequirements.maxAmountRequired,
2344
+ validAfter: validAfter.toString(),
2345
+ validBefore: validBefore.toString(),
2346
+ nonce
2347
+ }
2348
+ }
2349
+ };
2350
+ }
2351
+ async function signPaymentHeader(client, paymentRequirements, unsignedPaymentHeader) {
2352
+ const { authorization } = unsignedPaymentHeader.payload;
2353
+ const { signature } = await signAuthorization(client, authorization, paymentRequirements);
2354
+ return {
2355
+ ...unsignedPaymentHeader,
2356
+ payload: {
2357
+ authorizationType: "eip3009",
2358
+ signature,
2359
+ authorization
2360
+ }
2361
+ };
2362
+ }
2363
+ async function createPayment(client, x402Version, paymentRequirements) {
2364
+ const from = isSignerWallet(client) ? client.account.address : client.address;
2365
+ const unsignedPaymentHeader = preparePaymentHeader(from, x402Version, paymentRequirements);
2366
+ return signPaymentHeader(client, paymentRequirements, unsignedPaymentHeader);
2367
+ }
2368
+ async function createPaymentHeader(client, x402Version, paymentRequirements) {
2369
+ const payment = await createPayment(client, x402Version, paymentRequirements);
2370
+ return encodePayment(payment);
2371
+ }
2372
+
2373
+ // src/schemes/exact/evm/permit/index.ts
2374
+ var permit_exports = {};
2375
+ __export(permit_exports, {
2376
+ createPayment: () => createPayment2,
2377
+ createPaymentHeader: () => createPaymentHeader2,
2378
+ preparePaymentHeader: () => preparePaymentHeader2,
2379
+ settle: () => settle2,
2380
+ signPaymentHeader: () => signPaymentHeader2,
2381
+ signPermit: () => signPermit,
2382
+ splitSignature: () => splitSignature,
2383
+ verify: () => verify2
2384
+ });
2385
+
2386
+ // src/schemes/exact/evm/permit/client.ts
2387
+ function preparePaymentHeader2(from, x402Version, paymentRequirements) {
2388
+ const deadline = BigInt(
2389
+ Math.floor(Date.now() / 1e3 + paymentRequirements.maxTimeoutSeconds)
2390
+ ).toString();
2391
+ return {
2392
+ x402Version,
2393
+ scheme: paymentRequirements.scheme,
2394
+ network: paymentRequirements.network,
2395
+ payload: {
2396
+ authorizationType: "permit",
2397
+ signature: void 0,
2398
+ authorization: {
2399
+ owner: from,
2400
+ spender: paymentRequirements.payTo,
2401
+ value: paymentRequirements.maxAmountRequired,
2402
+ deadline
2403
+ }
2404
+ }
2405
+ };
2406
+ }
2407
+ async function signPaymentHeader2(client, paymentRequirements, unsignedPaymentHeader) {
2408
+ const { owner, spender, value, deadline } = unsignedPaymentHeader.payload.authorization;
2409
+ const { signature, nonce } = await signPermit(
2410
+ client,
2411
+ { owner, spender, value, deadline },
2412
+ paymentRequirements
2413
+ );
2414
+ return {
2415
+ ...unsignedPaymentHeader,
2416
+ payload: {
2417
+ authorizationType: "permit",
2418
+ signature,
2419
+ authorization: {
2420
+ owner,
2421
+ spender,
2422
+ value,
2423
+ deadline,
2424
+ nonce: nonce.toString()
2425
+ }
2426
+ }
2427
+ };
2428
+ }
2429
+ async function createPayment2(client, x402Version, paymentRequirements) {
2430
+ const from = isSignerWallet(client) ? client.account.address : client.address;
2431
+ const unsignedPaymentHeader = preparePaymentHeader2(from, x402Version, paymentRequirements);
2432
+ return signPaymentHeader2(client, paymentRequirements, unsignedPaymentHeader);
2433
+ }
2434
+ async function createPaymentHeader2(client, x402Version, paymentRequirements) {
2435
+ const payment = await createPayment2(client, x402Version, paymentRequirements);
2436
+ return encodePayment(payment);
2437
+ }
2438
+
2439
+ // src/schemes/exact/evm/permit2/index.ts
2440
+ var permit2_exports = {};
2441
+ __export(permit2_exports, {
2442
+ createPayment: () => createPayment3,
2443
+ createPaymentHeader: () => createPaymentHeader3,
2444
+ createPermit2Nonce: () => createPermit2Nonce,
2445
+ preparePaymentHeader: () => preparePaymentHeader3,
2446
+ settle: () => settle3,
2447
+ signPaymentHeader: () => signPaymentHeader3,
2448
+ signPermit2: () => signPermit2,
2449
+ verify: () => verify3
2450
+ });
2451
+
2452
+ // src/schemes/exact/evm/permit2/sign.ts
2453
+ var import_viem7 = require("viem");
2454
+ async function signPermit2(walletClient, { owner, spender, token, amount, deadline }, { network }) {
2455
+ const chainId = getNetworkId(network);
2456
+ const tokenAddress = (0, import_viem7.getAddress)(token);
2457
+ const ownerAddress = (0, import_viem7.getAddress)(owner);
2458
+ const spenderAddress = (0, import_viem7.getAddress)(spender);
2459
+ const nonce = await createPermit2Nonce(walletClient, ownerAddress);
2460
+ const data = {
2461
+ types: permit2Types,
2462
+ domain: {
2463
+ name: "Permit2",
2464
+ chainId,
2465
+ verifyingContract: PERMIT2_ADDRESS
2466
+ },
2467
+ primaryType: "PermitTransferFrom",
2468
+ message: {
2469
+ permitted: {
2470
+ token: tokenAddress,
2471
+ amount: BigInt(amount)
2472
+ },
2473
+ spender: spenderAddress,
2474
+ nonce,
2475
+ deadline: BigInt(deadline)
2476
+ }
2477
+ };
2478
+ if (isSignerWallet(walletClient)) {
2479
+ const signature = await walletClient.signTypedData(data);
2480
+ return {
2481
+ signature,
2482
+ nonce: nonce.toString()
2483
+ };
2484
+ }
2485
+ const account = walletClient;
2486
+ if (account.signTypedData) {
2487
+ const signature = await account.signTypedData(data);
2488
+ return {
2489
+ signature,
2490
+ nonce: nonce.toString()
2491
+ };
2492
+ }
2493
+ throw new Error("Invalid wallet client provided does not support signTypedData");
2494
+ }
2495
+ async function createPermit2Nonce(walletClient, ownerAddress) {
2496
+ if (!isSignerWallet(walletClient)) {
2497
+ throw new Error("Local account signing for permit2 requires a connected client");
2498
+ }
2499
+ const timestamp = BigInt(Math.floor(Date.now() / 1e3));
2500
+ const randomOffset = BigInt(Math.floor(Math.random() * 1e3));
2501
+ let nonce = timestamp * 1000n + randomOffset;
2502
+ try {
2503
+ const wordPos = nonce / 256n;
2504
+ const bitIndex = nonce % 256n;
2505
+ const bitmap = await walletClient.readContract({
2506
+ address: PERMIT2_ADDRESS,
2507
+ abi: permit2ABI,
2508
+ functionName: "nonceBitmap",
2509
+ args: [ownerAddress, wordPos]
2510
+ });
2511
+ const used = bitmap >> bitIndex & 1n;
2512
+ if (used === 1n) {
2513
+ nonce += 1n;
2514
+ }
2515
+ } catch (error) {
2516
+ console.warn("Could not check nonce bitmap, using timestamp-based nonce:", error);
2517
+ }
2518
+ return nonce;
2519
+ }
2520
+
2521
+ // src/schemes/exact/evm/permit2/client.ts
2522
+ function preparePaymentHeader3(from, x402Version, paymentRequirements) {
2523
+ const deadline = BigInt(
2524
+ Math.floor(Date.now() / 1e3 + paymentRequirements.maxTimeoutSeconds)
2525
+ ).toString();
2526
+ return {
2527
+ x402Version,
2528
+ scheme: paymentRequirements.scheme,
2529
+ network: paymentRequirements.network,
2530
+ payload: {
2531
+ authorizationType: "permit2",
2532
+ signature: void 0,
2533
+ authorization: {
2534
+ owner: from,
2535
+ spender: paymentRequirements.payTo,
2536
+ token: paymentRequirements.asset,
2537
+ amount: paymentRequirements.maxAmountRequired,
2538
+ deadline
2539
+ }
2540
+ }
2541
+ };
2542
+ }
2543
+ async function signPaymentHeader3(client, paymentRequirements, unsignedPaymentHeader) {
2544
+ const { owner, spender, token, amount, deadline } = unsignedPaymentHeader.payload.authorization;
2545
+ const { signature, nonce } = await signPermit2(
2546
+ client,
2547
+ { owner, spender, token, amount, deadline },
2548
+ paymentRequirements
2549
+ );
2550
+ return {
2551
+ ...unsignedPaymentHeader,
2552
+ payload: {
2553
+ authorizationType: "permit2",
2554
+ signature,
2555
+ authorization: {
2556
+ owner,
2557
+ spender,
2558
+ token,
2559
+ amount,
2560
+ deadline,
2561
+ nonce
2562
+ }
2563
+ }
2564
+ };
2565
+ }
2566
+ async function createPayment3(client, x402Version, paymentRequirements) {
2567
+ const from = isSignerWallet(client) ? client.account.address : client.address;
2568
+ const unsignedPaymentHeader = preparePaymentHeader3(from, x402Version, paymentRequirements);
2569
+ return signPaymentHeader3(client, paymentRequirements, unsignedPaymentHeader);
2570
+ }
2571
+ async function createPaymentHeader3(client, x402Version, paymentRequirements) {
2572
+ const payment = await createPayment3(client, x402Version, paymentRequirements);
2573
+ return encodePayment(payment);
2574
+ }
2575
+
2576
+ // src/schemes/exact/evm/index.ts
2577
+ async function verify4(client, payload, paymentRequirements) {
2578
+ const exactEvmPayload = payload.payload;
2579
+ switch (exactEvmPayload.authorizationType) {
2580
+ case "eip3009":
2581
+ return verify(
2582
+ client,
2583
+ payload,
2584
+ paymentRequirements
2585
+ );
2586
+ case "permit":
2587
+ return verify2(client, payload, paymentRequirements);
2588
+ case "permit2":
2589
+ return verify3(
2590
+ client,
2591
+ payload,
2592
+ paymentRequirements
2593
+ );
2594
+ default:
2595
+ return {
2596
+ isValid: false,
2597
+ invalidReason: "unsupported_authorization_type",
2598
+ payer: ""
2599
+ };
2600
+ }
2601
+ }
2602
+ async function settle4(wallet, paymentPayload, paymentRequirements) {
2603
+ const payload = paymentPayload.payload;
2604
+ switch (payload.authorizationType) {
2605
+ case "eip3009":
2606
+ return settle(
2607
+ wallet,
2608
+ paymentPayload,
2609
+ paymentRequirements
2610
+ );
2611
+ case "permit":
2612
+ return settle2(
2613
+ wallet,
2614
+ paymentPayload,
2615
+ paymentRequirements
2616
+ );
2617
+ case "permit2":
2618
+ return settle3(
2619
+ wallet,
2620
+ paymentPayload,
2621
+ paymentRequirements
2622
+ );
2623
+ default:
2624
+ return {
2625
+ success: false,
2626
+ errorReason: "unsupported_authorization_type",
2627
+ transaction: "",
2628
+ network: paymentPayload.network,
2629
+ payer: ""
2630
+ };
2631
+ }
2632
+ }
2633
+
2634
+ // src/schemes/exact/svm/index.ts
2635
+ var svm_exports3 = {};
2636
+ __export(svm_exports3, {
2637
+ confirmSignedTransaction: () => confirmSignedTransaction,
2638
+ createAndSignPayment: () => createAndSignPayment,
2639
+ createPaymentHeader: () => createPaymentHeader4,
2640
+ getValidatedTransferCheckedInstruction: () => getValidatedTransferCheckedInstruction,
2641
+ sendAndConfirmSignedTransaction: () => sendAndConfirmSignedTransaction,
2642
+ sendSignedTransaction: () => sendSignedTransaction,
2643
+ settle: () => settle5,
2644
+ transactionIntrospection: () => transactionIntrospection,
2645
+ verify: () => verify5,
2646
+ verifyComputeLimitInstruction: () => verifyComputeLimitInstruction,
2647
+ verifyComputePriceInstruction: () => verifyComputePriceInstruction,
2648
+ verifyCreateATAInstruction: () => verifyCreateATAInstruction,
2649
+ verifySchemesAndNetworks: () => verifySchemesAndNetworks,
2650
+ verifyTransactionInstructions: () => verifyTransactionInstructions,
2651
+ verifyTransferCheckedInstruction: () => verifyTransferCheckedInstruction,
2652
+ verifyTransferInstruction: () => verifyTransferInstruction
2653
+ });
2654
+
2655
+ // src/schemes/exact/svm/facilitator/settle.ts
2656
+ var import_kit5 = require("@solana/kit");
2657
+ var import_transaction_confirmation = require("@solana/transaction-confirmation");
2658
+
2659
+ // src/schemes/exact/svm/facilitator/verify.ts
2660
+ var import_kit4 = require("@solana/kit");
2661
+ var import_compute_budget = require("@solana-program/compute-budget");
2662
+ var import_token_20222 = require("@solana-program/token-2022");
2663
+ var import_token2 = require("@solana-program/token");
2664
+ async function verify5(signer, payload, paymentRequirements, config2) {
2665
+ try {
2666
+ verifySchemesAndNetworks(payload, paymentRequirements);
2667
+ const svmPayload = payload.payload;
2668
+ const decodedTransaction = decodeTransactionFromPayload(svmPayload);
2669
+ const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
2670
+ await transactionIntrospection(svmPayload, paymentRequirements, config2);
2671
+ const simulateResult = await signAndSimulateTransaction(signer, decodedTransaction, rpc);
2672
+ if (simulateResult.value?.err) {
2673
+ throw new Error(`invalid_exact_svm_payload_transaction_simulation_failed`);
2674
+ }
2675
+ return {
2676
+ isValid: true,
2677
+ invalidReason: void 0,
2678
+ payer: getTokenPayerFromTransaction(decodedTransaction)
2679
+ };
2680
+ } catch (error) {
2681
+ if (error instanceof Error) {
2682
+ if (ErrorReasons.includes(error.message)) {
2683
+ return {
2684
+ isValid: false,
2685
+ invalidReason: error.message,
2686
+ payer: (() => {
2687
+ try {
2688
+ const tx = decodeTransactionFromPayload(payload.payload);
2689
+ return getTokenPayerFromTransaction(tx);
2690
+ } catch {
2691
+ return void 0;
2692
+ }
2693
+ })()
2694
+ };
2695
+ }
2696
+ }
2697
+ console.error(error);
2698
+ return {
2699
+ isValid: false,
2700
+ invalidReason: "unexpected_verify_error",
2701
+ payer: (() => {
2702
+ try {
2703
+ const tx = decodeTransactionFromPayload(payload.payload);
2704
+ return getTokenPayerFromTransaction(tx);
2705
+ } catch {
2706
+ return void 0;
2707
+ }
2708
+ })()
2709
+ };
2710
+ }
2711
+ }
2712
+ function verifySchemesAndNetworks(payload, paymentRequirements) {
2713
+ if (payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
2714
+ throw new Error("unsupported_scheme");
2715
+ }
2716
+ if (payload.network !== paymentRequirements.network || !SupportedSVMNetworks.includes(paymentRequirements.network)) {
2717
+ throw new Error("invalid_network");
2718
+ }
2719
+ }
2720
+ async function transactionIntrospection(svmPayload, paymentRequirements, config2) {
2721
+ const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
2722
+ const decodedTransaction = decodeTransactionFromPayload(svmPayload);
2723
+ const compiledTransactionMessage = (0, import_kit4.getCompiledTransactionMessageDecoder)().decode(
2724
+ decodedTransaction.messageBytes
2725
+ );
2726
+ const transactionMessage = (0, import_kit4.decompileTransactionMessage)(
2727
+ compiledTransactionMessage
2728
+ );
2729
+ await verifyTransactionInstructions(transactionMessage, paymentRequirements, rpc);
2730
+ }
2731
+ async function verifyTransactionInstructions(transactionMessage, paymentRequirements, rpc) {
2732
+ if (transactionMessage.instructions.length !== 3 && transactionMessage.instructions.length !== 4) {
2733
+ throw new Error(`invalid_exact_svm_payload_transaction_instructions_length`);
2734
+ }
2735
+ verifyComputeLimitInstruction(transactionMessage.instructions[0]);
2736
+ verifyComputePriceInstruction(transactionMessage.instructions[1]);
2737
+ if (transactionMessage.instructions.length === 3) {
2738
+ await verifyTransferInstruction(
2739
+ transactionMessage.instructions[2],
2740
+ paymentRequirements,
2741
+ {
2742
+ txHasCreateDestATAInstruction: false
2743
+ },
2744
+ rpc
2745
+ );
2746
+ } else {
2747
+ verifyCreateATAInstruction(transactionMessage.instructions[2], paymentRequirements);
2748
+ await verifyTransferInstruction(
2749
+ transactionMessage.instructions[3],
2750
+ paymentRequirements,
2751
+ {
2752
+ txHasCreateDestATAInstruction: true
2753
+ },
2754
+ rpc
2755
+ );
2756
+ }
2757
+ }
2758
+ function verifyComputeLimitInstruction(instruction) {
2759
+ try {
2760
+ if (instruction.programAddress.toString() !== import_compute_budget.COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || instruction.data?.[0] !== 2) {
2761
+ throw new Error(
2762
+ `invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction`
2763
+ );
2764
+ }
2765
+ (0, import_compute_budget.parseSetComputeUnitLimitInstruction)(
2766
+ instruction
2767
+ );
2768
+ } catch (error) {
2769
+ console.error(error);
2770
+ throw new Error(`invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction`);
2771
+ }
2772
+ }
2773
+ function verifyComputePriceInstruction(instruction) {
2774
+ if (instruction.programAddress.toString() !== import_compute_budget.COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || instruction.data?.[0] !== 3) {
2775
+ throw new Error(`invalid_exact_svm_payload_transaction_instructions_compute_price_instruction`);
2776
+ }
2777
+ const parsedInstruction = (0, import_compute_budget.parseSetComputeUnitPriceInstruction)(
2778
+ instruction
2779
+ );
2780
+ if (parsedInstruction.data.microLamports > 5 * 1e6) {
2781
+ throw new Error(
2782
+ `invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high`
2783
+ );
2784
+ }
2785
+ }
2786
+ function verifyCreateATAInstruction(instruction, paymentRequirements) {
2787
+ let createATAInstruction;
2788
+ try {
2789
+ (0, import_kit4.assertIsInstructionWithAccounts)(instruction);
2790
+ (0, import_kit4.assertIsInstructionWithData)(instruction);
2791
+ createATAInstruction = (0, import_token_20222.parseCreateAssociatedTokenInstruction)({
2792
+ ...instruction,
2793
+ data: new Uint8Array(instruction.data)
2794
+ });
2795
+ } catch (error) {
2796
+ console.error(error);
2797
+ throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction`);
2798
+ }
2799
+ if (createATAInstruction.accounts.owner.address !== paymentRequirements.payTo) {
2800
+ throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee`);
2801
+ }
2802
+ if (createATAInstruction.accounts.mint.address !== paymentRequirements.asset) {
2803
+ throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset`);
2804
+ }
2805
+ }
2806
+ async function verifyTransferInstruction(instruction, paymentRequirements, { txHasCreateDestATAInstruction }, rpc) {
2807
+ const tokenInstruction = getValidatedTransferCheckedInstruction(instruction);
2808
+ await verifyTransferCheckedInstruction(
2809
+ tokenInstruction,
2810
+ paymentRequirements,
2811
+ {
2812
+ txHasCreateDestATAInstruction
2813
+ },
2814
+ rpc
2815
+ );
2816
+ }
2817
+ async function verifyTransferCheckedInstruction(parsedInstruction, paymentRequirements, { txHasCreateDestATAInstruction }, rpc) {
2818
+ const tokenProgramAddress = parsedInstruction.programAddress.toString() === import_token2.TOKEN_PROGRAM_ADDRESS.toString() ? import_token2.TOKEN_PROGRAM_ADDRESS : import_token_20222.TOKEN_2022_PROGRAM_ADDRESS;
2819
+ const payToATA = await (0, import_token_20222.findAssociatedTokenPda)({
2820
+ mint: paymentRequirements.asset,
2821
+ owner: paymentRequirements.payTo,
2822
+ tokenProgram: tokenProgramAddress
2823
+ });
2824
+ if (parsedInstruction.accounts.destination.address !== payToATA[0]) {
2825
+ throw new Error(`invalid_exact_svm_payload_transaction_transfer_to_incorrect_ata`);
2826
+ }
2827
+ const addresses = [parsedInstruction.accounts.source.address, payToATA[0]];
2828
+ const maybeAccounts = await (0, import_kit4.fetchEncodedAccounts)(rpc, addresses);
2829
+ const missingAccounts = maybeAccounts.filter((a) => !a.exists);
2830
+ for (const missingAccount of missingAccounts) {
2831
+ if (missingAccount.address === parsedInstruction.accounts.source.address) {
2832
+ throw new Error(`invalid_exact_svm_payload_transaction_sender_ata_not_found`);
2833
+ }
2834
+ if (missingAccount.address === payToATA[0] && !txHasCreateDestATAInstruction) {
2835
+ throw new Error(`invalid_exact_svm_payload_transaction_receiver_ata_not_found`);
2836
+ }
2837
+ }
2838
+ const instructionAmount = parsedInstruction.data.amount;
2839
+ const paymentRequirementsAmount = BigInt(paymentRequirements.maxAmountRequired);
2840
+ if (instructionAmount !== paymentRequirementsAmount) {
2841
+ throw new Error(`invalid_exact_svm_payload_transaction_amount_mismatch`);
2842
+ }
2843
+ }
2844
+ function getValidatedTransferCheckedInstruction(instruction) {
2845
+ try {
2846
+ (0, import_kit4.assertIsInstructionWithData)(instruction);
2847
+ (0, import_kit4.assertIsInstructionWithAccounts)(instruction);
2848
+ } catch (error) {
2849
+ console.error(error);
2850
+ throw new Error(`invalid_exact_svm_payload_transaction_instructions`);
2851
+ }
2852
+ let tokenInstruction;
2853
+ if (instruction.programAddress.toString() === import_token2.TOKEN_PROGRAM_ADDRESS.toString()) {
2854
+ const identifiedInstruction = (0, import_token2.identifyTokenInstruction)(instruction);
2855
+ if (identifiedInstruction !== import_token2.TokenInstruction.TransferChecked) {
2856
+ throw new Error(
2857
+ `invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked`
2858
+ );
2859
+ }
2860
+ tokenInstruction = (0, import_token2.parseTransferCheckedInstruction)({
2861
+ ...instruction,
2862
+ data: new Uint8Array(instruction.data)
2863
+ });
2864
+ } else if (instruction.programAddress.toString() === import_token_20222.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
2865
+ const identifiedInstruction = (0, import_token_20222.identifyToken2022Instruction)(instruction);
2866
+ if (identifiedInstruction !== import_token_20222.Token2022Instruction.TransferChecked) {
2867
+ throw new Error(
2868
+ `invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked`
2869
+ );
2870
+ }
2871
+ tokenInstruction = (0, import_token_20222.parseTransferCheckedInstruction)({
2872
+ ...instruction,
2873
+ data: new Uint8Array(instruction.data)
2874
+ });
2875
+ } else {
2876
+ throw new Error(`invalid_exact_svm_payload_transaction_not_a_transfer_instruction`);
2877
+ }
2878
+ return tokenInstruction;
2879
+ }
2880
+
2881
+ // src/schemes/exact/svm/facilitator/settle.ts
2882
+ async function settle5(signer, payload, paymentRequirements, config2) {
2883
+ const verifyResponse = await verify5(signer, payload, paymentRequirements, config2);
2884
+ if (!verifyResponse.isValid) {
2885
+ return {
2886
+ success: false,
2887
+ errorReason: verifyResponse.invalidReason,
2888
+ network: payload.network,
2889
+ transaction: ""
2890
+ };
2891
+ }
2892
+ const svmPayload = payload.payload;
2893
+ const decodedTransaction = decodeTransactionFromPayload(svmPayload);
2894
+ const signedTransaction = await (0, import_kit5.signTransaction)([signer.keyPair], decodedTransaction);
2895
+ const payer = getTokenPayerFromTransaction(decodedTransaction);
2896
+ const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
2897
+ const rpcSubscriptions = getRpcSubscriptions(
2898
+ paymentRequirements.network,
2899
+ config2?.svmConfig?.rpcUrl
2900
+ );
2901
+ try {
2902
+ const { success, errorReason, signature } = await sendAndConfirmSignedTransaction(
2903
+ signedTransaction,
2904
+ rpc,
2905
+ rpcSubscriptions
2906
+ );
2907
+ return {
2908
+ success,
2909
+ errorReason,
2910
+ payer,
2911
+ transaction: signature,
2912
+ network: payload.network
2913
+ };
2914
+ } catch (error) {
2915
+ console.error("Unexpected error during transaction settlement:", error);
2916
+ return {
2917
+ success: false,
2918
+ errorReason: "unexpected_settle_error",
2919
+ network: payload.network,
2920
+ transaction: (0, import_kit5.getSignatureFromTransaction)(signedTransaction),
2921
+ payer
2922
+ };
2923
+ }
2924
+ }
2925
+ async function sendSignedTransaction(signedTransaction, rpc, sendTxConfig = {
2926
+ skipPreflight: true,
2927
+ encoding: "base64"
2928
+ }) {
2929
+ const base64EncodedTransaction = (0, import_kit5.getBase64EncodedWireTransaction)(signedTransaction);
2930
+ return await rpc.sendTransaction(base64EncodedTransaction, sendTxConfig).send();
2931
+ }
2932
+ async function confirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions) {
2933
+ const signature = (0, import_kit5.getSignatureFromTransaction)(signedTransaction);
2934
+ const abortController = new AbortController();
2935
+ const timeout = setTimeout(() => {
2936
+ abortController.abort("Transaction confirmation timed out after 60 seconds");
2937
+ }, 6e4);
2938
+ try {
2939
+ const compiledTransactionMessage = (0, import_kit5.getCompiledTransactionMessageDecoder)().decode(
2940
+ signedTransaction.messageBytes
2941
+ );
2942
+ const decompiledTransactionMessage = await (0, import_kit5.decompileTransactionMessageFetchingLookupTables)(
2943
+ compiledTransactionMessage,
2944
+ rpc
2945
+ );
2946
+ (0, import_kit5.assertIsTransactionMessageWithBlockhashLifetime)(decompiledTransactionMessage);
2947
+ const signedTransactionWithBlockhashLifetime = {
2948
+ ...signedTransaction,
2949
+ lifetimeConstraint: decompiledTransactionMessage.lifetimeConstraint
2950
+ };
2951
+ const commitment = "confirmed";
2952
+ const getRecentSignatureConfirmationPromise = (0, import_transaction_confirmation.createRecentSignatureConfirmationPromiseFactory)({
2953
+ rpc,
2954
+ rpcSubscriptions
2955
+ });
2956
+ const getBlockHeightExceedencePromise = (0, import_transaction_confirmation.createBlockHeightExceedencePromiseFactory)({
2957
+ rpc,
2958
+ rpcSubscriptions
2959
+ });
2960
+ const config2 = {
2961
+ abortSignal: abortController.signal,
2962
+ commitment,
2963
+ getBlockHeightExceedencePromise,
2964
+ getRecentSignatureConfirmationPromise
2965
+ };
2966
+ await (0, import_transaction_confirmation.waitForRecentTransactionConfirmation)({
2967
+ ...config2,
2968
+ transaction: signedTransactionWithBlockhashLifetime
2969
+ });
2970
+ return {
2971
+ success: true,
2972
+ signature
2973
+ };
2974
+ } catch (error) {
2975
+ console.error(error);
2976
+ if ((0, import_kit5.isSolanaError)(error, import_kit5.SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED)) {
2977
+ return {
2978
+ success: false,
2979
+ errorReason: "settle_exact_svm_block_height_exceeded",
2980
+ signature
2981
+ };
2982
+ } else if (error instanceof DOMException && error.name === "AbortError") {
2983
+ return {
2984
+ success: false,
2985
+ errorReason: "settle_exact_svm_transaction_confirmation_timed_out",
2986
+ signature
2987
+ };
2988
+ } else {
2989
+ throw error;
2990
+ }
2991
+ } finally {
2992
+ clearTimeout(timeout);
2993
+ }
2994
+ }
2995
+ async function sendAndConfirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions) {
2996
+ await sendSignedTransaction(signedTransaction, rpc);
2997
+ return await confirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions);
2998
+ }
2999
+
3000
+ // src/schemes/exact/svm/client.ts
3001
+ var import_kit6 = require("@solana/kit");
3002
+ var import_token_20223 = require("@solana-program/token-2022");
3003
+ var import_token3 = require("@solana-program/token");
3004
+ var import_compute_budget2 = require("@solana-program/compute-budget");
3005
+ async function createPaymentHeader4(client, x402Version, paymentRequirements, config2) {
3006
+ const paymentPayload = await createAndSignPayment(
3007
+ client,
3008
+ x402Version,
3009
+ paymentRequirements,
3010
+ config2
3011
+ );
3012
+ return encodePayment(paymentPayload);
3013
+ }
3014
+ async function createAndSignPayment(client, x402Version, paymentRequirements, config2) {
3015
+ const transactionMessage = await createTransferTransactionMessage(
3016
+ client,
3017
+ paymentRequirements,
3018
+ config2
3019
+ );
3020
+ const signedTransaction = await (0, import_kit6.partiallySignTransactionMessageWithSigners)(transactionMessage);
3021
+ const base64EncodedWireTransaction = (0, import_kit6.getBase64EncodedWireTransaction)(signedTransaction);
3022
+ return {
3023
+ scheme: paymentRequirements.scheme,
3024
+ network: paymentRequirements.network,
3025
+ x402Version,
3026
+ payload: {
3027
+ transaction: base64EncodedWireTransaction
3028
+ }
3029
+ };
3030
+ }
3031
+ async function createTransferTransactionMessage(client, paymentRequirements, config2) {
3032
+ const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
3033
+ const transferInstructions = await createAtaAndTransferInstructions(
3034
+ client,
3035
+ paymentRequirements,
3036
+ config2
3037
+ );
3038
+ const feePayer = paymentRequirements.extra?.feePayer;
3039
+ const txToSimulate = (0, import_kit6.pipe)(
3040
+ (0, import_kit6.createTransactionMessage)({ version: 0 }),
3041
+ (tx2) => (0, import_compute_budget2.setTransactionMessageComputeUnitPrice)(1, tx2),
3042
+ // 1 microlamport priority fee
3043
+ (tx2) => (0, import_kit6.setTransactionMessageFeePayer)(feePayer, tx2),
3044
+ (tx2) => (0, import_kit6.appendTransactionMessageInstructions)(transferInstructions, tx2)
3045
+ );
3046
+ const estimateComputeUnitLimit = (0, import_compute_budget2.estimateComputeUnitLimitFactory)({ rpc });
3047
+ const estimatedUnits = await estimateComputeUnitLimit(txToSimulate);
3048
+ const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
3049
+ const tx = (0, import_kit6.pipe)(
3050
+ txToSimulate,
3051
+ (tx2) => (0, import_kit6.prependTransactionMessageInstruction)(
3052
+ (0, import_compute_budget2.getSetComputeUnitLimitInstruction)({ units: estimatedUnits }),
3053
+ tx2
3054
+ ),
3055
+ (tx2) => (0, import_kit6.setTransactionMessageLifetimeUsingBlockhash)(latestBlockhash, tx2)
3056
+ );
3057
+ return tx;
3058
+ }
3059
+ async function createAtaAndTransferInstructions(client, paymentRequirements, config2) {
3060
+ const { asset } = paymentRequirements;
3061
+ const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
3062
+ const tokenMint = await (0, import_token_20223.fetchMint)(rpc, asset);
3063
+ const tokenProgramAddress = tokenMint.programAddress;
3064
+ if (tokenProgramAddress.toString() !== import_token3.TOKEN_PROGRAM_ADDRESS.toString() && tokenProgramAddress.toString() !== import_token_20223.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
3065
+ throw new Error("Asset was not created by a known token program");
3066
+ }
3067
+ const instructions = [];
3068
+ const createAtaIx = await createAtaInstructionOrUndefined(
3069
+ paymentRequirements,
3070
+ tokenProgramAddress,
3071
+ config2
3072
+ );
3073
+ if (createAtaIx) {
3074
+ instructions.push(createAtaIx);
3075
+ }
3076
+ const transferIx = await createTransferInstruction(
3077
+ client,
3078
+ paymentRequirements,
3079
+ tokenMint.data.decimals,
3080
+ tokenProgramAddress
3081
+ );
3082
+ instructions.push(transferIx);
3083
+ return instructions;
3084
+ }
3085
+ async function createAtaInstructionOrUndefined(paymentRequirements, tokenProgramAddress, config2) {
3086
+ const { asset, payTo, extra } = paymentRequirements;
3087
+ const feePayer = extra?.feePayer;
3088
+ if (!feePayer) {
3089
+ throw new Error(
3090
+ "feePayer is required in paymentRequirements.extra in order to set the facilitator as the fee payer for the create associated token account instruction"
3091
+ );
3092
+ }
3093
+ const [destinationATAAddress] = await (0, import_token_20223.findAssociatedTokenPda)({
3094
+ mint: asset,
3095
+ owner: payTo,
3096
+ tokenProgram: tokenProgramAddress
3097
+ });
3098
+ const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
3099
+ const maybeAccount = await (0, import_kit6.fetchEncodedAccount)(rpc, destinationATAAddress);
3100
+ if (!maybeAccount.exists) {
3101
+ return (0, import_token_20223.getCreateAssociatedTokenInstruction)({
3102
+ payer: paymentRequirements.extra?.feePayer,
3103
+ ata: destinationATAAddress,
3104
+ owner: payTo,
3105
+ mint: asset,
3106
+ tokenProgram: tokenProgramAddress
3107
+ });
3108
+ }
3109
+ return void 0;
3110
+ }
3111
+ async function createTransferInstruction(client, paymentRequirements, decimals, tokenProgramAddress) {
3112
+ const { asset, maxAmountRequired: amount, payTo } = paymentRequirements;
3113
+ const [sourceATA] = await (0, import_token_20223.findAssociatedTokenPda)({
3114
+ mint: asset,
3115
+ owner: client.address,
3116
+ tokenProgram: tokenProgramAddress
3117
+ });
3118
+ const [destinationATA] = await (0, import_token_20223.findAssociatedTokenPda)({
3119
+ mint: asset,
3120
+ owner: payTo,
3121
+ tokenProgram: tokenProgramAddress
3122
+ });
3123
+ return (0, import_token_20223.getTransferCheckedInstruction)(
3124
+ {
3125
+ source: sourceATA,
3126
+ mint: asset,
3127
+ destination: destinationATA,
3128
+ authority: client,
3129
+ amount: BigInt(amount),
3130
+ decimals
3131
+ },
3132
+ { programAddress: tokenProgramAddress }
3133
+ );
3134
+ }
3135
+
3136
+ // src/schemes/exact/index.ts
3137
+ var SCHEME = "exact";
3138
+ // Annotate the CommonJS export names for ESM import in node:
3139
+ 0 && (module.exports = {
3140
+ decodePayment,
3141
+ encodePayment,
3142
+ exact
3143
+ });
3144
+ //# sourceMappingURL=index.js.map