UNPKG

@sky-mavis/tanto-connect

Version:
74 lines 3.1 kB
import {DEFAULT_CONNECTORS_CONFIG}from'../../common/connectors.mjs';import'../../common/constant.mjs';import'../../types/eip6963.mjs';import {ConnectorError,ConnectorErrorType}from'../../types/connector-error.mjs';import'@walletconnect/ethereum-provider';import {requestSafeProvider}from'../../providers/safe-provider.mjs';import'@sky-mavis/waypoint';import'../../common/chain.mjs';import {EIP1193Event}from'../../types/eip1193.mjs';import {BaseConnector}from'../base/BaseConnector.mjs';class SafeConnector extends BaseConnector { constructor(configs, provider) { super({ ...DEFAULT_CONNECTORS_CONFIG.SAFE, ...configs }, provider); } async connect() { const provider = await this.getProvider(); if (!provider) { throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND); } const accounts = await this.getAccounts(); const chainId = await this.getChainId(); if (accounts?.length && !!chainId) { const connectResults = { provider, chainId, account: accounts[0], }; this.setupProviderListeners(); this.onConnect(connectResults); return connectResults; } throw new ConnectorError(ConnectorErrorType.CONNECT_FAILED); } async disconnect() { 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', }); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async switchChain(chain) { throw new ConnectorError(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED); } 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 addChain() { throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED); } async requestProvider() { return await requestSafeProvider(); } setupProviderListeners() { this.removeProviderListeners(); if (this.provider) { this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); } } removeProviderListeners() { if (this.provider) { this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); } } }export{SafeConnector};