@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
243 lines (239 loc) • 12 kB
JavaScript
'use strict';
var react = require('react');
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');
require('../../base/loglevel.js');
require('@toruslabs/constants');
require('@toruslabs/http-helpers');
var constants$1 = require('../../base/constants.js');
require('../../base/wallet/index.js');
var connectorStatus = 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 getInitialState(web3Auth) {
var _web3Auth$currentChai, _web3Auth$currentChai2;
const isConnected = connectorStatus.CONNECTED_STATUSES.includes(web3Auth.status);
const isAuthorized = web3Auth.status === constants.CONNECTOR_STATUS.AUTHORIZED;
return {
chainId: isConnected ? web3Auth.currentChainId : null,
chainNamespace: isConnected ? (_web3Auth$currentChai = (_web3Auth$currentChai2 = web3Auth.currentChain) === null || _web3Auth$currentChai2 === void 0 ? void 0 : _web3Auth$currentChai2.chainNamespace) !== null && _web3Auth$currentChai !== void 0 ? _web3Auth$currentChai : null : null,
connection: isConnected ? web3Auth.connection : null,
isAuthorized,
isConnected,
isInitialized: isConnected,
status: web3Auth.status
};
}
function useWeb3AuthInnerContextValue({
Web3AuthConstructor,
web3AuthOptions,
initialState,
notReadyUsesCurrentStatus = false,
cleanupOnUnmount = false,
initEffectDependency
}) {
const web3Auth = react.useMemo(() => {
return new Web3AuthConstructor(web3AuthOptions, initialState);
}, [Web3AuthConstructor, web3AuthOptions, initialState]);
const initialWeb3AuthState = getInitialState(web3Auth);
const [chainId, setChainId] = react.useState(() => initialWeb3AuthState.chainId);
const [chainNamespace, setChainNamespace] = react.useState(() => initialWeb3AuthState.chainNamespace);
const [isInitializing, setIsInitializing] = react.useState(false);
const [initError, setInitError] = react.useState(null);
const [connection, setConnection] = react.useState(() => initialWeb3AuthState.connection);
const [isInitialized, setIsInitialized] = react.useState(initialWeb3AuthState.isInitialized);
const [isMFAEnabled, setIsMFAEnabled] = react.useState(false);
const [isConnected, setIsConnected] = react.useState(initialWeb3AuthState.isConnected);
const [status, setStatus] = react.useState(initialWeb3AuthState.status);
const [isAuthorized, setIsAuthorized] = react.useState(initialWeb3AuthState.isAuthorized);
const getPlugin = react.useCallback(name => {
if (!web3Auth) throw index.WalletInitializationError.notReady();
return web3Auth.getPlugin(name);
}, [web3Auth]);
react.useEffect(() => {
const controller = new AbortController();
async function init() {
try {
var _web3Auth$currentChai3;
setInitError(null);
setIsInitializing(true);
web3Auth.setAnalyticsProperties({
integration_type: analytics.ANALYTICS_INTEGRATION_TYPE.REACT_HOOKS
});
await web3Auth.init({
signal: controller.signal
});
setChainId(web3Auth.currentChainId);
setChainNamespace((_web3Auth$currentChai3 = web3Auth.currentChain) === null || _web3Auth$currentChai3 === void 0 ? void 0 : _web3Auth$currentChai3.chainNamespace);
} catch (error) {
setInitError(error);
} finally {
setIsInitializing(false);
}
}
init();
return () => {
controller.abort();
};
}, [web3Auth, initEffectDependency]);
react.useEffect(() => {
var _connection$ethereumP;
const handleChainChange = async nextChainId => {
var _web3Auth$currentChai4;
setChainId(nextChainId);
setChainNamespace((_web3Auth$currentChai4 = web3Auth.currentChain) === null || _web3Auth$currentChai4 === void 0 ? void 0 : _web3Auth$currentChai4.chainNamespace);
};
const provider = (_connection$ethereumP = connection === null || connection === void 0 ? void 0 : connection.ethereumProvider) !== null && _connection$ethereumP !== void 0 ? _connection$ethereumP : null;
if (!provider) return undefined;
provider.on("chainChanged", handleChainChange);
return () => {
provider.removeListener("chainChanged", handleChainChange);
};
}, [web3Auth.currentChain, connection === null || connection === void 0 ? void 0 : connection.ethereumProvider]);
react.useEffect(() => {
const notReadyListener = () => {
setStatus(notReadyUsesCurrentStatus ? web3Auth.status : constants.CONNECTOR_STATUS.NOT_READY);
};
const readyListener = () => {
setStatus(web3Auth.status);
setIsInitialized(true);
};
const connectedListener = data => {
if (data.pendingUserConsent) return;
setStatus(web3Auth.status);
// we do this because of rehydration issues. status connected is fired first but web3auth sdk is not ready yet.
if (web3Auth.status === constants.CONNECTOR_STATUS.CONNECTED) {
var _web3Auth$currentChai5, _web3Auth$currentChai6;
setIsInitialized(true);
setIsConnected(true);
setConnection(web3Auth.connection);
setChainId(web3Auth.currentChainId);
setChainNamespace((_web3Auth$currentChai5 = (_web3Auth$currentChai6 = web3Auth.currentChain) === null || _web3Auth$currentChai6 === void 0 ? void 0 : _web3Auth$currentChai6.chainNamespace) !== null && _web3Auth$currentChai5 !== void 0 ? _web3Auth$currentChai5 : null);
}
};
const disconnectedListener = () => {
setStatus(web3Auth.status);
setIsConnected(false);
setIsAuthorized(false);
setConnection(null);
};
const connectingListener = () => {
setStatus(web3Auth.status);
};
const errorListener = () => {
setStatus(web3Auth.status);
};
const rehydrationErrorListener = () => {
setStatus(web3Auth.status);
setIsConnected(false);
setIsAuthorized(false);
setConnection(null);
};
const authorizedListener = () => {
setStatus(web3Auth.status);
if (web3Auth.status === constants.CONNECTOR_STATUS.AUTHORIZED) {
var _web3Auth$currentChai7, _web3Auth$currentChai8;
setIsInitialized(true);
setIsConnected(true);
// 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.
setConnection(web3Auth.connection);
setChainId(web3Auth.currentChainId);
setChainNamespace((_web3Auth$currentChai7 = (_web3Auth$currentChai8 = web3Auth.currentChain) === null || _web3Auth$currentChai8 === void 0 ? void 0 : _web3Auth$currentChai8.chainNamespace) !== null && _web3Auth$currentChai7 !== void 0 ? _web3Auth$currentChai7 : null);
setIsAuthorized(true);
}
};
const consentAcceptedListener = () => {
setStatus(web3Auth.status);
if (web3Auth.status === constants.CONNECTOR_STATUS.CONNECTED || web3Auth.status === constants.CONNECTOR_STATUS.AUTHORIZED) {
var _web3Auth$currentChai9, _web3Auth$currentChai0;
setIsInitialized(true);
setIsConnected(true);
setConnection(web3Auth.connection);
setChainId(web3Auth.currentChainId);
setChainNamespace((_web3Auth$currentChai9 = (_web3Auth$currentChai0 = web3Auth.currentChain) === null || _web3Auth$currentChai0 === void 0 ? void 0 : _web3Auth$currentChai0.chainNamespace) !== null && _web3Auth$currentChai9 !== void 0 ? _web3Auth$currentChai9 : null);
if (web3Auth.status === constants.CONNECTOR_STATUS.AUTHORIZED) {
setIsAuthorized(true);
}
}
};
const mfaEnabledListener = nextIsMFAEnabled => {
if (typeof nextIsMFAEnabled === "boolean") setIsMFAEnabled(nextIsMFAEnabled);
};
const connectionUpdatedListener = () => {
var _web3Auth$currentChai1, _web3Auth$currentChai10;
setStatus(web3Auth.status);
setConnection(web3Auth.connection);
setChainId(web3Auth.currentChainId);
setChainNamespace((_web3Auth$currentChai1 = (_web3Auth$currentChai10 = web3Auth.currentChain) === null || _web3Auth$currentChai10 === void 0 ? void 0 : _web3Auth$currentChai10.chainNamespace) !== null && _web3Auth$currentChai1 !== void 0 ? _web3Auth$currentChai1 : null);
};
const connectorDataUpdatedListener = data => {
const updatedData = data.data;
if (updatedData.chainId) {
var _web3Auth$currentChai11;
setChainId(updatedData.chainId);
setChainNamespace((_web3Auth$currentChai11 = web3Auth.currentChain) === null || _web3Auth$currentChai11 === void 0 ? void 0 : _web3Auth$currentChai11.chainNamespace);
}
};
if (web3Auth) {
web3Auth.on(constants.CONNECTOR_EVENTS.NOT_READY, notReadyListener);
web3Auth.on(constants.CONNECTOR_EVENTS.READY, readyListener);
web3Auth.on(constants.CONNECTOR_EVENTS.CONNECTED, connectedListener);
web3Auth.on(constants.CONNECTOR_EVENTS.AUTHORIZED, authorizedListener);
web3Auth.on(constants.CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
web3Auth.on(constants.CONNECTOR_EVENTS.CONNECTING, connectingListener);
web3Auth.on(constants.CONNECTOR_EVENTS.ERRORED, errorListener);
web3Auth.on(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, rehydrationErrorListener);
web3Auth.on(constants.CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
web3Auth.on(constants.CONNECTOR_EVENTS.CONNECTION_UPDATED, connectionUpdatedListener);
web3Auth.on(constants.CONNECTOR_EVENTS.CONNECTOR_DATA_UPDATED, connectorDataUpdatedListener);
if (web3Auth.loginMode === constants$1.LOGIN_MODE.MODAL) {
web3Auth.on(constants.CONNECTOR_EVENTS.CONSENT_ACCEPTED, consentAcceptedListener);
}
}
return () => {
if (!web3Auth) return;
web3Auth.removeListener(constants.CONNECTOR_EVENTS.NOT_READY, notReadyListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.READY, readyListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTED, connectedListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTING, connectingListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.ERRORED, errorListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, rehydrationErrorListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.AUTHORIZED, authorizedListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTION_UPDATED, connectionUpdatedListener);
web3Auth.removeListener(constants.CONNECTOR_EVENTS.CONNECTOR_DATA_UPDATED, connectorDataUpdatedListener);
if (web3Auth.loginMode === constants$1.LOGIN_MODE.MODAL) {
web3Auth.removeListener(constants.CONNECTOR_EVENTS.CONSENT_ACCEPTED, consentAcceptedListener);
}
if (cleanupOnUnmount) {
web3Auth.cleanup();
}
};
}, [cleanupOnUnmount, notReadyUsesCurrentStatus, web3Auth]);
return react.useMemo(() => {
return {
web3Auth,
isConnected,
isInitialized,
connection,
status,
isInitializing,
initError,
isMFAEnabled,
chainId,
chainNamespace,
getPlugin,
setIsMFAEnabled,
isAuthorized
};
}, [web3Auth, isConnected, isInitialized, connection, status, getPlugin, isInitializing, initError, isMFAEnabled, setIsMFAEnabled, chainId, chainNamespace, isAuthorized]);
}
exports.useWeb3AuthInnerContextValue = useWeb3AuthInnerContextValue;