@wtflabs/x402 0.0.1-beta.14 → 0.0.1-beta.16

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 (51) hide show
  1. package/dist/cjs/client/index.d.ts +1 -1
  2. package/dist/cjs/client/index.js +1 -1
  3. package/dist/cjs/client/index.js.map +1 -1
  4. package/dist/cjs/facilitator/index.d.ts +1 -1
  5. package/dist/cjs/facilitator/index.js +1169 -1077
  6. package/dist/cjs/facilitator/index.js.map +1 -1
  7. package/dist/cjs/index.d.ts +1 -1
  8. package/dist/cjs/index.js +1636 -1544
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/middleware-qavKyUnr.d.ts +93 -0
  11. package/dist/cjs/paywall/index.d.ts +1 -1
  12. package/dist/cjs/schemes/index.d.ts +1 -1
  13. package/dist/cjs/schemes/index.js +697 -605
  14. package/dist/cjs/schemes/index.js.map +1 -1
  15. package/dist/cjs/shared/index.d.ts +2 -2
  16. package/dist/cjs/types/index.d.ts +335 -4
  17. package/dist/cjs/types/index.js +426 -0
  18. package/dist/cjs/types/index.js.map +1 -1
  19. package/dist/cjs/verify/index.d.ts +2 -2
  20. package/dist/esm/{chunk-VTLJOZXM.mjs → chunk-27VV4F7H.mjs} +3 -3
  21. package/dist/esm/chunk-27VV4F7H.mjs.map +1 -0
  22. package/dist/esm/{chunk-NPWDNT2P.mjs → chunk-5UE5XTGG.mjs} +428 -1
  23. package/dist/esm/chunk-5UE5XTGG.mjs.map +1 -0
  24. package/dist/esm/{chunk-KABV25HJ.mjs → chunk-VHQZ4KRR.mjs} +3 -3
  25. package/dist/esm/{chunk-WCQCFJWV.mjs → chunk-YWZNW3IG.mjs} +74 -409
  26. package/dist/esm/chunk-YWZNW3IG.mjs.map +1 -0
  27. package/dist/esm/{chunk-A6TSFIQP.mjs → chunk-YZUMYCYA.mjs} +3 -3
  28. package/dist/esm/client/index.d.mts +1 -1
  29. package/dist/esm/client/index.mjs +3 -3
  30. package/dist/esm/facilitator/index.d.mts +1 -1
  31. package/dist/esm/facilitator/index.mjs +4 -4
  32. package/dist/esm/index.d.mts +1 -1
  33. package/dist/esm/index.mjs +5 -5
  34. package/dist/esm/{middleware-DSDucaQ5.d.mts → middleware-CFWW-MAF.d.mts} +1 -1
  35. package/dist/esm/paywall/index.d.mts +1 -1
  36. package/dist/esm/schemes/index.d.mts +1 -1
  37. package/dist/esm/schemes/index.mjs +3 -3
  38. package/dist/esm/shared/index.d.mts +2 -2
  39. package/dist/esm/shared/index.mjs +1 -1
  40. package/dist/esm/types/index.d.mts +335 -4
  41. package/dist/esm/types/index.mjs +1 -1
  42. package/dist/esm/verify/index.d.mts +2 -2
  43. package/dist/esm/verify/index.mjs +1 -1
  44. package/dist/esm/x402Specs-B9zS3LnZ.d.mts +1778 -0
  45. package/package.json +1 -1
  46. package/dist/esm/chunk-NPWDNT2P.mjs.map +0 -1
  47. package/dist/esm/chunk-VTLJOZXM.mjs.map +0 -1
  48. package/dist/esm/chunk-WCQCFJWV.mjs.map +0 -1
  49. package/dist/{esm/x402Specs-BtRXj67U.d.mts → cjs/x402Specs-B9zS3LnZ.d.ts} +6 -6
  50. /package/dist/esm/{chunk-KABV25HJ.mjs.map → chunk-VHQZ4KRR.mjs.map} +0 -0
  51. /package/dist/esm/{chunk-A6TSFIQP.mjs.map → chunk-YZUMYCYA.mjs.map} +0 -0
@@ -1235,483 +1235,307 @@ var usdcABI = [
1235
1235
  }
1236
1236
  ];
1237
1237
 
