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