@puga-labs/x402-mantle-sdk 0.3.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-E46A7I6B.js +274 -0
- package/dist/chunk-FHU6CFHF.js +0 -0
- package/dist/chunk-GHUWTX7J.js +66 -0
- package/dist/chunk-HEZZ74SI.js +62 -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-U73CZU3X.js +237 -0
- package/dist/chunk-WAKJRVUP.js +135 -0
- package/dist/chunk-WFUDGBKK.js +68 -0
- package/dist/chunk-WO2MYZXT.js +0 -0
- package/dist/client.d.cts +4 -4
- package/dist/client.d.ts +4 -4
- package/dist/client.js +2 -2
- package/dist/constants-0ncqvV_O.d.ts +17 -0
- package/dist/constants-CsIL25uQ.d.cts +17 -0
- package/dist/createMantleClient-CO0uWPb-.d.cts +87 -0
- package/dist/createMantleClient-CuiPsTa6.d.ts +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 +186 -109
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +6 -4
- 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 +3 -2
- package/dist/react.d.cts +7 -4
- package/dist/react.d.ts +7 -4
- package/dist/react.js +3 -3
- 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 +316 -111
- package/dist/server.d.cts +67 -104
- package/dist/server.d.ts +67 -104
- package/dist/server.js +29 -5
- 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 +49 -2
|
@@ -0,0 +1,339 @@
|
|
|
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-nextjs.ts
|
|
21
|
+
var server_nextjs_exports = {};
|
|
22
|
+
__export(server_nextjs_exports, {
|
|
23
|
+
MANTLE_DEFAULTS: () => MANTLE_DEFAULTS,
|
|
24
|
+
mantlePaywall: () => mantlePaywall
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(server_nextjs_exports);
|
|
27
|
+
|
|
28
|
+
// src/server/adapters/nextjs.ts
|
|
29
|
+
var import_server = require("next/server");
|
|
30
|
+
|
|
31
|
+
// src/server/core/utils.ts
|
|
32
|
+
function validateAddress(address, paramName = "address") {
|
|
33
|
+
if (!address) {
|
|
34
|
+
throw new Error(`${paramName} is required`);
|
|
35
|
+
}
|
|
36
|
+
if (typeof address !== "string") {
|
|
37
|
+
throw new Error(`${paramName} must be a string, got ${typeof address}`);
|
|
38
|
+
}
|
|
39
|
+
if (!address.startsWith("0x")) {
|
|
40
|
+
const preview = address.length > 10 ? `${address.substring(0, 10)}...` : address;
|
|
41
|
+
throw new Error(
|
|
42
|
+
`${paramName} must start with "0x", got: ${preview}`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (address.length !== 42) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`${paramName} must be 42 characters (0x + 40 hex), got ${address.length} characters`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const hexPart = address.slice(2);
|
|
51
|
+
if (!/^[0-9a-fA-F]{40}$/.test(hexPart)) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`${paramName} must contain only hexadecimal characters (0-9, a-f, A-F) after "0x"`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function decodePaymentHeader(paymentHeaderBase64) {
|
|
58
|
+
try {
|
|
59
|
+
if (typeof Buffer !== "undefined") {
|
|
60
|
+
const json = Buffer.from(paymentHeaderBase64, "base64").toString("utf8");
|
|
61
|
+
return JSON.parse(json);
|
|
62
|
+
}
|
|
63
|
+
if (typeof atob === "function") {
|
|
64
|
+
const json = atob(paymentHeaderBase64);
|
|
65
|
+
return JSON.parse(json);
|
|
66
|
+
}
|
|
67
|
+
throw new Error("No base64 decoding available in this environment");
|
|
68
|
+
} catch (err) {
|
|
69
|
+
const msg = err instanceof Error ? err.message : "Unknown error";
|
|
70
|
+
throw new Error(`Failed to decode paymentHeader: ${msg}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function buildRouteKey(method, path) {
|
|
74
|
+
const normalizedMethod = (method || "GET").toUpperCase();
|
|
75
|
+
const normalizedPath = path || "/";
|
|
76
|
+
return `${normalizedMethod} ${normalizedPath}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/shared/constants.ts
|
|
80
|
+
var MANTLE_MAINNET_NETWORK_ID = "mantle-mainnet";
|
|
81
|
+
var MANTLE_MAINNET_USDC = {
|
|
82
|
+
symbol: "USDC",
|
|
83
|
+
address: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
84
|
+
decimals: 6
|
|
85
|
+
};
|
|
86
|
+
var MANTLE_DEFAULTS = {
|
|
87
|
+
NETWORK: "mantle-mainnet",
|
|
88
|
+
CHAIN_ID: 5e3,
|
|
89
|
+
USDC_ADDRESS: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
90
|
+
USDC_DECIMALS: 6,
|
|
91
|
+
CURRENCY: "USD",
|
|
92
|
+
SCHEME: "exact",
|
|
93
|
+
FACILITATOR_URL: (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) || "http://localhost:8080"
|
|
94
|
+
};
|
|
95
|
+
function getDefaultAssetForNetwork(network) {
|
|
96
|
+
switch (network) {
|
|
97
|
+
case MANTLE_MAINNET_NETWORK_ID:
|
|
98
|
+
default:
|
|
99
|
+
return MANTLE_MAINNET_USDC;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function usdCentsToAtomic(cents, decimals) {
|
|
103
|
+
if (cents < 0) {
|
|
104
|
+
throw new Error("priceUsdCents must be non-negative");
|
|
105
|
+
}
|
|
106
|
+
if (decimals < 2) {
|
|
107
|
+
throw new Error("token decimals must be >= 2 for USD cent conversion");
|
|
108
|
+
}
|
|
109
|
+
const base = BigInt(10) ** BigInt(decimals - 2);
|
|
110
|
+
return BigInt(cents) * base;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/server/constants.ts
|
|
114
|
+
var DEFAULT_TELEMETRY_ENDPOINT = void 0;
|
|
115
|
+
|
|
116
|
+
// src/server/telemetry.ts
|
|
117
|
+
function createTelemetryEvent(entry, config) {
|
|
118
|
+
const assetConfig = getDefaultAssetForNetwork(entry.network);
|
|
119
|
+
return {
|
|
120
|
+
event: "payment_verified",
|
|
121
|
+
ts: entry.timestamp,
|
|
122
|
+
projectKey: config.projectKey,
|
|
123
|
+
network: entry.network,
|
|
124
|
+
buyer: entry.from,
|
|
125
|
+
payTo: entry.to,
|
|
126
|
+
amountAtomic: entry.valueAtomic,
|
|
127
|
+
asset: entry.asset,
|
|
128
|
+
decimals: assetConfig.decimals,
|
|
129
|
+
nonce: entry.id,
|
|
130
|
+
route: entry.route ?? "unknown",
|
|
131
|
+
// Facilitator metadata
|
|
132
|
+
facilitatorType: "hosted",
|
|
133
|
+
// SDK always uses hosted mode
|
|
134
|
+
facilitatorUrl: entry.facilitatorUrl,
|
|
135
|
+
// From PaymentLogEntry
|
|
136
|
+
// facilitatorAddress is undefined for SDK (not available)
|
|
137
|
+
// Optional metadata
|
|
138
|
+
txHash: entry.txHash,
|
|
139
|
+
priceUsd: entry.paymentRequirements?.price
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async function sendTelemetry(event, endpoint) {
|
|
143
|
+
const targetEndpoint = endpoint ?? DEFAULT_TELEMETRY_ENDPOINT;
|
|
144
|
+
if (!targetEndpoint) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const response = await fetch(targetEndpoint, {
|
|
149
|
+
method: "POST",
|
|
150
|
+
headers: {
|
|
151
|
+
"Content-Type": "application/json",
|
|
152
|
+
"Authorization": `Bearer ${event.projectKey}`
|
|
153
|
+
},
|
|
154
|
+
body: JSON.stringify(event)
|
|
155
|
+
});
|
|
156
|
+
if (!response.ok) {
|
|
157
|
+
console.warn(
|
|
158
|
+
`[x402-telemetry] Failed to send event: HTTP ${response.status}`
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
} catch (err) {
|
|
162
|
+
console.error("[x402-telemetry] Error sending telemetry:", err);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/server/core/verifyPayment.ts
|
|
167
|
+
async function checkPayment(input) {
|
|
168
|
+
const {
|
|
169
|
+
paymentHeader,
|
|
170
|
+
paymentRequirements,
|
|
171
|
+
facilitatorUrl,
|
|
172
|
+
routeKey,
|
|
173
|
+
network,
|
|
174
|
+
asset,
|
|
175
|
+
telemetry,
|
|
176
|
+
onPaymentSettled
|
|
177
|
+
} = input;
|
|
178
|
+
if (!paymentHeader) {
|
|
179
|
+
return {
|
|
180
|
+
status: "require_payment",
|
|
181
|
+
statusCode: 402,
|
|
182
|
+
responseBody: {
|
|
183
|
+
error: "Payment Required",
|
|
184
|
+
paymentRequirements,
|
|
185
|
+
paymentHeader: null
|
|
186
|
+
},
|
|
187
|
+
isValid: false
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
const verifyUrl = `${facilitatorUrl.replace(/\/+$/, "")}/verify`;
|
|
192
|
+
const verifyRes = await fetch(verifyUrl, {
|
|
193
|
+
method: "POST",
|
|
194
|
+
headers: {
|
|
195
|
+
"Content-Type": "application/json"
|
|
196
|
+
},
|
|
197
|
+
body: JSON.stringify({
|
|
198
|
+
x402Version: 1,
|
|
199
|
+
paymentHeader,
|
|
200
|
+
paymentRequirements
|
|
201
|
+
})
|
|
202
|
+
});
|
|
203
|
+
if (!verifyRes.ok) {
|
|
204
|
+
const text = await verifyRes.text().catch(() => "");
|
|
205
|
+
console.error(
|
|
206
|
+
"[x402-mantle-sdk] Facilitator /verify returned non-OK:",
|
|
207
|
+
verifyRes.status,
|
|
208
|
+
text
|
|
209
|
+
);
|
|
210
|
+
return {
|
|
211
|
+
status: "verification_error",
|
|
212
|
+
statusCode: 500,
|
|
213
|
+
responseBody: {
|
|
214
|
+
error: "Payment verification error",
|
|
215
|
+
details: `Facilitator responded with HTTP ${verifyRes.status}`
|
|
216
|
+
},
|
|
217
|
+
isValid: false
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const verifyJson = await verifyRes.json();
|
|
221
|
+
if (!verifyJson.isValid) {
|
|
222
|
+
return {
|
|
223
|
+
status: "invalid_payment",
|
|
224
|
+
statusCode: 402,
|
|
225
|
+
responseBody: {
|
|
226
|
+
error: "Payment verification failed",
|
|
227
|
+
invalidReason: verifyJson.invalidReason ?? null,
|
|
228
|
+
paymentRequirements,
|
|
229
|
+
paymentHeader: null
|
|
230
|
+
},
|
|
231
|
+
isValid: false
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (onPaymentSettled) {
|
|
235
|
+
try {
|
|
236
|
+
const headerObj = decodePaymentHeader(paymentHeader);
|
|
237
|
+
const { authorization } = headerObj.payload;
|
|
238
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
239
|
+
const logEntry = {
|
|
240
|
+
id: authorization.nonce,
|
|
241
|
+
from: authorization.from,
|
|
242
|
+
to: authorization.to,
|
|
243
|
+
valueAtomic: authorization.value,
|
|
244
|
+
network,
|
|
245
|
+
asset,
|
|
246
|
+
route: routeKey,
|
|
247
|
+
timestamp: Date.now(),
|
|
248
|
+
facilitatorUrl,
|
|
249
|
+
paymentRequirements
|
|
250
|
+
};
|
|
251
|
+
onPaymentSettled(logEntry);
|
|
252
|
+
if (telemetry) {
|
|
253
|
+
const event = createTelemetryEvent(logEntry, telemetry);
|
|
254
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
255
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
} catch (err) {
|
|
259
|
+
console.error(
|
|
260
|
+
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
261
|
+
err
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
status: "verified",
|
|
267
|
+
statusCode: 200,
|
|
268
|
+
responseBody: null,
|
|
269
|
+
isValid: true
|
|
270
|
+
};
|
|
271
|
+
} catch (err) {
|
|
272
|
+
console.error(
|
|
273
|
+
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
274
|
+
err
|
|
275
|
+
);
|
|
276
|
+
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
277
|
+
return {
|
|
278
|
+
status: "verification_error",
|
|
279
|
+
statusCode: 500,
|
|
280
|
+
responseBody: {
|
|
281
|
+
error: "Payment verification error",
|
|
282
|
+
details: message
|
|
283
|
+
},
|
|
284
|
+
isValid: false
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/server/adapters/nextjs.ts
|
|
290
|
+
function mantlePaywall(opts) {
|
|
291
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
292
|
+
validateAddress(payTo, "payTo");
|
|
293
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
294
|
+
return function(handler) {
|
|
295
|
+
return async (req) => {
|
|
296
|
+
const url = new URL(req.url);
|
|
297
|
+
const method = req.method;
|
|
298
|
+
const path = url.pathname;
|
|
299
|
+
const routeKey = buildRouteKey(method, path);
|
|
300
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
301
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
302
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
303
|
+
priceUsdCents,
|
|
304
|
+
assetConfig.decimals
|
|
305
|
+
);
|
|
306
|
+
const paymentRequirements = {
|
|
307
|
+
scheme: "exact",
|
|
308
|
+
network,
|
|
309
|
+
asset: assetConfig.address,
|
|
310
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
311
|
+
payTo,
|
|
312
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
313
|
+
currency: "USD"
|
|
314
|
+
};
|
|
315
|
+
const paymentHeader = req.headers.get("X-PAYMENT") || req.headers.get("x-payment") || null;
|
|
316
|
+
const result = await checkPayment({
|
|
317
|
+
paymentHeader,
|
|
318
|
+
paymentRequirements,
|
|
319
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
320
|
+
routeKey,
|
|
321
|
+
network,
|
|
322
|
+
asset: assetConfig.address,
|
|
323
|
+
telemetry,
|
|
324
|
+
onPaymentSettled
|
|
325
|
+
});
|
|
326
|
+
if (!result.isValid) {
|
|
327
|
+
return import_server.NextResponse.json(result.responseBody, {
|
|
328
|
+
status: result.statusCode
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return handler(req);
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
336
|
+
0 && (module.exports = {
|
|
337
|
+
MANTLE_DEFAULTS,
|
|
338
|
+
mantlePaywall
|
|
339
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { N as NextJSHandler, a as NextJSPaywallWrapper, m as mantlePaywall } from './nextjs-Duecps0q.cjs';
|
|
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-CrOsOHcX.cjs';
|
|
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.cjs';
|
|
4
|
+
export { M as MANTLE_DEFAULTS } from './constants-CsIL25uQ.cjs';
|
|
5
|
+
import 'next/server';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { N as NextJSHandler, a as NextJSPaywallWrapper, m as mantlePaywall } from './nextjs-TFhFiQuL.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';
|
|
5
|
+
import 'next/server';
|
|
@@ -0,0 +1,339 @@
|
|
|
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-web.ts
|
|
21
|
+
var server_web_exports = {};
|
|
22
|
+
__export(server_web_exports, {
|
|
23
|
+
MANTLE_DEFAULTS: () => MANTLE_DEFAULTS,
|
|
24
|
+
mantlePaywall: () => mantlePaywall
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(server_web_exports);
|
|
27
|
+
|
|
28
|
+
// src/server/core/utils.ts
|
|
29
|
+
function validateAddress(address, paramName = "address") {
|
|
30
|
+
if (!address) {
|
|
31
|
+
throw new Error(`${paramName} is required`);
|
|
32
|
+
}
|
|
33
|
+
if (typeof address !== "string") {
|
|
34
|
+
throw new Error(`${paramName} must be a string, got ${typeof address}`);
|
|
35
|
+
}
|
|
36
|
+
if (!address.startsWith("0x")) {
|
|
37
|
+
const preview = address.length > 10 ? `${address.substring(0, 10)}...` : address;
|
|
38
|
+
throw new Error(
|
|
39
|
+
`${paramName} must start with "0x", got: ${preview}`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
if (address.length !== 42) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`${paramName} must be 42 characters (0x + 40 hex), got ${address.length} characters`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
const hexPart = address.slice(2);
|
|
48
|
+
if (!/^[0-9a-fA-F]{40}$/.test(hexPart)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`${paramName} must contain only hexadecimal characters (0-9, a-f, A-F) after "0x"`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function decodePaymentHeader(paymentHeaderBase64) {
|
|
55
|
+
try {
|
|
56
|
+
if (typeof Buffer !== "undefined") {
|
|
57
|
+
const json = Buffer.from(paymentHeaderBase64, "base64").toString("utf8");
|
|
58
|
+
return JSON.parse(json);
|
|
59
|
+
}
|
|
60
|
+
if (typeof atob === "function") {
|
|
61
|
+
const json = atob(paymentHeaderBase64);
|
|
62
|
+
return JSON.parse(json);
|
|
63
|
+
}
|
|
64
|
+
throw new Error("No base64 decoding available in this environment");
|
|
65
|
+
} catch (err) {
|
|
66
|
+
const msg = err instanceof Error ? err.message : "Unknown error";
|
|
67
|
+
throw new Error(`Failed to decode paymentHeader: ${msg}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function buildRouteKey(method, path) {
|
|
71
|
+
const normalizedMethod = (method || "GET").toUpperCase();
|
|
72
|
+
const normalizedPath = path || "/";
|
|
73
|
+
return `${normalizedMethod} ${normalizedPath}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/shared/constants.ts
|
|
77
|
+
var MANTLE_MAINNET_NETWORK_ID = "mantle-mainnet";
|
|
78
|
+
var MANTLE_MAINNET_USDC = {
|
|
79
|
+
symbol: "USDC",
|
|
80
|
+
address: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
81
|
+
decimals: 6
|
|
82
|
+
};
|
|
83
|
+
var MANTLE_DEFAULTS = {
|
|
84
|
+
NETWORK: "mantle-mainnet",
|
|
85
|
+
CHAIN_ID: 5e3,
|
|
86
|
+
USDC_ADDRESS: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
87
|
+
USDC_DECIMALS: 6,
|
|
88
|
+
CURRENCY: "USD",
|
|
89
|
+
SCHEME: "exact",
|
|
90
|
+
FACILITATOR_URL: (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) || "http://localhost:8080"
|
|
91
|
+
};
|
|
92
|
+
function getDefaultAssetForNetwork(network) {
|
|
93
|
+
switch (network) {
|
|
94
|
+
case MANTLE_MAINNET_NETWORK_ID:
|
|
95
|
+
default:
|
|
96
|
+
return MANTLE_MAINNET_USDC;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function usdCentsToAtomic(cents, decimals) {
|
|
100
|
+
if (cents < 0) {
|
|
101
|
+
throw new Error("priceUsdCents must be non-negative");
|
|
102
|
+
}
|
|
103
|
+
if (decimals < 2) {
|
|
104
|
+
throw new Error("token decimals must be >= 2 for USD cent conversion");
|
|
105
|
+
}
|
|
106
|
+
const base = BigInt(10) ** BigInt(decimals - 2);
|
|
107
|
+
return BigInt(cents) * base;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/server/constants.ts
|
|
111
|
+
var DEFAULT_TELEMETRY_ENDPOINT = void 0;
|
|
112
|
+
|
|
113
|
+
// src/server/telemetry.ts
|
|
114
|
+
function createTelemetryEvent(entry, config) {
|
|
115
|
+
const assetConfig = getDefaultAssetForNetwork(entry.network);
|
|
116
|
+
return {
|
|
117
|
+
event: "payment_verified",
|
|
118
|
+
ts: entry.timestamp,
|
|
119
|
+
projectKey: config.projectKey,
|
|
120
|
+
network: entry.network,
|
|
121
|
+
buyer: entry.from,
|
|
122
|
+
payTo: entry.to,
|
|
123
|
+
amountAtomic: entry.valueAtomic,
|
|
124
|
+
asset: entry.asset,
|
|
125
|
+
decimals: assetConfig.decimals,
|
|
126
|
+
nonce: entry.id,
|
|
127
|
+
route: entry.route ?? "unknown",
|
|
128
|
+
// Facilitator metadata
|
|
129
|
+
facilitatorType: "hosted",
|
|
130
|
+
// SDK always uses hosted mode
|
|
131
|
+
facilitatorUrl: entry.facilitatorUrl,
|
|
132
|
+
// From PaymentLogEntry
|
|
133
|
+
// facilitatorAddress is undefined for SDK (not available)
|
|
134
|
+
// Optional metadata
|
|
135
|
+
txHash: entry.txHash,
|
|
136
|
+
priceUsd: entry.paymentRequirements?.price
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
async function sendTelemetry(event, endpoint) {
|
|
140
|
+
const targetEndpoint = endpoint ?? DEFAULT_TELEMETRY_ENDPOINT;
|
|
141
|
+
if (!targetEndpoint) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const response = await fetch(targetEndpoint, {
|
|
146
|
+
method: "POST",
|
|
147
|
+
headers: {
|
|
148
|
+
"Content-Type": "application/json",
|
|
149
|
+
"Authorization": `Bearer ${event.projectKey}`
|
|
150
|
+
},
|
|
151
|
+
body: JSON.stringify(event)
|
|
152
|
+
});
|
|
153
|
+
if (!response.ok) {
|
|
154
|
+
console.warn(
|
|
155
|
+
`[x402-telemetry] Failed to send event: HTTP ${response.status}`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
} catch (err) {
|
|
159
|
+
console.error("[x402-telemetry] Error sending telemetry:", err);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/server/core/verifyPayment.ts
|
|
164
|
+
async function checkPayment(input) {
|
|
165
|
+
const {
|
|
166
|
+
paymentHeader,
|
|
167
|
+
paymentRequirements,
|
|
168
|
+
facilitatorUrl,
|
|
169
|
+
routeKey,
|
|
170
|
+
network,
|
|
171
|
+
asset,
|
|
172
|
+
telemetry,
|
|
173
|
+
onPaymentSettled
|
|
174
|
+
} = input;
|
|
175
|
+
if (!paymentHeader) {
|
|
176
|
+
return {
|
|
177
|
+
status: "require_payment",
|
|
178
|
+
statusCode: 402,
|
|
179
|
+
responseBody: {
|
|
180
|
+
error: "Payment Required",
|
|
181
|
+
paymentRequirements,
|
|
182
|
+
paymentHeader: null
|
|
183
|
+
},
|
|
184
|
+
isValid: false
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
const verifyUrl = `${facilitatorUrl.replace(/\/+$/, "")}/verify`;
|
|
189
|
+
const verifyRes = await fetch(verifyUrl, {
|
|
190
|
+
method: "POST",
|
|
191
|
+
headers: {
|
|
192
|
+
"Content-Type": "application/json"
|
|
193
|
+
},
|
|
194
|
+
body: JSON.stringify({
|
|
195
|
+
x402Version: 1,
|
|
196
|
+
paymentHeader,
|
|
197
|
+
paymentRequirements
|
|
198
|
+
})
|
|
199
|
+
});
|
|
200
|
+
if (!verifyRes.ok) {
|
|
201
|
+
const text = await verifyRes.text().catch(() => "");
|
|
202
|
+
console.error(
|
|
203
|
+
"[x402-mantle-sdk] Facilitator /verify returned non-OK:",
|
|
204
|
+
verifyRes.status,
|
|
205
|
+
text
|
|
206
|
+
);
|
|
207
|
+
return {
|
|
208
|
+
status: "verification_error",
|
|
209
|
+
statusCode: 500,
|
|
210
|
+
responseBody: {
|
|
211
|
+
error: "Payment verification error",
|
|
212
|
+
details: `Facilitator responded with HTTP ${verifyRes.status}`
|
|
213
|
+
},
|
|
214
|
+
isValid: false
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const verifyJson = await verifyRes.json();
|
|
218
|
+
if (!verifyJson.isValid) {
|
|
219
|
+
return {
|
|
220
|
+
status: "invalid_payment",
|
|
221
|
+
statusCode: 402,
|
|
222
|
+
responseBody: {
|
|
223
|
+
error: "Payment verification failed",
|
|
224
|
+
invalidReason: verifyJson.invalidReason ?? null,
|
|
225
|
+
paymentRequirements,
|
|
226
|
+
paymentHeader: null
|
|
227
|
+
},
|
|
228
|
+
isValid: false
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (onPaymentSettled) {
|
|
232
|
+
try {
|
|
233
|
+
const headerObj = decodePaymentHeader(paymentHeader);
|
|
234
|
+
const { authorization } = headerObj.payload;
|
|
235
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
236
|
+
const logEntry = {
|
|
237
|
+
id: authorization.nonce,
|
|
238
|
+
from: authorization.from,
|
|
239
|
+
to: authorization.to,
|
|
240
|
+
valueAtomic: authorization.value,
|
|
241
|
+
network,
|
|
242
|
+
asset,
|
|
243
|
+
route: routeKey,
|
|
244
|
+
timestamp: Date.now(),
|
|
245
|
+
facilitatorUrl,
|
|
246
|
+
paymentRequirements
|
|
247
|
+
};
|
|
248
|
+
onPaymentSettled(logEntry);
|
|
249
|
+
if (telemetry) {
|
|
250
|
+
const event = createTelemetryEvent(logEntry, telemetry);
|
|
251
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
252
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
} catch (err) {
|
|
256
|
+
console.error(
|
|
257
|
+
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
258
|
+
err
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
status: "verified",
|
|
264
|
+
statusCode: 200,
|
|
265
|
+
responseBody: null,
|
|
266
|
+
isValid: true
|
|
267
|
+
};
|
|
268
|
+
} catch (err) {
|
|
269
|
+
console.error(
|
|
270
|
+
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
271
|
+
err
|
|
272
|
+
);
|
|
273
|
+
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
274
|
+
return {
|
|
275
|
+
status: "verification_error",
|
|
276
|
+
statusCode: 500,
|
|
277
|
+
responseBody: {
|
|
278
|
+
error: "Payment verification error",
|
|
279
|
+
details: message
|
|
280
|
+
},
|
|
281
|
+
isValid: false
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// src/server/adapters/web-standards.ts
|
|
287
|
+
function mantlePaywall(opts) {
|
|
288
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
289
|
+
validateAddress(payTo, "payTo");
|
|
290
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
291
|
+
return function(handler) {
|
|
292
|
+
return async (request) => {
|
|
293
|
+
const url = new URL(request.url);
|
|
294
|
+
const method = request.method;
|
|
295
|
+
const path = url.pathname;
|
|
296
|
+
const routeKey = buildRouteKey(method, path);
|
|
297
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
298
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
299
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
300
|
+
priceUsdCents,
|
|
301
|
+
assetConfig.decimals
|
|
302
|
+
);
|
|
303
|
+
const paymentRequirements = {
|
|
304
|
+
scheme: "exact",
|
|
305
|
+
network,
|
|
306
|
+
asset: assetConfig.address,
|
|
307
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
308
|
+
payTo,
|
|
309
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
310
|
+
currency: "USD"
|
|
311
|
+
};
|
|
312
|
+
const paymentHeader = request.headers.get("X-PAYMENT") || request.headers.get("x-payment") || null;
|
|
313
|
+
const result = await checkPayment({
|
|
314
|
+
paymentHeader,
|
|
315
|
+
paymentRequirements,
|
|
316
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
317
|
+
routeKey,
|
|
318
|
+
network,
|
|
319
|
+
asset: assetConfig.address,
|
|
320
|
+
telemetry,
|
|
321
|
+
onPaymentSettled
|
|
322
|
+
});
|
|
323
|
+
if (!result.isValid) {
|
|
324
|
+
return new Response(JSON.stringify(result.responseBody), {
|
|
325
|
+
status: result.statusCode,
|
|
326
|
+
headers: {
|
|
327
|
+
"Content-Type": "application/json"
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return handler(request);
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
336
|
+
0 && (module.exports = {
|
|
337
|
+
MANTLE_DEFAULTS,
|
|
338
|
+
mantlePaywall
|
|
339
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-BNQyWzBC.cjs';
|
|
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-CrOsOHcX.cjs';
|
|
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.cjs';
|
|
4
|
+
export { M as MANTLE_DEFAULTS } from './constants-CsIL25uQ.cjs';
|