1238
- // src/types/shared/svm/regex.ts
1239
- var SvmAddressRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
1240
-
1241
- // src/shared/network.ts
1242
- function getNetworkId(network) {
1243
- if (EvmNetworkToChainId.has(network)) {
1244
- return EvmNetworkToChainId.get(network);
1245
- }
1246
- if (SvmNetworkToChainId.has(network)) {
1247
- return SvmNetworkToChainId.get(network);
1248
- }
1249
- throw new Error(`Unsupported network: ${network}`);
1250
- }
1251
-
1252
- // src/types/verify/x402Specs.ts
1253
- var import_zod3 = require("zod");
1254
- var EvmMaxAtomicUnits = 40;
1255
- var EvmAddressRegex = /^0x[0-9a-fA-F]{40}$/;
1256
- var MixedAddressRegex = /^0x[a-fA-F0-9]{40}|[A-Za-z0-9][A-Za-z0-9-]{0,34}[A-Za-z0-9]$/;
1257
- var HexEncoded64ByteRegex = /^0x[0-9a-fA-F]{64}$/;
1258
- var EvmSignatureRegex = /^0x[0-9a-fA-F]+$/;
1259
- var schemes = ["exact"];
1260
- var x402Versions = [1];
1261
- var ErrorReasons = [
1262
- "insufficient_funds",
1263
- "invalid_exact_evm_payload_authorization_valid_after",
1264
- "invalid_exact_evm_payload_authorization_valid_before",
1265
- "invalid_exact_evm_payload_authorization_value",
1266
- "invalid_exact_evm_payload_signature",
1267
- "invalid_exact_evm_payload_recipient_mismatch",
1268
- "invalid_exact_svm_payload_transaction",
1269
- "invalid_exact_svm_payload_transaction_amount_mismatch",
1270
- "invalid_exact_svm_payload_transaction_create_ata_instruction",
1271
- "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee",
1272
- "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset",
1273
- "invalid_exact_svm_payload_transaction_instructions",
1274
- "invalid_exact_svm_payload_transaction_instructions_length",
1275
- "invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction",
1276
- "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction",
1277
- "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high",
1278
- "invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked",
1279
- "invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked",
1280
- "invalid_exact_svm_payload_transaction_not_a_transfer_instruction",
1281
- "invalid_exact_svm_payload_transaction_receiver_ata_not_found",
1282
- "invalid_exact_svm_payload_transaction_sender_ata_not_found",
1283
- "invalid_exact_svm_payload_transaction_simulation_failed",
1284
- "invalid_exact_svm_payload_transaction_transfer_to_incorrect_ata",
1285
- "invalid_network",
1286
- "invalid_payload",
1287
- "invalid_payment_requirements",
1288
- "invalid_scheme",
1289
- "invalid_payment",
1290
- "payment_expired",
1291
- "unsupported_scheme",
1292
- "invalid_x402_version",
1293
- "invalid_transaction_state",
1294
- "settle_exact_svm_block_height_exceeded",
1295
- "settle_exact_svm_transaction_confirmation_timed_out",
1296
- "unexpected_settle_error",
1297
- "unexpected_verify_error",
1298
- // New error reasons for Permit and Permit2
1299
- "unsupported_authorization_type",
1300
- "invalid_authorization_type",
1301
- "invalid_permit_signature",
1302
- "invalid_permit2_signature",
1303
- "invalid_permit2_witness_signature",
1304
- "witness_recipient_mismatch",
1305
- "permit_expired",
1306
- "permit2_expired",
1307
- "permit2_not_approved",
1308
- "invalid_token_address",
1309
- "invalid_spender_address",
1310
- "token_mismatch",
1311
- "insufficient_payment_amount",
1312
- "insufficient_token_balance",
1313
- "transaction_failed",
1314
- "settlement_failed"
1315
- ];
1316
- var isInteger = (value) => Number.isInteger(Number(value)) && Number(value) >= 0;
1317
- var hasMaxLength = (maxLength) => (value) => value.length <= maxLength;
1318
- var EvmOrSvmAddress = import_zod3.z.string().regex(EvmAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
1319
- var mixedAddressOrSvmAddress = import_zod3.z.string().regex(MixedAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
1320
- var PaymentRequirementsSchema = import_zod3.z.object({
1321
- scheme: import_zod3.z.enum(schemes),
1322
- network: NetworkSchema,
1323
- maxAmountRequired: import_zod3.z.string().refine(isInteger),
1324
- resource: import_zod3.z.string().url(),
1325
- description: import_zod3.z.string(),
1326
- mimeType: import_zod3.z.string(),
1327
- outputSchema: import_zod3.z.record(import_zod3.z.any()).optional(),
1328
- payTo: EvmOrSvmAddress,
1329
- maxTimeoutSeconds: import_zod3.z.number().int(),
1330
- asset: mixedAddressOrSvmAddress,
1331
- paymentType: import_zod3.z.enum(["eip3009", "permit", "permit2"]).optional(),
1332
- extra: import_zod3.z.record(import_zod3.z.any()).optional()
1333
- });
1334
- var ExactEvmPayloadAuthorizationSchema = import_zod3.z.object({
1335
- from: import_zod3.z.string().regex(EvmAddressRegex),
1336
- to: import_zod3.z.string().regex(EvmAddressRegex),
1337
- value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1338
- validAfter: import_zod3.z.string().refine(isInteger),
1339
- validBefore: import_zod3.z.string().refine(isInteger),
1340
- nonce: import_zod3.z.string().regex(HexEncoded64ByteRegex)
1341
- });
1342
- var PermitEvmPayloadAuthorizationSchema = import_zod3.z.object({
1343
- owner: import_zod3.z.string().regex(EvmAddressRegex),
1344
- spender: import_zod3.z.string().regex(EvmAddressRegex),
1345
- value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1346
- deadline: import_zod3.z.string().refine(isInteger),
1347
- nonce: import_zod3.z.string().refine(isInteger)
1348
- });
1349
- var Permit2EvmPayloadAuthorizationSchema = import_zod3.z.object({
1350
- owner: import_zod3.z.string().regex(EvmAddressRegex),
1351
- spender: import_zod3.z.string().regex(EvmAddressRegex),
1352
- token: import_zod3.z.string().regex(EvmAddressRegex),
1353
- amount: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1354
- deadline: import_zod3.z.string().refine(isInteger),
1355
- nonce: import_zod3.z.string().refine(isInteger),
1356
- to: import_zod3.z.string().regex(EvmAddressRegex).optional()
1357
- // Witness: binds recipient address to signature
1358
- });
1359
- var ExactEvmPayloadSchema = import_zod3.z.discriminatedUnion("authorizationType", [
1360
- import_zod3.z.object({
1361
- authorizationType: import_zod3.z.literal("eip3009"),
1362
- signature: import_zod3.z.string().regex(EvmSignatureRegex),
1363
- authorization: ExactEvmPayloadAuthorizationSchema
1364
- }),
1365
- import_zod3.z.object({
1366
- authorizationType: import_zod3.z.literal("permit"),
1367
- signature: import_zod3.z.string().regex(EvmSignatureRegex),
1368
- authorization: PermitEvmPayloadAuthorizationSchema
1369
- }),
1370
- import_zod3.z.object({
1371
- authorizationType: import_zod3.z.literal("permit2"),
1372
- signature: import_zod3.z.string().regex(EvmSignatureRegex),
1373
- authorization: Permit2EvmPayloadAuthorizationSchema
1374
- })
1375
- ]);
1376
- var ExactSvmPayloadSchema = import_zod3.z.object({
1377
- transaction: import_zod3.z.string().regex(Base64EncodedRegex)
1378
- });
1379
- var PaymentPayloadSchema = import_zod3.z.object({
1380
- x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1381
- scheme: import_zod3.z.enum(schemes),
1382
- network: NetworkSchema,
1383
- payload: import_zod3.z.union([ExactEvmPayloadSchema, ExactSvmPayloadSchema])
1384
- });
1385
- var x402ResponseSchema = import_zod3.z.object({
1386
- x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1387
- error: import_zod3.z.enum(ErrorReasons).optional(),
1388
- accepts: import_zod3.z.array(PaymentRequirementsSchema).optional(),
1389
- payer: import_zod3.z.string().regex(MixedAddressRegex).optional()
1390
- });
1391
- var HTTPVerbsSchema = import_zod3.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]);
1392
- var HTTPRequestStructureSchema = import_zod3.z.object({
1393
- type: import_zod3.z.literal("http"),
1394
- method: HTTPVerbsSchema,
1395
- queryParams: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).optional(),
1396
- bodyType: import_zod3.z.enum(["json", "form-data", "multipart-form-data", "text", "binary"]).optional(),
1397
- bodyFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional(),
1398
- headerFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional()
1399
- });
1400
- var RequestStructureSchema = import_zod3.z.discriminatedUnion("type", [
1401
- HTTPRequestStructureSchema
1402
- // MCPRequestStructureSchema,
1403
- // OpenAPIRequestStructureSchema,
1404
- ]);
1405
- var DiscoveredResourceSchema = import_zod3.z.object({
1406
- resource: import_zod3.z.string(),
1407
- type: import_zod3.z.enum(["http"]),
1408
- x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1409
- accepts: import_zod3.z.array(PaymentRequirementsSchema),
1410
- lastUpdated: import_zod3.z.date(),
1411
- metadata: import_zod3.z.record(import_zod3.z.any()).optional()
1412
- });
1413
- var SettleRequestSchema = import_zod3.z.object({
1414
- paymentPayload: PaymentPayloadSchema,
1415
- paymentRequirements: PaymentRequirementsSchema
1416
- });
1417
- var VerifyRequestSchema = import_zod3.z.object({
1418
- paymentPayload: PaymentPayloadSchema,
1419
- paymentRequirements: PaymentRequirementsSchema
1420
- });
1421
- var VerifyResponseSchema = import_zod3.z.object({
1422
- isValid: import_zod3.z.boolean(),
1423
- invalidReason: import_zod3.z.enum(ErrorReasons).optional(),
1424
- payer: EvmOrSvmAddress.optional()
1425
- });
1426
- var SettleResponseSchema = import_zod3.z.object({
1427
- success: import_zod3.z.boolean(),
1428
- errorReason: import_zod3.z.enum(ErrorReasons).optional(),
1429
- payer: EvmOrSvmAddress.optional(),
1430
- transaction: import_zod3.z.string().regex(MixedAddressRegex),
1431
- network: NetworkSchema
1432
- });
1433
- var ListDiscoveryResourcesRequestSchema = import_zod3.z.object({
1434
- type: import_zod3.z.string().optional(),
1435
- limit: import_zod3.z.number().optional(),
1436
- offset: import_zod3.z.number().optional()
1437
- });
1438
- var ListDiscoveryResourcesResponseSchema = import_zod3.z.object({
1439
- x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1440
- items: import_zod3.z.array(DiscoveredResourceSchema),
1441
- pagination: import_zod3.z.object({
1442
- limit: import_zod3.z.number(),
1443
- offset: import_zod3.z.number(),
1444
- total: import_zod3.z.number()
1445
- })
1446
- });
1447
- var SupportedPaymentKindSchema = import_zod3.z.object({
1448
- x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1449
- scheme: import_zod3.z.enum(schemes),
1450
- network: NetworkSchema,
1451
- extra: import_zod3.z.record(import_zod3.z.any()).optional()
1452
- });
1453
- var SupportedPaymentKindsResponseSchema = import_zod3.z.object({
1454
- kinds: import_zod3.z.array(SupportedPaymentKindSchema)
1455
- });
1456
-
1457
- // src/types/verify/facilitator.ts
1458
- var import_zod4 = require("zod");
1459
- var facilitatorRequestSchema = import_zod4.z.object({
1460
- paymentHeader: import_zod4.z.string(),
1461
- paymentRequirements: PaymentRequirementsSchema
1462
- });
1463
-
1464
- // src/shared/evm/usdc.ts
1465
- function getUsdcAddress(client) {
1466
- return config[client.chain.id.toString()].usdcAddress;
1467
- }
1468
- var versionCache = /* @__PURE__ */ new Map();
1469
- async function getVersion(client, tokenAddress) {
1470
- const address = tokenAddress ?? getUsdcAddress(client);
1471
- const cacheKey = `${client.chain.id}-${address.toLowerCase()}`;
1472
- if (versionCache.has(cacheKey)) {
1473
- return versionCache.get(cacheKey);
1474
- }
1475
- let version = "1";
1476
- try {
1477
- const eip712DomainABI = [
1478
- {
1479
- inputs: [],
1480
- name: "eip712Domain",
1481
- outputs: [
1482
- { name: "fields", type: "bytes1" },
1483
- { name: "name", type: "string" },
1484
- { name: "version", type: "string" },
1485
- { name: "chainId", type: "uint256" },
1486
- { name: "verifyingContract", type: "address" },
1487
- { name: "salt", type: "bytes32" },
1488
- { name: "extensions", type: "uint256[]" }
1489
- ],
1490
- stateMutability: "view",
1491
- type: "function"
1492
- }
1493
- ];
1494
- const result = await client.readContract({
1495
- address,
1496
- abi: eip712DomainABI,
1497
- functionName: "eip712Domain"
1498
- });
1499
- version = result[2];
1500
- } catch {
1501
- try {
1502
- const result = await client.readContract({
1503
- address,
1504
- abi: usdcABI,
1505
- functionName: "version"
1506
- });
1507
- version = result;
1508
- } catch {
1509
- console.warn(
1510
- `Neither eip712Domain() nor version() available for token ${address}, using default: ${version}`
1511
- );
1512
- }
1513
- }
1514
- versionCache.set(cacheKey, version);
1515
- return version;
1516
- }
1517
-
1518
- // src/shared/evm/erc20.ts
1519
- async function getERC20Balance(client, erc20Address, address) {
1520
- const balance = await client.readContract({
1521
- address: erc20Address,
1522
- abi: usdcABI,
1523
- functionName: "balanceOf",
1524
- args: [address]
1525
- });
1526
- return balance;
1527
- }
1528
- async function getERC20Allowance(client, erc20Address, owner, spender) {
1529
- const allowance = await client.readContract({
1530
- address: erc20Address,
1531
- abi: usdcABI,
1532
- functionName: "allowance",
1533
- args: [owner, spender]
1534
- });
1535
- return allowance;
1536
- }
1537
-
1538
- // src/shared/svm/transaction.ts
1539
- var import_kit3 = require("@solana/kit");
1540
- var import_token = require("@solana-program/token");
1541
- var import_token_2022 = require("@solana-program/token-2022");
1542
- function decodeTransactionFromPayload(svmPayload) {
1543
- try {
1544
- const base64Encoder = (0, import_kit3.getBase64Encoder)();
1545
- const transactionBytes = base64Encoder.encode(svmPayload.transaction);
1546
- const transactionDecoder = (0, import_kit3.getTransactionDecoder)();
1547
- return transactionDecoder.decode(transactionBytes);
1548
- } catch (error) {
1549
- console.error("error", error);
1550
- throw new Error("invalid_exact_svm_payload_transaction");
1551
- }
1552
- }
1553
- function getTokenPayerFromTransaction(transaction) {
1554
- const compiled = (0, import_kit3.getCompiledTransactionMessageDecoder)().decode(
1555
- transaction.messageBytes
1556
- );
1557
- const staticAccounts = compiled.staticAccounts ?? [];
1558
- const instructions = compiled.instructions ?? [];
1559
- for (const ix of instructions) {
1560
- const programIndex = ix.programAddressIndex;
1561
- const programAddress = staticAccounts[programIndex].toString();
1562
- if (programAddress === import_token.TOKEN_PROGRAM_ADDRESS.toString() || programAddress === import_token_2022.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
1563
- const accountIndices = ix.accountIndices ?? [];
1564
- if (accountIndices.length >= 4) {
1565
- const ownerIndex = accountIndices[3];
1566
- const ownerAddress = staticAccounts[ownerIndex].toString();
1567
- if (ownerAddress) return ownerAddress;
1238
+ // src/types/shared/evm/eip7702ABI.ts
1239
+ var EIP7702SellerWalletMinimalAbi = [
1240
+ {
1241
+ inputs: [],
1242
+ name: "InvalidAmount",
1243
+ type: "error"
1244
+ },
1245
+ {
1246
+ inputs: [],
1247
+ name: "NotOwner",
1248
+ type: "error"
1249
+ },
1250
+ {
1251
+ inputs: [],
1252
+ name: "ReentrancyGuardReentrantCall",
1253
+ type: "error"
1254
+ },
1255
+ {
1256
+ inputs: [
1257
+ {
1258
+ internalType: "address",
1259
+ name: "token",
1260
+ type: "address"
1568
1261
  }
1569
- }
1570
- }
1571
- return "";
1572
- }
1573
- async function signAndSimulateTransaction(signer, transaction, rpc) {
1574
- const signedTransaction = await (0, import_kit3.partiallySignTransaction)([signer.keyPair], transaction);
1575
- const base64EncodedTransaction = (0, import_kit3.getBase64EncodedWireTransaction)(signedTransaction);
1576
- const simulateTxConfig = {
1577
- sigVerify: true,
1578
- replaceRecentBlockhash: false,
1579
- commitment: "confirmed",
1580
- encoding: "base64",
1581
- accounts: void 0,
1582
- innerInstructions: void 0,
1583
- minContextSlot: void 0
1584
- };
1585
- const simulateResult = await rpc.simulateTransaction(base64EncodedTransaction, simulateTxConfig).send();
1586
- return simulateResult;
1587
- }
1588
-
1589
- // src/types/shared/evm/permitProxyABI.ts
1590
- var permitProxyContractABI = [
1591
- // settleWithPermit - 使用 EIP-2612 Permit 进行结算
1262
+ ],
1263
+ name: "SafeERC20FailedOperation",
1264
+ type: "error"
1265
+ },
1266
+ {
1267
+ inputs: [],
1268
+ name: "ZeroAddress",
1269
+ type: "error"
1270
+ },
1271
+ {
1272
+ anonymous: false,
1273
+ inputs: [
1274
+ {
1275
+ indexed: true,
1276
+ internalType: "address",
1277
+ name: "beneficiary",
1278
+ type: "address"
1279
+ }
1280
+ ],
1281
+ name: "ConfigUpdated",
1282
+ type: "event"
1283
+ },
1592
1284
  {
1285
+ anonymous: false,
1593
1286
  inputs: [
1594
1287
  {
1288
+ indexed: true,
1595
1289
  internalType: "address",
1596
1290
  name: "token",
1597
1291
  type: "address"
1598
1292
  },
1599
1293
  {
1294
+ indexed: true,
1600
1295
  internalType: "address",
1601
1296
  name: "payer",
1602
1297
  type: "address"
1603
1298
  },
1604
1299
  {
1300
+ indexed: true,
1605
1301
  internalType: "address",
1606
- name: "seller",
1302
+ name: "facilitator",
1607
1303
  type: "address"
1608
1304
  },
1609
1305
  {
1306
+ indexed: false,
1610
1307
  internalType: "uint256",
1611
1308
  name: "amount",
1612
1309
  type: "uint256"
1613
1310
  },
1614
1311
  {
1312
+ indexed: false,
1615
1313
  internalType: "uint256",
1616
- name: "deadline",
1314
+ name: "beneficiaryAmount",
1617
1315
  type: "uint256"
1618
1316
  },
1619
1317
  {
1620
- internalType: "uint8",
1621
- name: "v",
1622
- type: "uint8"
1318
+ indexed: false,
1319
+ internalType: "uint256",
1320
+ name: "feeAmount",
1321
+ type: "uint256"
1623
1322
  },
1624
1323
  {
1625
- internalType: "bytes32",
1626
- name: "r",
1627
- type: "bytes32"
1324
+ indexed: false,
1325
+ internalType: "string",
1326
+ name: "method",
1327
+ type: "string"
1328
+ }
1329
+ ],
1330
+ name: "SettlementExecuted",
1331
+ type: "event"
1332
+ },
1333
+ {
1334
+ stateMutability: "payable",
1335
+ type: "fallback"
1336
+ },
1337
+ {
1338
+ inputs: [],
1339
+ name: "BASIS_POINTS",
1340
+ outputs: [
1341
+ {
1342
+ internalType: "uint96",
1343
+ name: "",
1344
+ type: "uint96"
1345
+ }
1346
+ ],
1347
+ stateMutability: "view",
1348
+ type: "function"
1349
+ },
1350
+ {
1351
+ inputs: [],
1352
+ name: "FEE_BPS",
1353
+ outputs: [
1354
+ {
1355
+ internalType: "uint96",
1356
+ name: "",
1357
+ type: "uint96"
1358
+ }
1359
+ ],
1360
+ stateMutability: "view",
1361
+ type: "function"
1362
+ },
1363
+ {
1364
+ inputs: [],
1365
+ name: "FEE_RECIPIENT",
1366
+ outputs: [
1367
+ {
1368
+ internalType: "address",
1369
+ name: "",
1370
+ type: "address"
1371
+ }
1372
+ ],
1373
+ stateMutability: "view",
1374
+ type: "function"
1375
+ },
1376
+ {
1377
+ inputs: [],
1378
+ name: "getConfig",
1379
+ outputs: [
1380
+ {
1381
+ internalType: "address",
1382
+ name: "beneficiary",
1383
+ type: "address"
1628
1384
  },
1629
1385
  {
1630
- internalType: "bytes32",
1631
- name: "s",
1632
- type: "bytes32"
1386
+ internalType: "bool",
1387
+ name: "initialized",
1388
+ type: "bool"
1633
1389
  }
1634
1390
  ],
1635
- name: "settleWithPermit",
1636
- outputs: [],
1637
- stateMutability: "nonpayable",
1391
+ stateMutability: "view",
1392
+ type: "function"
1393
+ },
1394
+ {
1395
+ inputs: [],
1396
+ name: "getEffectiveConfig",
1397
+ outputs: [
1398
+ {
1399
+ internalType: "address",
1400
+ name: "beneficiary",
1401
+ type: "address"
1402
+ },
1403
+ {
1404
+ internalType: "address",
1405
+ name: "feeRecipient",
1406
+ type: "address"
1407
+ },
1408
+ {
1409
+ internalType: "uint96",
1410
+ name: "feeBps",
1411
+ type: "uint96"
1412
+ }
1413
+ ],
1414
+ stateMutability: "view",
1638
1415
  type: "function"
1639
1416
  },
1640
- // settleWithERC3009 - 使用 EIP-3009 TransferWithAuthorization 进行结算
1641
1417
  {
1642
1418
  inputs: [
1643
1419
  {
1644
1420
  internalType: "address",
1645
- name: "token",
1421
+ name: "",
1646
1422
  type: "address"
1647
1423
  },
1648
1424
  {
1649
1425
  internalType: "address",
1650
- name: "payer",
1426
+ name: "",
1651
1427
  type: "address"
1652
1428
  },
1429
+ {
1430
+ internalType: "uint256[]",
1431
+ name: "",
1432
+ type: "uint256[]"
1433
+ },
1434
+ {
1435
+ internalType: "uint256[]",
1436
+ name: "",
1437
+ type: "uint256[]"
1438
+ },
1439
+ {
1440
+ internalType: "bytes",
1441
+ name: "",
1442
+ type: "bytes"
1443
+ }
1444
+ ],
1445
+ name: "onERC1155BatchReceived",
1446
+ outputs: [
1447
+ {
1448
+ internalType: "bytes4",
1449
+ name: "",
1450
+ type: "bytes4"
1451
+ }
1452
+ ],
1453
+ stateMutability: "nonpayable",
1454
+ type: "function"
1455
+ },
1456
+ {
1457
+ inputs: [
1653
1458
  {
1654
1459
  internalType: "address",
1655
- name: "seller",
1460
+ name: "",
1656
1461
  type: "address"
1657
1462
  },
1658
1463
  {
1659
- internalType: "uint256",
1660
- name: "amount",
1661
- type: "uint256"
1464
+ internalType: "address",
1465
+ name: "",
1466
+ type: "address"
1662
1467
  },
1663
1468
  {
1664
1469
  internalType: "uint256",
1665
- name: "validAfter",
1470
+ name: "",
1666
1471
  type: "uint256"
1667
1472
  },
1668
1473
  {
1669
1474
  internalType: "uint256",
1670
- name: "validBefore",
1475
+ name: "",
1671
1476
  type: "uint256"
1672
1477
  },
1673
1478
  {
1674
- internalType: "bytes32",
1675
- name: "nonce",
1676
- type: "bytes32"
1479
+ internalType: "bytes",
1480
+ name: "",
1481
+ type: "bytes"
1482
+ }
1483
+ ],
1484
+ name: "onERC1155Received",
1485
+ outputs: [
1486
+ {
1487
+ internalType: "bytes4",
1488
+ name: "",
1489
+ type: "bytes4"
1490
+ }
1491
+ ],
1492
+ stateMutability: "nonpayable",
1493
+ type: "function"
1494
+ },
1495
+ {
1496
+ inputs: [
1497
+ {
1498
+ internalType: "address",
1499
+ name: "",
1500
+ type: "address"
1677
1501
  },
1678
1502
  {
1679
- internalType: "uint8",
1680
- name: "v",
1681
- type: "uint8"
1503
+ internalType: "address",
1504
+ name: "",
1505
+ type: "address"
1682
1506
  },
1683
1507
  {
1684
- internalType: "bytes32",
1685
- name: "r",
1686
- type: "bytes32"
1508
+ internalType: "uint256",
1509
+ name: "",
1510
+ type: "uint256"
1687
1511
  },
1688
1512
  {
1689
- internalType: "bytes32",
1690
- name: "s",
1691
- type: "bytes32"
1513
+ internalType: "bytes",
1514
+ name: "",
1515
+ type: "bytes"
1516
+ }
1517
+ ],
1518
+ name: "onERC721Received",
1519
+ outputs: [
1520
+ {
1521
+ internalType: "bytes4",
1522
+ name: "",
1523
+ type: "bytes4"
1692
1524
  }
1693
1525
  ],
1694
- name: "settleWithERC3009",
1695
- outputs: [],
1696
1526
  stateMutability: "nonpayable",
1697
1527
  type: "function"
1698
1528
  },
1699
- // settleWithERC3009Direct - 直接结算(无手续费)
1700
1529
  {
1701
1530
  inputs: [
1702
1531
  {
1703
1532
  internalType: "address",
1704
- name: "token",
1705
- type: "address"
1706
- },
1707
- {
1708
- internalType: "address",
1709
- name: "payer",
1533
+ name: "token",
1710
1534
  type: "address"
1711
1535
  },
1712
1536
  {
1713
1537
  internalType: "address",
1714
- name: "seller",
1538
+ name: "payer",
1715
1539
  type: "address"
1716
1540
  },
1717
1541
  {
@@ -1750,99 +1574,442 @@ var permitProxyContractABI = [
1750
1574
  type: "bytes32"
1751
1575
  }
1752
1576
  ],
1753
- name: "settleWithERC3009Direct",
1577
+ name: "settleWithERC3009",
1754
1578
  outputs: [],
1755
1579
  stateMutability: "nonpayable",
1756
1580
  type: "function"
1757
1581
  },
1758
- // Events
1759
1582
  {
1760
- anonymous: false,
1761
1583
  inputs: [
1762
1584
  {
1763
- indexed: true,
1764
1585
  internalType: "address",
1765
1586
  name: "token",
1766
1587
  type: "address"
1767
1588
  },
1768
1589
  {
1769
- indexed: true,
1770
1590
  internalType: "address",
1771
1591
  name: "payer",
1772
1592
  type: "address"
1773
1593
  },
1774
1594
  {
1775
- indexed: true,
1776
- internalType: "address",
1777
- name: "seller",
1778
- type: "address"
1779
- },
1780
- {
1781
- indexed: false,
1782
1595
  internalType: "uint256",
1783
1596
  name: "amount",
1784
1597
  type: "uint256"
1785
1598
  },
1786
1599
  {
1787
- indexed: false,
1788
1600
  internalType: "uint256",
1789
- name: "sellerAmount",
1601
+ name: "deadline",
1790
1602
  type: "uint256"
1791
1603
  },
1792
1604
  {
1793
- indexed: false,
1794
- internalType: "uint256",
1795
- name: "feeAmount",
1796
- type: "uint256"
1605
+ internalType: "uint8",
1606
+ name: "v",
1607
+ type: "uint8"
1608
+ },
1609
+ {
1610
+ internalType: "bytes32",
1611
+ name: "r",
1612
+ type: "bytes32"
1797
1613
  },
1798
1614
  {
1799
- indexed: false,
1800
- internalType: "string",
1801
- name: "path",
1802
- type: "string"
1615
+ internalType: "bytes32",
1616
+ name: "s",
1617
+ type: "bytes32"
1618
+ }
1619
+ ],
1620
+ name: "settleWithPermit",
1621
+ outputs: [],
1622
+ stateMutability: "nonpayable",
1623
+ type: "function"
1624
+ },
1625
+ {
1626
+ inputs: [
1627
+ {
1628
+ internalType: "bytes4",
1629
+ name: "interfaceId",
1630
+ type: "bytes4"
1631
+ }
1632
+ ],
1633
+ name: "supportsInterface",
1634
+ outputs: [
1635
+ {
1636
+ internalType: "bool",
1637
+ name: "",
1638
+ type: "bool"
1639
+ }
1640
+ ],
1641
+ stateMutability: "view",
1642
+ type: "function"
1643
+ },
1644
+ {
1645
+ inputs: [
1646
+ {
1647
+ internalType: "address",
1648
+ name: "beneficiary_",
1649
+ type: "address"
1650
+ }
1651
+ ],
1652
+ name: "updateConfig",
1653
+ outputs: [],
1654
+ stateMutability: "nonpayable",
1655
+ type: "function"
1656
+ },
1657
+ {
1658
+ stateMutability: "payable",
1659
+ type: "receive"
1660
+ }
1661
+ ];
1662
+
1663
+ // src/types/shared/svm/regex.ts
1664
+ var SvmAddressRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
1665
+
1666
+ // src/shared/network.ts
1667
+ function getNetworkId(network) {
1668
+ if (EvmNetworkToChainId.has(network)) {
1669
+ return EvmNetworkToChainId.get(network);
1670
+ }
1671
+ if (SvmNetworkToChainId.has(network)) {
1672
+ return SvmNetworkToChainId.get(network);
1673
+ }
1674
+ throw new Error(`Unsupported network: ${network}`);
1675
+ }
1676
+
1677
+ // src/types/verify/x402Specs.ts
1678
+ var import_zod3 = require("zod");
1679
+ var EvmMaxAtomicUnits = 40;
1680
+ var EvmAddressRegex = /^0x[0-9a-fA-F]{40}$/;
1681
+ var MixedAddressRegex = /^0x[a-fA-F0-9]{40}|[A-Za-z0-9][A-Za-z0-9-]{0,34}[A-Za-z0-9]$/;
1682
+ var HexEncoded64ByteRegex = /^0x[0-9a-fA-F]{64}$/;
1683
+ var EvmSignatureRegex = /^0x[0-9a-fA-F]+$/;
1684
+ var schemes = ["exact"];
1685
+ var x402Versions = [1];
1686
+ var ErrorReasons = [
1687
+ "insufficient_funds",
1688
+ "invalid_exact_evm_payload_authorization_valid_after",
1689
+ "invalid_exact_evm_payload_authorization_valid_before",
1690
+ "invalid_exact_evm_payload_authorization_value",
1691
+ "invalid_exact_evm_payload_signature",
1692
+ "invalid_exact_evm_payload_recipient_mismatch",
1693
+ "invalid_exact_svm_payload_transaction",
1694
+ "invalid_exact_svm_payload_transaction_amount_mismatch",
1695
+ "invalid_exact_svm_payload_transaction_create_ata_instruction",
1696
+ "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee",
1697
+ "invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset",
1698
+ "invalid_exact_svm_payload_transaction_instructions",
1699
+ "invalid_exact_svm_payload_transaction_instructions_length",
1700
+ "invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction",
1701
+ "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction",
1702
+ "invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high",
1703
+ "invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked",
1704
+ "invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked",
1705
+ "invalid_exact_svm_payload_transaction_not_a_transfer_instruction",
1706
+ "invalid_exact_svm_payload_transaction_receiver_ata_not_found",
1707
+ "invalid_exact_svm_payload_transaction_sender_ata_not_found",
1708
+ "invalid_exact_svm_payload_transaction_simulation_failed",
1709
+ "invalid_exact_svm_payload_transaction_transfer_to_incorrect_ata",
1710
+ "invalid_network",
1711
+ "invalid_payload",
1712
+ "invalid_payment_requirements",
1713
+ "invalid_scheme",
1714
+ "invalid_payment",
1715
+ "payment_expired",
1716
+ "unsupported_scheme",
1717
+ "invalid_x402_version",
1718
+ "invalid_transaction_state",
1719
+ "settle_exact_svm_block_height_exceeded",
1720
+ "settle_exact_svm_transaction_confirmation_timed_out",
1721
+ "unexpected_settle_error",
1722
+ "unexpected_verify_error",
1723
+ // New error reasons for Permit and Permit2
1724
+ "unsupported_authorization_type",
1725
+ "invalid_authorization_type",
1726
+ "invalid_permit_signature",
1727
+ "invalid_permit2_signature",
1728
+ "invalid_permit2_witness_signature",
1729
+ "witness_recipient_mismatch",
1730
+ "permit_expired",
1731
+ "permit2_expired",
1732
+ "permit2_not_approved",
1733
+ "invalid_token_address",
1734
+ "invalid_spender_address",
1735
+ "token_mismatch",
1736
+ "insufficient_payment_amount",
1737
+ "insufficient_token_balance",
1738
+ "transaction_failed",
1739
+ "settlement_failed"
1740
+ ];
1741
+ var isInteger = (value) => Number.isInteger(Number(value)) && Number(value) >= 0;
1742
+ var hasMaxLength = (maxLength) => (value) => value.length <= maxLength;
1743
+ var EvmOrSvmAddress = import_zod3.z.string().regex(EvmAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
1744
+ var mixedAddressOrSvmAddress = import_zod3.z.string().regex(MixedAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
1745
+ var PaymentRequirementsSchema = import_zod3.z.object({
1746
+ scheme: import_zod3.z.enum(schemes),
1747
+ network: NetworkSchema,
1748
+ maxAmountRequired: import_zod3.z.string().refine(isInteger),
1749
+ resource: import_zod3.z.string().url(),
1750
+ description: import_zod3.z.string(),
1751
+ mimeType: import_zod3.z.string(),
1752
+ outputSchema: import_zod3.z.record(import_zod3.z.any()).optional(),
1753
+ payTo: EvmOrSvmAddress,
1754
+ maxTimeoutSeconds: import_zod3.z.number().int(),
1755
+ asset: mixedAddressOrSvmAddress,
1756
+ paymentType: import_zod3.z.enum(["eip3009", "permit", "permit2"]).optional(),
1757
+ extra: import_zod3.z.record(import_zod3.z.any()).optional()
1758
+ });
1759
+ var ExactEvmPayloadAuthorizationSchema = import_zod3.z.object({
1760
+ from: import_zod3.z.string().regex(EvmAddressRegex),
1761
+ to: import_zod3.z.string().regex(EvmAddressRegex),
1762
+ value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1763
+ validAfter: import_zod3.z.string().refine(isInteger),
1764
+ validBefore: import_zod3.z.string().refine(isInteger),
1765
+ nonce: import_zod3.z.string().regex(HexEncoded64ByteRegex)
1766
+ });
1767
+ var PermitEvmPayloadAuthorizationSchema = import_zod3.z.object({
1768
+ owner: import_zod3.z.string().regex(EvmAddressRegex),
1769
+ spender: import_zod3.z.string().regex(EvmAddressRegex),
1770
+ value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1771
+ deadline: import_zod3.z.string().refine(isInteger),
1772
+ nonce: import_zod3.z.string().refine(isInteger)
1773
+ });
1774
+ var Permit2EvmPayloadAuthorizationSchema = import_zod3.z.object({
1775
+ owner: import_zod3.z.string().regex(EvmAddressRegex),
1776
+ spender: import_zod3.z.string().regex(EvmAddressRegex),
1777
+ token: import_zod3.z.string().regex(EvmAddressRegex),
1778
+ amount: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
1779
+ deadline: import_zod3.z.string().refine(isInteger),
1780
+ nonce: import_zod3.z.string().refine(isInteger),
1781
+ to: import_zod3.z.string().regex(EvmAddressRegex).optional()
1782
+ // Witness: binds recipient address to signature
1783
+ });
1784
+ var ExactEvmPayloadSchema = import_zod3.z.discriminatedUnion("authorizationType", [
1785
+ import_zod3.z.object({
1786
+ authorizationType: import_zod3.z.literal("eip3009"),
1787
+ signature: import_zod3.z.string().regex(EvmSignatureRegex),
1788
+ authorization: ExactEvmPayloadAuthorizationSchema
1789
+ }),
1790
+ import_zod3.z.object({
1791
+ authorizationType: import_zod3.z.literal("permit"),
1792
+ signature: import_zod3.z.string().regex(EvmSignatureRegex),
1793
+ authorization: PermitEvmPayloadAuthorizationSchema
1794
+ }),
1795
+ import_zod3.z.object({
1796
+ authorizationType: import_zod3.z.literal("permit2"),
1797
+ signature: import_zod3.z.string().regex(EvmSignatureRegex),
1798
+ authorization: Permit2EvmPayloadAuthorizationSchema
1799
+ })
1800
+ ]);
1801
+ var ExactSvmPayloadSchema = import_zod3.z.object({
1802
+ transaction: import_zod3.z.string().regex(Base64EncodedRegex)
1803
+ });
1804
+ var PaymentPayloadSchema = import_zod3.z.object({
1805
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1806
+ scheme: import_zod3.z.enum(schemes),
1807
+ network: NetworkSchema,
1808
+ payload: import_zod3.z.union([ExactEvmPayloadSchema, ExactSvmPayloadSchema])
1809
+ });
1810
+ var x402ResponseSchema = import_zod3.z.object({
1811
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1812
+ error: import_zod3.z.enum(ErrorReasons).optional(),
1813
+ accepts: import_zod3.z.array(PaymentRequirementsSchema).optional(),
1814
+ payer: import_zod3.z.string().regex(MixedAddressRegex).optional()
1815
+ });
1816
+ var HTTPVerbsSchema = import_zod3.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]);
1817
+ var HTTPRequestStructureSchema = import_zod3.z.object({
1818
+ type: import_zod3.z.literal("http"),
1819
+ method: HTTPVerbsSchema,
1820
+ queryParams: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).optional(),
1821
+ bodyType: import_zod3.z.enum(["json", "form-data", "multipart-form-data", "text", "binary"]).optional(),
1822
+ bodyFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional(),
1823
+ headerFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional()
1824
+ });
1825
+ var RequestStructureSchema = import_zod3.z.discriminatedUnion("type", [
1826
+ HTTPRequestStructureSchema
1827
+ // MCPRequestStructureSchema,
1828
+ // OpenAPIRequestStructureSchema,
1829
+ ]);
1830
+ var DiscoveredResourceSchema = import_zod3.z.object({
1831
+ resource: import_zod3.z.string(),
1832
+ type: import_zod3.z.enum(["http"]),
1833
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1834
+ accepts: import_zod3.z.array(PaymentRequirementsSchema),
1835
+ lastUpdated: import_zod3.z.date(),
1836
+ metadata: import_zod3.z.record(import_zod3.z.any()).optional()
1837
+ });
1838
+ var SettleRequestSchema = import_zod3.z.object({
1839
+ paymentPayload: PaymentPayloadSchema,
1840
+ paymentRequirements: PaymentRequirementsSchema
1841
+ });
1842
+ var VerifyRequestSchema = import_zod3.z.object({
1843
+ paymentPayload: PaymentPayloadSchema,
1844
+ paymentRequirements: PaymentRequirementsSchema
1845
+ });
1846
+ var VerifyResponseSchema = import_zod3.z.object({
1847
+ isValid: import_zod3.z.boolean(),
1848
+ invalidReason: import_zod3.z.enum(ErrorReasons).optional(),
1849
+ payer: EvmOrSvmAddress.optional()
1850
+ });
1851
+ var SettleResponseSchema = import_zod3.z.object({
1852
+ success: import_zod3.z.boolean(),
1853
+ errorReason: import_zod3.z.enum(ErrorReasons).optional(),
1854
+ payer: EvmOrSvmAddress.optional(),
1855
+ transaction: import_zod3.z.string().regex(MixedAddressRegex),
1856
+ network: NetworkSchema
1857
+ });
1858
+ var ListDiscoveryResourcesRequestSchema = import_zod3.z.object({
1859
+ type: import_zod3.z.string().optional(),
1860
+ limit: import_zod3.z.number().optional(),
1861
+ offset: import_zod3.z.number().optional()
1862
+ });
1863
+ var ListDiscoveryResourcesResponseSchema = import_zod3.z.object({
1864
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1865
+ items: import_zod3.z.array(DiscoveredResourceSchema),
1866
+ pagination: import_zod3.z.object({
1867
+ limit: import_zod3.z.number(),
1868
+ offset: import_zod3.z.number(),
1869
+ total: import_zod3.z.number()
1870
+ })
1871
+ });
1872
+ var SupportedPaymentKindSchema = import_zod3.z.object({
1873
+ x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
1874
+ scheme: import_zod3.z.enum(schemes),
1875
+ network: NetworkSchema,
1876
+ extra: import_zod3.z.record(import_zod3.z.any()).optional()
1877
+ });
1878
+ var SupportedPaymentKindsResponseSchema = import_zod3.z.object({
1879
+ kinds: import_zod3.z.array(SupportedPaymentKindSchema)
1880
+ });
1881
+
1882
+ // src/types/verify/facilitator.ts
1883
+ var import_zod4 = require("zod");
1884
+ var facilitatorRequestSchema = import_zod4.z.object({
1885
+ paymentHeader: import_zod4.z.string(),
1886
+ paymentRequirements: PaymentRequirementsSchema
1887
+ });
1888
+
1889
+ // src/shared/evm/usdc.ts
1890
+ function getUsdcAddress(client) {
1891
+ return config[client.chain.id.toString()].usdcAddress;
1892
+ }
1893
+ var versionCache = /* @__PURE__ */ new Map();
1894
+ async function getVersion(client, tokenAddress) {
1895
+ const address = tokenAddress ?? getUsdcAddress(client);
1896
+ const cacheKey = `${client.chain.id}-${address.toLowerCase()}`;
1897
+ if (versionCache.has(cacheKey)) {
1898
+ return versionCache.get(cacheKey);
1899
+ }
1900
+ let version = "1";
1901
+ try {
1902
+ const eip712DomainABI = [
1903
+ {
1904
+ inputs: [],
1905
+ name: "eip712Domain",
1906
+ outputs: [
1907
+ { name: "fields", type: "bytes1" },
1908
+ { name: "name", type: "string" },
1909
+ { name: "version", type: "string" },
1910
+ { name: "chainId", type: "uint256" },
1911
+ { name: "verifyingContract", type: "address" },
1912
+ { name: "salt", type: "bytes32" },
1913
+ { name: "extensions", type: "uint256[]" }
1914
+ ],
1915
+ stateMutability: "view",
1916
+ type: "function"
1803
1917
  }
1804
- ],
1805
- name: "SettledSplit",
1806
- type: "event"
1807
- },
1808
- {
1809
- anonymous: false,
1810
- inputs: [
1811
- {
1812
- indexed: true,
1813
- internalType: "address",
1814
- name: "token",
1815
- type: "address"
1816
- },
1817
- {
1818
- indexed: true,
1819
- internalType: "address",
1820
- name: "payer",
1821
- type: "address"
1822
- },
1823
- {
1824
- indexed: true,
1825
- internalType: "address",
1826
- name: "seller",
1827
- type: "address"
1828
- },
1829
- {
1830
- indexed: false,
1831
- internalType: "uint256",
1832
- name: "amount",
1833
- type: "uint256"
1834
- },
1835
- {
1836
- indexed: false,
1837
- internalType: "string",
1838
- name: "path",
1839
- type: "string"
1918
+ ];
1919
+ const result = await client.readContract({
1920
+ address,
1921
+ abi: eip712DomainABI,
1922
+ functionName: "eip712Domain"
1923
+ });
1924
+ version = result[2];
1925
+ } catch {
1926
+ try {
1927
+ const result = await client.readContract({
1928
+ address,
1929
+ abi: usdcABI,
1930
+ functionName: "version"
1931
+ });
1932
+ version = result;
1933
+ } catch {
1934
+ console.warn(
1935
+ `Neither eip712Domain() nor version() available for token ${address}, using default: ${version}`
1936
+ );
1937
+ }
1938
+ }
1939
+ versionCache.set(cacheKey, version);
1940
+ return version;
1941
+ }
1942
+
1943
+ // src/shared/evm/erc20.ts
1944
+ async function getERC20Balance(client, erc20Address, address) {
1945
+ const balance = await client.readContract({
1946
+ address: erc20Address,
1947
+ abi: usdcABI,
1948
+ functionName: "balanceOf",
1949
+ args: [address]
1950
+ });
1951
+ return balance;
1952
+ }
1953
+ async function getERC20Allowance(client, erc20Address, owner, spender) {
1954
+ const allowance = await client.readContract({
1955
+ address: erc20Address,
1956
+ abi: usdcABI,
1957
+ functionName: "allowance",
1958
+ args: [owner, spender]
1959
+ });
1960
+ return allowance;
1961
+ }
1962
+
1963
+ // src/shared/svm/transaction.ts
1964
+ var import_kit3 = require("@solana/kit");
1965
+ var import_token = require("@solana-program/token");
1966
+ var import_token_2022 = require("@solana-program/token-2022");
1967
+ function decodeTransactionFromPayload(svmPayload) {
1968
+ try {
1969
+ const base64Encoder = (0, import_kit3.getBase64Encoder)();
1970
+ const transactionBytes = base64Encoder.encode(svmPayload.transaction);
1971
+ const transactionDecoder = (0, import_kit3.getTransactionDecoder)();
1972
+ return transactionDecoder.decode(transactionBytes);
1973
+ } catch (error) {
1974
+ console.error("error", error);
1975
+ throw new Error("invalid_exact_svm_payload_transaction");
1976
+ }
1977
+ }
1978
+ function getTokenPayerFromTransaction(transaction) {
1979
+ const compiled = (0, import_kit3.getCompiledTransactionMessageDecoder)().decode(
1980
+ transaction.messageBytes
1981
+ );
1982
+ const staticAccounts = compiled.staticAccounts ?? [];
1983
+ const instructions = compiled.instructions ?? [];
1984
+ for (const ix of instructions) {
1985
+ const programIndex = ix.programAddressIndex;
1986
+ const programAddress = staticAccounts[programIndex].toString();
1987
+ if (programAddress === import_token.TOKEN_PROGRAM_ADDRESS.toString() || programAddress === import_token_2022.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
1988
+ const accountIndices = ix.accountIndices ?? [];
1989
+ if (accountIndices.length >= 4) {
1990
+ const ownerIndex = accountIndices[3];
1991
+ const ownerAddress = staticAccounts[ownerIndex].toString();
1992
+ if (ownerAddress) return ownerAddress;
1840
1993
  }
1841
- ],
1842
- name: "SettledDirect",
1843
- type: "event"
1994
+ }
1844
1995
  }
1845
- ];
1996
+ return "";
1997
+ }
1998
+ async function signAndSimulateTransaction(signer, transaction, rpc) {
1999
+ const signedTransaction = await (0, import_kit3.partiallySignTransaction)([signer.keyPair], transaction);
2000
+ const base64EncodedTransaction = (0, import_kit3.getBase64EncodedWireTransaction)(signedTransaction);
2001
+ const simulateTxConfig = {
2002
+ sigVerify: true,
2003
+ replaceRecentBlockhash: false,
2004
+ commitment: "confirmed",
2005
+ encoding: "base64",
2006
+ accounts: void 0,
2007
+ innerInstructions: void 0,
2008
+ minContextSlot: void 0
2009
+ };
2010
+ const simulateResult = await rpc.simulateTransaction(base64EncodedTransaction, simulateTxConfig).send();
2011
+ return simulateResult;
2012
+ }
1846
2013
 
1847
2014
  // src/schemes/exact/evm/eip3009/facilitator.ts
1848
2015
  async function verify(client, payload, paymentRequirements) {
@@ -1908,7 +2075,7 @@ async function verify(client, payload, paymentRequirements) {
1908
2075
  payer: exactEvmPayload.authorization.from
1909
2076
  };
1910
2077
  }
1911
- if ((0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.payTo) && (0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.extra?.relayer)) {
2078
+ if ((0, import_viem2.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem2.getAddress)(paymentRequirements.payTo)) {
1912
2079
  return {
1913
2080
  isValid: false,
1914
2081
  invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
@@ -1981,57 +2148,36 @@ async function settle(wallet, paymentPayload, paymentRequirements) {
1981
2148
  };
1982
2149
  }
1983
2150
  const { signature } = (0, import_viem2.parseErc6492Signature)(payload.signature);
1984
- let tx;
1985
- if (paymentRequirements.extra?.relayer) {
1986
- const sig = (0, import_viem2.hexToSignature)(signature);
1987
- const v = Number(sig.v);
1988
- const r = sig.r;
1989
- const s = sig.s;
1990
- tx = await wallet.writeContract({
1991
- address: paymentRequirements.extra.relayer,
1992
- abi: permitProxyContractABI,
1993
- functionName: "settleWithERC3009",
1994
- args: [
1995
- paymentRequirements.asset,
1996
- // token
1997
- payload.authorization.from,
1998
- // payer
1999
- paymentRequirements.payTo,
2000
- // seller
2001
- BigInt(payload.authorization.value),
2002
- // amount
2003
- BigInt(payload.authorization.validAfter),
2004
- // validAfter
2005
- BigInt(payload.authorization.validBefore),
2006
- // validBefore
2007
- payload.authorization.nonce,
2008
- // nonce
2009
- v,
2010
- // v (uint8)
2011
- r,
2012
- // r (bytes32)
2013
- s
2014
- // s (bytes32)
2015
- ],
2016
- chain: wallet.chain
2017
- });
2018
- } else {
2019
- tx = await wallet.writeContract({
2020
- address: paymentRequirements.asset,
2021
- abi: usdcABI,
2022
- functionName: "transferWithAuthorization",
2023
- args: [
2024
- payload.authorization.from,
2025
- payload.authorization.to,
2026
- BigInt(payload.authorization.value),
2027
- BigInt(payload.authorization.validAfter),
2028
- BigInt(payload.authorization.validBefore),
2029
- payload.authorization.nonce,
2030
- signature
2031
- ],
2032
- chain: wallet.chain
2033
- });
2034
- }
2151
+ const sig = (0, import_viem2.hexToSignature)(signature);
2152
+ const v = Number(sig.v);
2153
+ const r = sig.r;
2154
+ const s = sig.s;
2155
+ const tx = await wallet.writeContract({
2156
+ address: paymentRequirements.payTo,
2157
+ abi: EIP7702SellerWalletMinimalAbi,
2158
+ functionName: "settleWithERC3009",
2159
+ args: [
2160
+ paymentRequirements.asset,
2161
+ // token
2162
+ payload.authorization.from,
2163
+ // payer
2164
+ BigInt(payload.authorization.value),
2165
+ // amount
2166
+ BigInt(payload.authorization.validAfter),
2167
+ // validAfter
2168
+ BigInt(payload.authorization.validBefore),
2169
+ // validBefore
2170
+ payload.authorization.nonce,
2171
+ // nonce
2172
+ v,
2173
+ // v (uint8)
2174
+ r,
2175
+ // r (bytes32)
2176
+ s
2177
+ // s (bytes32)
2178
+ ],
2179
+ chain: wallet.chain
2180
+ });
2035
2181
  const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
2036
2182
  if (receipt.status !== "success") {
2037
2183
  return {
@@ -2192,22 +2338,12 @@ async function verify2(client, payload, paymentRequirements) {
2192
2338
  payer: owner
2193
2339
  };
2194
2340
  }
2195
- if (paymentRequirements.extra?.relayer) {
2196
- if ((0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(paymentRequirements.extra?.relayer)) {
2197
- return {
2198
- isValid: false,
2199
- invalidReason: "invalid_spender_address",
2200
- payer: owner
2201
- };
2202
- }
2203
- } else {
2204
- if (client.account && (0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(client.account.address)) {
2205
- return {
2206
- isValid: false,
2207
- invalidReason: "invalid_spender_address",
2208
- payer: owner
2209
- };
2210
- }
2341
+ if ((0, import_viem4.getAddress)(spender) !== (0, import_viem4.getAddress)(paymentRequirements.payTo)) {
2342
+ return {
2343
+ isValid: false,
2344
+ invalidReason: "invalid_spender_address",
2345
+ payer: owner
2346
+ };
2211
2347
  }
2212
2348
  const balance = await getERC20Balance(client, erc20Address, owner);
2213
2349
  if (balance < BigInt(paymentRequirements.maxAmountRequired)) {
@@ -2250,84 +2386,40 @@ async function settle2(wallet, paymentPayload, paymentRequirements) {
2250
2386
  payer: permitPayload.authorization.owner
2251
2387
  };
2252
2388
  }
2253
- const { owner, spender, value, deadline } = permitPayload.authorization;
2389
+ const { owner, value, deadline } = permitPayload.authorization;
2254
2390
  const { v, r, s } = splitSignature(permitPayload.signature);
2255
2391
  const tokenAddress = paymentRequirements.asset;
2256
- const txNonce = await wallet.getTransactionCount({
2257
- address: wallet.account.address
2392
+ const transactionHash = await wallet.writeContract({
2393
+ address: paymentRequirements.payTo,
2394
+ abi: EIP7702SellerWalletMinimalAbi,
2395
+ functionName: "settleWithPermit",
2396
+ args: [
2397
+ tokenAddress,
2398
+ // token
2399
+ owner,
2400
+ // payer
2401
+ BigInt(value),
2402
+ // amount
2403
+ BigInt(deadline),
2404
+ // deadline
2405
+ v,
2406
+ // v
2407
+ r,
2408
+ // r
2409
+ s
2410
+ // s
2411
+ ],
2412
+ chain: wallet.chain
2258
2413
  });
2259
- let transactionHash;
2260
- if (paymentRequirements.extra?.relayer) {
2261
- transactionHash = await wallet.writeContract({
2262
- address: paymentRequirements.extra.relayer,
2263
- abi: permitProxyContractABI,
2264
- functionName: "settleWithPermit",
2265
- args: [
2266
- tokenAddress,
2267
- // token
2268
- owner,
2269
- // payer
2270
- paymentRequirements.payTo,
2271
- // seller
2272
- BigInt(value),
2273
- // amount
2274
- BigInt(deadline),
2275
- // deadline
2276
- v,
2277
- // v
2278
- r,
2279
- // r
2280
- s
2281
- // s
2282
- ],
2283
- chain: wallet.chain,
2284
- nonce: txNonce
2285
- });
2286
- const receipt = await wallet.waitForTransactionReceipt({ hash: transactionHash });
2287
- if (receipt.status !== "success") {
2288
- return {
2289
- success: false,
2290
- errorReason: "transaction_failed",
2291
- transaction: transactionHash,
2292
- network: paymentPayload.network,
2293
- payer: owner
2294
- };
2295
- }
2296
- } else {
2297
- const [permitTx, transferTx] = await Promise.all([
2298
- // Call permit to approve the spender
2299
- wallet.writeContract({
2300
- address: tokenAddress,
2301
- abi: erc20PermitABI,
2302
- functionName: "permit",
2303
- args: [owner, spender, BigInt(value), BigInt(deadline), v, r, s],
2304
- chain: wallet.chain,
2305
- nonce: txNonce
2306
- }),
2307
- // Call transferFrom to transfer tokens to payTo address
2308
- wallet.writeContract({
2309
- address: tokenAddress,
2310
- abi: erc20PermitABI,
2311
- functionName: "transferFrom",
2312
- args: [owner, paymentRequirements.payTo, BigInt(value)],
2313
- chain: wallet.chain,
2314
- nonce: txNonce + 1
2315
- })
2316
- ]);
2317
- const [, receipt] = await Promise.all([
2318
- wallet.waitForTransactionReceipt({ hash: permitTx }),
2319
- wallet.waitForTransactionReceipt({ hash: transferTx })
2320
- ]);
2321
- if (receipt.status !== "success") {
2322
- return {
2323
- success: false,
2324
- errorReason: "transaction_failed",
2325
- transaction: transferTx,
2326
- network: paymentPayload.network,
2327
- payer: owner
2328
- };
2329
- }
2330
- transactionHash = transferTx;
2414
+ const receipt = await wallet.waitForTransactionReceipt({ hash: transactionHash });
2415
+ if (receipt.status !== "success") {
2416
+ return {
2417
+ success: false,
2418
+ errorReason: "transaction_failed",
2419
+ transaction: transactionHash,
2420
+ network: paymentPayload.network,
2421
+ payer: owner
2422
+ };
2331
2423
  }
2332
2424
  return {
2333
2425
  success: true,
@@ -2695,7 +2787,7 @@ function preparePaymentHeader(from, x402Version, paymentRequirements) {
2695
2787
  signature: void 0,
2696
2788
  authorization: {
2697
2789
  from,
2698
- to: paymentRequirements.extra?.relayer || paymentRequirements.payTo,
2790
+ to: paymentRequirements.payTo,
2699
2791
  value: paymentRequirements.maxAmountRequired,
2700
2792
  validAfter: validAfter.toString(),
2701
2793
  validBefore: validBefore.toString(),
@@ -2753,7 +2845,7 @@ function preparePaymentHeader2(from, x402Version, paymentRequirements) {
2753
2845
  signature: void 0,
2754
2846
  authorization: {
2755
2847
  owner: from,
2756
- spender: paymentRequirements.extra?.relayer || paymentRequirements.payTo,
2848
+ spender: paymentRequirements.payTo,
2757
2849
  value: paymentRequirements.maxAmountRequired,
2758
2850
  deadline
2759
2851
  }