@getpara/evm-wallet-connectors 2.0.0-dev.1 → 2.0.0-dev.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-MMUBH76A.js +59 -0
- package/dist/global.d.js +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +8 -949
- package/dist/package.json +3 -0
- package/dist/providers/EvmExternalWalletContext.d.ts +6 -34
- package/dist/providers/EvmExternalWalletContext.js +457 -0
- package/dist/providers/ParaEvmContext.d.ts +2 -1
- package/dist/providers/ParaEvmContext.js +123 -0
- package/dist/providers/createParaWagmiConfig.d.ts +10 -0
- package/dist/providers/createParaWagmiConfig.js +65 -0
- package/dist/providers/externalHooks.d.ts +7 -0
- package/dist/providers/externalHooks.js +7 -0
- package/dist/stores/useStore.js +22 -0
- package/dist/stores/wagmiConfigStore.d.ts +14 -0
- package/dist/stores/wagmiConfigStore.js +16 -0
- package/dist/types/Wallet.d.ts +3 -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 +25 -0
- package/dist/utils/isEIP6963Connector.js +9 -0
- package/dist/utils/omitUndefinedValues.js +11 -0
- package/dist/utils/resolveWalletList.d.ts +3 -0
- package/dist/utils/resolveWalletList.js +16 -0
- package/dist/utils/uniqueBy.js +14 -0
- package/dist/wallets/connectors/backpack/backpack.d.ts +3 -0
- package/dist/wallets/connectors/backpack/backpack.js +31 -0
- package/dist/wallets/connectors/backpack/backpackIcon.d.ts +1 -0
- package/dist/wallets/connectors/backpack/backpackIcon.js +6 -0
- package/dist/wallets/connectors/coinbase/coinbase.js +36 -0
- package/dist/wallets/connectors/coinbase/coinbaseIcon.js +6 -0
- package/dist/wallets/connectors/farcaster/farcaster.d.ts +2 -0
- package/dist/wallets/connectors/farcaster/farcaster.js +22 -0
- package/dist/wallets/connectors/farcaster/farcasterIcon.d.ts +1 -0
- package/dist/wallets/connectors/farcaster/farcasterIcon.js +6 -0
- package/dist/wallets/connectors/haha/haha.d.ts +3 -0
- package/dist/wallets/connectors/haha/haha.js +31 -0
- package/dist/wallets/connectors/haha/hahaIcon.d.ts +1 -0
- package/dist/wallets/connectors/haha/hahaIcon.js +6 -0
- package/dist/wallets/connectors/index.d.ts +8 -1
- package/dist/wallets/connectors/index.js +45 -0
- package/dist/wallets/connectors/metaMask/metaMask.js +85 -0
- package/dist/wallets/connectors/metaMask/metaMaskIcon.d.ts +1 -1
- package/dist/wallets/connectors/metaMask/metaMaskIcon.js +6 -0
- package/dist/wallets/connectors/okx/okx.d.ts +3 -0
- package/dist/wallets/connectors/okx/okx.js +32 -0
- package/dist/wallets/connectors/okx/okxIcon.d.ts +1 -0
- package/dist/wallets/connectors/okx/okxIcon.js +6 -0
- package/dist/wallets/connectors/phantom/phantom.d.ts +3 -0
- package/dist/wallets/connectors/phantom/phantom.js +31 -0
- package/dist/wallets/connectors/phantom/phantomIcon.d.ts +1 -0
- package/dist/wallets/connectors/phantom/phantomIcon.js +6 -0
- package/dist/wallets/connectors/rabby/rabby.js +26 -0
- package/dist/wallets/connectors/rabby/rabbyIcon.js +6 -0
- package/dist/wallets/connectors/rainbow/rainbow.js +31 -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 +27 -0
- package/dist/wallets/connectors/safe/safeIcon.d.ts +1 -0
- package/dist/wallets/connectors/safe/safeIcon.js +6 -0
- package/dist/wallets/connectors/valora/valora.d.ts +3 -0
- package/dist/wallets/connectors/valora/valora.js +29 -0
- package/dist/wallets/connectors/valora/valoraIcon.d.ts +1 -0
- package/dist/wallets/connectors/valora/valoraIcon.js +6 -0
- package/dist/wallets/connectors/walletConnect/walletConnect.js +23 -0
- package/dist/wallets/connectors/walletConnect/walletConnectIcon.js +6 -0
- package/dist/wallets/connectors/zerion/zerion.js +37 -0
- package/dist/wallets/connectors/zerion/zerionIcon.js +6 -0
- package/dist/wallets/connectorsForWallets.d.ts +4 -1
- package/dist/wallets/connectorsForWallets.js +84 -0
- package/dist/window.d.js +1 -0
- package/package.json +33 -23
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
|
@@ -1,38 +1,10 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
|
-
import type
|
|
3
|
-
import
|
|
4
|
-
export type EvmExternalWalletContextType = {
|
|
5
|
-
wallets: CommonWallet[];
|
|
6
|
-
chains: CommonChain[];
|
|
7
|
-
chainId?: number;
|
|
2
|
+
import { FarcasterMiniAppManagement, type BalanceManagement, type ChainManagement, type ConnectParaEmbedded, type ExternalWalletContextType, type ExternalWalletProviderConfigBase } from '@getpara/react-common';
|
|
3
|
+
import { TExternalHooks } from './externalHooks.js';
|
|
4
|
+
export type EvmExternalWalletContextType = ExternalWalletContextType & ChainManagement<number> & BalanceManagement & ConnectParaEmbedded & TExternalHooks & {
|
|
8
5
|
username?: string;
|
|
9
6
|
avatar?: string;
|
|
10
|
-
|
|
11
|
-
switchChain: (chainId: number) => Promise<{
|
|
12
|
-
error?: string[];
|
|
13
|
-
}>;
|
|
14
|
-
connectParaEmbedded: () => Promise<{
|
|
15
|
-
result?: unknown;
|
|
16
|
-
error?: string;
|
|
17
|
-
}>;
|
|
18
|
-
signMessage: (message: string) => Promise<{
|
|
19
|
-
signature?: string;
|
|
20
|
-
error?: string;
|
|
21
|
-
}>;
|
|
22
|
-
signVerificationMessage: () => Promise<{
|
|
23
|
-
address?: string;
|
|
24
|
-
signature?: string;
|
|
25
|
-
error?: string;
|
|
26
|
-
}>;
|
|
27
|
-
};
|
|
7
|
+
} & FarcasterMiniAppManagement;
|
|
28
8
|
export declare const EvmExternalWalletContext: import("react").Context<EvmExternalWalletContextType>;
|
|
29
|
-
export type EvmExternalWalletProviderConfig =
|
|
30
|
-
|
|
31
|
-
address?: string;
|
|
32
|
-
error?: string;
|
|
33
|
-
}) => void;
|
|
34
|
-
para: ParaWeb;
|
|
35
|
-
walletsWithFullAuth: TExternalWallet[];
|
|
36
|
-
connectedWallet?: Omit<Wallet, 'signer'> | null;
|
|
37
|
-
};
|
|
38
|
-
export declare function EvmExternalWalletProvider({ children, onSwitchWallet, para, walletsWithFullAuth, connectedWallet, }: EvmExternalWalletProviderConfig & PropsWithChildren): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export type EvmExternalWalletProviderConfig = ExternalWalletProviderConfigBase;
|
|
10
|
+
export declare function EvmExternalWalletProvider({ children, onSwitchWallet, para, walletsWithFullAuth, connectedWallet, includeWalletVerification, connectionOnly, }: EvmExternalWalletProviderConfig & PropsWithChildren): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,457 @@
|
|
|
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 {
|
|
26
|
+
defaultEvmExternalWallet
|
|
27
|
+
} from "@getpara/react-common";
|
|
28
|
+
import { isMobile } from "@getpara/web-sdk";
|
|
29
|
+
import { etherUnits, formatUnits } from "viem";
|
|
30
|
+
import { externalHooks } from "./externalHooks.js";
|
|
31
|
+
const EvmExternalWalletContext = createContext(defaultEvmExternalWallet);
|
|
32
|
+
function EvmExternalWalletProvider({
|
|
33
|
+
children,
|
|
34
|
+
onSwitchWallet,
|
|
35
|
+
para,
|
|
36
|
+
walletsWithFullAuth,
|
|
37
|
+
connectedWallet,
|
|
38
|
+
includeWalletVerification,
|
|
39
|
+
connectionOnly
|
|
40
|
+
}) {
|
|
41
|
+
const { connectAsync, connectors: untypedConnectors } = useConnect();
|
|
42
|
+
const connections = useConnections();
|
|
43
|
+
const {
|
|
44
|
+
address: wagmiAddress,
|
|
45
|
+
isConnecting,
|
|
46
|
+
isReconnecting,
|
|
47
|
+
chainId,
|
|
48
|
+
connector: connectedConnector,
|
|
49
|
+
isConnected
|
|
50
|
+
} = useAccount();
|
|
51
|
+
const { switchAccount: wagmiSwitchAccount } = useSwitchAccount();
|
|
52
|
+
const { chains, switchChainAsync } = useSwitchChain();
|
|
53
|
+
const { disconnectAsync } = useDisconnect();
|
|
54
|
+
const { data: ensName, refetch: refetchEnsName } = useEnsName({ address: wagmiAddress });
|
|
55
|
+
const { data: ensAvatar, refetch: refetchEnsAvatar } = useEnsAvatar({
|
|
56
|
+
name: normalize(ensName)
|
|
57
|
+
});
|
|
58
|
+
const { signMessageAsync } = useSignMessage();
|
|
59
|
+
const isLinkingAccount = useRef(false);
|
|
60
|
+
const verificationMessage = useRef();
|
|
61
|
+
const { refetch: getBalance } = useBalance({ address: wagmiAddress });
|
|
62
|
+
const connectors = untypedConnectors;
|
|
63
|
+
const connectionsRef = useRef(connections);
|
|
64
|
+
const connectorsRef = useRef(connectors);
|
|
65
|
+
const isLocalConnecting = useExternalWalletStore((state) => state.isConnecting);
|
|
66
|
+
const updateExternalWalletState = useExternalWalletStore((state) => state.updateState);
|
|
67
|
+
const getStoredExternalWallets = () => {
|
|
68
|
+
const storedExternalWalletsString = localStorage.getItem("@CAPSULE/externalWallets");
|
|
69
|
+
let storedExternalWallets = {};
|
|
70
|
+
if (storedExternalWalletsString) {
|
|
71
|
+
storedExternalWallets = JSON.parse(storedExternalWalletsString);
|
|
72
|
+
}
|
|
73
|
+
return storedExternalWallets;
|
|
74
|
+
};
|
|
75
|
+
const switchAccount = useCallback(
|
|
76
|
+
(connectorName) => {
|
|
77
|
+
var _a;
|
|
78
|
+
const connector = (_a = connections.find((c) => c.connector.name === connectorName)) == null ? void 0 : _a.connector;
|
|
79
|
+
if (!connector) {
|
|
80
|
+
console.warn(`connector not found: ${connectorName}`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
wagmiSwitchAccount({ connector });
|
|
84
|
+
},
|
|
85
|
+
[connections, wagmiSwitchAccount]
|
|
86
|
+
);
|
|
87
|
+
const findConnectorAndAccount = (externalWallet) => {
|
|
88
|
+
var _a;
|
|
89
|
+
let connector;
|
|
90
|
+
switch (true) {
|
|
91
|
+
case !!externalWallet.providerId:
|
|
92
|
+
{
|
|
93
|
+
connector = (_a = connectionsRef.current.find(
|
|
94
|
+
(c) => {
|
|
95
|
+
var _a2;
|
|
96
|
+
return ((_a2 = c.connector.paraDetails) == null ? void 0 : _a2.internalId) === externalWallet.providerId;
|
|
97
|
+
}
|
|
98
|
+
)) == null ? void 0 : _a.connector;
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
return { connector, account: externalWallet.address };
|
|
103
|
+
};
|
|
104
|
+
const getWalletBalance = useCallback(
|
|
105
|
+
// Format from wei to eth
|
|
106
|
+
() => __async(this, null, function* () {
|
|
107
|
+
const { data: balance } = yield getBalance();
|
|
108
|
+
return balance ? formatUnits(balance.value, etherUnits.wei) : void 0;
|
|
109
|
+
}),
|
|
110
|
+
[chainId, wagmiAddress, getBalance]
|
|
111
|
+
);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
const storedExternalWallet = getStoredExternalWallets()[wagmiAddress != null ? wagmiAddress : ""];
|
|
114
|
+
if (!isConnecting && !isReconnecting && !isLocalConnecting && !!wagmiAddress && !storedExternalWallet && (connectedConnector == null ? void 0 : connectedConnector.id) !== "para" && !isLinkingAccount.current && para.isReady && !para.isFarcasterMiniApp) {
|
|
115
|
+
reset();
|
|
116
|
+
}
|
|
117
|
+
}, [isConnected, isLocalConnecting, wagmiAddress, connectedConnector, para.isReady, para.isFarcasterMiniApp]);
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
const storedExternalWallet = Object.values(para.externalWallets || {})[0];
|
|
120
|
+
if (!isLocalConnecting && isConnected && (storedExternalWallet == null ? void 0 : storedExternalWallet.type) === "EVM" && (storedExternalWallet == null ? void 0 : storedExternalWallet.address) !== wagmiAddress && (connectedConnector == null ? void 0 : connectedConnector.id) !== "para" && !isLinkingAccount.current) {
|
|
121
|
+
switchWallet(wagmiAddress);
|
|
122
|
+
}
|
|
123
|
+
}, [isLocalConnecting, wagmiAddress, isConnected]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
if (!isLocalConnecting && !isConnecting && !isReconnecting && connectedWallet && connectedConnector && connectedWallet.type === "EVM" && connectedConnector.name !== connectedWallet.name) {
|
|
126
|
+
switchAccount(connectedWallet.isExternal ? connectedWallet.name : "Para");
|
|
127
|
+
}
|
|
128
|
+
}, [isLocalConnecting, isConnecting, isReconnecting, connectedWallet, wagmiSwitchAccount]);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
if (!isLocalConnecting && !isConnecting && !isReconnecting && !isConnected && !connectedConnector) {
|
|
131
|
+
if (Object.values(para.wallets).length === 0) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
connectParaEmbedded();
|
|
135
|
+
}
|
|
136
|
+
}, [isLocalConnecting, isConnecting, isReconnecting, isConnected, connectedConnector]);
|
|
137
|
+
const reset = () => __async(this, null, function* () {
|
|
138
|
+
yield disconnectAsync();
|
|
139
|
+
yield para.logout();
|
|
140
|
+
});
|
|
141
|
+
const signMessage = (_0) => __async(this, [_0], function* ({ message, externalWallet }) {
|
|
142
|
+
let signOpts = {};
|
|
143
|
+
if (externalWallet) {
|
|
144
|
+
signOpts = findConnectorAndAccount(externalWallet);
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
const address = signOpts.account ? typeof signOpts.account === "string" ? signOpts.account : signOpts.account.getAddress() : wagmiAddress;
|
|
148
|
+
const signature = yield signMessageAsync(__spreadValues({
|
|
149
|
+
message,
|
|
150
|
+
account: address
|
|
151
|
+
}, signOpts));
|
|
152
|
+
return {
|
|
153
|
+
address,
|
|
154
|
+
signature
|
|
155
|
+
};
|
|
156
|
+
} catch (e) {
|
|
157
|
+
console.error("Error signing message:", e);
|
|
158
|
+
switch (e.name) {
|
|
159
|
+
case "UserRejectedRequestError": {
|
|
160
|
+
return { error: "Signature request rejected" };
|
|
161
|
+
}
|
|
162
|
+
default: {
|
|
163
|
+
return { error: "An unknown error occurred" };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
const signVerificationMessage = () => __async(this, null, function* () {
|
|
169
|
+
const signature = yield signMessage({ message: verificationMessage.current });
|
|
170
|
+
return signature;
|
|
171
|
+
});
|
|
172
|
+
const switchChain = (chainId2) => __async(this, null, function* () {
|
|
173
|
+
var _a, _b, _c;
|
|
174
|
+
let error;
|
|
175
|
+
try {
|
|
176
|
+
yield switchChainAsync({ chainId: chainId2 });
|
|
177
|
+
} catch (e) {
|
|
178
|
+
if (e.details.includes("Missing or invalid.")) {
|
|
179
|
+
const chain = chains.find((c) => c.id === chainId2);
|
|
180
|
+
error = [
|
|
181
|
+
"Network not supported.",
|
|
182
|
+
`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.`
|
|
183
|
+
];
|
|
184
|
+
} else {
|
|
185
|
+
switch (e.name) {
|
|
186
|
+
case "UserRejectedRequestError": {
|
|
187
|
+
error = ["Change request rejected"];
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
default: {
|
|
191
|
+
error = ["An unknown error occurred"];
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { error };
|
|
198
|
+
});
|
|
199
|
+
const login = (_0) => __async(this, [_0], function* ({ address, withFullParaAuth = false, providerId, provider }) {
|
|
200
|
+
try {
|
|
201
|
+
refetchEnsName();
|
|
202
|
+
refetchEnsAvatar();
|
|
203
|
+
return yield para.loginExternalWallet({
|
|
204
|
+
externalWallet: {
|
|
205
|
+
address,
|
|
206
|
+
type: "EVM",
|
|
207
|
+
provider,
|
|
208
|
+
providerId,
|
|
209
|
+
withFullParaAuth,
|
|
210
|
+
ensName,
|
|
211
|
+
ensAvatar,
|
|
212
|
+
isConnectionOnly: connectionOnly,
|
|
213
|
+
withVerification: includeWalletVerification
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
} catch (err) {
|
|
217
|
+
yield disconnectAsync();
|
|
218
|
+
yield para.logout();
|
|
219
|
+
throw "Error logging you in. Please try again.";
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
const switchWallet = (address) => __async(this, null, function* () {
|
|
223
|
+
updateExternalWalletState({ isConnecting: true });
|
|
224
|
+
let error;
|
|
225
|
+
if (!address) {
|
|
226
|
+
yield para.logout();
|
|
227
|
+
} else {
|
|
228
|
+
if (para.isExternalWalletAuth || para.isExternalWalletWithVerification) {
|
|
229
|
+
yield reset();
|
|
230
|
+
} else {
|
|
231
|
+
try {
|
|
232
|
+
const loginInfo = getConnectorInfo(connectedConnector);
|
|
233
|
+
yield login(__spreadValues({
|
|
234
|
+
address
|
|
235
|
+
}, loginInfo));
|
|
236
|
+
} catch (err) {
|
|
237
|
+
error = err;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
onSwitchWallet == null ? void 0 : onSwitchWallet({ address, error });
|
|
242
|
+
updateExternalWalletState({ isConnecting: false });
|
|
243
|
+
});
|
|
244
|
+
const connectBase = (connector) => __async(this, null, function* () {
|
|
245
|
+
var _a, _b, _c, _d;
|
|
246
|
+
const walletChainId = yield connector.getChainId();
|
|
247
|
+
const data = yield connectAsync({
|
|
248
|
+
// If the wallet is already on a supported chain, use that to avoid a chain switch prompt.
|
|
249
|
+
chainId: (_c = (_a = chains.find(({ id }) => id === walletChainId)) == null ? void 0 : _a.id) != null ? _c : (
|
|
250
|
+
// Fall back to the first chain provided.
|
|
251
|
+
(_b = chains[0]) == null ? void 0 : _b.id
|
|
252
|
+
),
|
|
253
|
+
connector
|
|
254
|
+
});
|
|
255
|
+
return (_d = data.accounts) == null ? void 0 : _d[0];
|
|
256
|
+
});
|
|
257
|
+
const connect = (connector) => __async(this, null, function* () {
|
|
258
|
+
updateExternalWalletState({ isConnecting: true });
|
|
259
|
+
yield disconnectAsync();
|
|
260
|
+
let authState;
|
|
261
|
+
let address;
|
|
262
|
+
let error;
|
|
263
|
+
try {
|
|
264
|
+
address = yield connectBase(connector);
|
|
265
|
+
if (address) {
|
|
266
|
+
try {
|
|
267
|
+
const loginInfo = getConnectorInfo(connector);
|
|
268
|
+
authState = yield login(__spreadValues({
|
|
269
|
+
address
|
|
270
|
+
}, loginInfo));
|
|
271
|
+
verificationMessage.current = authState.stage === "verify" ? authState.signatureVerificationMessage : void 0;
|
|
272
|
+
} catch (err) {
|
|
273
|
+
address = void 0;
|
|
274
|
+
error = err;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
} catch (e) {
|
|
278
|
+
switch (e.name) {
|
|
279
|
+
case "UserRejectedRequestError": {
|
|
280
|
+
error = "Connection request rejected";
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case "ResourceUnavailableRpcError": {
|
|
284
|
+
error = `${connector.name} not detected`;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
default: {
|
|
288
|
+
console.error("Wagmi connection error:", e.message);
|
|
289
|
+
error = "An unknown error occurred";
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
updateExternalWalletState({ isConnecting: false });
|
|
295
|
+
return { address, authState, error };
|
|
296
|
+
});
|
|
297
|
+
const connectMobile = (connector, isManualWalletConnect) => __async(this, null, function* () {
|
|
298
|
+
const _isMobile = isManualWalletConnect !== void 0 ? isManualWalletConnect : isMobile();
|
|
299
|
+
const _connector = connector.walletConnectModalConnector && _isMobile ? connector.walletConnectModalConnector : connector;
|
|
300
|
+
return yield connect(_connector);
|
|
301
|
+
});
|
|
302
|
+
const requestInfo = (providerId) => __async(this, null, function* () {
|
|
303
|
+
var _a, _b;
|
|
304
|
+
const connector = connectors.find((c) => {
|
|
305
|
+
var _a2;
|
|
306
|
+
return ((_a2 = c.paraDetails) == null ? void 0 : _a2.internalId) === providerId;
|
|
307
|
+
});
|
|
308
|
+
if (connector.isAuthorized) isLinkingAccount.current = true;
|
|
309
|
+
try {
|
|
310
|
+
const address = yield connectBase(connector);
|
|
311
|
+
return {
|
|
312
|
+
address,
|
|
313
|
+
type: "EVM",
|
|
314
|
+
providerId: (_a = connector.paraDetails) == null ? void 0 : _a.internalId,
|
|
315
|
+
provider: connector.name,
|
|
316
|
+
ensName,
|
|
317
|
+
ensAvatar
|
|
318
|
+
};
|
|
319
|
+
} catch (e) {
|
|
320
|
+
throw new Error((_b = e == null ? void 0 : e.message) != null ? _b : e);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
const disconnectBase = (providerId) => __async(this, null, function* () {
|
|
324
|
+
var _a;
|
|
325
|
+
if (!providerId) {
|
|
326
|
+
throw new Error("Provider ID is required to disconnect");
|
|
327
|
+
}
|
|
328
|
+
const connector = connectors.find((c) => {
|
|
329
|
+
var _a2;
|
|
330
|
+
return ((_a2 = c.paraDetails) == null ? void 0 : _a2.internalId) === providerId;
|
|
331
|
+
});
|
|
332
|
+
isLinkingAccount.current = true;
|
|
333
|
+
try {
|
|
334
|
+
yield connector.disconnect();
|
|
335
|
+
} catch (e) {
|
|
336
|
+
throw new Error((_a = e == null ? void 0 : e.message) != null ? _a : e);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
const nonEip6963ConnectorsByRdns = {};
|
|
340
|
+
let walletConnectModalConnector;
|
|
341
|
+
connectors.filter((c) => !isEIP6963Connector(c)).forEach((c) => {
|
|
342
|
+
if (c.paraDetails) {
|
|
343
|
+
nonEip6963ConnectorsByRdns[c.paraDetails.rdns] = c.paraDetails;
|
|
344
|
+
if (c.paraDetails.isWalletConnectModalConnector) {
|
|
345
|
+
walletConnectModalConnector = c;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
const eip6963ids = connectors.filter((c) => isEIP6963Connector(c)).map((c) => c.id);
|
|
350
|
+
const dedupedConnectors = connectors.map((c) => {
|
|
351
|
+
var _a, _b, _c;
|
|
352
|
+
if ((_a = c.paraDetails) == null ? void 0 : _a.isWalletConnectModalConnector) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (!isEIP6963Connector(c) && eip6963ids.includes((_b = c.paraDetails) == null ? void 0 : _b.rdns)) {
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (isEIP6963Connector(c)) {
|
|
359
|
+
const paraMetadata = nonEip6963ConnectorsByRdns[c.id];
|
|
360
|
+
return __spreadProps(__spreadValues({}, c), { paraDetails: paraMetadata });
|
|
361
|
+
}
|
|
362
|
+
if (((_c = c.paraDetails) == null ? void 0 : _c.id) === "WALLETCONNECT" && walletConnectModalConnector) {
|
|
363
|
+
return __spreadProps(__spreadValues({}, c), { walletConnectModalConnector });
|
|
364
|
+
}
|
|
365
|
+
return c;
|
|
366
|
+
}).filter((c) => !!c);
|
|
367
|
+
const wallets = dedupedConnectors.map((c) => {
|
|
368
|
+
var _a, _b, _c;
|
|
369
|
+
if (((_a = c.paraDetails) == null ? void 0 : _a.internalId) === "SAFE" && (typeof window === "undefined" || window.parent === window)) {
|
|
370
|
+
return void 0;
|
|
371
|
+
}
|
|
372
|
+
const connector = __spreadValues(__spreadValues({}, c), c.paraDetails);
|
|
373
|
+
const supportsWalletConnect = connector.type === "walletConnect" || ((_b = connector.paraDetails) == null ? void 0 : _b.internalId) === "WALLETCONNECT" || ((_c = connector.paraDetails) == null ? void 0 : _c.showQrModal);
|
|
374
|
+
return __spreadValues(__spreadProps(__spreadValues({}, connector), {
|
|
375
|
+
connect: () => connect(connector),
|
|
376
|
+
connectMobile: (manual) => connectMobile(connector, manual),
|
|
377
|
+
type: "EVM"
|
|
378
|
+
}), supportsWalletConnect && { getQrUri: () => getWalletConnectUri(connector, connector.getUri) });
|
|
379
|
+
}).filter(Boolean);
|
|
380
|
+
const getConnectorInfo = (connector) => {
|
|
381
|
+
const paraDetails = connector.paraDetails;
|
|
382
|
+
const providerId = paraDetails == null ? void 0 : paraDetails.internalId;
|
|
383
|
+
const withFullParaAuth = walletsWithFullAuth == null ? void 0 : walletsWithFullAuth.includes(providerId);
|
|
384
|
+
return {
|
|
385
|
+
type: "EVM",
|
|
386
|
+
providerId,
|
|
387
|
+
provider: paraDetails == null ? void 0 : paraDetails.name,
|
|
388
|
+
withFullParaAuth
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
const formattedChains = chains.map((c) => {
|
|
392
|
+
return {
|
|
393
|
+
id: c.id,
|
|
394
|
+
name: c.name
|
|
395
|
+
};
|
|
396
|
+
});
|
|
397
|
+
const username = useMemo(() => ensName != null ? ensName : wagmiAddress, [ensName, wagmiAddress]);
|
|
398
|
+
const farcasterStatus = useMemo(() => {
|
|
399
|
+
var _a;
|
|
400
|
+
const connection = connections.find(
|
|
401
|
+
(c) => {
|
|
402
|
+
var _a2;
|
|
403
|
+
return ((_a2 = c.connector.paraDetails) == null ? void 0 : _a2.internalId) === "FARCASTER";
|
|
404
|
+
}
|
|
405
|
+
);
|
|
406
|
+
if (!connection) {
|
|
407
|
+
return void 0;
|
|
408
|
+
}
|
|
409
|
+
const address = (_a = connection == null ? void 0 : connection.accounts) == null ? void 0 : _a[0];
|
|
410
|
+
return address ? { isConnected: true, address } : { isConnected: false };
|
|
411
|
+
}, [connections]);
|
|
412
|
+
const connectParaEmbedded = useCallback(() => __async(this, null, function* () {
|
|
413
|
+
const paraConnectorInstance = connectors.find((c) => c.id === "para");
|
|
414
|
+
if (!paraConnectorInstance) {
|
|
415
|
+
return { error: "No para connector instance" };
|
|
416
|
+
}
|
|
417
|
+
try {
|
|
418
|
+
const result = yield connectAsync({ connector: paraConnectorInstance });
|
|
419
|
+
return { result };
|
|
420
|
+
} catch (err) {
|
|
421
|
+
const error = err instanceof Error ? err.message : "Unknown error";
|
|
422
|
+
return { error };
|
|
423
|
+
}
|
|
424
|
+
}), [connectors]);
|
|
425
|
+
useEffect(() => {
|
|
426
|
+
connectionsRef.current = connections;
|
|
427
|
+
}, [connections]);
|
|
428
|
+
useEffect(() => {
|
|
429
|
+
connectorsRef.current = connectors;
|
|
430
|
+
}, [connectors]);
|
|
431
|
+
return /* @__PURE__ */ jsx(
|
|
432
|
+
EvmExternalWalletContext.Provider,
|
|
433
|
+
{
|
|
434
|
+
value: __spreadValues({
|
|
435
|
+
wallets,
|
|
436
|
+
chains: formattedChains,
|
|
437
|
+
chainId,
|
|
438
|
+
username,
|
|
439
|
+
avatar: ensAvatar,
|
|
440
|
+
disconnect: disconnectAsync,
|
|
441
|
+
switchChain,
|
|
442
|
+
connectParaEmbedded,
|
|
443
|
+
signMessage,
|
|
444
|
+
signVerificationMessage,
|
|
445
|
+
getWalletBalance,
|
|
446
|
+
requestInfo,
|
|
447
|
+
disconnectBase,
|
|
448
|
+
farcasterStatus
|
|
449
|
+
}, externalHooks),
|
|
450
|
+
children
|
|
451
|
+
}
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
export {
|
|
455
|
+
EvmExternalWalletContext,
|
|
456
|
+
EvmExternalWalletProvider
|
|
457
|
+
};
|
|
@@ -4,12 +4,13 @@ import { WalletList } from '../types/Wallet.js';
|
|
|
4
4
|
import { Chain, Transport } from 'viem';
|
|
5
5
|
import { EvmExternalWalletProviderConfig } from './EvmExternalWalletContext.js';
|
|
6
6
|
import { InjectedParameters } from 'wagmi/connectors';
|
|
7
|
+
import { TExternalWallet } from '@getpara/react-common';
|
|
7
8
|
export interface ParaEvmProviderConfig<chains extends readonly [Chain, ...Chain[]], transports extends Record<chains[number]['id'], Transport>> extends Omit<CreateConfigParameters<chains, transports>, 'connectors'> {
|
|
8
9
|
appName: string;
|
|
9
10
|
appDescription?: string;
|
|
10
11
|
appUrl?: string;
|
|
11
12
|
appIcon?: string;
|
|
12
|
-
wallets?: WalletList;
|
|
13
|
+
wallets?: WalletList | TExternalWallet[];
|
|
13
14
|
projectId: string;
|
|
14
15
|
paraConnectorOptions?: InjectedParameters;
|
|
15
16
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
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 { useEffect, useMemo, useRef, useState } from "react";
|
|
10
|
+
import { createConfig as createWagmiConfig, WagmiProvider } from "wagmi";
|
|
11
|
+
import { connectorsForWallets } from "../wallets/connectorsForWallets.js";
|
|
12
|
+
import { http } from "viem";
|
|
13
|
+
import { computeWalletConnectMetaData } from "../utils/computeWalletConnectMetaData.js";
|
|
14
|
+
import { EvmExternalWalletProvider } from "./EvmExternalWalletContext.js";
|
|
15
|
+
import { paraConnector } from "@getpara/wagmi-v2-connector";
|
|
16
|
+
import { setWagmiConfig, getWagmiConfig } from "../stores/wagmiConfigStore.js";
|
|
17
|
+
import { resolveWalletList } from "../utils/resolveWalletList.js";
|
|
18
|
+
import { farcasterWallet } from "../wallets/connectors/index.js";
|
|
19
|
+
const createDefaultTransports = (chains) => {
|
|
20
|
+
const transportsObject = chains.reduce((acc, chain) => {
|
|
21
|
+
const key = chain.id;
|
|
22
|
+
acc[key] = http();
|
|
23
|
+
return acc;
|
|
24
|
+
}, {});
|
|
25
|
+
return transportsObject;
|
|
26
|
+
};
|
|
27
|
+
function ParaEvmProvider({
|
|
28
|
+
children,
|
|
29
|
+
internalConfig,
|
|
30
|
+
config: _config,
|
|
31
|
+
wagmiProviderProps
|
|
32
|
+
}) {
|
|
33
|
+
const para = internalConfig.para;
|
|
34
|
+
const _a = _config, {
|
|
35
|
+
projectId,
|
|
36
|
+
appName,
|
|
37
|
+
appDescription,
|
|
38
|
+
appIcon,
|
|
39
|
+
appUrl,
|
|
40
|
+
wallets: propsWallets,
|
|
41
|
+
chains,
|
|
42
|
+
transports,
|
|
43
|
+
paraConnectorOptions
|
|
44
|
+
} = _a, wagmiConfigParams = __objRest(_a, [
|
|
45
|
+
"projectId",
|
|
46
|
+
"appName",
|
|
47
|
+
"appDescription",
|
|
48
|
+
"appIcon",
|
|
49
|
+
"appUrl",
|
|
50
|
+
"wallets",
|
|
51
|
+
"chains",
|
|
52
|
+
"transports",
|
|
53
|
+
"paraConnectorOptions"
|
|
54
|
+
]);
|
|
55
|
+
const propsWalletList = useMemo(() => {
|
|
56
|
+
return resolveWalletList(propsWallets != null ? propsWallets : []);
|
|
57
|
+
}, [propsWallets]);
|
|
58
|
+
const prevWallets = useRef(propsWalletList);
|
|
59
|
+
const paraConnectorInstance = useMemo(() => {
|
|
60
|
+
return paraConnector({
|
|
61
|
+
para,
|
|
62
|
+
chains: [...chains],
|
|
63
|
+
disableModal: true,
|
|
64
|
+
appName,
|
|
65
|
+
options: paraConnectorOptions != null ? paraConnectorOptions : {}
|
|
66
|
+
});
|
|
67
|
+
}, [para]);
|
|
68
|
+
const createConfig = (walletList, createFarcasterConnector) => {
|
|
69
|
+
const existing = getWagmiConfig();
|
|
70
|
+
if (existing && prevWallets.current === walletList) {
|
|
71
|
+
return existing;
|
|
72
|
+
}
|
|
73
|
+
prevWallets.current = walletList;
|
|
74
|
+
const wcMetadata = computeWalletConnectMetaData({ appName, appDescription, appUrl, appIcon });
|
|
75
|
+
const baseConnectors = connectorsForWallets(walletList, {
|
|
76
|
+
para,
|
|
77
|
+
createFarcasterConnector,
|
|
78
|
+
projectId,
|
|
79
|
+
appName,
|
|
80
|
+
appDescription,
|
|
81
|
+
appUrl,
|
|
82
|
+
appIcon,
|
|
83
|
+
walletConnectParameters: { metadata: wcMetadata }
|
|
84
|
+
});
|
|
85
|
+
const allConnectors = [...baseConnectors, paraConnectorInstance];
|
|
86
|
+
const createdConfig = createWagmiConfig(__spreadProps(__spreadValues({
|
|
87
|
+
ssr: true
|
|
88
|
+
}, wagmiConfigParams), {
|
|
89
|
+
chains,
|
|
90
|
+
transports: transports || createDefaultTransports(chains),
|
|
91
|
+
connectors: allConnectors
|
|
92
|
+
}));
|
|
93
|
+
setWagmiConfig(createdConfig);
|
|
94
|
+
return createdConfig;
|
|
95
|
+
};
|
|
96
|
+
const [config, setConfig] = useState(null);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (!para.isReady) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const initializeConfig = () => __async(this, null, function* () {
|
|
102
|
+
var _a2;
|
|
103
|
+
if (para.isFarcasterMiniApp) {
|
|
104
|
+
let createFarcasterConnector = null;
|
|
105
|
+
try {
|
|
106
|
+
createFarcasterConnector = (_a2 = (yield import("@farcaster/miniapp-wagmi-connector")).farcasterMiniApp) != null ? _a2 : void 0;
|
|
107
|
+
} catch (e) {
|
|
108
|
+
}
|
|
109
|
+
setConfig(createConfig([...propsWalletList, farcasterWallet], createFarcasterConnector));
|
|
110
|
+
} else {
|
|
111
|
+
setConfig(createConfig([...propsWalletList]));
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
initializeConfig();
|
|
115
|
+
}, [para.isFarcasterMiniApp, para.isReady, propsWalletList]);
|
|
116
|
+
if (!config) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return /* @__PURE__ */ jsx(WagmiProvider, __spreadProps(__spreadValues({ config }, wagmiProviderProps), { children: /* @__PURE__ */ jsx(EvmExternalWalletProvider, __spreadProps(__spreadValues({}, internalConfig), { children })) }));
|
|
120
|
+
}
|
|
121
|
+
export {
|
|
122
|
+
ParaEvmProvider
|
|
123
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Chain, Transport } from 'viem';
|
|
2
|
+
import type { ParaEvmProviderConfig } from './ParaEvmContext.js';
|
|
3
|
+
import ParaWeb from '@getpara/web-sdk';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Wagmi configuration for the Para EVM provider.
|
|
6
|
+
* @param para - The ParaWeb instance to use.
|
|
7
|
+
* @param cfg - The configuration options for the Para EVM provider.
|
|
8
|
+
* @returns The created Wagmi configuration.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createParaWagmiConfig<const chains extends readonly [Chain, ...Chain[]], transports extends Record<chains[number]['id'], Transport>>(para: ParaWeb, cfg: ParaEvmProviderConfig<chains, transports>): import("wagmi").Config<chains, transports, readonly import("wagmi").CreateConnectorFn[]>;
|