@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.
- package/dist/cjs/client/index.d.ts +1 -1
- package/dist/cjs/client/index.js +1 -1
- package/dist/cjs/client/index.js.map +1 -1
- package/dist/cjs/facilitator/index.d.ts +1 -1
- package/dist/cjs/facilitator/index.js +1169 -1077
- package/dist/cjs/facilitator/index.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1636 -1544
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/middleware-qavKyUnr.d.ts +93 -0
- package/dist/cjs/paywall/index.d.ts +1 -1
- package/dist/cjs/schemes/index.d.ts +1 -1
- package/dist/cjs/schemes/index.js +697 -605
- package/dist/cjs/schemes/index.js.map +1 -1
- package/dist/cjs/shared/index.d.ts +2 -2
- package/dist/cjs/types/index.d.ts +335 -4
- package/dist/cjs/types/index.js +426 -0
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/verify/index.d.ts +2 -2
- package/dist/esm/{chunk-VTLJOZXM.mjs → chunk-27VV4F7H.mjs} +3 -3
- package/dist/esm/chunk-27VV4F7H.mjs.map +1 -0
- package/dist/esm/{chunk-NPWDNT2P.mjs → chunk-5UE5XTGG.mjs} +428 -1
- package/dist/esm/chunk-5UE5XTGG.mjs.map +1 -0
- package/dist/esm/{chunk-KABV25HJ.mjs → chunk-VHQZ4KRR.mjs} +3 -3
- package/dist/esm/{chunk-WCQCFJWV.mjs → chunk-YWZNW3IG.mjs} +74 -409
- package/dist/esm/chunk-YWZNW3IG.mjs.map +1 -0
- package/dist/esm/{chunk-A6TSFIQP.mjs → chunk-YZUMYCYA.mjs} +3 -3
- package/dist/esm/client/index.d.mts +1 -1
- package/dist/esm/client/index.mjs +3 -3
- package/dist/esm/facilitator/index.d.mts +1 -1
- package/dist/esm/facilitator/index.mjs +4 -4
- package/dist/esm/index.d.mts +1 -1
- package/dist/esm/index.mjs +5 -5
- package/dist/esm/{middleware-DSDucaQ5.d.mts → middleware-CFWW-MAF.d.mts} +1 -1
- package/dist/esm/paywall/index.d.mts +1 -1
- package/dist/esm/schemes/index.d.mts +1 -1
- package/dist/esm/schemes/index.mjs +3 -3
- package/dist/esm/shared/index.d.mts +2 -2
- package/dist/esm/shared/index.mjs +1 -1
- package/dist/esm/types/index.d.mts +335 -4
- package/dist/esm/types/index.mjs +1 -1
- package/dist/esm/verify/index.d.mts +2 -2
- package/dist/esm/verify/index.mjs +1 -1
- package/dist/esm/x402Specs-B9zS3LnZ.d.mts +1778 -0
- package/package.json +1 -1
- package/dist/esm/chunk-NPWDNT2P.mjs.map +0 -1
- package/dist/esm/chunk-VTLJOZXM.mjs.map +0 -1
- package/dist/esm/chunk-WCQCFJWV.mjs.map +0 -1
- package/dist/{esm/x402Specs-BtRXj67U.d.mts → cjs/x402Specs-B9zS3LnZ.d.ts} +6 -6
- /package/dist/esm/{chunk-KABV25HJ.mjs.map → chunk-VHQZ4KRR.mjs.map} +0 -0
- /package/dist/esm/{chunk-A6TSFIQP.mjs.map → chunk-YZUMYCYA.mjs.map} +0 -0
package/dist/cjs/index.js
CHANGED
|
@@ -1077,1476 +1077,1643 @@ var usdcABI = [
|
|
|
1077
1077
|
}
|
|
1078
1078
|
];
|
|
1079
1079
|
|
|
1080
|
-
// src/types/shared/evm/
|
|
1081
|
-
var
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
}
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
}
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
network: NetworkSchema,
|
|
1374
|
-
payload: import_zod3.z.union([ExactEvmPayloadSchema, ExactSvmPayloadSchema])
|
|
1375
|
-
});
|
|
1376
|
-
var x402ResponseSchema = import_zod3.z.object({
|
|
1377
|
-
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1378
|
-
error: import_zod3.z.enum(ErrorReasons).optional(),
|
|
1379
|
-
accepts: import_zod3.z.array(PaymentRequirementsSchema).optional(),
|
|
1380
|
-
payer: import_zod3.z.string().regex(MixedAddressRegex).optional()
|
|
1381
|
-
});
|
|
1382
|
-
var HTTPVerbsSchema = import_zod3.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]);
|
|
1383
|
-
var HTTPRequestStructureSchema = import_zod3.z.object({
|
|
1384
|
-
type: import_zod3.z.literal("http"),
|
|
1385
|
-
method: HTTPVerbsSchema,
|
|
1386
|
-
queryParams: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).optional(),
|
|
1387
|
-
bodyType: import_zod3.z.enum(["json", "form-data", "multipart-form-data", "text", "binary"]).optional(),
|
|
1388
|
-
bodyFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional(),
|
|
1389
|
-
headerFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional()
|
|
1390
|
-
});
|
|
1391
|
-
var RequestStructureSchema = import_zod3.z.discriminatedUnion("type", [
|
|
1392
|
-
HTTPRequestStructureSchema
|
|
1393
|
-
// MCPRequestStructureSchema,
|
|
1394
|
-
// OpenAPIRequestStructureSchema,
|
|
1395
|
-
]);
|
|
1396
|
-
var DiscoveredResourceSchema = import_zod3.z.object({
|
|
1397
|
-
resource: import_zod3.z.string(),
|
|
1398
|
-
type: import_zod3.z.enum(["http"]),
|
|
1399
|
-
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1400
|
-
accepts: import_zod3.z.array(PaymentRequirementsSchema),
|
|
1401
|
-
lastUpdated: import_zod3.z.date(),
|
|
1402
|
-
metadata: import_zod3.z.record(import_zod3.z.any()).optional()
|
|
1403
|
-
});
|
|
1404
|
-
var SettleRequestSchema = import_zod3.z.object({
|
|
1405
|
-
paymentPayload: PaymentPayloadSchema,
|
|
1406
|
-
paymentRequirements: PaymentRequirementsSchema
|
|
1407
|
-
});
|
|
1408
|
-
var VerifyRequestSchema = import_zod3.z.object({
|
|
1409
|
-
paymentPayload: PaymentPayloadSchema,
|
|
1410
|
-
paymentRequirements: PaymentRequirementsSchema
|
|
1411
|
-
});
|
|
1412
|
-
var VerifyResponseSchema = import_zod3.z.object({
|
|
1413
|
-
isValid: import_zod3.z.boolean(),
|
|
1414
|
-
invalidReason: import_zod3.z.enum(ErrorReasons).optional(),
|
|
1415
|
-
payer: EvmOrSvmAddress.optional()
|
|
1416
|
-
});
|
|
1417
|
-
var SettleResponseSchema = import_zod3.z.object({
|
|
1418
|
-
success: import_zod3.z.boolean(),
|
|
1419
|
-
errorReason: import_zod3.z.enum(ErrorReasons).optional(),
|
|
1420
|
-
payer: EvmOrSvmAddress.optional(),
|
|
1421
|
-
transaction: import_zod3.z.string().regex(MixedAddressRegex),
|
|
1422
|
-
network: NetworkSchema
|
|
1423
|
-
});
|
|
1424
|
-
var ListDiscoveryResourcesRequestSchema = import_zod3.z.object({
|
|
1425
|
-
type: import_zod3.z.string().optional(),
|
|
1426
|
-
limit: import_zod3.z.number().optional(),
|
|
1427
|
-
offset: import_zod3.z.number().optional()
|
|
1428
|
-
});
|
|
1429
|
-
var ListDiscoveryResourcesResponseSchema = import_zod3.z.object({
|
|
1430
|
-
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1431
|
-
items: import_zod3.z.array(DiscoveredResourceSchema),
|
|
1432
|
-
pagination: import_zod3.z.object({
|
|
1433
|
-
limit: import_zod3.z.number(),
|
|
1434
|
-
offset: import_zod3.z.number(),
|
|
1435
|
-
total: import_zod3.z.number()
|
|
1436
|
-
})
|
|
1437
|
-
});
|
|
1438
|
-
var SupportedPaymentKindSchema = import_zod3.z.object({
|
|
1439
|
-
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1440
|
-
scheme: import_zod3.z.enum(schemes),
|
|
1441
|
-
network: NetworkSchema,
|
|
1442
|
-
extra: import_zod3.z.record(import_zod3.z.any()).optional()
|
|
1443
|
-
});
|
|
1444
|
-
var SupportedPaymentKindsResponseSchema = import_zod3.z.object({
|
|
1445
|
-
kinds: import_zod3.z.array(SupportedPaymentKindSchema)
|
|
1446
|
-
});
|
|
1447
|
-
|
|
1448
|
-
// src/types/verify/facilitator.ts
|
|
1449
|
-
var import_zod4 = require("zod");
|
|
1450
|
-
var facilitatorRequestSchema = import_zod4.z.object({
|
|
1451
|
-
paymentHeader: import_zod4.z.string(),
|
|
1452
|
-
paymentRequirements: PaymentRequirementsSchema
|
|
1453
|
-
});
|
|
1454
|
-
|
|
1455
|
-
// src/shared/evm/usdc.ts
|
|
1456
|
-
function getUsdcAddress(client) {
|
|
1457
|
-
return config[client.chain.id.toString()].usdcAddress;
|
|
1458
|
-
}
|
|
1459
|
-
function getUsdcChainConfigForChain(chainId) {
|
|
1460
|
-
return config[chainId.toString()];
|
|
1461
|
-
}
|
|
1462
|
-
var versionCache = /* @__PURE__ */ new Map();
|
|
1463
|
-
async function getVersion(client, tokenAddress) {
|
|
1464
|
-
const address = tokenAddress ?? getUsdcAddress(client);
|
|
1465
|
-
const cacheKey = `${client.chain.id}-${address.toLowerCase()}`;
|
|
1466
|
-
if (versionCache.has(cacheKey)) {
|
|
1467
|
-
return versionCache.get(cacheKey);
|
|
1468
|
-
}
|
|
1469
|
-
let version = "1";
|
|
1470
|
-
try {
|
|
1471
|
-
const eip712DomainABI = [
|
|
1080
|
+
// src/types/shared/evm/eip7702ABI.ts
|
|
1081
|
+
var EIP7702SellerWalletMinimalAbi = [
|
|
1082
|
+
{
|
|
1083
|
+
inputs: [],
|
|
1084
|
+
name: "InvalidAmount",
|
|
1085
|
+
type: "error"
|
|
1086
|
+
},
|
|
1087
|
+
{
|
|
1088
|
+
inputs: [],
|
|
1089
|
+
name: "NotOwner",
|
|
1090
|
+
type: "error"
|
|
1091
|
+
},
|
|
1092
|
+
{
|
|
1093
|
+
inputs: [],
|
|
1094
|
+
name: "ReentrancyGuardReentrantCall",
|
|
1095
|
+
type: "error"
|
|
1096
|
+
},
|
|
1097
|
+
{
|
|
1098
|
+
inputs: [
|
|
1099
|
+
{
|
|
1100
|
+
internalType: "address",
|
|
1101
|
+
name: "token",
|
|
1102
|
+
type: "address"
|
|
1103
|
+
}
|
|
1104
|
+
],
|
|
1105
|
+
name: "SafeERC20FailedOperation",
|
|
1106
|
+
type: "error"
|
|
1107
|
+
},
|
|
1108
|
+
{
|
|
1109
|
+
inputs: [],
|
|
1110
|
+
name: "ZeroAddress",
|
|
1111
|
+
type: "error"
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
anonymous: false,
|
|
1115
|
+
inputs: [
|
|
1116
|
+
{
|
|
1117
|
+
indexed: true,
|
|
1118
|
+
internalType: "address",
|
|
1119
|
+
name: "beneficiary",
|
|
1120
|
+
type: "address"
|
|
1121
|
+
}
|
|
1122
|
+
],
|
|
1123
|
+
name: "ConfigUpdated",
|
|
1124
|
+
type: "event"
|
|
1125
|
+
},
|
|
1126
|
+
{
|
|
1127
|
+
anonymous: false,
|
|
1128
|
+
inputs: [
|
|
1129
|
+
{
|
|
1130
|
+
indexed: true,
|
|
1131
|
+
internalType: "address",
|
|
1132
|
+
name: "token",
|
|
1133
|
+
type: "address"
|
|
1134
|
+
},
|
|
1135
|
+
{
|
|
1136
|
+
indexed: true,
|
|
1137
|
+
internalType: "address",
|
|
1138
|
+
name: "payer",
|
|
1139
|
+
type: "address"
|
|
1140
|
+
},
|
|
1141
|
+
{
|
|
1142
|
+
indexed: true,
|
|
1143
|
+
internalType: "address",
|
|
1144
|
+
name: "facilitator",
|
|
1145
|
+
type: "address"
|
|
1146
|
+
},
|
|
1147
|
+
{
|
|
1148
|
+
indexed: false,
|
|
1149
|
+
internalType: "uint256",
|
|
1150
|
+
name: "amount",
|
|
1151
|
+
type: "uint256"
|
|
1152
|
+
},
|
|
1153
|
+
{
|
|
1154
|
+
indexed: false,
|
|
1155
|
+
internalType: "uint256",
|
|
1156
|
+
name: "beneficiaryAmount",
|
|
1157
|
+
type: "uint256"
|
|
1158
|
+
},
|
|
1159
|
+
{
|
|
1160
|
+
indexed: false,
|
|
1161
|
+
internalType: "uint256",
|
|
1162
|
+
name: "feeAmount",
|
|
1163
|
+
type: "uint256"
|
|
1164
|
+
},
|
|
1165
|
+
{
|
|
1166
|
+
indexed: false,
|
|
1167
|
+
internalType: "string",
|
|
1168
|
+
name: "method",
|
|
1169
|
+
type: "string"
|
|
1170
|
+
}
|
|
1171
|
+
],
|
|
1172
|
+
name: "SettlementExecuted",
|
|
1173
|
+
type: "event"
|
|
1174
|
+
},
|
|
1175
|
+
{
|
|
1176
|
+
stateMutability: "payable",
|
|
1177
|
+
type: "fallback"
|
|
1178
|
+
},
|
|
1179
|
+
{
|
|
1180
|
+
inputs: [],
|
|
1181
|
+
name: "BASIS_POINTS",
|
|
1182
|
+
outputs: [
|
|
1183
|
+
{
|
|
1184
|
+
internalType: "uint96",
|
|
1185
|
+
name: "",
|
|
1186
|
+
type: "uint96"
|
|
1187
|
+
}
|
|
1188
|
+
],
|
|
1189
|
+
stateMutability: "view",
|
|
1190
|
+
type: "function"
|
|
1191
|
+
},
|
|
1192
|
+
{
|
|
1193
|
+
inputs: [],
|
|
1194
|
+
name: "FEE_BPS",
|
|
1195
|
+
outputs: [
|
|
1196
|
+
{
|
|
1197
|
+
internalType: "uint96",
|
|
1198
|
+
name: "",
|
|
1199
|
+
type: "uint96"
|
|
1200
|
+
}
|
|
1201
|
+
],
|
|
1202
|
+
stateMutability: "view",
|
|
1203
|
+
type: "function"
|
|
1204
|
+
},
|
|
1205
|
+
{
|
|
1206
|
+
inputs: [],
|
|
1207
|
+
name: "FEE_RECIPIENT",
|
|
1208
|
+
outputs: [
|
|
1209
|
+
{
|
|
1210
|
+
internalType: "address",
|
|
1211
|
+
name: "",
|
|
1212
|
+
type: "address"
|
|
1213
|
+
}
|
|
1214
|
+
],
|
|
1215
|
+
stateMutability: "view",
|
|
1216
|
+
type: "function"
|
|
1217
|
+
},
|
|
1218
|
+
{
|
|
1219
|
+
inputs: [],
|
|
1220
|
+
name: "getConfig",
|
|
1221
|
+
outputs: [
|
|
1222
|
+
{
|
|
1223
|
+
internalType: "address",
|
|
1224
|
+
name: "beneficiary",
|
|
1225
|
+
type: "address"
|
|
1226
|
+
},
|
|
1227
|
+
{
|
|
1228
|
+
internalType: "bool",
|
|
1229
|
+
name: "initialized",
|
|
1230
|
+
type: "bool"
|
|
1231
|
+
}
|
|
1232
|
+
],
|
|
1233
|
+
stateMutability: "view",
|
|
1234
|
+
type: "function"
|
|
1235
|
+
},
|
|
1236
|
+
{
|
|
1237
|
+
inputs: [],
|
|
1238
|
+
name: "getEffectiveConfig",
|
|
1239
|
+
outputs: [
|
|
1240
|
+
{
|
|
1241
|
+
internalType: "address",
|
|
1242
|
+
name: "beneficiary",
|
|
1243
|
+
type: "address"
|
|
1244
|
+
},
|
|
1245
|
+
{
|
|
1246
|
+
internalType: "address",
|
|
1247
|
+
name: "feeRecipient",
|
|
1248
|
+
type: "address"
|
|
1249
|
+
},
|
|
1250
|
+
{
|
|
1251
|
+
internalType: "uint96",
|
|
1252
|
+
name: "feeBps",
|
|
1253
|
+
type: "uint96"
|
|
1254
|
+
}
|
|
1255
|
+
],
|
|
1256
|
+
stateMutability: "view",
|
|
1257
|
+
type: "function"
|
|
1258
|
+
},
|
|
1259
|
+
{
|
|
1260
|
+
inputs: [
|
|
1261
|
+
{
|
|
1262
|
+
internalType: "address",
|
|
1263
|
+
name: "",
|
|
1264
|
+
type: "address"
|
|
1265
|
+
},
|
|
1266
|
+
{
|
|
1267
|
+
internalType: "address",
|
|
1268
|
+
name: "",
|
|
1269
|
+
type: "address"
|
|
1270
|
+
},
|
|
1271
|
+
{
|
|
1272
|
+
internalType: "uint256[]",
|
|
1273
|
+
name: "",
|
|
1274
|
+
type: "uint256[]"
|
|
1275
|
+
},
|
|
1276
|
+
{
|
|
1277
|
+
internalType: "uint256[]",
|
|
1278
|
+
name: "",
|
|
1279
|
+
type: "uint256[]"
|
|
1280
|
+
},
|
|
1281
|
+
{
|
|
1282
|
+
internalType: "bytes",
|
|
1283
|
+
name: "",
|
|
1284
|
+
type: "bytes"
|
|
1285
|
+
}
|
|
1286
|
+
],
|
|
1287
|
+
name: "onERC1155BatchReceived",
|
|
1288
|
+
outputs: [
|
|
1289
|
+
{
|
|
1290
|
+
internalType: "bytes4",
|
|
1291
|
+
name: "",
|
|
1292
|
+
type: "bytes4"
|
|
1293
|
+
}
|
|
1294
|
+
],
|
|
1295
|
+
stateMutability: "nonpayable",
|
|
1296
|
+
type: "function"
|
|
1297
|
+
},
|
|
1298
|
+
{
|
|
1299
|
+
inputs: [
|
|
1300
|
+
{
|
|
1301
|
+
internalType: "address",
|
|
1302
|
+
name: "",
|
|
1303
|
+
type: "address"
|
|
1304
|
+
},
|
|
1305
|
+
{
|
|
1306
|
+
internalType: "address",
|
|
1307
|
+
name: "",
|
|
1308
|
+
type: "address"
|
|
1309
|
+
},
|
|
1310
|
+
{
|
|
1311
|
+
internalType: "uint256",
|
|
1312
|
+
name: "",
|
|
1313
|
+
type: "uint256"
|
|
1314
|
+
},
|
|
1315
|
+
{
|
|
1316
|
+
internalType: "uint256",
|
|
1317
|
+
name: "",
|
|
1318
|
+
type: "uint256"
|
|
1319
|
+
},
|
|
1320
|
+
{
|
|
1321
|
+
internalType: "bytes",
|
|
1322
|
+
name: "",
|
|
1323
|
+
type: "bytes"
|
|
1324
|
+
}
|
|
1325
|
+
],
|
|
1326
|
+
name: "onERC1155Received",
|
|
1327
|
+
outputs: [
|
|
1328
|
+
{
|
|
1329
|
+
internalType: "bytes4",
|
|
1330
|
+
name: "",
|
|
1331
|
+
type: "bytes4"
|
|
1332
|
+
}
|
|
1333
|
+
],
|
|
1334
|
+
stateMutability: "nonpayable",
|
|
1335
|
+
type: "function"
|
|
1336
|
+
},
|
|
1337
|
+
{
|
|
1338
|
+
inputs: [
|
|
1339
|
+
{
|
|
1340
|
+
internalType: "address",
|
|
1341
|
+
name: "",
|
|
1342
|
+
type: "address"
|
|
1343
|
+
},
|
|
1344
|
+
{
|
|
1345
|
+
internalType: "address",
|
|
1346
|
+
name: "",
|
|
1347
|
+
type: "address"
|
|
1348
|
+
},
|
|
1349
|
+
{
|
|
1350
|
+
internalType: "uint256",
|
|
1351
|
+
name: "",
|
|
1352
|
+
type: "uint256"
|
|
1353
|
+
},
|
|
1354
|
+
{
|
|
1355
|
+
internalType: "bytes",
|
|
1356
|
+
name: "",
|
|
1357
|
+
type: "bytes"
|
|
1358
|
+
}
|
|
1359
|
+
],
|
|
1360
|
+
name: "onERC721Received",
|
|
1361
|
+
outputs: [
|
|
1362
|
+
{
|
|
1363
|
+
internalType: "bytes4",
|
|
1364
|
+
name: "",
|
|
1365
|
+
type: "bytes4"
|
|
1366
|
+
}
|
|
1367
|
+
],
|
|
1368
|
+
stateMutability: "nonpayable",
|
|
1369
|
+
type: "function"
|
|
1370
|
+
},
|
|
1371
|
+
{
|
|
1372
|
+
inputs: [
|
|
1472
1373
|
{
|
|
1473
|
-
|
|
1474
|
-
name: "
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
type: "
|
|
1374
|
+
internalType: "address",
|
|
1375
|
+
name: "token",
|
|
1376
|
+
type: "address"
|
|
1377
|
+
},
|
|
1378
|
+
{
|
|
1379
|
+
internalType: "address",
|
|
1380
|
+
name: "payer",
|
|
1381
|
+
type: "address"
|
|
1382
|
+
},
|
|
1383
|
+
{
|
|
1384
|
+
internalType: "uint256",
|
|
1385
|
+
name: "amount",
|
|
1386
|
+
type: "uint256"
|
|
1387
|
+
},
|
|
1388
|
+
{
|
|
1389
|
+
internalType: "uint256",
|
|
1390
|
+
name: "validAfter",
|
|
1391
|
+
type: "uint256"
|
|
1392
|
+
},
|
|
1393
|
+
{
|
|
1394
|
+
internalType: "uint256",
|
|
1395
|
+
name: "validBefore",
|
|
1396
|
+
type: "uint256"
|
|
1397
|
+
},
|
|
1398
|
+
{
|
|
1399
|
+
internalType: "bytes32",
|
|
1400
|
+
name: "nonce",
|
|
1401
|
+
type: "bytes32"
|
|
1402
|
+
},
|
|
1403
|
+
{
|
|
1404
|
+
internalType: "uint8",
|
|
1405
|
+
name: "v",
|
|
1406
|
+
type: "uint8"
|
|
1407
|
+
},
|
|
1408
|
+
{
|
|
1409
|
+
internalType: "bytes32",
|
|
1410
|
+
name: "r",
|
|
1411
|
+
type: "bytes32"
|
|
1412
|
+
},
|
|
1413
|
+
{
|
|
1414
|
+
internalType: "bytes32",
|
|
1415
|
+
name: "s",
|
|
1416
|
+
type: "bytes32"
|
|
1486
1417
|
}
|
|
1487
|
-
]
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
return allowance;
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
// src/shared/svm/transaction.ts
|
|
1533
|
-
var import_kit3 = require("@solana/kit");
|
|
1534
|
-
var import_token = require("@solana-program/token");
|
|
1535
|
-
var import_token_2022 = require("@solana-program/token-2022");
|
|
1536
|
-
function decodeTransactionFromPayload(svmPayload) {
|
|
1537
|
-
try {
|
|
1538
|
-
const base64Encoder = (0, import_kit3.getBase64Encoder)();
|
|
1539
|
-
const transactionBytes = base64Encoder.encode(svmPayload.transaction);
|
|
1540
|
-
const transactionDecoder = (0, import_kit3.getTransactionDecoder)();
|
|
1541
|
-
return transactionDecoder.decode(transactionBytes);
|
|
1542
|
-
} catch (error) {
|
|
1543
|
-
console.error("error", error);
|
|
1544
|
-
throw new Error("invalid_exact_svm_payload_transaction");
|
|
1545
|
-
}
|
|
1546
|
-
}
|
|
1547
|
-
function getTokenPayerFromTransaction(transaction) {
|
|
1548
|
-
const compiled = (0, import_kit3.getCompiledTransactionMessageDecoder)().decode(
|
|
1549
|
-
transaction.messageBytes
|
|
1550
|
-
);
|
|
1551
|
-
const staticAccounts = compiled.staticAccounts ?? [];
|
|
1552
|
-
const instructions = compiled.instructions ?? [];
|
|
1553
|
-
for (const ix of instructions) {
|
|
1554
|
-
const programIndex = ix.programAddressIndex;
|
|
1555
|
-
const programAddress = staticAccounts[programIndex].toString();
|
|
1556
|
-
if (programAddress === import_token.TOKEN_PROGRAM_ADDRESS.toString() || programAddress === import_token_2022.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
|
|
1557
|
-
const accountIndices = ix.accountIndices ?? [];
|
|
1558
|
-
if (accountIndices.length >= 4) {
|
|
1559
|
-
const ownerIndex = accountIndices[3];
|
|
1560
|
-
const ownerAddress = staticAccounts[ownerIndex].toString();
|
|
1561
|
-
if (ownerAddress) return ownerAddress;
|
|
1418
|
+
],
|
|
1419
|
+
name: "settleWithERC3009",
|
|
1420
|
+
outputs: [],
|
|
1421
|
+
stateMutability: "nonpayable",
|
|
1422
|
+
type: "function"
|
|
1423
|
+
},
|
|
1424
|
+
{
|
|
1425
|
+
inputs: [
|
|
1426
|
+
{
|
|
1427
|
+
internalType: "address",
|
|
1428
|
+
name: "token",
|
|
1429
|
+
type: "address"
|
|
1430
|
+
},
|
|
1431
|
+
{
|
|
1432
|
+
internalType: "address",
|
|
1433
|
+
name: "payer",
|
|
1434
|
+
type: "address"
|
|
1435
|
+
},
|
|
1436
|
+
{
|
|
1437
|
+
internalType: "uint256",
|
|
1438
|
+
name: "amount",
|
|
1439
|
+
type: "uint256"
|
|
1440
|
+
},
|
|
1441
|
+
{
|
|
1442
|
+
internalType: "uint256",
|
|
1443
|
+
name: "deadline",
|
|
1444
|
+
type: "uint256"
|
|
1445
|
+
},
|
|
1446
|
+
{
|
|
1447
|
+
internalType: "uint8",
|
|
1448
|
+
name: "v",
|
|
1449
|
+
type: "uint8"
|
|
1450
|
+
},
|
|
1451
|
+
{
|
|
1452
|
+
internalType: "bytes32",
|
|
1453
|
+
name: "r",
|
|
1454
|
+
type: "bytes32"
|
|
1455
|
+
},
|
|
1456
|
+
{
|
|
1457
|
+
internalType: "bytes32",
|
|
1458
|
+
name: "s",
|
|
1459
|
+
type: "bytes32"
|
|
1562
1460
|
}
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
}
|
|
1605
|
-
};
|
|
1606
|
-
if (isSignerWallet(walletClient)) {
|
|
1607
|
-
const signature = await walletClient.signTypedData(data);
|
|
1608
|
-
return {
|
|
1609
|
-
signature
|
|
1610
|
-
};
|
|
1611
|
-
} else if (isAccount(walletClient) && walletClient.signTypedData) {
|
|
1612
|
-
const signature = await walletClient.signTypedData(data);
|
|
1613
|
-
return {
|
|
1614
|
-
signature
|
|
1615
|
-
};
|
|
1616
|
-
} else {
|
|
1617
|
-
throw new Error("Invalid wallet client provided does not support signTypedData");
|
|
1461
|
+
],
|
|
1462
|
+
name: "settleWithPermit",
|
|
1463
|
+
outputs: [],
|
|
1464
|
+
stateMutability: "nonpayable",
|
|
1465
|
+
type: "function"
|
|
1466
|
+
},
|
|
1467
|
+
{
|
|
1468
|
+
inputs: [
|
|
1469
|
+
{
|
|
1470
|
+
internalType: "bytes4",
|
|
1471
|
+
name: "interfaceId",
|
|
1472
|
+
type: "bytes4"
|
|
1473
|
+
}
|
|
1474
|
+
],
|
|
1475
|
+
name: "supportsInterface",
|
|
1476
|
+
outputs: [
|
|
1477
|
+
{
|
|
1478
|
+
internalType: "bool",
|
|
1479
|
+
name: "",
|
|
1480
|
+
type: "bool"
|
|
1481
|
+
}
|
|
1482
|
+
],
|
|
1483
|
+
stateMutability: "view",
|
|
1484
|
+
type: "function"
|
|
1485
|
+
},
|
|
1486
|
+
{
|
|
1487
|
+
inputs: [
|
|
1488
|
+
{
|
|
1489
|
+
internalType: "address",
|
|
1490
|
+
name: "beneficiary_",
|
|
1491
|
+
type: "address"
|
|
1492
|
+
}
|
|
1493
|
+
],
|
|
1494
|
+
name: "updateConfig",
|
|
1495
|
+
outputs: [],
|
|
1496
|
+
stateMutability: "nonpayable",
|
|
1497
|
+
type: "function"
|
|
1498
|
+
},
|
|
1499
|
+
{
|
|
1500
|
+
stateMutability: "payable",
|
|
1501
|
+
type: "receive"
|
|
1618
1502
|
}
|
|
1503
|
+
];
|
|
1504
|
+
|
|
1505
|
+
// src/types/shared/evm/wallet.ts
|
|
1506
|
+
var import_viem = require("viem");
|
|
1507
|
+
var import_chains = require("viem/chains");
|
|
1508
|
+
var import_accounts = require("viem/accounts");
|
|
1509
|
+
function isSignerWallet(wallet) {
|
|
1510
|
+
return typeof wallet === "object" && wallet !== null && "chain" in wallet && "transport" in wallet;
|
|
1619
1511
|
}
|
|
1620
|
-
function
|
|
1621
|
-
const
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
);
|
|
1626
|
-
return (0, import_viem2.toHex)(cryptoObj.getRandomValues(new Uint8Array(32)));
|
|
1512
|
+
function isAccount(wallet) {
|
|
1513
|
+
const w = wallet;
|
|
1514
|
+
return typeof wallet === "object" && wallet !== null && typeof w.address === "string" && typeof w.type === "string" && // Check for essential signing capabilities
|
|
1515
|
+
typeof w.sign === "function" && typeof w.signMessage === "function" && typeof w.signTypedData === "function" && // Check for transaction signing (required by LocalAccount)
|
|
1516
|
+
typeof w.signTransaction === "function";
|
|
1627
1517
|
}
|
|
1628
1518
|
|
|
1629
|
-
// src/schemes/exact/evm/
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
...evmPayload,
|
|
1638
|
-
authorization: {
|
|
1639
|
-
...evmPayload.authorization,
|
|
1640
|
-
validAfter: evmPayload.authorization.validAfter.toString(),
|
|
1641
|
-
validBefore: evmPayload.authorization.validBefore.toString()
|
|
1642
|
-
}
|
|
1643
|
-
};
|
|
1644
|
-
} else if (evmPayload.authorizationType === "permit") {
|
|
1645
|
-
processedPayload = {
|
|
1646
|
-
...evmPayload,
|
|
1647
|
-
authorization: {
|
|
1648
|
-
...evmPayload.authorization,
|
|
1649
|
-
deadline: evmPayload.authorization.deadline.toString(),
|
|
1650
|
-
nonce: evmPayload.authorization.nonce.toString()
|
|
1651
|
-
}
|
|
1652
|
-
};
|
|
1653
|
-
} else {
|
|
1654
|
-
processedPayload = {
|
|
1655
|
-
...evmPayload,
|
|
1656
|
-
authorization: {
|
|
1657
|
-
...evmPayload.authorization,
|
|
1658
|
-
deadline: evmPayload.authorization.deadline.toString(),
|
|
1659
|
-
nonce: evmPayload.authorization.nonce.toString()
|
|
1660
|
-
}
|
|
1661
|
-
};
|
|
1662
|
-
}
|
|
1663
|
-
safe = {
|
|
1664
|
-
...payment,
|
|
1665
|
-
payload: processedPayload
|
|
1666
|
-
};
|
|
1667
|
-
return safeBase64Encode(JSON.stringify(safe));
|
|
1668
|
-
}
|
|
1669
|
-
if (SupportedSVMNetworks.includes(payment.network)) {
|
|
1670
|
-
safe = { ...payment, payload: payment.payload };
|
|
1671
|
-
return safeBase64Encode(JSON.stringify(safe));
|
|
1519
|
+
// src/schemes/exact/evm/eip3009/sign.ts
|
|
1520
|
+
var import_viem2 = require("viem");
|
|
1521
|
+
|
|
1522
|
+
// src/shared/base64.ts
|
|
1523
|
+
var Base64EncodedRegex = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
1524
|
+
function safeBase64Encode(data) {
|
|
1525
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
|
|
1526
|
+
return globalThis.btoa(data);
|
|
1672
1527
|
}
|
|
1673
|
-
|
|
1528
|
+
return Buffer.from(data).toString("base64");
|
|
1674
1529
|
}
|
|
1675
1530
|
|
|
1676
|
-
// src/
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1531
|
+
// src/types/shared/money.ts
|
|
1532
|
+
var import_zod = require("zod");
|
|
1533
|
+
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));
|
|
1534
|
+
|
|
1535
|
+
// src/types/shared/network.ts
|
|
1536
|
+
var import_zod2 = require("zod");
|
|
1537
|
+
var NetworkSchema = import_zod2.z.enum([
|
|
1538
|
+
"base-sepolia",
|
|
1539
|
+
"base",
|
|
1540
|
+
"avalanche-fuji",
|
|
1541
|
+
"avalanche",
|
|
1542
|
+
"iotex",
|
|
1543
|
+
"solana-devnet",
|
|
1544
|
+
"solana",
|
|
1545
|
+
"sei",
|
|
1546
|
+
"sei-testnet",
|
|
1547
|
+
"polygon",
|
|
1548
|
+
"polygon-amoy",
|
|
1549
|
+
"peaq",
|
|
1550
|
+
"bsc",
|
|
1551
|
+
"bsc-testnet"
|
|
1552
|
+
]);
|
|
1553
|
+
var SupportedEVMNetworks = [
|
|
1554
|
+
"base-sepolia",
|
|
1555
|
+
"base",
|
|
1556
|
+
"avalanche-fuji",
|
|
1557
|
+
"avalanche",
|
|
1558
|
+
"iotex",
|
|
1559
|
+
"sei",
|
|
1560
|
+
"sei-testnet",
|
|
1561
|
+
"polygon",
|
|
1562
|
+
"polygon-amoy",
|
|
1563
|
+
"peaq",
|
|
1564
|
+
"bsc",
|
|
1565
|
+
"bsc-testnet"
|
|
1566
|
+
];
|
|
1567
|
+
var EvmNetworkToChainId = /* @__PURE__ */ new Map([
|
|
1568
|
+
["base-sepolia", 84532],
|
|
1569
|
+
["base", 8453],
|
|
1570
|
+
["avalanche-fuji", 43113],
|
|
1571
|
+
["avalanche", 43114],
|
|
1572
|
+
["iotex", 4689],
|
|
1573
|
+
["sei", 1329],
|
|
1574
|
+
["sei-testnet", 1328],
|
|
1575
|
+
["polygon", 137],
|
|
1576
|
+
["polygon-amoy", 80002],
|
|
1577
|
+
["peaq", 3338],
|
|
1578
|
+
["bsc", 56],
|
|
1579
|
+
["bsc-testnet", 97]
|
|
1580
|
+
]);
|
|
1581
|
+
var SupportedSVMNetworks = ["solana-devnet", "solana"];
|
|
1582
|
+
var SvmNetworkToChainId = /* @__PURE__ */ new Map([
|
|
1583
|
+
["solana-devnet", 103],
|
|
1584
|
+
["solana", 101]
|
|
1585
|
+
]);
|
|
1586
|
+
var ChainIdToNetwork = Object.fromEntries(
|
|
1587
|
+
[...SupportedEVMNetworks, ...SupportedSVMNetworks].map((network) => [
|
|
1588
|
+
EvmNetworkToChainId.get(network),
|
|
1589
|
+
network
|
|
1590
|
+
])
|
|
1591
|
+
);
|
|
1592
|
+
|
|
1593
|
+
// src/shared/svm/wallet.ts
|
|
1594
|
+
var import_kit2 = require("@solana/kit");
|
|
1595
|
+
var import_base = require("@scure/base");
|
|
1596
|
+
|
|
1597
|
+
// src/shared/svm/rpc.ts
|
|
1598
|
+
var import_kit = require("@solana/kit");
|
|
1599
|
+
var DEVNET_RPC_URL = "https://api.devnet.solana.com";
|
|
1600
|
+
var MAINNET_RPC_URL = "https://api.mainnet-beta.solana.com";
|
|
1601
|
+
var DEVNET_WS_URL = "wss://api.devnet.solana.com";
|
|
1602
|
+
var MAINNET_WS_URL = "wss://api.mainnet-beta.solana.com";
|
|
1603
|
+
function createDevnetRpcClient(url) {
|
|
1604
|
+
return (0, import_kit.createSolanaRpc)(
|
|
1605
|
+
url ? (0, import_kit.devnet)(url) : (0, import_kit.devnet)(DEVNET_RPC_URL)
|
|
1606
|
+
);
|
|
1703
1607
|
}
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
...unsignedPaymentHeader,
|
|
1709
|
-
payload: {
|
|
1710
|
-
authorizationType: "eip3009",
|
|
1711
|
-
signature,
|
|
1712
|
-
authorization
|
|
1713
|
-
}
|
|
1714
|
-
};
|
|
1608
|
+
function createMainnetRpcClient(url) {
|
|
1609
|
+
return (0, import_kit.createSolanaRpc)(
|
|
1610
|
+
url ? (0, import_kit.mainnet)(url) : (0, import_kit.mainnet)(MAINNET_RPC_URL)
|
|
1611
|
+
);
|
|
1715
1612
|
}
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1613
|
+
function getRpcClient(network, url) {
|
|
1614
|
+
if (network === "solana-devnet") {
|
|
1615
|
+
return createDevnetRpcClient(url);
|
|
1616
|
+
} else if (network === "solana") {
|
|
1617
|
+
return createMainnetRpcClient(url);
|
|
1618
|
+
} else {
|
|
1619
|
+
throw new Error("Invalid network");
|
|
1620
|
+
}
|
|
1720
1621
|
}
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1622
|
+
function getRpcSubscriptions(network, url) {
|
|
1623
|
+
if (network === "solana-devnet") {
|
|
1624
|
+
return (0, import_kit.createSolanaRpcSubscriptions)((0, import_kit.devnet)(url ? httpToWs(url) : DEVNET_WS_URL));
|
|
1625
|
+
} else if (network === "solana") {
|
|
1626
|
+
return (0, import_kit.createSolanaRpcSubscriptions)((0, import_kit.mainnet)(url ? httpToWs(url) : MAINNET_WS_URL));
|
|
1627
|
+
} else {
|
|
1628
|
+
throw new Error("Invalid network");
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
function httpToWs(url) {
|
|
1632
|
+
if (url.startsWith("http")) {
|
|
1633
|
+
return url.replace("http", "ws");
|
|
1634
|
+
}
|
|
1635
|
+
return url;
|
|
1724
1636
|
}
|
|
1725
1637
|
|
|
1726
|
-
// src/
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
var import_token2 = require("@solana-program/token");
|
|
1730
|
-
var import_compute_budget = require("@solana-program/compute-budget");
|
|
1731
|
-
async function createPaymentHeader2(client, x402Version2, paymentRequirements, config2) {
|
|
1732
|
-
const paymentPayload = await createAndSignPayment(
|
|
1733
|
-
client,
|
|
1734
|
-
x402Version2,
|
|
1735
|
-
paymentRequirements,
|
|
1736
|
-
config2
|
|
1737
|
-
);
|
|
1738
|
-
return encodePayment(paymentPayload);
|
|
1638
|
+
// src/shared/svm/wallet.ts
|
|
1639
|
+
function isSignerWallet2(wallet) {
|
|
1640
|
+
return (0, import_kit2.isKeyPairSigner)(wallet);
|
|
1739
1641
|
}
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
config2
|
|
1745
|
-
);
|
|
1746
|
-
const signedTransaction = await (0, import_kit4.partiallySignTransactionMessageWithSigners)(transactionMessage);
|
|
1747
|
-
const base64EncodedWireTransaction = (0, import_kit4.getBase64EncodedWireTransaction)(signedTransaction);
|
|
1748
|
-
return {
|
|
1749
|
-
scheme: paymentRequirements.scheme,
|
|
1750
|
-
network: paymentRequirements.network,
|
|
1751
|
-
x402Version: x402Version2,
|
|
1752
|
-
payload: {
|
|
1753
|
-
transaction: base64EncodedWireTransaction
|
|
1754
|
-
}
|
|
1755
|
-
};
|
|
1642
|
+
|
|
1643
|
+
// src/types/shared/wallet.ts
|
|
1644
|
+
function isEvmSignerWallet(wallet) {
|
|
1645
|
+
return isSignerWallet(wallet) || isAccount(wallet);
|
|
1756
1646
|
}
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1647
|
+
function isSvmSignerWallet(wallet) {
|
|
1648
|
+
return isSignerWallet2(wallet);
|
|
1649
|
+
}
|
|
1650
|
+
function isMultiNetworkSigner(wallet) {
|
|
1651
|
+
return "evm" in wallet && "svm" in wallet;
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
// src/types/shared/svm/regex.ts
|
|
1655
|
+
var SvmAddressRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
1656
|
+
|
|
1657
|
+
// src/shared/network.ts
|
|
1658
|
+
function getNetworkId(network) {
|
|
1659
|
+
if (EvmNetworkToChainId.has(network)) {
|
|
1660
|
+
return EvmNetworkToChainId.get(network);
|
|
1661
|
+
}
|
|
1662
|
+
if (SvmNetworkToChainId.has(network)) {
|
|
1663
|
+
return SvmNetworkToChainId.get(network);
|
|
1664
|
+
}
|
|
1665
|
+
throw new Error(`Unsupported network: ${network}`);
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// src/types/verify/x402Specs.ts
|
|
1669
|
+
var import_zod3 = require("zod");
|
|
1670
|
+
var EvmMaxAtomicUnits = 40;
|
|
1671
|
+
var EvmAddressRegex = /^0x[0-9a-fA-F]{40}$/;
|
|
1672
|
+
var MixedAddressRegex = /^0x[a-fA-F0-9]{40}|[A-Za-z0-9][A-Za-z0-9-]{0,34}[A-Za-z0-9]$/;
|
|
1673
|
+
var HexEncoded64ByteRegex = /^0x[0-9a-fA-F]{64}$/;
|
|
1674
|
+
var EvmSignatureRegex = /^0x[0-9a-fA-F]+$/;
|
|
1675
|
+
var schemes = ["exact"];
|
|
1676
|
+
var x402Versions = [1];
|
|
1677
|
+
var ErrorReasons = [
|
|
1678
|
+
"insufficient_funds",
|
|
1679
|
+
"invalid_exact_evm_payload_authorization_valid_after",
|
|
1680
|
+
"invalid_exact_evm_payload_authorization_valid_before",
|
|
1681
|
+
"invalid_exact_evm_payload_authorization_value",
|
|
1682
|
+
"invalid_exact_evm_payload_signature",
|
|
1683
|
+
"invalid_exact_evm_payload_recipient_mismatch",
|
|
1684
|
+
"invalid_exact_svm_payload_transaction",
|
|
1685
|
+
"invalid_exact_svm_payload_transaction_amount_mismatch",
|
|
1686
|
+
"invalid_exact_svm_payload_transaction_create_ata_instruction",
|
|
1687
|
+
"invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee",
|
|
1688
|
+
"invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset",
|
|
1689
|
+
"invalid_exact_svm_payload_transaction_instructions",
|
|
1690
|
+
"invalid_exact_svm_payload_transaction_instructions_length",
|
|
1691
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction",
|
|
1692
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction",
|
|
1693
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high",
|
|
1694
|
+
"invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked",
|
|
1695
|
+
"invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked",
|
|
1696
|
+
"invalid_exact_svm_payload_transaction_not_a_transfer_instruction",
|
|
1697
|
+
"invalid_exact_svm_payload_transaction_receiver_ata_not_found",
|
|
1698
|
+
"invalid_exact_svm_payload_transaction_sender_ata_not_found",
|
|
1699
|
+
"invalid_exact_svm_payload_transaction_simulation_failed",
|
|
1700
|
+
"invalid_exact_svm_payload_transaction_transfer_to_incorrect_ata",
|
|
1701
|
+
"invalid_network",
|
|
1702
|
+
"invalid_payload",
|
|
1703
|
+
"invalid_payment_requirements",
|
|
1704
|
+
"invalid_scheme",
|
|
1705
|
+
"invalid_payment",
|
|
1706
|
+
"payment_expired",
|
|
1707
|
+
"unsupported_scheme",
|
|
1708
|
+
"invalid_x402_version",
|
|
1709
|
+
"invalid_transaction_state",
|
|
1710
|
+
"settle_exact_svm_block_height_exceeded",
|
|
1711
|
+
"settle_exact_svm_transaction_confirmation_timed_out",
|
|
1712
|
+
"unexpected_settle_error",
|
|
1713
|
+
"unexpected_verify_error",
|
|
1714
|
+
// New error reasons for Permit and Permit2
|
|
1715
|
+
"unsupported_authorization_type",
|
|
1716
|
+
"invalid_authorization_type",
|
|
1717
|
+
"invalid_permit_signature",
|
|
1718
|
+
"invalid_permit2_signature",
|
|
1719
|
+
"invalid_permit2_witness_signature",
|
|
1720
|
+
"witness_recipient_mismatch",
|
|
1721
|
+
"permit_expired",
|
|
1722
|
+
"permit2_expired",
|
|
1723
|
+
"permit2_not_approved",
|
|
1724
|
+
"invalid_token_address",
|
|
1725
|
+
"invalid_spender_address",
|
|
1726
|
+
"token_mismatch",
|
|
1727
|
+
"insufficient_payment_amount",
|
|
1728
|
+
"insufficient_token_balance",
|
|
1729
|
+
"transaction_failed",
|
|
1730
|
+
"settlement_failed"
|
|
1731
|
+
];
|
|
1732
|
+
var isInteger = (value) => Number.isInteger(Number(value)) && Number(value) >= 0;
|
|
1733
|
+
var hasMaxLength = (maxLength) => (value) => value.length <= maxLength;
|
|
1734
|
+
var EvmOrSvmAddress = import_zod3.z.string().regex(EvmAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
|
|
1735
|
+
var mixedAddressOrSvmAddress = import_zod3.z.string().regex(MixedAddressRegex).or(import_zod3.z.string().regex(SvmAddressRegex));
|
|
1736
|
+
var PaymentRequirementsSchema = import_zod3.z.object({
|
|
1737
|
+
scheme: import_zod3.z.enum(schemes),
|
|
1738
|
+
network: NetworkSchema,
|
|
1739
|
+
maxAmountRequired: import_zod3.z.string().refine(isInteger),
|
|
1740
|
+
resource: import_zod3.z.string().url(),
|
|
1741
|
+
description: import_zod3.z.string(),
|
|
1742
|
+
mimeType: import_zod3.z.string(),
|
|
1743
|
+
outputSchema: import_zod3.z.record(import_zod3.z.any()).optional(),
|
|
1744
|
+
payTo: EvmOrSvmAddress,
|
|
1745
|
+
maxTimeoutSeconds: import_zod3.z.number().int(),
|
|
1746
|
+
asset: mixedAddressOrSvmAddress,
|
|
1747
|
+
paymentType: import_zod3.z.enum(["eip3009", "permit", "permit2"]).optional(),
|
|
1748
|
+
extra: import_zod3.z.record(import_zod3.z.any()).optional()
|
|
1749
|
+
});
|
|
1750
|
+
var ExactEvmPayloadAuthorizationSchema = import_zod3.z.object({
|
|
1751
|
+
from: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1752
|
+
to: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1753
|
+
value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
|
|
1754
|
+
validAfter: import_zod3.z.string().refine(isInteger),
|
|
1755
|
+
validBefore: import_zod3.z.string().refine(isInteger),
|
|
1756
|
+
nonce: import_zod3.z.string().regex(HexEncoded64ByteRegex)
|
|
1757
|
+
});
|
|
1758
|
+
var PermitEvmPayloadAuthorizationSchema = import_zod3.z.object({
|
|
1759
|
+
owner: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1760
|
+
spender: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1761
|
+
value: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
|
|
1762
|
+
deadline: import_zod3.z.string().refine(isInteger),
|
|
1763
|
+
nonce: import_zod3.z.string().refine(isInteger)
|
|
1764
|
+
});
|
|
1765
|
+
var Permit2EvmPayloadAuthorizationSchema = import_zod3.z.object({
|
|
1766
|
+
owner: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1767
|
+
spender: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1768
|
+
token: import_zod3.z.string().regex(EvmAddressRegex),
|
|
1769
|
+
amount: import_zod3.z.string().refine(isInteger).refine(hasMaxLength(EvmMaxAtomicUnits)),
|
|
1770
|
+
deadline: import_zod3.z.string().refine(isInteger),
|
|
1771
|
+
nonce: import_zod3.z.string().refine(isInteger),
|
|
1772
|
+
to: import_zod3.z.string().regex(EvmAddressRegex).optional()
|
|
1773
|
+
// Witness: binds recipient address to signature
|
|
1774
|
+
});
|
|
1775
|
+
var ExactEvmPayloadSchema = import_zod3.z.discriminatedUnion("authorizationType", [
|
|
1776
|
+
import_zod3.z.object({
|
|
1777
|
+
authorizationType: import_zod3.z.literal("eip3009"),
|
|
1778
|
+
signature: import_zod3.z.string().regex(EvmSignatureRegex),
|
|
1779
|
+
authorization: ExactEvmPayloadAuthorizationSchema
|
|
1780
|
+
}),
|
|
1781
|
+
import_zod3.z.object({
|
|
1782
|
+
authorizationType: import_zod3.z.literal("permit"),
|
|
1783
|
+
signature: import_zod3.z.string().regex(EvmSignatureRegex),
|
|
1784
|
+
authorization: PermitEvmPayloadAuthorizationSchema
|
|
1785
|
+
}),
|
|
1786
|
+
import_zod3.z.object({
|
|
1787
|
+
authorizationType: import_zod3.z.literal("permit2"),
|
|
1788
|
+
signature: import_zod3.z.string().regex(EvmSignatureRegex),
|
|
1789
|
+
authorization: Permit2EvmPayloadAuthorizationSchema
|
|
1790
|
+
})
|
|
1791
|
+
]);
|
|
1792
|
+
var ExactSvmPayloadSchema = import_zod3.z.object({
|
|
1793
|
+
transaction: import_zod3.z.string().regex(Base64EncodedRegex)
|
|
1794
|
+
});
|
|
1795
|
+
var PaymentPayloadSchema = import_zod3.z.object({
|
|
1796
|
+
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1797
|
+
scheme: import_zod3.z.enum(schemes),
|
|
1798
|
+
network: NetworkSchema,
|
|
1799
|
+
payload: import_zod3.z.union([ExactEvmPayloadSchema, ExactSvmPayloadSchema])
|
|
1800
|
+
});
|
|
1801
|
+
var x402ResponseSchema = import_zod3.z.object({
|
|
1802
|
+
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1803
|
+
error: import_zod3.z.enum(ErrorReasons).optional(),
|
|
1804
|
+
accepts: import_zod3.z.array(PaymentRequirementsSchema).optional(),
|
|
1805
|
+
payer: import_zod3.z.string().regex(MixedAddressRegex).optional()
|
|
1806
|
+
});
|
|
1807
|
+
var HTTPVerbsSchema = import_zod3.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]);
|
|
1808
|
+
var HTTPRequestStructureSchema = import_zod3.z.object({
|
|
1809
|
+
type: import_zod3.z.literal("http"),
|
|
1810
|
+
method: HTTPVerbsSchema,
|
|
1811
|
+
queryParams: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).optional(),
|
|
1812
|
+
bodyType: import_zod3.z.enum(["json", "form-data", "multipart-form-data", "text", "binary"]).optional(),
|
|
1813
|
+
bodyFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional(),
|
|
1814
|
+
headerFields: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.any()).optional()
|
|
1815
|
+
});
|
|
1816
|
+
var RequestStructureSchema = import_zod3.z.discriminatedUnion("type", [
|
|
1817
|
+
HTTPRequestStructureSchema
|
|
1818
|
+
// MCPRequestStructureSchema,
|
|
1819
|
+
// OpenAPIRequestStructureSchema,
|
|
1820
|
+
]);
|
|
1821
|
+
var DiscoveredResourceSchema = import_zod3.z.object({
|
|
1822
|
+
resource: import_zod3.z.string(),
|
|
1823
|
+
type: import_zod3.z.enum(["http"]),
|
|
1824
|
+
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1825
|
+
accepts: import_zod3.z.array(PaymentRequirementsSchema),
|
|
1826
|
+
lastUpdated: import_zod3.z.date(),
|
|
1827
|
+
metadata: import_zod3.z.record(import_zod3.z.any()).optional()
|
|
1828
|
+
});
|
|
1829
|
+
var SettleRequestSchema = import_zod3.z.object({
|
|
1830
|
+
paymentPayload: PaymentPayloadSchema,
|
|
1831
|
+
paymentRequirements: PaymentRequirementsSchema
|
|
1832
|
+
});
|
|
1833
|
+
var VerifyRequestSchema = import_zod3.z.object({
|
|
1834
|
+
paymentPayload: PaymentPayloadSchema,
|
|
1835
|
+
paymentRequirements: PaymentRequirementsSchema
|
|
1836
|
+
});
|
|
1837
|
+
var VerifyResponseSchema = import_zod3.z.object({
|
|
1838
|
+
isValid: import_zod3.z.boolean(),
|
|
1839
|
+
invalidReason: import_zod3.z.enum(ErrorReasons).optional(),
|
|
1840
|
+
payer: EvmOrSvmAddress.optional()
|
|
1841
|
+
});
|
|
1842
|
+
var SettleResponseSchema = import_zod3.z.object({
|
|
1843
|
+
success: import_zod3.z.boolean(),
|
|
1844
|
+
errorReason: import_zod3.z.enum(ErrorReasons).optional(),
|
|
1845
|
+
payer: EvmOrSvmAddress.optional(),
|
|
1846
|
+
transaction: import_zod3.z.string().regex(MixedAddressRegex),
|
|
1847
|
+
network: NetworkSchema
|
|
1848
|
+
});
|
|
1849
|
+
var ListDiscoveryResourcesRequestSchema = import_zod3.z.object({
|
|
1850
|
+
type: import_zod3.z.string().optional(),
|
|
1851
|
+
limit: import_zod3.z.number().optional(),
|
|
1852
|
+
offset: import_zod3.z.number().optional()
|
|
1853
|
+
});
|
|
1854
|
+
var ListDiscoveryResourcesResponseSchema = import_zod3.z.object({
|
|
1855
|
+
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1856
|
+
items: import_zod3.z.array(DiscoveredResourceSchema),
|
|
1857
|
+
pagination: import_zod3.z.object({
|
|
1858
|
+
limit: import_zod3.z.number(),
|
|
1859
|
+
offset: import_zod3.z.number(),
|
|
1860
|
+
total: import_zod3.z.number()
|
|
1861
|
+
})
|
|
1862
|
+
});
|
|
1863
|
+
var SupportedPaymentKindSchema = import_zod3.z.object({
|
|
1864
|
+
x402Version: import_zod3.z.number().refine((val) => x402Versions.includes(val)),
|
|
1865
|
+
scheme: import_zod3.z.enum(schemes),
|
|
1866
|
+
network: NetworkSchema,
|
|
1867
|
+
extra: import_zod3.z.record(import_zod3.z.any()).optional()
|
|
1868
|
+
});
|
|
1869
|
+
var SupportedPaymentKindsResponseSchema = import_zod3.z.object({
|
|
1870
|
+
kinds: import_zod3.z.array(SupportedPaymentKindSchema)
|
|
1871
|
+
});
|
|
1872
|
+
|
|
1873
|
+
// src/types/verify/facilitator.ts
|
|
1874
|
+
var import_zod4 = require("zod");
|
|
1875
|
+
var facilitatorRequestSchema = import_zod4.z.object({
|
|
1876
|
+
paymentHeader: import_zod4.z.string(),
|
|
1877
|
+
paymentRequirements: PaymentRequirementsSchema
|
|
1878
|
+
});
|
|
1879
|
+
|
|
1880
|
+
// src/shared/evm/usdc.ts
|
|
1881
|
+
function getUsdcAddress(client) {
|
|
1882
|
+
return config[client.chain.id.toString()].usdcAddress;
|
|
1784
1883
|
}
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
1788
|
-
const tokenMint = await (0, import_token_20222.fetchMint)(rpc, asset);
|
|
1789
|
-
const tokenProgramAddress = tokenMint.programAddress;
|
|
1790
|
-
if (tokenProgramAddress.toString() !== import_token2.TOKEN_PROGRAM_ADDRESS.toString() && tokenProgramAddress.toString() !== import_token_20222.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
|
|
1791
|
-
throw new Error("Asset was not created by a known token program");
|
|
1792
|
-
}
|
|
1793
|
-
const instructions = [];
|
|
1794
|
-
const createAtaIx = await createAtaInstructionOrUndefined(
|
|
1795
|
-
paymentRequirements,
|
|
1796
|
-
tokenProgramAddress,
|
|
1797
|
-
config2
|
|
1798
|
-
);
|
|
1799
|
-
if (createAtaIx) {
|
|
1800
|
-
instructions.push(createAtaIx);
|
|
1801
|
-
}
|
|
1802
|
-
const transferIx = await createTransferInstruction(
|
|
1803
|
-
client,
|
|
1804
|
-
paymentRequirements,
|
|
1805
|
-
tokenMint.data.decimals,
|
|
1806
|
-
tokenProgramAddress
|
|
1807
|
-
);
|
|
1808
|
-
instructions.push(transferIx);
|
|
1809
|
-
return instructions;
|
|
1884
|
+
function getUsdcChainConfigForChain(chainId) {
|
|
1885
|
+
return config[chainId.toString()];
|
|
1810
1886
|
}
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
const
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
);
|
|
1818
|
-
}
|
|
1819
|
-
const [destinationATAAddress] = await (0, import_token_20222.findAssociatedTokenPda)({
|
|
1820
|
-
mint: asset,
|
|
1821
|
-
owner: payTo,
|
|
1822
|
-
tokenProgram: tokenProgramAddress
|
|
1823
|
-
});
|
|
1824
|
-
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
1825
|
-
const maybeAccount = await (0, import_kit4.fetchEncodedAccount)(rpc, destinationATAAddress);
|
|
1826
|
-
if (!maybeAccount.exists) {
|
|
1827
|
-
return (0, import_token_20222.getCreateAssociatedTokenInstruction)({
|
|
1828
|
-
payer: paymentRequirements.extra?.feePayer,
|
|
1829
|
-
ata: destinationATAAddress,
|
|
1830
|
-
owner: payTo,
|
|
1831
|
-
mint: asset,
|
|
1832
|
-
tokenProgram: tokenProgramAddress
|
|
1833
|
-
});
|
|
1887
|
+
var versionCache = /* @__PURE__ */ new Map();
|
|
1888
|
+
async function getVersion(client, tokenAddress) {
|
|
1889
|
+
const address = tokenAddress ?? getUsdcAddress(client);
|
|
1890
|
+
const cacheKey = `${client.chain.id}-${address.toLowerCase()}`;
|
|
1891
|
+
if (versionCache.has(cacheKey)) {
|
|
1892
|
+
return versionCache.get(cacheKey);
|
|
1834
1893
|
}
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
mint: asset,
|
|
1853
|
-
destination: destinationATA,
|
|
1854
|
-
authority: client,
|
|
1855
|
-
amount: BigInt(amount),
|
|
1856
|
-
decimals
|
|
1857
|
-
},
|
|
1858
|
-
{ programAddress: tokenProgramAddress }
|
|
1859
|
-
);
|
|
1860
|
-
}
|
|
1861
|
-
|
|
1862
|
-
// src/client/createPaymentHeader.ts
|
|
1863
|
-
async function createPaymentHeader3(client, x402Version2, paymentRequirements, config2) {
|
|
1864
|
-
if (paymentRequirements.scheme === "exact") {
|
|
1865
|
-
if (SupportedEVMNetworks.includes(paymentRequirements.network)) {
|
|
1866
|
-
const evmClient = isMultiNetworkSigner(client) ? client.evm : client;
|
|
1867
|
-
if (!isEvmSignerWallet(evmClient)) {
|
|
1868
|
-
throw new Error("Invalid evm wallet client provided");
|
|
1869
|
-
}
|
|
1870
|
-
return await createPaymentHeader(
|
|
1871
|
-
evmClient,
|
|
1872
|
-
x402Version2,
|
|
1873
|
-
paymentRequirements
|
|
1874
|
-
);
|
|
1875
|
-
}
|
|
1876
|
-
if (SupportedSVMNetworks.includes(paymentRequirements.network)) {
|
|
1877
|
-
const svmClient = isMultiNetworkSigner(client) ? client.svm : client;
|
|
1878
|
-
if (!isSvmSignerWallet(svmClient)) {
|
|
1879
|
-
throw new Error("Invalid svm wallet client provided");
|
|
1894
|
+
let version = "1";
|
|
1895
|
+
try {
|
|
1896
|
+
const eip712DomainABI = [
|
|
1897
|
+
{
|
|
1898
|
+
inputs: [],
|
|
1899
|
+
name: "eip712Domain",
|
|
1900
|
+
outputs: [
|
|
1901
|
+
{ name: "fields", type: "bytes1" },
|
|
1902
|
+
{ name: "name", type: "string" },
|
|
1903
|
+
{ name: "version", type: "string" },
|
|
1904
|
+
{ name: "chainId", type: "uint256" },
|
|
1905
|
+
{ name: "verifyingContract", type: "address" },
|
|
1906
|
+
{ name: "salt", type: "bytes32" },
|
|
1907
|
+
{ name: "extensions", type: "uint256[]" }
|
|
1908
|
+
],
|
|
1909
|
+
stateMutability: "view",
|
|
1910
|
+
type: "function"
|
|
1880
1911
|
}
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1912
|
+
];
|
|
1913
|
+
const result = await client.readContract({
|
|
1914
|
+
address,
|
|
1915
|
+
abi: eip712DomainABI,
|
|
1916
|
+
functionName: "eip712Domain"
|
|
1917
|
+
});
|
|
1918
|
+
version = result[2];
|
|
1919
|
+
} catch {
|
|
1920
|
+
try {
|
|
1921
|
+
const result = await client.readContract({
|
|
1922
|
+
address,
|
|
1923
|
+
abi: usdcABI,
|
|
1924
|
+
functionName: "version"
|
|
1925
|
+
});
|
|
1926
|
+
version = result;
|
|
1927
|
+
} catch {
|
|
1928
|
+
console.warn(
|
|
1929
|
+
`Neither eip712Domain() nor version() available for token ${address}, using default: ${version}`
|
|
1886
1930
|
);
|
|
1887
1931
|
}
|
|
1888
|
-
throw new Error("Unsupported network");
|
|
1889
|
-
}
|
|
1890
|
-
throw new Error("Unsupported scheme");
|
|
1891
|
-
}
|
|
1892
|
-
|
|
1893
|
-
// src/client/preparePaymentHeader.ts
|
|
1894
|
-
function preparePaymentHeader2(from, x402Version2, paymentRequirements) {
|
|
1895
|
-
if (paymentRequirements.scheme === "exact" && SupportedEVMNetworks.includes(paymentRequirements.network)) {
|
|
1896
|
-
return preparePaymentHeader(from, x402Version2, paymentRequirements);
|
|
1897
1932
|
}
|
|
1898
|
-
|
|
1933
|
+
versionCache.set(cacheKey, version);
|
|
1934
|
+
return version;
|
|
1899
1935
|
}
|
|
1900
1936
|
|
|
1901
|
-
// src/
|
|
1902
|
-
function
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
return 1;
|
|
1909
|
-
}
|
|
1910
|
-
return 0;
|
|
1911
|
-
});
|
|
1912
|
-
const broadlyAcceptedPaymentRequirements = paymentRequirements.filter((requirement) => {
|
|
1913
|
-
const isExpectedScheme = !scheme || requirement.scheme === scheme;
|
|
1914
|
-
const isExpectedChain = !network || (Array.isArray(network) ? network.includes(requirement.network) : network == requirement.network);
|
|
1915
|
-
return isExpectedScheme && isExpectedChain;
|
|
1937
|
+
// src/shared/evm/erc20.ts
|
|
1938
|
+
async function getERC20Balance(client, erc20Address, address) {
|
|
1939
|
+
const balance = await client.readContract({
|
|
1940
|
+
address: erc20Address,
|
|
1941
|
+
abi: usdcABI,
|
|
1942
|
+
functionName: "balanceOf",
|
|
1943
|
+
args: [address]
|
|
1916
1944
|
});
|
|
1917
|
-
|
|
1918
|
-
|
|
1945
|
+
return balance;
|
|
1946
|
+
}
|
|
1947
|
+
async function getERC20Allowance(client, erc20Address, owner, spender) {
|
|
1948
|
+
const allowance = await client.readContract({
|
|
1949
|
+
address: erc20Address,
|
|
1950
|
+
abi: usdcABI,
|
|
1951
|
+
functionName: "allowance",
|
|
1952
|
+
args: [owner, spender]
|
|
1919
1953
|
});
|
|
1920
|
-
|
|
1921
|
-
return usdcRequirements[0];
|
|
1922
|
-
}
|
|
1923
|
-
if (broadlyAcceptedPaymentRequirements.length > 0) {
|
|
1924
|
-
return broadlyAcceptedPaymentRequirements[0];
|
|
1925
|
-
}
|
|
1926
|
-
return paymentRequirements[0];
|
|
1954
|
+
return allowance;
|
|
1927
1955
|
}
|
|
1928
1956
|
|
|
1929
|
-
// src/
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1957
|
+
// src/shared/svm/transaction.ts
|
|
1958
|
+
var import_kit3 = require("@solana/kit");
|
|
1959
|
+
var import_token = require("@solana-program/token");
|
|
1960
|
+
var import_token_2022 = require("@solana-program/token-2022");
|
|
1961
|
+
function decodeTransactionFromPayload(svmPayload) {
|
|
1962
|
+
try {
|
|
1963
|
+
const base64Encoder = (0, import_kit3.getBase64Encoder)();
|
|
1964
|
+
const transactionBytes = base64Encoder.encode(svmPayload.transaction);
|
|
1965
|
+
const transactionDecoder = (0, import_kit3.getTransactionDecoder)();
|
|
1966
|
+
return transactionDecoder.decode(transactionBytes);
|
|
1967
|
+
} catch (error) {
|
|
1968
|
+
console.error("error", error);
|
|
1969
|
+
throw new Error("invalid_exact_svm_payload_transaction");
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
function getTokenPayerFromTransaction(transaction) {
|
|
1973
|
+
const compiled = (0, import_kit3.getCompiledTransactionMessageDecoder)().decode(
|
|
1974
|
+
transaction.messageBytes
|
|
1975
|
+
);
|
|
1976
|
+
const staticAccounts = compiled.staticAccounts ?? [];
|
|
1977
|
+
const instructions = compiled.instructions ?? [];
|
|
1978
|
+
for (const ix of instructions) {
|
|
1979
|
+
const programIndex = ix.programAddressIndex;
|
|
1980
|
+
const programAddress = staticAccounts[programIndex].toString();
|
|
1981
|
+
if (programAddress === import_token.TOKEN_PROGRAM_ADDRESS.toString() || programAddress === import_token_2022.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
|
|
1982
|
+
const accountIndices = ix.accountIndices ?? [];
|
|
1983
|
+
if (accountIndices.length >= 4) {
|
|
1984
|
+
const ownerIndex = accountIndices[3];
|
|
1985
|
+
const ownerAddress = staticAccounts[ownerIndex].toString();
|
|
1986
|
+
if (ownerAddress) return ownerAddress;
|
|
1987
|
+
}
|
|
1935
1988
|
}
|
|
1936
|
-
const signedPaymentHeader = await signPaymentHeader(evmClient, paymentRequirements, unsignedPaymentHeader);
|
|
1937
|
-
return encodePayment(signedPaymentHeader);
|
|
1938
1989
|
}
|
|
1939
|
-
|
|
1990
|
+
return "";
|
|
1991
|
+
}
|
|
1992
|
+
async function signAndSimulateTransaction(signer, transaction, rpc) {
|
|
1993
|
+
const signedTransaction = await (0, import_kit3.partiallySignTransaction)([signer.keyPair], transaction);
|
|
1994
|
+
const base64EncodedTransaction = (0, import_kit3.getBase64EncodedWireTransaction)(signedTransaction);
|
|
1995
|
+
const simulateTxConfig = {
|
|
1996
|
+
sigVerify: true,
|
|
1997
|
+
replaceRecentBlockhash: false,
|
|
1998
|
+
commitment: "confirmed",
|
|
1999
|
+
encoding: "base64",
|
|
2000
|
+
accounts: void 0,
|
|
2001
|
+
innerInstructions: void 0,
|
|
2002
|
+
minContextSlot: void 0
|
|
2003
|
+
};
|
|
2004
|
+
const simulateResult = await rpc.simulateTransaction(base64EncodedTransaction, simulateTxConfig).send();
|
|
2005
|
+
return simulateResult;
|
|
1940
2006
|
}
|
|
1941
2007
|
|
|
1942
|
-
// src/schemes/exact/evm/eip3009/
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
throw new Error(`invalid_exact_svm_payload_transaction_simulation_failed`);
|
|
2008
|
+
// src/schemes/exact/evm/eip3009/sign.ts
|
|
2009
|
+
async function signAuthorization(walletClient, { from, to, value, validAfter, validBefore, nonce }, { asset, network, extra }) {
|
|
2010
|
+
const chainId = getNetworkId(network);
|
|
2011
|
+
const name = extra?.name;
|
|
2012
|
+
const version = extra?.version;
|
|
2013
|
+
const data = {
|
|
2014
|
+
types: authorizationTypes,
|
|
2015
|
+
domain: {
|
|
2016
|
+
name,
|
|
2017
|
+
version,
|
|
2018
|
+
chainId,
|
|
2019
|
+
verifyingContract: (0, import_viem2.getAddress)(asset)
|
|
2020
|
+
},
|
|
2021
|
+
primaryType: "TransferWithAuthorization",
|
|
2022
|
+
message: {
|
|
2023
|
+
from: (0, import_viem2.getAddress)(from),
|
|
2024
|
+
to: (0, import_viem2.getAddress)(to),
|
|
2025
|
+
value,
|
|
2026
|
+
validAfter,
|
|
2027
|
+
validBefore,
|
|
2028
|
+
nonce
|
|
1964
2029
|
}
|
|
2030
|
+
};
|
|
2031
|
+
if (isSignerWallet(walletClient)) {
|
|
2032
|
+
const signature = await walletClient.signTypedData(data);
|
|
1965
2033
|
return {
|
|
1966
|
-
|
|
1967
|
-
invalidReason: void 0,
|
|
1968
|
-
payer: getTokenPayerFromTransaction(decodedTransaction)
|
|
2034
|
+
signature
|
|
1969
2035
|
};
|
|
1970
|
-
}
|
|
1971
|
-
|
|
1972
|
-
if (ErrorReasons.includes(error.message)) {
|
|
1973
|
-
return {
|
|
1974
|
-
isValid: false,
|
|
1975
|
-
invalidReason: error.message,
|
|
1976
|
-
payer: (() => {
|
|
1977
|
-
try {
|
|
1978
|
-
const tx = decodeTransactionFromPayload(payload.payload);
|
|
1979
|
-
return getTokenPayerFromTransaction(tx);
|
|
1980
|
-
} catch {
|
|
1981
|
-
return void 0;
|
|
1982
|
-
}
|
|
1983
|
-
})()
|
|
1984
|
-
};
|
|
1985
|
-
}
|
|
1986
|
-
}
|
|
1987
|
-
console.error(error);
|
|
2036
|
+
} else if (isAccount(walletClient) && walletClient.signTypedData) {
|
|
2037
|
+
const signature = await walletClient.signTypedData(data);
|
|
1988
2038
|
return {
|
|
1989
|
-
|
|
1990
|
-
invalidReason: "unexpected_verify_error",
|
|
1991
|
-
payer: (() => {
|
|
1992
|
-
try {
|
|
1993
|
-
const tx = decodeTransactionFromPayload(payload.payload);
|
|
1994
|
-
return getTokenPayerFromTransaction(tx);
|
|
1995
|
-
} catch {
|
|
1996
|
-
return void 0;
|
|
1997
|
-
}
|
|
1998
|
-
})()
|
|
2039
|
+
signature
|
|
1999
2040
|
};
|
|
2041
|
+
} else {
|
|
2042
|
+
throw new Error("Invalid wallet client provided does not support signTypedData");
|
|
2000
2043
|
}
|
|
2001
2044
|
}
|
|
2002
|
-
function
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
throw new Error("invalid_network");
|
|
2008
|
-
}
|
|
2009
|
-
}
|
|
2010
|
-
async function transactionIntrospection(svmPayload, paymentRequirements, config2) {
|
|
2011
|
-
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2012
|
-
const decodedTransaction = decodeTransactionFromPayload(svmPayload);
|
|
2013
|
-
const compiledTransactionMessage = (0, import_kit5.getCompiledTransactionMessageDecoder)().decode(
|
|
2014
|
-
decodedTransaction.messageBytes
|
|
2015
|
-
);
|
|
2016
|
-
const transactionMessage = (0, import_kit5.decompileTransactionMessage)(
|
|
2017
|
-
compiledTransactionMessage
|
|
2045
|
+
function createNonce() {
|
|
2046
|
+
const cryptoObj = typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.getRandomValues === "function" ? globalThis.crypto : (
|
|
2047
|
+
// Dynamic require is needed to support node.js
|
|
2048
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
2049
|
+
require("crypto").webcrypto
|
|
2018
2050
|
);
|
|
2019
|
-
|
|
2051
|
+
return (0, import_viem2.toHex)(cryptoObj.getRandomValues(new Uint8Array(32)));
|
|
2020
2052
|
}
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2053
|
+
|
|
2054
|
+
// src/schemes/exact/evm/utils/paymentUtils.ts
|
|
2055
|
+
function encodePayment(payment) {
|
|
2056
|
+
let safe;
|
|
2057
|
+
if (SupportedEVMNetworks.includes(payment.network)) {
|
|
2058
|
+
const evmPayload = payment.payload;
|
|
2059
|
+
let processedPayload;
|
|
2060
|
+
if (evmPayload.authorizationType === "eip3009") {
|
|
2061
|
+
processedPayload = {
|
|
2062
|
+
...evmPayload,
|
|
2063
|
+
authorization: {
|
|
2064
|
+
...evmPayload.authorization,
|
|
2065
|
+
validAfter: evmPayload.authorization.validAfter.toString(),
|
|
2066
|
+
validBefore: evmPayload.authorization.validBefore.toString()
|
|
2067
|
+
}
|
|
2068
|
+
};
|
|
2069
|
+
} else if (evmPayload.authorizationType === "permit") {
|
|
2070
|
+
processedPayload = {
|
|
2071
|
+
...evmPayload,
|
|
2072
|
+
authorization: {
|
|
2073
|
+
...evmPayload.authorization,
|
|
2074
|
+
deadline: evmPayload.authorization.deadline.toString(),
|
|
2075
|
+
nonce: evmPayload.authorization.nonce.toString()
|
|
2076
|
+
}
|
|
2077
|
+
};
|
|
2078
|
+
} else {
|
|
2079
|
+
processedPayload = {
|
|
2080
|
+
...evmPayload,
|
|
2081
|
+
authorization: {
|
|
2082
|
+
...evmPayload.authorization,
|
|
2083
|
+
deadline: evmPayload.authorization.deadline.toString(),
|
|
2084
|
+
nonce: evmPayload.authorization.nonce.toString()
|
|
2085
|
+
}
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2088
|
+
safe = {
|
|
2089
|
+
...payment,
|
|
2090
|
+
payload: processedPayload
|
|
2091
|
+
};
|
|
2092
|
+
return safeBase64Encode(JSON.stringify(safe));
|
|
2024
2093
|
}
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
await verifyTransferInstruction(
|
|
2029
|
-
transactionMessage.instructions[2],
|
|
2030
|
-
paymentRequirements,
|
|
2031
|
-
{
|
|
2032
|
-
txHasCreateDestATAInstruction: false
|
|
2033
|
-
},
|
|
2034
|
-
rpc
|
|
2035
|
-
);
|
|
2036
|
-
} else {
|
|
2037
|
-
verifyCreateATAInstruction(transactionMessage.instructions[2], paymentRequirements);
|
|
2038
|
-
await verifyTransferInstruction(
|
|
2039
|
-
transactionMessage.instructions[3],
|
|
2040
|
-
paymentRequirements,
|
|
2041
|
-
{
|
|
2042
|
-
txHasCreateDestATAInstruction: true
|
|
2043
|
-
},
|
|
2044
|
-
rpc
|
|
2045
|
-
);
|
|
2094
|
+
if (SupportedSVMNetworks.includes(payment.network)) {
|
|
2095
|
+
safe = { ...payment, payload: payment.payload };
|
|
2096
|
+
return safeBase64Encode(JSON.stringify(safe));
|
|
2046
2097
|
}
|
|
2098
|
+
throw new Error("Invalid network");
|
|
2047
2099
|
}
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2100
|
+
|
|
2101
|
+
// src/schemes/exact/evm/eip3009/client.ts
|
|
2102
|
+
function preparePaymentHeader(from, x402Version2, paymentRequirements) {
|
|
2103
|
+
const nonce = createNonce();
|
|
2104
|
+
const validAfter = BigInt(
|
|
2105
|
+
Math.floor(Date.now() / 1e3) - 600
|
|
2106
|
+
// 10 minutes before
|
|
2107
|
+
).toString();
|
|
2108
|
+
const validBefore = BigInt(
|
|
2109
|
+
Math.floor(Date.now() / 1e3 + paymentRequirements.maxTimeoutSeconds)
|
|
2110
|
+
).toString();
|
|
2111
|
+
return {
|
|
2112
|
+
x402Version: x402Version2,
|
|
2113
|
+
scheme: paymentRequirements.scheme,
|
|
2114
|
+
network: paymentRequirements.network,
|
|
2115
|
+
payload: {
|
|
2116
|
+
authorizationType: "eip3009",
|
|
2117
|
+
signature: void 0,
|
|
2118
|
+
authorization: {
|
|
2119
|
+
from,
|
|
2120
|
+
to: paymentRequirements.payTo,
|
|
2121
|
+
value: paymentRequirements.maxAmountRequired,
|
|
2122
|
+
validAfter: validAfter.toString(),
|
|
2123
|
+
validBefore: validBefore.toString(),
|
|
2124
|
+
nonce
|
|
2125
|
+
}
|
|
2054
2126
|
}
|
|
2055
|
-
|
|
2056
|
-
instruction
|
|
2057
|
-
);
|
|
2058
|
-
} catch (error) {
|
|
2059
|
-
console.error(error);
|
|
2060
|
-
throw new Error(`invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction`);
|
|
2061
|
-
}
|
|
2062
|
-
}
|
|
2063
|
-
function verifyComputePriceInstruction(instruction) {
|
|
2064
|
-
if (instruction.programAddress.toString() !== import_compute_budget2.COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || instruction.data?.[0] !== 3) {
|
|
2065
|
-
throw new Error(`invalid_exact_svm_payload_transaction_instructions_compute_price_instruction`);
|
|
2066
|
-
}
|
|
2067
|
-
const parsedInstruction = (0, import_compute_budget2.parseSetComputeUnitPriceInstruction)(
|
|
2068
|
-
instruction
|
|
2069
|
-
);
|
|
2070
|
-
if (parsedInstruction.data.microLamports > 5 * 1e6) {
|
|
2071
|
-
throw new Error(
|
|
2072
|
-
`invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high`
|
|
2073
|
-
);
|
|
2074
|
-
}
|
|
2127
|
+
};
|
|
2075
2128
|
}
|
|
2076
|
-
function
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction`);
|
|
2088
|
-
}
|
|
2089
|
-
if (createATAInstruction.accounts.owner.address !== paymentRequirements.payTo) {
|
|
2090
|
-
throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee`);
|
|
2091
|
-
}
|
|
2092
|
-
if (createATAInstruction.accounts.mint.address !== paymentRequirements.asset) {
|
|
2093
|
-
throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset`);
|
|
2094
|
-
}
|
|
2129
|
+
async function signPaymentHeader(client, paymentRequirements, unsignedPaymentHeader) {
|
|
2130
|
+
const { authorization } = unsignedPaymentHeader.payload;
|
|
2131
|
+
const { signature } = await signAuthorization(client, authorization, paymentRequirements);
|
|
2132
|
+
return {
|
|
2133
|
+
...unsignedPaymentHeader,
|
|
2134
|
+
payload: {
|
|
2135
|
+
authorizationType: "eip3009",
|
|
2136
|
+
signature,
|
|
2137
|
+
authorization
|
|
2138
|
+
}
|
|
2139
|
+
};
|
|
2095
2140
|
}
|
|
2096
|
-
async function
|
|
2097
|
-
const
|
|
2098
|
-
|
|
2099
|
-
|
|
2141
|
+
async function createPayment(client, x402Version2, paymentRequirements) {
|
|
2142
|
+
const from = isSignerWallet(client) ? client.account.address : client.address;
|
|
2143
|
+
const unsignedPaymentHeader = preparePaymentHeader(from, x402Version2, paymentRequirements);
|
|
2144
|
+
return signPaymentHeader(client, paymentRequirements, unsignedPaymentHeader);
|
|
2145
|
+
}
|
|
2146
|
+
async function createPaymentHeader(client, x402Version2, paymentRequirements) {
|
|
2147
|
+
const payment = await createPayment(client, x402Version2, paymentRequirements);
|
|
2148
|
+
return encodePayment(payment);
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
// src/schemes/exact/svm/client.ts
|
|
2152
|
+
var import_kit4 = require("@solana/kit");
|
|
2153
|
+
var import_token_20222 = require("@solana-program/token-2022");
|
|
2154
|
+
var import_token2 = require("@solana-program/token");
|
|
2155
|
+
var import_compute_budget = require("@solana-program/compute-budget");
|
|
2156
|
+
async function createPaymentHeader2(client, x402Version2, paymentRequirements, config2) {
|
|
2157
|
+
const paymentPayload = await createAndSignPayment(
|
|
2158
|
+
client,
|
|
2159
|
+
x402Version2,
|
|
2100
2160
|
paymentRequirements,
|
|
2101
|
-
|
|
2102
|
-
txHasCreateDestATAInstruction
|
|
2103
|
-
},
|
|
2104
|
-
rpc
|
|
2161
|
+
config2
|
|
2105
2162
|
);
|
|
2163
|
+
return encodePayment(paymentPayload);
|
|
2106
2164
|
}
|
|
2107
|
-
async function
|
|
2108
|
-
const
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
if (missingAccount.address === parsedInstruction.accounts.source.address) {
|
|
2122
|
-
throw new Error(`invalid_exact_svm_payload_transaction_sender_ata_not_found`);
|
|
2123
|
-
}
|
|
2124
|
-
if (missingAccount.address === payToATA[0] && !txHasCreateDestATAInstruction) {
|
|
2125
|
-
throw new Error(`invalid_exact_svm_payload_transaction_receiver_ata_not_found`);
|
|
2165
|
+
async function createAndSignPayment(client, x402Version2, paymentRequirements, config2) {
|
|
2166
|
+
const transactionMessage = await createTransferTransactionMessage(
|
|
2167
|
+
client,
|
|
2168
|
+
paymentRequirements,
|
|
2169
|
+
config2
|
|
2170
|
+
);
|
|
2171
|
+
const signedTransaction = await (0, import_kit4.partiallySignTransactionMessageWithSigners)(transactionMessage);
|
|
2172
|
+
const base64EncodedWireTransaction = (0, import_kit4.getBase64EncodedWireTransaction)(signedTransaction);
|
|
2173
|
+
return {
|
|
2174
|
+
scheme: paymentRequirements.scheme,
|
|
2175
|
+
network: paymentRequirements.network,
|
|
2176
|
+
x402Version: x402Version2,
|
|
2177
|
+
payload: {
|
|
2178
|
+
transaction: base64EncodedWireTransaction
|
|
2126
2179
|
}
|
|
2127
|
-
}
|
|
2128
|
-
const instructionAmount = parsedInstruction.data.amount;
|
|
2129
|
-
const paymentRequirementsAmount = BigInt(paymentRequirements.maxAmountRequired);
|
|
2130
|
-
if (instructionAmount !== paymentRequirementsAmount) {
|
|
2131
|
-
throw new Error(`invalid_exact_svm_payload_transaction_amount_mismatch`);
|
|
2132
|
-
}
|
|
2180
|
+
};
|
|
2133
2181
|
}
|
|
2134
|
-
function
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
tokenInstruction = (0, import_token_20223.parseTransferCheckedInstruction)({
|
|
2162
|
-
...instruction,
|
|
2163
|
-
data: new Uint8Array(instruction.data)
|
|
2164
|
-
});
|
|
2165
|
-
} else {
|
|
2166
|
-
throw new Error(`invalid_exact_svm_payload_transaction_not_a_transfer_instruction`);
|
|
2167
|
-
}
|
|
2168
|
-
return tokenInstruction;
|
|
2182
|
+
async function createTransferTransactionMessage(client, paymentRequirements, config2) {
|
|
2183
|
+
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2184
|
+
const transferInstructions = await createAtaAndTransferInstructions(
|
|
2185
|
+
client,
|
|
2186
|
+
paymentRequirements,
|
|
2187
|
+
config2
|
|
2188
|
+
);
|
|
2189
|
+
const feePayer = paymentRequirements.extra?.feePayer;
|
|
2190
|
+
const txToSimulate = (0, import_kit4.pipe)(
|
|
2191
|
+
(0, import_kit4.createTransactionMessage)({ version: 0 }),
|
|
2192
|
+
(tx2) => (0, import_compute_budget.setTransactionMessageComputeUnitPrice)(1, tx2),
|
|
2193
|
+
// 1 microlamport priority fee
|
|
2194
|
+
(tx2) => (0, import_kit4.setTransactionMessageFeePayer)(feePayer, tx2),
|
|
2195
|
+
(tx2) => (0, import_kit4.appendTransactionMessageInstructions)(transferInstructions, tx2)
|
|
2196
|
+
);
|
|
2197
|
+
const estimateComputeUnitLimit = (0, import_compute_budget.estimateComputeUnitLimitFactory)({ rpc });
|
|
2198
|
+
const estimatedUnits = await estimateComputeUnitLimit(txToSimulate);
|
|
2199
|
+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
|
|
2200
|
+
const tx = (0, import_kit4.pipe)(
|
|
2201
|
+
txToSimulate,
|
|
2202
|
+
(tx2) => (0, import_kit4.prependTransactionMessageInstruction)(
|
|
2203
|
+
(0, import_compute_budget.getSetComputeUnitLimitInstruction)({ units: estimatedUnits }),
|
|
2204
|
+
tx2
|
|
2205
|
+
),
|
|
2206
|
+
(tx2) => (0, import_kit4.setTransactionMessageLifetimeUsingBlockhash)(latestBlockhash, tx2)
|
|
2207
|
+
);
|
|
2208
|
+
return tx;
|
|
2169
2209
|
}
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
async function settle(signer, payload, paymentRequirements, config2) {
|
|
2173
|
-
const verifyResponse = await verify(signer, payload, paymentRequirements, config2);
|
|
2174
|
-
if (!verifyResponse.isValid) {
|
|
2175
|
-
return {
|
|
2176
|
-
success: false,
|
|
2177
|
-
errorReason: verifyResponse.invalidReason,
|
|
2178
|
-
network: payload.network,
|
|
2179
|
-
transaction: ""
|
|
2180
|
-
};
|
|
2181
|
-
}
|
|
2182
|
-
const svmPayload = payload.payload;
|
|
2183
|
-
const decodedTransaction = decodeTransactionFromPayload(svmPayload);
|
|
2184
|
-
const signedTransaction = await (0, import_kit6.signTransaction)([signer.keyPair], decodedTransaction);
|
|
2185
|
-
const payer = getTokenPayerFromTransaction(decodedTransaction);
|
|
2210
|
+
async function createAtaAndTransferInstructions(client, paymentRequirements, config2) {
|
|
2211
|
+
const { asset } = paymentRequirements;
|
|
2186
2212
|
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2187
|
-
const
|
|
2188
|
-
|
|
2189
|
-
|
|
2213
|
+
const tokenMint = await (0, import_token_20222.fetchMint)(rpc, asset);
|
|
2214
|
+
const tokenProgramAddress = tokenMint.programAddress;
|
|
2215
|
+
if (tokenProgramAddress.toString() !== import_token2.TOKEN_PROGRAM_ADDRESS.toString() && tokenProgramAddress.toString() !== import_token_20222.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
|
|
2216
|
+
throw new Error("Asset was not created by a known token program");
|
|
2217
|
+
}
|
|
2218
|
+
const instructions = [];
|
|
2219
|
+
const createAtaIx = await createAtaInstructionOrUndefined(
|
|
2220
|
+
paymentRequirements,
|
|
2221
|
+
tokenProgramAddress,
|
|
2222
|
+
config2
|
|
2190
2223
|
);
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
signedTransaction,
|
|
2194
|
-
rpc,
|
|
2195
|
-
rpcSubscriptions
|
|
2196
|
-
);
|
|
2197
|
-
return {
|
|
2198
|
-
success,
|
|
2199
|
-
errorReason,
|
|
2200
|
-
payer,
|
|
2201
|
-
transaction: signature,
|
|
2202
|
-
network: payload.network
|
|
2203
|
-
};
|
|
2204
|
-
} catch (error) {
|
|
2205
|
-
console.error("Unexpected error during transaction settlement:", error);
|
|
2206
|
-
return {
|
|
2207
|
-
success: false,
|
|
2208
|
-
errorReason: "unexpected_settle_error",
|
|
2209
|
-
network: payload.network,
|
|
2210
|
-
transaction: (0, import_kit6.getSignatureFromTransaction)(signedTransaction),
|
|
2211
|
-
payer
|
|
2212
|
-
};
|
|
2224
|
+
if (createAtaIx) {
|
|
2225
|
+
instructions.push(createAtaIx);
|
|
2213
2226
|
}
|
|
2227
|
+
const transferIx = await createTransferInstruction(
|
|
2228
|
+
client,
|
|
2229
|
+
paymentRequirements,
|
|
2230
|
+
tokenMint.data.decimals,
|
|
2231
|
+
tokenProgramAddress
|
|
2232
|
+
);
|
|
2233
|
+
instructions.push(transferIx);
|
|
2234
|
+
return instructions;
|
|
2214
2235
|
}
|
|
2215
|
-
async function
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
}
|
|
2222
|
-
async function confirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions) {
|
|
2223
|
-
const signature = (0, import_kit6.getSignatureFromTransaction)(signedTransaction);
|
|
2224
|
-
const abortController = new AbortController();
|
|
2225
|
-
const timeout = setTimeout(() => {
|
|
2226
|
-
abortController.abort("Transaction confirmation timed out after 60 seconds");
|
|
2227
|
-
}, 6e4);
|
|
2228
|
-
try {
|
|
2229
|
-
const compiledTransactionMessage = (0, import_kit6.getCompiledTransactionMessageDecoder)().decode(
|
|
2230
|
-
signedTransaction.messageBytes
|
|
2231
|
-
);
|
|
2232
|
-
const decompiledTransactionMessage = await (0, import_kit6.decompileTransactionMessageFetchingLookupTables)(
|
|
2233
|
-
compiledTransactionMessage,
|
|
2234
|
-
rpc
|
|
2236
|
+
async function createAtaInstructionOrUndefined(paymentRequirements, tokenProgramAddress, config2) {
|
|
2237
|
+
const { asset, payTo, extra } = paymentRequirements;
|
|
2238
|
+
const feePayer = extra?.feePayer;
|
|
2239
|
+
if (!feePayer) {
|
|
2240
|
+
throw new Error(
|
|
2241
|
+
"feePayer is required in paymentRequirements.extra in order to set the facilitator as the fee payer for the create associated token account instruction"
|
|
2235
2242
|
);
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
abortSignal: abortController.signal,
|
|
2252
|
-
commitment,
|
|
2253
|
-
getBlockHeightExceedencePromise,
|
|
2254
|
-
getRecentSignatureConfirmationPromise
|
|
2255
|
-
};
|
|
2256
|
-
await (0, import_transaction_confirmation.waitForRecentTransactionConfirmation)({
|
|
2257
|
-
...config2,
|
|
2258
|
-
transaction: signedTransactionWithBlockhashLifetime
|
|
2243
|
+
}
|
|
2244
|
+
const [destinationATAAddress] = await (0, import_token_20222.findAssociatedTokenPda)({
|
|
2245
|
+
mint: asset,
|
|
2246
|
+
owner: payTo,
|
|
2247
|
+
tokenProgram: tokenProgramAddress
|
|
2248
|
+
});
|
|
2249
|
+
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2250
|
+
const maybeAccount = await (0, import_kit4.fetchEncodedAccount)(rpc, destinationATAAddress);
|
|
2251
|
+
if (!maybeAccount.exists) {
|
|
2252
|
+
return (0, import_token_20222.getCreateAssociatedTokenInstruction)({
|
|
2253
|
+
payer: paymentRequirements.extra?.feePayer,
|
|
2254
|
+
ata: destinationATAAddress,
|
|
2255
|
+
owner: payTo,
|
|
2256
|
+
mint: asset,
|
|
2257
|
+
tokenProgram: tokenProgramAddress
|
|
2259
2258
|
});
|
|
2260
|
-
return {
|
|
2261
|
-
success: true,
|
|
2262
|
-
signature
|
|
2263
|
-
};
|
|
2264
|
-
} catch (error) {
|
|
2265
|
-
console.error(error);
|
|
2266
|
-
if ((0, import_kit6.isSolanaError)(error, import_kit6.SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED)) {
|
|
2267
|
-
return {
|
|
2268
|
-
success: false,
|
|
2269
|
-
errorReason: "settle_exact_svm_block_height_exceeded",
|
|
2270
|
-
signature
|
|
2271
|
-
};
|
|
2272
|
-
} else if (error instanceof DOMException && error.name === "AbortError") {
|
|
2273
|
-
return {
|
|
2274
|
-
success: false,
|
|
2275
|
-
errorReason: "settle_exact_svm_transaction_confirmation_timed_out",
|
|
2276
|
-
signature
|
|
2277
|
-
};
|
|
2278
|
-
} else {
|
|
2279
|
-
throw error;
|
|
2280
|
-
}
|
|
2281
|
-
} finally {
|
|
2282
|
-
clearTimeout(timeout);
|
|
2283
2259
|
}
|
|
2260
|
+
return void 0;
|
|
2284
2261
|
}
|
|
2285
|
-
async function
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
},
|
|
2318
|
-
{
|
|
2319
|
-
internalType: "uint256",
|
|
2320
|
-
name: "deadline",
|
|
2321
|
-
type: "uint256"
|
|
2322
|
-
},
|
|
2323
|
-
{
|
|
2324
|
-
internalType: "uint8",
|
|
2325
|
-
name: "v",
|
|
2326
|
-
type: "uint8"
|
|
2327
|
-
},
|
|
2328
|
-
{
|
|
2329
|
-
internalType: "bytes32",
|
|
2330
|
-
name: "r",
|
|
2331
|
-
type: "bytes32"
|
|
2332
|
-
},
|
|
2333
|
-
{
|
|
2334
|
-
internalType: "bytes32",
|
|
2335
|
-
name: "s",
|
|
2336
|
-
type: "bytes32"
|
|
2337
|
-
}
|
|
2338
|
-
],
|
|
2339
|
-
name: "settleWithPermit",
|
|
2340
|
-
outputs: [],
|
|
2341
|
-
stateMutability: "nonpayable",
|
|
2342
|
-
type: "function"
|
|
2343
|
-
},
|
|
2344
|
-
// settleWithERC3009 - 使用 EIP-3009 TransferWithAuthorization 进行结算
|
|
2345
|
-
{
|
|
2346
|
-
inputs: [
|
|
2347
|
-
{
|
|
2348
|
-
internalType: "address",
|
|
2349
|
-
name: "token",
|
|
2350
|
-
type: "address"
|
|
2351
|
-
},
|
|
2352
|
-
{
|
|
2353
|
-
internalType: "address",
|
|
2354
|
-
name: "payer",
|
|
2355
|
-
type: "address"
|
|
2356
|
-
},
|
|
2357
|
-
{
|
|
2358
|
-
internalType: "address",
|
|
2359
|
-
name: "seller",
|
|
2360
|
-
type: "address"
|
|
2361
|
-
},
|
|
2362
|
-
{
|
|
2363
|
-
internalType: "uint256",
|
|
2364
|
-
name: "amount",
|
|
2365
|
-
type: "uint256"
|
|
2366
|
-
},
|
|
2367
|
-
{
|
|
2368
|
-
internalType: "uint256",
|
|
2369
|
-
name: "validAfter",
|
|
2370
|
-
type: "uint256"
|
|
2371
|
-
},
|
|
2372
|
-
{
|
|
2373
|
-
internalType: "uint256",
|
|
2374
|
-
name: "validBefore",
|
|
2375
|
-
type: "uint256"
|
|
2376
|
-
},
|
|
2377
|
-
{
|
|
2378
|
-
internalType: "bytes32",
|
|
2379
|
-
name: "nonce",
|
|
2380
|
-
type: "bytes32"
|
|
2381
|
-
},
|
|
2382
|
-
{
|
|
2383
|
-
internalType: "uint8",
|
|
2384
|
-
name: "v",
|
|
2385
|
-
type: "uint8"
|
|
2386
|
-
},
|
|
2387
|
-
{
|
|
2388
|
-
internalType: "bytes32",
|
|
2389
|
-
name: "r",
|
|
2390
|
-
type: "bytes32"
|
|
2391
|
-
},
|
|
2392
|
-
{
|
|
2393
|
-
internalType: "bytes32",
|
|
2394
|
-
name: "s",
|
|
2395
|
-
type: "bytes32"
|
|
2396
|
-
}
|
|
2397
|
-
],
|
|
2398
|
-
name: "settleWithERC3009",
|
|
2399
|
-
outputs: [],
|
|
2400
|
-
stateMutability: "nonpayable",
|
|
2401
|
-
type: "function"
|
|
2402
|
-
},
|
|
2403
|
-
// settleWithERC3009Direct - 直接结算(无手续费)
|
|
2404
|
-
{
|
|
2405
|
-
inputs: [
|
|
2406
|
-
{
|
|
2407
|
-
internalType: "address",
|
|
2408
|
-
name: "token",
|
|
2409
|
-
type: "address"
|
|
2410
|
-
},
|
|
2411
|
-
{
|
|
2412
|
-
internalType: "address",
|
|
2413
|
-
name: "payer",
|
|
2414
|
-
type: "address"
|
|
2415
|
-
},
|
|
2416
|
-
{
|
|
2417
|
-
internalType: "address",
|
|
2418
|
-
name: "seller",
|
|
2419
|
-
type: "address"
|
|
2420
|
-
},
|
|
2421
|
-
{
|
|
2422
|
-
internalType: "uint256",
|
|
2423
|
-
name: "amount",
|
|
2424
|
-
type: "uint256"
|
|
2425
|
-
},
|
|
2426
|
-
{
|
|
2427
|
-
internalType: "uint256",
|
|
2428
|
-
name: "validAfter",
|
|
2429
|
-
type: "uint256"
|
|
2430
|
-
},
|
|
2431
|
-
{
|
|
2432
|
-
internalType: "uint256",
|
|
2433
|
-
name: "validBefore",
|
|
2434
|
-
type: "uint256"
|
|
2435
|
-
},
|
|
2436
|
-
{
|
|
2437
|
-
internalType: "bytes32",
|
|
2438
|
-
name: "nonce",
|
|
2439
|
-
type: "bytes32"
|
|
2440
|
-
},
|
|
2441
|
-
{
|
|
2442
|
-
internalType: "uint8",
|
|
2443
|
-
name: "v",
|
|
2444
|
-
type: "uint8"
|
|
2445
|
-
},
|
|
2446
|
-
{
|
|
2447
|
-
internalType: "bytes32",
|
|
2448
|
-
name: "r",
|
|
2449
|
-
type: "bytes32"
|
|
2450
|
-
},
|
|
2451
|
-
{
|
|
2452
|
-
internalType: "bytes32",
|
|
2453
|
-
name: "s",
|
|
2454
|
-
type: "bytes32"
|
|
2455
|
-
}
|
|
2456
|
-
],
|
|
2457
|
-
name: "settleWithERC3009Direct",
|
|
2458
|
-
outputs: [],
|
|
2459
|
-
stateMutability: "nonpayable",
|
|
2460
|
-
type: "function"
|
|
2461
|
-
},
|
|
2462
|
-
// Events
|
|
2463
|
-
{
|
|
2464
|
-
anonymous: false,
|
|
2465
|
-
inputs: [
|
|
2466
|
-
{
|
|
2467
|
-
indexed: true,
|
|
2468
|
-
internalType: "address",
|
|
2469
|
-
name: "token",
|
|
2470
|
-
type: "address"
|
|
2471
|
-
},
|
|
2472
|
-
{
|
|
2473
|
-
indexed: true,
|
|
2474
|
-
internalType: "address",
|
|
2475
|
-
name: "payer",
|
|
2476
|
-
type: "address"
|
|
2477
|
-
},
|
|
2478
|
-
{
|
|
2479
|
-
indexed: true,
|
|
2480
|
-
internalType: "address",
|
|
2481
|
-
name: "seller",
|
|
2482
|
-
type: "address"
|
|
2483
|
-
},
|
|
2484
|
-
{
|
|
2485
|
-
indexed: false,
|
|
2486
|
-
internalType: "uint256",
|
|
2487
|
-
name: "amount",
|
|
2488
|
-
type: "uint256"
|
|
2489
|
-
},
|
|
2490
|
-
{
|
|
2491
|
-
indexed: false,
|
|
2492
|
-
internalType: "uint256",
|
|
2493
|
-
name: "sellerAmount",
|
|
2494
|
-
type: "uint256"
|
|
2495
|
-
},
|
|
2496
|
-
{
|
|
2497
|
-
indexed: false,
|
|
2498
|
-
internalType: "uint256",
|
|
2499
|
-
name: "feeAmount",
|
|
2500
|
-
type: "uint256"
|
|
2501
|
-
},
|
|
2502
|
-
{
|
|
2503
|
-
indexed: false,
|
|
2504
|
-
internalType: "string",
|
|
2505
|
-
name: "path",
|
|
2506
|
-
type: "string"
|
|
2262
|
+
async function createTransferInstruction(client, paymentRequirements, decimals, tokenProgramAddress) {
|
|
2263
|
+
const { asset, maxAmountRequired: amount, payTo } = paymentRequirements;
|
|
2264
|
+
const [sourceATA] = await (0, import_token_20222.findAssociatedTokenPda)({
|
|
2265
|
+
mint: asset,
|
|
2266
|
+
owner: client.address,
|
|
2267
|
+
tokenProgram: tokenProgramAddress
|
|
2268
|
+
});
|
|
2269
|
+
const [destinationATA] = await (0, import_token_20222.findAssociatedTokenPda)({
|
|
2270
|
+
mint: asset,
|
|
2271
|
+
owner: payTo,
|
|
2272
|
+
tokenProgram: tokenProgramAddress
|
|
2273
|
+
});
|
|
2274
|
+
return (0, import_token_20222.getTransferCheckedInstruction)(
|
|
2275
|
+
{
|
|
2276
|
+
source: sourceATA,
|
|
2277
|
+
mint: asset,
|
|
2278
|
+
destination: destinationATA,
|
|
2279
|
+
authority: client,
|
|
2280
|
+
amount: BigInt(amount),
|
|
2281
|
+
decimals
|
|
2282
|
+
},
|
|
2283
|
+
{ programAddress: tokenProgramAddress }
|
|
2284
|
+
);
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
// src/client/createPaymentHeader.ts
|
|
2288
|
+
async function createPaymentHeader3(client, x402Version2, paymentRequirements, config2) {
|
|
2289
|
+
if (paymentRequirements.scheme === "exact") {
|
|
2290
|
+
if (SupportedEVMNetworks.includes(paymentRequirements.network)) {
|
|
2291
|
+
const evmClient = isMultiNetworkSigner(client) ? client.evm : client;
|
|
2292
|
+
if (!isEvmSignerWallet(evmClient)) {
|
|
2293
|
+
throw new Error("Invalid evm wallet client provided");
|
|
2507
2294
|
}
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2295
|
+
return await createPaymentHeader(
|
|
2296
|
+
evmClient,
|
|
2297
|
+
x402Version2,
|
|
2298
|
+
paymentRequirements
|
|
2299
|
+
);
|
|
2300
|
+
}
|
|
2301
|
+
if (SupportedSVMNetworks.includes(paymentRequirements.network)) {
|
|
2302
|
+
const svmClient = isMultiNetworkSigner(client) ? client.svm : client;
|
|
2303
|
+
if (!isSvmSignerWallet(svmClient)) {
|
|
2304
|
+
throw new Error("Invalid svm wallet client provided");
|
|
2305
|
+
}
|
|
2306
|
+
return await createPaymentHeader2(
|
|
2307
|
+
svmClient,
|
|
2308
|
+
x402Version2,
|
|
2309
|
+
paymentRequirements,
|
|
2310
|
+
config2
|
|
2311
|
+
);
|
|
2312
|
+
}
|
|
2313
|
+
throw new Error("Unsupported network");
|
|
2314
|
+
}
|
|
2315
|
+
throw new Error("Unsupported scheme");
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
// src/client/preparePaymentHeader.ts
|
|
2319
|
+
function preparePaymentHeader2(from, x402Version2, paymentRequirements) {
|
|
2320
|
+
if (paymentRequirements.scheme === "exact" && SupportedEVMNetworks.includes(paymentRequirements.network)) {
|
|
2321
|
+
return preparePaymentHeader(from, x402Version2, paymentRequirements);
|
|
2322
|
+
}
|
|
2323
|
+
throw new Error("Unsupported scheme");
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
// src/client/selectPaymentRequirements.ts
|
|
2327
|
+
function selectPaymentRequirements(paymentRequirements, network, scheme) {
|
|
2328
|
+
paymentRequirements.sort((a, b) => {
|
|
2329
|
+
if (a.network === "base" && b.network !== "base") {
|
|
2330
|
+
return -1;
|
|
2331
|
+
}
|
|
2332
|
+
if (a.network !== "base" && b.network === "base") {
|
|
2333
|
+
return 1;
|
|
2334
|
+
}
|
|
2335
|
+
return 0;
|
|
2336
|
+
});
|
|
2337
|
+
const broadlyAcceptedPaymentRequirements = paymentRequirements.filter((requirement) => {
|
|
2338
|
+
const isExpectedScheme = !scheme || requirement.scheme === scheme;
|
|
2339
|
+
const isExpectedChain = !network || (Array.isArray(network) ? network.includes(requirement.network) : network == requirement.network);
|
|
2340
|
+
return isExpectedScheme && isExpectedChain;
|
|
2341
|
+
});
|
|
2342
|
+
const usdcRequirements = broadlyAcceptedPaymentRequirements.filter((requirement) => {
|
|
2343
|
+
return requirement.asset === getUsdcChainConfigForChain(getNetworkId(requirement.network))?.usdcAddress;
|
|
2344
|
+
});
|
|
2345
|
+
if (usdcRequirements.length > 0) {
|
|
2346
|
+
return usdcRequirements[0];
|
|
2347
|
+
}
|
|
2348
|
+
if (broadlyAcceptedPaymentRequirements.length > 0) {
|
|
2349
|
+
return broadlyAcceptedPaymentRequirements[0];
|
|
2350
|
+
}
|
|
2351
|
+
return paymentRequirements[0];
|
|
2352
|
+
}
|
|
2353
|
+
|
|
2354
|
+
// src/client/signPaymentHeader.ts
|
|
2355
|
+
async function signPaymentHeader2(client, paymentRequirements, unsignedPaymentHeader) {
|
|
2356
|
+
if (paymentRequirements.scheme === "exact" && SupportedEVMNetworks.includes(paymentRequirements.network)) {
|
|
2357
|
+
const evmClient = isMultiNetworkSigner(client) ? client.evm : client;
|
|
2358
|
+
if (!isEvmSignerWallet(evmClient)) {
|
|
2359
|
+
throw new Error("Invalid evm wallet client provided");
|
|
2360
|
+
}
|
|
2361
|
+
const signedPaymentHeader = await signPaymentHeader(evmClient, paymentRequirements, unsignedPaymentHeader);
|
|
2362
|
+
return encodePayment(signedPaymentHeader);
|
|
2363
|
+
}
|
|
2364
|
+
throw new Error("Unsupported scheme");
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
// src/schemes/exact/evm/eip3009/facilitator.ts
|
|
2368
|
+
var import_viem3 = require("viem");
|
|
2369
|
+
|
|
2370
|
+
// src/schemes/exact/svm/facilitator/settle.ts
|
|
2371
|
+
var import_kit6 = require("@solana/kit");
|
|
2372
|
+
var import_transaction_confirmation = require("@solana/transaction-confirmation");
|
|
2373
|
+
|
|
2374
|
+
// src/schemes/exact/svm/facilitator/verify.ts
|
|
2375
|
+
var import_kit5 = require("@solana/kit");
|
|
2376
|
+
var import_compute_budget2 = require("@solana-program/compute-budget");
|
|
2377
|
+
var import_token_20223 = require("@solana-program/token-2022");
|
|
2378
|
+
var import_token3 = require("@solana-program/token");
|
|
2379
|
+
async function verify(signer, payload, paymentRequirements, config2) {
|
|
2380
|
+
try {
|
|
2381
|
+
verifySchemesAndNetworks(payload, paymentRequirements);
|
|
2382
|
+
const svmPayload = payload.payload;
|
|
2383
|
+
const decodedTransaction = decodeTransactionFromPayload(svmPayload);
|
|
2384
|
+
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2385
|
+
await transactionIntrospection(svmPayload, paymentRequirements, config2);
|
|
2386
|
+
const simulateResult = await signAndSimulateTransaction(signer, decodedTransaction, rpc);
|
|
2387
|
+
if (simulateResult.value?.err) {
|
|
2388
|
+
throw new Error(`invalid_exact_svm_payload_transaction_simulation_failed`);
|
|
2389
|
+
}
|
|
2390
|
+
return {
|
|
2391
|
+
isValid: true,
|
|
2392
|
+
invalidReason: void 0,
|
|
2393
|
+
payer: getTokenPayerFromTransaction(decodedTransaction)
|
|
2394
|
+
};
|
|
2395
|
+
} catch (error) {
|
|
2396
|
+
if (error instanceof Error) {
|
|
2397
|
+
if (ErrorReasons.includes(error.message)) {
|
|
2398
|
+
return {
|
|
2399
|
+
isValid: false,
|
|
2400
|
+
invalidReason: error.message,
|
|
2401
|
+
payer: (() => {
|
|
2402
|
+
try {
|
|
2403
|
+
const tx = decodeTransactionFromPayload(payload.payload);
|
|
2404
|
+
return getTokenPayerFromTransaction(tx);
|
|
2405
|
+
} catch {
|
|
2406
|
+
return void 0;
|
|
2407
|
+
}
|
|
2408
|
+
})()
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
console.error(error);
|
|
2413
|
+
return {
|
|
2414
|
+
isValid: false,
|
|
2415
|
+
invalidReason: "unexpected_verify_error",
|
|
2416
|
+
payer: (() => {
|
|
2417
|
+
try {
|
|
2418
|
+
const tx = decodeTransactionFromPayload(payload.payload);
|
|
2419
|
+
return getTokenPayerFromTransaction(tx);
|
|
2420
|
+
} catch {
|
|
2421
|
+
return void 0;
|
|
2422
|
+
}
|
|
2423
|
+
})()
|
|
2424
|
+
};
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
function verifySchemesAndNetworks(payload, paymentRequirements) {
|
|
2428
|
+
if (payload.scheme !== SCHEME || paymentRequirements.scheme !== SCHEME) {
|
|
2429
|
+
throw new Error("unsupported_scheme");
|
|
2430
|
+
}
|
|
2431
|
+
if (payload.network !== paymentRequirements.network || !SupportedSVMNetworks.includes(paymentRequirements.network)) {
|
|
2432
|
+
throw new Error("invalid_network");
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
async function transactionIntrospection(svmPayload, paymentRequirements, config2) {
|
|
2436
|
+
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2437
|
+
const decodedTransaction = decodeTransactionFromPayload(svmPayload);
|
|
2438
|
+
const compiledTransactionMessage = (0, import_kit5.getCompiledTransactionMessageDecoder)().decode(
|
|
2439
|
+
decodedTransaction.messageBytes
|
|
2440
|
+
);
|
|
2441
|
+
const transactionMessage = (0, import_kit5.decompileTransactionMessage)(
|
|
2442
|
+
compiledTransactionMessage
|
|
2443
|
+
);
|
|
2444
|
+
await verifyTransactionInstructions(transactionMessage, paymentRequirements, rpc);
|
|
2445
|
+
}
|
|
2446
|
+
async function verifyTransactionInstructions(transactionMessage, paymentRequirements, rpc) {
|
|
2447
|
+
if (transactionMessage.instructions.length !== 3 && transactionMessage.instructions.length !== 4) {
|
|
2448
|
+
throw new Error(`invalid_exact_svm_payload_transaction_instructions_length`);
|
|
2449
|
+
}
|
|
2450
|
+
verifyComputeLimitInstruction(transactionMessage.instructions[0]);
|
|
2451
|
+
verifyComputePriceInstruction(transactionMessage.instructions[1]);
|
|
2452
|
+
if (transactionMessage.instructions.length === 3) {
|
|
2453
|
+
await verifyTransferInstruction(
|
|
2454
|
+
transactionMessage.instructions[2],
|
|
2455
|
+
paymentRequirements,
|
|
2527
2456
|
{
|
|
2528
|
-
|
|
2529
|
-
internalType: "address",
|
|
2530
|
-
name: "seller",
|
|
2531
|
-
type: "address"
|
|
2457
|
+
txHasCreateDestATAInstruction: false
|
|
2532
2458
|
},
|
|
2459
|
+
rpc
|
|
2460
|
+
);
|
|
2461
|
+
} else {
|
|
2462
|
+
verifyCreateATAInstruction(transactionMessage.instructions[2], paymentRequirements);
|
|
2463
|
+
await verifyTransferInstruction(
|
|
2464
|
+
transactionMessage.instructions[3],
|
|
2465
|
+
paymentRequirements,
|
|
2533
2466
|
{
|
|
2534
|
-
|
|
2535
|
-
internalType: "uint256",
|
|
2536
|
-
name: "amount",
|
|
2537
|
-
type: "uint256"
|
|
2467
|
+
txHasCreateDestATAInstruction: true
|
|
2538
2468
|
},
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
]
|
|
2546
|
-
|
|
2547
|
-
|
|
2469
|
+
rpc
|
|
2470
|
+
);
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2473
|
+
function verifyComputeLimitInstruction(instruction) {
|
|
2474
|
+
try {
|
|
2475
|
+
if (instruction.programAddress.toString() !== import_compute_budget2.COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || instruction.data?.[0] !== 2) {
|
|
2476
|
+
throw new Error(
|
|
2477
|
+
`invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction`
|
|
2478
|
+
);
|
|
2479
|
+
}
|
|
2480
|
+
(0, import_compute_budget2.parseSetComputeUnitLimitInstruction)(
|
|
2481
|
+
instruction
|
|
2482
|
+
);
|
|
2483
|
+
} catch (error) {
|
|
2484
|
+
console.error(error);
|
|
2485
|
+
throw new Error(`invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction`);
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
function verifyComputePriceInstruction(instruction) {
|
|
2489
|
+
if (instruction.programAddress.toString() !== import_compute_budget2.COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || instruction.data?.[0] !== 3) {
|
|
2490
|
+
throw new Error(`invalid_exact_svm_payload_transaction_instructions_compute_price_instruction`);
|
|
2491
|
+
}
|
|
2492
|
+
const parsedInstruction = (0, import_compute_budget2.parseSetComputeUnitPriceInstruction)(
|
|
2493
|
+
instruction
|
|
2494
|
+
);
|
|
2495
|
+
if (parsedInstruction.data.microLamports > 5 * 1e6) {
|
|
2496
|
+
throw new Error(
|
|
2497
|
+
`invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high`
|
|
2498
|
+
);
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
function verifyCreateATAInstruction(instruction, paymentRequirements) {
|
|
2502
|
+
let createATAInstruction;
|
|
2503
|
+
try {
|
|
2504
|
+
(0, import_kit5.assertIsInstructionWithAccounts)(instruction);
|
|
2505
|
+
(0, import_kit5.assertIsInstructionWithData)(instruction);
|
|
2506
|
+
createATAInstruction = (0, import_token_20223.parseCreateAssociatedTokenInstruction)({
|
|
2507
|
+
...instruction,
|
|
2508
|
+
data: new Uint8Array(instruction.data)
|
|
2509
|
+
});
|
|
2510
|
+
} catch (error) {
|
|
2511
|
+
console.error(error);
|
|
2512
|
+
throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction`);
|
|
2548
2513
|
}
|
|
2549
|
-
|
|
2514
|
+
if (createATAInstruction.accounts.owner.address !== paymentRequirements.payTo) {
|
|
2515
|
+
throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_payee`);
|
|
2516
|
+
}
|
|
2517
|
+
if (createATAInstruction.accounts.mint.address !== paymentRequirements.asset) {
|
|
2518
|
+
throw new Error(`invalid_exact_svm_payload_transaction_create_ata_instruction_incorrect_asset`);
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2521
|
+
async function verifyTransferInstruction(instruction, paymentRequirements, { txHasCreateDestATAInstruction }, rpc) {
|
|
2522
|
+
const tokenInstruction = getValidatedTransferCheckedInstruction(instruction);
|
|
2523
|
+
await verifyTransferCheckedInstruction(
|
|
2524
|
+
tokenInstruction,
|
|
2525
|
+
paymentRequirements,
|
|
2526
|
+
{
|
|
2527
|
+
txHasCreateDestATAInstruction
|
|
2528
|
+
},
|
|
2529
|
+
rpc
|
|
2530
|
+
);
|
|
2531
|
+
}
|
|
2532
|
+
async function verifyTransferCheckedInstruction(parsedInstruction, paymentRequirements, { txHasCreateDestATAInstruction }, rpc) {
|
|
2533
|
+
const tokenProgramAddress = parsedInstruction.programAddress.toString() === import_token3.TOKEN_PROGRAM_ADDRESS.toString() ? import_token3.TOKEN_PROGRAM_ADDRESS : import_token_20223.TOKEN_2022_PROGRAM_ADDRESS;
|
|
2534
|
+
const payToATA = await (0, import_token_20223.findAssociatedTokenPda)({
|
|
2535
|
+
mint: paymentRequirements.asset,
|
|
2536
|
+
owner: paymentRequirements.payTo,
|
|
2537
|
+
tokenProgram: tokenProgramAddress
|
|
2538
|
+
});
|
|
2539
|
+
if (parsedInstruction.accounts.destination.address !== payToATA[0]) {
|
|
2540
|
+
throw new Error(`invalid_exact_svm_payload_transaction_transfer_to_incorrect_ata`);
|
|
2541
|
+
}
|
|
2542
|
+
const addresses = [parsedInstruction.accounts.source.address, payToATA[0]];
|
|
2543
|
+
const maybeAccounts = await (0, import_kit5.fetchEncodedAccounts)(rpc, addresses);
|
|
2544
|
+
const missingAccounts = maybeAccounts.filter((a) => !a.exists);
|
|
2545
|
+
for (const missingAccount of missingAccounts) {
|
|
2546
|
+
if (missingAccount.address === parsedInstruction.accounts.source.address) {
|
|
2547
|
+
throw new Error(`invalid_exact_svm_payload_transaction_sender_ata_not_found`);
|
|
2548
|
+
}
|
|
2549
|
+
if (missingAccount.address === payToATA[0] && !txHasCreateDestATAInstruction) {
|
|
2550
|
+
throw new Error(`invalid_exact_svm_payload_transaction_receiver_ata_not_found`);
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
const instructionAmount = parsedInstruction.data.amount;
|
|
2554
|
+
const paymentRequirementsAmount = BigInt(paymentRequirements.maxAmountRequired);
|
|
2555
|
+
if (instructionAmount !== paymentRequirementsAmount) {
|
|
2556
|
+
throw new Error(`invalid_exact_svm_payload_transaction_amount_mismatch`);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
function getValidatedTransferCheckedInstruction(instruction) {
|
|
2560
|
+
try {
|
|
2561
|
+
(0, import_kit5.assertIsInstructionWithData)(instruction);
|
|
2562
|
+
(0, import_kit5.assertIsInstructionWithAccounts)(instruction);
|
|
2563
|
+
} catch (error) {
|
|
2564
|
+
console.error(error);
|
|
2565
|
+
throw new Error(`invalid_exact_svm_payload_transaction_instructions`);
|
|
2566
|
+
}
|
|
2567
|
+
let tokenInstruction;
|
|
2568
|
+
if (instruction.programAddress.toString() === import_token3.TOKEN_PROGRAM_ADDRESS.toString()) {
|
|
2569
|
+
const identifiedInstruction = (0, import_token3.identifyTokenInstruction)(instruction);
|
|
2570
|
+
if (identifiedInstruction !== import_token3.TokenInstruction.TransferChecked) {
|
|
2571
|
+
throw new Error(
|
|
2572
|
+
`invalid_exact_svm_payload_transaction_instruction_not_spl_token_transfer_checked`
|
|
2573
|
+
);
|
|
2574
|
+
}
|
|
2575
|
+
tokenInstruction = (0, import_token3.parseTransferCheckedInstruction)({
|
|
2576
|
+
...instruction,
|
|
2577
|
+
data: new Uint8Array(instruction.data)
|
|
2578
|
+
});
|
|
2579
|
+
} else if (instruction.programAddress.toString() === import_token_20223.TOKEN_2022_PROGRAM_ADDRESS.toString()) {
|
|
2580
|
+
const identifiedInstruction = (0, import_token_20223.identifyToken2022Instruction)(instruction);
|
|
2581
|
+
if (identifiedInstruction !== import_token_20223.Token2022Instruction.TransferChecked) {
|
|
2582
|
+
throw new Error(
|
|
2583
|
+
`invalid_exact_svm_payload_transaction_instruction_not_token_2022_transfer_checked`
|
|
2584
|
+
);
|
|
2585
|
+
}
|
|
2586
|
+
tokenInstruction = (0, import_token_20223.parseTransferCheckedInstruction)({
|
|
2587
|
+
...instruction,
|
|
2588
|
+
data: new Uint8Array(instruction.data)
|
|
2589
|
+
});
|
|
2590
|
+
} else {
|
|
2591
|
+
throw new Error(`invalid_exact_svm_payload_transaction_not_a_transfer_instruction`);
|
|
2592
|
+
}
|
|
2593
|
+
return tokenInstruction;
|
|
2594
|
+
}
|
|
2595
|
+
|
|
2596
|
+
// src/schemes/exact/svm/facilitator/settle.ts
|
|
2597
|
+
async function settle(signer, payload, paymentRequirements, config2) {
|
|
2598
|
+
const verifyResponse = await verify(signer, payload, paymentRequirements, config2);
|
|
2599
|
+
if (!verifyResponse.isValid) {
|
|
2600
|
+
return {
|
|
2601
|
+
success: false,
|
|
2602
|
+
errorReason: verifyResponse.invalidReason,
|
|
2603
|
+
network: payload.network,
|
|
2604
|
+
transaction: ""
|
|
2605
|
+
};
|
|
2606
|
+
}
|
|
2607
|
+
const svmPayload = payload.payload;
|
|
2608
|
+
const decodedTransaction = decodeTransactionFromPayload(svmPayload);
|
|
2609
|
+
const signedTransaction = await (0, import_kit6.signTransaction)([signer.keyPair], decodedTransaction);
|
|
2610
|
+
const payer = getTokenPayerFromTransaction(decodedTransaction);
|
|
2611
|
+
const rpc = getRpcClient(paymentRequirements.network, config2?.svmConfig?.rpcUrl);
|
|
2612
|
+
const rpcSubscriptions = getRpcSubscriptions(
|
|
2613
|
+
paymentRequirements.network,
|
|
2614
|
+
config2?.svmConfig?.rpcUrl
|
|
2615
|
+
);
|
|
2616
|
+
try {
|
|
2617
|
+
const { success, errorReason, signature } = await sendAndConfirmSignedTransaction(
|
|
2618
|
+
signedTransaction,
|
|
2619
|
+
rpc,
|
|
2620
|
+
rpcSubscriptions
|
|
2621
|
+
);
|
|
2622
|
+
return {
|
|
2623
|
+
success,
|
|
2624
|
+
errorReason,
|
|
2625
|
+
payer,
|
|
2626
|
+
transaction: signature,
|
|
2627
|
+
network: payload.network
|
|
2628
|
+
};
|
|
2629
|
+
} catch (error) {
|
|
2630
|
+
console.error("Unexpected error during transaction settlement:", error);
|
|
2631
|
+
return {
|
|
2632
|
+
success: false,
|
|
2633
|
+
errorReason: "unexpected_settle_error",
|
|
2634
|
+
network: payload.network,
|
|
2635
|
+
transaction: (0, import_kit6.getSignatureFromTransaction)(signedTransaction),
|
|
2636
|
+
payer
|
|
2637
|
+
};
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
async function sendSignedTransaction(signedTransaction, rpc, sendTxConfig = {
|
|
2641
|
+
skipPreflight: true,
|
|
2642
|
+
encoding: "base64"
|
|
2643
|
+
}) {
|
|
2644
|
+
const base64EncodedTransaction = (0, import_kit6.getBase64EncodedWireTransaction)(signedTransaction);
|
|
2645
|
+
return await rpc.sendTransaction(base64EncodedTransaction, sendTxConfig).send();
|
|
2646
|
+
}
|
|
2647
|
+
async function confirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions) {
|
|
2648
|
+
const signature = (0, import_kit6.getSignatureFromTransaction)(signedTransaction);
|
|
2649
|
+
const abortController = new AbortController();
|
|
2650
|
+
const timeout = setTimeout(() => {
|
|
2651
|
+
abortController.abort("Transaction confirmation timed out after 60 seconds");
|
|
2652
|
+
}, 6e4);
|
|
2653
|
+
try {
|
|
2654
|
+
const compiledTransactionMessage = (0, import_kit6.getCompiledTransactionMessageDecoder)().decode(
|
|
2655
|
+
signedTransaction.messageBytes
|
|
2656
|
+
);
|
|
2657
|
+
const decompiledTransactionMessage = await (0, import_kit6.decompileTransactionMessageFetchingLookupTables)(
|
|
2658
|
+
compiledTransactionMessage,
|
|
2659
|
+
rpc
|
|
2660
|
+
);
|
|
2661
|
+
(0, import_kit6.assertIsTransactionMessageWithBlockhashLifetime)(decompiledTransactionMessage);
|
|
2662
|
+
const signedTransactionWithBlockhashLifetime = {
|
|
2663
|
+
...signedTransaction,
|
|
2664
|
+
lifetimeConstraint: decompiledTransactionMessage.lifetimeConstraint
|
|
2665
|
+
};
|
|
2666
|
+
const commitment = "confirmed";
|
|
2667
|
+
const getRecentSignatureConfirmationPromise = (0, import_transaction_confirmation.createRecentSignatureConfirmationPromiseFactory)({
|
|
2668
|
+
rpc,
|
|
2669
|
+
rpcSubscriptions
|
|
2670
|
+
});
|
|
2671
|
+
const getBlockHeightExceedencePromise = (0, import_transaction_confirmation.createBlockHeightExceedencePromiseFactory)({
|
|
2672
|
+
rpc,
|
|
2673
|
+
rpcSubscriptions
|
|
2674
|
+
});
|
|
2675
|
+
const config2 = {
|
|
2676
|
+
abortSignal: abortController.signal,
|
|
2677
|
+
commitment,
|
|
2678
|
+
getBlockHeightExceedencePromise,
|
|
2679
|
+
getRecentSignatureConfirmationPromise
|
|
2680
|
+
};
|
|
2681
|
+
await (0, import_transaction_confirmation.waitForRecentTransactionConfirmation)({
|
|
2682
|
+
...config2,
|
|
2683
|
+
transaction: signedTransactionWithBlockhashLifetime
|
|
2684
|
+
});
|
|
2685
|
+
return {
|
|
2686
|
+
success: true,
|
|
2687
|
+
signature
|
|
2688
|
+
};
|
|
2689
|
+
} catch (error) {
|
|
2690
|
+
console.error(error);
|
|
2691
|
+
if ((0, import_kit6.isSolanaError)(error, import_kit6.SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED)) {
|
|
2692
|
+
return {
|
|
2693
|
+
success: false,
|
|
2694
|
+
errorReason: "settle_exact_svm_block_height_exceeded",
|
|
2695
|
+
signature
|
|
2696
|
+
};
|
|
2697
|
+
} else if (error instanceof DOMException && error.name === "AbortError") {
|
|
2698
|
+
return {
|
|
2699
|
+
success: false,
|
|
2700
|
+
errorReason: "settle_exact_svm_transaction_confirmation_timed_out",
|
|
2701
|
+
signature
|
|
2702
|
+
};
|
|
2703
|
+
} else {
|
|
2704
|
+
throw error;
|
|
2705
|
+
}
|
|
2706
|
+
} finally {
|
|
2707
|
+
clearTimeout(timeout);
|
|
2708
|
+
}
|
|
2709
|
+
}
|
|
2710
|
+
async function sendAndConfirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions) {
|
|
2711
|
+
await sendSignedTransaction(signedTransaction, rpc);
|
|
2712
|
+
return await confirmSignedTransaction(signedTransaction, rpc, rpcSubscriptions);
|
|
2713
|
+
}
|
|
2714
|
+
|
|
2715
|
+
// src/schemes/exact/index.ts
|
|
2716
|
+
var SCHEME = "exact";
|
|
2550
2717
|
|
|
2551
2718
|
// src/schemes/exact/evm/eip3009/facilitator.ts
|
|
2552
2719
|
async function verify2(client, payload, paymentRequirements) {
|
|
@@ -2612,7 +2779,7 @@ async function verify2(client, payload, paymentRequirements) {
|
|
|
2612
2779
|
payer: exactEvmPayload.authorization.from
|
|
2613
2780
|
};
|
|
2614
2781
|
}
|
|
2615
|
-
if ((0, import_viem3.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem3.getAddress)(paymentRequirements.payTo)
|
|
2782
|
+
if ((0, import_viem3.getAddress)(exactEvmPayload.authorization.to) !== (0, import_viem3.getAddress)(paymentRequirements.payTo)) {
|
|
2616
2783
|
return {
|
|
2617
2784
|
isValid: false,
|
|
2618
2785
|
invalidReason: "invalid_exact_evm_payload_recipient_mismatch",
|
|
@@ -2685,57 +2852,36 @@ async function settle2(wallet, paymentPayload, paymentRequirements) {
|
|
|
2685
2852
|
};
|
|
2686
2853
|
}
|
|
2687
2854
|
const { signature } = (0, import_viem3.parseErc6492Signature)(payload.signature);
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
// s (bytes32)
|
|
2719
|
-
],
|
|
2720
|
-
chain: wallet.chain
|
|
2721
|
-
});
|
|
2722
|
-
} else {
|
|
2723
|
-
tx = await wallet.writeContract({
|
|
2724
|
-
address: paymentRequirements.asset,
|
|
2725
|
-
abi: usdcABI,
|
|
2726
|
-
functionName: "transferWithAuthorization",
|
|
2727
|
-
args: [
|
|
2728
|
-
payload.authorization.from,
|
|
2729
|
-
payload.authorization.to,
|
|
2730
|
-
BigInt(payload.authorization.value),
|
|
2731
|
-
BigInt(payload.authorization.validAfter),
|
|
2732
|
-
BigInt(payload.authorization.validBefore),
|
|
2733
|
-
payload.authorization.nonce,
|
|
2734
|
-
signature
|
|
2735
|
-
],
|
|
2736
|
-
chain: wallet.chain
|
|
2737
|
-
});
|
|
2738
|
-
}
|
|
2855
|
+
const sig = (0, import_viem3.hexToSignature)(signature);
|
|
2856
|
+
const v = Number(sig.v);
|
|
2857
|
+
const r = sig.r;
|
|
2858
|
+
const s = sig.s;
|
|
2859
|
+
const tx = await wallet.writeContract({
|
|
2860
|
+
address: paymentRequirements.payTo,
|
|
2861
|
+
abi: EIP7702SellerWalletMinimalAbi,
|
|
2862
|
+
functionName: "settleWithERC3009",
|
|
2863
|
+
args: [
|
|
2864
|
+
paymentRequirements.asset,
|
|
2865
|
+
// token
|
|
2866
|
+
payload.authorization.from,
|
|
2867
|
+
// payer
|
|
2868
|
+
BigInt(payload.authorization.value),
|
|
2869
|
+
// amount
|
|
2870
|
+
BigInt(payload.authorization.validAfter),
|
|
2871
|
+
// validAfter
|
|
2872
|
+
BigInt(payload.authorization.validBefore),
|
|
2873
|
+
// validBefore
|
|
2874
|
+
payload.authorization.nonce,
|
|
2875
|
+
// nonce
|
|
2876
|
+
v,
|
|
2877
|
+
// v (uint8)
|
|
2878
|
+
r,
|
|
2879
|
+
// r (bytes32)
|
|
2880
|
+
s
|
|
2881
|
+
// s (bytes32)
|
|
2882
|
+
],
|
|
2883
|
+
chain: wallet.chain
|
|
2884
|
+
});
|
|
2739
2885
|
const receipt = await wallet.waitForTransactionReceipt({ hash: tx });
|
|
2740
2886
|
if (receipt.status !== "success") {
|
|
2741
2887
|
return {
|
|
@@ -2838,22 +2984,12 @@ async function verify3(client, payload, paymentRequirements) {
|
|
|
2838
2984
|
payer: owner
|
|
2839
2985
|
};
|
|
2840
2986
|
}
|
|
2841
|
-
if (paymentRequirements.
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
};
|
|
2848
|
-
}
|
|
2849
|
-
} else {
|
|
2850
|
-
if (client.account && (0, import_viem5.getAddress)(spender) !== (0, import_viem5.getAddress)(client.account.address)) {
|
|
2851
|
-
return {
|
|
2852
|
-
isValid: false,
|
|
2853
|
-
invalidReason: "invalid_spender_address",
|
|
2854
|
-
payer: owner
|
|
2855
|
-
};
|
|
2856
|
-
}
|
|
2987
|
+
if ((0, import_viem5.getAddress)(spender) !== (0, import_viem5.getAddress)(paymentRequirements.payTo)) {
|
|
2988
|
+
return {
|
|
2989
|
+
isValid: false,
|
|
2990
|
+
invalidReason: "invalid_spender_address",
|
|
2991
|
+
payer: owner
|
|
2992
|
+
};
|
|
2857
2993
|
}
|
|
2858
2994
|
const balance = await getERC20Balance(client, erc20Address, owner);
|
|
2859
2995
|
if (balance < BigInt(paymentRequirements.maxAmountRequired)) {
|
|
@@ -2896,84 +3032,40 @@ async function settle3(wallet, paymentPayload, paymentRequirements) {
|
|
|
2896
3032
|
payer: permitPayload.authorization.owner
|
|
2897
3033
|
};
|
|
2898
3034
|
}
|
|
2899
|
-
const { owner,
|
|
3035
|
+
const { owner, value, deadline } = permitPayload.authorization;
|
|
2900
3036
|
const { v, r, s } = splitSignature(permitPayload.signature);
|
|
2901
3037
|
const tokenAddress = paymentRequirements.asset;
|
|
2902
|
-
const
|
|
2903
|
-
address:
|
|
3038
|
+
const transactionHash = await wallet.writeContract({
|
|
3039
|
+
address: paymentRequirements.payTo,
|
|
3040
|
+
abi: EIP7702SellerWalletMinimalAbi,
|
|
3041
|
+
functionName: "settleWithPermit",
|
|
3042
|
+
args: [
|
|
3043
|
+
tokenAddress,
|
|
3044
|
+
// token
|
|
3045
|
+
owner,
|
|
3046
|
+
// payer
|
|
3047
|
+
BigInt(value),
|
|
3048
|
+
// amount
|
|
3049
|
+
BigInt(deadline),
|
|
3050
|
+
// deadline
|
|
3051
|
+
v,
|
|
3052
|
+
// v
|
|
3053
|
+
r,
|
|
3054
|
+
// r
|
|
3055
|
+
s
|
|
3056
|
+
// s
|
|
3057
|
+
],
|
|
3058
|
+
chain: wallet.chain
|
|
2904
3059
|
});
|
|
2905
|
-
|
|
2906
|
-
if (
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
owner,
|
|
2915
|
-
// payer
|
|
2916
|
-
paymentRequirements.payTo,
|
|
2917
|
-
// seller
|
|
2918
|
-
BigInt(value),
|
|
2919
|
-
// amount
|
|
2920
|
-
BigInt(deadline),
|
|
2921
|
-
// deadline
|
|
2922
|
-
v,
|
|
2923
|
-
// v
|
|
2924
|
-
r,
|
|
2925
|
-
// r
|
|
2926
|
-
s
|
|
2927
|
-
// s
|
|
2928
|
-
],
|
|
2929
|
-
chain: wallet.chain,
|
|
2930
|
-
nonce: txNonce
|
|
2931
|
-
});
|
|
2932
|
-
const receipt = await wallet.waitForTransactionReceipt({ hash: transactionHash });
|
|
2933
|
-
if (receipt.status !== "success") {
|
|
2934
|
-
return {
|
|
2935
|
-
success: false,
|
|
2936
|
-
errorReason: "transaction_failed",
|
|
2937
|
-
transaction: transactionHash,
|
|
2938
|
-
network: paymentPayload.network,
|
|
2939
|
-
payer: owner
|
|
2940
|
-
};
|
|
2941
|
-
}
|
|
2942
|
-
} else {
|
|
2943
|
-
const [permitTx, transferTx] = await Promise.all([
|
|
2944
|
-
// Call permit to approve the spender
|
|
2945
|
-
wallet.writeContract({
|
|
2946
|
-
address: tokenAddress,
|
|
2947
|
-
abi: erc20PermitABI,
|
|
2948
|
-
functionName: "permit",
|
|
2949
|
-
args: [owner, spender, BigInt(value), BigInt(deadline), v, r, s],
|
|
2950
|
-
chain: wallet.chain,
|
|
2951
|
-
nonce: txNonce
|
|
2952
|
-
}),
|
|
2953
|
-
// Call transferFrom to transfer tokens to payTo address
|
|
2954
|
-
wallet.writeContract({
|
|
2955
|
-
address: tokenAddress,
|
|
2956
|
-
abi: erc20PermitABI,
|
|
2957
|
-
functionName: "transferFrom",
|
|
2958
|
-
args: [owner, paymentRequirements.payTo, BigInt(value)],
|
|
2959
|
-
chain: wallet.chain,
|
|
2960
|
-
nonce: txNonce + 1
|
|
2961
|
-
})
|
|
2962
|
-
]);
|
|
2963
|
-
const [, receipt] = await Promise.all([
|
|
2964
|
-
wallet.waitForTransactionReceipt({ hash: permitTx }),
|
|
2965
|
-
wallet.waitForTransactionReceipt({ hash: transferTx })
|
|
2966
|
-
]);
|
|
2967
|
-
if (receipt.status !== "success") {
|
|
2968
|
-
return {
|
|
2969
|
-
success: false,
|
|
2970
|
-
errorReason: "transaction_failed",
|
|
2971
|
-
transaction: transferTx,
|
|
2972
|
-
network: paymentPayload.network,
|
|
2973
|
-
payer: owner
|
|
2974
|
-
};
|
|
2975
|
-
}
|
|
2976
|
-
transactionHash = transferTx;
|
|
3060
|
+
const receipt = await wallet.waitForTransactionReceipt({ hash: transactionHash });
|
|
3061
|
+
if (receipt.status !== "success") {
|
|
3062
|
+
return {
|
|
3063
|
+
success: false,
|
|
3064
|
+
errorReason: "transaction_failed",
|
|
3065
|
+
transaction: transactionHash,
|
|
3066
|
+
network: paymentPayload.network,
|
|
3067
|
+
payer: owner
|
|
3068
|
+
};
|
|
2977
3069
|
}
|
|
2978
3070
|
return {
|
|
2979
3071
|
success: true,
|