-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathmetamaskConnector.ts
More file actions
285 lines (247 loc) · 10.6 KB
/
Copy pathmetamaskConnector.ts
File metadata and controls
285 lines (247 loc) · 10.6 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { MetaMaskSDK, type MetaMaskSDKOptions } from "@metamask/sdk";
import { getErrorAnalyticsProperties } from "@toruslabs/base-controllers";
import deepmerge from "deepmerge";
import {
AddEthereumChainConfig,
type Analytics,
ANALYTICS_EVENTS,
BaseConnectorLoginParams,
type BaseConnectorSettings,
CHAIN_NAMESPACES,
type ChainNamespaceType,
CONNECTED_EVENT_DATA,
CONNECTOR_CATEGORY,
type CONNECTOR_CATEGORY_TYPE,
CONNECTOR_EVENTS,
CONNECTOR_NAMESPACES,
CONNECTOR_STATUS,
type CONNECTOR_STATUS_TYPE,
type ConnectorFn,
type ConnectorInitOptions,
type ConnectorNamespaceType,
type ConnectorParams,
type CustomChainConfig,
getCaipChainId,
IdentityTokenInfo,
type IProvider,
type MetaMaskConnectorData,
type UserInfo,
WALLET_CONNECTOR_TYPE,
WALLET_CONNECTORS,
WalletLoginError,
Web3AuthError,
} from "../../base";
import { BaseEvmConnector } from "../base-evm-connector";
import { getSiteIcon, getSiteName } from "../utils";
export interface MetaMaskConnectorOptions extends BaseConnectorSettings {
connectorSettings?: Partial<MetaMaskSDKOptions>;
}
class MetaMaskConnector extends BaseEvmConnector<void> {
readonly connectorNamespace: ConnectorNamespaceType = CONNECTOR_NAMESPACES.EIP155;
readonly currentChainNamespace: ChainNamespaceType = CHAIN_NAMESPACES.EIP155;
readonly type: CONNECTOR_CATEGORY_TYPE = CONNECTOR_CATEGORY.EXTERNAL;
readonly name: WALLET_CONNECTOR_TYPE = WALLET_CONNECTORS.METAMASK;
public status: CONNECTOR_STATUS_TYPE = CONNECTOR_STATUS.NOT_READY;
private metamaskProvider: IProvider = null;
private metamaskSDK: MetaMaskSDK = null;
private metamaskOptions: Partial<MetaMaskSDKOptions>;
private analytics?: Analytics;
constructor(connectorOptions: MetaMaskConnectorOptions) {
super(connectorOptions);
this.metamaskOptions = connectorOptions.connectorSettings;
this.analytics = connectorOptions.analytics;
}
get provider(): IProvider | null {
if (this.status !== CONNECTOR_STATUS.NOT_READY && this.metamaskProvider) {
return this.metamaskProvider as unknown as IProvider;
}
return null;
}
set provider(_: IProvider | null) {
throw new Error("Not implemented");
}
async init(options: ConnectorInitOptions): Promise<void> {
await super.init(options);
const chainConfig = this.coreOptions.chains.find((x) => x.chainId === options.chainId);
super.checkInitializationRequirements({ chainConfig });
// Detect app metadata
const iconUrl = await getSiteIcon(window);
// TODO: handle ssr
const appMetadata: MetaMaskSDKOptions["dappMetadata"] = {
name: getSiteName(window) || "web3auth",
url: window.location.origin || "https://web3auth.io",
iconUrl: iconUrl ?? undefined,
};
// initialize the MetaMask SDK
const metamaskOptions = deepmerge(this.metamaskOptions || {}, { dappMetadata: appMetadata });
this.metamaskSDK = new MetaMaskSDK({ ...metamaskOptions, _source: "web3auth", preferDesktop: true });
// Work around: in case there is an existing SDK instance in memory (window.mmsdk exists), it won't initialize the new SDK instance again
// and return the existing instance instead of undefined (this is an assumption, not sure if it's a bug or feature of the MetaMask SDK)
const initResult = await this.metamaskSDK.init();
if (initResult) {
this.metamaskSDK = initResult;
}
this.isInjected = this.metamaskSDK.isExtensionActive();
this.status = CONNECTOR_STATUS.READY;
this.emit(CONNECTOR_EVENTS.READY, WALLET_CONNECTORS.METAMASK);
try {
if (options.autoConnect) {
this.rehydrated = true;
const provider = await this.connect({ chainId: options.chainId, getIdentityToken: false });
if (!provider) {
this.rehydrated = false;
throw WalletLoginError.connectionError("Failed to rehydrate.");
}
}
} catch (error) {
this.emit(CONNECTOR_EVENTS.REHYDRATION_ERROR, error as Web3AuthError);
}
}
async connect({ chainId, getIdentityToken }: BaseConnectorLoginParams): Promise<IProvider | null> {
super.checkConnectionRequirements();
if (!this.metamaskSDK) throw WalletLoginError.notConnectedError("Connector is not initialized");
const chainConfig = this.coreOptions.chains.find((x) => x.chainId === chainId);
if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available");
// Skip tracking for injected MetaMask since it's handled in connectTo
// Skip tracking for rehydration since only new connections are tracked
// Only track non-injected MetaMask when connection completes since it auto-initializes to generate QR code
const shouldTrack = !this.isInjected && !this.rehydrated;
const startTime = Date.now();
const eventData = {
connector: this.name,
connector_type: this.type,
is_injected: this.isInjected,
chain_id: getCaipChainId(chainConfig),
chain_name: chainConfig?.displayName,
chain_namespace: chainConfig?.chainNamespace,
};
try {
if (this.status !== CONNECTOR_STATUS.CONNECTING) {
this.status = CONNECTOR_STATUS.CONNECTING;
this.emit(CONNECTOR_EVENTS.CONNECTING, { connector: WALLET_CONNECTORS.METAMASK });
if (!this.metamaskSDK.isExtensionActive() && this.metamaskOptions?.headless) {
// when metamask is not injected and headless is true, broadcast the uri to the login modal
this.metamaskSDK.getProvider().on("display_uri", (uri) => {
this.updateConnectorData({ uri } as MetaMaskConnectorData);
});
}
await this.metamaskSDK.connect();
}
this.metamaskProvider = this.metamaskSDK.getProvider() as unknown as IProvider;
if (!this.metamaskProvider) throw WalletLoginError.notConnectedError("Failed to connect with provider");
// switch chain if not connected to the right chain
const currentChainId = (await this.metamaskProvider.request({ method: "eth_chainId" })) as string;
if (currentChainId !== chainConfig.chainId) {
await this.switchChain(chainConfig, true);
}
// handle disconnect event
const accountDisconnectHandler = (accounts: string[]) => {
if (accounts.length === 0) this.disconnect();
};
this.metamaskProvider.on("accountsChanged", accountDisconnectHandler);
this.metamaskProvider.once("disconnect", () => {
this.disconnect();
});
this.status = CONNECTOR_STATUS.CONNECTED;
// track connection events
if (shouldTrack) {
this.analytics?.track(ANALYTICS_EVENTS.CONNECTION_STARTED, eventData);
this.analytics?.track(ANALYTICS_EVENTS.CONNECTION_COMPLETED, {
...eventData,
duration: Date.now() - startTime,
});
}
let identityTokenInfo: IdentityTokenInfo | undefined;
if (getIdentityToken) {
identityTokenInfo = await this.getIdentityToken();
}
this.emit(CONNECTOR_EVENTS.CONNECTED, {
connector: WALLET_CONNECTORS.METAMASK,
reconnected: this.rehydrated,
provider: this.metamaskProvider,
identityTokenInfo,
} as CONNECTED_EVENT_DATA);
return this.metamaskProvider;
} catch (error) {
// ready again to be connected
this.status = CONNECTOR_STATUS.READY;
if (!this.rehydrated) this.emit(CONNECTOR_EVENTS.ERRORED, error as Web3AuthError);
this.rehydrated = false;
// track connection events
if (shouldTrack) {
this.analytics?.track(ANALYTICS_EVENTS.CONNECTION_STARTED, eventData);
this.analytics?.track(ANALYTICS_EVENTS.CONNECTION_FAILED, {
...eventData,
...getErrorAnalyticsProperties(error),
duration: Date.now() - startTime,
});
}
if (error instanceof Web3AuthError) throw error;
throw WalletLoginError.connectionError("Failed to login with MetaMask wallet", error);
}
}
async disconnect(options: { cleanup: boolean } = { cleanup: false }): Promise<void> {
if (!this.metamaskProvider) throw WalletLoginError.connectionError("MetaMask provider is not available");
await super.disconnectSession();
if (typeof this.metamaskProvider.removeAllListeners !== "undefined") this.metamaskProvider.removeAllListeners();
await this.metamaskSDK.terminate();
if (options.cleanup) {
this.status = CONNECTOR_STATUS.NOT_READY;
this.metamaskProvider = null;
} else {
// ready to be connected again
this.status = CONNECTOR_STATUS.READY;
}
await super.disconnect();
}
async getUserInfo(): Promise<Partial<UserInfo>> {
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
return {};
}
public async switchChain(params: { chainId: string }, init = false): Promise<void> {
super.checkSwitchChainRequirements(params, init);
const requestSwitchChain = () => this.metamaskProvider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] });
try {
await requestSwitchChain();
} catch (error) {
// If the error code is 4902, the network needs to be added
if ((error as { code?: number })?.code === 4902) {
const chainConfig = this.coreOptions.chains.find(
(x) =>
x.chainId === params.chainId && ([CHAIN_NAMESPACES.EIP155, CHAIN_NAMESPACES.SOLANA] as ChainNamespaceType[]).includes(x.chainNamespace)
);
await this.addChain(chainConfig);
await requestSwitchChain();
} else {
throw error;
}
}
}
public async enableMFA(): Promise<void> {
throw new Error("Method Not implemented");
}
public async manageMFA(): Promise<void> {
throw new Error("Method Not implemented");
}
private async addChain(chainConfig: CustomChainConfig): Promise<void> {
if (!this.metamaskProvider) throw WalletLoginError.connectionError("Injected provider is not available");
await this.metamaskProvider.request<AddEthereumChainConfig[], void>({
method: "wallet_addEthereumChain",
params: [
{
chainId: chainConfig.chainId,
chainName: chainConfig.displayName,
rpcUrls: [chainConfig.rpcTarget],
blockExplorerUrls: [chainConfig.blockExplorerUrl],
nativeCurrency: { name: chainConfig.tickerName, symbol: chainConfig.ticker, decimals: chainConfig.decimals || 18 },
iconUrls: [chainConfig.logo],
},
],
});
}
}
export const metaMaskConnector = (params?: Partial<MetaMaskSDKOptions>): ConnectorFn => {
return ({ coreOptions, analytics }: ConnectorParams) => {
return new MetaMaskConnector({ connectorSettings: params, coreOptions, analytics });
};
};