@getpara/evm-wallet-connectors 2.0.0-alpha.3 → 2.0.0-alpha.6
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.d.ts +1 -1
- package/dist/index.js +5 -952
- package/dist/package.json +3 -0
- package/dist/providers/EvmExternalWalletContext.d.ts +2 -0
- package/dist/providers/EvmExternalWalletContext.js +369 -0
- package/dist/providers/ParaEvmContext.js +81 -0
- package/dist/stores/useStore.js +22 -0
- package/dist/types/Wallet.js +1 -0
- package/dist/types/utils.d.ts +1 -1
- 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 +61 -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.d.ts +2 -1
- package/dist/wallets/connectors/index.js +28 -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.d.ts +2 -0
- package/dist/wallets/connectors/safe/safe.js +26 -0
- package/dist/wallets/connectors/safe/safeIcon.d.ts +1 -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 +11 -8
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
|
@@ -7,6 +7,7 @@ export type EvmExternalWalletContextType = {
|
|
|
7
7
|
chainId?: number;
|
|
8
8
|
username?: string;
|
|
9
9
|
avatar?: string;
|
|
10
|
+
balance?: string;
|
|
10
11
|
disconnect: () => Promise<void>;
|
|
11
12
|
switchChain: (chainId: number) => Promise<{
|
|
12
13
|
error?: string[];
|
|
@@ -24,6 +25,7 @@ export type EvmExternalWalletContextType = {
|
|
|
24
25
|
signature?: string;
|
|
25
26
|
error?: string;
|
|
26
27
|
}>;
|
|
28
|
+
getWalletBalance: () => Promise<string | undefined>;
|
|
27
29
|
};
|
|
28
30
|
export declare const EvmExternalWalletContext: import("react").Context<EvmExternalWalletContextType>;
|
|
29
31
|
export type EvmExternalWalletProviderConfig = {
|
|
@@ -0,0 +1,369 @@
|
|
|
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, useCallback, useEffect, useMemo, useRef } from "react";
|
|
9
|
+
import {
|
|
10
|
+
useAccount,
|
|
11
|
+
useSwitchChain,
|
|
12
|
+
useConnect,
|
|
13
|
+
useDisconnect,
|
|
14
|
+
useEnsName,
|
|
15
|
+
useEnsAvatar,
|
|
16
|
+
useSignMessage,
|
|
17
|
+
useSwitchAccount,
|
|
18
|
+
useConnections,
|
|
19
|
+
useBalance
|
|
20
|
+
} from "wagmi";
|
|
21
|
+
import { isEIP6963Connector } from "../utils/isEIP6963Connector.js";
|
|
22
|
+
import { getWalletConnectUri } from "../utils/getWalletConnectUri.js";
|
|
23
|
+
import { normalize } from "viem/ens";
|
|
24
|
+
import { useExternalWalletStore } from "../stores/useStore.js";
|
|
25
|
+
import { isMobile } from "@getpara/web-sdk";
|
|
26
|
+
import { etherUnits, formatUnits } from "viem";
|
|
27
|
+
const defaultEvmExternalWallet = {
|
|
28
|
+
wallets: [],
|
|
29
|
+
chains: [],
|
|
30
|
+
chainId: void 0,
|
|
31
|
+
username: void 0,
|
|
32
|
+
avatar: void 0,
|
|
33
|
+
balance: void 0,
|
|
34
|
+
disconnect: () => Promise.resolve(),
|
|
35
|
+
switchChain: () => Promise.resolve({}),
|
|
36
|
+
connectParaEmbedded: () => Promise.resolve({}),
|
|
37
|
+
signMessage: () => Promise.resolve({}),
|
|
38
|
+
signVerificationMessage: () => Promise.resolve({}),
|
|
39
|
+
getWalletBalance: () => Promise.resolve(void 0)
|
|
40
|
+
};
|
|
41
|
+
const EvmExternalWalletContext = createContext(defaultEvmExternalWallet);
|
|
42
|
+
function EvmExternalWalletProvider({
|
|
43
|
+
children,
|
|
44
|
+
onSwitchWallet,
|
|
45
|
+
para,
|
|
46
|
+
walletsWithFullAuth,
|
|
47
|
+
connectedWallet
|
|
48
|
+
}) {
|
|
49
|
+
const { connectAsync, connectors: untypedConnectors } = useConnect();
|
|
50
|
+
const connections = useConnections();
|
|
51
|
+
const {
|
|
52
|
+
address: wagmiAddress,
|
|
53
|
+
isConnecting,
|
|
54
|
+
isReconnecting,
|
|
55
|
+
chainId,
|
|
56
|
+
connector: connectedConnector,
|
|
57
|
+
isConnected
|
|
58
|
+
} = useAccount();
|
|
59
|
+
const { switchAccount: wagmiSwitchAccount } = useSwitchAccount();
|
|
60
|
+
const { chains, switchChainAsync } = useSwitchChain();
|
|
61
|
+
const { disconnectAsync } = useDisconnect();
|
|
62
|
+
const { data: ensName, refetch: refetchEnsName } = useEnsName({ address: wagmiAddress });
|
|
63
|
+
const { data: ensAvatar, refetch: refetchEnsAvatar } = useEnsAvatar({
|
|
64
|
+
name: normalize(ensName)
|
|
65
|
+
});
|
|
66
|
+
const { signMessageAsync } = useSignMessage();
|
|
67
|
+
const verificationMessage = useRef();
|
|
68
|
+
const { refetch: getBalance } = useBalance({ address: wagmiAddress });
|
|
69
|
+
const isLocalConnecting = useExternalWalletStore((state) => state.isConnecting);
|
|
70
|
+
const updateExternalWalletState = useExternalWalletStore((state) => state.updateState);
|
|
71
|
+
const getStoredExternalWallets = () => {
|
|
72
|
+
const storedExternalWalletsString = localStorage.getItem("@CAPSULE/externalWallets");
|
|
73
|
+
let storedExternalWallets = {};
|
|
74
|
+
if (storedExternalWalletsString) {
|
|
75
|
+
storedExternalWallets = JSON.parse(storedExternalWalletsString);
|
|
76
|
+
}
|
|
77
|
+
return storedExternalWallets;
|
|
78
|
+
};
|
|
79
|
+
const switchAccount = useCallback(
|
|
80
|
+
(connectorName) => {
|
|
81
|
+
var _a;
|
|
82
|
+
const connector = (_a = connections.find((c) => c.connector.name === connectorName)) == null ? void 0 : _a.connector;
|
|
83
|
+
if (!connector) {
|
|
84
|
+
console.warn(`connector not found: ${connectorName}`);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
wagmiSwitchAccount({ connector });
|
|
88
|
+
},
|
|
89
|
+
[connections, wagmiSwitchAccount]
|
|
90
|
+
);
|
|
91
|
+
const getWalletBalance = useCallback(
|
|
92
|
+
// Format from wei to eth
|
|
93
|
+
() => __async(this, null, function* () {
|
|
94
|
+
const { data: balance } = yield getBalance();
|
|
95
|
+
return balance ? formatUnits(balance.value, etherUnits.wei) : void 0;
|
|
96
|
+
}),
|
|
97
|
+
[chainId, wagmiAddress, getBalance]
|
|
98
|
+
);
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
const storedExternalWallet = getStoredExternalWallets()[wagmiAddress != null ? wagmiAddress : ""];
|
|
101
|
+
if (!isConnecting && !isReconnecting && !isLocalConnecting && !!wagmiAddress && !storedExternalWallet && (connectedConnector == null ? void 0 : connectedConnector.id) !== "para") {
|
|
102
|
+
reset();
|
|
103
|
+
}
|
|
104
|
+
}, [isConnecting, isReconnecting, isLocalConnecting, wagmiAddress, connectedConnector]);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
const storedExternalWallet = Object.values(para.externalWallets || {})[0];
|
|
107
|
+
if (!isLocalConnecting && !isConnecting && !isReconnecting && (storedExternalWallet == null ? void 0 : storedExternalWallet.type) === "EVM" && (storedExternalWallet == null ? void 0 : storedExternalWallet.address) !== wagmiAddress && (connectedConnector == null ? void 0 : connectedConnector.id) !== "para") {
|
|
108
|
+
switchWallet(wagmiAddress);
|
|
109
|
+
}
|
|
110
|
+
}, [isLocalConnecting, wagmiAddress, isReconnecting, isConnecting]);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
if (!isLocalConnecting && !isConnecting && !isReconnecting && connectedWallet && connectedConnector && connectedWallet.type === "EVM" && connectedConnector.name !== connectedWallet.name) {
|
|
113
|
+
switchAccount(connectedWallet.isExternal ? connectedWallet.name : "Para");
|
|
114
|
+
}
|
|
115
|
+
}, [isLocalConnecting, isConnecting, isReconnecting, connectedWallet, wagmiSwitchAccount]);
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (!isLocalConnecting && !isConnecting && !isReconnecting && !isConnected && !connectedConnector) {
|
|
118
|
+
if (Object.values(para.wallets).length === 0) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
connectParaEmbedded();
|
|
122
|
+
}
|
|
123
|
+
}, [isLocalConnecting, isConnecting, isReconnecting, isConnected, connectedConnector]);
|
|
124
|
+
const connectors = untypedConnectors;
|
|
125
|
+
const reset = () => __async(this, null, function* () {
|
|
126
|
+
yield disconnectAsync();
|
|
127
|
+
yield para.logout();
|
|
128
|
+
});
|
|
129
|
+
const signMessage = (message) => __async(this, null, function* () {
|
|
130
|
+
try {
|
|
131
|
+
const signature = yield signMessageAsync({
|
|
132
|
+
message,
|
|
133
|
+
account: wagmiAddress
|
|
134
|
+
});
|
|
135
|
+
return {
|
|
136
|
+
address: wagmiAddress,
|
|
137
|
+
signature
|
|
138
|
+
};
|
|
139
|
+
} catch (e) {
|
|
140
|
+
switch (e.name) {
|
|
141
|
+
case "UserRejectedRequestError": {
|
|
142
|
+
return { error: "Signature request rejected" };
|
|
143
|
+
}
|
|
144
|
+
default: {
|
|
145
|
+
return { error: "An unknown error occurred" };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
const signVerificationMessage = () => __async(this, null, function* () {
|
|
151
|
+
const signature = yield signMessage(verificationMessage.current);
|
|
152
|
+
return signature;
|
|
153
|
+
});
|
|
154
|
+
const switchChain = (chainId2) => __async(this, null, function* () {
|
|
155
|
+
var _a, _b, _c;
|
|
156
|
+
let error;
|
|
157
|
+
try {
|
|
158
|
+
yield switchChainAsync({ chainId: chainId2 });
|
|
159
|
+
} catch (e) {
|
|
160
|
+
if (e.details.includes("Missing or invalid.")) {
|
|
161
|
+
const chain = chains.find((c) => c.id === chainId2);
|
|
162
|
+
error = [
|
|
163
|
+
"Network not supported.",
|
|
164
|
+
`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.`
|
|
165
|
+
];
|
|
166
|
+
} else {
|
|
167
|
+
switch (e.name) {
|
|
168
|
+
case "UserRejectedRequestError": {
|
|
169
|
+
error = ["Change request rejected"];
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
default: {
|
|
173
|
+
error = ["An unknown error occurred"];
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return { error };
|
|
180
|
+
});
|
|
181
|
+
const login = (_0) => __async(this, [_0], function* ({
|
|
182
|
+
address,
|
|
183
|
+
walletId,
|
|
184
|
+
connectorName
|
|
185
|
+
}) {
|
|
186
|
+
var _a;
|
|
187
|
+
try {
|
|
188
|
+
refetchEnsName();
|
|
189
|
+
refetchEnsAvatar();
|
|
190
|
+
return yield para.loginExternalWallet({
|
|
191
|
+
externalWallet: {
|
|
192
|
+
address,
|
|
193
|
+
type: "EVM",
|
|
194
|
+
provider: connectorName,
|
|
195
|
+
withFullParaAuth: walletsWithFullAuth == null ? void 0 : walletsWithFullAuth.includes((_a = walletId == null ? void 0 : walletId.toUpperCase()) != null ? _a : ""),
|
|
196
|
+
ensName,
|
|
197
|
+
ensAvatar
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
} catch (err) {
|
|
201
|
+
yield disconnectAsync();
|
|
202
|
+
yield para.logout();
|
|
203
|
+
throw "Error logging you in. Please try again.";
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
const switchWallet = (address) => __async(this, null, function* () {
|
|
207
|
+
var _a;
|
|
208
|
+
updateExternalWalletState({ isConnecting: true });
|
|
209
|
+
let error;
|
|
210
|
+
if (!address) {
|
|
211
|
+
yield para.logout();
|
|
212
|
+
} else {
|
|
213
|
+
if (para.isExternalWalletAuth) {
|
|
214
|
+
yield reset();
|
|
215
|
+
} else {
|
|
216
|
+
try {
|
|
217
|
+
yield login({
|
|
218
|
+
address,
|
|
219
|
+
connectorName: connectedConnector == null ? void 0 : connectedConnector.name,
|
|
220
|
+
walletId: (_a = getParaDetails(connectedConnector.id)) == null ? void 0 : _a.id
|
|
221
|
+
});
|
|
222
|
+
} catch (err) {
|
|
223
|
+
error = err;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
onSwitchWallet == null ? void 0 : onSwitchWallet({ address, error });
|
|
228
|
+
updateExternalWalletState({ isConnecting: false });
|
|
229
|
+
});
|
|
230
|
+
const connect = (connector) => __async(this, null, function* () {
|
|
231
|
+
var _a, _b, _c, _d;
|
|
232
|
+
updateExternalWalletState({ isConnecting: true });
|
|
233
|
+
yield disconnectAsync();
|
|
234
|
+
const walletChainId = yield connector.getChainId();
|
|
235
|
+
let authState;
|
|
236
|
+
let address;
|
|
237
|
+
let error;
|
|
238
|
+
try {
|
|
239
|
+
const data = yield connectAsync({
|
|
240
|
+
// If the wallet is already on a supported chain, use that to avoid a chain switch prompt.
|
|
241
|
+
chainId: (_c = (_a = chains.find(({ id }) => id === walletChainId)) == null ? void 0 : _a.id) != null ? _c : (
|
|
242
|
+
// Fall back to the first chain provided.
|
|
243
|
+
(_b = chains[0]) == null ? void 0 : _b.id
|
|
244
|
+
),
|
|
245
|
+
connector
|
|
246
|
+
});
|
|
247
|
+
address = (_d = data.accounts) == null ? void 0 : _d[0];
|
|
248
|
+
if (address) {
|
|
249
|
+
try {
|
|
250
|
+
authState = yield login({ address, connectorName: connector.name, walletId: connector.paraDetails.id });
|
|
251
|
+
verificationMessage.current = authState.stage === "verify" ? authState.signatureVerificationMessage : void 0;
|
|
252
|
+
} catch (err) {
|
|
253
|
+
address = void 0;
|
|
254
|
+
error = err;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} catch (e) {
|
|
258
|
+
switch (e.name) {
|
|
259
|
+
case "UserRejectedRequestError": {
|
|
260
|
+
error = "Connection request rejected";
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
case "ResourceUnavailableRpcError": {
|
|
264
|
+
`${connector.name} not detected`;
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
default: {
|
|
268
|
+
error = "An unknown error occurred";
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
updateExternalWalletState({ isConnecting: false });
|
|
274
|
+
return { address, authState, error };
|
|
275
|
+
});
|
|
276
|
+
const connectMobile = (connector, isManualWalletConnect) => __async(this, null, function* () {
|
|
277
|
+
const _isMobile = isManualWalletConnect !== void 0 ? isManualWalletConnect : isMobile();
|
|
278
|
+
const _connector = connector.walletConnectModalConnector && _isMobile ? connector.walletConnectModalConnector : connector;
|
|
279
|
+
return yield connect(_connector);
|
|
280
|
+
});
|
|
281
|
+
const getQrUri = (connector) => () => {
|
|
282
|
+
var _a;
|
|
283
|
+
return getWalletConnectUri(connector, (_a = connector.paraDetails) == null ? void 0 : _a.getUri);
|
|
284
|
+
};
|
|
285
|
+
const nonEip6963ConnectorsByRdns = {};
|
|
286
|
+
let walletConnectModalConnector;
|
|
287
|
+
connectors.filter((c) => !isEIP6963Connector(c)).forEach((c) => {
|
|
288
|
+
if (c.paraDetails) {
|
|
289
|
+
nonEip6963ConnectorsByRdns[c.paraDetails.rdns] = c.paraDetails;
|
|
290
|
+
if (c.paraDetails.isWalletConnectModalConnector) {
|
|
291
|
+
walletConnectModalConnector = c;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
const eip6963ids = connectors.filter((c) => isEIP6963Connector(c)).map((c) => c.id);
|
|
296
|
+
const dedupedConnectors = connectors.map((c) => {
|
|
297
|
+
var _a, _b, _c;
|
|
298
|
+
if ((_a = c.paraDetails) == null ? void 0 : _a.isWalletConnectModalConnector) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (!isEIP6963Connector(c) && eip6963ids.includes((_b = c.paraDetails) == null ? void 0 : _b.rdns)) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (isEIP6963Connector(c)) {
|
|
305
|
+
const paraMetadata = nonEip6963ConnectorsByRdns[c.id];
|
|
306
|
+
return __spreadProps(__spreadValues({}, c), { paraDetails: paraMetadata });
|
|
307
|
+
}
|
|
308
|
+
if (((_c = c.paraDetails) == null ? void 0 : _c.id) === "walletConnect" && walletConnectModalConnector) {
|
|
309
|
+
return __spreadProps(__spreadValues({}, c), { walletConnectModalConnector });
|
|
310
|
+
}
|
|
311
|
+
return c;
|
|
312
|
+
}).filter((c) => !!c);
|
|
313
|
+
const wallets = dedupedConnectors.map((c) => {
|
|
314
|
+
const connector = __spreadValues(__spreadValues({}, c), c.paraDetails);
|
|
315
|
+
return __spreadProps(__spreadValues({}, connector), {
|
|
316
|
+
connect: () => connect(connector),
|
|
317
|
+
connectMobile: (isManualWalletConnect) => connectMobile(connector, isManualWalletConnect),
|
|
318
|
+
type: "EVM",
|
|
319
|
+
getQrUri: getQrUri(connector)
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
const getParaDetails = (id) => {
|
|
323
|
+
var _a;
|
|
324
|
+
return (_a = connectors.find((w) => w.id === id)) == null ? void 0 : _a.paraDetails;
|
|
325
|
+
};
|
|
326
|
+
const formattedChains = chains.map((c) => {
|
|
327
|
+
return {
|
|
328
|
+
id: c.id,
|
|
329
|
+
name: c.name
|
|
330
|
+
};
|
|
331
|
+
});
|
|
332
|
+
const username = useMemo(() => ensName != null ? ensName : wagmiAddress, [ensName, wagmiAddress]);
|
|
333
|
+
const connectParaEmbedded = useCallback(() => __async(this, null, function* () {
|
|
334
|
+
const paraConnectorInstance = connectors.find((c) => c.id === "para");
|
|
335
|
+
if (!paraConnectorInstance) {
|
|
336
|
+
return { error: "No para connector instance" };
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
const result = yield connectAsync({ connector: paraConnectorInstance });
|
|
340
|
+
return { result };
|
|
341
|
+
} catch (err) {
|
|
342
|
+
const error = err instanceof Error ? err.message : "Unknown error";
|
|
343
|
+
return { error };
|
|
344
|
+
}
|
|
345
|
+
}), [connectors]);
|
|
346
|
+
return /* @__PURE__ */ jsx(
|
|
347
|
+
EvmExternalWalletContext.Provider,
|
|
348
|
+
{
|
|
349
|
+
value: {
|
|
350
|
+
wallets,
|
|
351
|
+
chains: formattedChains,
|
|
352
|
+
chainId,
|
|
353
|
+
username,
|
|
354
|
+
avatar: ensAvatar,
|
|
355
|
+
disconnect: disconnectAsync,
|
|
356
|
+
switchChain,
|
|
357
|
+
connectParaEmbedded,
|
|
358
|
+
signMessage,
|
|
359
|
+
signVerificationMessage,
|
|
360
|
+
getWalletBalance
|
|
361
|
+
},
|
|
362
|
+
children
|
|
363
|
+
}
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
export {
|
|
367
|
+
EvmExternalWalletContext,
|
|
368
|
+
EvmExternalWalletProvider
|
|
369
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__objRest,
|
|
4
|
+
__spreadProps,
|
|
5
|
+
__spreadValues
|
|
6
|
+
} from "../chunk-MMUBH76A.js";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
import { useMemo } from "react";
|
|
9
|
+
import { createConfig, WagmiProvider } from "wagmi";
|
|
10
|
+
import { connectorsForWallets } from "../wallets/connectorsForWallets.js";
|
|
11
|
+
import { http } from "viem";
|
|
12
|
+
import { computeWalletConnectMetaData } from "../utils/computeWalletConnectMetaData.js";
|
|
13
|
+
import { EvmExternalWalletProvider } from "./EvmExternalWalletContext.js";
|
|
14
|
+
import { paraConnector } from "@getpara/wagmi-v2-connector";
|
|
15
|
+
const createDefaultTransports = (chains) => {
|
|
16
|
+
const transportsObject = chains.reduce((acc, chain) => {
|
|
17
|
+
const key = chain.id;
|
|
18
|
+
acc[key] = http();
|
|
19
|
+
return acc;
|
|
20
|
+
}, {});
|
|
21
|
+
return transportsObject;
|
|
22
|
+
};
|
|
23
|
+
function ParaEvmProvider({
|
|
24
|
+
children,
|
|
25
|
+
internalConfig,
|
|
26
|
+
config: _config,
|
|
27
|
+
wagmiProviderProps
|
|
28
|
+
}) {
|
|
29
|
+
const para = internalConfig.para;
|
|
30
|
+
const _a = _config, {
|
|
31
|
+
projectId,
|
|
32
|
+
appName,
|
|
33
|
+
appDescription,
|
|
34
|
+
appIcon,
|
|
35
|
+
appUrl,
|
|
36
|
+
wallets,
|
|
37
|
+
chains,
|
|
38
|
+
transports,
|
|
39
|
+
paraConnectorOptions
|
|
40
|
+
} = _a, wagmiConfigParams = __objRest(_a, [
|
|
41
|
+
"projectId",
|
|
42
|
+
"appName",
|
|
43
|
+
"appDescription",
|
|
44
|
+
"appIcon",
|
|
45
|
+
"appUrl",
|
|
46
|
+
"wallets",
|
|
47
|
+
"chains",
|
|
48
|
+
"transports",
|
|
49
|
+
"paraConnectorOptions"
|
|
50
|
+
]);
|
|
51
|
+
const paraConnectorInstance = useMemo(() => {
|
|
52
|
+
return paraConnector({
|
|
53
|
+
para,
|
|
54
|
+
chains: [...chains],
|
|
55
|
+
disableModal: true,
|
|
56
|
+
appName,
|
|
57
|
+
options: paraConnectorOptions != null ? paraConnectorOptions : {}
|
|
58
|
+
});
|
|
59
|
+
}, [para]);
|
|
60
|
+
const config = useMemo(() => {
|
|
61
|
+
const wcMetadata = computeWalletConnectMetaData({ appName, appDescription, appUrl, appIcon });
|
|
62
|
+
const baseConnectors = connectorsForWallets(wallets, {
|
|
63
|
+
projectId,
|
|
64
|
+
appName,
|
|
65
|
+
appDescription,
|
|
66
|
+
appUrl,
|
|
67
|
+
appIcon,
|
|
68
|
+
walletConnectParameters: { metadata: wcMetadata }
|
|
69
|
+
});
|
|
70
|
+
const allConnectors = [...baseConnectors, paraConnectorInstance];
|
|
71
|
+
return createConfig(__spreadProps(__spreadValues({}, wagmiConfigParams), {
|
|
72
|
+
chains,
|
|
73
|
+
transports: transports || createDefaultTransports(chains),
|
|
74
|
+
connectors: allConnectors
|
|
75
|
+
}));
|
|
76
|
+
}, [wallets, paraConnectorInstance]);
|
|
77
|
+
return /* @__PURE__ */ jsx(WagmiProvider, __spreadProps(__spreadValues({ config }, wagmiProviderProps), { children: /* @__PURE__ */ jsx(EvmExternalWalletProvider, __spreadProps(__spreadValues({}, internalConfig), { children })) }));
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
ParaEvmProvider
|
|
81
|
+
};
|
|
@@ -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";
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type Mutable<type extends object> = {
|
|
|
9
9
|
};
|
|
10
10
|
/** Strict version of built-in Omit type */
|
|
11
11
|
export type Omit<type, keys extends keyof type> = Pick<type, Exclude<keyof type, keys>>;
|
|
12
|
-
export type WalletProviderFlags = 'isApexWallet' | 'isAvalanche' | 'isBackpack' | 'isBifrost' | 'isBitKeep' | 'isBitski' | 'isBlockWallet' | 'isBraveWallet' | 'isCoinbaseWallet' | 'isDawn' | 'isEnkrypt' | 'isExodus' | 'isFrame' | 'isFrontier' | 'isGamestop' | 'isHyperPay' | 'isImToken' | 'isKuCoinWallet' | 'isMathWallet' | 'isMetaMask' | 'isNestWallet' | 'isOkxWallet' | 'isOKExWallet' | 'isOneInchAndroidWallet' | 'isOneInchIOSWallet' | 'isOpera' | 'isPhantom' | 'isPortal' | 'isRabby' | 'isRainbow' | 'isStatus' | 'isTally' | 'isTokenPocket' | 'isTokenary' | 'isTrust' | 'isTrustWallet' | 'isXDEFI' | 'isZerion' | 'isTalisman' | 'isZeal' | 'isCoin98' | 'isMEWwallet' | 'isSafeheron' | 'isSafePal' | '__seif';
|
|
12
|
+
export type WalletProviderFlags = 'isApexWallet' | 'isAvalanche' | 'isBackpack' | 'isBifrost' | 'isBitKeep' | 'isBitski' | 'isBlockWallet' | 'isBraveWallet' | 'isCoinbaseWallet' | 'isDawn' | 'isEnkrypt' | 'isExodus' | 'isFrame' | 'isFrontier' | 'isGamestop' | 'isHyperPay' | 'isImToken' | 'isKuCoinWallet' | 'isMathWallet' | 'isMetaMask' | 'isNestWallet' | 'isOkxWallet' | 'isOKExWallet' | 'isOneInchAndroidWallet' | 'isOneInchIOSWallet' | 'isOpera' | 'isPhantom' | 'isPortal' | 'isRabby' | 'isRainbow' | 'isSafe' | 'isStatus' | 'isTally' | 'isTokenPocket' | 'isTokenary' | 'isTrust' | 'isTrustWallet' | 'isXDEFI' | 'isZerion' | 'isTalisman' | 'isZeal' | 'isCoin98' | 'isMEWwallet' | 'isSafeheron' | 'isSafePal' | '__seif';
|
|
13
13
|
export type WalletProvider = Evaluate<EIP1193Provider & {
|
|
14
14
|
[key in WalletProviderFlags]?: true | undefined;
|
|
15
15
|
} & {
|
|
@@ -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,61 @@
|
|
|
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
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
return (walletDetails) => createWalletConnectConnector({
|
|
54
|
+
projectId,
|
|
55
|
+
walletDetails,
|
|
56
|
+
walletConnectParameters
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
getWalletConnectConnector
|
|
61
|
+
};
|
|
@@ -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
|
+
};
|