UNPKG

@sky-mavis/tanto-connect

Version:
111 lines 4.89 kB
import {DEFAULT_CONNECTORS_CONFIG,RONIN_WALLET_CONNECT_PROJECT_ID}from'../../common/connectors.mjs';import {WC_SUPPORTED_CHAIN_IDS}from'../../common/constant.mjs';import {ReconnectStorage}from'../../common/storage.mjs';import'../../types/eip6963.mjs';import {ConnectorError,ConnectorErrorType}from'../../types/connector-error.mjs';import {numberToHex}from'../../utils/index.mjs';import {requestRoninWalletConnectProvider}from'../../providers/ronin-wallet-connect.mjs';import'@safe-global/safe-apps-provider';import'@safe-global/safe-apps-sdk';import'@sky-mavis/waypoint';import'../../common/chain.mjs';import {ConnectorEvent,WCEvent}from'../../types/connector-event.mjs';import {EIP1193Event}from'../../types/eip1193.mjs';import {BaseConnector}from'../base/BaseConnector.mjs';class RoninWalletConnectConnector extends BaseConnector { providerOptions; isRonin; constructor({ connectorConfigs, provider, providerOptions, }) { super({ ...DEFAULT_CONNECTORS_CONFIG.RONIN_WC, ...connectorConfigs }, provider); this.providerOptions = providerOptions; this.isRonin = true; } getProvider = async () => { if (!this.provider) { this.provider = await this.requestProvider(); } return this.provider; }; async connect(chainId) { const provider = await this.getProvider(); const isAuthorized = await this.isAuthorized(); this.setupProviderListeners(); if (!isAuthorized) { await provider.connect(); } else { await provider.enable(); } const currentChainId = await this.getChainId(); if (chainId && currentChainId !== chainId) { await this.switchChain(chainId); } const connectResults = { provider, chainId: chainId || currentChainId, account: provider.accounts[0], }; ReconnectStorage.add(this.id); this.onConnect(connectResults); return connectResults; } async disconnect() { const provider = await this.getProvider(); await provider.disconnect(); this.onDisconnect(); ReconnectStorage.remove(this.id); this.removeProviderListeners(); } async isAuthorized() { const accounts = await this.getAccounts(); return accounts.length > 0; } async getAccounts() { const provider = await this.getProvider(); return provider.accounts; } async switchChain(chain) { const provider = await this.getProvider(); return provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: numberToHex(chain) }], }); } async addChain() { throw new ConnectorError(ConnectorErrorType.ADD_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 requestProvider() { return requestRoninWalletConnectProvider({ projectId: RONIN_WALLET_CONNECT_PROJECT_ID, chains: WC_SUPPORTED_CHAIN_IDS, optionalChains: WC_SUPPORTED_CHAIN_IDS, showQrModal: this.isRonin, ...this.providerOptions, }); } onDisplayUri = (uri) => { this.emit(ConnectorEvent.DISPLAY_URI, uri); }; setupWalletConnectEvent() { this.getProvider().then(provider => { provider.on(WCEvent.SESSION_DELETE, this.onDisconnect); provider.on(WCEvent.DISPLAY_URI, this.onDisplayUri); }); } setupProviderListeners() { if (this.provider) { this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); this.provider.on(EIP1193Event.CHAIN_CHANGED, this.onChainChanged); this.setupWalletConnectEvent(); } } removeProviderListeners() { if (this.provider) { this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); this.provider.removeListener(EIP1193Event.CHAIN_CHANGED, this.onChainChanged); this.provider.removeListener(WCEvent.DISPLAY_URI, this.onDisplayUri); this.provider.removeListener(WCEvent.SESSION_DELETE, this.onDisconnect); } } }export{RoninWalletConnectConnector};