UNPKG

@web3auth/no-modal

Version:
191 lines (188 loc) 7.11 kB
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { BaseEvmConnector } from '../base-evm-connector/baseEvmConnector.js'; import { CONNECTOR_NAMESPACES } from '../../base/chain/IChainInterface.js'; import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers'; import { CONNECTOR_CATEGORY, CONNECTOR_STATUS, CONNECTOR_EVENTS } from '../../base/connector/constants.js'; import { WALLET_CONNECTORS } from '../../base/wallet/index.js'; import { WalletLoginError, Web3AuthError } from '../../base/errors/index.js'; class CoinbaseConnector extends BaseEvmConnector { constructor(connectorOptions) { super(connectorOptions); _defineProperty(this, "connectorNamespace", CONNECTOR_NAMESPACES.EIP155); _defineProperty(this, "currentChainNamespace", CHAIN_NAMESPACES.EIP155); _defineProperty(this, "type", CONNECTOR_CATEGORY.EXTERNAL); _defineProperty(this, "name", WALLET_CONNECTORS.COINBASE); _defineProperty(this, "status", CONNECTOR_STATUS.NOT_READY); _defineProperty(this, "coinbaseProvider", null); _defineProperty(this, "coinbaseOptions", { appName: "Web3Auth" }); this.coinbaseOptions = _objectSpread(_objectSpread({}, this.coinbaseOptions), connectorOptions.connectorSettings); } get provider() { if (this.status !== CONNECTOR_STATUS.NOT_READY && this.coinbaseProvider) { return this.coinbaseProvider; } return null; } set provider(_) { throw new Error("Not implemented"); } async init(options) { await super.init(options); const chainConfig = this.coreOptions.chains.find(x => x.chainId === options.chainId); super.checkInitializationRequirements({ chainConfig }); const { createCoinbaseWalletSDK } = await import('@coinbase/wallet-sdk'); const coinbaseInstance = createCoinbaseWalletSDK(_objectSpread(_objectSpread({}, this.coinbaseOptions), {}, { preference: { options: this.coinbaseOptions.options || "smartWalletOnly" }, appChainIds: this.coreOptions.chains.map(x => Number.parseInt(x.chainId, 16)), appName: this.coinbaseOptions.appName || "Web3Auth", appLogoUrl: this.coinbaseOptions.appLogoUrl || "" })); this.coinbaseProvider = coinbaseInstance.getProvider(); this.status = CONNECTOR_STATUS.READY; this.emit(CONNECTOR_EVENTS.READY, WALLET_CONNECTORS.COINBASE); try { if (options.autoConnect) { this.rehydrated = true; const provider = await this.connect({ chainId: options.chainId, getIdentityToken: options.getIdentityToken }); // the connect function could fail silently as well. if (!provider) { this.rehydrated = false; throw WalletLoginError.connectionError("Failed to rehydrate."); } } } catch (error) { this.emit(CONNECTOR_EVENTS.REHYDRATION_ERROR, error); } } async connect({ chainId, getIdentityToken }) { super.checkConnectionRequirements(); if (!this.coinbaseProvider) throw WalletLoginError.notConnectedError("Connector is not initialized"); this.status = CONNECTOR_STATUS.CONNECTING; this.emit(CONNECTOR_EVENTS.CONNECTING, { connector: WALLET_CONNECTORS.COINBASE }); try { const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId); if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); await this.coinbaseProvider.request({ method: "eth_requestAccounts" }); const currentChainId = await this.coinbaseProvider.request({ method: "eth_chainId" }); if (currentChainId !== chainConfig.chainId) { await this.switchChain(chainConfig, true); } this.status = CONNECTOR_STATUS.CONNECTED; if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider"); this.provider.once("disconnect", () => { // ready to be connected again this.disconnect(); }); let identityTokenInfo; this.emit(CONNECTOR_EVENTS.CONNECTED, { connector: WALLET_CONNECTORS.COINBASE, reconnected: this.rehydrated, provider: this.provider, identityTokenInfo }); if (getIdentityToken) { identityTokenInfo = await this.getIdentityToken(); } return this.provider; } catch (error) { // ready again to be connected this.status = CONNECTOR_STATUS.READY; // only throw error if the connector is not rehydrated. if (!this.rehydrated) this.emit(CONNECTOR_EVENTS.ERRORED, error); this.rehydrated = false; if (error instanceof Web3AuthError) throw error; throw WalletLoginError.connectionError("Failed to login with coinbase wallet", error); } } async disconnect(options = { cleanup: false }) { var _this$provider; await super.disconnectSession(); (_this$provider = this.provider) === null || _this$provider === void 0 || _this$provider.removeAllListeners(); if (options.cleanup) { this.status = CONNECTOR_STATUS.NOT_READY; this.coinbaseProvider = null; } else { // ready to be connected again this.status = CONNECTOR_STATUS.READY; } await super.disconnect(); } async getUserInfo() { if (!this.canAuthorize) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); return {}; } async switchChain(params, init = false) { super.checkSwitchChainRequirements(params, init); try { await this.coinbaseProvider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] }); } catch (switchError) { // 4902 indicates that the client does not recognize the Harmony One network if (switchError.code === 4902) { const chainConfig = this.coreOptions.chains.find(x => x.chainId === params.chainId); if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); await this.coinbaseProvider.request({ method: "wallet_addEthereumChain", params: [{ chainId: chainConfig.chainId, rpcUrls: [chainConfig.rpcTarget], chainName: chainConfig.displayName, nativeCurrency: { name: chainConfig.tickerName, symbol: chainConfig.ticker, decimals: chainConfig.decimals || 18 }, blockExplorerUrls: [chainConfig.blockExplorerUrl], iconUrls: [chainConfig.logo] }] }); return; } throw switchError; } } async enableMFA() { throw new Error("Method Not implemented"); } async manageMFA() { throw new Error("Method Not implemented"); } } const coinbaseConnector = params => { return ({ coreOptions }) => { return new CoinbaseConnector({ connectorSettings: params, coreOptions }); }; }; export { coinbaseConnector };