UNPKG

@web3auth-mpc/torus-evm-adapter

Version:
201 lines (197 loc) 7.87 kB
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import Torus from '@toruslabs/torus-embed'; import { WALLET_ADAPTERS, ADAPTER_NAMESPACES, CHAIN_NAMESPACES, ADAPTER_CATEGORY, ADAPTER_STATUS, log, ADAPTER_EVENTS, WalletInitializationError, Web3AuthError, WalletLoginError } from '@web3auth-mpc/base'; import { BaseEvmAdapter } from '@web3auth-mpc/base-evm-adapter'; class TorusWalletAdapter extends BaseEvmAdapter { constructor() { let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; super(params); _defineProperty(this, "name", WALLET_ADAPTERS.TORUS_EVM); _defineProperty(this, "adapterNamespace", ADAPTER_NAMESPACES.EIP155); _defineProperty(this, "currentChainNamespace", CHAIN_NAMESPACES.EIP155); _defineProperty(this, "type", ADAPTER_CATEGORY.EXTERNAL); _defineProperty(this, "status", ADAPTER_STATUS.NOT_READY); _defineProperty(this, "torusInstance", null); _defineProperty(this, "torusWalletOptions", void 0); _defineProperty(this, "initParams", void 0); _defineProperty(this, "loginSettings", {}); this.torusWalletOptions = params.adapterSettings || {}; this.initParams = params.initParams || {}; this.loginSettings = params.loginSettings || {}; } get provider() { if (this.status !== ADAPTER_STATUS.NOT_READY && this.torusInstance) { return this.torusInstance.provider; } return null; } set provider(_) { throw new Error("Not implemented"); } async init() { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; await super.init(options); super.checkInitializationRequirements(); const { chainId, blockExplorer, displayName, rpcTarget, ticker, tickerName } = this.chainConfig; const network = { chainId: Number.parseInt(chainId, 16), host: rpcTarget, blockExplorer, networkName: displayName, ticker, tickerName // decimals: decimals || 18, }; this.torusInstance = new Torus(this.torusWalletOptions); log.debug("initializing torus evm adapter init"); await this.torusInstance.init(_objectSpread(_objectSpread({ showTorusButton: false }, this.initParams), {}, { network })); this.status = ADAPTER_STATUS.READY; this.emit(ADAPTER_EVENTS.READY, WALLET_ADAPTERS.TORUS_EVM); try { log.debug("initializing torus evm adapter"); if (options.autoConnect) { this.rehydrated = true; await this.connect(); } } catch (error) { log.error("Failed to connect with torus evm provider", error); this.emit(ADAPTER_EVENTS.ERRORED, error); } } async connect() { super.checkConnectionRequirements(); if (!this.torusInstance) throw WalletInitializationError.notReady("Torus wallet is not initialized"); this.status = ADAPTER_STATUS.CONNECTING; this.emit(ADAPTER_EVENTS.CONNECTING, { adapter: WALLET_ADAPTERS.TORUS_EVM }); try { await this.torusInstance.login(this.loginSettings); const chainId = await this.torusInstance.provider.request({ method: "eth_chainId" }); if (chainId && parseInt(chainId) !== parseInt(this.chainConfig.chainId, 16)) { const { chainId: _chainId, blockExplorer, displayName, rpcTarget, ticker, tickerName } = this.chainConfig; const network = { chainId: Number.parseInt(_chainId, 16), host: rpcTarget, blockExplorer, networkName: displayName, tickerName, ticker }; // in some cases when user manually switches chain and relogin then adapter will not connect to initially passed // chainConfig but will connect to the one that user switched to. // So here trying to switch network to the one that was initially passed in chainConfig. await this.torusInstance.setProvider(_objectSpread({}, network)); const updatedChainID = await this.torusInstance.ethereum.request({ method: "eth_chainId" }); if (updatedChainID && parseInt(updatedChainID) !== parseInt(this.chainConfig.chainId, 16)) { throw WalletInitializationError.fromCode(5000, `Not connected to correct chainId. Expected: ${this.chainConfig.chainId}, Current: ${updatedChainID}`); } } this.status = ADAPTER_STATUS.CONNECTED; this.torusInstance.showTorusButton(); this.emit(ADAPTER_STATUS.CONNECTED, { adapter: WALLET_ADAPTERS.TORUS_EVM, reconnected: this.rehydrated }); return this.provider; } catch (error) { // ready again to be connected this.status = ADAPTER_STATUS.READY; this.rehydrated = false; this.emit(ADAPTER_STATUS.ERRORED, error); throw error instanceof Web3AuthError ? error : WalletLoginError.connectionError("Failed to login with torus wallet"); } } async disconnect() { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { cleanup: false }; await super.disconnectSession(); if (!this.torusInstance) throw WalletInitializationError.notReady("Torus wallet is not initialized"); await this.torusInstance.logout(); this.torusInstance.hideTorusButton(); if (options.cleanup) { this.status = ADAPTER_STATUS.NOT_READY; this.torusInstance = null; } else { // ready to be connected again this.status = ADAPTER_STATUS.READY; } await super.disconnect(); } async getUserInfo() { if (this.status !== ADAPTER_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet"); if (!this.torusInstance) throw WalletInitializationError.notReady("Torus wallet is not initialized"); const userInfo = await this.torusInstance.getUserInfo(""); return userInfo; } async addChain(chainConfig) { let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; super.checkAddChainRequirements(chainConfig, init); // TODO: add these in torus wallet. // await this.torusInstance?.provider.request({ // method: "wallet_addEthereumChain", // params: [ // { // chainId: chainConfig.chainId, // chainName: chainConfig.displayName, // rpcUrls: [chainConfig.rpcTarget], // blockExplorerUrls: [chainConfig.blockExplorer], // nativeCurrency: { // name: chainConfig.tickerName, // symbol: chainConfig.ticker, // decimals: chainConfig.decimals || 18, // }, // }, // ], // }); this.addChainConfig(chainConfig); } async switchChain(params) { var _this$torusInstance; let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; super.checkSwitchChainRequirements(params, init); // TODO: add these in torus wallet. // await this.torusInstance?.provider.request({ // method: "wallet_switchEthereumChain", // params: [{ chainId: params.chainId }], // }); const chainConfig = this.getChainConfig(params.chainId); await ((_this$torusInstance = this.torusInstance) === null || _this$torusInstance === void 0 ? void 0 : _this$torusInstance.setProvider({ host: chainConfig.rpcTarget, chainId: parseInt(chainConfig.chainId, 16), networkName: chainConfig.displayName, blockExplorer: chainConfig.blockExplorer, ticker: chainConfig.ticker, tickerName: chainConfig.tickerName })); this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) }); } } export { TorusWalletAdapter }; //# sourceMappingURL=torusEvmAdapter.esm.js.map