@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
202 lines (198 loc) • 7.65 kB
JavaScript
'use strict';
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('@segment/analytics-next');
require('../../base/loglevel.js');
var IChainInterface = require('../../base/chain/IChainInterface.js');
require('@web3auth/auth');
var index$1 = require('../../base/errors/index.js');
var index = 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 baseControllers = require('@toruslabs/base-controllers');
require('@toruslabs/constants');
require('@toruslabs/http-helpers');
var baseEvmConnector = require('../base-evm-connector/baseEvmConnector.js');
class CoinbaseConnector extends baseEvmConnector.BaseEvmConnector {
constructor(connectorOptions) {
super(connectorOptions);
_defineProperty(this, "connectorNamespace", IChainInterface.CONNECTOR_NAMESPACES.EIP155);
_defineProperty(this, "currentChainNamespace", baseControllers.CHAIN_NAMESPACES.EIP155);
_defineProperty(this, "type", constants.CONNECTOR_CATEGORY.EXTERNAL);
_defineProperty(this, "name", index.WALLET_CONNECTORS.COINBASE);
_defineProperty(this, "status", constants.CONNECTOR_STATUS.NOT_READY);
_defineProperty(this, "coinbaseProvider", null);
_defineProperty(this, "coinbaseOptions", {
appName: "Web3Auth"
});
this.coinbaseOptions = _objectSpread(_objectSpread({}, this.coinbaseOptions), connectorOptions.connectorSettings);
}
get provider() {
if (this.status !== constants.CONNECTOR_STATUS.NOT_READY && this.coinbaseProvider) {
return this.coinbaseProvider;
}
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 {
createCoinbaseWalletSDK
} = await import('@coinbase/wallet-sdk');
const coinbaseInstance = createCoinbaseWalletSDK(_objectSpread(_objectSpread({}, this.coinbaseOptions), {}, {
preference: {
options: this.coinbaseOptions.options || "smartWalletOnly"
},
appChainIds: this.coreOptions.chains.map(x => Number.parseInt(x.chainId, 16)),
appName: this.coinbaseOptions.appName || "Web3Auth",
appLogoUrl: this.coinbaseOptions.appLogoUrl || ""
}));
this.coinbaseProvider = coinbaseInstance.getProvider();
this.status = constants.CONNECTOR_STATUS.READY;
this.emit(constants.CONNECTOR_EVENTS.READY, index.WALLET_CONNECTORS.COINBASE);
try {
if (options.autoConnect) {
this.rehydrated = true;
const provider = await this.connect({
chainId: options.chainId,
getIdentityToken: options.getIdentityToken
});
// the connect function could fail silently as well.
if (!provider) {
this.rehydrated = false;
throw index$1.WalletLoginError.connectionError("Failed to rehydrate.");
}
}
} catch (error) {
this.emit(constants.CONNECTOR_EVENTS.REHYDRATION_ERROR, error);
}
}
async connect({
chainId,
getIdentityToken
}) {
super.checkConnectionRequirements();
if (!this.coinbaseProvider) throw index$1.WalletLoginError.notConnectedError("Connector is not initialized");
this.status = constants.CONNECTOR_STATUS.CONNECTING;
this.emit(constants.CONNECTOR_EVENTS.CONNECTING, {
connector: index.WALLET_CONNECTORS.COINBASE
});
try {
const chainConfig = this.coreOptions.chains.find(x => x.chainId === chainId);
if (!chainConfig) throw index$1.WalletLoginError.connectionError("Chain config is not available");
await this.coinbaseProvider.request({
method: "eth_requestAccounts"
});
const currentChainId = await this.coinbaseProvider.request({
method: "eth_chainId"
});
if (currentChainId !== chainConfig.chainId) {
await this.switchChain(chainConfig, true);
}
this.status = constants.CONNECTOR_STATUS.CONNECTED;
if (!this.provider) throw index$1.WalletLoginError.notConnectedError("Failed to connect with provider");
this.provider.once("disconnect", () => {
// ready to be connected again
this.disconnect();
});
let identityTokenInfo;
this.emit(constants.CONNECTOR_EVENTS.CONNECTED, {
connector: index.WALLET_CONNECTORS.COINBASE,
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;
// only throw error if the connector is not rehydrated.
if (!this.rehydrated) this.emit(constants.CONNECTOR_EVENTS.ERRORED, error);
this.rehydrated = false;
if (error instanceof index$1.Web3AuthError) throw error;
throw index$1.WalletLoginError.connectionError("Failed to login with coinbase wallet", 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 = constants.CONNECTOR_STATUS.NOT_READY;
this.coinbaseProvider = null;
} else {
// ready to be connected again
this.status = constants.CONNECTOR_STATUS.READY;
}
await super.disconnect();
}
async getUserInfo() {
if (!this.canAuthorize) throw index$1.WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
return {};
}
async switchChain(params, init = false) {
super.checkSwitchChainRequirements(params, init);
try {
await this.coinbaseProvider.request({
method: "wallet_switchEthereumChain",
params: [{
chainId: params.chainId
}]
});
} catch (switchError) {
// 4902 indicates that the client does not recognize the Harmony One network
if (switchError.code === 4902) {
const chainConfig = this.coreOptions.chains.find(x => x.chainId === params.chainId);
if (!chainConfig) throw index$1.WalletLoginError.connectionError("Chain config is not available");
await this.coinbaseProvider.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 coinbaseConnector = params => {
return ({
coreOptions
}) => {
return new CoinbaseConnector({
connectorSettings: params,
coreOptions
});
};
};
exports.coinbaseConnector = coinbaseConnector;