-
Notifications
You must be signed in to change notification settings - Fork 20
/
connectors.ts
157 lines (143 loc) · 4.14 KB
/
connectors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import tailwindWalletLogo from 'assets/logos/tailwindWallet.svg';
import compassWalletLogo from 'assets/logos/compassWallet.svg';
import seifWalletLogo from 'assets/logos/seifWallet.svg';
import { createStore } from 'mipd';
import { Connector, CreateConnectorFn, createConnector } from 'wagmi';
import {
metaMask,
coinbaseWallet,
walletConnect,
safe,
} from 'wagmi/connectors';
import config, { networks } from 'config';
import {
type SelectableConnectionName,
providerMapRdnsToName,
selectedConnectors,
currentChain,
} from 'libs/wagmi';
import { externalLinks } from 'libs/routing';
const PLACEHOLDER_TAG = '_placeholder';
export const isPlaceHolderConnector = (c: Connector) =>
c.type.endsWith(PLACEHOLDER_TAG);
const createPlaceholderConnector = ({
id,
name,
icon,
}: {
id: string;
name: string;
icon?: string;
}) => {
return createConnector(() => {
return {
id: id,
name: name,
type: id + PLACEHOLDER_TAG,
icon: icon,
async setup() {},
async connect() {
throw Error('Wallet not installed');
},
async disconnect() {},
async getAccounts() {
return [];
},
async getChainId() {
return currentChain.id;
},
async isAuthorized() {
return true;
},
onAccountsChanged() {},
onChainChanged() {},
async onDisconnect(_error) {},
async getProvider() {},
};
});
};
const getDefaultConnector = (connectorType: SelectableConnectionName) => {
switch (connectorType) {
case 'Compass Wallet':
return createPlaceholderConnector({
name: 'Compass Wallet',
id: 'compass',
icon: compassWalletLogo,
});
case 'Tailwind':
return createPlaceholderConnector({
name: 'TAILWIND',
id: 'tailwind',
icon: tailwindWalletLogo,
});
case 'Seif':
return createPlaceholderConnector({
name: 'Seif',
id: 'seif',
icon: seifWalletLogo,
});
case 'MetaMask':
return metaMask({
extensionOnly: false,
preferDesktop: true,
dappMetadata: {
name: config.appName,
url: config.appUrl,
},
});
case 'Coinbase Wallet':
return coinbaseWallet({
appName: config.appName,
});
case 'WalletConnect':
return walletConnect({
projectId: config.walletConnectProjectId,
qrModalOptions: {
themeMode: 'dark',
privacyPolicyUrl: externalLinks.privacy,
termsOfServiceUrl: externalLinks.terms,
},
});
case 'Safe':
return safe({
allowedDomains: [/gnosis-safe.io$/, /app.safe.global$/],
});
}
};
const isIframe = () =>
typeof window !== 'undefined' && window?.parent !== window;
export const redirectSafeWallet = (
currentId: number,
redirectToId?: number
) => {
if (isIframe() && redirectToId && currentId !== redirectToId) {
const networkToRedirect = networks.find(
(network) => network.chainId === redirectToId
);
if (!networkToRedirect) return;
window.location.href = networkToRedirect.appUrl;
}
};
export const providerRdnsToName = (
connectionName: string
): string | undefined => providerMapRdnsToName[connectionName];
const getConfigConnectors = (): CreateConnectorFn[] => {
const store = createStore();
const initializedProvidersRdns = store
.getProviders()
.map((provider) => provider.info.rdns.toLowerCase());
// Safe wallet always runs through an iFrame, the same check as the safe connector's getProvider is performed here.
// The allowedDomains param in the safe connector is another way to check we're in a safe wallet
if (!isIframe()) initializedProvidersRdns.push('safe');
const initializedProvidersNames = initializedProvidersRdns
.map(providerRdnsToName)
.filter((c) => c)
.map((c) => c!.toLowerCase());
// Only initialize connectors that are not injected
const missingConnectors = selectedConnectors.filter(
(name) => !initializedProvidersNames.includes(name.toLowerCase())
);
store.destroy();
return missingConnectors.map(getDefaultConnector);
};
export const configConnectors = getConfigConnectors();