@sky-mavis/tanto-connect
Version:
Tanto Connect
104 lines (100 loc) • 3.84 kB
JavaScript
'use strict';
var connectors = require('../../common/connectors.cjs');
var storage = require('../../common/storage.cjs');
require('../../common/constant.cjs');
require('../../types/eip6963.cjs');
var ronin = require('../../providers/ronin.cjs');
require('@walletconnect/ethereum-provider');
require('@safe-global/safe-apps-provider');
require('@safe-global/safe-apps-sdk');
var connectorError = require('../../types/connector-error.cjs');
require('@sky-mavis/waypoint');
require('../../common/chain.cjs');
var index = require('../../utils/index.cjs');
var eip1193 = require('../../types/eip1193.cjs');
var BaseConnector = require('../base/BaseConnector.cjs');
class RoninWalletConnector extends BaseConnector.BaseConnector {
isRonin;
constructor(configs, provider) {
super({ ...connectors.DEFAULT_CONNECTORS_CONFIG.RONIN_WALLET, ...configs }, provider);
this.isRonin = true;
}
async connect(chainId) {
const provider = await this.getProvider();
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);
storage.ReconnectStorage.add(this.id);
return connectResults;
}
async disconnect() {
storage.ReconnectStorage.remove(this.id);
const provider = await this.getProvider();
await provider?.request({
method: 'wallet_disconnectSession',
});
this.removeProviderListeners();
this.onDisconnect();
}
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: 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 ronin.requestRoninProvider();
}
setupProviderListeners() {
this.removeProviderListeners();
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);
}
}
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);
}
}
}
exports.RoninWalletConnector = RoninWalletConnector;