UNPKG

@web3auth/no-modal

Version:
71 lines (67 loc) 4.19 kB
'use strict'; var _defineProperty = require('@babel/runtime/helpers/defineProperty'); var auth = require('@web3auth/auth'); var IChainInterface = require('../chain/IChainInterface.js'); var index$1 = require('../errors/index.js'); var index = require('../wallet/index.js'); var connectorStatus = require('./connectorStatus.js'); var constants = require('./constants.js'); var baseControllers = require('@toruslabs/base-controllers'); class BaseConnector extends auth.SafeEventEmitter { constructor(options) { super(); _defineProperty(this, "connectorData", {}); _defineProperty(this, "isInjected", void 0); _defineProperty(this, "icon", void 0); _defineProperty(this, "coreOptions", void 0); _defineProperty(this, "rehydrated", false); this.coreOptions = options.coreOptions; } get connected() { return connectorStatus.CONNECTED_STATUSES.includes(this.status); } get canAuthorize() { return connectorStatus.CAN_AUTHORIZE_STATUSES.includes(this.status); } checkConnectionRequirements() { // we reconnect without killing existing Wallet Connect or Metamask Connect session on calling connect again. if (this.name === index.WALLET_CONNECTORS.WALLET_CONNECT_V2 && this.status === constants.CONNECTOR_STATUS.CONNECTING) return; if (this.name === index.WALLET_CONNECTORS.METAMASK && !this.isInjected && this.status === constants.CONNECTOR_STATUS.CONNECTING) return; if (this.status === constants.CONNECTOR_STATUS.CONNECTING) throw index$1.WalletInitializationError.notReady("Already connecting"); if (this.connected) throw index$1.WalletLoginError.connectionError("Already connected"); if (this.status !== constants.CONNECTOR_STATUS.READY) throw index$1.WalletLoginError.connectionError("Wallet connector is not ready yet, Please wait for init function to resolve before calling connect/connectTo function"); } checkInitializationRequirements({ chainConfig }) { if (!this.coreOptions.clientId) throw index$1.WalletInitializationError.invalidParams("Please initialize Web3Auth with a valid clientId in constructor"); if (!chainConfig) throw index$1.WalletInitializationError.invalidParams("chainConfig is required before initialization"); if (!chainConfig.rpcTarget && chainConfig.chainNamespace !== baseControllers.CHAIN_NAMESPACES.OTHER) { throw index$1.WalletInitializationError.invalidParams("rpcTarget is required in chainConfig"); } if (!chainConfig.chainId && chainConfig.chainNamespace !== baseControllers.CHAIN_NAMESPACES.OTHER) { throw index$1.WalletInitializationError.invalidParams("chainID is required in chainConfig"); } if (this.connectorNamespace !== IChainInterface.CONNECTOR_NAMESPACES.MULTICHAIN && this.connectorNamespace !== chainConfig.chainNamespace) throw index$1.WalletInitializationError.invalidParams("Connector doesn't support this chain namespace"); if (this.status === constants.CONNECTOR_STATUS.NOT_READY) return; if (this.connected) throw index$1.WalletInitializationError.notReady("Already connected"); if (this.status === constants.CONNECTOR_STATUS.READY) throw index$1.WalletInitializationError.notReady("Connector is already initialized"); } checkDisconnectionRequirements() { if (!this.connected) throw index$1.WalletLoginError.disconnectionError("Not connected with wallet"); } checkSwitchChainRequirements(params, init = false) { if (!init && !this.provider) throw index$1.WalletLoginError.notConnectedError("Not connected with wallet."); if (!this.coreOptions.chains) throw index$1.WalletInitializationError.invalidParams("chainConfigs is required"); const doesChainExist = this.coreOptions.chains.some(x => x.chainId === params.chainId && (x.chainNamespace === this.connectorNamespace || this.connectorNamespace === IChainInterface.CONNECTOR_NAMESPACES.MULTICHAIN)); if (!doesChainExist) throw index$1.WalletInitializationError.invalidParams("Invalid chainId"); } updateConnectorData(data) { this.connectorData = data; this.emit(constants.CONNECTOR_EVENTS.CONNECTOR_DATA_UPDATED, { connectorName: this.name, data }); } } exports.BaseConnector = BaseConnector;