@puga-labs/x402-mantle-sdk 0.2.1 → 0.3.2
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-23QNUJIB.js +136 -0
- package/dist/chunk-6GLR5EBK.js +96 -0
- package/dist/chunk-BRGW36P7.js +211 -0
- package/dist/chunk-CTI5CRDY.js +274 -0
- package/dist/chunk-DA6ZBXNO.js +275 -0
- package/dist/chunk-E46A7I6B.js +274 -0
- package/dist/chunk-FD4HG7KR.js +135 -0
- package/dist/chunk-FHU6CFHF.js +0 -0
- package/dist/chunk-GHUWTX7J.js +66 -0
- package/dist/chunk-GWVWPS3R.js +277 -0
- package/dist/chunk-HEZZ74SI.js +62 -0
- package/dist/chunk-HTZ3QFY4.js +135 -0
- package/dist/chunk-MQALBRGV.js +135 -0
- package/dist/chunk-NQWSCY44.js +70 -0
- package/dist/chunk-PCJEJYP6.js +68 -0
- package/dist/chunk-PPVS3X5Z.js +99 -0
- package/dist/chunk-PYIYE3HI.js +135 -0
- package/dist/chunk-Q6SPMEIW.js +235 -0
- package/dist/chunk-RNKXSBT7.js +135 -0
- package/dist/chunk-SPCXFN7C.js +284 -0
- package/dist/chunk-T5DRYLNB.js +135 -0
- package/dist/chunk-TSEE5NSJ.js +297 -0
- package/dist/chunk-U73CZU3X.js +237 -0
- package/dist/chunk-WAKJRVUP.js +135 -0
- package/dist/chunk-WELDWRDX.js +307 -0
- package/dist/chunk-WFUDGBKK.js +68 -0
- package/dist/chunk-WO2MYZXT.js +0 -0
- package/dist/chunk-XAQGMFSR.js +56 -0
- package/dist/client.cjs +328 -0
- package/dist/client.d.cts +17 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +12 -0
- package/dist/constants-0ncqvV_O.d.ts +17 -0
- package/dist/constants-C7aY8u5b.d.cts +77 -0
- package/dist/constants-C7aY8u5b.d.ts +77 -0
- package/dist/constants-CVFF0ray.d.ts +17 -0
- package/dist/constants-CsIL25uQ.d.cts +17 -0
- package/dist/constants-DzCGK0Q3.d.cts +17 -0
- package/dist/createMantleClient-CO0uWPb-.d.cts +87 -0
- package/dist/createMantleClient-CuiPsTa6.d.ts +87 -0
- package/dist/createMantleClient-DS1Ghqrz.d.cts +51 -0
- package/dist/createMantleClient-DS1Ghqrz.d.ts +51 -0
- package/dist/createMantleClient-DVFkbBfS.d.ts +87 -0
- package/dist/createMantleClient-NN0Nitp9.d.cts +87 -0
- package/dist/express-Ck7eryef.d.cts +66 -0
- package/dist/express-CmEg5hR3.d.ts +66 -0
- package/dist/express-D8EwEcOL.d.ts +66 -0
- package/dist/express-eQOPxfnI.d.cts +66 -0
- package/dist/index.cjs +342 -155
- package/dist/index.d.cts +10 -290
- package/dist/index.d.ts +10 -290
- package/dist/index.js +18 -567
- package/dist/nextjs-Bp8DxYDN.d.ts +45 -0
- package/dist/nextjs-CliiaTPe.d.cts +45 -0
- package/dist/nextjs-D7t-BC4N.d.ts +45 -0
- package/dist/nextjs-Duecps0q.d.cts +45 -0
- package/dist/nextjs-TFhFiQuL.d.ts +45 -0
- package/dist/nextjs-ZO2J9mdo.d.cts +45 -0
- package/dist/react.cjs +454 -0
- package/dist/react.d.cts +93 -0
- package/dist/react.d.ts +93 -0
- package/dist/react.js +10 -0
- package/dist/server-express.cjs +368 -0
- package/dist/server-express.d.cts +5 -0
- package/dist/server-express.d.ts +5 -0
- package/dist/server-express.js +13 -0
- package/dist/server-nextjs.cjs +339 -0
- package/dist/server-nextjs.d.cts +5 -0
- package/dist/server-nextjs.d.ts +5 -0
- package/dist/server-nextjs.js +11 -0
- package/dist/server-web.cjs +339 -0
- package/dist/server-web.d.cts +4 -0
- package/dist/server-web.d.ts +4 -0
- package/dist/server-web.js +11 -0
- package/dist/server.cjs +497 -0
- package/dist/server.d.cts +79 -0
- package/dist/server.d.ts +79 -0
- package/dist/server.js +36 -0
- package/dist/types-2zqbJvcz.d.cts +63 -0
- package/dist/types-2zqbJvcz.d.ts +63 -0
- package/dist/types-BFUqKBBO.d.cts +63 -0
- package/dist/types-BFUqKBBO.d.ts +63 -0
- package/dist/types-C7EPJ6Hd.d.ts +85 -0
- package/dist/types-CEttmr1U.d.cts +97 -0
- package/dist/types-CoOdbZSp.d.cts +97 -0
- package/dist/types-CqQ6OgRi.d.ts +85 -0
- package/dist/types-CrOsOHcX.d.cts +85 -0
- package/dist/types-D20K36pc.d.ts +97 -0
- package/dist/types-DTzov_EE.d.ts +97 -0
- package/dist/types-xkNm0uzE.d.cts +85 -0
- package/dist/web-standards-BNQyWzBC.d.cts +77 -0
- package/dist/web-standards-C3D2vdTg.d.ts +77 -0
- package/dist/web-standards-D8j1kZxd.d.ts +77 -0
- package/dist/web-standards-DOYp0UDT.d.cts +77 -0
- package/package.json +80 -8
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-D8j1kZxd.js';
|
|
2
|
+
export { M as MinimalPaywallOptions, c as PaymentCheckInput, d as PaymentCheckResult, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CqQ6OgRi.js';
|
|
3
|
+
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.js';
|
|
4
|
+
export { M as MANTLE_DEFAULTS } from './constants-0ncqvV_O.js';
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
DEFAULT_TELEMETRY_ENDPOINT: () => DEFAULT_TELEMETRY_ENDPOINT,
|
|
24
|
+
buildRouteKey: () => buildRouteKey,
|
|
25
|
+
checkPayment: () => checkPayment,
|
|
26
|
+
createPaymentMiddleware: () => createPaymentMiddleware,
|
|
27
|
+
createTelemetryEvent: () => createTelemetryEvent,
|
|
28
|
+
decodePaymentHeader: () => decodePaymentHeader,
|
|
29
|
+
express: () => express_exports,
|
|
30
|
+
mantlePaywall: () => mantlePaywall,
|
|
31
|
+
nextjs: () => nextjs_exports,
|
|
32
|
+
sendTelemetry: () => sendTelemetry,
|
|
33
|
+
validateAddress: () => validateAddress,
|
|
34
|
+
web: () => web_standards_exports
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(server_exports);
|
|
37
|
+
|
|
38
|
+
// src/server/core/utils.ts
|
|
39
|
+
function validateAddress(address, paramName = "address") {
|
|
40
|
+
if (!address) {
|
|
41
|
+
throw new Error(`${paramName} is required`);
|
|
42
|
+
}
|
|
43
|
+
if (typeof address !== "string") {
|
|
44
|
+
throw new Error(`${paramName} must be a string, got ${typeof address}`);
|
|
45
|
+
}
|
|
46
|
+
if (!address.startsWith("0x")) {
|
|
47
|
+
const preview = address.length > 10 ? `${address.substring(0, 10)}...` : address;
|
|
48
|
+
throw new Error(
|
|
49
|
+
`${paramName} must start with "0x", got: ${preview}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (address.length !== 42) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`${paramName} must be 42 characters (0x + 40 hex), got ${address.length} characters`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const hexPart = address.slice(2);
|
|
58
|
+
if (!/^[0-9a-fA-F]{40}$/.test(hexPart)) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`${paramName} must contain only hexadecimal characters (0-9, a-f, A-F) after "0x"`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function decodePaymentHeader(paymentHeaderBase64) {
|
|
65
|
+
try {
|
|
66
|
+
if (typeof Buffer !== "undefined") {
|
|
67
|
+
const json = Buffer.from(paymentHeaderBase64, "base64").toString("utf8");
|
|
68
|
+
return JSON.parse(json);
|
|
69
|
+
}
|
|
70
|
+
if (typeof atob === "function") {
|
|
71
|
+
const json = atob(paymentHeaderBase64);
|
|
72
|
+
return JSON.parse(json);
|
|
73
|
+
}
|
|
74
|
+
throw new Error("No base64 decoding available in this environment");
|
|
75
|
+
} catch (err) {
|
|
76
|
+
const msg = err instanceof Error ? err.message : "Unknown error";
|
|
77
|
+
throw new Error(`Failed to decode paymentHeader: ${msg}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function buildRouteKey(method, path) {
|
|
81
|
+
const normalizedMethod = (method || "GET").toUpperCase();
|
|
82
|
+
const normalizedPath = path || "/";
|
|
83
|
+
return `${normalizedMethod} ${normalizedPath}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/shared/constants.ts
|
|
87
|
+
var MANTLE_MAINNET_NETWORK_ID = "mantle-mainnet";
|
|
88
|
+
var MANTLE_MAINNET_USDC = {
|
|
89
|
+
symbol: "USDC",
|
|
90
|
+
address: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
91
|
+
decimals: 6
|
|
92
|
+
};
|
|
93
|
+
var MANTLE_DEFAULTS = {
|
|
94
|
+
NETWORK: "mantle-mainnet",
|
|
95
|
+
CHAIN_ID: 5e3,
|
|
96
|
+
USDC_ADDRESS: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
97
|
+
USDC_DECIMALS: 6,
|
|
98
|
+
CURRENCY: "USD",
|
|
99
|
+
SCHEME: "exact",
|
|
100
|
+
FACILITATOR_URL: (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) || "http://localhost:8080"
|
|
101
|
+
};
|
|
102
|
+
function getDefaultAssetForNetwork(network) {
|
|
103
|
+
switch (network) {
|
|
104
|
+
case MANTLE_MAINNET_NETWORK_ID:
|
|
105
|
+
default:
|
|
106
|
+
return MANTLE_MAINNET_USDC;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function usdCentsToAtomic(cents, decimals) {
|
|
110
|
+
if (cents < 0) {
|
|
111
|
+
throw new Error("priceUsdCents must be non-negative");
|
|
112
|
+
}
|
|
113
|
+
if (decimals < 2) {
|
|
114
|
+
throw new Error("token decimals must be >= 2 for USD cent conversion");
|
|
115
|
+
}
|
|
116
|
+
const base = BigInt(10) ** BigInt(decimals - 2);
|
|
117
|
+
return BigInt(cents) * base;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/server/constants.ts
|
|
121
|
+
var DEFAULT_TELEMETRY_ENDPOINT = void 0;
|
|
122
|
+
|
|
123
|
+
// src/server/telemetry.ts
|
|
124
|
+
function createTelemetryEvent(entry, config) {
|
|
125
|
+
const assetConfig = getDefaultAssetForNetwork(entry.network);
|
|
126
|
+
return {
|
|
127
|
+
event: "payment_verified",
|
|
128
|
+
ts: entry.timestamp,
|
|
129
|
+
projectKey: config.projectKey,
|
|
130
|
+
network: entry.network,
|
|
131
|
+
buyer: entry.from,
|
|
132
|
+
payTo: entry.to,
|
|
133
|
+
amountAtomic: entry.valueAtomic,
|
|
134
|
+
asset: entry.asset,
|
|
135
|
+
decimals: assetConfig.decimals,
|
|
136
|
+
nonce: entry.id,
|
|
137
|
+
route: entry.route ?? "unknown",
|
|
138
|
+
// Facilitator metadata
|
|
139
|
+
facilitatorType: "hosted",
|
|
140
|
+
// SDK always uses hosted mode
|
|
141
|
+
facilitatorUrl: entry.facilitatorUrl,
|
|
142
|
+
// From PaymentLogEntry
|
|
143
|
+
// facilitatorAddress is undefined for SDK (not available)
|
|
144
|
+
// Optional metadata
|
|
145
|
+
txHash: entry.txHash,
|
|
146
|
+
priceUsd: entry.paymentRequirements?.price
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function sendTelemetry(event, endpoint) {
|
|
150
|
+
const targetEndpoint = endpoint ?? DEFAULT_TELEMETRY_ENDPOINT;
|
|
151
|
+
if (!targetEndpoint) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const response = await fetch(targetEndpoint, {
|
|
156
|
+
method: "POST",
|
|
157
|
+
headers: {
|
|
158
|
+
"Content-Type": "application/json",
|
|
159
|
+
"Authorization": `Bearer ${event.projectKey}`
|
|
160
|
+
},
|
|
161
|
+
body: JSON.stringify(event)
|
|
162
|
+
});
|
|
163
|
+
if (!response.ok) {
|
|
164
|
+
console.warn(
|
|
165
|
+
`[x402-telemetry] Failed to send event: HTTP ${response.status}`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
console.error("[x402-telemetry] Error sending telemetry:", err);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/server/core/verifyPayment.ts
|
|
174
|
+
async function checkPayment(input) {
|
|
175
|
+
const {
|
|
176
|
+
paymentHeader,
|
|
177
|
+
paymentRequirements,
|
|
178
|
+
facilitatorUrl,
|
|
179
|
+
routeKey,
|
|
180
|
+
network,
|
|
181
|
+
asset,
|
|
182
|
+
telemetry,
|
|
183
|
+
onPaymentSettled
|
|
184
|
+
} = input;
|
|
185
|
+
if (!paymentHeader) {
|
|
186
|
+
return {
|
|
187
|
+
status: "require_payment",
|
|
188
|
+
statusCode: 402,
|
|
189
|
+
responseBody: {
|
|
190
|
+
error: "Payment Required",
|
|
191
|
+
paymentRequirements,
|
|
192
|
+
paymentHeader: null
|
|
193
|
+
},
|
|
194
|
+
isValid: false
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const verifyUrl = `${facilitatorUrl.replace(/\/+$/, "")}/verify`;
|
|
199
|
+
const verifyRes = await fetch(verifyUrl, {
|
|
200
|
+
method: "POST",
|
|
201
|
+
headers: {
|
|
202
|
+
"Content-Type": "application/json"
|
|
203
|
+
},
|
|
204
|
+
body: JSON.stringify({
|
|
205
|
+
x402Version: 1,
|
|
206
|
+
paymentHeader,
|
|
207
|
+
paymentRequirements
|
|
208
|
+
})
|
|
209
|
+
});
|
|
210
|
+
if (!verifyRes.ok) {
|
|
211
|
+
const text = await verifyRes.text().catch(() => "");
|
|
212
|
+
console.error(
|
|
213
|
+
"[x402-mantle-sdk] Facilitator /verify returned non-OK:",
|
|
214
|
+
verifyRes.status,
|
|
215
|
+
text
|
|
216
|
+
);
|
|
217
|
+
return {
|
|
218
|
+
status: "verification_error",
|
|
219
|
+
statusCode: 500,
|
|
220
|
+
responseBody: {
|
|
221
|
+
error: "Payment verification error",
|
|
222
|
+
details: `Facilitator responded with HTTP ${verifyRes.status}`
|
|
223
|
+
},
|
|
224
|
+
isValid: false
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
const verifyJson = await verifyRes.json();
|
|
228
|
+
if (!verifyJson.isValid) {
|
|
229
|
+
return {
|
|
230
|
+
status: "invalid_payment",
|
|
231
|
+
statusCode: 402,
|
|
232
|
+
responseBody: {
|
|
233
|
+
error: "Payment verification failed",
|
|
234
|
+
invalidReason: verifyJson.invalidReason ?? null,
|
|
235
|
+
paymentRequirements,
|
|
236
|
+
paymentHeader: null
|
|
237
|
+
},
|
|
238
|
+
isValid: false
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
if (onPaymentSettled) {
|
|
242
|
+
try {
|
|
243
|
+
const headerObj = decodePaymentHeader(paymentHeader);
|
|
244
|
+
const { authorization } = headerObj.payload;
|
|
245
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
246
|
+
const logEntry = {
|
|
247
|
+
id: authorization.nonce,
|
|
248
|
+
from: authorization.from,
|
|
249
|
+
to: authorization.to,
|
|
250
|
+
valueAtomic: authorization.value,
|
|
251
|
+
network,
|
|
252
|
+
asset,
|
|
253
|
+
route: routeKey,
|
|
254
|
+
timestamp: Date.now(),
|
|
255
|
+
facilitatorUrl,
|
|
256
|
+
paymentRequirements
|
|
257
|
+
};
|
|
258
|
+
onPaymentSettled(logEntry);
|
|
259
|
+
if (telemetry) {
|
|
260
|
+
const event = createTelemetryEvent(logEntry, telemetry);
|
|
261
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
262
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
} catch (err) {
|
|
266
|
+
console.error(
|
|
267
|
+
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
268
|
+
err
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
status: "verified",
|
|
274
|
+
statusCode: 200,
|
|
275
|
+
responseBody: null,
|
|
276
|
+
isValid: true
|
|
277
|
+
};
|
|
278
|
+
} catch (err) {
|
|
279
|
+
console.error(
|
|
280
|
+
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
281
|
+
err
|
|
282
|
+
);
|
|
283
|
+
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
284
|
+
return {
|
|
285
|
+
status: "verification_error",
|
|
286
|
+
statusCode: 500,
|
|
287
|
+
responseBody: {
|
|
288
|
+
error: "Payment verification error",
|
|
289
|
+
details: message
|
|
290
|
+
},
|
|
291
|
+
isValid: false
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/server/adapters/express.ts
|
|
297
|
+
var express_exports = {};
|
|
298
|
+
__export(express_exports, {
|
|
299
|
+
createPaymentMiddleware: () => createPaymentMiddleware,
|
|
300
|
+
mantlePaywall: () => mantlePaywall
|
|
301
|
+
});
|
|
302
|
+
function createPaymentMiddleware(config) {
|
|
303
|
+
const { facilitatorUrl, receiverAddress, routes, onPaymentSettled, telemetry } = config;
|
|
304
|
+
if (!facilitatorUrl) {
|
|
305
|
+
throw new Error("facilitatorUrl is required");
|
|
306
|
+
}
|
|
307
|
+
if (!receiverAddress) {
|
|
308
|
+
throw new Error("receiverAddress is required");
|
|
309
|
+
}
|
|
310
|
+
validateAddress(receiverAddress, "receiverAddress");
|
|
311
|
+
if (!routes || Object.keys(routes).length === 0) {
|
|
312
|
+
throw new Error("routes config must not be empty");
|
|
313
|
+
}
|
|
314
|
+
return async function paymentMiddleware(req, res, next) {
|
|
315
|
+
const routeKey = buildRouteKey(req.method, req.path);
|
|
316
|
+
const routeConfig = routes[routeKey];
|
|
317
|
+
if (!routeConfig) {
|
|
318
|
+
next();
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const { priceUsdCents, network } = routeConfig;
|
|
322
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
323
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
324
|
+
priceUsdCents,
|
|
325
|
+
assetConfig.decimals
|
|
326
|
+
);
|
|
327
|
+
const paymentRequirements = {
|
|
328
|
+
scheme: "exact",
|
|
329
|
+
network,
|
|
330
|
+
asset: assetConfig.address,
|
|
331
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
332
|
+
payTo: receiverAddress,
|
|
333
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
334
|
+
currency: "USD"
|
|
335
|
+
};
|
|
336
|
+
const paymentHeader = req.header("X-PAYMENT") || req.header("x-payment") || null;
|
|
337
|
+
const result = await checkPayment({
|
|
338
|
+
paymentHeader,
|
|
339
|
+
paymentRequirements,
|
|
340
|
+
facilitatorUrl,
|
|
341
|
+
routeKey,
|
|
342
|
+
network,
|
|
343
|
+
asset: assetConfig.address,
|
|
344
|
+
telemetry,
|
|
345
|
+
onPaymentSettled
|
|
346
|
+
});
|
|
347
|
+
if (!result.isValid) {
|
|
348
|
+
res.status(result.statusCode).json(result.responseBody);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
next();
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function mantlePaywall(opts) {
|
|
355
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
356
|
+
validateAddress(payTo, "payTo");
|
|
357
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
358
|
+
return async (req, res, next) => {
|
|
359
|
+
const method = (req.method || "GET").toUpperCase();
|
|
360
|
+
const path = req.path || "/";
|
|
361
|
+
const routeKey = `${method} ${path}`;
|
|
362
|
+
const middleware = createPaymentMiddleware({
|
|
363
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
364
|
+
receiverAddress: payTo,
|
|
365
|
+
routes: {
|
|
366
|
+
[routeKey]: {
|
|
367
|
+
priceUsdCents,
|
|
368
|
+
network: MANTLE_DEFAULTS.NETWORK
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
telemetry,
|
|
372
|
+
onPaymentSettled
|
|
373
|
+
});
|
|
374
|
+
return middleware(req, res, next);
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// src/server/adapters/nextjs.ts
|
|
379
|
+
var nextjs_exports = {};
|
|
380
|
+
__export(nextjs_exports, {
|
|
381
|
+
mantlePaywall: () => mantlePaywall2
|
|
382
|
+
});
|
|
383
|
+
var import_server = require("next/server");
|
|
384
|
+
function mantlePaywall2(opts) {
|
|
385
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
386
|
+
validateAddress(payTo, "payTo");
|
|
387
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
388
|
+
return function(handler) {
|
|
389
|
+
return async (req) => {
|
|
390
|
+
const url = new URL(req.url);
|
|
391
|
+
const method = req.method;
|
|
392
|
+
const path = url.pathname;
|
|
393
|
+
const routeKey = buildRouteKey(method, path);
|
|
394
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
395
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
396
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
397
|
+
priceUsdCents,
|
|
398
|
+
assetConfig.decimals
|
|
399
|
+
);
|
|
400
|
+
const paymentRequirements = {
|
|
401
|
+
scheme: "exact",
|
|
402
|
+
network,
|
|
403
|
+
asset: assetConfig.address,
|
|
404
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
405
|
+
payTo,
|
|
406
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
407
|
+
currency: "USD"
|
|
408
|
+
};
|
|
409
|
+
const paymentHeader = req.headers.get("X-PAYMENT") || req.headers.get("x-payment") || null;
|
|
410
|
+
const result = await checkPayment({
|
|
411
|
+
paymentHeader,
|
|
412
|
+
paymentRequirements,
|
|
413
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
414
|
+
routeKey,
|
|
415
|
+
network,
|
|
416
|
+
asset: assetConfig.address,
|
|
417
|
+
telemetry,
|
|
418
|
+
onPaymentSettled
|
|
419
|
+
});
|
|
420
|
+
if (!result.isValid) {
|
|
421
|
+
return import_server.NextResponse.json(result.responseBody, {
|
|
422
|
+
status: result.statusCode
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
return handler(req);
|
|
426
|
+
};
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// src/server/adapters/web-standards.ts
|
|
431
|
+
var web_standards_exports = {};
|
|
432
|
+
__export(web_standards_exports, {
|
|
433
|
+
mantlePaywall: () => mantlePaywall3
|
|
434
|
+
});
|
|
435
|
+
function mantlePaywall3(opts) {
|
|
436
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
437
|
+
validateAddress(payTo, "payTo");
|
|
438
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
439
|
+
return function(handler) {
|
|
440
|
+
return async (request) => {
|
|
441
|
+
const url = new URL(request.url);
|
|
442
|
+
const method = request.method;
|
|
443
|
+
const path = url.pathname;
|
|
444
|
+
const routeKey = buildRouteKey(method, path);
|
|
445
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
446
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
447
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
448
|
+
priceUsdCents,
|
|
449
|
+
assetConfig.decimals
|
|
450
|
+
);
|
|
451
|
+
const paymentRequirements = {
|
|
452
|
+
scheme: "exact",
|
|
453
|
+
network,
|
|
454
|
+
asset: assetConfig.address,
|
|
455
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
456
|
+
payTo,
|
|
457
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
458
|
+
currency: "USD"
|
|
459
|
+
};
|
|
460
|
+
const paymentHeader = request.headers.get("X-PAYMENT") || request.headers.get("x-payment") || null;
|
|
461
|
+
const result = await checkPayment({
|
|
462
|
+
paymentHeader,
|
|
463
|
+
paymentRequirements,
|
|
464
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
465
|
+
routeKey,
|
|
466
|
+
network,
|
|
467
|
+
asset: assetConfig.address,
|
|
468
|
+
telemetry,
|
|
469
|
+
onPaymentSettled
|
|
470
|
+
});
|
|
471
|
+
if (!result.isValid) {
|
|
472
|
+
return new Response(JSON.stringify(result.responseBody), {
|
|
473
|
+
status: result.statusCode,
|
|
474
|
+
headers: {
|
|
475
|
+
"Content-Type": "application/json"
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
return handler(request);
|
|
480
|
+
};
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
484
|
+
0 && (module.exports = {
|
|
485
|
+
DEFAULT_TELEMETRY_ENDPOINT,
|
|
486
|
+
buildRouteKey,
|
|
487
|
+
checkPayment,
|
|
488
|
+
createPaymentMiddleware,
|
|
489
|
+
createTelemetryEvent,
|
|
490
|
+
decodePaymentHeader,
|
|
491
|
+
express,
|
|
492
|
+
mantlePaywall,
|
|
493
|
+
nextjs,
|
|
494
|
+
sendTelemetry,
|
|
495
|
+
validateAddress,
|
|
496
|
+
web
|
|
497
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-CrOsOHcX.cjs';
|
|
2
|
+
export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CrOsOHcX.cjs';
|
|
3
|
+
import { c as PaymentHeaderObject } from './types-BFUqKBBO.cjs';
|
|
4
|
+
export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-eQOPxfnI.cjs';
|
|
5
|
+
export { n as nextjs } from './nextjs-Duecps0q.cjs';
|
|
6
|
+
export { w as web } from './web-standards-BNQyWzBC.cjs';
|
|
7
|
+
import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-CoOdbZSp.cjs';
|
|
8
|
+
import 'express';
|
|
9
|
+
import 'next/server';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Validate Ethereum address format.
|
|
13
|
+
* Throws descriptive error if invalid.
|
|
14
|
+
*
|
|
15
|
+
* @param address - Address to validate
|
|
16
|
+
* @param paramName - Parameter name for error messages (default: "address")
|
|
17
|
+
* @throws Error if address is invalid
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* validateAddress("0x1234567890123456789012345678901234567890", "payTo");
|
|
22
|
+
* // Valid - no error thrown
|
|
23
|
+
*
|
|
24
|
+
* validateAddress("invalid", "payTo");
|
|
25
|
+
* // Throws: "payTo must start with \"0x\"..."
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function validateAddress(address: string, paramName?: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* Decode base64 payment header into JSON object.
|
|
31
|
+
* Works in both Node.js and browser environments.
|
|
32
|
+
*/
|
|
33
|
+
declare function decodePaymentHeader(paymentHeaderBase64: string): PaymentHeaderObject;
|
|
34
|
+
/**
|
|
35
|
+
* Build a route key from HTTP method and path.
|
|
36
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
37
|
+
* @param path - URL path (e.g. "/api/protected")
|
|
38
|
+
* @returns Route key string (e.g. "POST /api/protected")
|
|
39
|
+
*/
|
|
40
|
+
declare function buildRouteKey(method: string, path: string): string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Check payment header and verify with facilitator.
|
|
44
|
+
* This function is framework-agnostic and can be used with any HTTP framework.
|
|
45
|
+
*
|
|
46
|
+
* @param input - Payment check configuration
|
|
47
|
+
* @returns Result indicating whether to allow access or return error response
|
|
48
|
+
*/
|
|
49
|
+
declare function checkPayment(input: PaymentCheckInput): Promise<PaymentCheckResult>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Default telemetry endpoint URL.
|
|
53
|
+
*
|
|
54
|
+
* Set to a valid URL when the official analytics backend is ready.
|
|
55
|
+
* If undefined, telemetry will only be sent if explicitly configured
|
|
56
|
+
* via TelemetryConfig.endpoint.
|
|
57
|
+
*/
|
|
58
|
+
declare const DEFAULT_TELEMETRY_ENDPOINT: string | undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Converts PaymentLogEntry to TelemetryEvent.
|
|
62
|
+
*
|
|
63
|
+
* This function transforms the internal PaymentLogEntry format
|
|
64
|
+
* to the telemetry event format expected by the analytics backend.
|
|
65
|
+
*/
|
|
66
|
+
declare function createTelemetryEvent(entry: PaymentLogEntry, config: TelemetryConfig): TelemetryEvent;
|
|
67
|
+
/**
|
|
68
|
+
* Sends telemetry event to endpoint (fire-and-forget).
|
|
69
|
+
*
|
|
70
|
+
* This function sends the telemetry event asynchronously and never throws.
|
|
71
|
+
* Errors are logged but never propagate to the caller, ensuring that
|
|
72
|
+
* telemetry failures never break the payment flow.
|
|
73
|
+
*
|
|
74
|
+
* @param event - The telemetry event to send
|
|
75
|
+
* @param endpoint - The telemetry endpoint URL (optional, falls back to DEFAULT_TELEMETRY_ENDPOINT)
|
|
76
|
+
*/
|
|
77
|
+
declare function sendTelemetry(event: TelemetryEvent, endpoint?: string): Promise<void>;
|
|
78
|
+
|
|
79
|
+
export { DEFAULT_TELEMETRY_ENDPOINT, PaymentCheckInput, PaymentCheckResult, buildRouteKey, checkPayment, createTelemetryEvent, decodePaymentHeader, sendTelemetry, validateAddress };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-CqQ6OgRi.js';
|
|
2
|
+
export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CqQ6OgRi.js';
|
|
3
|
+
import { c as PaymentHeaderObject } from './types-BFUqKBBO.js';
|
|
4
|
+
export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-D8EwEcOL.js';
|
|
5
|
+
export { n as nextjs } from './nextjs-TFhFiQuL.js';
|
|
6
|
+
export { w as web } from './web-standards-D8j1kZxd.js';
|
|
7
|
+
import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-DTzov_EE.js';
|
|
8
|
+
import 'express';
|
|
9
|
+
import 'next/server';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Validate Ethereum address format.
|
|
13
|
+
* Throws descriptive error if invalid.
|
|
14
|
+
*
|
|
15
|
+
* @param address - Address to validate
|
|
16
|
+
* @param paramName - Parameter name for error messages (default: "address")
|
|
17
|
+
* @throws Error if address is invalid
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* validateAddress("0x1234567890123456789012345678901234567890", "payTo");
|
|
22
|
+
* // Valid - no error thrown
|
|
23
|
+
*
|
|
24
|
+
* validateAddress("invalid", "payTo");
|
|
25
|
+
* // Throws: "payTo must start with \"0x\"..."
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function validateAddress(address: string, paramName?: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* Decode base64 payment header into JSON object.
|
|
31
|
+
* Works in both Node.js and browser environments.
|
|
32
|
+
*/
|
|
33
|
+
declare function decodePaymentHeader(paymentHeaderBase64: string): PaymentHeaderObject;
|
|
34
|
+
/**
|
|
35
|
+
* Build a route key from HTTP method and path.
|
|
36
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
37
|
+
* @param path - URL path (e.g. "/api/protected")
|
|
38
|
+
* @returns Route key string (e.g. "POST /api/protected")
|
|
39
|
+
*/
|
|
40
|
+
declare function buildRouteKey(method: string, path: string): string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Check payment header and verify with facilitator.
|
|
44
|
+
* This function is framework-agnostic and can be used with any HTTP framework.
|
|
45
|
+
*
|
|
46
|
+
* @param input - Payment check configuration
|
|
47
|
+
* @returns Result indicating whether to allow access or return error response
|
|
48
|
+
*/
|
|
49
|
+
declare function checkPayment(input: PaymentCheckInput): Promise<PaymentCheckResult>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Default telemetry endpoint URL.
|
|
53
|
+
*
|
|
54
|
+
* Set to a valid URL when the official analytics backend is ready.
|
|
55
|
+
* If undefined, telemetry will only be sent if explicitly configured
|
|
56
|
+
* via TelemetryConfig.endpoint.
|
|
57
|
+
*/
|
|
58
|
+
declare const DEFAULT_TELEMETRY_ENDPOINT: string | undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Converts PaymentLogEntry to TelemetryEvent.
|
|
62
|
+
*
|
|
63
|
+
* This function transforms the internal PaymentLogEntry format
|
|
64
|
+
* to the telemetry event format expected by the analytics backend.
|
|
65
|
+
*/
|
|
66
|
+
declare function createTelemetryEvent(entry: PaymentLogEntry, config: TelemetryConfig): TelemetryEvent;
|
|
67
|
+
/**
|
|
68
|
+
* Sends telemetry event to endpoint (fire-and-forget).
|
|
69
|
+
*
|
|
70
|
+
* This function sends the telemetry event asynchronously and never throws.
|
|
71
|
+
* Errors are logged but never propagate to the caller, ensuring that
|
|
72
|
+
* telemetry failures never break the payment flow.
|
|
73
|
+
*
|
|
74
|
+
* @param event - The telemetry event to send
|
|
75
|
+
* @param endpoint - The telemetry endpoint URL (optional, falls back to DEFAULT_TELEMETRY_ENDPOINT)
|
|
76
|
+
*/
|
|
77
|
+
declare function sendTelemetry(event: TelemetryEvent, endpoint?: string): Promise<void>;
|
|
78
|
+
|
|
79
|
+
export { DEFAULT_TELEMETRY_ENDPOINT, PaymentCheckInput, PaymentCheckResult, buildRouteKey, checkPayment, createTelemetryEvent, decodePaymentHeader, sendTelemetry, validateAddress };
|