@armory-sh/middleware-hono 0.3.2 → 0.3.8
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/index.js +41 -14
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -3,21 +3,49 @@ import {
|
|
|
3
3
|
resolveNetwork,
|
|
4
4
|
resolveToken,
|
|
5
5
|
validateAcceptConfig,
|
|
6
|
-
isValidationError
|
|
6
|
+
isValidationError,
|
|
7
|
+
normalizeNetworkName as normalizeNetworkName2
|
|
7
8
|
} from "@armory-sh/base";
|
|
8
9
|
|
|
9
10
|
// src/core.ts
|
|
10
11
|
import {
|
|
11
12
|
getNetworkConfig,
|
|
12
13
|
getNetworkByChainId,
|
|
13
|
-
encodePaymentPayload
|
|
14
|
+
encodePaymentPayload,
|
|
15
|
+
normalizeNetworkName
|
|
14
16
|
} from "@armory-sh/base";
|
|
15
|
-
var
|
|
16
|
-
if (typeof network === "
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
var toSlug = (network) => {
|
|
18
|
+
if (typeof network === "number") {
|
|
19
|
+
const net = getNetworkByChainId(network);
|
|
20
|
+
if (!net) throw new Error(`No network found for chainId: ${network}`);
|
|
21
|
+
return normalizeNetworkName(net.name);
|
|
22
|
+
}
|
|
23
|
+
if (network.startsWith("eip155:")) {
|
|
24
|
+
const chainId = parseInt(network.split(":")[1], 10);
|
|
25
|
+
const net = getNetworkByChainId(chainId);
|
|
26
|
+
if (!net) throw new Error(`No network found for chainId: ${chainId}`);
|
|
27
|
+
return normalizeNetworkName(net.name);
|
|
28
|
+
}
|
|
29
|
+
return normalizeNetworkName(network);
|
|
30
|
+
};
|
|
31
|
+
var toEip155 = (network) => {
|
|
32
|
+
if (typeof network === "number") {
|
|
33
|
+
const net2 = getNetworkByChainId(network);
|
|
34
|
+
if (!net2) throw new Error(`No network found for chainId: ${network}`);
|
|
35
|
+
return net2.caip2Id;
|
|
36
|
+
}
|
|
37
|
+
if (network.startsWith("eip155:")) {
|
|
38
|
+
const net2 = getNetworkConfig(network);
|
|
39
|
+
if (!net2) throw new Error(`No network found for: ${network}`);
|
|
40
|
+
return net2.caip2Id;
|
|
41
|
+
}
|
|
42
|
+
const slug = normalizeNetworkName(network);
|
|
43
|
+
const net = getNetworkConfig(slug);
|
|
44
|
+
if (!net) throw new Error(`No network found for: ${slug}`);
|
|
45
|
+
return net.caip2Id;
|
|
20
46
|
};
|
|
47
|
+
var getNetworkName = (network) => toSlug(network);
|
|
48
|
+
var getChainId = (network) => toEip155(network);
|
|
21
49
|
var createV1Requirements = (config, expiry) => {
|
|
22
50
|
const networkName = getNetworkName(config.network);
|
|
23
51
|
const network = getNetworkConfig(networkName);
|
|
@@ -37,7 +65,7 @@ var createV2Requirements = (config, expiry) => {
|
|
|
37
65
|
return {
|
|
38
66
|
amount: config.amount,
|
|
39
67
|
to: config.payTo,
|
|
40
|
-
chainId: network
|
|
68
|
+
chainId: getChainId(config.network),
|
|
41
69
|
assetId: network.caipAssetId,
|
|
42
70
|
nonce: `${Date.now()}-${crypto.randomUUID()}`,
|
|
43
71
|
expiry
|
|
@@ -145,7 +173,7 @@ var resolveMiddlewareConfig = (config) => {
|
|
|
145
173
|
createHeaders: f.input.headers
|
|
146
174
|
})) ?? [];
|
|
147
175
|
const enrichedConfigs = result.config.map((c) => {
|
|
148
|
-
const networkName = c.network.config.name;
|
|
176
|
+
const networkName = normalizeNetworkName2(c.network.config.name);
|
|
149
177
|
const tokenSymbol = c.token.config.symbol;
|
|
150
178
|
const facilitatorPricing = c.facilitators.map((f) => {
|
|
151
179
|
const pricingConfig = findPricingConfig(pricing, networkName, tokenSymbol, f.url);
|
|
@@ -172,7 +200,7 @@ var getPrimaryConfig = (resolved) => {
|
|
|
172
200
|
}
|
|
173
201
|
return {
|
|
174
202
|
payTo: primary.payTo,
|
|
175
|
-
network: primary.network.config.name,
|
|
203
|
+
network: normalizeNetworkName2(primary.network.config.name),
|
|
176
204
|
amount: primary.amount,
|
|
177
205
|
facilitator: resolved.facilitators[0],
|
|
178
206
|
settlementMode: "verify"
|
|
@@ -241,13 +269,12 @@ var acceptPaymentsViaArmory = (config) => {
|
|
|
241
269
|
}
|
|
242
270
|
const primaryConfig = getPrimaryConfig(resolved);
|
|
243
271
|
const defaultVersion = config.defaultVersion ?? 2;
|
|
244
|
-
const
|
|
245
|
-
const requirementsV2 = createPaymentRequirements(primaryConfig, 2);
|
|
272
|
+
const createRequirements = (version) => createPaymentRequirements(primaryConfig, version);
|
|
246
273
|
return async (c, next) => {
|
|
247
274
|
const paymentHeader = c.req.header("X-Payment") || c.req.header("x402-payment");
|
|
248
275
|
if (!paymentHeader) {
|
|
249
|
-
const requirements = defaultVersion === 1 ? requirementsV1 : requirementsV2;
|
|
250
276
|
const version = defaultVersion === 1 ? 1 : 2;
|
|
277
|
+
const requirements = createRequirements(version);
|
|
251
278
|
const headers = getHeadersForVersion(version);
|
|
252
279
|
c.status(402);
|
|
253
280
|
c.header(headers.required, encodeRequirements(requirements));
|
|
@@ -259,7 +286,7 @@ var acceptPaymentsViaArmory = (config) => {
|
|
|
259
286
|
if (primaryConfig.facilitator) {
|
|
260
287
|
const verifyResult = await verifyWithFacilitator(toHttpRequest(c), primaryConfig.facilitator);
|
|
261
288
|
if (!verifyResult.success) {
|
|
262
|
-
const requirements = version
|
|
289
|
+
const requirements = createRequirements(version);
|
|
263
290
|
const headers2 = getHeadersForVersion(version);
|
|
264
291
|
c.status(402);
|
|
265
292
|
c.header(headers2.required, encodeRequirements(requirements));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armory-sh/middleware-hono",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Sawyer Cutler <sawyer@dirtroad.dev>",
|
|
6
6
|
"type": "module",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"hono": "^4"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@armory-sh/base": "0.2.
|
|
32
|
-
"@armory-sh/facilitator": "0.2.
|
|
31
|
+
"@armory-sh/base": "0.2.9",
|
|
32
|
+
"@armory-sh/facilitator": "0.2.9"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"bun-types": "latest",
|