UNPKG

@sky-mavis/tanto-connect

Version:
120 lines (116 loc) 4.61 kB
'use strict'; var waypoint = require('@sky-mavis/waypoint'); var connectors = require('../../common/connectors.cjs'); var storage = require('../../common/storage.cjs'); require('../../common/constant.cjs'); require('../../types/eip6963.cjs'); var connectorError = require('../../types/connector-error.cjs'); require('@walletconnect/ethereum-provider'); require('@safe-global/safe-apps-provider'); require('@safe-global/safe-apps-sdk'); var waypointProvider = require('../../providers/waypoint-provider.cjs'); var eip1193 = require('../../types/eip1193.cjs'); var BaseConnector = require('../base/BaseConnector.cjs'); class WaypointConnector extends BaseConnector.BaseConnector { providerConfigs; isRonin; constructor({ connectorConfigs, providerConfigs, provider, }) { super({ ...connectors.DEFAULT_CONNECTORS_CONFIG.WAYPOINT, ...connectorConfigs }, provider); this.providerConfigs = providerConfigs; this.isRonin = true; } 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() { throw new connectorError.ConnectorError(connectorError.ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED); } 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 connect(chainId) { const currentChainId = await this.getChainId(); if (chainId && currentChainId !== chainId) { this.provider = await this.requestProvider({ chainId }); } const provider = await this.getProvider(); const accounts = await this.getAccounts(); const account = accounts[0]; const connectResult = { accessToken: storage.LocalStorage.get(storage.WAYPOINT_ACCESS_TOKEN_STORAGE_KEY), chainId: chainId || currentChainId, provider, account, }; if (!account || !connectResult.accessToken) { const { address, token: accessToken } = await provider.connect(); connectResult.account = address; connectResult.accessToken = accessToken; storage.LocalStorage.set(storage.WAYPOINT_ACCESS_TOKEN_STORAGE_KEY, accessToken); } storage.ReconnectStorage.add(this.id); this.onConnect(connectResult); return connectResult; } async authorize(configs) { const { clientId } = this.providerConfigs || {}; const currentClientId = configs.clientId; const authorizationResult = await waypoint.authorize({ clientId: (currentClientId || clientId), ...configs, }); if (!authorizationResult) { return null; } const { address, token: accessToken, secondaryAddress } = authorizationResult; storage.LocalStorage.set(storage.WAYPOINT_ACCESS_TOKEN_STORAGE_KEY, accessToken); return { account: address, accessToken, secondaryAccount: secondaryAddress, }; } async disconnect() { storage.LocalStorage.remove(storage.WAYPOINT_ACCESS_TOKEN_STORAGE_KEY); const provider = await this.getProvider(); provider.disconnect(); storage.ReconnectStorage.remove(this.id); this.onDisconnect(); this.removeProviderListeners(); } async requestProvider(configs) { return waypointProvider.requestWaypointProvider({ ...this.providerConfigs, ...configs }); } setupProviderListeners() { this.removeProviderListeners(); if (this.provider) { this.provider.on(eip1193.EIP1193Event.DISCONNECT, this.onDisconnect); } } removeProviderListeners() { if (this.provider) { this.provider.removeListener(eip1193.EIP1193Event.DISCONNECT, this.onDisconnect); } } } exports.WaypointConnector = WaypointConnector;