UNPKG

@sky-mavis/tanto-connect

Version:
131 lines (127 loc) 5.13 kB
'use strict'; var connectors = require('../../common/connectors.cjs'); var constant = require('../../common/constant.cjs'); var storage = require('../../common/storage.cjs'); require('../../types/eip6963.cjs'); var connectorError = require('../../types/connector-error.cjs'); var index = require('../../utils/index.cjs'); var roninWalletConnect = require('../../providers/ronin-wallet-connect.cjs'); require('@safe-global/safe-apps-provider'); require('@safe-global/safe-apps-sdk'); require('@sky-mavis/waypoint'); require('../../common/chain.cjs'); var connectorEvent = require('../../types/connector-event.cjs'); var eip1193 = require('../../types/eip1193.cjs'); var BaseConnector = require('../base/BaseConnector.cjs'); class RoninWalletConnectConnector extends BaseConnector.BaseConnector { providerOptions; isRonin; constructor({ connectorConfigs, provider, providerOptions, }) { super({ ...connectors.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], }; storage.ReconnectStorage.add(this.id); this.onConnect(connectResults); return connectResults; } async disconnect() { const provider = await this.getProvider(); await provider.disconnect(); this.onDisconnect(); storage.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: index.numberToHex(chain) }], }); } async addChain() { throw new connectorError.ConnectorError(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 roninWalletConnect.requestRoninWalletConnectProvider({ projectId: connectors.RONIN_WALLET_CONNECT_PROJECT_ID, chains: constant.WC_SUPPORTED_CHAIN_IDS, optionalChains: constant.WC_SUPPORTED_CHAIN_IDS, showQrModal: this.isRonin, ...this.providerOptions, }); } onDisplayUri = (uri) => { this.emit(connectorEvent.ConnectorEvent.DISPLAY_URI, uri); }; setupWalletConnectEvent() { this.getProvider().then(provider => { provider.on(connectorEvent.WCEvent.SESSION_DELETE, this.onDisconnect); provider.on(connectorEvent.WCEvent.DISPLAY_URI, this.onDisplayUri); }); } 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); this.setupWalletConnectEvent(); } } 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); this.provider.removeListener(connectorEvent.WCEvent.DISPLAY_URI, this.onDisplayUri); this.provider.removeListener(connectorEvent.WCEvent.SESSION_DELETE, this.onDisconnect); } } } exports.RoninWalletConnectConnector = RoninWalletConnectConnector;