@puga-labs/x402-mantle-sdk 0.3.8 → 0.3.10
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-3DGAB7HD.js +126 -0
- package/dist/chunk-CKBTOS7X.js +86 -0
- package/dist/chunk-IEJB5W26.js +113 -0
- package/dist/chunk-IXIFGPJ2.js +250 -0
- package/dist/chunk-JXMWK3BO.js +96 -0
- package/dist/chunk-P5FKQVHW.js +99 -0
- package/dist/chunk-T4DIHTBP.js +83 -0
- package/dist/chunk-UVYA6H32.js +293 -0
- package/dist/express-BWE0nQty.d.cts +68 -0
- package/dist/express-BvuN0Lx1.d.ts +68 -0
- package/dist/express-DqyVgO5n.d.cts +68 -0
- package/dist/express-DxxlKmmF.d.ts +68 -0
- package/dist/index.cjs +89 -10
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/nextjs-Bujo9Okf.d.cts +89 -0
- package/dist/nextjs-CzSejZe8.d.ts +89 -0
- package/dist/nextjs-DGcN_MGa.d.ts +89 -0
- package/dist/nextjs-EoISXVEo.d.cts +89 -0
- package/dist/server-express.cjs +89 -10
- 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 +90 -11
- 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 +90 -11
- 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 +141 -12
- package/dist/server.d.cts +8 -7
- package/dist/server.d.ts +8 -7
- package/dist/server.js +4 -4
- package/dist/types-B87bD2yo.d.cts +102 -0
- package/dist/types-Ba0v9XsC.d.ts +108 -0
- package/dist/types-BkGUHT4x.d.cts +108 -0
- package/dist/types-DEpSrXCf.d.ts +112 -0
- package/dist/types-DrBw0xwj.d.cts +112 -0
- package/dist/types-DvKDSdL6.d.ts +106 -0
- package/dist/types-X6DeBEgb.d.cts +106 -0
- package/dist/types-vicT7qsY.d.ts +102 -0
- package/dist/web-standards-BJcdcxD6.d.ts +77 -0
- package/dist/web-standards-C6JwCDmd.d.cts +77 -0
- package/dist/web-standards-DsCZRJPE.d.ts +77 -0
- package/dist/web-standards-QCbyQ14G.d.cts +77 -0
- package/package.json +1 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-UVYA6H32.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 debugLog(config, prefix, message, data) {
|
|
20
|
+
if (config?.debug) {
|
|
21
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
22
|
+
console.log(`[${timestamp}] [x402-debug:${prefix}]`, message, data || "");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function createPaymentMiddleware(config) {
|
|
26
|
+
const { facilitatorUrl, receiverAddress, routes, apiKey, onPaymentSettled, telemetry } = config;
|
|
27
|
+
if (!facilitatorUrl) {
|
|
28
|
+
throw new Error("facilitatorUrl is required");
|
|
29
|
+
}
|
|
30
|
+
if (!receiverAddress) {
|
|
31
|
+
throw new Error("receiverAddress is required");
|
|
32
|
+
}
|
|
33
|
+
validateAddress(receiverAddress, "receiverAddress");
|
|
34
|
+
if (!routes || Object.keys(routes).length === 0) {
|
|
35
|
+
throw new Error("routes config must not be empty");
|
|
36
|
+
}
|
|
37
|
+
return async function paymentMiddleware(req, res, next) {
|
|
38
|
+
const routeKey = buildRouteKey(req.method, req.path);
|
|
39
|
+
const routeConfig = routes[routeKey];
|
|
40
|
+
if (!routeConfig) {
|
|
41
|
+
next();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const { priceUsdCents, network } = routeConfig;
|
|
45
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
46
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
47
|
+
priceUsdCents,
|
|
48
|
+
assetConfig.decimals
|
|
49
|
+
);
|
|
50
|
+
const paymentRequirements = {
|
|
51
|
+
scheme: "exact",
|
|
52
|
+
network,
|
|
53
|
+
asset: assetConfig.address,
|
|
54
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
55
|
+
payTo: receiverAddress,
|
|
56
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
57
|
+
currency: "USD"
|
|
58
|
+
};
|
|
59
|
+
const paymentHeader = req.header("X-PAYMENT") || req.header("x-payment") || null;
|
|
60
|
+
const result = await checkPayment({
|
|
61
|
+
paymentHeader,
|
|
62
|
+
paymentRequirements,
|
|
63
|
+
facilitatorUrl,
|
|
64
|
+
apiKey,
|
|
65
|
+
routeKey,
|
|
66
|
+
network,
|
|
67
|
+
asset: assetConfig.address,
|
|
68
|
+
telemetry,
|
|
69
|
+
onPaymentSettled
|
|
70
|
+
});
|
|
71
|
+
if (!result.isValid) {
|
|
72
|
+
res.status(result.statusCode).json(result.responseBody);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
debugLog(telemetry, "handler", "\u25B6\uFE0F Handler execution started (Express middleware)");
|
|
76
|
+
if (result.sendTelemetryAfterResponse) {
|
|
77
|
+
res.on("finish", () => {
|
|
78
|
+
const statusCode = res.statusCode;
|
|
79
|
+
debugLog(telemetry, "handler", `\u2705 Handler completed: ${statusCode}`);
|
|
80
|
+
const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
|
|
81
|
+
debugLog(telemetry, "callback", `\u{1F4E4} Calling telemetry callback with status ${statusCode}`);
|
|
82
|
+
result.sendTelemetryAfterResponse(statusCode, errorMessage);
|
|
83
|
+
});
|
|
84
|
+
res.on("close", () => {
|
|
85
|
+
if (!res.writableEnded) {
|
|
86
|
+
debugLog(telemetry, "handler", "\u274C Response closed without finishing");
|
|
87
|
+
debugLog(telemetry, "callback", "\u{1F4E4} Calling telemetry callback with error");
|
|
88
|
+
result.sendTelemetryAfterResponse(500, "Response closed without finishing");
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
} else {
|
|
92
|
+
debugLog(telemetry, "callback", "\u26A0\uFE0F No telemetry callback to call");
|
|
93
|
+
}
|
|
94
|
+
next();
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function mantlePaywall(opts) {
|
|
98
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
99
|
+
validateAddress(payTo, "payTo");
|
|
100
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
101
|
+
return async (req, res, next) => {
|
|
102
|
+
const method = (req.method || "GET").toUpperCase();
|
|
103
|
+
const path = req.path || "/";
|
|
104
|
+
const routeKey = `${method} ${path}`;
|
|
105
|
+
const middleware = createPaymentMiddleware({
|
|
106
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
107
|
+
receiverAddress: payTo,
|
|
108
|
+
routes: {
|
|
109
|
+
[routeKey]: {
|
|
110
|
+
priceUsdCents,
|
|
111
|
+
network: MANTLE_DEFAULTS.NETWORK
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
apiKey,
|
|
115
|
+
telemetry,
|
|
116
|
+
onPaymentSettled
|
|
117
|
+
});
|
|
118
|
+
return middleware(req, res, next);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
createPaymentMiddleware,
|
|
124
|
+
mantlePaywall,
|
|
125
|
+
express_exports
|
|
126
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-IXIFGPJ2.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
|
+
try {
|
|
63
|
+
const response = await handler(req);
|
|
64
|
+
if (result.sendTelemetryAfterResponse) {
|
|
65
|
+
result.sendTelemetryAfterResponse(response.status);
|
|
66
|
+
}
|
|
67
|
+
return response;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
|
70
|
+
if (result.sendTelemetryAfterResponse) {
|
|
71
|
+
result.sendTelemetryAfterResponse(500, errorMessage);
|
|
72
|
+
}
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function isPaywallErrorResponse(response) {
|
|
79
|
+
return typeof response === "object" && response !== null && "error" in response && typeof response.error === "string";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
mantlePaywall,
|
|
84
|
+
isPaywallErrorResponse,
|
|
85
|
+
nextjs_exports
|
|
86
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-IXIFGPJ2.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
|
+
if (result.sendTelemetryAfterResponse) {
|
|
70
|
+
res.on("finish", () => {
|
|
71
|
+
const statusCode = res.statusCode;
|
|
72
|
+
const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
|
|
73
|
+
result.sendTelemetryAfterResponse(statusCode, errorMessage);
|
|
74
|
+
});
|
|
75
|
+
res.on("close", () => {
|
|
76
|
+
if (!res.writableEnded) {
|
|
77
|
+
result.sendTelemetryAfterResponse(500, "Response closed without finishing");
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
next();
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function mantlePaywall(opts) {
|
|
85
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
86
|
+
validateAddress(payTo, "payTo");
|
|
87
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
88
|
+
return async (req, res, next) => {
|
|
89
|
+
const method = (req.method || "GET").toUpperCase();
|
|
90
|
+
const path = req.path || "/";
|
|
91
|
+
const routeKey = `${method} ${path}`;
|
|
92
|
+
const middleware = createPaymentMiddleware({
|
|
93
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
94
|
+
receiverAddress: payTo,
|
|
95
|
+
routes: {
|
|
96
|
+
[routeKey]: {
|
|
97
|
+
priceUsdCents,
|
|
98
|
+
network: MANTLE_DEFAULTS.NETWORK
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
apiKey,
|
|
102
|
+
telemetry,
|
|
103
|
+
onPaymentSettled
|
|
104
|
+
});
|
|
105
|
+
return middleware(req, res, next);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export {
|
|
110
|
+
createPaymentMiddleware,
|
|
111
|
+
mantlePaywall,
|
|
112
|
+
express_exports
|
|
113
|
+
};
|
|
@@ -0,0 +1,250 @@
|
|
|
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
|
+
let baseLogEntry = null;
|
|
177
|
+
if (onPaymentSettled || telemetry) {
|
|
178
|
+
try {
|
|
179
|
+
const headerObj = decodePaymentHeader(paymentHeader);
|
|
180
|
+
const { authorization } = headerObj.payload;
|
|
181
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
182
|
+
baseLogEntry = {
|
|
183
|
+
id: authorization.nonce,
|
|
184
|
+
from: authorization.from,
|
|
185
|
+
to: authorization.to,
|
|
186
|
+
valueAtomic: authorization.value,
|
|
187
|
+
network,
|
|
188
|
+
asset,
|
|
189
|
+
route: routeKey,
|
|
190
|
+
timestamp: Date.now(),
|
|
191
|
+
facilitatorUrl,
|
|
192
|
+
paymentRequirements
|
|
193
|
+
};
|
|
194
|
+
if (onPaymentSettled) {
|
|
195
|
+
onPaymentSettled(baseLogEntry);
|
|
196
|
+
}
|
|
197
|
+
} catch (err) {
|
|
198
|
+
console.error(
|
|
199
|
+
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
200
|
+
err
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
|
|
205
|
+
try {
|
|
206
|
+
const event = createTelemetryEvent(baseLogEntry, telemetry);
|
|
207
|
+
event.responseStatus = responseStatus;
|
|
208
|
+
event.errorMessage = error;
|
|
209
|
+
event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
|
|
210
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
211
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
212
|
+
);
|
|
213
|
+
} catch (err) {
|
|
214
|
+
console.error("[x402-telemetry] Error creating telemetry event:", err);
|
|
215
|
+
}
|
|
216
|
+
} : void 0;
|
|
217
|
+
return {
|
|
218
|
+
status: "verified",
|
|
219
|
+
statusCode: 200,
|
|
220
|
+
responseBody: null,
|
|
221
|
+
isValid: true,
|
|
222
|
+
sendTelemetryAfterResponse
|
|
223
|
+
};
|
|
224
|
+
} catch (err) {
|
|
225
|
+
console.error(
|
|
226
|
+
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
227
|
+
err
|
|
228
|
+
);
|
|
229
|
+
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
230
|
+
return {
|
|
231
|
+
status: "verification_error",
|
|
232
|
+
statusCode: 500,
|
|
233
|
+
responseBody: {
|
|
234
|
+
error: "Payment verification error",
|
|
235
|
+
details: message
|
|
236
|
+
},
|
|
237
|
+
isValid: false
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export {
|
|
243
|
+
validateAddress,
|
|
244
|
+
decodePaymentHeader,
|
|
245
|
+
buildRouteKey,
|
|
246
|
+
DEFAULT_TELEMETRY_ENDPOINT,
|
|
247
|
+
createTelemetryEvent,
|
|
248
|
+
sendTelemetry,
|
|
249
|
+
checkPayment
|
|
250
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-UVYA6H32.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 debugLog(config, prefix, message, data) {
|
|
19
|
+
if (config?.debug) {
|
|
20
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
21
|
+
console.log(`[${timestamp}] [x402-debug:${prefix}]`, message, data || "");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function mantlePaywall(opts) {
|
|
25
|
+
const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
|
|
26
|
+
validateAddress(payTo, "payTo");
|
|
27
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
28
|
+
return function(handler) {
|
|
29
|
+
return async (request) => {
|
|
30
|
+
const url = new URL(request.url);
|
|
31
|
+
const method = request.method;
|
|
32
|
+
const path = url.pathname;
|
|
33
|
+
const routeKey = buildRouteKey(method, path);
|
|
34
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
35
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
36
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
37
|
+
priceUsdCents,
|
|
38
|
+
assetConfig.decimals
|
|
39
|
+
);
|
|
40
|
+
const paymentRequirements = {
|
|
41
|
+
scheme: "exact",
|
|
42
|
+
network,
|
|
43
|
+
asset: assetConfig.address,
|
|
44
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
45
|
+
payTo,
|
|
46
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
47
|
+
currency: "USD"
|
|
48
|
+
};
|
|
49
|
+
const paymentHeader = request.headers.get("X-PAYMENT") || request.headers.get("x-payment") || null;
|
|
50
|
+
const result = await checkPayment({
|
|
51
|
+
paymentHeader,
|
|
52
|
+
paymentRequirements,
|
|
53
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
54
|
+
apiKey,
|
|
55
|
+
routeKey,
|
|
56
|
+
network,
|
|
57
|
+
asset: assetConfig.address,
|
|
58
|
+
telemetry,
|
|
59
|
+
onPaymentSettled
|
|
60
|
+
});
|
|
61
|
+
if (!result.isValid) {
|
|
62
|
+
return new Response(JSON.stringify(result.responseBody), {
|
|
63
|
+
status: result.statusCode,
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/json"
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
debugLog(telemetry, "handler", "\u25B6\uFE0F Handler execution started");
|
|
71
|
+
const response = await handler(request);
|
|
72
|
+
debugLog(telemetry, "handler", `\u2705 Handler completed: ${response.status}`);
|
|
73
|
+
if (result.sendTelemetryAfterResponse) {
|
|
74
|
+
debugLog(telemetry, "callback", `\u{1F4E4} Calling telemetry callback with status ${response.status}`);
|
|
75
|
+
result.sendTelemetryAfterResponse(response.status);
|
|
76
|
+
} else {
|
|
77
|
+
debugLog(telemetry, "callback", "\u26A0\uFE0F No telemetry callback to call");
|
|
78
|
+
}
|
|
79
|
+
return response;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
|
82
|
+
debugLog(telemetry, "handler", `\u274C Handler error: ${errorMessage}`);
|
|
83
|
+
if (result.sendTelemetryAfterResponse) {
|
|
84
|
+
debugLog(telemetry, "callback", "\u{1F4E4} Calling telemetry callback with error");
|
|
85
|
+
result.sendTelemetryAfterResponse(500, errorMessage);
|
|
86
|
+
}
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export {
|
|
94
|
+
mantlePaywall,
|
|
95
|
+
web_standards_exports
|
|
96
|
+
};
|