@getpara/evm-wallet-connectors 1.7.0 → 1.8.0
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-MMUBH76A.js +59 -0
- package/dist/global.d.js +1 -0
- package/dist/index.js +5 -993
- package/dist/package.json +1 -2
- package/dist/providers/EvmExternalWalletContext.d.ts +2 -0
- package/dist/providers/EvmExternalWalletContext.js +345 -0
- package/dist/providers/ParaEvmContext.js +115 -0
- package/dist/stores/useStore.js +22 -0
- package/dist/types/Wallet.js +1 -0
- package/dist/types/utils.js +1 -0
- package/dist/utils/computeWalletConnectMetaData.js +18 -0
- package/dist/utils/getInjectedConnector.js +69 -0
- package/dist/utils/getWalletConnectConnector.js +63 -0
- package/dist/utils/getWalletConnectUri.js +22 -0
- package/dist/utils/isEIP6963Connector.js +9 -0
- package/dist/utils/omitUndefinedValues.js +11 -0
- package/dist/utils/uniqueBy.js +14 -0
- package/dist/wallets/connectors/coinbase/coinbase.js +35 -0
- package/dist/wallets/connectors/coinbase/coinbaseIcon.js +6 -0
- package/dist/wallets/connectors/index.js +18 -0
- package/dist/wallets/connectors/metaMask/metaMask.js +84 -0
- package/dist/wallets/connectors/metaMask/metaMaskIcon.js +6 -0
- package/dist/wallets/connectors/rabby/rabby.js +25 -0
- package/dist/wallets/connectors/rabby/rabbyIcon.js +6 -0
- package/dist/wallets/connectors/rainbow/rainbow.js +30 -0
- package/dist/wallets/connectors/rainbow/rainbowIcon.js +6 -0
- package/dist/wallets/connectors/safe/safe.js +26 -0
- package/dist/wallets/connectors/safe/safeIcon.js +6 -0
- package/dist/wallets/connectors/walletConnect/walletConnect.js +22 -0
- package/dist/wallets/connectors/walletConnect/walletConnectIcon.js +6 -0
- package/dist/wallets/connectors/zerion/zerion.js +36 -0
- package/dist/wallets/connectors/zerion/zerionIcon.js +6 -0
- package/dist/wallets/connectorsForWallets.js +73 -0
- package/dist/window.d.js +1 -0
- package/package.json +6 -6
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
package/dist/package.json
CHANGED
|
@@ -6,6 +6,7 @@ export declare const defaultEvmExternalWallet: {
|
|
|
6
6
|
chainId: any;
|
|
7
7
|
username: any;
|
|
8
8
|
avatar: any;
|
|
9
|
+
balance: any;
|
|
9
10
|
disconnect: () => Promise<void>;
|
|
10
11
|
switchChain: () => Promise<{}>;
|
|
11
12
|
signMessage: () => Promise<{}>;
|
|
@@ -17,6 +18,7 @@ export declare const EvmExternalWalletContext: import("react").Context<{
|
|
|
17
18
|
chainId: number;
|
|
18
19
|
username: string;
|
|
19
20
|
avatar?: string;
|
|
21
|
+
balance?: string;
|
|
20
22
|
disconnect: () => Promise<void>;
|
|
21
23
|
switchChain: (chainId: number) => Promise<{
|
|
22
24
|
error?: string[];
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async,
|
|
4
|
+
__spreadProps,
|
|
5
|
+
__spreadValues
|
|
6
|
+
} from "../chunk-MMUBH76A.js";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
import { createContext, useEffect, useMemo, useRef } from "react";
|
|
9
|
+
import {
|
|
10
|
+
useAccount,
|
|
11
|
+
useSwitchChain,
|
|
12
|
+
useConnect,
|
|
13
|
+
useDisconnect,
|
|
14
|
+
useEnsName,
|
|
15
|
+
useEnsAvatar,
|
|
16
|
+
useSwitchAccount,
|
|
17
|
+
useConnections,
|
|
18
|
+
useSignMessage,
|
|
19
|
+
useBalance
|
|
20
|
+
} from "wagmi";
|
|
21
|
+
import { isEIP6963Connector } from "../utils/isEIP6963Connector.js";
|
|
22
|
+
import { getWalletConnectUri } from "../utils/getWalletConnectUri.js";
|
|
23
|
+
import {
|
|
24
|
+
isMobile,
|
|
25
|
+
useExternalWalletProviderStore,
|
|
26
|
+
useWalletState,
|
|
27
|
+
WalletType
|
|
28
|
+
} from "@getpara/react-sdk";
|
|
29
|
+
import { normalize } from "viem/ens";
|
|
30
|
+
import { useExternalWalletStore } from "../stores/useStore.js";
|
|
31
|
+
import { etherUnits, formatUnits } from "viem";
|
|
32
|
+
const defaultEvmExternalWallet = {
|
|
33
|
+
wallets: [],
|
|
34
|
+
chains: [],
|
|
35
|
+
chainId: void 0,
|
|
36
|
+
username: void 0,
|
|
37
|
+
avatar: void 0,
|
|
38
|
+
balance: void 0,
|
|
39
|
+
disconnect: () => Promise.resolve(),
|
|
40
|
+
switchChain: () => Promise.resolve({}),
|
|
41
|
+
signMessage: () => Promise.resolve({}),
|
|
42
|
+
signVerificationMessage: () => Promise.resolve({})
|
|
43
|
+
};
|
|
44
|
+
const EvmExternalWalletContext = createContext(defaultEvmExternalWallet);
|
|
45
|
+
function EvmExternalWalletProvider({ children, para, onSwitchWallet }) {
|
|
46
|
+
const { connectAsync, connectors: untypedConnectors } = useConnect();
|
|
47
|
+
const connections = useConnections();
|
|
48
|
+
const { address: wagmiAddress, isConnecting, isReconnecting, chainId, connector: connectedConnector } = useAccount();
|
|
49
|
+
const { chains, switchChainAsync } = useSwitchChain();
|
|
50
|
+
const { disconnectAsync } = useDisconnect();
|
|
51
|
+
const { switchAccountAsync } = useSwitchAccount();
|
|
52
|
+
const { signMessageAsync } = useSignMessage();
|
|
53
|
+
const { data: ensName, refetch: refetchEnsName } = useEnsName({ address: wagmiAddress });
|
|
54
|
+
const { data: ensAvatar, refetch: refetchEnsAvatar } = useEnsAvatar({
|
|
55
|
+
name: normalize(ensName)
|
|
56
|
+
});
|
|
57
|
+
const { data: wagmiBalance, isLoading: isBalanceLoading } = useBalance({ address: wagmiAddress });
|
|
58
|
+
const fullAuthWallets = useExternalWalletProviderStore((state) => state.fullAuthWallets);
|
|
59
|
+
const { selectedWallet } = useWalletState();
|
|
60
|
+
const isLocalConnecting = useExternalWalletStore((state) => state.isConnecting);
|
|
61
|
+
const updateExternalWalletState = useExternalWalletStore((state) => state.updateState);
|
|
62
|
+
const verificationMessage = useRef();
|
|
63
|
+
const getStoredExternalWallets = () => {
|
|
64
|
+
const storedExternalWalletsString = localStorage.getItem("@CAPSULE/externalWallets");
|
|
65
|
+
let storedExternalWallets = {};
|
|
66
|
+
if (storedExternalWalletsString) {
|
|
67
|
+
storedExternalWallets = JSON.parse(storedExternalWalletsString);
|
|
68
|
+
}
|
|
69
|
+
return storedExternalWallets;
|
|
70
|
+
};
|
|
71
|
+
const balance = useMemo(
|
|
72
|
+
// Format from wei to eth
|
|
73
|
+
() => isBalanceLoading || wagmiBalance === void 0 ? void 0 : formatUnits(wagmiBalance.value, etherUnits.wei),
|
|
74
|
+
[wagmiBalance, isBalanceLoading]
|
|
75
|
+
);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const storedExternalWallet = getStoredExternalWallets()[wagmiAddress != null ? wagmiAddress : ""];
|
|
78
|
+
if (!isConnecting && !isReconnecting && !isLocalConnecting && !!wagmiAddress && !storedExternalWallet && (connectedConnector == null ? void 0 : connectedConnector.id) !== "para") {
|
|
79
|
+
reset();
|
|
80
|
+
}
|
|
81
|
+
}, [isConnecting, isReconnecting, isLocalConnecting, wagmiAddress, connectedConnector]);
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const storedExternalWallet = Object.values(para.externalWallets || {})[0];
|
|
84
|
+
if (!isLocalConnecting && !isConnecting && !isReconnecting && (storedExternalWallet == null ? void 0 : storedExternalWallet.type) === WalletType.EVM && (storedExternalWallet == null ? void 0 : storedExternalWallet.address) !== wagmiAddress && (connectedConnector == null ? void 0 : connectedConnector.id) !== "para") {
|
|
85
|
+
switchWallet(wagmiAddress);
|
|
86
|
+
}
|
|
87
|
+
}, [isLocalConnecting, wagmiAddress, isReconnecting, isConnecting]);
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
const wallet = para.findWallet(selectedWallet.id, selectedWallet.type);
|
|
90
|
+
if (wallet && connectedConnector && wallet.type === WalletType.EVM && (connectedConnector.id === "para" && wallet.isExternal || connectedConnector.id !== "para" && !wallet.isExternal)) {
|
|
91
|
+
switchAccount(wallet.isExternal ? wallet.name : "Para");
|
|
92
|
+
}
|
|
93
|
+
}, [selectedWallet]);
|
|
94
|
+
const connectors = untypedConnectors;
|
|
95
|
+
const reset = () => __async(this, null, function* () {
|
|
96
|
+
yield disconnectAsync();
|
|
97
|
+
yield para.logout();
|
|
98
|
+
});
|
|
99
|
+
const signMessage = (message) => __async(this, null, function* () {
|
|
100
|
+
try {
|
|
101
|
+
const signature = yield signMessageAsync({
|
|
102
|
+
message,
|
|
103
|
+
account: wagmiAddress
|
|
104
|
+
});
|
|
105
|
+
return { address: wagmiAddress, signature };
|
|
106
|
+
} catch (e) {
|
|
107
|
+
switch (e.name) {
|
|
108
|
+
case "UserRejectedRequestError": {
|
|
109
|
+
return { error: "Signature request rejected" };
|
|
110
|
+
}
|
|
111
|
+
default: {
|
|
112
|
+
return { error: "An unknown error occurred" };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
const signVerificationMessage = () => __async(this, null, function* () {
|
|
118
|
+
const signature = yield signMessage(verificationMessage.current);
|
|
119
|
+
return signature;
|
|
120
|
+
});
|
|
121
|
+
const switchAccount = (connectorName) => __async(this, null, function* () {
|
|
122
|
+
var _a;
|
|
123
|
+
const connector = (_a = connections.find((c) => c.connector.name === connectorName)) == null ? void 0 : _a.connector;
|
|
124
|
+
if (!connector) {
|
|
125
|
+
throw new Error(`connector not found: ${connectorName}`);
|
|
126
|
+
}
|
|
127
|
+
yield switchAccountAsync({ connector });
|
|
128
|
+
});
|
|
129
|
+
const switchChain = (chainId2) => __async(this, null, function* () {
|
|
130
|
+
var _a, _b, _c;
|
|
131
|
+
let error;
|
|
132
|
+
try {
|
|
133
|
+
yield switchChainAsync({ chainId: chainId2 });
|
|
134
|
+
} catch (e) {
|
|
135
|
+
if (e.details.includes("Missing or invalid.")) {
|
|
136
|
+
const chain = chains.find((c) => c.id === chainId2);
|
|
137
|
+
error = [
|
|
138
|
+
"Network not supported.",
|
|
139
|
+
`You may need to add ${chain == null ? void 0 : chain.name} support to ${(_c = (_b = (_a = connectedConnector == null ? void 0 : connectedConnector.paraDetails) == null ? void 0 : _a.name) != null ? _b : connectedConnector == null ? void 0 : connectedConnector.name) != null ? _c : "the wallet"} manually.`
|
|
140
|
+
];
|
|
141
|
+
} else {
|
|
142
|
+
switch (e.name) {
|
|
143
|
+
case "UserRejectedRequestError": {
|
|
144
|
+
error = ["Change request rejected"];
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
default: {
|
|
148
|
+
error = ["An unknown error occurred"];
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return { error };
|
|
155
|
+
});
|
|
156
|
+
const login = (_0) => __async(this, [_0], function* ({
|
|
157
|
+
address,
|
|
158
|
+
walletId,
|
|
159
|
+
connectorName
|
|
160
|
+
}) {
|
|
161
|
+
var _a;
|
|
162
|
+
try {
|
|
163
|
+
refetchEnsName();
|
|
164
|
+
refetchEnsAvatar();
|
|
165
|
+
return yield para.externalWalletLogin({
|
|
166
|
+
address,
|
|
167
|
+
type: WalletType.EVM,
|
|
168
|
+
provider: connectorName,
|
|
169
|
+
withFullParaAuth: fullAuthWallets == null ? void 0 : fullAuthWallets.includes((_a = walletId == null ? void 0 : walletId.toUpperCase()) != null ? _a : "")
|
|
170
|
+
});
|
|
171
|
+
} catch (err) {
|
|
172
|
+
yield disconnectAsync();
|
|
173
|
+
yield para.logout();
|
|
174
|
+
throw "Error logging you in. Please try again.";
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
const switchWallet = (address) => __async(this, null, function* () {
|
|
178
|
+
var _a;
|
|
179
|
+
updateExternalWalletState({ isConnecting: true });
|
|
180
|
+
let error;
|
|
181
|
+
if (!address) {
|
|
182
|
+
yield para.logout();
|
|
183
|
+
} else {
|
|
184
|
+
if (para.isExternalWalletAuth) {
|
|
185
|
+
yield reset();
|
|
186
|
+
} else {
|
|
187
|
+
try {
|
|
188
|
+
yield login({
|
|
189
|
+
address,
|
|
190
|
+
connectorName: connectedConnector == null ? void 0 : connectedConnector.name,
|
|
191
|
+
walletId: (_a = getParaDetails(connectedConnector.id)) == null ? void 0 : _a.id
|
|
192
|
+
});
|
|
193
|
+
} catch (err) {
|
|
194
|
+
error = err;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
onSwitchWallet({ address, error });
|
|
199
|
+
updateExternalWalletState({ isConnecting: false });
|
|
200
|
+
});
|
|
201
|
+
const connect = (connector) => __async(this, null, function* () {
|
|
202
|
+
var _a, _b, _c, _d;
|
|
203
|
+
updateExternalWalletState({ isConnecting: true });
|
|
204
|
+
yield disconnectAsync();
|
|
205
|
+
const walletChainId = yield connector.getChainId();
|
|
206
|
+
let address;
|
|
207
|
+
let error;
|
|
208
|
+
let userExists = false;
|
|
209
|
+
let isVerified = false;
|
|
210
|
+
try {
|
|
211
|
+
const data = yield connectAsync({
|
|
212
|
+
// If the wallet is already on a supported chain, use that to avoid a chain switch prompt.
|
|
213
|
+
chainId: (_c = (_a = chains.find(({ id }) => id === walletChainId)) == null ? void 0 : _a.id) != null ? _c : (
|
|
214
|
+
// Fall back to the first chain provided.
|
|
215
|
+
(_b = chains[0]) == null ? void 0 : _b.id
|
|
216
|
+
),
|
|
217
|
+
connector
|
|
218
|
+
});
|
|
219
|
+
address = (_d = data.accounts) == null ? void 0 : _d[0];
|
|
220
|
+
if (address) {
|
|
221
|
+
try {
|
|
222
|
+
const loginResp = yield login({ address, connectorName: connector.name, walletId: connector.paraDetails.id });
|
|
223
|
+
userExists = loginResp.userExists;
|
|
224
|
+
isVerified = loginResp.isVerified;
|
|
225
|
+
verificationMessage.current = loginResp.signatureVerificationMessage;
|
|
226
|
+
} catch (err) {
|
|
227
|
+
address = void 0;
|
|
228
|
+
error = err;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
} catch (e) {
|
|
232
|
+
switch (e.name) {
|
|
233
|
+
case "UserRejectedRequestError": {
|
|
234
|
+
error = "Connection request rejected";
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
case "ResourceUnavailableRpcError": {
|
|
238
|
+
`${connector.name} not detected`;
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
default: {
|
|
242
|
+
error = "An unknown error occurred";
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
updateExternalWalletState({ isConnecting: false });
|
|
248
|
+
return { address, error, userExists, isVerified };
|
|
249
|
+
});
|
|
250
|
+
const connectMobile = (connector, isManualWalletConnect) => __async(this, null, function* () {
|
|
251
|
+
const _isMobile = isManualWalletConnect !== void 0 ? isManualWalletConnect : isMobile();
|
|
252
|
+
const _connector = connector.walletConnectModalConnector && _isMobile ? connector.walletConnectModalConnector : connector;
|
|
253
|
+
return yield connect(_connector);
|
|
254
|
+
});
|
|
255
|
+
const getQrUri = (connector) => () => {
|
|
256
|
+
var _a;
|
|
257
|
+
return getWalletConnectUri(connector, (_a = connector.paraDetails) == null ? void 0 : _a.getUri);
|
|
258
|
+
};
|
|
259
|
+
const nonEip6963ConnectorsByRdns = {};
|
|
260
|
+
let walletConnectModalConnector;
|
|
261
|
+
connectors.filter((c) => !isEIP6963Connector(c)).forEach((c) => {
|
|
262
|
+
if (c.paraDetails) {
|
|
263
|
+
nonEip6963ConnectorsByRdns[c.paraDetails.rdns] = c.paraDetails;
|
|
264
|
+
if (c.paraDetails.isWalletConnectModalConnector) {
|
|
265
|
+
walletConnectModalConnector = c;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
const eip6963ids = connectors.filter((c) => isEIP6963Connector(c)).map((c) => c.id);
|
|
270
|
+
const dedupedConnectors = connectors.map((c) => {
|
|
271
|
+
var _a, _b, _c;
|
|
272
|
+
if ((_a = c.paraDetails) == null ? void 0 : _a.isWalletConnectModalConnector) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (!isEIP6963Connector(c) && eip6963ids.includes((_b = c.paraDetails) == null ? void 0 : _b.rdns)) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (isEIP6963Connector(c)) {
|
|
279
|
+
const paraMetadata = nonEip6963ConnectorsByRdns[c.id];
|
|
280
|
+
return __spreadProps(__spreadValues({}, c), { paraDetails: paraMetadata });
|
|
281
|
+
}
|
|
282
|
+
if (((_c = c.paraDetails) == null ? void 0 : _c.id) === "walletConnect" && walletConnectModalConnector) {
|
|
283
|
+
return __spreadProps(__spreadValues({}, c), { walletConnectModalConnector });
|
|
284
|
+
}
|
|
285
|
+
return c;
|
|
286
|
+
}).filter((c) => !!c);
|
|
287
|
+
const wallets = dedupedConnectors.map((c) => {
|
|
288
|
+
const connector = __spreadValues(__spreadValues({}, c), c.paraDetails);
|
|
289
|
+
return __spreadProps(__spreadValues({}, connector), {
|
|
290
|
+
connect: () => connect(connector),
|
|
291
|
+
connectMobile: ({ isManualWalletConnect }) => connectMobile(connector, isManualWalletConnect),
|
|
292
|
+
type: WalletType.EVM,
|
|
293
|
+
getQrUri: getQrUri(connector)
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
const getParaDetails = (id) => {
|
|
297
|
+
var _a;
|
|
298
|
+
return (_a = connectors.find((w) => w.id === id)) == null ? void 0 : _a.paraDetails;
|
|
299
|
+
};
|
|
300
|
+
const formattedChains = chains.map((c) => {
|
|
301
|
+
return {
|
|
302
|
+
id: c.id,
|
|
303
|
+
name: c.name
|
|
304
|
+
};
|
|
305
|
+
});
|
|
306
|
+
const username = useMemo(() => ensName != null ? ensName : wagmiAddress, [ensName, wagmiAddress]);
|
|
307
|
+
const disconnect = disconnectAsync;
|
|
308
|
+
return /* @__PURE__ */ jsx(
|
|
309
|
+
EvmExternalWalletContext.Provider,
|
|
310
|
+
{
|
|
311
|
+
value: useMemo(
|
|
312
|
+
() => ({
|
|
313
|
+
wallets,
|
|
314
|
+
chains: formattedChains,
|
|
315
|
+
chainId,
|
|
316
|
+
username,
|
|
317
|
+
avatar: ensAvatar,
|
|
318
|
+
balance,
|
|
319
|
+
disconnect,
|
|
320
|
+
switchChain,
|
|
321
|
+
signMessage,
|
|
322
|
+
signVerificationMessage
|
|
323
|
+
}),
|
|
324
|
+
[
|
|
325
|
+
wallets,
|
|
326
|
+
formattedChains,
|
|
327
|
+
chainId,
|
|
328
|
+
username,
|
|
329
|
+
ensAvatar,
|
|
330
|
+
balance,
|
|
331
|
+
disconnect,
|
|
332
|
+
switchChain,
|
|
333
|
+
signMessage,
|
|
334
|
+
signVerificationMessage
|
|
335
|
+
]
|
|
336
|
+
),
|
|
337
|
+
children
|
|
338
|
+
}
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
export {
|
|
342
|
+
EvmExternalWalletContext,
|
|
343
|
+
EvmExternalWalletProvider,
|
|
344
|
+
defaultEvmExternalWallet
|
|
345
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async,
|
|
4
|
+
__objRest,
|
|
5
|
+
__spreadProps,
|
|
6
|
+
__spreadValues
|
|
7
|
+
} from "../chunk-MMUBH76A.js";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
10
|
+
import { createConfig, WagmiProvider } from "wagmi";
|
|
11
|
+
import { connectorsForWallets } from "../wallets/connectorsForWallets.js";
|
|
12
|
+
import { http } from "viem";
|
|
13
|
+
import { computeWalletConnectMetaData } from "../utils/computeWalletConnectMetaData.js";
|
|
14
|
+
import { EvmExternalWalletContext, EvmExternalWalletProvider } from "./EvmExternalWalletContext.js";
|
|
15
|
+
import { useClient, useExternalWalletProviderStore } from "@getpara/react-sdk";
|
|
16
|
+
import { connect } from "wagmi/actions";
|
|
17
|
+
import { paraConnector } from "@getpara/wagmi-v2-integration";
|
|
18
|
+
const createDefaultTransports = (chains) => {
|
|
19
|
+
const transportsObject = chains.reduce((acc, chain) => {
|
|
20
|
+
const key = chain.id;
|
|
21
|
+
acc[key] = http();
|
|
22
|
+
return acc;
|
|
23
|
+
}, {});
|
|
24
|
+
return transportsObject;
|
|
25
|
+
};
|
|
26
|
+
function ParaEvmProvider(_a) {
|
|
27
|
+
var _b = _a, { children, config: _config } = _b, wagmiProviderProps = __objRest(_b, ["children", "config"]);
|
|
28
|
+
var _a2;
|
|
29
|
+
const updateExternalWalletProviderState = useExternalWalletProviderStore((state) => state.updateState);
|
|
30
|
+
const EvmProvider = useExternalWalletProviderStore((state) => state.EvmProvider);
|
|
31
|
+
const evmContext = useExternalWalletProviderStore((state) => state.evmContext);
|
|
32
|
+
const para = (_a2 = _config.para) != null ? _a2 : useClient();
|
|
33
|
+
const _b2 = _config, {
|
|
34
|
+
projectId,
|
|
35
|
+
appName,
|
|
36
|
+
appDescription,
|
|
37
|
+
appIcon,
|
|
38
|
+
appUrl,
|
|
39
|
+
wallets,
|
|
40
|
+
chains,
|
|
41
|
+
transports,
|
|
42
|
+
paraDisableModal,
|
|
43
|
+
paraOptions
|
|
44
|
+
} = _b2, wagmiConfigParams = __objRest(_b2, [
|
|
45
|
+
"projectId",
|
|
46
|
+
"appName",
|
|
47
|
+
"appDescription",
|
|
48
|
+
"appIcon",
|
|
49
|
+
"appUrl",
|
|
50
|
+
"wallets",
|
|
51
|
+
"chains",
|
|
52
|
+
"transports",
|
|
53
|
+
"paraDisableModal",
|
|
54
|
+
"paraOptions"
|
|
55
|
+
]);
|
|
56
|
+
const wcMetadata = computeWalletConnectMetaData({ appName, appDescription, appUrl, appIcon });
|
|
57
|
+
const paraConnectorInstance = para ? paraConnector({
|
|
58
|
+
para,
|
|
59
|
+
chains: [...chains],
|
|
60
|
+
disableModal: paraDisableModal != null ? paraDisableModal : true,
|
|
61
|
+
appName,
|
|
62
|
+
options: paraOptions != null ? paraOptions : {}
|
|
63
|
+
}) : void 0;
|
|
64
|
+
const allConnectors = useMemo(() => {
|
|
65
|
+
const baseConnectors = connectorsForWallets(wallets, {
|
|
66
|
+
projectId,
|
|
67
|
+
appName,
|
|
68
|
+
appDescription,
|
|
69
|
+
appUrl,
|
|
70
|
+
appIcon,
|
|
71
|
+
walletConnectParameters: { metadata: wcMetadata }
|
|
72
|
+
});
|
|
73
|
+
return paraConnectorInstance ? [...baseConnectors, paraConnectorInstance] : baseConnectors;
|
|
74
|
+
}, [wallets, projectId, appName, appDescription, appUrl, appIcon, wcMetadata, paraConnectorInstance]);
|
|
75
|
+
const config = useMemo(
|
|
76
|
+
() => createConfig(__spreadProps(__spreadValues({}, wagmiConfigParams), {
|
|
77
|
+
chains,
|
|
78
|
+
transports: transports || createDefaultTransports(chains),
|
|
79
|
+
connectors: allConnectors
|
|
80
|
+
})),
|
|
81
|
+
[wagmiConfigParams, chains, transports, allConnectors]
|
|
82
|
+
);
|
|
83
|
+
const connectParaEvmWallet = useCallback(() => __async(this, null, function* () {
|
|
84
|
+
if (!paraConnectorInstance) {
|
|
85
|
+
return { error: "No para connector instance" };
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const result = yield connect(config, { connector: paraConnectorInstance });
|
|
89
|
+
return { result };
|
|
90
|
+
} catch (err) {
|
|
91
|
+
const error = err instanceof Error ? err.message : "Unknown error";
|
|
92
|
+
return { error };
|
|
93
|
+
}
|
|
94
|
+
}), [paraConnectorInstance, config, connect]);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!evmContext || !EvmProvider) {
|
|
97
|
+
updateExternalWalletProviderState({
|
|
98
|
+
EvmProvider: EvmExternalWalletProvider,
|
|
99
|
+
evmContext: EvmExternalWalletContext
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}, []);
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
updateExternalWalletProviderState({
|
|
105
|
+
connectParaEvmWallet: paraConnectorInstance ? connectParaEvmWallet : void 0
|
|
106
|
+
});
|
|
107
|
+
}, [paraConnectorInstance]);
|
|
108
|
+
if (!evmContext || !EvmProvider) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
return /* @__PURE__ */ jsx(WagmiProvider, __spreadProps(__spreadValues({ config }, wagmiProviderProps), { children }));
|
|
112
|
+
}
|
|
113
|
+
export {
|
|
114
|
+
ParaEvmProvider
|
|
115
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__spreadValues
|
|
4
|
+
} from "../chunk-MMUBH76A.js";
|
|
5
|
+
import { create } from "zustand";
|
|
6
|
+
import { syncTabs } from "zustand-sync-tabs";
|
|
7
|
+
const useExternalWalletStore = create(
|
|
8
|
+
syncTabs(
|
|
9
|
+
(set) => ({
|
|
10
|
+
isConnecting: false,
|
|
11
|
+
updateState: (state) => {
|
|
12
|
+
set(__spreadValues({}, state));
|
|
13
|
+
}
|
|
14
|
+
}),
|
|
15
|
+
{
|
|
16
|
+
name: "para-evm-external-wallet-state"
|
|
17
|
+
}
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
export {
|
|
21
|
+
useExternalWalletStore
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import "../chunk-MMUBH76A.js";
|
|
3
|
+
const computeWalletConnectMetaData = ({
|
|
4
|
+
appName,
|
|
5
|
+
appDescription,
|
|
6
|
+
appUrl,
|
|
7
|
+
appIcon
|
|
8
|
+
}) => {
|
|
9
|
+
return {
|
|
10
|
+
name: appName,
|
|
11
|
+
description: appDescription != null ? appDescription : appName,
|
|
12
|
+
url: appUrl != null ? appUrl : typeof window !== "undefined" ? window.location.href : "",
|
|
13
|
+
icons: [...appIcon ? [appIcon] : []]
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
computeWalletConnectMetaData
|
|
18
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__spreadValues
|
|
4
|
+
} from "../chunk-MMUBH76A.js";
|
|
5
|
+
import { createConnector } from "wagmi";
|
|
6
|
+
import { injected } from "wagmi/connectors";
|
|
7
|
+
function getExplicitInjectedProvider(flag) {
|
|
8
|
+
const _window = typeof window !== "undefined" ? window : void 0;
|
|
9
|
+
if (typeof _window === "undefined" || typeof _window.ethereum === "undefined") return;
|
|
10
|
+
const providers = _window.ethereum.providers;
|
|
11
|
+
return providers ? providers.find((provider) => provider[flag]) : _window.ethereum[flag] ? _window.ethereum : void 0;
|
|
12
|
+
}
|
|
13
|
+
function getWindowProviderNamespace(namespace) {
|
|
14
|
+
const providerSearch = (provider, namespace2) => {
|
|
15
|
+
const [property, ...path] = namespace2.split(".");
|
|
16
|
+
const _provider = provider[property];
|
|
17
|
+
if (_provider) {
|
|
18
|
+
if (path.length === 0) return _provider;
|
|
19
|
+
return providerSearch(_provider, path.join("."));
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
if (typeof window !== "undefined") return providerSearch(window, namespace);
|
|
23
|
+
}
|
|
24
|
+
function hasInjectedProvider({ flag, namespace }) {
|
|
25
|
+
if (namespace && typeof getWindowProviderNamespace(namespace) !== "undefined") return true;
|
|
26
|
+
if (flag && typeof getExplicitInjectedProvider(flag) !== "undefined") return true;
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
function getInjectedProvider({ flag, namespace }) {
|
|
30
|
+
var _a;
|
|
31
|
+
const _window = typeof window !== "undefined" ? window : void 0;
|
|
32
|
+
if (typeof _window === "undefined") return;
|
|
33
|
+
if (namespace) {
|
|
34
|
+
const windowProvider = getWindowProviderNamespace(namespace);
|
|
35
|
+
if (windowProvider) return windowProvider;
|
|
36
|
+
}
|
|
37
|
+
const providers = (_a = _window.ethereum) == null ? void 0 : _a.providers;
|
|
38
|
+
if (flag) {
|
|
39
|
+
const provider = getExplicitInjectedProvider(flag);
|
|
40
|
+
if (provider) return provider;
|
|
41
|
+
}
|
|
42
|
+
if (typeof providers !== "undefined" && providers.length > 0) return providers[0];
|
|
43
|
+
return _window.ethereum;
|
|
44
|
+
}
|
|
45
|
+
function createInjectedConnector(provider) {
|
|
46
|
+
return (walletDetails) => {
|
|
47
|
+
const injectedConfig = provider ? {
|
|
48
|
+
target: () => ({
|
|
49
|
+
id: walletDetails.paraDetails.id,
|
|
50
|
+
name: walletDetails.paraDetails.name,
|
|
51
|
+
provider
|
|
52
|
+
})
|
|
53
|
+
} : {};
|
|
54
|
+
return createConnector((config) => __spreadValues(__spreadValues({}, injected(injectedConfig)(config)), walletDetails));
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function getInjectedConnector({
|
|
58
|
+
flag,
|
|
59
|
+
namespace,
|
|
60
|
+
target
|
|
61
|
+
}) {
|
|
62
|
+
const provider = target ? target : getInjectedProvider({ flag, namespace });
|
|
63
|
+
return createInjectedConnector(provider);
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
getInjectedConnector,
|
|
67
|
+
getInjectedProvider,
|
|
68
|
+
hasInjectedProvider
|
|
69
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__spreadProps,
|
|
4
|
+
__spreadValues
|
|
5
|
+
} from "../chunk-MMUBH76A.js";
|
|
6
|
+
import { createConnector } from "wagmi";
|
|
7
|
+
import { walletConnect } from "wagmi/connectors";
|
|
8
|
+
const walletConnectInstances = /* @__PURE__ */ new Map();
|
|
9
|
+
const getOrCreateWalletConnectInstance = ({
|
|
10
|
+
projectId,
|
|
11
|
+
walletConnectParameters,
|
|
12
|
+
paraDetailsShowQrModal
|
|
13
|
+
}) => {
|
|
14
|
+
let config = __spreadProps(__spreadValues({}, walletConnectParameters ? walletConnectParameters : {}), {
|
|
15
|
+
projectId,
|
|
16
|
+
showQrModal: false
|
|
17
|
+
// Required. Otherwise WalletConnect modal (Web3Modal) will popup during time of connection for a wallet
|
|
18
|
+
});
|
|
19
|
+
if (paraDetailsShowQrModal) {
|
|
20
|
+
config = __spreadProps(__spreadValues({}, config), { showQrModal: true });
|
|
21
|
+
}
|
|
22
|
+
const serializedConfig = JSON.stringify(config);
|
|
23
|
+
const sharedWalletConnector = walletConnectInstances.get(serializedConfig);
|
|
24
|
+
if (sharedWalletConnector) {
|
|
25
|
+
return sharedWalletConnector;
|
|
26
|
+
}
|
|
27
|
+
const newWalletConnectInstance = walletConnect(config);
|
|
28
|
+
walletConnectInstances.set(serializedConfig, newWalletConnectInstance);
|
|
29
|
+
return newWalletConnectInstance;
|
|
30
|
+
};
|
|
31
|
+
function createWalletConnectConnector({
|
|
32
|
+
projectId,
|
|
33
|
+
walletDetails,
|
|
34
|
+
walletConnectParameters
|
|
35
|
+
}) {
|
|
36
|
+
return createConnector((config) => __spreadProps(__spreadValues(__spreadValues({}, getOrCreateWalletConnectInstance({
|
|
37
|
+
projectId,
|
|
38
|
+
walletConnectParameters,
|
|
39
|
+
// Used in `connectorsForWallets` to add another
|
|
40
|
+
// walletConnect wallet into Para with modal popup option
|
|
41
|
+
paraDetailsShowQrModal: walletDetails.paraDetails.showQrModal
|
|
42
|
+
})(config)), walletDetails), {
|
|
43
|
+
id: walletDetails.paraDetails.id
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
function getWalletConnectConnector({
|
|
47
|
+
projectId,
|
|
48
|
+
walletConnectParameters
|
|
49
|
+
}) {
|
|
50
|
+
if (!projectId || projectId === "") {
|
|
51
|
+
throw new Error(
|
|
52
|
+
"No projectId found. Every dApp must now provide a WalletConnect Cloud projectId to enable WalletConnect v2. Sign up for your free key at https://cloud.walletconnect.com/sign-in"
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return (walletDetails) => createWalletConnectConnector({
|
|
56
|
+
projectId,
|
|
57
|
+
walletDetails,
|
|
58
|
+
walletConnectParameters
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
getWalletConnectConnector
|
|
63
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async
|
|
4
|
+
} from "../chunk-MMUBH76A.js";
|
|
5
|
+
const getWalletConnectUri = (connector, uriConverter) => __async(void 0, null, function* () {
|
|
6
|
+
const provider = yield connector.getProvider();
|
|
7
|
+
if (connector.type === "coinbaseWallet") {
|
|
8
|
+
return provider.qrUrl;
|
|
9
|
+
}
|
|
10
|
+
return new Promise(
|
|
11
|
+
(resolve) => (
|
|
12
|
+
// Wagmi v2 doesn't have a return type for provider yet
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
provider.once("display_uri", (uri) => {
|
|
15
|
+
resolve(uriConverter ? uriConverter(uri) : uri);
|
|
16
|
+
})
|
|
17
|
+
)
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
export {
|
|
21
|
+
getWalletConnectUri
|
|
22
|
+
};
|