UNPKG

@web3auth/no-modal

Version:
244 lines (240 loc) 11.5 kB
'use strict'; var vue = require('vue'); var analytics = require('../base/analytics.js'); require('@toruslabs/base-controllers'); require('@babel/runtime/helpers/defineProperty'); require('@toruslabs/session-manager'); require('@web3auth/auth'); var index = require('../base/errors/index.js'); var loglevel = require('../base/loglevel.js'); require('@toruslabs/constants'); require('@toruslabs/http-helpers'); var constants$1 = require('../base/constants.js'); 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'); function useWeb3AuthInnerContextValue({ Web3AuthConstructor, watchSource, getWeb3AuthOptions, initialState, createConnectionRef = () => vue.ref(null) }) { const web3Auth = vue.shallowRef(null); const connection = createConnectionRef(); const isMFAEnabled = vue.ref(false); const status = vue.ref(null); const chainId = vue.ref(null); const chainNamespace = vue.ref(null); const isInitializing = vue.ref(false); const initError = vue.ref(null); const isInitialized = vue.ref(false); const isConnected = vue.ref(false); const isAuthorized = vue.ref(false); const getPlugin = name => { if (!web3Auth.value) throw index.WalletInitializationError.notReady(); return web3Auth.value.getPlugin(name); }; const setIsMFAEnabled = isMfaEnabled => { isMFAEnabled.value = isMfaEnabled; }; vue.watch(watchSource, (newSource, _, onInvalidate) => { const resetHookState = () => { connection.value = null; isMFAEnabled.value = false; isConnected.value = false; isAuthorized.value = false; status.value = null; chainId.value = null; chainNamespace.value = null; }; onInvalidate(() => { if (web3Auth.value) { web3Auth.value.cleanup(); } }); resetHookState(); const web3AuthInstance = new Web3AuthConstructor(getWeb3AuthOptions(newSource), initialState); web3AuthInstance.setAnalyticsProperties({ integration_type: analytics.ANALYTICS_INTEGRATION_TYPE.VUE_COMPOSABLES }); web3Auth.value = web3AuthInstance; }, { immediate: true }); vue.watch(web3Auth, async (newWeb3Auth, _, onInvalidate) => { const controller = new AbortController(); onInvalidate(() => { controller.abort(); }); if (newWeb3Auth) { try { initError.value = null; isInitializing.value = true; await newWeb3Auth.init({ signal: controller.signal }); } catch (error) { loglevel.log.error("Error initializing web3auth", error); initError.value = error; } finally { isInitializing.value = false; } } }, { immediate: true }); vue.watch(web3Auth, (newWeb3Auth, prevWeb3Auth) => { const notReadyListener = () => { status.value = web3Auth.value.status; }; const readyListener = () => { status.value = web3Auth.value.status; isInitialized.value = true; }; const connectedListener = data => { if (data.pendingUserConsent) return; status.value = web3Auth.value.status; // We do this because of rehydration issues. status connected is fired first but web3auth sdk is not ready yet. if (web3Auth.value.status === constants.CONNECTOR_STATUS.CONNECTED) { var _web3Auth$value$curre, _web3Auth$value$curre2; if (!isInitialized.value) isInitialized.value = true; isConnected.value = true; connection.value = web3Auth.value.connection; chainId.value = web3Auth.value.currentChainId; chainNamespace.value = (_web3Auth$value$curre = (_web3Auth$value$curre2 = web3Auth.value.currentChain) === null || _web3Auth$value$curre2 === void 0 ? void 0 : _web3Auth$value$curre2.chainNamespace) !== null && _web3Auth$value$curre !== void 0 ? _web3Auth$value$curre : null; } }; const authorizedListener = () => { status.value = web3Auth.value.status; // on rehydration, `AUTHORIZED` event can be fired first in `CONNECT_AND_SIGN` mode, before `CONNECTED` event. // Update the connection state here, so that clients can use the connection state immediately. if (web3Auth.value.status === constants.CONNECTOR_STATUS.AUTHORIZED) { var _web3Auth$value$curre3, _web3Auth$value$curre4; if (!isInitialized.value) isInitialized.value = true; isAuthorized.value = true; isConnected.value = true; connection.value = web3Auth.value.connection; chainId.value = web3Auth.value.currentChainId; chainNamespace.value = (_web3Auth$value$curre3 = (_web3Auth$value$curre4 = web3Auth.value.currentChain) === null || _web3Auth$value$curre4 === void 0 ? void 0 : _web3Auth$value$curre4.chainNamespace) !== null && _web3Auth$value$curre3 !== void 0 ? _web3Auth$value$curre3 : null; } }; const consentAcceptedListener = () => { status.value = web3Auth.value.status; if (web3Auth.value.status === constants.CONNECTOR_STATUS.CONNECTED || web3Auth.value.status === constants.CONNECTOR_STATUS.AUTHORIZED) { var _web3Auth$value$curre5, _web3Auth$value$curre6; if (!isInitialized.value) isInitialized.value = true; isConnected.value = true; connection.value = web3Auth.value.connection; chainId.value = web3Auth.value.currentChainId; chainNamespace.value = (_web3Auth$value$curre5 = (_web3Auth$value$curre6 = web3Auth.value.currentChain) === null || _web3Auth$value$curre6 === void 0 ? void 0 : _web3Auth$value$curre6.chainNamespace) !== null && _web3Auth$value$curre5 !== void 0 ? _web3Auth$value$curre5 : null; if (web3Auth.value.status === constants.CONNECTOR_STATUS.AUTHORIZED) { isAuthorized.value = true; } } }; const disconnectedListener = () => { status.value = web3Auth.value.status; isConnected.value = false; connection.value = null; isMFAEnabled.value = false; isAuthorized.value = false; }; const connectingListener = () => { status.value = web3Auth.value.status; }; const errorListener = () => { status.value = web3Auth.value.status; }; const mfaEnabledListener = () => { isMFAEnabled.value = true; }; const connectionUpdatedListener = () => { var _web3Auth$value$curre7, _web3Auth$value$curre8; status.value = web3Auth.value.status; connection.value = web3Auth.value.connection; chainId.value = web3Auth.value.currentChainId; chainNamespace.value = (_web3Auth$value$curre7 = (_web3Auth$value$curre8 = web3Auth.value.currentChain) === null || _web3Auth$value$curre8 === void 0 ? void 0 : _web3Auth$value$curre8.chainNamespace) !== null && _web3Auth$value$curre7 !== void 0 ? _web3Auth$value$curre7 : null; }; const connectorDataUpdatedListener = data => { const updatedData = data.data; if (updatedData.chainId && chainId.value !== updatedData.chainId) { var _web3Auth$value$curre9; chainId.value = updatedData.chainId; chainNamespace.value = (_web3Auth$value$curre9 = web3Auth.value.currentChain) === null || _web3Auth$value$curre9 === void 0 ? void 0 : _web3Auth$value$curre9.chainNamespace; } }; if (prevWeb3Auth && newWeb3Auth !== prevWeb3Auth) { prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.NOT_READY, notReadyListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.READY, readyListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTED, connectedListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.AUTHORIZED, authorizedListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTING, connectingListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.ERRORED, errorListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, errorListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTION_UPDATED, connectionUpdatedListener); prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTOR_DATA_UPDATED, connectorDataUpdatedListener); if (prevWeb3Auth.loginMode === constants$1.LOGIN_MODE.MODAL) { prevWeb3Auth.removeListener(constants.CONNECTOR_EVENTS.CONSENT_ACCEPTED, consentAcceptedListener); } } if (newWeb3Auth && newWeb3Auth !== prevWeb3Auth) { status.value = newWeb3Auth.status; newWeb3Auth.on(constants.CONNECTOR_EVENTS.NOT_READY, notReadyListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.READY, readyListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.CONNECTED, connectedListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.AUTHORIZED, authorizedListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.CONNECTING, connectingListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.ERRORED, errorListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, errorListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.CONNECTION_UPDATED, connectionUpdatedListener); newWeb3Auth.on(constants.CONNECTOR_EVENTS.CONNECTOR_DATA_UPDATED, connectorDataUpdatedListener); if (newWeb3Auth.loginMode === constants$1.LOGIN_MODE.MODAL) { newWeb3Auth.on(constants.CONNECTOR_EVENTS.CONSENT_ACCEPTED, consentAcceptedListener); } } }, { immediate: true }); vue.watch(connection, (newConnection, prevConnection) => { var _prevConnection$ether, _newConnection$ethere; const handleChainChange = newChainId => { var _web3Auth$value$curre0, _web3Auth$value; chainId.value = newChainId; chainNamespace.value = (_web3Auth$value$curre0 = (_web3Auth$value = web3Auth.value) === null || _web3Auth$value === void 0 || (_web3Auth$value = _web3Auth$value.currentChain) === null || _web3Auth$value === void 0 ? void 0 : _web3Auth$value.chainNamespace) !== null && _web3Auth$value$curre0 !== void 0 ? _web3Auth$value$curre0 : null; }; const prevProvider = (_prevConnection$ether = prevConnection === null || prevConnection === void 0 ? void 0 : prevConnection.ethereumProvider) !== null && _prevConnection$ether !== void 0 ? _prevConnection$ether : null; const newProvider = (_newConnection$ethere = newConnection === null || newConnection === void 0 ? void 0 : newConnection.ethereumProvider) !== null && _newConnection$ethere !== void 0 ? _newConnection$ethere : null; if (prevProvider && newProvider !== prevProvider) { prevProvider.removeListener("chainChanged", handleChainChange); } if (newProvider && newProvider !== prevProvider) { newProvider.on("chainChanged", handleChainChange); } }, { immediate: true }); return { web3Auth, isConnected, isAuthorized, connection, isInitializing, initError, isInitialized, status, isMFAEnabled, chainId, chainNamespace, getPlugin, setIsMFAEnabled }; } exports.useWeb3AuthInnerContextValue = useWeb3AuthInnerContextValue;