@getpara/evm-wallet-connectors 0.1.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/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/providers/EvmExternalWalletContext.d.ts +33 -0
- package/dist/providers/EvmExternalWalletContext.js +228 -0
- package/dist/providers/ParaEvmContext.d.ts +23 -0
- package/dist/providers/ParaEvmContext.js +94 -0
- package/dist/types/CommonTypes.d.ts +27 -0
- package/dist/types/CommonTypes.js +1 -0
- package/dist/types/Wallet.d.ts +32 -0
- package/dist/types/Wallet.js +1 -0
- package/dist/types/utils.d.ts +37 -0
- package/dist/types/utils.js +1 -0
- package/dist/utils/computeWalletConnectMetaData.d.ts +9 -0
- package/dist/utils/computeWalletConnectMetaData.js +8 -0
- package/dist/utils/getInjectedConnector.d.ts +11 -0
- package/dist/utils/getInjectedConnector.js +81 -0
- package/dist/utils/getWalletConnectConnector.d.ts +7 -0
- package/dist/utils/getWalletConnectConnector.js +43 -0
- package/dist/utils/getWalletConnectUri.d.ts +2 -0
- package/dist/utils/getWalletConnectUri.js +22 -0
- package/dist/utils/isEIP6963Connector.d.ts +2 -0
- package/dist/utils/isEIP6963Connector.js +4 -0
- package/dist/utils/omitUndefinedValues.d.ts +1 -0
- package/dist/utils/omitUndefinedValues.js +5 -0
- package/dist/utils/uniqueBy.d.ts +1 -0
- package/dist/utils/uniqueBy.js +9 -0
- package/dist/wallets/connectors/coinbase/coinbase.d.ts +6 -0
- package/dist/wallets/connectors/coinbase/coinbase.js +28 -0
- package/dist/wallets/connectors/coinbase/coinbaseIcon.d.ts +1 -0
- package/dist/wallets/connectors/coinbase/coinbaseIcon.js +1 -0
- package/dist/wallets/connectors/index.d.ts +7 -0
- package/dist/wallets/connectors/index.js +7 -0
- package/dist/wallets/connectors/metaMask/metaMask.d.ts +3 -0
- package/dist/wallets/connectors/metaMask/metaMask.js +122 -0
- package/dist/wallets/connectors/metaMask/metaMaskIcon.d.ts +1 -0
- package/dist/wallets/connectors/metaMask/metaMaskIcon.js +1 -0
- package/dist/wallets/connectors/rabby/rabby.d.ts +3 -0
- package/dist/wallets/connectors/rabby/rabby.js +22 -0
- package/dist/wallets/connectors/rabby/rabbyIcon.d.ts +1 -0
- package/dist/wallets/connectors/rabby/rabbyIcon.js +1 -0
- package/dist/wallets/connectors/rainbow/rainbow.d.ts +3 -0
- package/dist/wallets/connectors/rainbow/rainbow.js +33 -0
- package/dist/wallets/connectors/rainbow/rainbowIcon.d.ts +1 -0
- package/dist/wallets/connectors/rainbow/rainbowIcon.js +1 -0
- package/dist/wallets/connectors/walletConnect/walletConnect.d.ts +6 -0
- package/dist/wallets/connectors/walletConnect/walletConnect.js +17 -0
- package/dist/wallets/connectors/walletConnect/walletConnectIcon.d.ts +1 -0
- package/dist/wallets/connectors/walletConnect/walletConnectIcon.js +1 -0
- package/dist/wallets/connectors/zerion/zerion.d.ts +3 -0
- package/dist/wallets/connectors/zerion/zerion.js +35 -0
- package/dist/wallets/connectors/zerion/zerionIcon.d.ts +1 -0
- package/dist/wallets/connectors/zerion/zerionIcon.js +1 -0
- package/dist/wallets/connectorsForWallets.d.ts +15 -0
- package/dist/wallets/connectorsForWallets.js +68 -0
- package/package.json +38 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { CommonChain, CommonWallet } from '../types/CommonTypes.js';
|
|
3
|
+
import ParaWeb from '@getpara/react-sdk';
|
|
4
|
+
export declare const defaultEvmExternalWallet: {
|
|
5
|
+
wallets: any[];
|
|
6
|
+
chains: any[];
|
|
7
|
+
chainId: any;
|
|
8
|
+
username: any;
|
|
9
|
+
avatar: any;
|
|
10
|
+
disconnect: () => Promise<void>;
|
|
11
|
+
switchChain: () => Promise<{}>;
|
|
12
|
+
};
|
|
13
|
+
export declare const EvmExternalWalletContext: import("react").Context<{
|
|
14
|
+
wallets: CommonWallet[];
|
|
15
|
+
chains: CommonChain[];
|
|
16
|
+
chainId: number;
|
|
17
|
+
username: string;
|
|
18
|
+
avatar?: string;
|
|
19
|
+
disconnect: () => Promise<void>;
|
|
20
|
+
switchChain: (chainId: number) => Promise<{
|
|
21
|
+
error?: string[];
|
|
22
|
+
}>;
|
|
23
|
+
}>;
|
|
24
|
+
interface EvmExternalWalletProviderProps {
|
|
25
|
+
children: ReactNode;
|
|
26
|
+
para: ParaWeb;
|
|
27
|
+
onSwitchWallet: (args: {
|
|
28
|
+
address?: string;
|
|
29
|
+
error?: string;
|
|
30
|
+
}) => void;
|
|
31
|
+
}
|
|
32
|
+
export declare function EvmExternalWalletProvider({ children, para, onSwitchWallet }: EvmExternalWalletProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
|
+
import { createContext, useEffect, useMemo, useState } from 'react';
|
|
12
|
+
import { useAccount, useSwitchChain, useConnect, useDisconnect, useEnsName, useEnsAvatar } from 'wagmi';
|
|
13
|
+
import { isEIP6963Connector } from '../utils/isEIP6963Connector.js';
|
|
14
|
+
import { getWalletConnectUri } from '../utils/getWalletConnectUri.js';
|
|
15
|
+
import { isMobile, WalletType } from '@getpara/react-sdk';
|
|
16
|
+
import { normalize } from 'viem/ens';
|
|
17
|
+
export const defaultEvmExternalWallet = {
|
|
18
|
+
wallets: [],
|
|
19
|
+
chains: [],
|
|
20
|
+
chainId: undefined,
|
|
21
|
+
username: undefined,
|
|
22
|
+
avatar: undefined,
|
|
23
|
+
disconnect: () => Promise.resolve(),
|
|
24
|
+
switchChain: () => Promise.resolve({}),
|
|
25
|
+
};
|
|
26
|
+
export const EvmExternalWalletContext = createContext(defaultEvmExternalWallet);
|
|
27
|
+
export function EvmExternalWalletProvider({ children, para, onSwitchWallet }) {
|
|
28
|
+
const { connectAsync, connectors: untypedConnectors } = useConnect();
|
|
29
|
+
const { address: wagmiAddress, isConnecting, isReconnecting, chainId, connector: connectedConnector } = useAccount();
|
|
30
|
+
const { chains, switchChainAsync } = useSwitchChain();
|
|
31
|
+
const { disconnectAsync } = useDisconnect();
|
|
32
|
+
const { data: ensName } = useEnsName({ address: wagmiAddress });
|
|
33
|
+
const { data: ensAvatar } = useEnsAvatar({
|
|
34
|
+
name: normalize(ensName),
|
|
35
|
+
});
|
|
36
|
+
const [isLocalConnecting, setIsLocalConnecting] = useState(false);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const storedExternalWallet = para.externalWallets[wagmiAddress !== null && wagmiAddress !== void 0 ? wagmiAddress : ''];
|
|
39
|
+
if (!isConnecting &&
|
|
40
|
+
!isReconnecting &&
|
|
41
|
+
!isLocalConnecting &&
|
|
42
|
+
!!wagmiAddress &&
|
|
43
|
+
!storedExternalWallet &&
|
|
44
|
+
!((connectedConnector === null || connectedConnector === void 0 ? void 0 : connectedConnector.id) === 'para')) {
|
|
45
|
+
reset();
|
|
46
|
+
}
|
|
47
|
+
}, [isConnecting, isReconnecting, isLocalConnecting, wagmiAddress, connectedConnector]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
const storedExternalWallet = para.externalWallets[(_b = (_a = para.currentExternalWalletAddresses) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : ''];
|
|
51
|
+
// If the user is using an external EVM wallet we want to watch for wallet changes and log them in to a different user when the wallet changes
|
|
52
|
+
if (!isConnecting &&
|
|
53
|
+
!isReconnecting &&
|
|
54
|
+
(storedExternalWallet === null || storedExternalWallet === void 0 ? void 0 : storedExternalWallet.type) === WalletType.EVM &&
|
|
55
|
+
(storedExternalWallet === null || storedExternalWallet === void 0 ? void 0 : storedExternalWallet.address) !== wagmiAddress) {
|
|
56
|
+
switchWallet(wagmiAddress);
|
|
57
|
+
}
|
|
58
|
+
}, [wagmiAddress, isReconnecting, isConnecting]);
|
|
59
|
+
const connectors = untypedConnectors;
|
|
60
|
+
const reset = () => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
yield disconnectAsync();
|
|
62
|
+
yield para.logout();
|
|
63
|
+
});
|
|
64
|
+
const switchChain = (chainId) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
var _a, _b, _c;
|
|
66
|
+
let error;
|
|
67
|
+
try {
|
|
68
|
+
yield switchChainAsync({ chainId });
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
// Invalid networks throws the same error as connection rejected requests, so catching the error using the detail string here.
|
|
72
|
+
if (e.details.includes('Missing or invalid.')) {
|
|
73
|
+
const chain = chains.find(c => c.id === chainId);
|
|
74
|
+
error = [
|
|
75
|
+
'Network not supported.',
|
|
76
|
+
`You may need to add ${chain === null || chain === void 0 ? void 0 : chain.name} support to ${(_c = (_b = (_a = connectedConnector === null || connectedConnector === void 0 ? void 0 : connectedConnector.paraDetails) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : connectedConnector === null || connectedConnector === void 0 ? void 0 : connectedConnector.name) !== null && _c !== void 0 ? _c : 'the wallet'} manually.`,
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
switch (e.name) {
|
|
81
|
+
case 'UserRejectedRequestError': {
|
|
82
|
+
error = ['Change request rejected'];
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
default: {
|
|
86
|
+
error = ['An unknown error occurred'];
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return { error };
|
|
93
|
+
});
|
|
94
|
+
const login = (address, connectorName) => __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
yield para.externalWalletLogin({ address, type: WalletType.EVM, provider: connectorName });
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
yield disconnectAsync();
|
|
100
|
+
yield para.logout();
|
|
101
|
+
throw 'Error logging you in. Please try again.';
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
const switchWallet = (address) => __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
setIsLocalConnecting(true);
|
|
106
|
+
let error;
|
|
107
|
+
// If we're calling switch wallet with no address, treat it as if the user disconnected the wallet from the app and logout to reset the Para instance.
|
|
108
|
+
if (!address) {
|
|
109
|
+
yield para.logout();
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
try {
|
|
113
|
+
yield login(address, connectedConnector === null || connectedConnector === void 0 ? void 0 : connectedConnector.name);
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
error = err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
onSwitchWallet({ address, error });
|
|
120
|
+
setIsLocalConnecting(false);
|
|
121
|
+
});
|
|
122
|
+
const connect = (connector) => __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
var _a, _b, _c, _d;
|
|
124
|
+
setIsLocalConnecting(true);
|
|
125
|
+
yield disconnectAsync();
|
|
126
|
+
const walletChainId = yield connector.getChainId();
|
|
127
|
+
let address;
|
|
128
|
+
let error;
|
|
129
|
+
try {
|
|
130
|
+
const data = yield connectAsync({
|
|
131
|
+
// If the wallet is already on a supported chain, use that to avoid a chain switch prompt.
|
|
132
|
+
chainId: (_b = (_a = chains.find(({ id }) => id === walletChainId)) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b :
|
|
133
|
+
// Fall back to the first chain provided.
|
|
134
|
+
(_c = chains[0]) === null || _c === void 0 ? void 0 : _c.id,
|
|
135
|
+
connector,
|
|
136
|
+
});
|
|
137
|
+
address = (_d = data.accounts) === null || _d === void 0 ? void 0 : _d[0];
|
|
138
|
+
if (address) {
|
|
139
|
+
try {
|
|
140
|
+
yield login(address, connector.name);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
address = undefined;
|
|
144
|
+
error = err;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
switch (e.name) {
|
|
150
|
+
case 'UserRejectedRequestError': {
|
|
151
|
+
error = 'Connection request rejected';
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 'ResourceUnavailableRpcError': {
|
|
155
|
+
`${connector.name} not detected`;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
default: {
|
|
159
|
+
error = 'An unknown error occurred';
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
setIsLocalConnecting(false);
|
|
165
|
+
return { address, error };
|
|
166
|
+
});
|
|
167
|
+
const connectMobile = (connector, isManualWalletConnect) => __awaiter(this, void 0, void 0, function* () {
|
|
168
|
+
const _isMobile = isManualWalletConnect !== undefined ? isManualWalletConnect : isMobile();
|
|
169
|
+
// If on mobile and the connector contains the wallet connect modal connector, use it.
|
|
170
|
+
const _connector = connector.walletConnectModalConnector && _isMobile ? connector.walletConnectModalConnector : connector;
|
|
171
|
+
return yield connect(_connector);
|
|
172
|
+
});
|
|
173
|
+
const getQrUri = (connector) => () => {
|
|
174
|
+
var _a;
|
|
175
|
+
return getWalletConnectUri(connector, (_a = connector.paraDetails) === null || _a === void 0 ? void 0 : _a.getUri);
|
|
176
|
+
};
|
|
177
|
+
// If an Eip6963 wallet is injected we want to remove the non Eip6963 connector and attach its metadata to the Eip6963 connector
|
|
178
|
+
const nonEip6963ConnectorsByRdns = {};
|
|
179
|
+
let walletConnectModalConnector;
|
|
180
|
+
connectors
|
|
181
|
+
.filter(c => !isEIP6963Connector(c))
|
|
182
|
+
.forEach(c => {
|
|
183
|
+
if (c.paraDetails) {
|
|
184
|
+
nonEip6963ConnectorsByRdns[c.paraDetails.rdns] = c.paraDetails;
|
|
185
|
+
if (c.paraDetails.isWalletConnectModalConnector) {
|
|
186
|
+
walletConnectModalConnector = c;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
const eip6963ids = connectors.filter(c => isEIP6963Connector(c)).map(c => c.id);
|
|
191
|
+
const dedupedConnectors = connectors
|
|
192
|
+
.map(c => {
|
|
193
|
+
var _a, _b, _c;
|
|
194
|
+
// Filter out the duplicated walletConnect connector with the modal
|
|
195
|
+
// This connector will be attached to the WC connector that doesn't contain the modal for use on mobile
|
|
196
|
+
if ((_a = c.paraDetails) === null || _a === void 0 ? void 0 : _a.isWalletConnectModalConnector) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
// Remove any non EIP6963 connectors if they have a matching EIP6963 connectors
|
|
200
|
+
if (!isEIP6963Connector(c) && eip6963ids.includes((_b = c.paraDetails) === null || _b === void 0 ? void 0 : _b.rdns)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// Return the EIP6963 connectors
|
|
204
|
+
if (isEIP6963Connector(c)) {
|
|
205
|
+
const paraMetadata = nonEip6963ConnectorsByRdns[c.id];
|
|
206
|
+
return Object.assign(Object.assign({}, c), { paraDetails: paraMetadata });
|
|
207
|
+
}
|
|
208
|
+
// Return the WC connector with the attached WC modal connector
|
|
209
|
+
if (((_c = c.paraDetails) === null || _c === void 0 ? void 0 : _c.id) === 'walletConnect' && walletConnectModalConnector) {
|
|
210
|
+
return Object.assign(Object.assign({}, c), { walletConnectModalConnector });
|
|
211
|
+
}
|
|
212
|
+
return c;
|
|
213
|
+
})
|
|
214
|
+
.filter(c => !!c);
|
|
215
|
+
const wallets = dedupedConnectors.map(c => {
|
|
216
|
+
const connector = Object.assign(Object.assign({}, c), c.paraDetails);
|
|
217
|
+
return Object.assign(Object.assign({}, connector), { connect: () => connect(connector), connectMobile: isManualWalletConnect => connectMobile(connector, isManualWalletConnect), type: WalletType.EVM, getQrUri: getQrUri(connector) });
|
|
218
|
+
});
|
|
219
|
+
const formattedChains = chains.map(c => {
|
|
220
|
+
return {
|
|
221
|
+
id: c.id,
|
|
222
|
+
name: c.name,
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
const username = useMemo(() => ensName !== null && ensName !== void 0 ? ensName : wagmiAddress, [ensName, wagmiAddress]);
|
|
226
|
+
const disconnect = disconnectAsync;
|
|
227
|
+
return (_jsx(EvmExternalWalletContext.Provider, { value: useMemo(() => ({ wallets, chains: formattedChains, chainId, username, avatar: ensAvatar, disconnect, switchChain }), [wallets, formattedChains, chainId, username, ensAvatar, disconnect, switchChain]), children: children }));
|
|
228
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { CreateConfigParameters, WagmiProviderProps } from 'wagmi';
|
|
3
|
+
import { WalletList } from '../types/Wallet.js';
|
|
4
|
+
import { Chain, Transport } from 'viem';
|
|
5
|
+
import ParaWeb from '@getpara/react-sdk';
|
|
6
|
+
import { InjectedParameters } from 'wagmi/connectors';
|
|
7
|
+
interface GetDefaultConfigParameters<chains extends readonly [Chain, ...Chain[]], transports extends Record<chains[number]['id'], Transport>> extends Omit<CreateConfigParameters<chains, transports>, 'connectors'> {
|
|
8
|
+
appName: string;
|
|
9
|
+
appDescription?: string;
|
|
10
|
+
appUrl?: string;
|
|
11
|
+
appIcon?: string;
|
|
12
|
+
wallets?: WalletList;
|
|
13
|
+
projectId: string;
|
|
14
|
+
para?: ParaWeb;
|
|
15
|
+
paraDisableModal?: boolean;
|
|
16
|
+
paraOptions?: InjectedParameters;
|
|
17
|
+
}
|
|
18
|
+
interface ParaEvmProviderProps<chains extends readonly [Chain, ...Chain[]], transports extends Record<chains[number]['id'], Transport>> extends Omit<WagmiProviderProps, 'config'> {
|
|
19
|
+
children: ReactNode;
|
|
20
|
+
config: GetDefaultConfigParameters<chains, transports>;
|
|
21
|
+
}
|
|
22
|
+
export declare function ParaEvmProvider<const chains extends readonly [Chain, ...Chain[]], transports extends Record<chains[number]['id'], Transport>>({ children, config: _config, ...wagmiProviderProps }: ParaEvmProviderProps<chains, transports>): import("react/jsx-runtime").JSX.Element;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
22
|
+
import { useCallback, useEffect, useMemo } from 'react';
|
|
23
|
+
import { createConfig, WagmiProvider } from 'wagmi';
|
|
24
|
+
import { connectorsForWallets } from '../wallets/connectorsForWallets.js';
|
|
25
|
+
import { http } from 'viem';
|
|
26
|
+
import { computeWalletConnectMetaData } from '../utils/computeWalletConnectMetaData.js';
|
|
27
|
+
import { EvmExternalWalletContext, EvmExternalWalletProvider } from './EvmExternalWalletContext.js';
|
|
28
|
+
import { useExternalWalletProviderStore } from '@getpara/react-sdk';
|
|
29
|
+
import { connect } from 'wagmi/actions';
|
|
30
|
+
import { paraConnector } from '@getpara/wagmi-v2-integration';
|
|
31
|
+
const createDefaultTransports = (chains) => {
|
|
32
|
+
const transportsObject = chains.reduce((acc, chain) => {
|
|
33
|
+
const key = chain.id;
|
|
34
|
+
acc[key] = http(); // Type assertion here
|
|
35
|
+
return acc;
|
|
36
|
+
}, {});
|
|
37
|
+
return transportsObject;
|
|
38
|
+
};
|
|
39
|
+
export function ParaEvmProvider(_a) {
|
|
40
|
+
var { children, config: _config } = _a, wagmiProviderProps = __rest(_a, ["children", "config"]);
|
|
41
|
+
const updateExternalWalletProviderState = useExternalWalletProviderStore(state => state.updateState);
|
|
42
|
+
const EvmProvider = useExternalWalletProviderStore(state => state.EvmProvider);
|
|
43
|
+
const evmContext = useExternalWalletProviderStore(state => state.evmContext);
|
|
44
|
+
const { projectId, appName, appDescription, appIcon, appUrl, wallets, chains, transports, para, paraDisableModal, paraOptions } = _config, wagmiConfigParams = __rest(_config, ["projectId", "appName", "appDescription", "appIcon", "appUrl", "wallets", "chains", "transports", "para", "paraDisableModal", "paraOptions"]);
|
|
45
|
+
const wcMetadata = computeWalletConnectMetaData({ appName, appDescription, appUrl, appIcon });
|
|
46
|
+
const paraConnectorInstance = useMemo(() => {
|
|
47
|
+
if (!para)
|
|
48
|
+
return undefined;
|
|
49
|
+
const instance = paraConnector({
|
|
50
|
+
para,
|
|
51
|
+
chains: [...chains],
|
|
52
|
+
disableModal: paraDisableModal !== null && paraDisableModal !== void 0 ? paraDisableModal : true,
|
|
53
|
+
appName,
|
|
54
|
+
options: paraOptions !== null && paraOptions !== void 0 ? paraOptions : {},
|
|
55
|
+
});
|
|
56
|
+
return instance;
|
|
57
|
+
}, [para, chains, paraDisableModal, appName, paraOptions]);
|
|
58
|
+
const allConnectors = useMemo(() => {
|
|
59
|
+
const baseConnectors = connectorsForWallets(wallets, {
|
|
60
|
+
projectId,
|
|
61
|
+
appName,
|
|
62
|
+
appDescription,
|
|
63
|
+
appUrl,
|
|
64
|
+
appIcon,
|
|
65
|
+
walletConnectParameters: { metadata: wcMetadata },
|
|
66
|
+
});
|
|
67
|
+
return paraConnectorInstance ? [...baseConnectors, paraConnectorInstance] : baseConnectors;
|
|
68
|
+
}, [wallets, projectId, appName, appDescription, appUrl, appIcon, wcMetadata, paraConnectorInstance]);
|
|
69
|
+
const config = useMemo(() => createConfig(Object.assign(Object.assign({}, wagmiConfigParams), { chains, transports: transports || createDefaultTransports(chains), connectors: allConnectors })), [wagmiConfigParams, chains, transports, allConnectors]);
|
|
70
|
+
const connectParaEvmWallet = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
if (!paraConnectorInstance) {
|
|
72
|
+
return { error: 'No para connector instance' };
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const result = yield connect(config, { connector: paraConnectorInstance });
|
|
76
|
+
return { result };
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
const error = err instanceof Error ? err.message : 'Unknown error';
|
|
80
|
+
return { error };
|
|
81
|
+
}
|
|
82
|
+
}), [paraConnectorInstance, config, connect]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
updateExternalWalletProviderState({
|
|
85
|
+
EvmProvider: evmContext && EvmProvider ? EvmProvider : EvmExternalWalletProvider,
|
|
86
|
+
evmContext: evmContext || EvmExternalWalletContext,
|
|
87
|
+
connectParaEvmWallet: paraConnectorInstance ? connectParaEvmWallet : undefined,
|
|
88
|
+
});
|
|
89
|
+
}, [evmContext, EvmProvider, paraConnectorInstance, connectParaEvmWallet, updateExternalWalletProviderState]);
|
|
90
|
+
if (!evmContext || !EvmProvider) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return (_jsx(WagmiProvider, Object.assign({ config: config }, wagmiProviderProps, { children: children })));
|
|
94
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type WalletMetadata = {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
rdns?: string;
|
|
5
|
+
iconUrl: string;
|
|
6
|
+
installed?: boolean;
|
|
7
|
+
isExtension?: boolean;
|
|
8
|
+
isMobile?: boolean;
|
|
9
|
+
isWeb?: boolean;
|
|
10
|
+
downloadUrl?: string;
|
|
11
|
+
getQrUri?: () => Promise<string>;
|
|
12
|
+
};
|
|
13
|
+
export type CommonWallet = {
|
|
14
|
+
connect: () => Promise<{
|
|
15
|
+
address?: string;
|
|
16
|
+
error?: string;
|
|
17
|
+
}>;
|
|
18
|
+
connectMobile: (isManualWalletConnect?: boolean) => Promise<{
|
|
19
|
+
address?: string;
|
|
20
|
+
error?: string;
|
|
21
|
+
}>;
|
|
22
|
+
type: 'EVM' | 'SOLANA' | 'COSMOS';
|
|
23
|
+
} & WalletMetadata;
|
|
24
|
+
export type CommonChain = {
|
|
25
|
+
id: string | number;
|
|
26
|
+
name: string;
|
|
27
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Connector, CreateConnectorFn } from 'wagmi';
|
|
2
|
+
import { WalletConnectParameters } from 'wagmi/connectors';
|
|
3
|
+
import { CoinbaseWalletOptions } from '../wallets/connectors/coinbase/coinbase.js';
|
|
4
|
+
import { WalletConnectWalletOptions } from '../wallets/connectors/walletConnect/walletConnect.js';
|
|
5
|
+
import { WalletMetadata } from './CommonTypes.js';
|
|
6
|
+
export type Wallet = {
|
|
7
|
+
createConnector?: (walletDetails: WalletDetailsParams) => CreateConnectorFn;
|
|
8
|
+
createMobileConnector?: (walletDetails: WalletDetailsParams) => CreateConnectorFn;
|
|
9
|
+
getUri?: (uri: string) => string;
|
|
10
|
+
} & WalletMetadata;
|
|
11
|
+
export interface DefaultWalletOptions {
|
|
12
|
+
projectId: string;
|
|
13
|
+
walletConnectParameters?: ParaWalletConnectParameters;
|
|
14
|
+
}
|
|
15
|
+
export type CreateWalletFn = (createWalletParams: CoinbaseWalletOptions & Omit<WalletConnectWalletOptions, 'projectId'> & DefaultWalletOptions) => Wallet;
|
|
16
|
+
export type WalletList = CreateWalletFn[];
|
|
17
|
+
export type ParaWalletConnectParameters = Omit<WalletConnectParameters, 'showQrModal' | 'projectId'>;
|
|
18
|
+
export type ParaDetails = Omit<Wallet, 'createConnector' | 'createWCConnector' | 'hidden'> & {
|
|
19
|
+
isWalletConnectModalConnector?: boolean;
|
|
20
|
+
isParaConnector: boolean;
|
|
21
|
+
walletConnectModalConnector?: Connector;
|
|
22
|
+
showQrModal?: true;
|
|
23
|
+
};
|
|
24
|
+
export type WalletDetailsParams = {
|
|
25
|
+
paraDetails: ParaDetails;
|
|
26
|
+
};
|
|
27
|
+
export type CreateConnector = (walletDetails: WalletDetailsParams) => CreateConnectorFn;
|
|
28
|
+
export type WagmiConnectorInstance = Connector & {
|
|
29
|
+
paraDetails?: ParaDetails;
|
|
30
|
+
walletConnectModalConnector?: WagmiConnectorInstance;
|
|
31
|
+
};
|
|
32
|
+
export type WalletInstance = Connector & ParaDetails;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { EIP1193Provider } from 'viem';
|
|
2
|
+
/** Combines members of an intersection into a readable type. */
|
|
3
|
+
export type Evaluate<type> = {
|
|
4
|
+
[key in keyof type]: type[key];
|
|
5
|
+
} & unknown;
|
|
6
|
+
/** Removes `readonly` from all properties of an object. */
|
|
7
|
+
export type Mutable<type extends object> = {
|
|
8
|
+
-readonly [key in keyof type]: type[key];
|
|
9
|
+
};
|
|
10
|
+
/** Strict version of built-in Omit type */
|
|
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';
|
|
13
|
+
export type WalletProvider = Evaluate<EIP1193Provider & {
|
|
14
|
+
[key in WalletProviderFlags]?: true | undefined;
|
|
15
|
+
} & {
|
|
16
|
+
providers?: any[] | undefined;
|
|
17
|
+
/** Only exists in MetaMask as of 2022/04/03 */
|
|
18
|
+
_events?: {
|
|
19
|
+
connect?: (() => void) | undefined;
|
|
20
|
+
} | undefined;
|
|
21
|
+
/** Only exists in MetaMask as of 2022/04/03 */
|
|
22
|
+
_state?: {
|
|
23
|
+
accounts?: string[];
|
|
24
|
+
initialized?: boolean;
|
|
25
|
+
isConnected?: boolean;
|
|
26
|
+
isPermanentlyDisconnected?: boolean;
|
|
27
|
+
isUnlocked?: boolean;
|
|
28
|
+
} | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
export type WindowProvider = {
|
|
31
|
+
coinbaseWalletExtension?: WalletProvider | undefined;
|
|
32
|
+
ethereum?: WalletProvider | undefined;
|
|
33
|
+
phantom?: {
|
|
34
|
+
ethereum: WalletProvider;
|
|
35
|
+
} | undefined;
|
|
36
|
+
providers?: any[] | undefined;
|
|
37
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ParaWalletConnectParameters } from '../types/Wallet.js';
|
|
2
|
+
interface ComputeMetaDataParameters {
|
|
3
|
+
appName: string;
|
|
4
|
+
appDescription?: string;
|
|
5
|
+
appUrl?: string;
|
|
6
|
+
appIcon?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const computeWalletConnectMetaData: ({ appName, appDescription, appUrl, appIcon, }: ComputeMetaDataParameters) => ParaWalletConnectParameters["metadata"];
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const computeWalletConnectMetaData = ({ appName, appDescription, appUrl, appIcon, }) => {
|
|
2
|
+
return {
|
|
3
|
+
name: appName,
|
|
4
|
+
description: appDescription !== null && appDescription !== void 0 ? appDescription : appName,
|
|
5
|
+
url: appUrl !== null && appUrl !== void 0 ? appUrl : (typeof window !== 'undefined' ? window.location.href : ''),
|
|
6
|
+
icons: [...(appIcon ? [appIcon] : [])],
|
|
7
|
+
};
|
|
8
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { WalletProviderFlags } from '../types/utils.js';
|
|
2
|
+
import { CreateConnector } from '../types/Wallet.js';
|
|
3
|
+
export declare function hasInjectedProvider({ flag, namespace }: {
|
|
4
|
+
flag?: WalletProviderFlags;
|
|
5
|
+
namespace?: string;
|
|
6
|
+
}): boolean;
|
|
7
|
+
export declare function getInjectedConnector({ flag, namespace, target, }: {
|
|
8
|
+
flag?: WalletProviderFlags;
|
|
9
|
+
namespace?: string;
|
|
10
|
+
target?: any;
|
|
11
|
+
}): CreateConnector;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createConnector } from 'wagmi';
|
|
2
|
+
import { injected } from 'wagmi/connectors';
|
|
3
|
+
/*
|
|
4
|
+
* Returns the explicit window provider that matches the flag and the flag is true
|
|
5
|
+
*/
|
|
6
|
+
function getExplicitInjectedProvider(flag) {
|
|
7
|
+
const _window = typeof window !== 'undefined' ? window : undefined;
|
|
8
|
+
if (typeof _window === 'undefined' || typeof _window.ethereum === 'undefined')
|
|
9
|
+
return;
|
|
10
|
+
const providers = _window.ethereum.providers;
|
|
11
|
+
return providers ? providers.find(provider => provider[flag]) : _window.ethereum[flag] ? _window.ethereum : undefined;
|
|
12
|
+
}
|
|
13
|
+
/*
|
|
14
|
+
* Gets the `window.namespace` window provider if it exists
|
|
15
|
+
*/
|
|
16
|
+
function getWindowProviderNamespace(namespace) {
|
|
17
|
+
const providerSearch = (provider, namespace) => {
|
|
18
|
+
const [property, ...path] = namespace.split('.');
|
|
19
|
+
const _provider = provider[property];
|
|
20
|
+
if (_provider) {
|
|
21
|
+
if (path.length === 0)
|
|
22
|
+
return _provider;
|
|
23
|
+
return providerSearch(_provider, path.join('.'));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
if (typeof window !== 'undefined')
|
|
27
|
+
return providerSearch(window, namespace);
|
|
28
|
+
}
|
|
29
|
+
/*
|
|
30
|
+
* Checks if the explict provider or window ethereum exists
|
|
31
|
+
*/
|
|
32
|
+
export function hasInjectedProvider({ flag, namespace }) {
|
|
33
|
+
if (namespace && typeof getWindowProviderNamespace(namespace) !== 'undefined')
|
|
34
|
+
return true;
|
|
35
|
+
if (flag && typeof getExplicitInjectedProvider(flag) !== 'undefined')
|
|
36
|
+
return true;
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
/*
|
|
40
|
+
* Returns an injected provider that favors the flag match, but falls back to window.ethereum
|
|
41
|
+
*/
|
|
42
|
+
function getInjectedProvider({ flag, namespace }) {
|
|
43
|
+
var _a;
|
|
44
|
+
const _window = typeof window !== 'undefined' ? window : undefined;
|
|
45
|
+
if (typeof _window === 'undefined')
|
|
46
|
+
return;
|
|
47
|
+
if (namespace) {
|
|
48
|
+
// prefer custom eip1193 namespaces
|
|
49
|
+
const windowProvider = getWindowProviderNamespace(namespace);
|
|
50
|
+
if (windowProvider)
|
|
51
|
+
return windowProvider;
|
|
52
|
+
}
|
|
53
|
+
const providers = (_a = _window.ethereum) === null || _a === void 0 ? void 0 : _a.providers;
|
|
54
|
+
if (flag) {
|
|
55
|
+
const provider = getExplicitInjectedProvider(flag);
|
|
56
|
+
if (provider)
|
|
57
|
+
return provider;
|
|
58
|
+
}
|
|
59
|
+
if (typeof providers !== 'undefined' && providers.length > 0)
|
|
60
|
+
return providers[0];
|
|
61
|
+
return _window.ethereum;
|
|
62
|
+
}
|
|
63
|
+
function createInjectedConnector(provider) {
|
|
64
|
+
return (walletDetails) => {
|
|
65
|
+
// Create the injected configuration object conditionally based on the provider.
|
|
66
|
+
const injectedConfig = provider
|
|
67
|
+
? {
|
|
68
|
+
target: () => ({
|
|
69
|
+
id: walletDetails.paraDetails.id,
|
|
70
|
+
name: walletDetails.paraDetails.name,
|
|
71
|
+
provider,
|
|
72
|
+
}),
|
|
73
|
+
}
|
|
74
|
+
: {};
|
|
75
|
+
return createConnector(config => (Object.assign(Object.assign({}, injected(injectedConfig)(config)), walletDetails)));
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export function getInjectedConnector({ flag, namespace, target, }) {
|
|
79
|
+
const provider = target ? target : getInjectedProvider({ flag, namespace });
|
|
80
|
+
return createInjectedConnector(provider);
|
|
81
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CreateConnector, ParaWalletConnectParameters } from '../types/Wallet.js';
|
|
2
|
+
interface GetWalletConnectConnectorParams {
|
|
3
|
+
projectId: string;
|
|
4
|
+
walletConnectParameters?: ParaWalletConnectParameters;
|
|
5
|
+
}
|
|
6
|
+
export declare function getWalletConnectConnector({ projectId, walletConnectParameters, }: GetWalletConnectConnectorParams): CreateConnector;
|
|
7
|
+
export {};
|