UNPKG

@web3auth/no-modal

Version:
212 lines (208 loc) 8.31 kB
'use strict'; var _objectSpread = require('@babel/runtime/helpers/objectSpread2'); var _defineProperty = require('@babel/runtime/helpers/defineProperty'); require('@segment/analytics-next'); require('../../base/loglevel.js'); var IChainInterface = require('../../base/chain/IChainInterface.js'); var baseControllers = require('@toruslabs/base-controllers'); require('@toruslabs/session-manager'); require('@web3auth/auth'); var index$1 = require('../../base/errors/index.js'); require('@toruslabs/constants'); require('@toruslabs/http-helpers'); require('../../base/constants.js'); var index = require('../../base/wallet/index.js'); require('../../base/connector/connectorStatus.js'); var constants = require('../../base/connector/constants.js'); require('jwt-decode'); require('../../base/plugin/errors.js'); require('../../base/plugin/IPlugin.js'); var baseEvmConnector = require('../base-evm-connector/baseEvmConnector.js'); var utils = require('../utils.js'); class BaseAccountConnector extends baseEvmConnector.BaseEvmConnector { constructor(connectorOptions) { super(connectorOptions); _defineProperty(this, "connectorNamespace", IChainInterface.CONNECTOR_NAMESPACES.EIP155); _defineProperty(this, "currentChainNamespace", baseControllers.CHAIN_NAMESPACES.EIP155); _defineProperty(this, "type", constants.CONNECTOR_CATEGORY.EXTERNAL); _defineProperty(this, "name", index.WALLET_CONNECTORS.BASE_ACCOUNT); _defineProperty(this, "status", constants.CONNECTOR_STATUS.NOT_READY); _defineProperty(this, "baseAccountProvider", null); _defineProperty(this, "baseAccountOptions", { appName: "Web3Auth" }); this.baseAccountOptions = _objectSpread(_objectSpread({}, this.baseAccountOptions), connectorOptions.connectorSettings); } get provider() { if (this.status !== constants.CONNECTOR_STATUS.NOT_READY && this.baseAccountProvider) { return this.baseAccountProvider; } 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 { createBaseAccountSDK } = await import('@base-org/account'); let appName = this.baseAccountOptions.appName || "Web3Auth"; let appLogoUrl = this.baseAccountOptions.appLogoUrl || ""; if (typeof window !== "undefined") { if (!this.baseAccountOptions.appName) { appName = utils.getSiteName(window) || "Web3Auth"; } if (!this.baseAccountOptions.appLogoUrl) { appLogoUrl = (await utils.getSiteIcon(window)) || ""; } } const appChainIds = this.coreOptions.chains.filter(x => x.chainNamespace === baseControllers.CHAIN_NAMESPACES.EIP155).map(x => Number.parseInt(x.chainId, 16)); const sdk = createBaseAccountSDK(_objectSpread(_objectSpread({}, this.baseAccountOptions), {}, { appName, appLogoUrl: appLogoUrl || null, appChainIds: this.baseAccountOptions.appChainIds || appChainIds })); this.baseAccountProvider = sdk.getProvider(); this.status = constants.CONNECTOR_STATUS.READY; this.emit(constants.CONNECTOR_EVENTS.READY, index.WALLET_CONNECTORS.BASE_ACCOUNT); try { if (options.autoConnect) { this.rehydrated = true; const connection = await this.connect({ chainId: options.chainId, getAuthTokenInfo: options.getAuthTokenInfo }); if (!connection) { this.rehydrated = false; throw index$1.WalletLoginError.connectionError("Failed to rehydrate."); } } } catch (error) { this.emit(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, error); } } async connect({ chainId, getAuthTokenInfo }) { super.checkConnectionRequirements(); if (!this.baseAccountProvider) throw index$1.WalletLoginError.notConnectedError("Connector is not initialized"); this.status = constants.CONNECTOR_STATUS.CONNECTING; this.emit(constants.CONNECTOR_EVENTS.CONNECTING, { connector: index.WALLET_CONNECTORS.BASE_ACCOUNT }); try { const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId); if (!chainConfig) throw index$1.WalletLoginError.connectionError("Chain config is not available"); await this.baseAccountProvider.request({ method: "eth_requestAccounts" }); const currentChainId = await this.baseAccountProvider.request({ method: "eth_chainId" }); if (currentChainId !== chainConfig.chainId) { await this.switchChain(chainConfig, true); } this.status = constants.CONNECTOR_STATUS.CONNECTED; if (!this.provider) throw index$1.WalletLoginError.notConnectedError("Failed to connect with provider"); this.provider.once("disconnect", () => { this.disconnect(); }); this.emit(constants.CONNECTOR_EVENTS.CONNECTED, { connectorName: index.WALLET_CONNECTORS.BASE_ACCOUNT, reconnected: this.rehydrated, ethereumProvider: this.provider, solanaWallet: null, connectorNamespace: this.connectorNamespace }); await this.authorizeOrDisconnect(getAuthTokenInfo); return { ethereumProvider: this.provider, solanaWallet: null, connectorName: this.name, connectorNamespace: this.connectorNamespace }; } catch (error) { this.status = constants.CONNECTOR_STATUS.READY; if (!this.rehydrated) this.emit(constants.CONNECTOR_EVENTS.ERRORED, error); this.rehydrated = false; if (error instanceof index$1.Web3AuthError) throw error; throw index$1.WalletLoginError.connectionError("Failed to login with Base Account", 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 = constants.CONNECTOR_STATUS.NOT_READY; this.baseAccountProvider = null; } else { this.status = constants.CONNECTOR_STATUS.READY; } await super.disconnect(); } async getUserInfo() { if (!this.canAuthorize) throw index$1.WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); return {}; } async switchChain(params, init = false) { super.checkSwitchChainRequirements(params, init); try { var _this$baseAccountProv; await ((_this$baseAccountProv = this.baseAccountProvider) === null || _this$baseAccountProv === void 0 ? void 0 : _this$baseAccountProv.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] })); } catch (switchError) { if (switchError.code === 4902) { var _this$baseAccountProv2; const chainConfig = this.coreOptions.chains.find(x => x.chainId === params.chainId); if (!chainConfig) throw index$1.WalletLoginError.connectionError("Chain config is not available"); await ((_this$baseAccountProv2 = this.baseAccountProvider) === null || _this$baseAccountProv2 === void 0 ? void 0 : _this$baseAccountProv2.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 baseAccountConnector = params => { return ({ coreOptions }) => { return new BaseAccountConnector({ connectorSettings: params, coreOptions }); }; }; exports.baseAccountConnector = baseAccountConnector;