@puga-labs/x402-mantle-sdk 0.3.6 → 0.3.7
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-26OQ36EN.js +179 -0
- package/dist/chunk-5TTSYEOF.js +73 -0
- package/dist/chunk-7SOCLVGM.js +301 -0
- package/dist/chunk-EKEVUVF3.js +237 -0
- package/dist/chunk-F2OTZ3BB.js +70 -0
- package/dist/chunk-MBJTUNDL.js +99 -0
- package/dist/client.cjs +20 -5
- package/dist/client.js +1 -1
- package/dist/index.cjs +21 -6
- package/dist/index.js +4 -4
- package/dist/react.cjs +20 -5
- package/dist/react.js +2 -2
- package/dist/server-express.cjs +1 -1
- package/dist/server-express.js +2 -2
- package/dist/server-nextjs.cjs +1 -1
- package/dist/server-nextjs.js +2 -2
- package/dist/server-web.cjs +1 -1
- package/dist/server-web.js +2 -2
- package/dist/server.cjs +1 -1
- package/dist/server.js +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createMantleClient
|
|
3
|
+
} from "./chunk-7SOCLVGM.js";
|
|
4
|
+
|
|
5
|
+
// src/client/react/useEthersWallet.ts
|
|
6
|
+
import { useState, useEffect, useCallback } from "react";
|
|
7
|
+
import { ethers } from "ethers";
|
|
8
|
+
function useEthersWallet(options) {
|
|
9
|
+
const [address, setAddress] = useState(void 0);
|
|
10
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
11
|
+
const [provider, setProvider] = useState(
|
|
12
|
+
void 0
|
|
13
|
+
);
|
|
14
|
+
const [chainId, setChainId] = useState(void 0);
|
|
15
|
+
const [error, setError] = useState(void 0);
|
|
16
|
+
const setProviderAndChain = useCallback(async () => {
|
|
17
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
18
|
+
const browserProvider = new ethers.BrowserProvider(
|
|
19
|
+
window.ethereum
|
|
20
|
+
);
|
|
21
|
+
setProvider(browserProvider);
|
|
22
|
+
const network = await browserProvider.getNetwork();
|
|
23
|
+
setChainId(Number(network.chainId));
|
|
24
|
+
}, []);
|
|
25
|
+
const hydrateFromPermissions = useCallback(async () => {
|
|
26
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
27
|
+
try {
|
|
28
|
+
const accounts = await window.ethereum.request({
|
|
29
|
+
method: "eth_accounts"
|
|
30
|
+
});
|
|
31
|
+
if (accounts && accounts.length > 0) {
|
|
32
|
+
const userAddress = accounts[0];
|
|
33
|
+
setAddress(userAddress);
|
|
34
|
+
setIsConnected(true);
|
|
35
|
+
await setProviderAndChain();
|
|
36
|
+
}
|
|
37
|
+
} catch (err) {
|
|
38
|
+
console.warn("[useEthersWallet] Failed to hydrate from permissions:", err);
|
|
39
|
+
}
|
|
40
|
+
}, [setProviderAndChain]);
|
|
41
|
+
const connect = useCallback(async () => {
|
|
42
|
+
try {
|
|
43
|
+
setError(void 0);
|
|
44
|
+
if (typeof window === "undefined" || !window.ethereum) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"No Ethereum wallet detected. Please install MetaMask or another wallet."
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
const browserProvider = new ethers.BrowserProvider(
|
|
50
|
+
window.ethereum
|
|
51
|
+
);
|
|
52
|
+
setProvider(browserProvider);
|
|
53
|
+
const accounts = await window.ethereum.request({
|
|
54
|
+
method: "eth_requestAccounts"
|
|
55
|
+
});
|
|
56
|
+
if (!accounts || accounts.length === 0) {
|
|
57
|
+
throw new Error("No accounts returned from wallet");
|
|
58
|
+
}
|
|
59
|
+
const userAddress = accounts[0];
|
|
60
|
+
setAddress(userAddress);
|
|
61
|
+
setIsConnected(true);
|
|
62
|
+
const network = await browserProvider.getNetwork();
|
|
63
|
+
setChainId(Number(network.chainId));
|
|
64
|
+
} catch (err) {
|
|
65
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
66
|
+
setError(errorObj);
|
|
67
|
+
setIsConnected(false);
|
|
68
|
+
setAddress(void 0);
|
|
69
|
+
setChainId(void 0);
|
|
70
|
+
throw errorObj;
|
|
71
|
+
}
|
|
72
|
+
}, []);
|
|
73
|
+
const disconnect = useCallback(() => {
|
|
74
|
+
setAddress(void 0);
|
|
75
|
+
setIsConnected(false);
|
|
76
|
+
setChainId(void 0);
|
|
77
|
+
setError(void 0);
|
|
78
|
+
}, []);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
81
|
+
const ethereum = window.ethereum;
|
|
82
|
+
const handleAccountsChanged = (accounts) => {
|
|
83
|
+
const accountsArray = accounts;
|
|
84
|
+
if (!accountsArray || accountsArray.length === 0) {
|
|
85
|
+
disconnect();
|
|
86
|
+
} else {
|
|
87
|
+
setAddress(accountsArray[0]);
|
|
88
|
+
setIsConnected(true);
|
|
89
|
+
void setProviderAndChain();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
if (ethereum.on) {
|
|
93
|
+
ethereum.on("accountsChanged", handleAccountsChanged);
|
|
94
|
+
}
|
|
95
|
+
return () => {
|
|
96
|
+
if (ethereum.removeListener) {
|
|
97
|
+
ethereum.removeListener("accountsChanged", handleAccountsChanged);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}, [disconnect, setProviderAndChain]);
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
103
|
+
const ethereum = window.ethereum;
|
|
104
|
+
const handleConnect = () => {
|
|
105
|
+
void hydrateFromPermissions();
|
|
106
|
+
};
|
|
107
|
+
if (ethereum.on) {
|
|
108
|
+
ethereum.on("connect", handleConnect);
|
|
109
|
+
}
|
|
110
|
+
return () => {
|
|
111
|
+
if (ethereum.removeListener) {
|
|
112
|
+
ethereum.removeListener("connect", handleConnect);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}, [hydrateFromPermissions]);
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
118
|
+
const ethereum = window.ethereum;
|
|
119
|
+
const handleChainChanged = (chainIdHex) => {
|
|
120
|
+
const newChainId = parseInt(chainIdHex, 16);
|
|
121
|
+
setChainId(newChainId);
|
|
122
|
+
};
|
|
123
|
+
if (ethereum.on) {
|
|
124
|
+
ethereum.on("chainChanged", handleChainChanged);
|
|
125
|
+
}
|
|
126
|
+
return () => {
|
|
127
|
+
if (ethereum.removeListener) {
|
|
128
|
+
ethereum.removeListener("chainChanged", handleChainChanged);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}, []);
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
void hydrateFromPermissions();
|
|
134
|
+
}, [hydrateFromPermissions]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
if (options?.autoConnect) {
|
|
137
|
+
connect().catch((err) => {
|
|
138
|
+
console.warn("[useEthersWallet] Auto-connect failed:", err);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}, [options?.autoConnect, connect]);
|
|
142
|
+
return {
|
|
143
|
+
address,
|
|
144
|
+
isConnected,
|
|
145
|
+
provider,
|
|
146
|
+
chainId,
|
|
147
|
+
connect,
|
|
148
|
+
disconnect,
|
|
149
|
+
error
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/client/react/useMantleX402.ts
|
|
154
|
+
function useMantleX402(opts) {
|
|
155
|
+
const { address, isConnected } = useEthersWallet({
|
|
156
|
+
autoConnect: opts?.autoConnect ?? false
|
|
157
|
+
});
|
|
158
|
+
const client = createMantleClient({
|
|
159
|
+
facilitatorUrl: opts?.facilitatorUrl,
|
|
160
|
+
resourceUrl: opts?.resourceUrl,
|
|
161
|
+
projectKey: opts?.projectKey,
|
|
162
|
+
getAccount: () => {
|
|
163
|
+
if (!isConnected || !address) return void 0;
|
|
164
|
+
return address;
|
|
165
|
+
},
|
|
166
|
+
getProvider: () => {
|
|
167
|
+
if (typeof window !== "undefined" && window.ethereum) {
|
|
168
|
+
return window.ethereum;
|
|
169
|
+
}
|
|
170
|
+
return void 0;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
return client;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export {
|
|
177
|
+
useEthersWallet,
|
|
178
|
+
useMantleX402
|
|
179
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-EKEVUVF3.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, 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
|
+
routeKey,
|
|
51
|
+
network,
|
|
52
|
+
asset: assetConfig.address,
|
|
53
|
+
telemetry,
|
|
54
|
+
onPaymentSettled
|
|
55
|
+
});
|
|
56
|
+
if (!result.isValid) {
|
|
57
|
+
return NextResponse.json(result.responseBody, {
|
|
58
|
+
status: result.statusCode
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return handler(req);
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function isPaywallErrorResponse(response) {
|
|
66
|
+
return typeof response === "object" && response !== null && "error" in response && typeof response.error === "string";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
mantlePaywall,
|
|
71
|
+
isPaywallErrorResponse,
|
|
72
|
+
nextjs_exports
|
|
73
|
+
};
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__require,
|
|
3
|
+
getChainIdForNetwork
|
|
4
|
+
} from "./chunk-HEZZ74SI.js";
|
|
5
|
+
|
|
6
|
+
// src/shared/utils.ts
|
|
7
|
+
function encodeJsonToBase64(value) {
|
|
8
|
+
const json = JSON.stringify(value);
|
|
9
|
+
if (typeof btoa === "function") {
|
|
10
|
+
const bytes = new TextEncoder().encode(json);
|
|
11
|
+
const binString = Array.from(
|
|
12
|
+
bytes,
|
|
13
|
+
(byte) => String.fromCodePoint(byte)
|
|
14
|
+
).join("");
|
|
15
|
+
return btoa(binString);
|
|
16
|
+
}
|
|
17
|
+
if (typeof globalThis.Buffer !== "undefined") {
|
|
18
|
+
return globalThis.Buffer.from(json, "utf8").toString("base64");
|
|
19
|
+
}
|
|
20
|
+
throw new Error("No base64 implementation found in this environment");
|
|
21
|
+
}
|
|
22
|
+
function randomBytes32Hex() {
|
|
23
|
+
if (typeof crypto !== "undefined" && "getRandomValues" in crypto) {
|
|
24
|
+
const arr = new Uint8Array(32);
|
|
25
|
+
crypto.getRandomValues(arr);
|
|
26
|
+
return "0x" + Array.from(arr).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const nodeCrypto = __require("crypto");
|
|
30
|
+
const buf = nodeCrypto.randomBytes(32);
|
|
31
|
+
return "0x" + buf.toString("hex");
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error(
|
|
34
|
+
"No cryptographically secure random number generator found. This environment does not support crypto.getRandomValues or Node.js crypto module."
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/client/paymentClient.ts
|
|
40
|
+
function joinUrl(base, path) {
|
|
41
|
+
const normalizedBase = base.replace(/\/+$/, "");
|
|
42
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
43
|
+
return `${normalizedBase}${normalizedPath}`;
|
|
44
|
+
}
|
|
45
|
+
function buildTypedDataForAuthorization(authorization, paymentRequirements) {
|
|
46
|
+
const chainId = getChainIdForNetwork(paymentRequirements.network);
|
|
47
|
+
const verifyingContract = paymentRequirements.asset;
|
|
48
|
+
const domain = {
|
|
49
|
+
name: "USD Coin",
|
|
50
|
+
version: "2",
|
|
51
|
+
chainId,
|
|
52
|
+
verifyingContract
|
|
53
|
+
};
|
|
54
|
+
const types = {
|
|
55
|
+
TransferWithAuthorization: [
|
|
56
|
+
{ name: "from", type: "address" },
|
|
57
|
+
{ name: "to", type: "address" },
|
|
58
|
+
{ name: "value", type: "uint256" },
|
|
59
|
+
{ name: "validAfter", type: "uint256" },
|
|
60
|
+
{ name: "validBefore", type: "uint256" },
|
|
61
|
+
{ name: "nonce", type: "bytes32" }
|
|
62
|
+
]
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
domain,
|
|
66
|
+
types,
|
|
67
|
+
primaryType: "TransferWithAuthorization",
|
|
68
|
+
message: authorization
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async function signAuthorizationWithProvider(provider, authorization, paymentRequirements) {
|
|
72
|
+
const { BrowserProvider } = await import("ethers");
|
|
73
|
+
const chainId = getChainIdForNetwork(paymentRequirements.network);
|
|
74
|
+
const browserProvider = new BrowserProvider(provider, chainId);
|
|
75
|
+
const signer = await browserProvider.getSigner();
|
|
76
|
+
const from = await signer.getAddress();
|
|
77
|
+
const authWithFrom = {
|
|
78
|
+
...authorization,
|
|
79
|
+
from
|
|
80
|
+
};
|
|
81
|
+
const typedData = buildTypedDataForAuthorization(
|
|
82
|
+
authWithFrom,
|
|
83
|
+
paymentRequirements
|
|
84
|
+
);
|
|
85
|
+
const signature = await signer.signTypedData(
|
|
86
|
+
typedData.domain,
|
|
87
|
+
typedData.types,
|
|
88
|
+
typedData.message
|
|
89
|
+
);
|
|
90
|
+
return { signature, from };
|
|
91
|
+
}
|
|
92
|
+
function createPaymentClient(config) {
|
|
93
|
+
const {
|
|
94
|
+
resourceUrl,
|
|
95
|
+
facilitatorUrl,
|
|
96
|
+
provider,
|
|
97
|
+
userAddress: userAddressOverride,
|
|
98
|
+
projectKey
|
|
99
|
+
} = config;
|
|
100
|
+
if (!resourceUrl) {
|
|
101
|
+
throw new Error("resourceUrl is required");
|
|
102
|
+
}
|
|
103
|
+
if (!facilitatorUrl) {
|
|
104
|
+
throw new Error("facilitatorUrl is required");
|
|
105
|
+
}
|
|
106
|
+
if (!provider) {
|
|
107
|
+
throw new Error("provider is required (e.g. window.ethereum)");
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
async callWithPayment(path, options) {
|
|
111
|
+
const method = options?.method ?? "GET";
|
|
112
|
+
const headers = {
|
|
113
|
+
...options?.headers ?? {}
|
|
114
|
+
};
|
|
115
|
+
let body;
|
|
116
|
+
if (options?.body !== void 0) {
|
|
117
|
+
headers["Content-Type"] = headers["Content-Type"] ?? "application/json";
|
|
118
|
+
body = JSON.stringify(options.body);
|
|
119
|
+
}
|
|
120
|
+
const initialUrl = joinUrl(resourceUrl, path);
|
|
121
|
+
const initialRes = await fetch(initialUrl, {
|
|
122
|
+
method,
|
|
123
|
+
headers,
|
|
124
|
+
body
|
|
125
|
+
});
|
|
126
|
+
if (initialRes.status !== 402) {
|
|
127
|
+
const json = await initialRes.json().catch((err) => {
|
|
128
|
+
console.error("[x402] Failed to parse initial response JSON:", err);
|
|
129
|
+
return null;
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
response: json,
|
|
133
|
+
txHash: void 0
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const bodyJson = await initialRes.json();
|
|
137
|
+
const paymentRequirements = bodyJson.paymentRequirements;
|
|
138
|
+
if (!paymentRequirements) {
|
|
139
|
+
throw new Error(
|
|
140
|
+
"402 response did not include paymentRequirements field"
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const nowSec = Math.floor(Date.now() / 1e3);
|
|
144
|
+
const validAfter = "0";
|
|
145
|
+
const validBefore = String(nowSec + 10 * 60);
|
|
146
|
+
const nonce = randomBytes32Hex();
|
|
147
|
+
const valueAtomic = options?.valueOverrideAtomic ?? paymentRequirements.maxAmountRequired;
|
|
148
|
+
let authorization = {
|
|
149
|
+
from: "0x0000000000000000000000000000000000000000",
|
|
150
|
+
to: paymentRequirements.payTo,
|
|
151
|
+
value: valueAtomic,
|
|
152
|
+
validAfter,
|
|
153
|
+
validBefore,
|
|
154
|
+
nonce
|
|
155
|
+
};
|
|
156
|
+
let signature;
|
|
157
|
+
let from;
|
|
158
|
+
try {
|
|
159
|
+
const signed = await signAuthorizationWithProvider(
|
|
160
|
+
provider,
|
|
161
|
+
authorization,
|
|
162
|
+
paymentRequirements
|
|
163
|
+
);
|
|
164
|
+
signature = signed.signature;
|
|
165
|
+
from = signed.from;
|
|
166
|
+
} catch (err) {
|
|
167
|
+
const code = err?.code;
|
|
168
|
+
if (code === 4001 || code === "ACTION_REJECTED") {
|
|
169
|
+
const userErr = new Error("User rejected payment signature");
|
|
170
|
+
userErr.code = "USER_REJECTED_SIGNATURE";
|
|
171
|
+
userErr.userRejected = true;
|
|
172
|
+
throw userErr;
|
|
173
|
+
}
|
|
174
|
+
throw err;
|
|
175
|
+
}
|
|
176
|
+
authorization = {
|
|
177
|
+
...authorization,
|
|
178
|
+
from
|
|
179
|
+
};
|
|
180
|
+
if (userAddressOverride && userAddressOverride.toLowerCase() !== from.toLowerCase()) {
|
|
181
|
+
console.warn(
|
|
182
|
+
"[SDK WARNING] userAddress override differs from signer address",
|
|
183
|
+
{ override: userAddressOverride, signer: from }
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
const paymentHeaderObject = {
|
|
187
|
+
x402Version: 1,
|
|
188
|
+
scheme: paymentRequirements.scheme,
|
|
189
|
+
network: paymentRequirements.network,
|
|
190
|
+
payload: {
|
|
191
|
+
signature,
|
|
192
|
+
authorization
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const paymentHeader = encodeJsonToBase64(paymentHeaderObject);
|
|
196
|
+
const settleUrl = joinUrl(facilitatorUrl, "/settle");
|
|
197
|
+
const settleRes = await fetch(settleUrl, {
|
|
198
|
+
method: "POST",
|
|
199
|
+
headers: {
|
|
200
|
+
"Content-Type": "application/json",
|
|
201
|
+
...projectKey ? { "X-Project-Key": projectKey } : {}
|
|
202
|
+
},
|
|
203
|
+
body: JSON.stringify({
|
|
204
|
+
x402Version: 1,
|
|
205
|
+
paymentHeader,
|
|
206
|
+
paymentRequirements
|
|
207
|
+
})
|
|
208
|
+
});
|
|
209
|
+
if (!settleRes.ok) {
|
|
210
|
+
const text = await settleRes.text().catch((err) => {
|
|
211
|
+
console.error("[x402] Failed to read settle response text:", err);
|
|
212
|
+
return "";
|
|
213
|
+
});
|
|
214
|
+
throw new Error(
|
|
215
|
+
`Facilitator /settle failed with HTTP ${settleRes.status}: ${text}`
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const settleJson = await settleRes.json();
|
|
219
|
+
if (!settleJson.success) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`Facilitator /settle returned error: ${settleJson.error ?? "unknown error"}`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const txHash = settleJson.txHash ?? void 0;
|
|
225
|
+
const retryHeaders = {
|
|
226
|
+
...headers,
|
|
227
|
+
"X-PAYMENT": paymentHeader
|
|
228
|
+
};
|
|
229
|
+
const retryRes = await fetch(initialUrl, {
|
|
230
|
+
method,
|
|
231
|
+
headers: retryHeaders,
|
|
232
|
+
body
|
|
233
|
+
});
|
|
234
|
+
if (!retryRes.ok) {
|
|
235
|
+
const text = await retryRes.text().catch((err) => {
|
|
236
|
+
console.error("[x402] Failed to read retry response text:", err);
|
|
237
|
+
return "";
|
|
238
|
+
});
|
|
239
|
+
throw new Error(
|
|
240
|
+
`Protected request with X-PAYMENT failed: HTTP ${retryRes.status} ${text}`
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const finalJson = await retryRes.json();
|
|
244
|
+
return {
|
|
245
|
+
response: finalJson,
|
|
246
|
+
txHash,
|
|
247
|
+
paymentHeader,
|
|
248
|
+
paymentRequirements
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// src/client/createMantleClient.ts
|
|
255
|
+
function createMantleClient(config) {
|
|
256
|
+
const resourceUrl = config?.resourceUrl ?? (typeof window !== "undefined" ? window.location.origin : "");
|
|
257
|
+
const facilitatorUrl = config?.facilitatorUrl ?? (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) ?? "http://localhost:8080";
|
|
258
|
+
return {
|
|
259
|
+
async postWithPayment(url, body) {
|
|
260
|
+
const provider = config?.getProvider?.();
|
|
261
|
+
if (!provider) {
|
|
262
|
+
throw new Error("Wallet provider not available");
|
|
263
|
+
}
|
|
264
|
+
let account = await config?.getAccount?.();
|
|
265
|
+
if (!account && provider?.request) {
|
|
266
|
+
try {
|
|
267
|
+
const accounts = await provider.request({
|
|
268
|
+
method: "eth_accounts"
|
|
269
|
+
});
|
|
270
|
+
if (Array.isArray(accounts) && accounts.length > 0) {
|
|
271
|
+
account = accounts[0];
|
|
272
|
+
}
|
|
273
|
+
} catch (err) {
|
|
274
|
+
console.warn("[x402] Failed to hydrate account from provider:", err);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!account) {
|
|
278
|
+
throw new Error(
|
|
279
|
+
"Wallet not connected. Please connect your wallet first."
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const client = createPaymentClient({
|
|
283
|
+
resourceUrl,
|
|
284
|
+
facilitatorUrl,
|
|
285
|
+
provider,
|
|
286
|
+
userAddress: account,
|
|
287
|
+
projectKey: config?.projectKey
|
|
288
|
+
});
|
|
289
|
+
const result = await client.callWithPayment(url, {
|
|
290
|
+
method: "POST",
|
|
291
|
+
body
|
|
292
|
+
});
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export {
|
|
299
|
+
createPaymentClient,
|
|
300
|
+
createMantleClient
|
|
301
|
+
};
|
|
@@ -0,0 +1,237 @@
|
|
|
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
|
+
routeKey,
|
|
113
|
+
network,
|
|
114
|
+
asset,
|
|
115
|
+
telemetry,
|
|
116
|
+
onPaymentSettled
|
|
117
|
+
} = input;
|
|
118
|
+
if (!paymentHeader || paymentHeader.trim() === "") {
|
|
119
|
+
return {
|
|
120
|
+
status: "require_payment",
|
|
121
|
+
statusCode: 402,
|
|
122
|
+
responseBody: {
|
|
123
|
+
error: "Payment Required",
|
|
124
|
+
paymentRequirements,
|
|
125
|
+
paymentHeader: null
|
|
126
|
+
},
|
|
127
|
+
isValid: false
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const verifyUrl = `${facilitatorUrl.replace(/\/+$/, "")}/verify`;
|
|
132
|
+
const verifyRes = await fetch(verifyUrl, {
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers: {
|
|
135
|
+
"Content-Type": "application/json"
|
|
136
|
+
},
|
|
137
|
+
body: JSON.stringify({
|
|
138
|
+
x402Version: 1,
|
|
139
|
+
paymentHeader,
|
|
140
|
+
paymentRequirements
|
|
141
|
+
})
|
|
142
|
+
});
|
|
143
|
+
if (!verifyRes.ok) {
|
|
144
|
+
const text = await verifyRes.text().catch(() => "");
|
|
145
|
+
console.error(
|
|
146
|
+
"[x402-mantle-sdk] Facilitator /verify returned non-OK:",
|
|
147
|
+
verifyRes.status,
|
|
148
|
+
text
|
|
149
|
+
);
|
|
150
|
+
return {
|
|
151
|
+
status: "verification_error",
|
|
152
|
+
statusCode: 500,
|
|
153
|
+
responseBody: {
|
|
154
|
+
error: "Payment verification error",
|
|
155
|
+
details: `Facilitator responded with HTTP ${verifyRes.status}`
|
|
156
|
+
},
|
|
157
|
+
isValid: false
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const verifyJson = await verifyRes.json();
|
|
161
|
+
if (!verifyJson.isValid) {
|
|
162
|
+
return {
|
|
163
|
+
status: "invalid_payment",
|
|
164
|
+
statusCode: 402,
|
|
165
|
+
responseBody: {
|
|
166
|
+
error: "Payment verification failed",
|
|
167
|
+
invalidReason: verifyJson.invalidReason ?? null,
|
|
168
|
+
paymentRequirements,
|
|
169
|
+
paymentHeader: null
|
|
170
|
+
},
|
|
171
|
+
isValid: false
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (onPaymentSettled) {
|
|
175
|
+
try {
|
|
176
|
+
const headerObj = decodePaymentHeader(paymentHeader);
|
|
177
|
+
const { authorization } = headerObj.payload;
|
|
178
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
179
|
+
const logEntry = {
|
|
180
|
+
id: authorization.nonce,
|
|
181
|
+
from: authorization.from,
|
|
182
|
+
to: authorization.to,
|
|
183
|
+
valueAtomic: authorization.value,
|
|
184
|
+
network,
|
|
185
|
+
asset,
|
|
186
|
+
route: routeKey,
|
|
187
|
+
timestamp: Date.now(),
|
|
188
|
+
facilitatorUrl,
|
|
189
|
+
paymentRequirements
|
|
190
|
+
};
|
|
191
|
+
onPaymentSettled(logEntry);
|
|
192
|
+
if (telemetry) {
|
|
193
|
+
const event = createTelemetryEvent(logEntry, telemetry);
|
|
194
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
195
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
} catch (err) {
|
|
199
|
+
console.error(
|
|
200
|
+
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
201
|
+
err
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
status: "verified",
|
|
207
|
+
statusCode: 200,
|
|
208
|
+
responseBody: null,
|
|
209
|
+
isValid: true
|
|
210
|
+
};
|
|
211
|
+
} catch (err) {
|
|
212
|
+
console.error(
|
|
213
|
+
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
214
|
+
err
|
|
215
|
+
);
|
|
216
|
+
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
217
|
+
return {
|
|
218
|
+
status: "verification_error",
|
|
219
|
+
statusCode: 500,
|
|
220
|
+
responseBody: {
|
|
221
|
+
error: "Payment verification error",
|
|
222
|
+
details: message
|
|
223
|
+
},
|
|
224
|
+
isValid: false
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export {
|
|
230
|
+
validateAddress,
|
|
231
|
+
decodePaymentHeader,
|
|
232
|
+
buildRouteKey,
|
|
233
|
+
DEFAULT_TELEMETRY_ENDPOINT,
|
|
234
|
+
createTelemetryEvent,
|
|
235
|
+
sendTelemetry,
|
|
236
|
+
checkPayment
|
|
237
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-EKEVUVF3.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 mantlePaywall(opts) {
|
|
19
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
20
|
+
validateAddress(payTo, "payTo");
|
|
21
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
22
|
+
return function(handler) {
|
|
23
|
+
return async (request) => {
|
|
24
|
+
const url = new URL(request.url);
|
|
25
|
+
const method = request.method;
|
|
26
|
+
const path = url.pathname;
|
|
27
|
+
const routeKey = buildRouteKey(method, path);
|
|
28
|
+
const network = MANTLE_DEFAULTS.NETWORK;
|
|
29
|
+
const assetConfig = getDefaultAssetForNetwork(network);
|
|
30
|
+
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
31
|
+
priceUsdCents,
|
|
32
|
+
assetConfig.decimals
|
|
33
|
+
);
|
|
34
|
+
const paymentRequirements = {
|
|
35
|
+
scheme: "exact",
|
|
36
|
+
network,
|
|
37
|
+
asset: assetConfig.address,
|
|
38
|
+
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
39
|
+
payTo,
|
|
40
|
+
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
41
|
+
currency: "USD"
|
|
42
|
+
};
|
|
43
|
+
const paymentHeader = request.headers.get("X-PAYMENT") || request.headers.get("x-payment") || null;
|
|
44
|
+
const result = await checkPayment({
|
|
45
|
+
paymentHeader,
|
|
46
|
+
paymentRequirements,
|
|
47
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
48
|
+
routeKey,
|
|
49
|
+
network,
|
|
50
|
+
asset: assetConfig.address,
|
|
51
|
+
telemetry,
|
|
52
|
+
onPaymentSettled
|
|
53
|
+
});
|
|
54
|
+
if (!result.isValid) {
|
|
55
|
+
return new Response(JSON.stringify(result.responseBody), {
|
|
56
|
+
status: result.statusCode,
|
|
57
|
+
headers: {
|
|
58
|
+
"Content-Type": "application/json"
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return handler(request);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
mantlePaywall,
|
|
69
|
+
web_standards_exports
|
|
70
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildRouteKey,
|
|
3
|
+
checkPayment,
|
|
4
|
+
validateAddress
|
|
5
|
+
} from "./chunk-EKEVUVF3.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, 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
|
+
routeKey,
|
|
59
|
+
network,
|
|
60
|
+
asset: assetConfig.address,
|
|
61
|
+
telemetry,
|
|
62
|
+
onPaymentSettled
|
|
63
|
+
});
|
|
64
|
+
if (!result.isValid) {
|
|
65
|
+
res.status(result.statusCode).json(result.responseBody);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
next();
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function mantlePaywall(opts) {
|
|
72
|
+
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
73
|
+
validateAddress(payTo, "payTo");
|
|
74
|
+
const priceUsdCents = Math.round(priceUsd * 100);
|
|
75
|
+
return async (req, res, next) => {
|
|
76
|
+
const method = (req.method || "GET").toUpperCase();
|
|
77
|
+
const path = req.path || "/";
|
|
78
|
+
const routeKey = `${method} ${path}`;
|
|
79
|
+
const middleware = createPaymentMiddleware({
|
|
80
|
+
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
81
|
+
receiverAddress: payTo,
|
|
82
|
+
routes: {
|
|
83
|
+
[routeKey]: {
|
|
84
|
+
priceUsdCents,
|
|
85
|
+
network: MANTLE_DEFAULTS.NETWORK
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
telemetry,
|
|
89
|
+
onPaymentSettled
|
|
90
|
+
});
|
|
91
|
+
return middleware(req, res, next);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export {
|
|
96
|
+
createPaymentMiddleware,
|
|
97
|
+
mantlePaywall,
|
|
98
|
+
express_exports
|
|
99
|
+
};
|
package/dist/client.cjs
CHANGED
|
@@ -206,11 +206,26 @@ function createPaymentClient(config) {
|
|
|
206
206
|
validBefore,
|
|
207
207
|
nonce
|
|
208
208
|
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
209
|
+
let signature;
|
|
210
|
+
let from;
|
|
211
|
+
try {
|
|
212
|
+
const signed = await signAuthorizationWithProvider(
|
|
213
|
+
provider,
|
|
214
|
+
authorization,
|
|
215
|
+
paymentRequirements
|
|
216
|
+
);
|
|
217
|
+
signature = signed.signature;
|
|
218
|
+
from = signed.from;
|
|
219
|
+
} catch (err) {
|
|
220
|
+
const code = err?.code;
|
|
221
|
+
if (code === 4001 || code === "ACTION_REJECTED") {
|
|
222
|
+
const userErr = new Error("User rejected payment signature");
|
|
223
|
+
userErr.code = "USER_REJECTED_SIGNATURE";
|
|
224
|
+
userErr.userRejected = true;
|
|
225
|
+
throw userErr;
|
|
226
|
+
}
|
|
227
|
+
throw err;
|
|
228
|
+
}
|
|
214
229
|
authorization = {
|
|
215
230
|
...authorization,
|
|
216
231
|
from
|
package/dist/client.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -131,7 +131,7 @@ function buildRouteKey(method, path) {
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
// src/server/constants.ts
|
|
134
|
-
var DEFAULT_TELEMETRY_ENDPOINT =
|
|
134
|
+
var DEFAULT_TELEMETRY_ENDPOINT = "https://x402mantlesdk.xyz/api/telemetry/settled";
|
|
135
135
|
|
|
136
136
|
// src/server/telemetry.ts
|
|
137
137
|
function createTelemetryEvent(entry, config) {
|
|
@@ -533,11 +533,26 @@ function createPaymentClient(config) {
|
|
|
533
533
|
validBefore,
|
|
534
534
|
nonce
|
|
535
535
|
};
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
536
|
+
let signature;
|
|
537
|
+
let from;
|
|
538
|
+
try {
|
|
539
|
+
const signed = await signAuthorizationWithProvider(
|
|
540
|
+
provider,
|
|
541
|
+
authorization,
|
|
542
|
+
paymentRequirements
|
|
543
|
+
);
|
|
544
|
+
signature = signed.signature;
|
|
545
|
+
from = signed.from;
|
|
546
|
+
} catch (err) {
|
|
547
|
+
const code = err?.code;
|
|
548
|
+
if (code === 4001 || code === "ACTION_REJECTED") {
|
|
549
|
+
const userErr = new Error("User rejected payment signature");
|
|
550
|
+
userErr.code = "USER_REJECTED_SIGNATURE";
|
|
551
|
+
userErr.userRejected = true;
|
|
552
|
+
throw userErr;
|
|
553
|
+
}
|
|
554
|
+
throw err;
|
|
555
|
+
}
|
|
541
556
|
authorization = {
|
|
542
557
|
...authorization,
|
|
543
558
|
from
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useEthersWallet,
|
|
3
3
|
useMantleX402
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-26OQ36EN.js";
|
|
5
5
|
import {
|
|
6
6
|
createMantleClient,
|
|
7
7
|
createPaymentClient
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-7SOCLVGM.js";
|
|
9
9
|
import "./chunk-WO2MYZXT.js";
|
|
10
10
|
import {
|
|
11
11
|
createPaymentMiddleware,
|
|
12
12
|
mantlePaywall
|
|
13
|
-
} from "./chunk-
|
|
14
|
-
import "./chunk-
|
|
13
|
+
} from "./chunk-MBJTUNDL.js";
|
|
14
|
+
import "./chunk-EKEVUVF3.js";
|
|
15
15
|
import {
|
|
16
16
|
MANTLE_DEFAULTS
|
|
17
17
|
} from "./chunk-HEZZ74SI.js";
|
package/dist/react.cjs
CHANGED
|
@@ -353,11 +353,26 @@ function createPaymentClient(config) {
|
|
|
353
353
|
validBefore,
|
|
354
354
|
nonce
|
|
355
355
|
};
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
356
|
+
let signature;
|
|
357
|
+
let from;
|
|
358
|
+
try {
|
|
359
|
+
const signed = await signAuthorizationWithProvider(
|
|
360
|
+
provider,
|
|
361
|
+
authorization,
|
|
362
|
+
paymentRequirements
|
|
363
|
+
);
|
|
364
|
+
signature = signed.signature;
|
|
365
|
+
from = signed.from;
|
|
366
|
+
} catch (err) {
|
|
367
|
+
const code = err?.code;
|
|
368
|
+
if (code === 4001 || code === "ACTION_REJECTED") {
|
|
369
|
+
const userErr = new Error("User rejected payment signature");
|
|
370
|
+
userErr.code = "USER_REJECTED_SIGNATURE";
|
|
371
|
+
userErr.userRejected = true;
|
|
372
|
+
throw userErr;
|
|
373
|
+
}
|
|
374
|
+
throw err;
|
|
375
|
+
}
|
|
361
376
|
authorization = {
|
|
362
377
|
...authorization,
|
|
363
378
|
from
|
package/dist/react.js
CHANGED
package/dist/server-express.cjs
CHANGED
|
@@ -109,7 +109,7 @@ function usdCentsToAtomic(cents, decimals) {
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
// src/server/constants.ts
|
|
112
|
-
var DEFAULT_TELEMETRY_ENDPOINT =
|
|
112
|
+
var DEFAULT_TELEMETRY_ENDPOINT = "https://x402mantlesdk.xyz/api/telemetry/settled";
|
|
113
113
|
|
|
114
114
|
// src/server/telemetry.ts
|
|
115
115
|
function createTelemetryEvent(entry, config) {
|
package/dist/server-express.js
CHANGED
package/dist/server-nextjs.cjs
CHANGED
|
@@ -112,7 +112,7 @@ function usdCentsToAtomic(cents, decimals) {
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
// src/server/constants.ts
|
|
115
|
-
var DEFAULT_TELEMETRY_ENDPOINT =
|
|
115
|
+
var DEFAULT_TELEMETRY_ENDPOINT = "https://x402mantlesdk.xyz/api/telemetry/settled";
|
|
116
116
|
|
|
117
117
|
// src/server/telemetry.ts
|
|
118
118
|
function createTelemetryEvent(entry, config) {
|
package/dist/server-nextjs.js
CHANGED
package/dist/server-web.cjs
CHANGED
|
@@ -108,7 +108,7 @@ function usdCentsToAtomic(cents, decimals) {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
// src/server/constants.ts
|
|
111
|
-
var DEFAULT_TELEMETRY_ENDPOINT =
|
|
111
|
+
var DEFAULT_TELEMETRY_ENDPOINT = "https://x402mantlesdk.xyz/api/telemetry/settled";
|
|
112
112
|
|
|
113
113
|
// src/server/telemetry.ts
|
|
114
114
|
function createTelemetryEvent(entry, config) {
|
package/dist/server-web.js
CHANGED
package/dist/server.cjs
CHANGED
|
@@ -118,7 +118,7 @@ function usdCentsToAtomic(cents, decimals) {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
// src/server/constants.ts
|
|
121
|
-
var DEFAULT_TELEMETRY_ENDPOINT =
|
|
121
|
+
var DEFAULT_TELEMETRY_ENDPOINT = "https://x402mantlesdk.xyz/api/telemetry/settled";
|
|
122
122
|
|
|
123
123
|
// src/server/telemetry.ts
|
|
124
124
|
function createTelemetryEvent(entry, config) {
|
package/dist/server.js
CHANGED
|
@@ -3,13 +3,13 @@ import {
|
|
|
3
3
|
createPaymentMiddleware,
|
|
4
4
|
express_exports,
|
|
5
5
|
mantlePaywall
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-MBJTUNDL.js";
|
|
7
7
|
import {
|
|
8
8
|
nextjs_exports
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-5TTSYEOF.js";
|
|
10
10
|
import {
|
|
11
11
|
web_standards_exports
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-F2OTZ3BB.js";
|
|
13
13
|
import {
|
|
14
14
|
DEFAULT_TELEMETRY_ENDPOINT,
|
|
15
15
|
buildRouteKey,
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
decodePaymentHeader,
|
|
19
19
|
sendTelemetry,
|
|
20
20
|
validateAddress
|
|
21
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-EKEVUVF3.js";
|
|
22
22
|
import "./chunk-HEZZ74SI.js";
|
|
23
23
|
export {
|
|
24
24
|
DEFAULT_TELEMETRY_ENDPOINT,
|
package/package.json
CHANGED