@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
204 lines (200 loc) • 13.7 kB
JavaScript
;
var baseControllers = require('@toruslabs/base-controllers');
var constants$1 = require('@toruslabs/constants');
var httpHelpers = require('@toruslabs/http-helpers');
require('@babel/runtime/helpers/defineProperty');
var auth = require('@web3auth/auth');
var index = require('./errors/index.js');
require('./wallet/index.js');
require('./connector/connectorStatus.js');
require('./connector/constants.js');
require('jwt-decode');
var constants = require('./constants.js');
const isHexStrict = hex => {
return (typeof hex === "string" || typeof hex === "number") && /^(-)?0x[0-9a-f]*$/i.test(hex);
};
const signerHost = (web3AuthNetwork = auth.WEB3AUTH_NETWORK.SAPPHIRE_MAINNET) => {
return constants$1.SIGNER_MAP[web3AuthNetwork];
};
const fetchProjectConfig = async ({
clientId,
web3AuthNetwork,
aaProvider,
authBuildEnv
}) => {
const url = new URL(`${signerHost(web3AuthNetwork)}/api/v2/configuration`);
url.searchParams.append("project_id", clientId);
url.searchParams.append("network", web3AuthNetwork);
if (authBuildEnv) url.searchParams.append("build_env", authBuildEnv);
if (aaProvider) url.searchParams.append("aa_provider", aaProvider);
const res = await httpHelpers.get(url.href);
return res;
};
const fetchWalletRegistry = async url => {
const res = await httpHelpers.get(url || constants.WALLET_REGISTRY_URL);
return res;
};
// Normalize wallet name to a standard format, used for external wallets that are auto-detected by MIPD (EIP-6963 and Wallet Standard)
const normalizeWalletName = name => {
let normalizedName = name.toLowerCase();
// remove decriptive part after | e.g. "Crypto.com | Defi Wallet" => "Crypto.com"
normalizedName = normalizedName.split("|")[0];
// replace - with space e.g. "Trust - Wallet" => "Trust Wallet"
normalizedName = normalizedName.replace(/-/g, " ");
// replace multiple spaces with single space
normalizedName = normalizedName.replace(/\s+/g, " ");
// remove trailing "wallet" e.g. "Trust Wallet" => "Trust", "GateWallet" => "Gate"
normalizedName = normalizedName.replace(/wallet$/i, "").trim();
// replace space with -
normalizedName = normalizedName.replace(/\s/g, "-");
return normalizedName;
};
const fromWagmiChain = chain => {
var _chain$blockExplorers, _chain$rpcUrls$defaul;
return {
chainNamespace: baseControllers.CHAIN_NAMESPACES.EIP155,
chainId: `0x${chain.id.toString(16)}`,
rpcTarget: chain.rpcUrls.default.http[0],
displayName: chain.name,
blockExplorerUrl: ((_chain$blockExplorers = chain.blockExplorers) === null || _chain$blockExplorers === void 0 ? void 0 : _chain$blockExplorers.default.url) || "",
ticker: chain.nativeCurrency.symbol,
tickerName: chain.nativeCurrency.name,
logo: "",
decimals: chain.nativeCurrency.decimals,
isTestnet: chain.testnet,
wsTarget: (_chain$rpcUrls$defaul = chain.rpcUrls.default.webSocket) === null || _chain$rpcUrls$defaul === void 0 ? void 0 : _chain$rpcUrls$defaul[0]
};
};
function withAbort(fn, signal, onAbort) {
if (!signal) return fn();
if (signal.aborted) return Promise.reject(new DOMException("Aborted", "AbortError"));
return new Promise((resolve, reject) => {
const abort = () => {
onAbort === null || onAbort === void 0 || onAbort();
reject(new DOMException("Aborted", "AbortError"));
};
signal.addEventListener("abort", abort);
return Promise.resolve().then(() => fn()).then(resolve, reject).finally(() => {
signal.removeEventListener("abort", abort);
});
});
}
const isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
const fromViemChain = fromWagmiChain;
const getWhitelabelAnalyticsProperties = uiConfig => {
var _uiConfig$theme, _uiConfig$theme2;
return {
whitelabel_app_name: uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.appName,
whitelabel_app_url: uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.appUrl,
whitelabel_logo_light_enabled: Boolean(uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.logoLight),
whitelabel_logo_dark_enabled: Boolean(uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.logoDark),
whitelabel_default_language: uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.defaultLanguage,
whitelabel_theme_mode: uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.mode,
whitelabel_use_logo_loader: uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.useLogoLoader,
whitelabel_theme_primary: uiConfig === null || uiConfig === void 0 || (_uiConfig$theme = uiConfig.theme) === null || _uiConfig$theme === void 0 ? void 0 : _uiConfig$theme.primary,
whitelabel_theme_on_primary: uiConfig === null || uiConfig === void 0 || (_uiConfig$theme2 = uiConfig.theme) === null || _uiConfig$theme2 === void 0 ? void 0 : _uiConfig$theme2.onPrimary,
whitelabel_tnc_link_enabled: Boolean(uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.tncLink),
whitelabel_privacy_policy_enabled: Boolean(uiConfig === null || uiConfig === void 0 ? void 0 : uiConfig.privacyPolicy)
};
};
const getAaAnalyticsProperties = accountAbstractionConfig => {
var _accountAbstractionCo, _accountAbstractionCo2, _accountAbstractionCo3, _accountAbstractionCo4, _accountAbstractionCo5;
const bundlerHostnames = Array.from(new Set(accountAbstractionConfig === null || accountAbstractionConfig === void 0 || (_accountAbstractionCo = accountAbstractionConfig.chains) === null || _accountAbstractionCo === void 0 ? void 0 : _accountAbstractionCo.map(chain => {
var _chain$bundlerConfig;
return getHostname((_chain$bundlerConfig = chain.bundlerConfig) === null || _chain$bundlerConfig === void 0 ? void 0 : _chain$bundlerConfig.url);
}).filter(Boolean)));
const paymasterHostnames = Array.from(new Set(accountAbstractionConfig === null || accountAbstractionConfig === void 0 || (_accountAbstractionCo2 = accountAbstractionConfig.chains) === null || _accountAbstractionCo2 === void 0 ? void 0 : _accountAbstractionCo2.map(chain => {
var _chain$paymasterConfi;
return getHostname((_chain$paymasterConfi = chain.paymasterConfig) === null || _chain$paymasterConfi === void 0 ? void 0 : _chain$paymasterConfi.url);
}).filter(Boolean)));
return {
aa_smart_account_type: accountAbstractionConfig === null || accountAbstractionConfig === void 0 ? void 0 : accountAbstractionConfig.smartAccountType,
aa_chain_ids: accountAbstractionConfig === null || accountAbstractionConfig === void 0 || (_accountAbstractionCo3 = accountAbstractionConfig.chains) === null || _accountAbstractionCo3 === void 0 ? void 0 : _accountAbstractionCo3.map(chain => getCaipChainId({
chainId: chain.chainId,
chainNamespace: baseControllers.CHAIN_NAMESPACES.EIP155
})),
aa_bundler_urls: bundlerHostnames,
aa_paymaster_urls: paymasterHostnames,
aa_paymaster_enabled: paymasterHostnames.length > 0,
aa_paymaster_context_enabled: accountAbstractionConfig === null || accountAbstractionConfig === void 0 || (_accountAbstractionCo4 = accountAbstractionConfig.chains) === null || _accountAbstractionCo4 === void 0 ? void 0 : _accountAbstractionCo4.some(chain => {
var _chain$bundlerConfig2;
return (_chain$bundlerConfig2 = chain.bundlerConfig) === null || _chain$bundlerConfig2 === void 0 ? void 0 : _chain$bundlerConfig2.paymasterContext;
}),
aa_erc20_paymaster_enabled: accountAbstractionConfig === null || accountAbstractionConfig === void 0 || (_accountAbstractionCo5 = accountAbstractionConfig.chains) === null || _accountAbstractionCo5 === void 0 ? void 0 : _accountAbstractionCo5.some(chain => {
var _chain$bundlerConfig3;
return (_chain$bundlerConfig3 = chain.bundlerConfig) === null || _chain$bundlerConfig3 === void 0 || (_chain$bundlerConfig3 = _chain$bundlerConfig3.paymasterContext) === null || _chain$bundlerConfig3 === void 0 ? void 0 : _chain$bundlerConfig3.token;
})
};
};
const getWalletServicesAnalyticsProperties = walletServicesConfig => {
var _walletServicesConfig, _walletServicesConfig2, _walletServicesConfig3, _walletServicesConfig4, _walletServicesConfig5, _walletServicesConfig6, _walletServicesConfig7, _walletServicesConfig8, _walletServicesConfig9, _walletServicesConfig0, _walletServicesConfig1, _walletServicesConfig10;
return {
ws_confirmation_strategy: walletServicesConfig === null || walletServicesConfig === void 0 ? void 0 : walletServicesConfig.confirmationStrategy,
ws_enable_key_export: walletServicesConfig === null || walletServicesConfig === void 0 ? void 0 : walletServicesConfig.enableKeyExport,
ws_show_widget_button: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig = walletServicesConfig.whiteLabel) === null || _walletServicesConfig === void 0 ? void 0 : _walletServicesConfig.showWidgetButton,
ws_button_position: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig2 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig2 === void 0 ? void 0 : _walletServicesConfig2.buttonPosition,
ws_hide_nft_display: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig3 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig3 === void 0 ? void 0 : _walletServicesConfig3.hideNftDisplay,
ws_hide_token_display: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig4 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig4 === void 0 ? void 0 : _walletServicesConfig4.hideTokenDisplay,
ws_hide_transfers: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig5 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig5 === void 0 ? void 0 : _walletServicesConfig5.hideTransfers,
ws_hide_topup: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig6 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig6 === void 0 ? void 0 : _walletServicesConfig6.hideTopup,
ws_hide_receive: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig7 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig7 === void 0 ? void 0 : _walletServicesConfig7.hideReceive,
ws_hide_swap: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig8 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig8 === void 0 ? void 0 : _walletServicesConfig8.hideSwap,
ws_hide_show_all_tokens: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig9 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig9 === void 0 ? void 0 : _walletServicesConfig9.hideShowAllTokens,
ws_hide_wallet_connect: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig0 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig0 === void 0 ? void 0 : _walletServicesConfig0.hideWalletConnect,
ws_hide_defi_positions_display: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig1 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig1 === void 0 ? void 0 : _walletServicesConfig1.hideDefiPositionsDisplay,
ws_default_portfolio: walletServicesConfig === null || walletServicesConfig === void 0 || (_walletServicesConfig10 = walletServicesConfig.whiteLabel) === null || _walletServicesConfig10 === void 0 ? void 0 : _walletServicesConfig10.defaultPortfolio
};
};
const sdkVersion = "10.16.0";
const getErrorAnalyticsProperties = error => {
try {
const code = error instanceof index.Web3AuthError ? error.code : error === null || error === void 0 ? void 0 : error.code;
const message = error instanceof Error ? error.message : (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : error.toString());
return {
error_message: message,
error_code: code
};
} catch {
return {
error_message: "Unknown error",
error_code: undefined
};
}
};
const getHostname = url => {
try {
return new URL(url).hostname;
} catch {
return "";
}
};
const getCaipChainId = chain => {
if (chain.chainNamespace === baseControllers.CHAIN_NAMESPACES.EIP155) {
return `${chain.chainNamespace}:${parseInt(chain.chainId, 16)}`;
}
if (chain.chainNamespace === baseControllers.CHAIN_NAMESPACES.SOLANA) {
return `${chain.chainNamespace}:${constants.SOLANA_CAIP_CHAIN_MAP[chain.chainId]}`;
}
// for other chain namespaces, we just return the chainId as is
return `${chain.chainNamespace}:${chain.chainId}`;
};
Object.defineProperty(exports, "cloneDeep", {
enumerable: true,
get: function () { return baseControllers.cloneDeep; }
});
exports.fetchProjectConfig = fetchProjectConfig;
exports.fetchWalletRegistry = fetchWalletRegistry;
exports.fromViemChain = fromViemChain;
exports.fromWagmiChain = fromWagmiChain;
exports.getAaAnalyticsProperties = getAaAnalyticsProperties;
exports.getCaipChainId = getCaipChainId;
exports.getErrorAnalyticsProperties = getErrorAnalyticsProperties;
exports.getHostname = getHostname;
exports.getWalletServicesAnalyticsProperties = getWalletServicesAnalyticsProperties;
exports.getWhitelabelAnalyticsProperties = getWhitelabelAnalyticsProperties;
exports.isBrowser = isBrowser;
exports.isHexStrict = isHexStrict;
exports.normalizeWalletName = normalizeWalletName;
exports.sdkVersion = sdkVersion;
exports.signerHost = signerHost;
exports.withAbort = withAbort;