|
| 1 | +import { MetaMaskSDK, type MetaMaskSDKOptions, type SDKProvider } from "@metamask/sdk"; |
| 2 | + |
| 3 | +import { |
| 4 | + BaseConnectorSettings, |
| 5 | + CHAIN_NAMESPACES, |
| 6 | + ChainNamespaceType, |
| 7 | + CONNECTED_EVENT_DATA, |
| 8 | + CONNECTOR_CATEGORY, |
| 9 | + CONNECTOR_CATEGORY_TYPE, |
| 10 | + CONNECTOR_EVENTS, |
| 11 | + CONNECTOR_NAMESPACES, |
| 12 | + CONNECTOR_STATUS, |
| 13 | + CONNECTOR_STATUS_TYPE, |
| 14 | + ConnectorFn, |
| 15 | + ConnectorInitOptions, |
| 16 | + ConnectorNamespaceType, |
| 17 | + ConnectorParams, |
| 18 | + IProvider, |
| 19 | + UserInfo, |
| 20 | + WALLET_CONNECTORS, |
| 21 | + WalletLoginError, |
| 22 | + Web3AuthError, |
| 23 | +} from "@/core/base"; |
| 24 | + |
| 25 | +import { BaseEvmConnector } from "../base-evm-connector"; |
| 26 | + |
| 27 | +export interface MetaMaskConnectorOptions extends BaseConnectorSettings { |
| 28 | + connectorSettings?: MetaMaskSDKOptions; |
| 29 | +} |
| 30 | + |
| 31 | +class MetaMaskConnector extends BaseEvmConnector<void> { |
| 32 | + readonly connectorNamespace: ConnectorNamespaceType = CONNECTOR_NAMESPACES.EIP155; |
| 33 | + |
| 34 | + readonly currentChainNamespace: ChainNamespaceType = CHAIN_NAMESPACES.EIP155; |
| 35 | + |
| 36 | + readonly type: CONNECTOR_CATEGORY_TYPE = CONNECTOR_CATEGORY.EXTERNAL; |
| 37 | + |
| 38 | + readonly name: string = WALLET_CONNECTORS.METAMASK; |
| 39 | + |
| 40 | + public status: CONNECTOR_STATUS_TYPE = CONNECTOR_STATUS.NOT_READY; |
| 41 | + |
| 42 | + private metamaskProvider: SDKProvider | null = null; |
| 43 | + |
| 44 | + private metamaskSDK: MetaMaskSDK | null = null; |
| 45 | + |
| 46 | + private metamaskOptions: MetaMaskSDKOptions = { dappMetadata: { name: "Web3Auth" } }; |
| 47 | + |
| 48 | + constructor(connectorOptions: MetaMaskConnectorOptions) { |
| 49 | + super(connectorOptions); |
| 50 | + this.metamaskOptions = { ...this.metamaskOptions, ...connectorOptions.connectorSettings }; |
| 51 | + } |
| 52 | + |
| 53 | + get provider(): IProvider | null { |
| 54 | + if (this.status !== CONNECTOR_STATUS.NOT_READY && this.metamaskProvider) { |
| 55 | + return this.metamaskProvider as unknown as IProvider; |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + set provider(_: IProvider | null) { |
| 61 | + throw new Error("Not implemented"); |
| 62 | + } |
| 63 | + |
| 64 | + async init(options: ConnectorInitOptions): Promise<void> { |
| 65 | + await super.init(options); |
| 66 | + const chainConfig = this.coreOptions.chains.find((x) => x.chainId === options.chainId); |
| 67 | + super.checkInitializationRequirements({ chainConfig }); |
| 68 | + |
| 69 | + this.metamaskSDK = new MetaMaskSDK(this.metamaskOptions); |
| 70 | + |
| 71 | + this.status = CONNECTOR_STATUS.READY; |
| 72 | + this.emit(CONNECTOR_EVENTS.READY, WALLET_CONNECTORS.METAMASK); |
| 73 | + try { |
| 74 | + if (options.autoConnect) { |
| 75 | + this.rehydrated = true; |
| 76 | + await this.connect({ chainId: options.chainId }); |
| 77 | + } |
| 78 | + } catch (error) { |
| 79 | + this.emit(CONNECTOR_EVENTS.ERRORED, error as Web3AuthError); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + async connect({ chainId }: { chainId: string }): Promise<IProvider | null> { |
| 84 | + super.checkConnectionRequirements(); |
| 85 | + if (!this.metamaskSDK) throw WalletLoginError.notConnectedError("Connector is not initialized"); |
| 86 | + |
| 87 | + this.status = CONNECTOR_STATUS.CONNECTING; |
| 88 | + this.emit(CONNECTOR_EVENTS.CONNECTING, { connector: WALLET_CONNECTORS.METAMASK }); |
| 89 | + try { |
| 90 | + const chainConfig = this.coreOptions.chains.find((x) => x.chainId === chainId); |
| 91 | + if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); |
| 92 | + |
| 93 | + await this.metamaskSDK.connect(); |
| 94 | + this.metamaskProvider = this.metamaskSDK.getProvider(); |
| 95 | + |
| 96 | + const currentChainId = (await this.metamaskProvider.request({ method: "eth_chainId" })) as string; |
| 97 | + if (currentChainId !== chainConfig.chainId) { |
| 98 | + await this.switchChain(chainConfig, true); |
| 99 | + } |
| 100 | + this.status = CONNECTOR_STATUS.CONNECTED; |
| 101 | + if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider"); |
| 102 | + this.provider.once("disconnect", () => { |
| 103 | + // ready to be connected again |
| 104 | + this.disconnect(); |
| 105 | + }); |
| 106 | + this.emit(CONNECTOR_EVENTS.CONNECTED, { |
| 107 | + connector: WALLET_CONNECTORS.METAMASK, |
| 108 | + reconnected: this.rehydrated, |
| 109 | + provider: this.provider, |
| 110 | + } as CONNECTED_EVENT_DATA); |
| 111 | + return this.provider; |
| 112 | + } catch (error) { |
| 113 | + // ready again to be connected |
| 114 | + this.status = CONNECTOR_STATUS.READY; |
| 115 | + this.rehydrated = false; |
| 116 | + this.emit(CONNECTOR_EVENTS.ERRORED, error as Web3AuthError); |
| 117 | + if (error instanceof Web3AuthError) throw error; |
| 118 | + throw WalletLoginError.connectionError("Failed to login with MetaMask wallet", error); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + async disconnect(options: { cleanup: boolean } = { cleanup: false }): Promise<void> { |
| 123 | + await super.disconnectSession(); |
| 124 | + this.provider?.removeAllListeners(); |
| 125 | + if (options.cleanup) { |
| 126 | + this.status = CONNECTOR_STATUS.NOT_READY; |
| 127 | + this.metamaskProvider = null; |
| 128 | + } else { |
| 129 | + // ready to be connected again |
| 130 | + this.status = CONNECTOR_STATUS.READY; |
| 131 | + } |
| 132 | + await super.disconnect(); |
| 133 | + } |
| 134 | + |
| 135 | + async getUserInfo(): Promise<Partial<UserInfo>> { |
| 136 | + if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); |
| 137 | + return {}; |
| 138 | + } |
| 139 | + |
| 140 | + public async switchChain(params: { chainId: string }, init = false): Promise<void> { |
| 141 | + super.checkSwitchChainRequirements(params, init); |
| 142 | + await this.metamaskProvider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] }); |
| 143 | + } |
| 144 | + |
| 145 | + public async enableMFA(): Promise<void> { |
| 146 | + throw new Error("Method Not implemented"); |
| 147 | + } |
| 148 | + |
| 149 | + public async manageMFA(): Promise<void> { |
| 150 | + throw new Error("Method Not implemented"); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +export const metaMaskConnector = (params?: MetaMaskSDKOptions): ConnectorFn => { |
| 155 | + return ({ coreOptions }: ConnectorParams) => { |
| 156 | + return new MetaMaskConnector({ connectorSettings: params, coreOptions }); |
| 157 | + }; |
| 158 | +}; |
0 commit comments