@puga-labs/x402-mantle-sdk 0.3.7 → 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/chunk-33WHNZDS.js +74 -0
- package/dist/chunk-B4GST723.js +101 -0
- package/dist/chunk-OW2BDRGZ.js +239 -0
- package/dist/chunk-T63MVWX3.js +71 -0
- package/dist/express-D8L5Dg1D.d.ts +68 -0
- package/dist/express-Pukmnwuu.d.cts +68 -0
- package/dist/index.cjs +7 -3
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/nextjs-CSUWjcxv.d.cts +89 -0
- package/dist/nextjs-DUdgN0d_.d.ts +89 -0
- package/dist/server-express.cjs +7 -3
- package/dist/server-express.d.cts +2 -2
- package/dist/server-express.d.ts +2 -2
- package/dist/server-express.js +2 -2
- package/dist/server-nextjs.cjs +5 -2
- package/dist/server-nextjs.d.cts +2 -2
- package/dist/server-nextjs.d.ts +2 -2
- package/dist/server-nextjs.js +2 -2
- package/dist/server-web.cjs +5 -2
- package/dist/server-web.d.cts +2 -2
- package/dist/server-web.d.ts +2 -2
- package/dist/server-web.js +2 -2
- package/dist/server.cjs +11 -5
- package/dist/server.d.cts +6 -6
- package/dist/server.d.ts +6 -6
- package/dist/server.js +4 -4
- package/dist/types-BWfKovFm.d.cts +103 -0
- package/dist/types-BmK0G74m.d.cts +93 -0
- package/dist/types-CXdNC0Ra.d.ts +93 -0
- package/dist/types-DgfVPQFb.d.ts +103 -0
- package/dist/web-standards-BvMLEKlU.d.cts +77 -0
- package/dist/web-standards-g2rYUpgc.d.ts +77 -0
- package/package.json +1 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-OW2BDRGZ.js";
|
|
6
|
+
import {
|
|
7
|
+
MANTLE_DEFAULTS,
|
|
8
|
+
__export,
|
|
9
|
+
getDefaultAssetForNetwork,
|
|
10
|
+
usdCentsToAtomic
|
|
11
|
+
} from "./chunk-HEZZ74SI.js";
|
|
12
|
+
|
|
13
|
+
// src/server/adapters/nextjs.ts
|
|
14
|
+
var nextjs_exports = {};
|
|
15
|
+
__export(nextjs_exports, {
|
|
16
|
+
isPaywallErrorResponse: () => isPaywallErrorResponse,
|
|
17
|
+
mantlePaywall: () => mantlePaywall
|
|
18
|
+
});
|
|
19
|
+
import { NextResponse } from "next/server";
|
|
20
|
+
function mantlePaywall(opts) {
|
|
21
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
22
|
+
validateAddress(payTo, "payTo");
|
|
23
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
24
|
+
return function(handler) {
|
|
25
|
+
return async (req) => {
|
|
26
|
+
const url = new URL(req.url);
|
|
27
|
+
const method = req.method;
|
|
28
|
+
const path = url.pathname;
|
|
29
|
+
const routeKey = buildRouteKey(method, path);
|
|
30
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
31
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
32
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
33
|
+
priceUsdCents,
|
|
34
|
+
assetConfig.decimals
|
|
35
|
+
);
|
|
36
|
+
const paymentRequirements = {
|
|
37
|
+
scheme: "exact",
|
|
38
|
+
network,
|
|
39
|
+
asset: assetConfig.address,
|
|
40
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
41
|
+
payTo,
|
|
42
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
43
|
+
currency: "USD"
|
|
44
|
+
};
|
|
45
|
+
const paymentHeader = req.headers.get("X-PAYMENT") || req.headers.get("x-payment") || null;
|
|
46
|
+
const result = await checkPayment({
|
|
47
|
+
paymentHeader,
|
|
48
|
+
paymentRequirements,
|
|
49
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
50
|
+
apiKey,
|
|
51
|
+
routeKey,
|
|
52
|
+
network,
|
|
53
|
+
asset: assetConfig.address,
|
|
54
|
+
telemetry,
|
|
55
|
+
onPaymentSettled
|
|
56
|
+
});
|
|
57
|
+
if (!result.isValid) {
|
|
58
|
+
return NextResponse.json(result.responseBody, {
|
|
59
|
+
status: result.statusCode
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return handler(req);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function isPaywallErrorResponse(response) {
|
|
67
|
+
return typeof response === "object" && response !== null && "error" in response && typeof response.error === "string";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export {
|
|
71
|
+
mantlePaywall,
|
|
72
|
+
isPaywallErrorResponse,
|
|
73
|
+
nextjs_exports
|
|
74
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-OW2BDRGZ.js";
|
|
6
|
+
import {
|
|
7
|
+
MANTLE_DEFAULTS,
|
|
8
|
+
__export,
|
|
9
|
+
getDefaultAssetForNetwork,
|
|
10
|
+
usdCentsToAtomic
|
|
11
|
+
} from "./chunk-HEZZ74SI.js";
|
|
12
|
+
|
|
13
|
+
// src/server/adapters/express.ts
|
|
14
|
+
var express_exports = {};
|
|
15
|
+
__export(express_exports, {
|
|
16
|
+
createPaymentMiddleware: () => createPaymentMiddleware,
|
|
17
|
+
mantlePaywall: () => mantlePaywall
|
|
18
|
+
});
|
|
19
|
+
function createPaymentMiddleware(config) {
|
|
20
|
+
const { facilitatorUrl, receiverAddress, routes, apiKey, onPaymentSettled, telemetry } = config;
|
|
21
|
+
if (!facilitatorUrl) {
|
|
22
|
+
throw new Error("facilitatorUrl is required");
|
|
23
|
+
}
|
|
24
|
+
if (!receiverAddress) {
|
|
25
|
+
throw new Error("receiverAddress is required");
|
|
26
|
+
}
|
|
27
|
+
validateAddress(receiverAddress, "receiverAddress");
|
|
28
|
+
if (!routes || Object.keys(routes).length === 0) {
|
|
29
|
+
throw new Error("routes config must not be empty");
|
|
30
|
+
}
|
|
31
|
+
return async function paymentMiddleware(req, res, next) {
|
|
32
|
+
const routeKey = buildRouteKey(req.method, req.path);
|
|
33
|
+
const routeConfig = routes[routeKey];
|
|
34
|
+
if (!routeConfig) {
|
|
35
|
+
next();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const { priceUsdCents, network } = routeConfig;
|
|
39
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
40
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
41
|
+
priceUsdCents,
|
|
42
|
+
assetConfig.decimals
|
|
43
|
+
);
|
|
44
|
+
const paymentRequirements = {
|
|
45
|
+
scheme: "exact",
|
|
46
|
+
network,
|
|
47
|
+
asset: assetConfig.address,
|
|
48
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
49
|
+
payTo: receiverAddress,
|
|
50
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
51
|
+
currency: "USD"
|
|
52
|
+
};
|
|
53
|
+
const paymentHeader = req.header("X-PAYMENT") || req.header("x-payment") || null;
|
|
54
|
+
const result = await checkPayment({
|
|
55
|
+
paymentHeader,
|
|
56
|
+
paymentRequirements,
|
|
57
|
+
facilitatorUrl,
|
|
58
|
+
apiKey,
|
|
59
|
+
routeKey,
|
|
60
|
+
network,
|
|
61
|
+
asset: assetConfig.address,
|
|
62
|
+
telemetry,
|
|
63
|
+
onPaymentSettled
|
|
64
|
+
});
|
|
65
|
+
if (!result.isValid) {
|
|
66
|
+
res.status(result.statusCode).json(result.responseBody);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
next();
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function mantlePaywall(opts) {
|
|
73
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
74
|
+
validateAddress(payTo, "payTo");
|
|
75
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
76
|
+
return async (req, res, next) => {
|
|
77
|
+
const method = (req.method || "GET").toUpperCase();
|
|
78
|
+
const path = req.path || "/";
|
|
79
|
+
const routeKey = `${method} ${path}`;
|
|
80
|
+
const middleware = createPaymentMiddleware({
|
|
81
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
82
|
+
receiverAddress: payTo,
|
|
83
|
+
routes: {
|
|
84
|
+
[routeKey]: {
|
|
85
|
+
priceUsdCents,
|
|
86
|
+
network: MANTLE_DEFAULTS.NETWORK
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
apiKey,
|
|
90
|
+
telemetry,
|
|
91
|
+
onPaymentSettled
|
|
92
|
+
});
|
|
93
|
+
return middleware(req, res, next);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export {
|
|
98
|
+
createPaymentMiddleware,
|
|
99
|
+
mantlePaywall,
|
|
100
|
+
express_exports
|
|
101
|
+
};
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getDefaultAssetForNetwork
|
|
3
|
+
} from "./chunk-HEZZ74SI.js";
|
|
4
|
+
|
|
5
|
+
// src/server/core/utils.ts
|
|
6
|
+
function validateAddress(address, paramName = "address") {
|
|
7
|
+
if (!address) {
|
|
8
|
+
throw new Error(`${paramName} is required`);
|
|
9
|
+
}
|
|
10
|
+
if (typeof address !== "string") {
|
|
11
|
+
throw new Error(`${paramName} must be a string, got ${typeof address}`);
|
|
12
|
+
}
|
|
13
|
+
if (!address.startsWith("0x")) {
|
|
14
|
+
const preview = address.length > 10 ? `${address.substring(0, 10)}...` : address;
|
|
15
|
+
throw new Error(
|
|
16
|
+
`${paramName} must start with "0x", got: ${preview}`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (address.length !== 42) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`${paramName} must be 42 characters (0x + 40 hex), got ${address.length} characters`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
const hexPart = address.slice(2);
|
|
25
|
+
if (!/^[0-9a-fA-F]{40}$/.test(hexPart)) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`${paramName} must contain only hexadecimal characters (0-9, a-f, A-F) after "0x"`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function decodePaymentHeader(paymentHeaderBase64) {
|
|
32
|
+
try {
|
|
33
|
+
if (typeof Buffer !== "undefined") {
|
|
34
|
+
const json = Buffer.from(paymentHeaderBase64, "base64").toString("utf8");
|
|
35
|
+
return JSON.parse(json);
|
|
36
|
+
}
|
|
37
|
+
if (typeof atob === "function") {
|
|
38
|
+
const json = atob(paymentHeaderBase64);
|
|
39
|
+
return JSON.parse(json);
|
|
40
|
+
}
|
|
41
|
+
throw new Error("No base64 decoding available in this environment");
|
|
42
|
+
} catch (err) {
|
|
43
|
+
const msg = err instanceof Error ? err.message : "Unknown error";
|
|
44
|
+
throw new Error(`Failed to decode paymentHeader: ${msg}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function buildRouteKey(method, path) {
|
|
48
|
+
const normalizedMethod = (method || "GET").toUpperCase();
|
|
49
|
+
const normalizedPath = path || "/";
|
|
50
|
+
return `${normalizedMethod} ${normalizedPath}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/server/constants.ts
|
|
54
|
+
var DEFAULT_TELEMETRY_ENDPOINT = "https://x402mantlesdk.xyz/api/telemetry/settled";
|
|
55
|
+
|
|
56
|
+
// src/server/telemetry.ts
|
|
57
|
+
function createTelemetryEvent(entry, config) {
|
|
58
|
+
const assetConfig = getDefaultAssetForNetwork(entry.network);
|
|
59
|
+
return {
|
|
60
|
+
event: "payment_verified",
|
|
61
|
+
ts: entry.timestamp,
|
|
62
|
+
projectKey: config.projectKey,
|
|
63
|
+
network: entry.network,
|
|
64
|
+
buyer: entry.from,
|
|
65
|
+
payTo: entry.to,
|
|
66
|
+
amountAtomic: entry.valueAtomic,
|
|
67
|
+
asset: entry.asset,
|
|
68
|
+
decimals: assetConfig.decimals,
|
|
69
|
+
nonce: entry.id,
|
|
70
|
+
route: entry.route ?? "unknown",
|
|
71
|
+
// Facilitator metadata
|
|
72
|
+
facilitatorType: "hosted",
|
|
73
|
+
// SDK always uses hosted mode
|
|
74
|
+
facilitatorUrl: entry.facilitatorUrl,
|
|
75
|
+
// From PaymentLogEntry
|
|
76
|
+
// facilitatorAddress is undefined for SDK (not available)
|
|
77
|
+
// Optional metadata
|
|
78
|
+
txHash: entry.txHash,
|
|
79
|
+
priceUsd: entry.paymentRequirements?.price
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async function sendTelemetry(event, endpoint) {
|
|
83
|
+
const targetEndpoint = endpoint ?? DEFAULT_TELEMETRY_ENDPOINT;
|
|
84
|
+
if (!targetEndpoint) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const response = await fetch(targetEndpoint, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: {
|
|
91
|
+
"Content-Type": "application/json",
|
|
92
|
+
"Authorization": `Bearer ${event.projectKey}`
|
|
93
|
+
},
|
|
94
|
+
body: JSON.stringify(event)
|
|
95
|
+
});
|
|
96
|
+
if (!response.ok) {
|
|
97
|
+
console.warn(
|
|
98
|
+
`[x402-telemetry] Failed to send event: HTTP ${response.status}`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.error("[x402-telemetry] Error sending telemetry:", err);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/server/core/verifyPayment.ts
|
|
107
|
+
async function checkPayment(input) {
|
|
108
|
+
const {
|
|
109
|
+
paymentHeader,
|
|
110
|
+
paymentRequirements,
|
|
111
|
+
facilitatorUrl,
|
|
112
|
+
apiKey,
|
|
113
|
+
routeKey,
|
|
114
|
+
network,
|
|
115
|
+
asset,
|
|
116
|
+
telemetry,
|
|
117
|
+
onPaymentSettled
|
|
118
|
+
} = input;
|
|
119
|
+
if (!paymentHeader || paymentHeader.trim() === "") {
|
|
120
|
+
return {
|
|
121
|
+
status: "require_payment",
|
|
122
|
+
statusCode: 402,
|
|
123
|
+
responseBody: {
|
|
124
|
+
error: "Payment Required",
|
|
125
|
+
paymentRequirements,
|
|
126
|
+
paymentHeader: null
|
|
127
|
+
},
|
|
128
|
+
isValid: false
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
const verifyUrl = `${facilitatorUrl.replace(/\/+$/, "")}/verify`;
|
|
133
|
+
const verifyRes = await fetch(verifyUrl, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
headers: {
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
...apiKey && { "Authorization": `Bearer ${apiKey}` }
|
|
138
|
+
},
|
|
139
|
+
body: JSON.stringify({
|
|
140
|
+
x402Version: 1,
|
|
141
|
+
paymentHeader,
|
|
142
|
+
paymentRequirements
|
|
143
|
+
})
|
|
144
|
+
});
|
|
145
|
+
if (!verifyRes.ok) {
|
|
146
|
+
const text = await verifyRes.text().catch(() => "");
|
|
147
|
+
console.error(
|
|
148
|
+
"[x402-mantle-sdk] Facilitator /verify returned non-OK:",
|
|
149
|
+
verifyRes.status,
|
|
150
|
+
text
|
|
151
|
+
);
|
|
152
|
+
return {
|
|
153
|
+
status: "verification_error",
|
|
154
|
+
statusCode: 500,
|
|
155
|
+
responseBody: {
|
|
156
|
+
error: "Payment verification error",
|
|
157
|
+
details: `Facilitator responded with HTTP ${verifyRes.status}`
|
|
158
|
+
},
|
|
159
|
+
isValid: false
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const verifyJson = await verifyRes.json();
|
|
163
|
+
if (!verifyJson.isValid) {
|
|
164
|
+
return {
|
|
165
|
+
status: "invalid_payment",
|
|
166
|
+
statusCode: 402,
|
|
167
|
+
responseBody: {
|
|
168
|
+
error: "Payment verification failed",
|
|
169
|
+
invalidReason: verifyJson.invalidReason ?? null,
|
|
170
|
+
paymentRequirements,
|
|
171
|
+
paymentHeader: null
|
|
172
|
+
},
|
|
173
|
+
isValid: false
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (onPaymentSettled) {
|
|
177
|
+
try {
|
|
178
|
+
const headerObj = decodePaymentHeader(paymentHeader);
|
|
179
|
+
const { authorization } = headerObj.payload;
|
|
180
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
181
|
+
const logEntry = {
|
|
182
|
+
id: authorization.nonce,
|
|
183
|
+
from: authorization.from,
|
|
184
|
+
to: authorization.to,
|
|
185
|
+
valueAtomic: authorization.value,
|
|
186
|
+
network,
|
|
187
|
+
asset,
|
|
188
|
+
route: routeKey,
|
|
189
|
+
timestamp: Date.now(),
|
|
190
|
+
facilitatorUrl,
|
|
191
|
+
paymentRequirements
|
|
192
|
+
};
|
|
193
|
+
onPaymentSettled(logEntry);
|
|
194
|
+
if (telemetry) {
|
|
195
|
+
const event = createTelemetryEvent(logEntry, telemetry);
|
|
196
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
197
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
} catch (err) {
|
|
201
|
+
console.error(
|
|
202
|
+
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
203
|
+
err
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
status: "verified",
|
|
209
|
+
statusCode: 200,
|
|
210
|
+
responseBody: null,
|
|
211
|
+
isValid: true
|
|
212
|
+
};
|
|
213
|
+
} catch (err) {
|
|
214
|
+
console.error(
|
|
215
|
+
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
216
|
+
err
|
|
217
|
+
);
|
|
218
|
+
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
219
|
+
return {
|
|
220
|
+
status: "verification_error",
|
|
221
|
+
statusCode: 500,
|
|
222
|
+
responseBody: {
|
|
223
|
+
error: "Payment verification error",
|
|
224
|
+
details: message
|
|
225
|
+
},
|
|
226
|
+
isValid: false
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export {
|
|
232
|
+
validateAddress,
|
|
233
|
+
decodePaymentHeader,
|
|
234
|
+
buildRouteKey,
|
|
235
|
+
DEFAULT_TELEMETRY_ENDPOINT,
|
|
236
|
+
createTelemetryEvent,
|
|
237
|
+
sendTelemetry,
|
|
238
|
+
checkPayment
|
|
239
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-OW2BDRGZ.js";
|
|
6
|
+
import {
|
|
7
|
+
MANTLE_DEFAULTS,
|
|
8
|
+
__export,
|
|
9
|
+
getDefaultAssetForNetwork,
|
|
10
|
+
usdCentsToAtomic
|
|
11
|
+
} from "./chunk-HEZZ74SI.js";
|
|
12
|
+
|
|
13
|
+
// src/server/adapters/web-standards.ts
|
|
14
|
+
var web_standards_exports = {};
|
|
15
|
+
__export(web_standards_exports, {
|
|
16
|
+
mantlePaywall: () => mantlePaywall
|
|
17
|
+
});
|
|
18
|
+
function mantlePaywall(opts) {
|
|
19
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
20
|
+
validateAddress(payTo, "payTo");
|
|
21
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
22
|
+
return function(handler) {
|
|
23
|
+
return async (request) => {
|
|
24
|
+
const url = new URL(request.url);
|
|
25
|
+
const method = request.method;
|
|
26
|
+
const path = url.pathname;
|
|
27
|
+
const routeKey = buildRouteKey(method, path);
|
|
28
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
29
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
30
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
31
|
+
priceUsdCents,
|
|
32
|
+
assetConfig.decimals
|
|
33
|
+
);
|
|
34
|
+
const paymentRequirements = {
|
|
35
|
+
scheme: "exact",
|
|
36
|
+
network,
|
|
37
|
+
asset: assetConfig.address,
|
|
38
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
39
|
+
payTo,
|
|
40
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
41
|
+
currency: "USD"
|
|
42
|
+
};
|
|
43
|
+
const paymentHeader = request.headers.get("X-PAYMENT") || request.headers.get("x-payment") || null;
|
|
44
|
+
const result = await checkPayment({
|
|
45
|
+
paymentHeader,
|
|
46
|
+
paymentRequirements,
|
|
47
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
48
|
+
apiKey,
|
|
49
|
+
routeKey,
|
|
50
|
+
network,
|
|
51
|
+
asset: assetConfig.address,
|
|
52
|
+
telemetry,
|
|
53
|
+
onPaymentSettled
|
|
54
|
+
});
|
|
55
|
+
if (!result.isValid) {
|
|
56
|
+
return new Response(JSON.stringify(result.responseBody), {
|
|
57
|
+
status: result.statusCode,
|
|
58
|
+
headers: {
|
|
59
|
+
"Content-Type": "application/json"
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return handler(request);
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export {
|
|
69
|
+
mantlePaywall,
|
|
70
|
+
web_standards_exports
|
|
71
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { b as RoutesConfig, M as MinimalPaywallOptions } from './types-CXdNC0Ra.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Express middleware function type for Mantle paywall.
|
|
6
|
+
*/
|
|
7
|
+
type MantleMiddleware = (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
8
|
+
/** Config for createPaymentMiddleware. */
|
|
9
|
+
interface PaymentMiddlewareConfig {
|
|
10
|
+
/** Base URL of facilitator, e.g. https://facilitator.nosubs.ai */
|
|
11
|
+
facilitatorUrl: string;
|
|
12
|
+
/** Recipient address (developer). Validated at runtime. */
|
|
13
|
+
receiverAddress: string;
|
|
14
|
+
/** Map of protected routes and their pricing. */
|
|
15
|
+
routes: RoutesConfig;
|
|
16
|
+
/** Optional API key for hosted facilitator billing. */
|
|
17
|
+
apiKey?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Optional hook called whenever a payment is successfully settled.
|
|
20
|
+
*/
|
|
21
|
+
onPaymentSettled?: MinimalPaywallOptions["onPaymentSettled"];
|
|
22
|
+
/** Optional: Send usage telemetry for billing/analytics. */
|
|
23
|
+
telemetry?: MinimalPaywallOptions["telemetry"];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create Express middleware for x402 payment verification on multiple routes.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const middleware = createPaymentMiddleware({
|
|
31
|
+
* facilitatorUrl: 'https://facilitator.nosubs.ai',
|
|
32
|
+
* receiverAddress: '0x...',
|
|
33
|
+
* routes: {
|
|
34
|
+
* 'POST /api/generate': { priceUsdCents: 1, network: 'mantle-mainnet' },
|
|
35
|
+
* 'GET /api/data': { priceUsdCents: 5, network: 'mantle-mainnet' },
|
|
36
|
+
* }
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* app.use(middleware);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): MantleMiddleware;
|
|
43
|
+
/**
|
|
44
|
+
* Simplified wrapper for protecting a single route with x402 payments.
|
|
45
|
+
* Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const pay = mantlePaywall({ priceUsd: 0.01, payTo: "0x..." });
|
|
50
|
+
* app.post('/api/generate', pay, async (req, res) => {
|
|
51
|
+
* // Your handler code here
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
|
|
56
|
+
* @returns Express middleware function for single-route protection.
|
|
57
|
+
*/
|
|
58
|
+
declare function mantlePaywall(opts: MinimalPaywallOptions): MantleMiddleware;
|
|
59
|
+
|
|
60
|
+
type express_MantleMiddleware = MantleMiddleware;
|
|
61
|
+
type express_PaymentMiddlewareConfig = PaymentMiddlewareConfig;
|
|
62
|
+
declare const express_createPaymentMiddleware: typeof createPaymentMiddleware;
|
|
63
|
+
declare const express_mantlePaywall: typeof mantlePaywall;
|
|
64
|
+
declare namespace express {
|
|
65
|
+
export { type express_MantleMiddleware as MantleMiddleware, type express_PaymentMiddlewareConfig as PaymentMiddlewareConfig, express_createPaymentMiddleware as createPaymentMiddleware, express_mantlePaywall as mantlePaywall };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { type MantleMiddleware as M, type PaymentMiddlewareConfig as P, createPaymentMiddleware as c, express as e, mantlePaywall as m };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { b as RoutesConfig, M as MinimalPaywallOptions } from './types-BmK0G74m.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Express middleware function type for Mantle paywall.
|
|
6
|
+
*/
|
|
7
|
+
type MantleMiddleware = (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
8
|
+
/** Config for createPaymentMiddleware. */
|
|
9
|
+
interface PaymentMiddlewareConfig {
|
|
10
|
+
/** Base URL of facilitator, e.g. https://facilitator.nosubs.ai */
|
|
11
|
+
facilitatorUrl: string;
|
|
12
|
+
/** Recipient address (developer). Validated at runtime. */
|
|
13
|
+
receiverAddress: string;
|
|
14
|
+
/** Map of protected routes and their pricing. */
|
|
15
|
+
routes: RoutesConfig;
|
|
16
|
+
/** Optional API key for hosted facilitator billing. */
|
|
17
|
+
apiKey?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Optional hook called whenever a payment is successfully settled.
|
|
20
|
+
*/
|
|
21
|
+
onPaymentSettled?: MinimalPaywallOptions["onPaymentSettled"];
|
|
22
|
+
/** Optional: Send usage telemetry for billing/analytics. */
|
|
23
|
+
telemetry?: MinimalPaywallOptions["telemetry"];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create Express middleware for x402 payment verification on multiple routes.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const middleware = createPaymentMiddleware({
|
|
31
|
+
* facilitatorUrl: 'https://facilitator.nosubs.ai',
|
|
32
|
+
* receiverAddress: '0x...',
|
|
33
|
+
* routes: {
|
|
34
|
+
* 'POST /api/generate': { priceUsdCents: 1, network: 'mantle-mainnet' },
|
|
35
|
+
* 'GET /api/data': { priceUsdCents: 5, network: 'mantle-mainnet' },
|
|
36
|
+
* }
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* app.use(middleware);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): MantleMiddleware;
|
|
43
|
+
/**
|
|
44
|
+
* Simplified wrapper for protecting a single route with x402 payments.
|
|
45
|
+
* Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const pay = mantlePaywall({ priceUsd: 0.01, payTo: "0x..." });
|
|
50
|
+
* app.post('/api/generate', pay, async (req, res) => {
|
|
51
|
+
* // Your handler code here
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
|
|
56
|
+
* @returns Express middleware function for single-route protection.
|
|
57
|
+
*/
|
|
58
|
+
declare function mantlePaywall(opts: MinimalPaywallOptions): MantleMiddleware;
|
|
59
|
+
|
|
60
|
+
type express_MantleMiddleware = MantleMiddleware;
|
|
61
|
+
type express_PaymentMiddlewareConfig = PaymentMiddlewareConfig;
|
|
62
|
+
declare const express_createPaymentMiddleware: typeof createPaymentMiddleware;
|
|
63
|
+
declare const express_mantlePaywall: typeof mantlePaywall;
|
|
64
|
+
declare namespace express {
|
|
65
|
+
export { type express_MantleMiddleware as MantleMiddleware, type express_PaymentMiddlewareConfig as PaymentMiddlewareConfig, express_createPaymentMiddleware as createPaymentMiddleware, express_mantlePaywall as mantlePaywall };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { type MantleMiddleware as M, type PaymentMiddlewareConfig as P, createPaymentMiddleware as c, express as e, mantlePaywall as m };
|
package/dist/index.cjs
CHANGED
|
@@ -189,6 +189,7 @@ async function checkPayment(input) {
|
|
|
189
189
|
paymentHeader,
|
|
190
190
|
paymentRequirements,
|
|
191
191
|
facilitatorUrl,
|
|
192
|
+
apiKey,
|
|
192
193
|
routeKey,
|
|
193
194
|
network,
|
|
194
195
|
asset,
|
|
@@ -212,7 +213,8 @@ async function checkPayment(input) {
|
|
|
212
213
|
const verifyRes = await fetch(verifyUrl, {
|
|
213
214
|
method: "POST",
|
|
214
215
|
headers: {
|
|
215
|
-
"Content-Type": "application/json"
|
|
216
|
+
"Content-Type": "application/json",
|
|
217
|
+
...apiKey && { "Authorization": `Bearer ${apiKey}` }
|
|
216
218
|
},
|
|
217
219
|
body: JSON.stringify({
|
|
218
220
|
x402Version: 1,
|
|
@@ -308,7 +310,7 @@ async function checkPayment(input) {
|
|
|
308
310
|
|
|
309
311
|
// src/server/adapters/express.ts
|
|
310
312
|
function createPaymentMiddleware(config) {
|
|
311
|
-
const { facilitatorUrl, receiverAddress, routes, onPaymentSettled, telemetry } = config;
|
|
313
|
+
const { facilitatorUrl, receiverAddress, routes, apiKey, onPaymentSettled, telemetry } = config;
|
|
312
314
|
if (!facilitatorUrl) {
|
|
313
315
|
throw new Error("facilitatorUrl is required");
|
|
314
316
|
}
|
|
@@ -346,6 +348,7 @@ function createPaymentMiddleware(config) {
|
|
|
346
348
|
paymentHeader,
|
|
347
349
|
paymentRequirements,
|
|
348
350
|
facilitatorUrl,
|
|
351
|
+
apiKey,
|
|
349
352
|
routeKey,
|
|
350
353
|
network,
|
|
351
354
|
asset: assetConfig.address,
|
|
@@ -360,7 +363,7 @@ function createPaymentMiddleware(config) {
|
|
|
360
363
|
};
|
|
361
364
|
}
|
|
362
365
|
function mantlePaywall(opts) {
|
|
363
|
-
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
366
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
364
367
|
validateAddress(payTo, "payTo");
|
|
365
368
|
const priceUsdCents = Math.round(priceUsd * 100);
|
|
366
369
|
return async (req, res, next) => {
|
|
@@ -376,6 +379,7 @@ function mantlePaywall(opts) {
|
|
|
376
379
|
network: MANTLE_DEFAULTS.NETWORK
|
|
377
380
|
}
|
|
378
381
|
},
|
|
382
|
+
apiKey,
|
|
379
383
|
telemetry,
|
|
380
384
|
onPaymentSettled
|
|
381
385
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { A as AssetConfig, a as Authorization, E as EIP1193Provider, N as NetworkId, d as PaymentHeaderBase64, c as PaymentHeaderObject, b as PaymentHeaderPayload, P as PaymentRequirements } from './types-BFUqKBBO.cjs';
|
|
2
2
|
export { M as MANTLE_DEFAULTS } from './constants-CsIL25uQ.cjs';
|
|
3
|
-
export { M as MantleMiddleware, e as MinimalPaywallOptions, P as PaymentLogEntry, d as PaymentMiddlewareConfig, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig, c as TelemetryEvent } from './types-
|
|
4
|
-
export { c as createPaymentMiddleware, m as mantlePaywall } from './express-
|
|
3
|
+
export { M as MantleMiddleware, e as MinimalPaywallOptions, P as PaymentLogEntry, d as PaymentMiddlewareConfig, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig, c as TelemetryEvent } from './types-BWfKovFm.cjs';
|
|
4
|
+
export { c as createPaymentMiddleware, m as mantlePaywall } from './express-Pukmnwuu.cjs';
|
|
5
5
|
export { C as CallWithPaymentResult, M as MantleClient, a as MantleClientConfig, b as PaymentClient, P as PaymentClientConfig, c as createMantleClient } from './createMantleClient-CO0uWPb-.cjs';
|
|
6
6
|
export { createPaymentClient } from './client.cjs';
|
|
7
7
|
export { UseEthersWalletOptions, UseEthersWalletReturn, UseMantleX402Options, useEthersWallet, useMantleX402 } from './react.cjs';
|
|
8
8
|
import 'express';
|
|
9
|
-
import './types-
|
|
9
|
+
import './types-BmK0G74m.cjs';
|
|
10
10
|
import 'ethers';
|