@sky-mavis/tanto-connect
Version:
Tanto Connect
101 lines • 3.91 kB
JavaScript
import {CHAINS_CONFIG}from'../../common/chain.mjs';import {ReconnectStorage}from'../../common/storage.mjs';import {ConnectorError,ConnectorErrorType}from'../../types/connector-error.mjs';import {EIP1193Event}from'../../types/eip1193.mjs';import {numberToHex}from'../../utils/index.mjs';import {BaseConnector}from'../base/BaseConnector.mjs';class InjectedConnector extends BaseConnector {
isRonin;
provider;
constructor(configs, provider) {
super(configs);
this.isRonin = !!provider.isRonin;
this.provider = provider;
}
async connect(chainId) {
const provider = await this.getProvider();
try {
const accounts = await this.requestAccounts();
const currentChainId = await this.getChainId();
if (chainId && currentChainId !== chainId) {
await this.switchChain(chainId);
}
const connectResults = {
provider,
chainId: chainId || currentChainId,
account: accounts[0],
};
this.setupProviderListeners();
this.onConnect(connectResults);
ReconnectStorage.add(this.id);
return connectResults;
}
catch (err) {
throw new ConnectorError(ConnectorErrorType.CONNECT_FAILED, err);
}
}
async disconnect() {
ReconnectStorage.remove(this.id);
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',
});
}
async switchChain(chain) {
const provider = await this.getProvider();
return provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chain) }],
});
}
async addChain(chain) {
const provider = await this.getProvider();
const { iconUrl, blockExplorerUrl, ...chainConfig } = CHAINS_CONFIG[chain] ?? {};
if (!chainConfig) {
throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED);
}
return provider.request({
method: 'wallet_addEthereumChain',
params: [
{
...chainConfig,
chainId: numberToHex(chain),
iconUrls: [iconUrl],
blockExplorerUrls: [blockExplorerUrl],
},
],
});
}
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 this.provider;
}
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);
}
}
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);
}
}
}export{InjectedConnector};