UNPKG

@web3auth/no-modal

Version:
150 lines (147 loc) 6.2 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { StandardConnect, StandardDisconnect } from '@wallet-standard/features'; import { BaseSolanaConnector } from '../base-solana-connector/baseSolanaConnector.js'; import { WalletStandardProvider } from '../../providers/solana-provider/providers/injectedProviders/walletStandardProvider.js'; import { getSolanaChainByChainConfig } from '../../providers/solana-provider/providers/injectedProviders/utils.js'; import { normalizeWalletName } from '../../base/utils.js'; import { CONNECTOR_NAMESPACES } from '../../base/chain/IChainInterface.js'; import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers'; import { CONNECTOR_CATEGORY, CONNECTOR_STATUS, CONNECTOR_EVENTS } from '../../base/connector/constants.js'; import { log } from '../../base/loglevel.js'; import { WalletLoginError } from '../../base/errors/index.js'; class WalletStandardConnector extends BaseSolanaConnector { constructor(options) { super(options); _defineProperty(this, "name", void 0); _defineProperty(this, "connectorNamespace", CONNECTOR_NAMESPACES.SOLANA); _defineProperty(this, "currentChainNamespace", CHAIN_NAMESPACES.SOLANA); _defineProperty(this, "type", CONNECTOR_CATEGORY.EXTERNAL); _defineProperty(this, "isInjected", true); _defineProperty(this, "status", CONNECTOR_STATUS.NOT_READY); _defineProperty(this, "wallet", null); _defineProperty(this, "injectedProvider", null); this.name = options.name; this.icon = options.wallet.icon; // in VueJS, for some wallets e.g. Gate, Solflare, when connecting it throws error "attempted to get private field on non-instance" // it seems that Vue create a Proxy object for the wallet object which causes the issue // ref: https://stackoverflow.com/questions/64917686/vue-array-converted-to-proxy-object this.wallet = ["gate", "solflare"].includes(this.name) ? Object.freeze(options.wallet) : options.wallet; } get provider() { if (this.status !== CONNECTOR_STATUS.NOT_READY && this.injectedProvider) { return this.injectedProvider; } return null; } get isWalletConnected() { return !!(this.status === CONNECTOR_STATUS.CONNECTED && this.wallet.accounts.length > 0); } async init(options) { await super.init(options); const chainConfig = this.coreOptions.chains.find(x => x.chainId === options.chainId); super.checkInitializationRequirements({ chainConfig }); this.injectedProvider = new WalletStandardProvider({ config: { chain: chainConfig, chains: this.coreOptions.chains } }); await this.injectedProvider.setupProvider(this.wallet, options.chainId); this.status = CONNECTOR_STATUS.READY; this.emit(CONNECTOR_EVENTS.READY, this.name); try { log.debug("initializing solana injected connector"); if (options.autoConnect) { this.rehydrated = true; const provider = await this.connect({ chainId: options.chainId }); if (!provider) { this.rehydrated = false; throw WalletLoginError.connectionError("Failed to rehydrate."); } } } catch (error) { this.emit(CONNECTOR_EVENTS.REHYDRATION_ERROR, error); } } async connect({ chainId }) { try { super.checkConnectionRequirements(); const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId); if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); this.status = CONNECTOR_STATUS.CONNECTING; this.emit(CONNECTOR_EVENTS.CONNECTING, { connector: this.name }); const chainName = getSolanaChainByChainConfig(chainConfig); if (!this.wallet.chains.find(chain => chain === chainName)) throw WalletLoginError.connectionError(`Chain ${chainName} not supported. Supported chains are ${this.wallet.chains.join(", ")}`); if (!this.isWalletConnected) { await this.wallet.features[StandardConnect].connect(); } if (this.wallet.accounts.length === 0) throw WalletLoginError.connectionError(); this.status = CONNECTOR_STATUS.CONNECTED; this.emit(CONNECTOR_EVENTS.CONNECTED, { connector: this.name, reconnected: this.rehydrated, provider: this.provider }); return this.provider; } catch (error) { // ready again to be connected this.status = CONNECTOR_STATUS.READY; if (!this.rehydrated) this.emit(CONNECTOR_EVENTS.ERRORED, error); this.rehydrated = false; throw error; } } async disconnect(options = { cleanup: false }) { await super.disconnectSession(); try { var _this$wallet$features; await ((_this$wallet$features = this.wallet.features[StandardDisconnect]) === null || _this$wallet$features === void 0 ? void 0 : _this$wallet$features.disconnect()); if (options.cleanup) { this.status = CONNECTOR_STATUS.NOT_READY; this.injectedProvider = null; } else { this.status = CONNECTOR_STATUS.READY; } await super.disconnect(); } catch (error) { this.emit(CONNECTOR_EVENTS.ERRORED, WalletLoginError.disconnectionError(error === null || error === void 0 ? void 0 : error.message)); } } async getUserInfo() { if (!this.isWalletConnected) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); return {}; } async switchChain(params, init = false) { var _this$injectedProvide; super.checkSwitchChainRequirements(params, init); await ((_this$injectedProvide = this.injectedProvider) === null || _this$injectedProvide === void 0 ? void 0 : _this$injectedProvide.switchChain(params)); } async enableMFA() { throw new Error("Method not implemented."); } async manageMFA() { throw new Error("Method Not implemented"); } } const walletStandardConnector = wallet => { return ({ coreOptions }) => { return new WalletStandardConnector({ name: normalizeWalletName(wallet.name), wallet, coreOptions }); }; }; export { WalletStandardConnector, walletStandardConnector };