@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
199 lines (196 loc) • 7.68 kB
JavaScript
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { getSiteName, getSiteIcon } from '../utils.js';
import { BaseEvmConnector } from '../base-evm-connector/baseEvmConnector.js';
import { CONNECTOR_NAMESPACES } from '../../base/chain/IChainInterface.js';
import { WALLET_CONNECTORS } from '../../base/wallet/index.js';
import { WalletLoginError, Web3AuthError } from '../../base/errors/index.js';
import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers';
import { CONNECTOR_CATEGORY, CONNECTOR_STATUS, CONNECTOR_EVENTS } from '../../base/connector/constants.js';
class BaseAccountConnector extends BaseEvmConnector {
constructor(connectorOptions) {
super(connectorOptions);
_defineProperty(this, "connectorNamespace", CONNECTOR_NAMESPACES.EIP155);
_defineProperty(this, "currentChainNamespace", CHAIN_NAMESPACES.EIP155);
_defineProperty(this, "type", CONNECTOR_CATEGORY.EXTERNAL);
_defineProperty(this, "name", WALLET_CONNECTORS.BASE_ACCOUNT);
_defineProperty(this, "status", CONNECTOR_STATUS.NOT_READY);
_defineProperty(this, "baseAccountProvider", null);
_defineProperty(this, "baseAccountOptions", {
appName: "Web3Auth"
});
this.baseAccountOptions = _objectSpread(_objectSpread({}, this.baseAccountOptions), connectorOptions.connectorSettings);
}
get provider() {
if (this.status !== CONNECTOR_STATUS.NOT_READY && this.baseAccountProvider) {
return this.baseAccountProvider;
}
return null;
}
set provider(_) {
throw new Error("Not implemented");
}
async init(options) {
await super.init(options);
const chainConfig = this.coreOptions.chains.find(x => x.chainId === options.chainId);
super.checkInitializationRequirements({
chainConfig
});
const {
createBaseAccountSDK
} = await import('@base-org/account');
let appName = this.baseAccountOptions.appName || "Web3Auth";
let appLogoUrl = this.baseAccountOptions.appLogoUrl || "";
if (typeof window !== "undefined") {
if (!this.baseAccountOptions.appName) {
appName = getSiteName(window) || "Web3Auth";
}
if (!this.baseAccountOptions.appLogoUrl) {
appLogoUrl = (await getSiteIcon(window)) || "";
}
}
const appChainIds = this.coreOptions.chains.filter(x => x.chainNamespace === CHAIN_NAMESPACES.EIP155).map(x => Number.parseInt(x.chainId, 16));
const sdk = createBaseAccountSDK(_objectSpread(_objectSpread({}, this.baseAccountOptions), {}, {
appName,
appLogoUrl: appLogoUrl || null,
appChainIds: this.baseAccountOptions.appChainIds || appChainIds
}));
this.baseAccountProvider = sdk.getProvider();
this.status = CONNECTOR_STATUS.READY;
this.emit(CONNECTOR_EVENTS.READY, WALLET_CONNECTORS.BASE_ACCOUNT);
try {
if (options.autoConnect) {
this.rehydrated = true;
const connection = await this.connect({
chainId: options.chainId,
getAuthTokenInfo: options.getAuthTokenInfo
});
if (!connection) {
this.rehydrated = false;
throw WalletLoginError.connectionError("Failed to rehydrate.");
}
}
} catch (error) {
this.emit(CONNECTOR_EVENTS.REHYDRATION_ERROR, error);
}
}
async connect({
chainId,
getAuthTokenInfo
}) {
super.checkConnectionRequirements();
if (!this.baseAccountProvider) throw WalletLoginError.notConnectedError("Connector is not initialized");
this.status = CONNECTOR_STATUS.CONNECTING;
this.emit(CONNECTOR_EVENTS.CONNECTING, {
connector: WALLET_CONNECTORS.BASE_ACCOUNT
});
try {
const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId);
if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available");
await this.baseAccountProvider.request({
method: "eth_requestAccounts"
});
const currentChainId = await this.baseAccountProvider.request({
method: "eth_chainId"
});
if (currentChainId !== chainConfig.chainId) {
await this.switchChain(chainConfig, true);
}
this.status = CONNECTOR_STATUS.CONNECTED;
if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider");
this.provider.once("disconnect", () => {
this.disconnect();
});
this.emit(CONNECTOR_EVENTS.CONNECTED, {
connectorName: WALLET_CONNECTORS.BASE_ACCOUNT,
reconnected: this.rehydrated,
ethereumProvider: this.provider,
solanaWallet: null,
connectorNamespace: this.connectorNamespace
});
await this.authorizeOrDisconnect(getAuthTokenInfo);
return {
ethereumProvider: this.provider,
solanaWallet: null,
connectorName: this.name,
connectorNamespace: this.connectorNamespace
};
} catch (error) {
this.status = CONNECTOR_STATUS.READY;
if (!this.rehydrated) this.emit(CONNECTOR_EVENTS.ERRORED, error);
this.rehydrated = false;
if (error instanceof Web3AuthError) throw error;
throw WalletLoginError.connectionError("Failed to login with Base Account", error);
}
}
async disconnect(options = {
cleanup: false
}) {
var _this$provider;
await super.disconnectSession();
(_this$provider = this.provider) === null || _this$provider === void 0 || _this$provider.removeAllListeners();
if (options.cleanup) {
this.status = CONNECTOR_STATUS.NOT_READY;
this.baseAccountProvider = null;
} else {
this.status = CONNECTOR_STATUS.READY;
}
await super.disconnect();
}
async getUserInfo() {
if (!this.canAuthorize) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
return {};
}
async switchChain(params, init = false) {
super.checkSwitchChainRequirements(params, init);
try {
var _this$baseAccountProv;
await ((_this$baseAccountProv = this.baseAccountProvider) === null || _this$baseAccountProv === void 0 ? void 0 : _this$baseAccountProv.request({
method: "wallet_switchEthereumChain",
params: [{
chainId: params.chainId
}]
}));
} catch (switchError) {
if (switchError.code === 4902) {
var _this$baseAccountProv2;
const chainConfig = this.coreOptions.chains.find(x => x.chainId === params.chainId);
if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available");
await ((_this$baseAccountProv2 = this.baseAccountProvider) === null || _this$baseAccountProv2 === void 0 ? void 0 : _this$baseAccountProv2.request({
method: "wallet_addEthereumChain",
params: [{
chainId: chainConfig.chainId,
rpcUrls: [chainConfig.rpcTarget],
chainName: chainConfig.displayName,
nativeCurrency: {
name: chainConfig.tickerName,
symbol: chainConfig.ticker,
decimals: chainConfig.decimals || 18
},
blockExplorerUrls: [chainConfig.blockExplorerUrl],
iconUrls: [chainConfig.logo]
}]
}));
return;
}
throw switchError;
}
}
async enableMFA() {
throw new Error("Method Not implemented");
}
async manageMFA() {
throw new Error("Method Not implemented");
}
}
const baseAccountConnector = params => {
return ({
coreOptions
}) => {
return new BaseAccountConnector({
connectorSettings: params,
coreOptions
});
};
};
export { baseAccountConnector };