@sky-mavis/tanto-connect
Version:
Tanto Connect
102 lines • 4.42 kB
JavaScript
import {authorize}from'@sky-mavis/waypoint';import {DEFAULT_CONNECTORS_CONFIG}from'../../common/connectors.mjs';import {LocalStorage,WAYPOINT_ACCESS_TOKEN_STORAGE_KEY,ReconnectStorage}from'../../common/storage.mjs';import'../../common/constant.mjs';import'../../types/eip6963.mjs';import {ConnectorError,ConnectorErrorType}from'../../types/connector-error.mjs';import'@walletconnect/ethereum-provider';import'@safe-global/safe-apps-provider';import'@safe-global/safe-apps-sdk';import {requestWaypointProvider}from'../../providers/waypoint-provider.mjs';import {EIP1193Event}from'../../types/eip1193.mjs';import {BaseConnector}from'../base/BaseConnector.mjs';class WaypointConnector extends BaseConnector {
providerConfigs;
isRonin;
constructor({ connectorConfigs, providerConfigs, provider, }) {
super({ ...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(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED);
}
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 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: LocalStorage.get(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;
LocalStorage.set(WAYPOINT_ACCESS_TOKEN_STORAGE_KEY, accessToken);
}
ReconnectStorage.add(this.id);
this.onConnect(connectResult);
return connectResult;
}
async authorize(configs) {
const { clientId } = this.providerConfigs || {};
const currentClientId = configs.clientId;
const authorizationResult = await authorize({
clientId: (currentClientId || clientId),
...configs,
});
if (!authorizationResult) {
return null;
}
const { address, token: accessToken, secondaryAddress } = authorizationResult;
LocalStorage.set(WAYPOINT_ACCESS_TOKEN_STORAGE_KEY, accessToken);
return {
account: address,
accessToken,
secondaryAccount: secondaryAddress,
};
}
async disconnect() {
LocalStorage.remove(WAYPOINT_ACCESS_TOKEN_STORAGE_KEY);
const provider = await this.getProvider();
provider.disconnect();
ReconnectStorage.remove(this.id);
this.onDisconnect();
this.removeProviderListeners();
}
async requestProvider(configs) {
return requestWaypointProvider({ ...this.providerConfigs, ...configs });
}
setupProviderListeners() {
this.removeProviderListeners();
if (this.provider) {
this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect);
}
}
removeProviderListeners() {
if (this.provider) {
this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect);
}
}
}export{WaypointConnector};