@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
170 lines (166 loc) • 7.01 kB
JavaScript
'use strict';
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var features = require('@wallet-standard/features');
require('@babel/runtime/helpers/objectSpread2');
require('@segment/analytics-next');
var loglevel = require('../../base/loglevel.js');
var IChainInterface = require('../../base/chain/IChainInterface.js');
require('@web3auth/auth');
var index = require('../../base/errors/index.js');
require('../../base/wallet/index.js');
require('../../base/connector/connectorStatus.js');
var constants = require('../../base/connector/constants.js');
require('jwt-decode');
require('../../base/plugin/errors.js');
require('../../base/plugin/IPlugin.js');
var utils = require('../../base/utils.js');
var utils$1 = require('../../providers/solana-provider/providers/injectedProviders/utils.js');
var walletStandardProvider = require('../../providers/solana-provider/providers/injectedProviders/walletStandardProvider.js');
var baseControllers = require('@toruslabs/base-controllers');
require('@web3auth/ws-embed');
require('@solana/web3.js');
var baseSolanaConnector = require('../base-solana-connector/baseSolanaConnector.js');
class WalletStandardConnector extends baseSolanaConnector.BaseSolanaConnector {
constructor(options) {
super(options);
_defineProperty(this, "name", void 0);
_defineProperty(this, "connectorNamespace", IChainInterface.CONNECTOR_NAMESPACES.SOLANA);
_defineProperty(this, "currentChainNamespace", baseControllers.CHAIN_NAMESPACES.SOLANA);
_defineProperty(this, "type", constants.CONNECTOR_CATEGORY.EXTERNAL);
_defineProperty(this, "isInjected", true);
_defineProperty(this, "status", constants.CONNECTOR_STATUS.NOT_READY);
_defineProperty(this, "wallet", null);
_defineProperty(this, "injectedProvider", 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 provider() {
if (this.status !== constants.CONNECTOR_STATUS.NOT_READY && this.injectedProvider) {
return this.injectedProvider;
}
return null;
}
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.injectedProvider = new walletStandardProvider.WalletStandardProvider({
config: {
chain: chainConfig,
chains: this.coreOptions.chains
}
});
await this.injectedProvider.setupProvider(this.wallet, options.chainId);
this.status = constants.CONNECTOR_STATUS.READY;
this.emit(constants.CONNECTOR_EVENTS.READY, this.name);
try {
loglevel.log.debug("initializing solana injected connector");
if (options.autoConnect) {
this.rehydrated = true;
const provider = await this.connect({
chainId: options.chainId,
getIdentityToken: options.getIdentityToken
});
if (!provider) {
this.rehydrated = false;
throw index.WalletLoginError.connectionError("Failed to rehydrate.");
}
}
} catch (error) {
this.emit(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, error);
}
}
async connect({
chainId,
getIdentityToken
}) {
try {
super.checkConnectionRequirements();
const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId);
if (!chainConfig) throw index.WalletLoginError.connectionError("Chain config is not available");
this.status = constants.CONNECTOR_STATUS.CONNECTING;
this.emit(constants.CONNECTOR_EVENTS.CONNECTING, {
connector: this.name
});
const chainName = utils$1.getSolanaChainByChainConfig(chainConfig);
if (!this.wallet.chains.find(chain => chain === chainName)) throw index.WalletLoginError.connectionError(`Chain ${chainName} not supported. Supported chains are ${this.wallet.chains.join(", ")}`);
if (!this.isWalletConnected) {
await this.wallet.features[features.StandardConnect].connect();
}
if (this.wallet.accounts.length === 0) throw index.WalletLoginError.connectionError();
this.status = constants.CONNECTOR_STATUS.CONNECTED;
let identityTokenInfo;
this.emit(constants.CONNECTOR_EVENTS.CONNECTED, {
connector: this.name,
reconnected: this.rehydrated,
provider: this.provider,
identityTokenInfo
});
if (getIdentityToken) {
identityTokenInfo = await this.getIdentityToken();
}
return this.provider;
} catch (error) {
// ready again to be connected
this.status = constants.CONNECTOR_STATUS.READY;
if (!this.rehydrated) this.emit(constants.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[features.StandardDisconnect]) === null || _this$wallet$features === void 0 ? void 0 : _this$wallet$features.disconnect());
if (options.cleanup) {
this.status = constants.CONNECTOR_STATUS.NOT_READY;
this.injectedProvider = null;
} else {
this.status = constants.CONNECTOR_STATUS.READY;
}
await super.disconnect();
} catch (error) {
this.emit(constants.CONNECTOR_EVENTS.ERRORED, index.WalletLoginError.disconnectionError(error === null || error === void 0 ? void 0 : error.message));
}
}
async getUserInfo() {
if (!this.canAuthorize) throw index.WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
return {};
}
async switchChain(params, init = false) {
var _this$injectedProvide;
super.checkSwitchChainRequirements(params, init);
await ((_this$injectedProvide = this.injectedProvider) === null || _this$injectedProvide === void 0 ? void 0 : _this$injectedProvide.switchChain(params));
}
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: utils.normalizeWalletName(wallet.name),
wallet,
coreOptions
});
};
};
exports.WalletStandardConnector = WalletStandardConnector;
exports.walletStandardConnector = walletStandardConnector;