@armory-sh/middleware-elysia 0.3.25-alpha.23.76 → 0.3.25-alpha.23.78
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/{chunk-XZ4WVA3M.js → chunk-L45RQOFN.js} +59 -18
- package/dist/index.d.ts +1 -1
- package/dist/index.js +76 -20
- package/dist/routes.d.ts +1 -1
- package/dist/routes.js +1 -1
- package/package.json +4 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Elysia } from 'elysia';
|
|
2
1
|
import { matchRoute, PAYMENT_SIGNATURE_HEADER, createPaymentRequiredHeaders, decodePayloadHeader, verifyPayment, settlePayment, createSettlementHeaders, validateRouteConfig } from '@armory-sh/base';
|
|
2
|
+
import { Elysia } from 'elysia';
|
|
3
3
|
|
|
4
4
|
// src/routes.ts
|
|
5
5
|
var resolveRouteConfig = (config) => {
|
|
@@ -37,18 +37,25 @@ var routeAwarePaymentMiddleware = (perRouteConfig) => {
|
|
|
37
37
|
try {
|
|
38
38
|
if (configError) {
|
|
39
39
|
return errorResponse(
|
|
40
|
-
{
|
|
40
|
+
{
|
|
41
|
+
error: "Payment middleware configuration error",
|
|
42
|
+
details: configError.message
|
|
43
|
+
},
|
|
41
44
|
500
|
|
42
45
|
);
|
|
43
46
|
}
|
|
44
47
|
const path = new URL(context.request.url).pathname;
|
|
45
|
-
const matchedRoute = resolvedRoutes.find(
|
|
48
|
+
const matchedRoute = resolvedRoutes.find(
|
|
49
|
+
(r) => matchRoute(r.pattern, path)
|
|
50
|
+
);
|
|
46
51
|
if (!matchedRoute) {
|
|
47
52
|
return;
|
|
48
53
|
}
|
|
49
54
|
const routeConfig = matchedRoute.config;
|
|
50
55
|
const { requirements, facilitatorUrl } = routeConfig;
|
|
51
|
-
const paymentHeader = context.request.headers.get(
|
|
56
|
+
const paymentHeader = context.request.headers.get(
|
|
57
|
+
PAYMENT_SIGNATURE_HEADER
|
|
58
|
+
);
|
|
52
59
|
if (!paymentHeader) {
|
|
53
60
|
return errorResponse(
|
|
54
61
|
{ error: "Payment required", accepts: [requirements] },
|
|
@@ -62,16 +69,29 @@ var routeAwarePaymentMiddleware = (perRouteConfig) => {
|
|
|
62
69
|
accepted: requirements
|
|
63
70
|
});
|
|
64
71
|
} catch (error) {
|
|
65
|
-
return errorResponse(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
return errorResponse(
|
|
73
|
+
{
|
|
74
|
+
error: "Invalid payment payload",
|
|
75
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
76
|
+
},
|
|
77
|
+
400
|
|
78
|
+
);
|
|
69
79
|
}
|
|
70
80
|
if (!facilitatorUrl) {
|
|
71
|
-
return errorResponse(
|
|
81
|
+
return errorResponse(
|
|
82
|
+
{
|
|
83
|
+
error: "Payment middleware configuration error",
|
|
84
|
+
message: "Facilitator URL is required for verification"
|
|
85
|
+
},
|
|
86
|
+
500
|
|
87
|
+
);
|
|
72
88
|
}
|
|
73
89
|
const verifyConfig = { url: facilitatorUrl };
|
|
74
|
-
const verifyResult = await verifyPayment(
|
|
90
|
+
const verifyResult = await verifyPayment(
|
|
91
|
+
payload,
|
|
92
|
+
requirements,
|
|
93
|
+
verifyConfig
|
|
94
|
+
);
|
|
75
95
|
if (!verifyResult.isValid) {
|
|
76
96
|
return errorResponse(
|
|
77
97
|
{ error: verifyResult.invalidReason },
|
|
@@ -80,12 +100,20 @@ var routeAwarePaymentMiddleware = (perRouteConfig) => {
|
|
|
80
100
|
);
|
|
81
101
|
}
|
|
82
102
|
const payerAddress = verifyResult.payer ?? payload.payload.authorization.from;
|
|
83
|
-
context.payment = {
|
|
103
|
+
context.payment = {
|
|
104
|
+
payload,
|
|
105
|
+
payerAddress,
|
|
106
|
+
verified: true,
|
|
107
|
+
route: matchedRoute.pattern
|
|
108
|
+
};
|
|
84
109
|
} catch (error) {
|
|
85
|
-
return errorResponse(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
110
|
+
return errorResponse(
|
|
111
|
+
{
|
|
112
|
+
error: "Payment middleware error",
|
|
113
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
114
|
+
},
|
|
115
|
+
500
|
|
116
|
+
);
|
|
89
117
|
}
|
|
90
118
|
}).onAfterHandle(async (context) => {
|
|
91
119
|
const payment = context.payment;
|
|
@@ -102,12 +130,25 @@ var routeAwarePaymentMiddleware = (perRouteConfig) => {
|
|
|
102
130
|
return;
|
|
103
131
|
}
|
|
104
132
|
if (!routeConfig.facilitatorUrl) {
|
|
105
|
-
return errorResponse(
|
|
133
|
+
return errorResponse(
|
|
134
|
+
{
|
|
135
|
+
error: "Payment middleware configuration error",
|
|
136
|
+
message: "Facilitator URL is required for settlement"
|
|
137
|
+
},
|
|
138
|
+
500
|
|
139
|
+
);
|
|
106
140
|
}
|
|
107
141
|
const settleConfig = { url: routeConfig.facilitatorUrl };
|
|
108
|
-
const settlementResult = await settlePayment(
|
|
142
|
+
const settlementResult = await settlePayment(
|
|
143
|
+
payment.payload,
|
|
144
|
+
routeConfig.requirements,
|
|
145
|
+
settleConfig
|
|
146
|
+
);
|
|
109
147
|
if (!settlementResult.success) {
|
|
110
|
-
return errorResponse(
|
|
148
|
+
return errorResponse(
|
|
149
|
+
{ error: "Settlement failed", details: settlementResult.errorReason },
|
|
150
|
+
502
|
|
151
|
+
);
|
|
111
152
|
}
|
|
112
153
|
return {
|
|
113
154
|
headers: createSettlementHeaders(settlementResult)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PaymentRequirementsV2, X402PaymentPayload, X402PaymentRequirements } from '@armory-sh/base';
|
|
2
|
-
export { PaymentMiddlewareConfigEntry, RouteAwarePaymentInfo, RouteAwarePaymentMiddlewareConfig, routeAwarePaymentMiddleware } from './routes.js';
|
|
3
2
|
import { Elysia } from 'elysia';
|
|
3
|
+
export { PaymentMiddlewareConfigEntry, RouteAwarePaymentInfo, RouteAwarePaymentMiddlewareConfig, routeAwarePaymentMiddleware } from './routes.js';
|
|
4
4
|
|
|
5
5
|
type NetworkId = string | number;
|
|
6
6
|
type TokenId = string;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { routeAwarePaymentMiddleware } from './chunk-
|
|
2
|
-
import { Elysia } from 'elysia';
|
|
1
|
+
export { routeAwarePaymentMiddleware } from './chunk-L45RQOFN.js';
|
|
3
2
|
import { resolveNetwork, isValidationError, resolveToken, createPaymentRequirements as createPaymentRequirements$1, PAYMENT_SIGNATURE_HEADER, createPaymentRequiredHeaders, decodePayloadHeader, verifyPayment, settlePayment, createSettlementHeaders, TOKENS, registerToken } from '@armory-sh/base';
|
|
3
|
+
import { Elysia } from 'elysia';
|
|
4
4
|
|
|
5
5
|
function ensureTokensRegistered() {
|
|
6
6
|
for (const token of Object.values(TOKENS)) {
|
|
@@ -14,7 +14,9 @@ function resolveFacilitatorUrlFromRequirement(config, requirement) {
|
|
|
14
14
|
const chainId = parseInt(requirement.network.split(":")[1] || "0", 10);
|
|
15
15
|
const assetAddress = requirement.asset.toLowerCase();
|
|
16
16
|
if (config.facilitatorUrlByToken) {
|
|
17
|
-
for (const [chainKey, tokenMap] of Object.entries(
|
|
17
|
+
for (const [chainKey, tokenMap] of Object.entries(
|
|
18
|
+
config.facilitatorUrlByToken
|
|
19
|
+
)) {
|
|
18
20
|
const resolvedChain = resolveNetwork(chainKey);
|
|
19
21
|
if (!isValidationError(resolvedChain) && resolvedChain.config.chainId === chainId) {
|
|
20
22
|
for (const [, url] of Object.entries(tokenMap)) {
|
|
@@ -32,7 +34,9 @@ function resolveFacilitatorUrlFromRequirement(config, requirement) {
|
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
if (config.facilitatorUrlByChain) {
|
|
35
|
-
for (const [chainKey, url] of Object.entries(
|
|
37
|
+
for (const [chainKey, url] of Object.entries(
|
|
38
|
+
config.facilitatorUrlByChain
|
|
39
|
+
)) {
|
|
36
40
|
const resolvedChain = resolveNetwork(chainKey);
|
|
37
41
|
if (!isValidationError(resolvedChain) && resolvedChain.config.chainId === chainId) {
|
|
38
42
|
return url;
|
|
@@ -56,15 +60,26 @@ var paymentMiddleware = (config) => {
|
|
|
56
60
|
})).onBeforeHandle(async (context) => {
|
|
57
61
|
if (error) {
|
|
58
62
|
return errorResponse(
|
|
59
|
-
{
|
|
63
|
+
{
|
|
64
|
+
error: "Payment middleware configuration error",
|
|
65
|
+
details: error.message
|
|
66
|
+
},
|
|
60
67
|
500
|
|
61
68
|
);
|
|
62
69
|
}
|
|
63
70
|
const primaryRequirement = requirements[0];
|
|
64
71
|
if (!primaryRequirement) {
|
|
65
|
-
return errorResponse(
|
|
72
|
+
return errorResponse(
|
|
73
|
+
{
|
|
74
|
+
error: "Payment middleware configuration error",
|
|
75
|
+
message: "No payment requirements configured"
|
|
76
|
+
},
|
|
77
|
+
500
|
|
78
|
+
);
|
|
66
79
|
}
|
|
67
|
-
const paymentHeader = context.request.headers.get(
|
|
80
|
+
const paymentHeader = context.request.headers.get(
|
|
81
|
+
PAYMENT_SIGNATURE_HEADER
|
|
82
|
+
);
|
|
68
83
|
if (!paymentHeader) {
|
|
69
84
|
return errorResponse(
|
|
70
85
|
{ error: "Payment required", accepts: requirements },
|
|
@@ -78,19 +93,38 @@ var paymentMiddleware = (config) => {
|
|
|
78
93
|
accepted: primaryRequirement
|
|
79
94
|
});
|
|
80
95
|
} catch (err) {
|
|
81
|
-
return errorResponse(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
96
|
+
return errorResponse(
|
|
97
|
+
{
|
|
98
|
+
error: "Invalid payment payload",
|
|
99
|
+
message: err instanceof Error ? err.message : "Unknown error"
|
|
100
|
+
},
|
|
101
|
+
400
|
|
102
|
+
);
|
|
85
103
|
}
|
|
86
|
-
const facilitatorUrl = resolveFacilitatorUrlFromRequirement(
|
|
104
|
+
const facilitatorUrl = resolveFacilitatorUrlFromRequirement(
|
|
105
|
+
config,
|
|
106
|
+
primaryRequirement
|
|
107
|
+
);
|
|
87
108
|
if (!facilitatorUrl) {
|
|
88
|
-
return errorResponse(
|
|
109
|
+
return errorResponse(
|
|
110
|
+
{
|
|
111
|
+
error: "Payment middleware configuration error",
|
|
112
|
+
message: "Facilitator URL is required for verification"
|
|
113
|
+
},
|
|
114
|
+
500
|
|
115
|
+
);
|
|
89
116
|
}
|
|
90
|
-
const verifyResult = await verifyPayment(
|
|
117
|
+
const verifyResult = await verifyPayment(
|
|
118
|
+
paymentPayload,
|
|
119
|
+
primaryRequirement,
|
|
120
|
+
{ url: facilitatorUrl }
|
|
121
|
+
);
|
|
91
122
|
if (!verifyResult.isValid) {
|
|
92
123
|
return errorResponse(
|
|
93
|
-
{
|
|
124
|
+
{
|
|
125
|
+
error: "Payment verification failed",
|
|
126
|
+
message: verifyResult.invalidReason
|
|
127
|
+
},
|
|
94
128
|
402,
|
|
95
129
|
createPaymentRequiredHeaders(requirements)
|
|
96
130
|
);
|
|
@@ -108,15 +142,37 @@ var paymentMiddleware = (config) => {
|
|
|
108
142
|
}
|
|
109
143
|
const primaryRequirement = requirements[0];
|
|
110
144
|
if (!primaryRequirement) {
|
|
111
|
-
return errorResponse(
|
|
145
|
+
return errorResponse(
|
|
146
|
+
{
|
|
147
|
+
error: "Payment middleware configuration error",
|
|
148
|
+
message: "No payment requirements configured"
|
|
149
|
+
},
|
|
150
|
+
500
|
|
151
|
+
);
|
|
112
152
|
}
|
|
113
|
-
const facilitatorUrl = resolveFacilitatorUrlFromRequirement(
|
|
153
|
+
const facilitatorUrl = resolveFacilitatorUrlFromRequirement(
|
|
154
|
+
config,
|
|
155
|
+
primaryRequirement
|
|
156
|
+
);
|
|
114
157
|
if (!facilitatorUrl) {
|
|
115
|
-
return errorResponse(
|
|
158
|
+
return errorResponse(
|
|
159
|
+
{
|
|
160
|
+
error: "Payment middleware configuration error",
|
|
161
|
+
message: "Facilitator URL is required for settlement"
|
|
162
|
+
},
|
|
163
|
+
500
|
|
164
|
+
);
|
|
116
165
|
}
|
|
117
|
-
const settleResult = await settlePayment(
|
|
166
|
+
const settleResult = await settlePayment(
|
|
167
|
+
payment.payload,
|
|
168
|
+
primaryRequirement,
|
|
169
|
+
{ url: facilitatorUrl }
|
|
170
|
+
);
|
|
118
171
|
if (!settleResult.success) {
|
|
119
|
-
return errorResponse(
|
|
172
|
+
return errorResponse(
|
|
173
|
+
{ error: "Settlement failed", details: settleResult.errorReason },
|
|
174
|
+
502
|
|
175
|
+
);
|
|
120
176
|
}
|
|
121
177
|
return {
|
|
122
178
|
headers: createSettlementHeaders(settleResult)
|
package/dist/routes.d.ts
CHANGED
package/dist/routes.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { routeAwarePaymentMiddleware } from './chunk-
|
|
1
|
+
export { routeAwarePaymentMiddleware } from './chunk-L45RQOFN.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armory-sh/middleware-elysia",
|
|
3
|
-
"version": "0.3.25-alpha.23.
|
|
3
|
+
"version": "0.3.25-alpha.23.78",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Sawyer Cutler <sawyer@dirtroad.dev>",
|
|
6
6
|
"keywords": [
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"elysia": "^1"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@armory-sh/base": "0.2.27-alpha.23.
|
|
50
|
+
"@armory-sh/base": "0.2.27-alpha.23.78"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"bun-types": "latest",
|
|
@@ -57,6 +57,8 @@
|
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "rm -rf dist && tsup",
|
|
60
|
+
"lint": "bun run build",
|
|
61
|
+
"format": "bun run lint",
|
|
60
62
|
"test": "bun test"
|
|
61
63
|
}
|
|
62
64
|
}
|