@x402x/extensions 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +53 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -27
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -11
package/dist/index.mjs
CHANGED
|
@@ -158,7 +158,7 @@ function validateCommitmentParams(params) {
|
|
|
158
158
|
BigInt(params.validAfter);
|
|
159
159
|
BigInt(params.validBefore);
|
|
160
160
|
BigInt(params.facilitatorFee);
|
|
161
|
-
} catch
|
|
161
|
+
} catch {
|
|
162
162
|
throw new Error("Invalid numeric parameter");
|
|
163
163
|
}
|
|
164
164
|
if (!isValidHex(params.salt) || params.salt.length !== 66) {
|
|
@@ -181,17 +181,20 @@ var NETWORK_ALIASES_V1_TO_V2 = {
|
|
|
181
181
|
"base-sepolia": "eip155:84532",
|
|
182
182
|
"x-layer-testnet": "eip155:1952",
|
|
183
183
|
"skale-base-sepolia": "eip155:324705682",
|
|
184
|
-
|
|
184
|
+
base: "eip155:8453",
|
|
185
185
|
"x-layer": "eip155:196",
|
|
186
186
|
"bsc-testnet": "eip155:97",
|
|
187
|
-
|
|
187
|
+
bsc: "eip155:56"
|
|
188
188
|
};
|
|
189
189
|
var NETWORK_ALIASES = Object.entries(
|
|
190
190
|
NETWORK_ALIASES_V1_TO_V2
|
|
191
|
-
).reduce(
|
|
192
|
-
acc[caip2]
|
|
193
|
-
|
|
194
|
-
|
|
191
|
+
).reduce(
|
|
192
|
+
(acc, [name, caip2]) => {
|
|
193
|
+
acc[caip2] = name;
|
|
194
|
+
return acc;
|
|
195
|
+
},
|
|
196
|
+
{}
|
|
197
|
+
);
|
|
195
198
|
var DEFAULT_ASSETS = {
|
|
196
199
|
"eip155:84532": {
|
|
197
200
|
address: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
|
|
@@ -260,7 +263,8 @@ function getNetworkAlias(network) {
|
|
|
260
263
|
return networkAlias;
|
|
261
264
|
}
|
|
262
265
|
function getDefaultAsset(network) {
|
|
263
|
-
const
|
|
266
|
+
const canonicalNetwork = toCanonicalNetworkKey(network);
|
|
267
|
+
const assetInfo = DEFAULT_ASSETS[canonicalNetwork];
|
|
264
268
|
if (!assetInfo) {
|
|
265
269
|
throw new Error(`No default asset configured for network: ${network}`);
|
|
266
270
|
}
|
|
@@ -804,7 +808,10 @@ function validateSettlementExtra(extra) {
|
|
|
804
808
|
return { valid: false, error: "salt is required" };
|
|
805
809
|
}
|
|
806
810
|
if (!isValid32ByteHex(extra.salt)) {
|
|
807
|
-
return {
|
|
811
|
+
return {
|
|
812
|
+
valid: false,
|
|
813
|
+
error: "salt must be a 32-byte hex string (0x followed by 64 hex characters)"
|
|
814
|
+
};
|
|
808
815
|
}
|
|
809
816
|
return { valid: true };
|
|
810
817
|
}
|
|
@@ -1009,8 +1016,8 @@ async function calculateFacilitatorFee(facilitatorUrl, network, hook, hookData,
|
|
|
1009
1016
|
headers: {
|
|
1010
1017
|
"Content-Type": "application/json"
|
|
1011
1018
|
},
|
|
1012
|
-
// Add timeout
|
|
1013
|
-
signal: AbortSignal.timeout(
|
|
1019
|
+
// Add timeout (10 seconds for remote facilitators with potential network latency)
|
|
1020
|
+
signal: AbortSignal.timeout(1e4)
|
|
1014
1021
|
});
|
|
1015
1022
|
if (!response.ok) {
|
|
1016
1023
|
const errorText = await response.text();
|
|
@@ -1138,7 +1145,9 @@ function createSettlementRouteConfig(baseConfig, settlementOptions) {
|
|
|
1138
1145
|
const originalPayTo = typeof option.payTo === "string" ? option.payTo : void 0;
|
|
1139
1146
|
const finalPayTo = settlementOptions?.finalPayTo || originalPayTo;
|
|
1140
1147
|
if (!finalPayTo) {
|
|
1141
|
-
throw new Error(
|
|
1148
|
+
throw new Error(
|
|
1149
|
+
`Cannot determine finalPayTo: neither settlementOptions.finalPayTo nor option.payTo (string) is provided for network ${network}`
|
|
1150
|
+
);
|
|
1142
1151
|
}
|
|
1143
1152
|
const hook = settlementOptions?.hook || TransferHook.getAddress(network);
|
|
1144
1153
|
const hookData = settlementOptions?.hookData || TransferHook.encode();
|
|
@@ -1160,10 +1169,15 @@ function createSettlementRouteConfig(baseConfig, settlementOptions) {
|
|
|
1160
1169
|
extra: accepted.extra
|
|
1161
1170
|
};
|
|
1162
1171
|
} else {
|
|
1163
|
-
console.warn(
|
|
1172
|
+
console.warn(
|
|
1173
|
+
"[x402x-settlement] Network/scheme mismatch in retry, falling back to probe"
|
|
1174
|
+
);
|
|
1164
1175
|
}
|
|
1165
1176
|
} catch (error) {
|
|
1166
|
-
console.error(
|
|
1177
|
+
console.error(
|
|
1178
|
+
"[x402x-settlement] Failed to decode payment header, falling back to probe:",
|
|
1179
|
+
error
|
|
1180
|
+
);
|
|
1167
1181
|
}
|
|
1168
1182
|
}
|
|
1169
1183
|
console.log("[x402x-settlement] Probe request, generating new salt and querying fee");
|
|
@@ -1187,7 +1201,11 @@ function createSettlementRouteConfig(baseConfig, settlementOptions) {
|
|
|
1187
1201
|
facilitatorFee = settlementOptions.facilitatorFee;
|
|
1188
1202
|
console.log("[x402x-settlement] Using fixed facilitatorFee:", facilitatorFee);
|
|
1189
1203
|
} else {
|
|
1190
|
-
console.log("[x402x-settlement] Querying facilitator for fee:", {
|
|
1204
|
+
console.log("[x402x-settlement] Querying facilitator for fee:", {
|
|
1205
|
+
network,
|
|
1206
|
+
hook,
|
|
1207
|
+
hookData
|
|
1208
|
+
});
|
|
1191
1209
|
try {
|
|
1192
1210
|
const feeResult = await calculateFacilitatorFee(facilitatorUrl, network, hook, hookData);
|
|
1193
1211
|
if (!feeResult.hookAllowed) {
|
|
@@ -1246,10 +1264,7 @@ function createSettlementRouteConfig(baseConfig, settlementOptions) {
|
|
|
1246
1264
|
};
|
|
1247
1265
|
}
|
|
1248
1266
|
function registerSettlementHooks(server, config = {}) {
|
|
1249
|
-
const {
|
|
1250
|
-
enableSaltExtraction = true,
|
|
1251
|
-
validateSettlementParams = true
|
|
1252
|
-
} = config;
|
|
1267
|
+
const { enableSaltExtraction = true, validateSettlementParams = true } = config;
|
|
1253
1268
|
if (enableSaltExtraction) {
|
|
1254
1269
|
server.onBeforeVerify(async (context) => {
|
|
1255
1270
|
const { paymentPayload, requirements } = context;
|
|
@@ -1261,11 +1276,13 @@ function registerSettlementHooks(server, config = {}) {
|
|
|
1261
1276
|
}
|
|
1262
1277
|
const info = extension.info;
|
|
1263
1278
|
if (info.salt) requirements.extra.salt = info.salt;
|
|
1264
|
-
if (info.settlementRouter)
|
|
1279
|
+
if (info.settlementRouter)
|
|
1280
|
+
requirements.extra.settlementRouter = info.settlementRouter;
|
|
1265
1281
|
if (info.hook) requirements.extra.hook = info.hook;
|
|
1266
1282
|
if (info.hookData) requirements.extra.hookData = info.hookData;
|
|
1267
1283
|
if (info.finalPayTo) requirements.extra.payTo = info.finalPayTo;
|
|
1268
|
-
if (info.facilitatorFee !== void 0)
|
|
1284
|
+
if (info.facilitatorFee !== void 0)
|
|
1285
|
+
requirements.extra.facilitatorFee = info.facilitatorFee;
|
|
1269
1286
|
}
|
|
1270
1287
|
}
|
|
1271
1288
|
return void 0;
|
|
@@ -1519,7 +1536,7 @@ var ExactEvmSchemeWithRouterSettlement = class {
|
|
|
1519
1536
|
}
|
|
1520
1537
|
/**
|
|
1521
1538
|
* Creates a payment payload for the Exact scheme with router settlement.
|
|
1522
|
-
*
|
|
1539
|
+
*
|
|
1523
1540
|
* This method:
|
|
1524
1541
|
* 1. Extracts settlement parameters from PaymentRequired.extensions
|
|
1525
1542
|
* 2. Calculates a commitment hash binding all parameters
|
|
@@ -1529,7 +1546,7 @@ var ExactEvmSchemeWithRouterSettlement = class {
|
|
|
1529
1546
|
* @param x402Version - The x402 protocol version (must be 2)
|
|
1530
1547
|
* @param paymentRequirements - The payment requirements from the server
|
|
1531
1548
|
* @returns Promise resolving to a payment payload
|
|
1532
|
-
*
|
|
1549
|
+
*
|
|
1533
1550
|
* @throws Error if x402Version is not 2
|
|
1534
1551
|
* @throws Error if x402x-router-settlement extension is missing
|
|
1535
1552
|
* @throws Error if required settlement parameters are missing
|
|
@@ -1638,18 +1655,27 @@ function injectX402xExtensionHandler(client, onRouterSettlementExtension) {
|
|
|
1638
1655
|
const { paymentRequired, selectedRequirements } = context;
|
|
1639
1656
|
console.log("[x402x-handler] onBeforePaymentCreation called");
|
|
1640
1657
|
console.log("[x402x-handler] selectedRequirements.network:", selectedRequirements.network);
|
|
1641
|
-
console.log(
|
|
1658
|
+
console.log(
|
|
1659
|
+
"[x402x-handler] selectedRequirements.extra keys:",
|
|
1660
|
+
Object.keys(selectedRequirements.extra || {})
|
|
1661
|
+
);
|
|
1642
1662
|
const perOptionExtension = selectedRequirements.extra?.[ROUTER_SETTLEMENT_KEY];
|
|
1643
1663
|
if (perOptionExtension) {
|
|
1644
1664
|
if (!paymentRequired.extensions) {
|
|
1645
1665
|
paymentRequired.extensions = {};
|
|
1646
1666
|
}
|
|
1647
1667
|
paymentRequired.extensions[ROUTER_SETTLEMENT_KEY] = perOptionExtension;
|
|
1648
|
-
console.log(
|
|
1668
|
+
console.log(
|
|
1669
|
+
"[x402x-handler] \u2705 Copied per-option x402x info into PaymentRequired.extensions"
|
|
1670
|
+
);
|
|
1649
1671
|
console.log("[x402x-handler] Extension info:", JSON.stringify(perOptionExtension, null, 2));
|
|
1650
1672
|
} else {
|
|
1651
|
-
console.warn(
|
|
1652
|
-
|
|
1673
|
+
console.warn(
|
|
1674
|
+
"[x402x-handler] \u26A0\uFE0F No per-option x402x info found in selectedRequirements.extra"
|
|
1675
|
+
);
|
|
1676
|
+
console.warn(
|
|
1677
|
+
"[x402x-handler] This may cause facilitator errors. Check server-side createSettlementRouteConfig."
|
|
1678
|
+
);
|
|
1653
1679
|
}
|
|
1654
1680
|
if (onRouterSettlementExtension) {
|
|
1655
1681
|
const extensionToUse = perOptionExtension || paymentRequired.extensions?.[ROUTER_SETTLEMENT_KEY];
|