@puga-labs/x402-mantle-sdk 0.2.0 → 0.3.1
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-CTI5CRDY.js +274 -0
- package/dist/chunk-DA6ZBXNO.js +275 -0
- package/dist/chunk-FD4HG7KR.js +135 -0
- package/dist/chunk-GWVWPS3R.js +277 -0
- package/dist/chunk-HTZ3QFY4.js +135 -0
- package/dist/chunk-MQALBRGV.js +135 -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-WELDWRDX.js +307 -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-C7aY8u5b.d.cts +77 -0
- package/dist/constants-C7aY8u5b.d.ts +77 -0
- package/dist/constants-CVFF0ray.d.ts +17 -0
- package/dist/constants-DzCGK0Q3.d.cts +17 -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/index.cjs +244 -43
- package/dist/index.d.cts +8 -164
- package/dist/index.d.ts +8 -164
- package/dist/index.js +21 -485
- package/dist/react.cjs +453 -0
- package/dist/react.d.cts +90 -0
- package/dist/react.d.ts +90 -0
- package/dist/react.js +10 -0
- package/dist/server.cjs +292 -0
- package/dist/server.d.cts +116 -0
- package/dist/server.d.ts +116 -0
- package/dist/server.js +12 -0
- package/dist/types-2zqbJvcz.d.cts +63 -0
- package/dist/types-2zqbJvcz.d.ts +63 -0
- package/package.json +37 -3
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/react.ts
|
|
31
|
+
var react_exports = {};
|
|
32
|
+
__export(react_exports, {
|
|
33
|
+
useEthersWallet: () => useEthersWallet,
|
|
34
|
+
useMantleX402: () => useMantleX402
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(react_exports);
|
|
37
|
+
|
|
38
|
+
// src/client/react/useEthersWallet.ts
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
var import_ethers = require("ethers");
|
|
41
|
+
function useEthersWallet(options) {
|
|
42
|
+
const [address, setAddress] = (0, import_react.useState)(void 0);
|
|
43
|
+
const [isConnected, setIsConnected] = (0, import_react.useState)(false);
|
|
44
|
+
const [provider, setProvider] = (0, import_react.useState)(
|
|
45
|
+
void 0
|
|
46
|
+
);
|
|
47
|
+
const [chainId, setChainId] = (0, import_react.useState)(void 0);
|
|
48
|
+
const [error, setError] = (0, import_react.useState)(void 0);
|
|
49
|
+
const connect = (0, import_react.useCallback)(async () => {
|
|
50
|
+
try {
|
|
51
|
+
setError(void 0);
|
|
52
|
+
if (typeof window === "undefined" || !window.ethereum) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"No Ethereum wallet detected. Please install MetaMask or another wallet."
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const browserProvider = new import_ethers.ethers.BrowserProvider(
|
|
58
|
+
window.ethereum
|
|
59
|
+
);
|
|
60
|
+
setProvider(browserProvider);
|
|
61
|
+
const accounts = await window.ethereum.request({
|
|
62
|
+
method: "eth_requestAccounts"
|
|
63
|
+
});
|
|
64
|
+
if (!accounts || accounts.length === 0) {
|
|
65
|
+
throw new Error("No accounts returned from wallet");
|
|
66
|
+
}
|
|
67
|
+
const userAddress = accounts[0];
|
|
68
|
+
setAddress(userAddress);
|
|
69
|
+
setIsConnected(true);
|
|
70
|
+
const network = await browserProvider.getNetwork();
|
|
71
|
+
setChainId(Number(network.chainId));
|
|
72
|
+
} catch (err) {
|
|
73
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
74
|
+
setError(errorObj);
|
|
75
|
+
setIsConnected(false);
|
|
76
|
+
setAddress(void 0);
|
|
77
|
+
setChainId(void 0);
|
|
78
|
+
throw errorObj;
|
|
79
|
+
}
|
|
80
|
+
}, []);
|
|
81
|
+
const disconnect = (0, import_react.useCallback)(() => {
|
|
82
|
+
setAddress(void 0);
|
|
83
|
+
setIsConnected(false);
|
|
84
|
+
setChainId(void 0);
|
|
85
|
+
setError(void 0);
|
|
86
|
+
}, []);
|
|
87
|
+
(0, import_react.useEffect)(() => {
|
|
88
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
89
|
+
const ethereum = window.ethereum;
|
|
90
|
+
const handleAccountsChanged = (accounts) => {
|
|
91
|
+
const accountsArray = accounts;
|
|
92
|
+
if (!accountsArray || accountsArray.length === 0) {
|
|
93
|
+
disconnect();
|
|
94
|
+
} else {
|
|
95
|
+
setAddress(accountsArray[0]);
|
|
96
|
+
setIsConnected(true);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
if (ethereum.on) {
|
|
100
|
+
ethereum.on("accountsChanged", handleAccountsChanged);
|
|
101
|
+
}
|
|
102
|
+
return () => {
|
|
103
|
+
if (ethereum.removeListener) {
|
|
104
|
+
ethereum.removeListener("accountsChanged", handleAccountsChanged);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}, [disconnect]);
|
|
108
|
+
(0, import_react.useEffect)(() => {
|
|
109
|
+
if (typeof window === "undefined" || !window.ethereum) return;
|
|
110
|
+
const ethereum = window.ethereum;
|
|
111
|
+
const handleChainChanged = (chainIdHex) => {
|
|
112
|
+
const newChainId = parseInt(chainIdHex, 16);
|
|
113
|
+
setChainId(newChainId);
|
|
114
|
+
};
|
|
115
|
+
if (ethereum.on) {
|
|
116
|
+
ethereum.on("chainChanged", handleChainChanged);
|
|
117
|
+
}
|
|
118
|
+
return () => {
|
|
119
|
+
if (ethereum.removeListener) {
|
|
120
|
+
ethereum.removeListener("chainChanged", handleChainChanged);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}, []);
|
|
124
|
+
(0, import_react.useEffect)(() => {
|
|
125
|
+
if (options?.autoConnect) {
|
|
126
|
+
connect().catch((err) => {
|
|
127
|
+
console.warn("[useEthersWallet] Auto-connect failed:", err);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}, [options?.autoConnect, connect]);
|
|
131
|
+
return {
|
|
132
|
+
address,
|
|
133
|
+
isConnected,
|
|
134
|
+
provider,
|
|
135
|
+
chainId,
|
|
136
|
+
connect,
|
|
137
|
+
disconnect,
|
|
138
|
+
error
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/shared/constants.ts
|
|
143
|
+
var MANTLE_MAINNET_NETWORK_ID = "mantle-mainnet";
|
|
144
|
+
var MANTLE_MAINNET_CHAIN_ID = 5e3;
|
|
145
|
+
var MANTLE_DEFAULTS = {
|
|
146
|
+
NETWORK: "mantle-mainnet",
|
|
147
|
+
CHAIN_ID: 5e3,
|
|
148
|
+
USDC_ADDRESS: "0x09Bc4E0D864854c6aFB6eB9A9cdF58aC190D0dF9",
|
|
149
|
+
USDC_DECIMALS: 6,
|
|
150
|
+
CURRENCY: "USD",
|
|
151
|
+
SCHEME: "exact",
|
|
152
|
+
FACILITATOR_URL: (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) || "http://localhost:8080"
|
|
153
|
+
};
|
|
154
|
+
function getChainIdForNetwork(network) {
|
|
155
|
+
switch (network) {
|
|
156
|
+
case MANTLE_MAINNET_NETWORK_ID:
|
|
157
|
+
default:
|
|
158
|
+
return MANTLE_MAINNET_CHAIN_ID;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/shared/utils.ts
|
|
163
|
+
function encodeJsonToBase64(value) {
|
|
164
|
+
const json = JSON.stringify(value);
|
|
165
|
+
if (typeof btoa === "function") {
|
|
166
|
+
const bytes = new TextEncoder().encode(json);
|
|
167
|
+
const binString = Array.from(
|
|
168
|
+
bytes,
|
|
169
|
+
(byte) => String.fromCodePoint(byte)
|
|
170
|
+
).join("");
|
|
171
|
+
return btoa(binString);
|
|
172
|
+
}
|
|
173
|
+
if (typeof globalThis.Buffer !== "undefined") {
|
|
174
|
+
return globalThis.Buffer.from(json, "utf8").toString("base64");
|
|
175
|
+
}
|
|
176
|
+
throw new Error("No base64 implementation found in this environment");
|
|
177
|
+
}
|
|
178
|
+
function randomBytes32Hex() {
|
|
179
|
+
if (typeof crypto !== "undefined" && "getRandomValues" in crypto) {
|
|
180
|
+
const arr = new Uint8Array(32);
|
|
181
|
+
crypto.getRandomValues(arr);
|
|
182
|
+
return "0x" + Array.from(arr).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const nodeCrypto = require("crypto");
|
|
186
|
+
const buf = nodeCrypto.randomBytes(32);
|
|
187
|
+
return "0x" + buf.toString("hex");
|
|
188
|
+
} catch {
|
|
189
|
+
throw new Error(
|
|
190
|
+
"No cryptographically secure random number generator found. This environment does not support crypto.getRandomValues or Node.js crypto module."
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/client/paymentClient.ts
|
|
196
|
+
function joinUrl(base, path) {
|
|
197
|
+
const normalizedBase = base.replace(/\/+$/, "");
|
|
198
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
199
|
+
return `${normalizedBase}${normalizedPath}`;
|
|
200
|
+
}
|
|
201
|
+
function buildTypedDataForAuthorization(authorization, paymentRequirements) {
|
|
202
|
+
const chainId = getChainIdForNetwork(paymentRequirements.network);
|
|
203
|
+
const verifyingContract = paymentRequirements.asset;
|
|
204
|
+
const domain = {
|
|
205
|
+
name: "USD Coin",
|
|
206
|
+
version: "2",
|
|
207
|
+
chainId,
|
|
208
|
+
verifyingContract
|
|
209
|
+
};
|
|
210
|
+
const types = {
|
|
211
|
+
TransferWithAuthorization: [
|
|
212
|
+
{ name: "from", type: "address" },
|
|
213
|
+
{ name: "to", type: "address" },
|
|
214
|
+
{ name: "value", type: "uint256" },
|
|
215
|
+
{ name: "validAfter", type: "uint256" },
|
|
216
|
+
{ name: "validBefore", type: "uint256" },
|
|
217
|
+
{ name: "nonce", type: "bytes32" }
|
|
218
|
+
]
|
|
219
|
+
};
|
|
220
|
+
return {
|
|
221
|
+
domain,
|
|
222
|
+
types,
|
|
223
|
+
primaryType: "TransferWithAuthorization",
|
|
224
|
+
message: authorization
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
async function signAuthorizationWithProvider(provider, authorization, paymentRequirements) {
|
|
228
|
+
const { BrowserProvider } = await import("ethers");
|
|
229
|
+
const chainId = getChainIdForNetwork(paymentRequirements.network);
|
|
230
|
+
const browserProvider = new BrowserProvider(provider, chainId);
|
|
231
|
+
const signer = await browserProvider.getSigner();
|
|
232
|
+
const from = await signer.getAddress();
|
|
233
|
+
const authWithFrom = {
|
|
234
|
+
...authorization,
|
|
235
|
+
from
|
|
236
|
+
};
|
|
237
|
+
const typedData = buildTypedDataForAuthorization(
|
|
238
|
+
authWithFrom,
|
|
239
|
+
paymentRequirements
|
|
240
|
+
);
|
|
241
|
+
const signature = await signer.signTypedData(
|
|
242
|
+
typedData.domain,
|
|
243
|
+
typedData.types,
|
|
244
|
+
typedData.message
|
|
245
|
+
);
|
|
246
|
+
return { signature, from };
|
|
247
|
+
}
|
|
248
|
+
function createPaymentClient(config) {
|
|
249
|
+
const {
|
|
250
|
+
resourceUrl,
|
|
251
|
+
facilitatorUrl,
|
|
252
|
+
provider,
|
|
253
|
+
userAddress: userAddressOverride,
|
|
254
|
+
projectKey
|
|
255
|
+
} = config;
|
|
256
|
+
if (!resourceUrl) {
|
|
257
|
+
throw new Error("resourceUrl is required");
|
|
258
|
+
}
|
|
259
|
+
if (!facilitatorUrl) {
|
|
260
|
+
throw new Error("facilitatorUrl is required");
|
|
261
|
+
}
|
|
262
|
+
if (!provider) {
|
|
263
|
+
throw new Error("provider is required (e.g. window.ethereum)");
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
async callWithPayment(path, options) {
|
|
267
|
+
const method = options?.method ?? "GET";
|
|
268
|
+
const headers = {
|
|
269
|
+
...options?.headers ?? {}
|
|
270
|
+
};
|
|
271
|
+
let body;
|
|
272
|
+
if (options?.body !== void 0) {
|
|
273
|
+
headers["Content-Type"] = headers["Content-Type"] ?? "application/json";
|
|
274
|
+
body = JSON.stringify(options.body);
|
|
275
|
+
}
|
|
276
|
+
const initialUrl = joinUrl(resourceUrl, path);
|
|
277
|
+
const initialRes = await fetch(initialUrl, {
|
|
278
|
+
method,
|
|
279
|
+
headers,
|
|
280
|
+
body
|
|
281
|
+
});
|
|
282
|
+
if (initialRes.status !== 402) {
|
|
283
|
+
const json = await initialRes.json().catch((err) => {
|
|
284
|
+
console.error("[x402] Failed to parse initial response JSON:", err);
|
|
285
|
+
return null;
|
|
286
|
+
});
|
|
287
|
+
return {
|
|
288
|
+
response: json,
|
|
289
|
+
txHash: void 0
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
const bodyJson = await initialRes.json();
|
|
293
|
+
const paymentRequirements = bodyJson.paymentRequirements;
|
|
294
|
+
if (!paymentRequirements) {
|
|
295
|
+
throw new Error(
|
|
296
|
+
"402 response did not include paymentRequirements field"
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
const nowSec = Math.floor(Date.now() / 1e3);
|
|
300
|
+
const validAfter = "0";
|
|
301
|
+
const validBefore = String(nowSec + 10 * 60);
|
|
302
|
+
const nonce = randomBytes32Hex();
|
|
303
|
+
const valueAtomic = options?.valueOverrideAtomic ?? paymentRequirements.maxAmountRequired;
|
|
304
|
+
let authorization = {
|
|
305
|
+
from: "0x0000000000000000000000000000000000000000",
|
|
306
|
+
to: paymentRequirements.payTo,
|
|
307
|
+
value: valueAtomic,
|
|
308
|
+
validAfter,
|
|
309
|
+
validBefore,
|
|
310
|
+
nonce
|
|
311
|
+
};
|
|
312
|
+
const { signature, from } = await signAuthorizationWithProvider(
|
|
313
|
+
provider,
|
|
314
|
+
authorization,
|
|
315
|
+
paymentRequirements
|
|
316
|
+
);
|
|
317
|
+
authorization = {
|
|
318
|
+
...authorization,
|
|
319
|
+
from
|
|
320
|
+
};
|
|
321
|
+
if (userAddressOverride && userAddressOverride.toLowerCase() !== from.toLowerCase()) {
|
|
322
|
+
console.warn(
|
|
323
|
+
"[SDK WARNING] userAddress override differs from signer address",
|
|
324
|
+
{ override: userAddressOverride, signer: from }
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const paymentHeaderObject = {
|
|
328
|
+
x402Version: 1,
|
|
329
|
+
scheme: paymentRequirements.scheme,
|
|
330
|
+
network: paymentRequirements.network,
|
|
331
|
+
payload: {
|
|
332
|
+
signature,
|
|
333
|
+
authorization
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const paymentHeader = encodeJsonToBase64(paymentHeaderObject);
|
|
337
|
+
const settleUrl = joinUrl(facilitatorUrl, "/settle");
|
|
338
|
+
const settleRes = await fetch(settleUrl, {
|
|
339
|
+
method: "POST",
|
|
340
|
+
headers: {
|
|
341
|
+
"Content-Type": "application/json",
|
|
342
|
+
...projectKey ? { "X-Project-Key": projectKey } : {}
|
|
343
|
+
},
|
|
344
|
+
body: JSON.stringify({
|
|
345
|
+
x402Version: 1,
|
|
346
|
+
paymentHeader,
|
|
347
|
+
paymentRequirements
|
|
348
|
+
})
|
|
349
|
+
});
|
|
350
|
+
if (!settleRes.ok) {
|
|
351
|
+
const text = await settleRes.text().catch((err) => {
|
|
352
|
+
console.error("[x402] Failed to read settle response text:", err);
|
|
353
|
+
return "";
|
|
354
|
+
});
|
|
355
|
+
throw new Error(
|
|
356
|
+
`Facilitator /settle failed with HTTP ${settleRes.status}: ${text}`
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
const settleJson = await settleRes.json();
|
|
360
|
+
if (!settleJson.success) {
|
|
361
|
+
throw new Error(
|
|
362
|
+
`Facilitator /settle returned error: ${settleJson.error ?? "unknown error"}`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
const txHash = settleJson.txHash ?? void 0;
|
|
366
|
+
const retryHeaders = {
|
|
367
|
+
...headers,
|
|
368
|
+
"X-PAYMENT": paymentHeader
|
|
369
|
+
};
|
|
370
|
+
const retryRes = await fetch(initialUrl, {
|
|
371
|
+
method,
|
|
372
|
+
headers: retryHeaders,
|
|
373
|
+
body
|
|
374
|
+
});
|
|
375
|
+
if (!retryRes.ok) {
|
|
376
|
+
const text = await retryRes.text().catch((err) => {
|
|
377
|
+
console.error("[x402] Failed to read retry response text:", err);
|
|
378
|
+
return "";
|
|
379
|
+
});
|
|
380
|
+
throw new Error(
|
|
381
|
+
`Protected request with X-PAYMENT failed: HTTP ${retryRes.status} ${text}`
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
const finalJson = await retryRes.json();
|
|
385
|
+
return {
|
|
386
|
+
response: finalJson,
|
|
387
|
+
txHash,
|
|
388
|
+
paymentHeader,
|
|
389
|
+
paymentRequirements
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// src/client/createMantleClient.ts
|
|
396
|
+
function createMantleClient(config) {
|
|
397
|
+
const resourceUrl = config?.resourceUrl ?? (typeof window !== "undefined" ? window.location.origin : "");
|
|
398
|
+
const facilitatorUrl = config?.facilitatorUrl ?? (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) ?? "http://localhost:8080";
|
|
399
|
+
return {
|
|
400
|
+
async postWithPayment(url, body) {
|
|
401
|
+
const account = await config?.getAccount?.();
|
|
402
|
+
if (!account) {
|
|
403
|
+
throw new Error(
|
|
404
|
+
"Wallet not connected. Please connect your wallet first."
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
const provider = config?.getProvider?.();
|
|
408
|
+
if (!provider) {
|
|
409
|
+
throw new Error("Wallet provider not available");
|
|
410
|
+
}
|
|
411
|
+
const client = createPaymentClient({
|
|
412
|
+
resourceUrl,
|
|
413
|
+
facilitatorUrl,
|
|
414
|
+
provider,
|
|
415
|
+
userAddress: account,
|
|
416
|
+
projectKey: config?.projectKey
|
|
417
|
+
});
|
|
418
|
+
const result = await client.callWithPayment(url, {
|
|
419
|
+
method: "POST",
|
|
420
|
+
body
|
|
421
|
+
});
|
|
422
|
+
return result;
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// src/client/react/useMantleX402.ts
|
|
428
|
+
function useMantleX402(opts) {
|
|
429
|
+
const { address, isConnected } = useEthersWallet({
|
|
430
|
+
autoConnect: opts?.autoConnect ?? false
|
|
431
|
+
});
|
|
432
|
+
const client = createMantleClient({
|
|
433
|
+
facilitatorUrl: opts?.facilitatorUrl,
|
|
434
|
+
resourceUrl: opts?.resourceUrl,
|
|
435
|
+
projectKey: opts?.projectKey,
|
|
436
|
+
getAccount: () => {
|
|
437
|
+
if (!isConnected || !address) return void 0;
|
|
438
|
+
return address;
|
|
439
|
+
},
|
|
440
|
+
getProvider: () => {
|
|
441
|
+
if (typeof window !== "undefined" && window.ethereum) {
|
|
442
|
+
return window.ethereum;
|
|
443
|
+
}
|
|
444
|
+
return void 0;
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
return client;
|
|
448
|
+
}
|
|
449
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
450
|
+
0 && (module.exports = {
|
|
451
|
+
useEthersWallet,
|
|
452
|
+
useMantleX402
|
|
453
|
+
});
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { M as MantleClient } from './createMantleClient-NN0Nitp9.cjs';
|
|
2
|
+
export { a as MantleClientConfig } from './createMantleClient-NN0Nitp9.cjs';
|
|
3
|
+
import { ethers } from 'ethers';
|
|
4
|
+
import './types-2zqbJvcz.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
|
+
/** Auto-connect to wallet on mount (default: false). */
|
|
46
|
+
autoConnect?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Return type for useEthersWallet hook.
|
|
50
|
+
*/
|
|
51
|
+
interface UseEthersWalletReturn {
|
|
52
|
+
/** User's wallet address (undefined if not connected). */
|
|
53
|
+
address: string | undefined;
|
|
54
|
+
/** Whether wallet is connected. */
|
|
55
|
+
isConnected: boolean;
|
|
56
|
+
/** ethers.BrowserProvider instance (undefined if not available). */
|
|
57
|
+
provider: ethers.BrowserProvider | undefined;
|
|
58
|
+
/** Current chain ID (undefined if not connected). */
|
|
59
|
+
chainId: number | undefined;
|
|
60
|
+
/** Connect to wallet (prompts user for permission). */
|
|
61
|
+
connect: () => Promise<void>;
|
|
62
|
+
/** Disconnect from wallet (clears state). */
|
|
63
|
+
disconnect: () => void;
|
|
64
|
+
/** Error that occurred during connection or operation. */
|
|
65
|
+
error: Error | undefined;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* React hook for managing Ethereum wallet connection using ethers.js.
|
|
69
|
+
*
|
|
70
|
+
* Works with any EIP-1193 compatible wallet provider (MetaMask, WalletConnect, etc.)
|
|
71
|
+
*
|
|
72
|
+
* Usage:
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const { address, isConnected, connect, disconnect } = useEthersWallet({
|
|
75
|
+
* autoConnect: true
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* if (!isConnected) {
|
|
79
|
+
* return <button onClick={connect}>Connect Wallet</button>;
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* return <div>Connected: {address}</div>;
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @param options - Configuration options
|
|
86
|
+
* @returns Wallet state and methods
|
|
87
|
+
*/
|
|
88
|
+
declare function useEthersWallet(options?: UseEthersWalletOptions): UseEthersWalletReturn;
|
|
89
|
+
|
|
90
|
+
export { MantleClient, type UseEthersWalletOptions, type UseEthersWalletReturn, type UseMantleX402Options, useEthersWallet, useMantleX402 };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { M as MantleClient } from './createMantleClient-DVFkbBfS.js';
|
|
2
|
+
export { a as MantleClientConfig } from './createMantleClient-DVFkbBfS.js';
|
|
3
|
+
import { ethers } from 'ethers';
|
|
4
|
+
import './types-2zqbJvcz.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
|
+
/** Auto-connect to wallet on mount (default: false). */
|
|
46
|
+
autoConnect?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Return type for useEthersWallet hook.
|
|
50
|
+
*/
|
|
51
|
+
interface UseEthersWalletReturn {
|
|
52
|
+
/** User's wallet address (undefined if not connected). */
|
|
53
|
+
address: string | undefined;
|
|
54
|
+
/** Whether wallet is connected. */
|
|
55
|
+
isConnected: boolean;
|
|
56
|
+
/** ethers.BrowserProvider instance (undefined if not available). */
|
|
57
|
+
provider: ethers.BrowserProvider | undefined;
|
|
58
|
+
/** Current chain ID (undefined if not connected). */
|
|
59
|
+
chainId: number | undefined;
|
|
60
|
+
/** Connect to wallet (prompts user for permission). */
|
|
61
|
+
connect: () => Promise<void>;
|
|
62
|
+
/** Disconnect from wallet (clears state). */
|
|
63
|
+
disconnect: () => void;
|
|
64
|
+
/** Error that occurred during connection or operation. */
|
|
65
|
+
error: Error | undefined;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* React hook for managing Ethereum wallet connection using ethers.js.
|
|
69
|
+
*
|
|
70
|
+
* Works with any EIP-1193 compatible wallet provider (MetaMask, WalletConnect, etc.)
|
|
71
|
+
*
|
|
72
|
+
* Usage:
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const { address, isConnected, connect, disconnect } = useEthersWallet({
|
|
75
|
+
* autoConnect: true
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* if (!isConnected) {
|
|
79
|
+
* return <button onClick={connect}>Connect Wallet</button>;
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* return <div>Connected: {address}</div>;
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @param options - Configuration options
|
|
86
|
+
* @returns Wallet state and methods
|
|
87
|
+
*/
|
|
88
|
+
declare function useEthersWallet(options?: UseEthersWalletOptions): UseEthersWalletReturn;
|
|
89
|
+
|
|
90
|
+
export { MantleClient, type UseEthersWalletOptions, type UseEthersWalletReturn, type UseMantleX402Options, useEthersWallet, useMantleX402 };
|