UNPKG

@web3auth/no-modal

Version:
174 lines (163 loc) 9.97 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { SafeEventEmitter } from '@web3auth/auth'; export { BUTTON_POSITION, CONFIRMATION_STRATEGY } from '@web3auth/ws-embed'; import { EVM_PLUGINS, PLUGIN_STATUS, PLUGIN_NAMESPACES, PLUGIN_EVENTS } from '../../base/plugin/IPlugin.js'; import { WalletServicesPluginError } from '../../base/plugin/errors.js'; import { CAN_AUTHORIZE_STATUSES } from '../../base/connector/connectorStatus.js'; import { WALLET_CONNECTORS } from '../../base/wallet/index.js'; import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers'; import { log } from '../../base/loglevel.js'; import { ANALYTICS_EVENTS } from '../../base/analytics.js'; class WalletServicesPlugin extends SafeEventEmitter { constructor(...args) { super(...args); _defineProperty(this, "name", EVM_PLUGINS.WALLET_SERVICES); _defineProperty(this, "status", PLUGIN_STATUS.DISCONNECTED); _defineProperty(this, "SUPPORTED_CONNECTORS", [WALLET_CONNECTORS.AUTH]); _defineProperty(this, "pluginNamespace", PLUGIN_NAMESPACES.MULTICHAIN); _defineProperty(this, "wsEmbedInstance", void 0); _defineProperty(this, "provider", null); _defineProperty(this, "web3auth", null); _defineProperty(this, "isInitialized", false); _defineProperty(this, "analytics", void 0); } get proxyProvider() { var _this$wsEmbedInstance; return (_this$wsEmbedInstance = this.wsEmbedInstance) !== null && _this$wsEmbedInstance !== void 0 && _this$wsEmbedInstance.provider ? this.wsEmbedInstance.provider : null; } async initWithWeb3Auth(web3auth, _whiteLabel, analytics) { if (this.isInitialized) return; if (!web3auth) throw WalletServicesPluginError.web3authRequired(); if (web3auth.provider && !this.SUPPORTED_CONNECTORS.includes(web3auth.connectedConnectorName)) throw WalletServicesPluginError.notInitialized(); const currentChainConfig = web3auth.currentChain; if (![CHAIN_NAMESPACES.EIP155, CHAIN_NAMESPACES.SOLANA].includes(currentChainConfig === null || currentChainConfig === void 0 ? void 0 : currentChainConfig.chainNamespace)) throw WalletServicesPluginError.unsupportedChainNamespace(); // Not connected yet to auth if (web3auth.provider) { this.provider = web3auth.provider; } this.web3auth = web3auth; this.analytics = analytics; // Auth connector uses WS embed const authInstance = web3auth.getConnector(WALLET_CONNECTORS.AUTH); if (!authInstance || !authInstance.wsEmbed) throw WalletServicesPluginError.web3AuthNotConnected(); this.wsEmbedInstance = authInstance.wsEmbed; this.isInitialized = true; this.status = PLUGIN_STATUS.READY; this.emit(PLUGIN_EVENTS.READY); } initWithProvider() { throw new Error("Method not implemented."); } async connect() { // if web3auth is being used and connected to unsupported connector throw error if (!this.isInitialized) throw WalletServicesPluginError.notInitialized(); this.emit(PLUGIN_EVENTS.CONNECTING); this.status = PLUGIN_STATUS.CONNECTING; if (!this.provider) { var _this$web3auth; if ((_this$web3auth = this.web3auth) !== null && _this$web3auth !== void 0 && _this$web3auth.provider) { this.provider = this.web3auth.provider; } } if (!CAN_AUTHORIZE_STATUSES.includes(this.web3auth.status)) { throw WalletServicesPluginError.web3AuthNotConnected(); } else if (!this.web3auth.provider) { throw WalletServicesPluginError.providerRequired(); } try { this.emit(PLUGIN_EVENTS.CONNECTED); this.status = PLUGIN_STATUS.CONNECTED; } catch (error) { log.error(error); this.status = PLUGIN_STATUS.ERRORED; this.emit(PLUGIN_EVENTS.ERRORED, { error: error.message || "Something went wrong" }); } } async showWalletConnectScanner(showWalletConnectParams) { var _this$wsEmbedInstance2, _this$analytics; if (!((_this$wsEmbedInstance2 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance2 !== void 0 && _this$wsEmbedInstance2.isLoggedIn)) throw WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics = this.analytics) === null || _this$analytics === void 0 || _this$analytics.track(ANALYTICS_EVENTS.WALLET_CONNECT_SCANNER_CLICKED, { is_visible: showWalletConnectParams === null || showWalletConnectParams === void 0 ? void 0 : showWalletConnectParams.show }); return this.wsEmbedInstance.showWalletConnectScanner(showWalletConnectParams); } async showFunding(showFundingParams) { var _this$wsEmbedInstance3, _this$analytics2; if (!((_this$wsEmbedInstance3 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance3 !== void 0 && _this$wsEmbedInstance3.isLoggedIn)) throw WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics2 = this.analytics) === null || _this$analytics2 === void 0 || _this$analytics2.track(ANALYTICS_EVENTS.WALLET_FUNDING_CLICKED, { is_visible: showFundingParams === null || showFundingParams === void 0 ? void 0 : showFundingParams.show }); return this.wsEmbedInstance.showFunding(showFundingParams); } async showCheckout(showCheckoutParams) { var _this$wsEmbedInstance4, _this$analytics3; if (!((_this$wsEmbedInstance4 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance4 !== void 0 && _this$wsEmbedInstance4.isLoggedIn)) throw WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics3 = this.analytics) === null || _this$analytics3 === void 0 || _this$analytics3.track(ANALYTICS_EVENTS.WALLET_CHECKOUT_CLICKED, { is_visible: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.show, receive_wallet_address_enabled: !!(showCheckoutParams !== null && showCheckoutParams !== void 0 && showCheckoutParams.receiveWalletAddress), token_list: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.tokenList, fiat_list: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.fiatList, crypto: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.crypto, fiat: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.fiat, fiat_amount: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.fiatAmount, crypto_amount: showCheckoutParams === null || showCheckoutParams === void 0 ? void 0 : showCheckoutParams.cryptoAmount }); return this.wsEmbedInstance.showCheckout(showCheckoutParams); } async showReceive(showReceiveParams) { var _this$wsEmbedInstance5, _this$analytics4; if (!((_this$wsEmbedInstance5 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance5 !== void 0 && _this$wsEmbedInstance5.isLoggedIn)) throw WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics4 = this.analytics) === null || _this$analytics4 === void 0 || _this$analytics4.track(ANALYTICS_EVENTS.WALLET_RECEIVE_CLICKED, { is_visible: showReceiveParams === null || showReceiveParams === void 0 ? void 0 : showReceiveParams.show }); return this.wsEmbedInstance.showReceive(showReceiveParams); } async showWalletUi(showWalletUiParams) { var _this$wsEmbedInstance6, _this$analytics5; if (!((_this$wsEmbedInstance6 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance6 !== void 0 && _this$wsEmbedInstance6.isLoggedIn)) throw WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics5 = this.analytics) === null || _this$analytics5 === void 0 || _this$analytics5.track(ANALYTICS_EVENTS.WALLET_UI_CLICKED, { is_visible: showWalletUiParams === null || showWalletUiParams === void 0 ? void 0 : showWalletUiParams.show, path: showWalletUiParams === null || showWalletUiParams === void 0 ? void 0 : showWalletUiParams.path }); return this.wsEmbedInstance.showWalletUi(showWalletUiParams); } async showSwap(showSwapParams) { var _this$wsEmbedInstance7, _this$analytics6; if (!((_this$wsEmbedInstance7 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance7 !== void 0 && _this$wsEmbedInstance7.isLoggedIn)) throw WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics6 = this.analytics) === null || _this$analytics6 === void 0 || _this$analytics6.track(ANALYTICS_EVENTS.WALLET_SWAP_CLICKED, { is_visible: showSwapParams === null || showSwapParams === void 0 ? void 0 : showSwapParams.show, from_token: showSwapParams === null || showSwapParams === void 0 ? void 0 : showSwapParams.fromToken, to_token: showSwapParams === null || showSwapParams === void 0 ? void 0 : showSwapParams.toToken, from_value_enabled: !!(showSwapParams !== null && showSwapParams !== void 0 && showSwapParams.fromValue), to_address_enabled: !!(showSwapParams !== null && showSwapParams !== void 0 && showSwapParams.toAddress) }); return this.wsEmbedInstance.showSwap(showSwapParams); } async cleanup() { var _this$wsEmbedInstance8; return (_this$wsEmbedInstance8 = this.wsEmbedInstance) === null || _this$wsEmbedInstance8 === void 0 ? void 0 : _this$wsEmbedInstance8.cleanUp(); } async disconnect() { var _this$wsEmbedInstance9; if (this.status !== PLUGIN_STATUS.CONNECTED) throw WalletServicesPluginError.invalidSession("Wallet Services plugin is not connected"); if ((_this$wsEmbedInstance9 = this.wsEmbedInstance) !== null && _this$wsEmbedInstance9 !== void 0 && _this$wsEmbedInstance9.isLoggedIn) { await this.wsEmbedInstance.logout(); } this.emit(PLUGIN_EVENTS.DISCONNECTED); this.status = PLUGIN_STATUS.DISCONNECTED; } } const walletServicesPlugin = () => { return () => { return new WalletServicesPlugin(); }; }; export { walletServicesPlugin };