@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
146 lines (143 loc) • 5.83 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { StandardConnect, StandardDisconnect } from '@wallet-standard/features';
import { normalizeWalletName } from '../../base/utils.js';
import { BaseSolanaConnector } from '../base-solana-connector/baseSolanaConnector.js';
import { CONNECTOR_NAMESPACES } from '../../base/chain/IChainInterface.js';
import { log } from '../../base/loglevel.js';
import { WalletLoginError } from '../../base/errors/index.js';
import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers';
import { CONNECTOR_CATEGORY, CONNECTOR_STATUS, CONNECTOR_EVENTS } from '../../base/connector/constants.js';
import { getSolanaChainByChainConfig } from '../../base/wallet/solana.js';
class WalletStandardConnector extends BaseSolanaConnector {
constructor(options) {
super(options);
_defineProperty(this, "name", void 0);
_defineProperty(this, "connectorNamespace", CONNECTOR_NAMESPACES.SOLANA);
_defineProperty(this, "currentChainNamespace", CHAIN_NAMESPACES.SOLANA);
_defineProperty(this, "type", CONNECTOR_CATEGORY.EXTERNAL);
_defineProperty(this, "isInjected", true);
_defineProperty(this, "status", CONNECTOR_STATUS.NOT_READY);
_defineProperty(this, "wallet", null);
this.name = options.name;
this.icon = options.wallet.icon;
// in VueJS, for some wallets e.g. Gate, Solflare, when connecting it throws error "attempted to get private field on non-instance"
// it seems that Vue create a Proxy object for the wallet object which causes the issue
// ref: https://stackoverflow.com/questions/64917686/vue-array-converted-to-proxy-object
this.wallet = ["gate", "solflare"].includes(this.name) ? Object.freeze(options.wallet) : options.wallet;
}
get solanaWallet() {
return this.wallet;
}
get isWalletConnected() {
return !!(this.connected && this.wallet.accounts.length > 0);
}
async init(options) {
await super.init(options);
const chainConfig = this.coreOptions.chains.find(x => x.chainId === options.chainId);
super.checkInitializationRequirements({
chainConfig
});
this.status = CONNECTOR_STATUS.READY;
this.emit(CONNECTOR_EVENTS.READY, this.name);
try {
log.debug("initializing solana injected connector");
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
}) {
try {
super.checkConnectionRequirements();
const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId);
if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available");
this.status = CONNECTOR_STATUS.CONNECTING;
this.emit(CONNECTOR_EVENTS.CONNECTING, {
connector: this.name
});
const chainName = getSolanaChainByChainConfig(chainConfig);
if (!this.wallet.chains.find(chain => chain === chainName)) throw WalletLoginError.connectionError(`Chain ${chainName} not supported. Supported chains are ${this.wallet.chains.join(", ")}`);
if (!this.isWalletConnected) {
await this.wallet.features[StandardConnect].connect();
}
if (this.wallet.accounts.length === 0) throw WalletLoginError.connectionError();
this.status = CONNECTOR_STATUS.CONNECTED;
const connectorNamespace = this.connectorNamespace;
this.emit(CONNECTOR_EVENTS.CONNECTED, {
connectorName: this.name,
reconnected: this.rehydrated,
ethereumProvider: null,
solanaWallet: this.solanaWallet,
connectorNamespace
});
await this.authorizeOrDisconnect(getAuthTokenInfo);
return {
ethereumProvider: null,
solanaWallet: this.solanaWallet,
connectorName: this.name,
connectorNamespace
};
} catch (error) {
// ready again to be connected
this.status = CONNECTOR_STATUS.READY;
if (!this.rehydrated) this.emit(CONNECTOR_EVENTS.ERRORED, error);
this.rehydrated = false;
throw error;
}
}
async disconnect(options = {
cleanup: false
}) {
await super.disconnectSession();
try {
var _this$wallet$features;
await ((_this$wallet$features = this.wallet.features[StandardDisconnect]) === null || _this$wallet$features === void 0 ? void 0 : _this$wallet$features.disconnect());
if (options.cleanup) {
this.status = CONNECTOR_STATUS.NOT_READY;
} else {
this.status = CONNECTOR_STATUS.READY;
}
await super.disconnect();
} catch (error) {
this.emit(CONNECTOR_EVENTS.ERRORED, WalletLoginError.disconnectionError(error === null || error === void 0 ? void 0 : error.message));
}
}
async getUserInfo() {
if (!this.canAuthorize) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
return {};
}
async switchChain(_params, _init = false) {
throw WalletLoginError.unsupportedOperation("Injected Solana wallets do not support chain switching.");
}
async enableMFA() {
throw new Error("Method not implemented.");
}
async manageMFA() {
throw new Error("Method Not implemented");
}
}
const walletStandardConnector = wallet => {
return ({
coreOptions
}) => {
return new WalletStandardConnector({
name: normalizeWalletName(wallet.name),
wallet,
coreOptions
});
};
};
export { WalletStandardConnector, walletStandardConnector };