@puga-labs/x402-mantle-sdk 0.3.2 → 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-GHUWTX7J.js → chunk-CXRILT3C.js} +4 -2
- package/dist/{chunk-6GLR5EBK.js → chunk-QWQUSRLY.js} +5 -2
- package/dist/{chunk-BRGW36P7.js → chunk-ZLCKBFVJ.js} +27 -1
- package/dist/{chunk-WFUDGBKK.js → chunk-ZROK2XOB.js} +4 -2
- package/dist/index.cjs +1 -1
- package/dist/index.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 +2 -2
- package/dist/chunk-CTI5CRDY.js +0 -274
- package/dist/chunk-DA6ZBXNO.js +0 -275
- package/dist/chunk-FD4HG7KR.js +0 -135
- package/dist/chunk-FHU6CFHF.js +0 -0
- package/dist/chunk-GWVWPS3R.js +0 -277
- package/dist/chunk-HTZ3QFY4.js +0 -135
- 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-WAKJRVUP.js +0 -135
- package/dist/chunk-WELDWRDX.js +0 -307
- package/dist/chunk-XAQGMFSR.js +0 -56
- package/dist/constants-C7aY8u5b.d.cts +0 -77
- package/dist/constants-C7aY8u5b.d.ts +0 -77
- package/dist/constants-CVFF0ray.d.ts +0 -17
- package/dist/constants-DzCGK0Q3.d.cts +0 -17
- package/dist/createMantleClient-DS1Ghqrz.d.cts +0 -51
- package/dist/createMantleClient-DS1Ghqrz.d.ts +0 -51
- package/dist/createMantleClient-DVFkbBfS.d.ts +0 -87
- package/dist/createMantleClient-NN0Nitp9.d.cts +0 -87
- package/dist/express-Ck7eryef.d.cts +0 -66
- package/dist/express-CmEg5hR3.d.ts +0 -66
- package/dist/nextjs-Bp8DxYDN.d.ts +0 -45
- package/dist/nextjs-CliiaTPe.d.cts +0 -45
- package/dist/nextjs-D7t-BC4N.d.ts +0 -45
- package/dist/nextjs-ZO2J9mdo.d.cts +0 -45
- package/dist/types-2zqbJvcz.d.cts +0 -63
- package/dist/types-2zqbJvcz.d.ts +0 -63
- package/dist/types-C7EPJ6Hd.d.ts +0 -85
- package/dist/types-CEttmr1U.d.cts +0 -97
- package/dist/types-D20K36pc.d.ts +0 -97
- package/dist/types-xkNm0uzE.d.cts +0 -85
- package/dist/web-standards-C3D2vdTg.d.ts +0 -77
- package/dist/web-standards-DOYp0UDT.d.cts +0 -77
package/dist/chunk-HTZ3QFY4.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createMantleClient
|
|
3
|
-
} from "./chunk-WELDWRDX.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-MQALBRGV.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createMantleClient
|
|
3
|
-
} from "./chunk-SPCXFN7C.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-PYIYE3HI.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createMantleClient
|
|
3
|
-
} from "./chunk-DA6ZBXNO.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-Q6SPMEIW.js
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
MANTLE_DEFAULTS,
|
|
3
|
-
getDefaultAssetForNetwork,
|
|
4
|
-
usdCentsToAtomic
|
|
5
|
-
} from "./chunk-XAQGMFSR.js";
|
|
6
|
-
|
|
7
|
-
// src/server/constants.ts
|
|
8
|
-
var DEFAULT_TELEMETRY_ENDPOINT = void 0;
|
|
9
|
-
|
|
10
|
-
// src/server/telemetry.ts
|
|
11
|
-
function createTelemetryEvent(entry, config) {
|
|
12
|
-
const assetConfig = getDefaultAssetForNetwork(entry.network);
|
|
13
|
-
return {
|
|
14
|
-
event: "payment_verified",
|
|
15
|
-
ts: entry.timestamp,
|
|
16
|
-
projectKey: config.projectKey,
|
|
17
|
-
network: entry.network,
|
|
18
|
-
buyer: entry.from,
|
|
19
|
-
payTo: entry.to,
|
|
20
|
-
amountAtomic: entry.valueAtomic,
|
|
21
|
-
asset: entry.asset,
|
|
22
|
-
decimals: assetConfig.decimals,
|
|
23
|
-
nonce: entry.id,
|
|
24
|
-
route: entry.route ?? "unknown",
|
|
25
|
-
// Facilitator metadata
|
|
26
|
-
facilitatorType: "hosted",
|
|
27
|
-
// SDK always uses hosted mode
|
|
28
|
-
facilitatorUrl: entry.facilitatorUrl,
|
|
29
|
-
// From PaymentLogEntry
|
|
30
|
-
// facilitatorAddress is undefined for SDK (not available)
|
|
31
|
-
// Optional metadata
|
|
32
|
-
txHash: entry.txHash,
|
|
33
|
-
priceUsd: entry.paymentRequirements?.price
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
async function sendTelemetry(event, endpoint) {
|
|
37
|
-
const targetEndpoint = endpoint ?? DEFAULT_TELEMETRY_ENDPOINT;
|
|
38
|
-
if (!targetEndpoint) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
const response = await fetch(targetEndpoint, {
|
|
43
|
-
method: "POST",
|
|
44
|
-
headers: {
|
|
45
|
-
"Content-Type": "application/json",
|
|
46
|
-
"Authorization": `Bearer ${event.projectKey}`
|
|
47
|
-
},
|
|
48
|
-
body: JSON.stringify(event)
|
|
49
|
-
});
|
|
50
|
-
if (!response.ok) {
|
|
51
|
-
console.warn(
|
|
52
|
-
`[x402-telemetry] Failed to send event: HTTP ${response.status}`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
} catch (err) {
|
|
56
|
-
console.error("[x402-telemetry] Error sending telemetry:", err);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/server/paymentMiddleware.ts
|
|
61
|
-
function getRouteKey(req) {
|
|
62
|
-
const method = (req.method || "GET").toUpperCase();
|
|
63
|
-
const path = req.path || "/";
|
|
64
|
-
return `${method} ${path}`;
|
|
65
|
-
}
|
|
66
|
-
function decodePaymentHeader(paymentHeaderBase64) {
|
|
67
|
-
try {
|
|
68
|
-
if (typeof Buffer !== "undefined") {
|
|
69
|
-
const json = Buffer.from(paymentHeaderBase64, "base64").toString("utf8");
|
|
70
|
-
return JSON.parse(json);
|
|
71
|
-
}
|
|
72
|
-
if (typeof atob === "function") {
|
|
73
|
-
const json = atob(paymentHeaderBase64);
|
|
74
|
-
return JSON.parse(json);
|
|
75
|
-
}
|
|
76
|
-
throw new Error("No base64 decoding available in this environment");
|
|
77
|
-
} catch (err) {
|
|
78
|
-
const msg = err instanceof Error ? err.message : "Unknown error";
|
|
79
|
-
throw new Error(`Failed to decode paymentHeader: ${msg}`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function createPaymentMiddleware(config) {
|
|
83
|
-
const { facilitatorUrl, receiverAddress, routes, onPaymentSettled, telemetry } = config;
|
|
84
|
-
if (!facilitatorUrl) {
|
|
85
|
-
throw new Error("facilitatorUrl is required");
|
|
86
|
-
}
|
|
87
|
-
if (!receiverAddress) {
|
|
88
|
-
throw new Error("receiverAddress is required");
|
|
89
|
-
}
|
|
90
|
-
if (!routes || Object.keys(routes).length === 0) {
|
|
91
|
-
throw new Error("routes config must not be empty");
|
|
92
|
-
}
|
|
93
|
-
return async function paymentMiddleware(req, res, next) {
|
|
94
|
-
const routeKey = getRouteKey(req);
|
|
95
|
-
const routeConfig = routes[routeKey];
|
|
96
|
-
if (!routeConfig) {
|
|
97
|
-
next();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
const { priceUsdCents, network } = routeConfig;
|
|
101
|
-
const assetConfig = getDefaultAssetForNetwork(network);
|
|
102
|
-
const maxAmountRequiredBigInt = usdCentsToAtomic(
|
|
103
|
-
priceUsdCents,
|
|
104
|
-
assetConfig.decimals
|
|
105
|
-
);
|
|
106
|
-
const paymentRequirements = {
|
|
107
|
-
scheme: "exact",
|
|
108
|
-
network,
|
|
109
|
-
asset: assetConfig.address,
|
|
110
|
-
maxAmountRequired: maxAmountRequiredBigInt.toString(),
|
|
111
|
-
payTo: receiverAddress,
|
|
112
|
-
price: `$${(priceUsdCents / 100).toFixed(2)}`,
|
|
113
|
-
currency: "USD"
|
|
114
|
-
};
|
|
115
|
-
const paymentHeader = req.header("X-PAYMENT") ?? req.header("x-payment");
|
|
116
|
-
if (!paymentHeader) {
|
|
117
|
-
res.status(402).json({
|
|
118
|
-
error: "Payment Required",
|
|
119
|
-
paymentRequirements,
|
|
120
|
-
paymentHeader: null
|
|
121
|
-
});
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
try {
|
|
125
|
-
const verifyUrl = `${facilitatorUrl.replace(/\/+$/, "")}/verify`;
|
|
126
|
-
const verifyRes = await fetch(verifyUrl, {
|
|
127
|
-
method: "POST",
|
|
128
|
-
headers: {
|
|
129
|
-
"Content-Type": "application/json"
|
|
130
|
-
},
|
|
131
|
-
body: JSON.stringify({
|
|
132
|
-
x402Version: 1,
|
|
133
|
-
paymentHeader,
|
|
134
|
-
paymentRequirements
|
|
135
|
-
})
|
|
136
|
-
});
|
|
137
|
-
if (!verifyRes.ok) {
|
|
138
|
-
const text = await verifyRes.text().catch(() => "");
|
|
139
|
-
console.error(
|
|
140
|
-
"[x402-mantle-sdk] Facilitator /verify returned non-OK:",
|
|
141
|
-
verifyRes.status,
|
|
142
|
-
text
|
|
143
|
-
);
|
|
144
|
-
res.status(500).json({
|
|
145
|
-
error: "Payment verification error",
|
|
146
|
-
details: `Facilitator responded with HTTP ${verifyRes.status}`
|
|
147
|
-
});
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
const verifyJson = await verifyRes.json();
|
|
151
|
-
if (!verifyJson.isValid) {
|
|
152
|
-
res.status(402).json({
|
|
153
|
-
error: "Payment verification failed",
|
|
154
|
-
invalidReason: verifyJson.invalidReason ?? null,
|
|
155
|
-
paymentRequirements,
|
|
156
|
-
paymentHeader: null
|
|
157
|
-
});
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
if (onPaymentSettled) {
|
|
161
|
-
try {
|
|
162
|
-
const headerObj = decodePaymentHeader(paymentHeader);
|
|
163
|
-
const { authorization } = headerObj.payload;
|
|
164
|
-
const logEntry = {
|
|
165
|
-
id: authorization.nonce,
|
|
166
|
-
from: authorization.from,
|
|
167
|
-
to: authorization.to,
|
|
168
|
-
valueAtomic: authorization.value,
|
|
169
|
-
network,
|
|
170
|
-
asset: assetConfig.address,
|
|
171
|
-
route: routeKey,
|
|
172
|
-
timestamp: Date.now(),
|
|
173
|
-
facilitatorUrl,
|
|
174
|
-
// Pass from config closure
|
|
175
|
-
paymentRequirements
|
|
176
|
-
};
|
|
177
|
-
onPaymentSettled(logEntry);
|
|
178
|
-
if (telemetry) {
|
|
179
|
-
const event = createTelemetryEvent(logEntry, telemetry);
|
|
180
|
-
sendTelemetry(event, telemetry.endpoint).catch(
|
|
181
|
-
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
} catch (err) {
|
|
185
|
-
console.error(
|
|
186
|
-
"[x402-mantle-sdk] Error calling onPaymentSettled hook:",
|
|
187
|
-
err
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
next();
|
|
192
|
-
return;
|
|
193
|
-
} catch (err) {
|
|
194
|
-
console.error(
|
|
195
|
-
"[x402-mantle-sdk] Error while calling facilitator /verify:",
|
|
196
|
-
err
|
|
197
|
-
);
|
|
198
|
-
const message = err instanceof Error ? err.message : "Unknown verification error";
|
|
199
|
-
res.status(500).json({
|
|
200
|
-
error: "Payment verification error",
|
|
201
|
-
details: message
|
|
202
|
-
});
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// src/server/mantlePaywall.ts
|
|
209
|
-
function mantlePaywall(opts) {
|
|
210
|
-
const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
|
|
211
|
-
const priceUsdCents = Math.round(priceUsd * 100);
|
|
212
|
-
return async (req, res, next) => {
|
|
213
|
-
const method = (req.method || "GET").toUpperCase();
|
|
214
|
-
const path = req.path || "/";
|
|
215
|
-
const routeKey = `${method} ${path}`;
|
|
216
|
-
const middleware = createPaymentMiddleware({
|
|
217
|
-
facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
|
|
218
|
-
receiverAddress: payTo,
|
|
219
|
-
routes: {
|
|
220
|
-
[routeKey]: {
|
|
221
|
-
priceUsdCents,
|
|
222
|
-
network: MANTLE_DEFAULTS.NETWORK
|
|
223
|
-
}
|
|
224
|
-
},
|
|
225
|
-
telemetry,
|
|
226
|
-
onPaymentSettled
|
|
227
|
-
});
|
|
228
|
-
return middleware(req, res, next);
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export {
|
|
233
|
-
createPaymentMiddleware,
|
|
234
|
-
mantlePaywall
|
|
235
|
-
};
|