UNPKG

@web3auth/no-modal

Version:
181 lines (177 loc) 10.7 kB
'use strict'; var _defineProperty = require('@babel/runtime/helpers/defineProperty'); var auth = require('@web3auth/auth'); var analytics = require('../../base/analytics.js'); var baseControllers = require('@toruslabs/base-controllers'); require('../../base/errors/index.js'); var index = require('../../base/wallet/index.js'); var connectorStatus = require('../../base/connector/connectorStatus.js'); require('../../base/connector/constants.js'); require('jwt-decode'); var loglevel = require('../../base/loglevel.js'); var errors = require('../../base/plugin/errors.js'); var IPlugin = require('../../base/plugin/IPlugin.js'); require('@toruslabs/constants'); require('@toruslabs/http-helpers'); var wsEmbed = require('@web3auth/ws-embed'); class WalletServicesPlugin extends auth.SafeEventEmitter { constructor(...args) { super(...args); _defineProperty(this, "name", IPlugin.EVM_PLUGINS.WALLET_SERVICES); _defineProperty(this, "status", IPlugin.PLUGIN_STATUS.DISCONNECTED); _defineProperty(this, "SUPPORTED_CONNECTORS", [index.WALLET_CONNECTORS.AUTH]); _defineProperty(this, "pluginNamespace", IPlugin.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 errors.WalletServicesPluginError.web3authRequired(); if (web3auth.provider && !this.SUPPORTED_CONNECTORS.includes(web3auth.connectedConnectorName)) throw errors.WalletServicesPluginError.notInitialized(); const currentChainConfig = web3auth.currentChain; if (![baseControllers.CHAIN_NAMESPACES.EIP155, baseControllers.CHAIN_NAMESPACES.SOLANA].includes(currentChainConfig === null || currentChainConfig === void 0 ? void 0 : currentChainConfig.chainNamespace)) throw errors.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(index.WALLET_CONNECTORS.AUTH); if (!authInstance || !authInstance.wsEmbed) throw errors.WalletServicesPluginError.web3AuthNotConnected(); this.wsEmbedInstance = authInstance.wsEmbed; this.isInitialized = true; this.status = IPlugin.PLUGIN_STATUS.READY; this.emit(IPlugin.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 errors.WalletServicesPluginError.notInitialized(); this.emit(IPlugin.PLUGIN_EVENTS.CONNECTING); this.status = IPlugin.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 (!connectorStatus.CAN_AUTHORIZE_STATUSES.includes(this.web3auth.status)) { throw errors.WalletServicesPluginError.web3AuthNotConnected(); } else if (!this.web3auth.provider) { throw errors.WalletServicesPluginError.providerRequired(); } try { this.emit(IPlugin.PLUGIN_EVENTS.CONNECTED); this.status = IPlugin.PLUGIN_STATUS.CONNECTED; } catch (error) { loglevel.log.error(error); this.status = IPlugin.PLUGIN_STATUS.ERRORED; this.emit(IPlugin.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 errors.WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics = this.analytics) === null || _this$analytics === void 0 || _this$analytics.track(analytics.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 errors.WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics2 = this.analytics) === null || _this$analytics2 === void 0 || _this$analytics2.track(analytics.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 errors.WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics3 = this.analytics) === null || _this$analytics3 === void 0 || _this$analytics3.track(analytics.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 errors.WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics4 = this.analytics) === null || _this$analytics4 === void 0 || _this$analytics4.track(analytics.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 errors.WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics5 = this.analytics) === null || _this$analytics5 === void 0 || _this$analytics5.track(analytics.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 errors.WalletServicesPluginError.walletPluginNotConnected(); // analytics (_this$analytics6 = this.analytics) === null || _this$analytics6 === void 0 || _this$analytics6.track(analytics.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 !== IPlugin.PLUGIN_STATUS.CONNECTED) throw errors.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(IPlugin.PLUGIN_EVENTS.DISCONNECTED); this.status = IPlugin.PLUGIN_STATUS.DISCONNECTED; } } const walletServicesPlugin = () => { return () => { return new WalletServicesPlugin(); }; }; Object.defineProperty(exports, "BUTTON_POSITION", { enumerable: true, get: function () { return wsEmbed.BUTTON_POSITION; } }); Object.defineProperty(exports, "CONFIRMATION_STRATEGY", { enumerable: true, get: function () { return wsEmbed.CONFIRMATION_STRATEGY; } }); exports.walletServicesPlugin = walletServicesPlugin;