@puga-labs/x402-mantle-sdk 0.3.1 → 0.3.3
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-HTZ3QFY4.js → chunk-23QNUJIB.js} +4 -3
- package/dist/chunk-CXRILT3C.js +68 -0
- package/dist/{chunk-CTI5CRDY.js → chunk-E46A7I6B.js} +1 -1
- package/dist/{chunk-XAQGMFSR.js → chunk-HEZZ74SI.js} +6 -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-QWQUSRLY.js +99 -0
- package/dist/chunk-U73CZU3X.js +237 -0
- package/dist/chunk-WO2MYZXT.js +0 -0
- package/dist/chunk-ZLCKBFVJ.js +237 -0
- package/dist/chunk-ZROK2XOB.js +70 -0
- package/dist/client.d.cts +4 -4
- package/dist/client.d.ts +4 -4
- package/dist/client.js +2 -2
- package/dist/{constants-CVFF0ray.d.ts → constants-0ncqvV_O.d.ts} +1 -1
- package/dist/{constants-DzCGK0Q3.d.cts → constants-CsIL25uQ.d.cts} +1 -1
- package/dist/{createMantleClient-NN0Nitp9.d.cts → createMantleClient-CO0uWPb-.d.cts} +1 -1
- package/dist/{createMantleClient-DVFkbBfS.d.ts → createMantleClient-CuiPsTa6.d.ts} +1 -1
- package/dist/express-D8EwEcOL.d.ts +66 -0
- package/dist/express-eQOPxfnI.d.cts +66 -0
- package/dist/index.cjs +186 -109
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +6 -4
- package/dist/nextjs-Duecps0q.d.cts +45 -0
- package/dist/nextjs-TFhFiQuL.d.ts +45 -0
- package/dist/react.cjs +3 -2
- package/dist/react.d.cts +7 -4
- package/dist/react.d.ts +7 -4
- package/dist/react.js +3 -3
- package/dist/server-express.cjs +368 -0
- package/dist/server-express.d.cts +5 -0
- package/dist/server-express.d.ts +5 -0
- package/dist/server-express.js +13 -0
- package/dist/server-nextjs.cjs +339 -0
- package/dist/server-nextjs.d.cts +5 -0
- package/dist/server-nextjs.d.ts +5 -0
- package/dist/server-nextjs.js +11 -0
- package/dist/server-web.cjs +339 -0
- package/dist/server-web.d.cts +4 -0
- package/dist/server-web.d.ts +4 -0
- package/dist/server-web.js +11 -0
- package/dist/server.cjs +316 -111
- package/dist/server.d.cts +67 -104
- package/dist/server.d.ts +67 -104
- package/dist/server.js +29 -5
- package/dist/{types-2zqbJvcz.d.cts → types-BFUqKBBO.d.cts} +1 -1
- package/dist/{types-2zqbJvcz.d.ts → types-BFUqKBBO.d.ts} +1 -1
- 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-DTzov_EE.d.ts +97 -0
- package/dist/web-standards-BNQyWzBC.d.cts +77 -0
- package/dist/web-standards-D8j1kZxd.d.ts +77 -0
- package/package.json +49 -2
- package/dist/chunk-DA6ZBXNO.js +0 -275
- package/dist/chunk-FD4HG7KR.js +0 -135
- package/dist/chunk-GWVWPS3R.js +0 -277
- package/dist/chunk-MQALBRGV.js +0 -135
- package/dist/chunk-PYIYE3HI.js +0 -135
- package/dist/chunk-Q6SPMEIW.js +0 -235
- package/dist/chunk-RNKXSBT7.js +0 -135
- package/dist/chunk-SPCXFN7C.js +0 -284
- package/dist/chunk-T5DRYLNB.js +0 -135
- package/dist/chunk-TSEE5NSJ.js +0 -297
- package/dist/chunk-WELDWRDX.js +0 -307
- package/dist/constants-C7aY8u5b.d.cts +0 -77
- package/dist/constants-C7aY8u5b.d.ts +0 -77
- package/dist/createMantleClient-DS1Ghqrz.d.cts +0 -51
- package/dist/createMantleClient-DS1Ghqrz.d.ts +0 -51
package/dist/chunk-T5DRYLNB.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createMantleClient
|
|
3
|
-
} from "./chunk-CTI5CRDY.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 connect = useCallback(async () => {
|
|
17
|
-
try {
|
|
18
|
-
setError(void 0);
|
|
19
|
-
if (typeof window === "undefined" || !window.ethereum) {
|
|
20
|
-
throw new Error(
|
|
21
|
-
"No Ethereum wallet detected. Please install MetaMask or another wallet."
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
const browserProvider = new ethers.BrowserProvider(
|
|
25
|
-
window.ethereum
|
|
26
|
-
);
|
|
27
|
-
setProvider(browserProvider);
|
|
28
|
-
const accounts = await window.ethereum.request({
|
|
29
|
-
method: "eth_requestAccounts"
|
|
30
|
-
});
|
|
31
|
-
if (!accounts || accounts.length === 0) {
|
|
32
|
-
throw new Error("No accounts returned from wallet");
|
|
33
|
-
}
|
|
34
|
-
const userAddress = accounts[0];
|
|
35
|
-
setAddress(userAddress);
|
|
36
|
-
setIsConnected(true);
|
|
37
|
-
const network = await browserProvider.getNetwork();
|
|
38
|
-
setChainId(Number(network.chainId));
|
|
39
|
-
} catch (err) {
|
|
40
|
-
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
41
|
-
setError(errorObj);
|
|
42
|
-
setIsConnected(false);
|
|
43
|
-
setAddress(void 0);
|
|
44
|
-
setChainId(void 0);
|
|
45
|
-
throw errorObj;
|
|
46
|
-
}
|
|
47
|
-
}, []);
|
|
48
|
-
const disconnect = useCallback(() => {
|
|
49
|
-
setAddress(void 0);
|
|
50
|
-
setIsConnected(false);
|
|
51
|
-
setChainId(void 0);
|
|
52
|
-
setError(void 0);
|
|
53
|
-
}, []);
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
if (typeof window === "undefined" || !window.ethereum) return;
|
|
56
|
-
const ethereum = window.ethereum;
|
|
57
|
-
const handleAccountsChanged = (accounts) => {
|
|
58
|
-
const accountsArray = accounts;
|
|
59
|
-
if (!accountsArray || accountsArray.length === 0) {
|
|
60
|
-
disconnect();
|
|
61
|
-
} else {
|
|
62
|
-
setAddress(accountsArray[0]);
|
|
63
|
-
setIsConnected(true);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
if (ethereum.on) {
|
|
67
|
-
ethereum.on("accountsChanged", handleAccountsChanged);
|
|
68
|
-
}
|
|
69
|
-
return () => {
|
|
70
|
-
if (ethereum.removeListener) {
|
|
71
|
-
ethereum.removeListener("accountsChanged", handleAccountsChanged);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}, [disconnect]);
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
if (typeof window === "undefined" || !window.ethereum) return;
|
|
77
|
-
const ethereum = window.ethereum;
|
|
78
|
-
const handleChainChanged = (chainIdHex) => {
|
|
79
|
-
const newChainId = parseInt(chainIdHex, 16);
|
|
80
|
-
setChainId(newChainId);
|
|
81
|
-
};
|
|
82
|
-
if (ethereum.on) {
|
|
83
|
-
ethereum.on("chainChanged", handleChainChanged);
|
|
84
|
-
}
|
|
85
|
-
return () => {
|
|
86
|
-
if (ethereum.removeListener) {
|
|
87
|
-
ethereum.removeListener("chainChanged", handleChainChanged);
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
}, []);
|
|
91
|
-
useEffect(() => {
|
|
92
|
-
if (options?.autoConnect) {
|
|
93
|
-
connect().catch((err) => {
|
|
94
|
-
console.warn("[useEthersWallet] Auto-connect failed:", err);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}, [options?.autoConnect, connect]);
|
|
98
|
-
return {
|
|
99
|
-
address,
|
|
100
|
-
isConnected,
|
|
101
|
-
provider,
|
|
102
|
-
chainId,
|
|
103
|
-
connect,
|
|
104
|
-
disconnect,
|
|
105
|
-
error
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// src/client/react/useMantleX402.ts
|
|
110
|
-
function useMantleX402(opts) {
|
|
111
|
-
const { address, isConnected } = useEthersWallet({
|
|
112
|
-
autoConnect: opts?.autoConnect ?? false
|
|
113
|
-
});
|
|
114
|
-
const client = createMantleClient({
|
|
115
|
-
facilitatorUrl: opts?.facilitatorUrl,
|
|
116
|
-
resourceUrl: opts?.resourceUrl,
|
|
117
|
-
projectKey: opts?.projectKey,
|
|
118
|
-
getAccount: () => {
|
|
119
|
-
if (!isConnected || !address) return void 0;
|
|
120
|
-
return address;
|
|
121
|
-
},
|
|
122
|
-
getProvider: () => {
|
|
123
|
-
if (typeof window !== "undefined" && window.ethereum) {
|
|
124
|
-
return window.ethereum;
|
|
125
|
-
}
|
|
126
|
-
return void 0;
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
return client;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export {
|
|
133
|
-
useEthersWallet,
|
|
134
|
-
useMantleX402
|
|
135
|
-
};
|
package/dist/chunk-TSEE5NSJ.js
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__require,
|
|
3
|
-
getChainIdForNetwork
|
|
4
|
-
} from "./chunk-XAQGMFSR.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
|
-
async function getUserAddressFromProvider(provider) {
|
|
46
|
-
const p = provider;
|
|
47
|
-
if (!p || typeof p.request !== "function") {
|
|
48
|
-
throw new Error("provider must implement EIP-1193 request()");
|
|
49
|
-
}
|
|
50
|
-
const accounts = await p.request({
|
|
51
|
-
method: "eth_requestAccounts"
|
|
52
|
-
});
|
|
53
|
-
if (!accounts || accounts.length === 0) {
|
|
54
|
-
throw new Error("No accounts returned from provider");
|
|
55
|
-
}
|
|
56
|
-
return accounts[0];
|
|
57
|
-
}
|
|
58
|
-
function buildTypedDataForAuthorization(authorization, paymentRequirements) {
|
|
59
|
-
const chainId = getChainIdForNetwork(paymentRequirements.network);
|
|
60
|
-
const verifyingContract = paymentRequirements.asset;
|
|
61
|
-
const domain = {
|
|
62
|
-
name: "USD Coin",
|
|
63
|
-
version: "2",
|
|
64
|
-
chainId,
|
|
65
|
-
verifyingContract
|
|
66
|
-
};
|
|
67
|
-
const types = {
|
|
68
|
-
TransferWithAuthorization: [
|
|
69
|
-
{ name: "from", type: "address" },
|
|
70
|
-
{ name: "to", type: "address" },
|
|
71
|
-
{ name: "value", type: "uint256" },
|
|
72
|
-
{ name: "validAfter", type: "uint256" },
|
|
73
|
-
{ name: "validBefore", type: "uint256" },
|
|
74
|
-
{ name: "nonce", type: "bytes32" }
|
|
75
|
-
]
|
|
76
|
-
};
|
|
77
|
-
return {
|
|
78
|
-
domain,
|
|
79
|
-
types,
|
|
80
|
-
primaryType: "TransferWithAuthorization",
|
|
81
|
-
message: authorization
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
async function signAuthorizationWithProvider(provider, address, authorization, paymentRequirements) {
|
|
85
|
-
const p = provider;
|
|
86
|
-
if (!p || typeof p.request !== "function") {
|
|
87
|
-
throw new Error("provider must implement EIP-1193 request()");
|
|
88
|
-
}
|
|
89
|
-
const typedData = buildTypedDataForAuthorization(
|
|
90
|
-
authorization,
|
|
91
|
-
paymentRequirements
|
|
92
|
-
);
|
|
93
|
-
console.log("[SDK DEBUG] Signing with address:", address);
|
|
94
|
-
console.log("[SDK DEBUG] Authorization.from:", authorization.from);
|
|
95
|
-
console.log("[SDK DEBUG] Match:", address.toLowerCase() === authorization.from.toLowerCase());
|
|
96
|
-
console.log("[SDK DEBUG] Full typedData being signed:", JSON.stringify(typedData, null, 2));
|
|
97
|
-
const params = [address, JSON.stringify(typedData)];
|
|
98
|
-
const signature = await p.request({
|
|
99
|
-
method: "eth_signTypedData_v4",
|
|
100
|
-
params
|
|
101
|
-
});
|
|
102
|
-
console.log("[SDK DEBUG] Signature created:", signature);
|
|
103
|
-
try {
|
|
104
|
-
const { ethers } = await import("ethers");
|
|
105
|
-
const recovered = ethers.verifyTypedData(
|
|
106
|
-
typedData.domain,
|
|
107
|
-
typedData.types,
|
|
108
|
-
typedData.message,
|
|
109
|
-
signature
|
|
110
|
-
);
|
|
111
|
-
console.log("[SDK DEBUG] Locally recovered address:", recovered);
|
|
112
|
-
console.log("[SDK DEBUG] Does it match expected?", recovered.toLowerCase() === address.toLowerCase());
|
|
113
|
-
} catch (err) {
|
|
114
|
-
console.error("[SDK DEBUG] Local verification failed:", err);
|
|
115
|
-
}
|
|
116
|
-
if (typeof signature !== "string" || !signature.startsWith("0x")) {
|
|
117
|
-
throw new Error("Invalid signature returned from provider");
|
|
118
|
-
}
|
|
119
|
-
return signature;
|
|
120
|
-
}
|
|
121
|
-
function createPaymentClient(config) {
|
|
122
|
-
const {
|
|
123
|
-
resourceUrl,
|
|
124
|
-
facilitatorUrl,
|
|
125
|
-
provider,
|
|
126
|
-
userAddress: userAddressOverride,
|
|
127
|
-
projectKey
|
|
128
|
-
} = config;
|
|
129
|
-
if (!resourceUrl) {
|
|
130
|
-
throw new Error("resourceUrl is required");
|
|
131
|
-
}
|
|
132
|
-
if (!facilitatorUrl) {
|
|
133
|
-
throw new Error("facilitatorUrl is required");
|
|
134
|
-
}
|
|
135
|
-
if (!provider) {
|
|
136
|
-
throw new Error("provider is required (e.g. window.ethereum)");
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
async callWithPayment(path, options) {
|
|
140
|
-
const method = options?.method ?? "GET";
|
|
141
|
-
const headers = {
|
|
142
|
-
...options?.headers ?? {}
|
|
143
|
-
};
|
|
144
|
-
let body;
|
|
145
|
-
if (options?.body !== void 0) {
|
|
146
|
-
headers["Content-Type"] = headers["Content-Type"] ?? "application/json";
|
|
147
|
-
body = JSON.stringify(options.body);
|
|
148
|
-
}
|
|
149
|
-
const initialUrl = joinUrl(resourceUrl, path);
|
|
150
|
-
const initialRes = await fetch(initialUrl, {
|
|
151
|
-
method,
|
|
152
|
-
headers,
|
|
153
|
-
body
|
|
154
|
-
});
|
|
155
|
-
if (initialRes.status !== 402) {
|
|
156
|
-
const json = await initialRes.json().catch((err) => {
|
|
157
|
-
console.error("[x402] Failed to parse initial response JSON:", err);
|
|
158
|
-
return null;
|
|
159
|
-
});
|
|
160
|
-
return {
|
|
161
|
-
response: json,
|
|
162
|
-
txHash: void 0
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
const bodyJson = await initialRes.json();
|
|
166
|
-
const paymentRequirements = bodyJson.paymentRequirements;
|
|
167
|
-
if (!paymentRequirements) {
|
|
168
|
-
throw new Error(
|
|
169
|
-
"402 response did not include paymentRequirements field"
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
const fromAddress = userAddressOverride ?? await getUserAddressFromProvider(provider);
|
|
173
|
-
console.log("[SDK DEBUG] fromAddress determined:", fromAddress);
|
|
174
|
-
console.log("[SDK DEBUG] userAddressOverride:", userAddressOverride);
|
|
175
|
-
const nowSec = Math.floor(Date.now() / 1e3);
|
|
176
|
-
const validAfter = "0";
|
|
177
|
-
const validBefore = String(nowSec + 10 * 60);
|
|
178
|
-
const nonce = randomBytes32Hex();
|
|
179
|
-
const valueAtomic = options?.valueOverrideAtomic ?? paymentRequirements.maxAmountRequired;
|
|
180
|
-
const authorization = {
|
|
181
|
-
from: fromAddress,
|
|
182
|
-
to: paymentRequirements.payTo,
|
|
183
|
-
value: valueAtomic,
|
|
184
|
-
validAfter,
|
|
185
|
-
validBefore,
|
|
186
|
-
nonce
|
|
187
|
-
};
|
|
188
|
-
const signature = await signAuthorizationWithProvider(
|
|
189
|
-
provider,
|
|
190
|
-
fromAddress,
|
|
191
|
-
authorization,
|
|
192
|
-
paymentRequirements
|
|
193
|
-
);
|
|
194
|
-
const paymentHeaderObject = {
|
|
195
|
-
x402Version: 1,
|
|
196
|
-
scheme: paymentRequirements.scheme,
|
|
197
|
-
network: paymentRequirements.network,
|
|
198
|
-
payload: {
|
|
199
|
-
signature,
|
|
200
|
-
authorization
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
const paymentHeader = encodeJsonToBase64(paymentHeaderObject);
|
|
204
|
-
const settleUrl = joinUrl(facilitatorUrl, "/settle");
|
|
205
|
-
const settleRes = await fetch(settleUrl, {
|
|
206
|
-
method: "POST",
|
|
207
|
-
headers: {
|
|
208
|
-
"Content-Type": "application/json",
|
|
209
|
-
...projectKey ? { "X-Project-Key": projectKey } : {}
|
|
210
|
-
},
|
|
211
|
-
body: JSON.stringify({
|
|
212
|
-
x402Version: 1,
|
|
213
|
-
paymentHeader,
|
|
214
|
-
paymentRequirements
|
|
215
|
-
})
|
|
216
|
-
});
|
|
217
|
-
if (!settleRes.ok) {
|
|
218
|
-
const text = await settleRes.text().catch((err) => {
|
|
219
|
-
console.error("[x402] Failed to read settle response text:", err);
|
|
220
|
-
return "";
|
|
221
|
-
});
|
|
222
|
-
throw new Error(
|
|
223
|
-
`Facilitator /settle failed with HTTP ${settleRes.status}: ${text}`
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
const settleJson = await settleRes.json();
|
|
227
|
-
if (!settleJson.success) {
|
|
228
|
-
throw new Error(
|
|
229
|
-
`Facilitator /settle returned error: ${settleJson.error ?? "unknown error"}`
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
const txHash = settleJson.txHash ?? void 0;
|
|
233
|
-
const retryHeaders = {
|
|
234
|
-
...headers,
|
|
235
|
-
"X-PAYMENT": paymentHeader
|
|
236
|
-
};
|
|
237
|
-
const retryRes = await fetch(initialUrl, {
|
|
238
|
-
method,
|
|
239
|
-
headers: retryHeaders,
|
|
240
|
-
body
|
|
241
|
-
});
|
|
242
|
-
if (!retryRes.ok) {
|
|
243
|
-
const text = await retryRes.text().catch((err) => {
|
|
244
|
-
console.error("[x402] Failed to read retry response text:", err);
|
|
245
|
-
return "";
|
|
246
|
-
});
|
|
247
|
-
throw new Error(
|
|
248
|
-
`Protected request with X-PAYMENT failed: HTTP ${retryRes.status} ${text}`
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
const finalJson = await retryRes.json();
|
|
252
|
-
return {
|
|
253
|
-
response: finalJson,
|
|
254
|
-
txHash,
|
|
255
|
-
paymentHeader,
|
|
256
|
-
paymentRequirements
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// src/client/createMantleClient.ts
|
|
263
|
-
function createMantleClient(config) {
|
|
264
|
-
const resourceUrl = config?.resourceUrl ?? (typeof window !== "undefined" ? window.location.origin : "");
|
|
265
|
-
const facilitatorUrl = config?.facilitatorUrl ?? (typeof process !== "undefined" ? process.env?.NEXT_PUBLIC_FACILITATOR_URL : void 0) ?? "http://localhost:8080";
|
|
266
|
-
return {
|
|
267
|
-
async postWithPayment(url, body) {
|
|
268
|
-
const account = await config?.getAccount?.();
|
|
269
|
-
if (!account) {
|
|
270
|
-
throw new Error(
|
|
271
|
-
"Wallet not connected. Please connect your wallet first."
|
|
272
|
-
);
|
|
273
|
-
}
|
|
274
|
-
const provider = config?.getProvider?.();
|
|
275
|
-
if (!provider) {
|
|
276
|
-
throw new Error("Wallet provider not available");
|
|
277
|
-
}
|
|
278
|
-
const client = createPaymentClient({
|
|
279
|
-
resourceUrl,
|
|
280
|
-
facilitatorUrl,
|
|
281
|
-
provider,
|
|
282
|
-
userAddress: account,
|
|
283
|
-
projectKey: config?.projectKey
|
|
284
|
-
});
|
|
285
|
-
const result = await client.callWithPayment(url, {
|
|
286
|
-
method: "POST",
|
|
287
|
-
body
|
|
288
|
-
});
|
|
289
|
-
return result.response;
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export {
|
|
295
|
-
createPaymentClient,
|
|
296
|
-
createMantleClient
|
|
297
|
-
};
|