@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
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { M as MantleClient } from './createMantleClient-CO0uWPb-.cjs';
|
|
2
|
+
export { a as MantleClientConfig } from './createMantleClient-CO0uWPb-.cjs';
|
|
3
|
+
import { ethers } from 'ethers';
|
|
4
|
+
import './types-BFUqKBBO.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Options for useMantleX402 React hook.
|
|
8
|
+
*/
|
|
9
|
+
interface UseMantleX402Options {
|
|
10
|
+
/** Optional facilitator URL (auto-detects from NEXT_PUBLIC_FACILITATOR_URL). */
|
|
11
|
+
facilitatorUrl?: string;
|
|
12
|
+
/** Optional resource URL (defaults to current origin). */
|
|
13
|
+
resourceUrl?: string;
|
|
14
|
+
/** Optional project key for hosted facilitator billing. */
|
|
15
|
+
projectKey?: string;
|
|
16
|
+
/** Auto-connect to wallet on mount (default: false). */
|
|
17
|
+
autoConnect?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* React hook for simplified Mantle x402 payments with ethers.js integration.
|
|
21
|
+
*
|
|
22
|
+
* Automatically manages wallet connection and provides
|
|
23
|
+
* a clean API for making paid requests.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const { postWithPayment } = useMantleX402({
|
|
28
|
+
* autoConnect: true
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* const data = await postWithPayment("/api/generate-image", {
|
|
32
|
+
* prompt: "A beautiful sunset"
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @param opts - Optional configuration (facilitatorUrl, resourceUrl, projectKey, autoConnect).
|
|
37
|
+
* @returns MantleClient instance with postWithPayment() method.
|
|
38
|
+
*/
|
|
39
|
+
declare function useMantleX402(opts?: UseMantleX402Options): MantleClient;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Options for useEthersWallet hook.
|
|
43
|
+
*/
|
|
44
|
+
interface UseEthersWalletOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Auto-connect to existing MetaMask connection on mount.
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
autoConnect?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Return type for useEthersWallet hook.
|
|
53
|
+
*/
|
|
54
|
+
interface UseEthersWalletReturn {
|
|
55
|
+
/** User's wallet address (undefined if not connected). */
|
|
56
|
+
address: string | undefined;
|
|
57
|
+
/** Whether wallet is connected. */
|
|
58
|
+
isConnected: boolean;
|
|
59
|
+
/** ethers.BrowserProvider instance (undefined if not available). */
|
|
60
|
+
provider: ethers.BrowserProvider | undefined;
|
|
61
|
+
/** Current chain ID (undefined if not connected). */
|
|
62
|
+
chainId: number | undefined;
|
|
63
|
+
/** Connect to wallet (prompts user for permission). */
|
|
64
|
+
connect: () => Promise<void>;
|
|
65
|
+
/** Disconnect from wallet (clears state). */
|
|
66
|
+
disconnect: () => void;
|
|
67
|
+
/** Error that occurred during connection or operation. */
|
|
68
|
+
error: Error | undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* React hook for managing Ethereum wallet connection using ethers.js.
|
|
72
|
+
*
|
|
73
|
+
* Works with any EIP-1193 compatible wallet provider (MetaMask, WalletConnect, etc.)
|
|
74
|
+
*
|
|
75
|
+
* Usage:
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const { address, isConnected, connect, disconnect } = useEthersWallet({
|
|
78
|
+
* autoConnect: true
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (!isConnected) {
|
|
82
|
+
* return <button onClick={connect}>Connect Wallet</button>;
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* return <div>Connected: {address}</div>;
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param options - Configuration options
|
|
89
|
+
* @returns Wallet state and methods
|
|
90
|
+
*/
|
|
91
|
+
declare function useEthersWallet(options?: UseEthersWalletOptions): UseEthersWalletReturn;
|
|
92
|
+
|
|
93
|
+
export { MantleClient, type UseEthersWalletOptions, type UseEthersWalletReturn, type UseMantleX402Options, useEthersWallet, useMantleX402 };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { M as MantleClient } from './createMantleClient-CuiPsTa6.js';
|
|
2
|
+
export { a as MantleClientConfig } from './createMantleClient-CuiPsTa6.js';
|
|
3
|
+
import { ethers } from 'ethers';
|
|
4
|
+
import './types-BFUqKBBO.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Options for useMantleX402 React hook.
|
|
8
|
+
*/
|
|
9
|
+
interface UseMantleX402Options {
|
|
10
|
+
/** Optional facilitator URL (auto-detects from NEXT_PUBLIC_FACILITATOR_URL). */
|
|
11
|
+
facilitatorUrl?: string;
|
|
12
|
+
/** Optional resource URL (defaults to current origin). */
|
|
13
|
+
resourceUrl?: string;
|
|
14
|
+
/** Optional project key for hosted facilitator billing. */
|
|
15
|
+
projectKey?: string;
|
|
16
|
+
/** Auto-connect to wallet on mount (default: false). */
|
|
17
|
+
autoConnect?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* React hook for simplified Mantle x402 payments with ethers.js integration.
|
|
21
|
+
*
|
|
22
|
+
* Automatically manages wallet connection and provides
|
|
23
|
+
* a clean API for making paid requests.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const { postWithPayment } = useMantleX402({
|
|
28
|
+
* autoConnect: true
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* const data = await postWithPayment("/api/generate-image", {
|
|
32
|
+
* prompt: "A beautiful sunset"
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @param opts - Optional configuration (facilitatorUrl, resourceUrl, projectKey, autoConnect).
|
|
37
|
+
* @returns MantleClient instance with postWithPayment() method.
|
|
38
|
+
*/
|
|
39
|
+
declare function useMantleX402(opts?: UseMantleX402Options): MantleClient;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Options for useEthersWallet hook.
|
|
43
|
+
*/
|
|
44
|
+
interface UseEthersWalletOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Auto-connect to existing MetaMask connection on mount.
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
autoConnect?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Return type for useEthersWallet hook.
|
|
53
|
+
*/
|
|
54
|
+
interface UseEthersWalletReturn {
|
|
55
|
+
/** User's wallet address (undefined if not connected). */
|
|
56
|
+
address: string | undefined;
|
|
57
|
+
/** Whether wallet is connected. */
|
|
58
|
+
isConnected: boolean;
|
|
59
|
+
/** ethers.BrowserProvider instance (undefined if not available). */
|
|
60
|
+
provider: ethers.BrowserProvider | undefined;
|
|
61
|
+
/** Current chain ID (undefined if not connected). */
|
|
62
|
+
chainId: number | undefined;
|
|
63
|
+
/** Connect to wallet (prompts user for permission). */
|
|
64
|
+
connect: () => Promise<void>;
|
|
65
|
+
/** Disconnect from wallet (clears state). */
|
|
66
|
+
disconnect: () => void;
|
|
67
|
+
/** Error that occurred during connection or operation. */
|
|
68
|
+
error: Error | undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* React hook for managing Ethereum wallet connection using ethers.js.
|
|
72
|
+
*
|
|
73
|
+
* Works with any EIP-1193 compatible wallet provider (MetaMask, WalletConnect, etc.)
|
|
74
|
+
*
|
|
75
|
+
* Usage:
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const { address, isConnected, connect, disconnect } = useEthersWallet({
|
|
78
|
+
* autoConnect: true
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (!isConnected) {
|
|
82
|
+
* return <button onClick={connect}>Connect Wallet</button>;
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* return <div>Connected: {address}</div>;
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param options - Configuration options
|
|
89
|
+
* @returns Wallet state and methods
|
|
90
|
+
*/
|
|
91
|
+
declare function useEthersWallet(options?: UseEthersWalletOptions): UseEthersWalletReturn;
|
|
92
|
+
|
|
93
|
+
export { MantleClient, type UseEthersWalletOptions, type UseEthersWalletReturn, type UseMantleX402Options, useEthersWallet, useMantleX402 };
|
package/dist/react.js
ADDED
|
@@ -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
|
+
};
|