UNPKG

@web3auth/no-modal

Version:
206 lines (203 loc) 9.11 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { CHAIN_NAMESPACES, verifySignedChallenge, getDeviceInfo } from '@toruslabs/base-controllers'; import { AuthSessionManager } from '@toruslabs/session-manager'; import { SafeEventEmitter } from '@web3auth/auth'; import { CONNECTOR_NAMESPACES } from '../chain/IChainInterface.js'; import { WalletInitializationError, WalletLoginError } from '../errors/index.js'; import { log } from '../loglevel.js'; import { citadelServerUrl } from '../utils.js'; import { WALLET_CONNECTORS } from '../wallet/index.js'; import { CONNECTED_STATUSES, CAN_AUTHORIZE_STATUSES } from './connectorStatus.js'; import { CONNECTOR_STATUS, CONNECTOR_EVENTS } from './constants.js'; import { checkIfTokenIsExpired } from './utils.js'; class BaseConnector extends SafeEventEmitter { constructor(options) { super(); _defineProperty(this, "connectorData", {}); _defineProperty(this, "isInjected", void 0); _defineProperty(this, "icon", void 0); _defineProperty(this, "coreOptions", void 0); _defineProperty(this, "rehydrated", false); _defineProperty(this, "authSessionManager", null); _defineProperty(this, "connectorNamespace", void 0); _defineProperty(this, "type", void 0); _defineProperty(this, "name", void 0); _defineProperty(this, "status", void 0); this.coreOptions = options.coreOptions; } get connected() { return CONNECTED_STATUSES.includes(this.status); } get canAuthorize() { return CAN_AUTHORIZE_STATUSES.includes(this.status); } get solanaWallet() { return null; } get provider() { return null; } checkConnectionRequirements() { // we reconnect without killing existing Wallet Connect or Metamask Connect session on calling connect again. if (this.name === WALLET_CONNECTORS.WALLET_CONNECT_V2 && this.status === CONNECTOR_STATUS.CONNECTING) return; if (this.name === WALLET_CONNECTORS.METAMASK && !this.isInjected && this.status === CONNECTOR_STATUS.CONNECTING) return; if (this.status === CONNECTOR_STATUS.CONNECTING) throw WalletInitializationError.notReady("Already connecting"); if (this.connected) throw WalletLoginError.connectionError("Already connected"); if (this.status !== CONNECTOR_STATUS.READY) throw WalletLoginError.connectionError("Wallet connector is not ready yet, Please wait for init function to resolve before calling connect/connectTo function"); } checkInitializationRequirements({ chainConfig }) { if (!this.coreOptions.clientId) throw WalletInitializationError.invalidParams("Please initialize Web3Auth with a valid clientId in constructor"); if (!chainConfig) throw WalletInitializationError.invalidParams("chainConfig is required before initialization"); if (!chainConfig.rpcTarget && chainConfig.chainNamespace !== CHAIN_NAMESPACES.OTHER) { throw WalletInitializationError.invalidParams("rpcTarget is required in chainConfig"); } if (!chainConfig.chainId && chainConfig.chainNamespace !== CHAIN_NAMESPACES.OTHER) { throw WalletInitializationError.invalidParams("chainID is required in chainConfig"); } if (this.connectorNamespace !== CONNECTOR_NAMESPACES.MULTICHAIN && this.connectorNamespace !== chainConfig.chainNamespace) throw WalletInitializationError.invalidParams("Connector doesn't support this chain namespace"); if (this.status === CONNECTOR_STATUS.NOT_READY) return; if (this.connected) throw WalletInitializationError.notReady("Already connected"); if (this.status === CONNECTOR_STATUS.READY) throw WalletInitializationError.notReady("Connector is already initialized"); } checkDisconnectionRequirements() { if (!this.connected) throw WalletLoginError.disconnectionError("Not connected with wallet"); } checkSwitchChainRequirements(params, init = false) { if (!init && !this.provider && !this.solanaWallet) throw WalletLoginError.notConnectedError("Not connected with wallet."); if (!this.coreOptions.chains) throw WalletInitializationError.invalidParams("chainConfigs is required"); const doesChainExist = this.coreOptions.chains.some(x => x.chainId === params.chainId && (x.chainNamespace === this.connectorNamespace || this.connectorNamespace === CONNECTOR_NAMESPACES.MULTICHAIN)); if (!doesChainExist) throw WalletInitializationError.invalidParams("Invalid chainId"); } updateConnectorData(data) { this.connectorData = data; this.emit(CONNECTOR_EVENTS.CONNECTOR_DATA_UPDATED, { connectorName: this.name, data }); } initSessionManager(address) { this.authSessionManager = new AuthSessionManager({ storageKeyPrefix: `w3a:siww:${this.name}:${address.toLowerCase()}`, apiClientConfig: { baseURL: citadelServerUrl(this.coreOptions.authBuildEnv) }, storage: this.coreOptions.storage, cookieOptions: this.coreOptions.cookieOptions }); } async getCachedAuthTokenInfo() { if (!this.authSessionManager) return null; const idToken = await this.authSessionManager.getIdToken(); if (!idToken || checkIfTokenIsExpired(idToken)) return null; let [accessToken, refreshToken] = await Promise.all([this.authSessionManager.getAccessToken(), this.authSessionManager.getRefreshToken()]); if ((!accessToken || checkIfTokenIsExpired(accessToken)) && refreshToken) { try { const response = await this.authSessionManager.ensureRefresh(); accessToken = response.access_token || (await this.authSessionManager.getAccessToken()); refreshToken = response.refresh_token || refreshToken; } catch { // access token refresh failed; still return the valid idToken log.error("Access token refresh failed"); } } return { idToken, accessToken: accessToken !== null && accessToken !== void 0 ? accessToken : undefined, refreshToken: refreshToken !== null && refreshToken !== void 0 ? refreshToken : undefined }; } async saveAuthTokenInfo(tokens) { if (!this.authSessionManager) return; await this.authSessionManager.setTokens({ idToken: tokens.idToken, accessToken: tokens.accessToken, refreshToken: tokens.refreshToken }); } async getCachedOrNullAuthTokenInfo(account) { this.initSessionManager(account); const cached = await this.getCachedAuthTokenInfo(); if (cached) { this.status = CONNECTOR_STATUS.AUTHORIZED; this.emit(CONNECTOR_EVENTS.AUTHORIZED, { connector: this.name, authTokenInfo: cached }); } return cached; } async verifyAndAuthorize(params) { let tokens; try { tokens = await verifySignedChallenge({ chainNamespace: params.chainNamespace, signedMessage: params.signedMessage, challenge: params.challenge, connector: this.name, authServer: params.authServer, web3AuthClientId: this.coreOptions.clientId, web3AuthNetwork: this.coreOptions.web3AuthNetwork, sessionTimeout: this.coreOptions.sessionTime, deviceInfo: getDeviceInfo() }); } catch (error) { if (error instanceof Response && error.status === 401) { const body = await error.clone().json().catch(() => ({})); if (body.message === "ACCESS_CONTROL_DENIED") { throw WalletLoginError.userBlocked("User is blocked by the application", error); } } throw error; } await this.saveAuthTokenInfo(tokens); const tokenInfo = { idToken: tokens.idToken, accessToken: tokens.accessToken, refreshToken: tokens.refreshToken }; this.status = CONNECTOR_STATUS.AUTHORIZED; this.emit(CONNECTOR_EVENTS.AUTHORIZED, { connector: this.name, authTokenInfo: tokenInfo }); return tokenInfo; } async authorizeOrDisconnect(getAuthTokenInfo, chainId) { if (!getAuthTokenInfo) return; try { await this.getAuthTokenInfo(chainId); } catch (error) { log.error("Authorization failed after connect; disconnecting wallet to keep state consistent", error); const wasRehydrated = this.rehydrated; try { // getAuthTokenInfo moved status to AUTHORIZING; restore CONNECTED so disconnect requirements pass. if (!this.connected) this.status = CONNECTOR_STATUS.CONNECTED; await this.disconnect(); } catch (disconnectError) { log.error("Failed to disconnect wallet after authorization error", disconnectError); } finally { // disconnect() resets rehydrated=false; restore so caller catch emits the right event. this.rehydrated = wasRehydrated; } throw error; } } async clearWalletSession() { if (!this.authSessionManager) return; try { await this.authSessionManager.logout(); } catch { try { await this.authSessionManager.clearSessionData(); } catch { // best-effort cleanup; don't block disconnect log.error("Failed to clear wallet session"); } } finally { this.authSessionManager = null; } } } export { BaseConnector };