UNPKG

@sky-mavis/tanto-connect

Version:
113 lines (109 loc) 4.09 kB
'use strict'; var chain = require('../../common/chain.cjs'); var storage = require('../../common/storage.cjs'); var connectorError = require('../../types/connector-error.cjs'); var eip1193 = require('../../types/eip1193.cjs'); var index = require('../../utils/index.cjs'); var BaseConnector = require('../base/BaseConnector.cjs'); class InjectedConnector extends BaseConnector.BaseConnector { isRonin; provider; constructor(configs, provider) { super(configs); this.isRonin = !!provider.isRonin; this.provider = provider; } async connect(chainId) { const provider = await this.getProvider(); try { const accounts = await this.requestAccounts(); const currentChainId = await this.getChainId(); if (chainId && currentChainId !== chainId) { await this.switchChain(chainId); } const connectResults = { provider, chainId: chainId || currentChainId, account: accounts[0], }; this.setupProviderListeners(); this.onConnect(connectResults); storage.ReconnectStorage.add(this.id); return connectResults; } catch (err) { throw new connectorError.ConnectorError(connectorError.ConnectorErrorType.CONNECT_FAILED, err); } } async disconnect() { storage.ReconnectStorage.remove(this.id); this.onDisconnect(); this.removeProviderListeners(); } async isAuthorized() { const accounts = await this.getAccounts(); return accounts.length > 0; } async getAccounts() { const provider = await this.getProvider(); return provider.request({ method: 'eth_accounts', }); } async switchChain(chain) { const provider = await this.getProvider(); return provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: index.numberToHex(chain) }], }); } async addChain(chain$1) { const provider = await this.getProvider(); const { iconUrl, blockExplorerUrl, ...chainConfig } = chain.CHAINS_CONFIG[chain$1] ?? {}; if (!chainConfig) { throw new connectorError.ConnectorError(connectorError.ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED); } return provider.request({ method: 'wallet_addEthereumChain', params: [ { ...chainConfig, chainId: index.numberToHex(chain$1), iconUrls: [iconUrl], blockExplorerUrls: [blockExplorerUrl], }, ], }); } async getChainId() { const provider = await this.getProvider(); const chainId = await provider?.request({ method: 'eth_chainId', }); return Number(chainId); } async requestAccounts() { const provider = await this.getProvider(); return provider?.request({ method: 'eth_requestAccounts', }); } async requestProvider() { return this.provider; } setupProviderListeners() { if (this.provider) { this.provider.on(eip1193.EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.on(eip1193.EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); this.provider.on(eip1193.EIP1193Event.CHAIN_CHANGED, this.onChainChanged); } } removeProviderListeners() { if (this.provider) { this.provider.removeListener(eip1193.EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.removeListener(eip1193.EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); this.provider.removeListener(eip1193.EIP1193Event.CHAIN_CHANGED, this.onChainChanged); } } } exports.InjectedConnector = InjectedConnector;