UNPKG

@web3auth/no-modal

Version:
177 lines (173 loc) 6.63 kB
'use strict'; var _objectSpread = require('@babel/runtime/helpers/objectSpread2'); var ethereumControllers = require('@toruslabs/ethereum-controllers'); var utils$1 = require('@walletconnect/utils'); var auth = require('@web3auth/auth'); var wsEmbed = require('@web3auth/ws-embed'); require('@babel/runtime/helpers/defineProperty'); require('@segment/analytics-next'); var loglevel = require('../../base/loglevel.js'); require('@toruslabs/base-controllers'); require('@toruslabs/session-manager'); var index = require('../../base/errors/index.js'); require('@toruslabs/constants'); require('@toruslabs/http-helpers'); require('../../base/constants.js'); require('../../base/wallet/index.js'); require('../../base/connector/connectorStatus.js'); require('../../base/connector/constants.js'); require('jwt-decode'); require('../../base/plugin/errors.js'); require('../../base/plugin/IPlugin.js'); var utils = require('./utils.js'); async function getLastActiveSession(signClient) { if (signClient.session.length) { const lastKeyIndex = signClient.session.keys.length - 1; return signClient.session.get(signClient.session.keys[lastKeyIndex]); } return null; } function isMobileDevice() { return /Mobi|Android|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i.test(window.navigator.userAgent); } function isSolanaChain(chainId) { return chainId.startsWith("solana:"); } async function sendJrpcRequest(signClient, chainId, method, params) { const session = await getLastActiveSession(signClient); if (!session) { throw auth.providerErrors.disconnected(); } if (typeof window !== "undefined" && isMobileDevice()) { if (session.peer.metadata.redirect && session.peer.metadata.redirect.native) { const redirectUrl = session.peer.metadata.redirect.native; try { const parsedUrl = new URL(redirectUrl); if (["javascript:", "data:", "vbscript:"].includes(parsedUrl.protocol)) { throw new Error("Invalid redirect scheme"); } window.open(parsedUrl.href, "_blank"); } catch (e) { loglevel.log.error("Invalid redirect URL", e); } } } return signClient.request({ topic: session.topic, chainId, request: { method, params: isSolanaChain(chainId) ? _objectSpread(_objectSpread({}, params), {}, { pubkey: session.self.publicKey }) : params } }); } async function getAccounts(signClient) { const session = await getLastActiveSession(signClient); if (!session) { throw auth.providerErrors.disconnected(); } const accounts = utils$1.getAccountsFromNamespaces(session.namespaces); if (accounts && accounts.length) { return [...new Set(accounts.map(add => { return utils$1.parseAccountId(add).address; }))]; } throw index.WalletLoginError.connectionError("Failed to get accounts"); } async function getSolanaAccounts(signClient) { const session = await getLastActiveSession(signClient); if (!session) { throw auth.providerErrors.disconnected(); } const accounts = utils$1.getAccountsFromNamespaces(session.namespaces); return [...new Set(accounts.filter(add => add.startsWith("solana:")).map(add => utils$1.parseAccountId(add).address))]; } function getEthProviderHandlers({ connector, chainId }) { return { getPrivateKey: async () => { throw auth.rpcErrors.methodNotSupported(); }, getPublicKey: async () => { throw auth.rpcErrors.methodNotSupported(); }, getAccounts: async _ => { return getAccounts(connector); }, processTransaction: async (txParams, _) => { const methodRes = await sendJrpcRequest(connector, `eip155:${chainId}`, wsEmbed.EVM_METHOD_TYPES.ETH_TRANSACTION, [txParams]); return methodRes; }, processSignTransaction: async (txParams, _) => { const methodRes = await sendJrpcRequest(connector, `eip155:${chainId}`, "eth_signTransaction", [txParams]); return methodRes; }, processEthSignMessage: async (msgParams, _) => { const methodRes = await sendJrpcRequest(connector, `eip155:${chainId}`, wsEmbed.EVM_METHOD_TYPES.ETH_SIGN, [msgParams.from, msgParams.data]); return methodRes; }, processPersonalMessage: async (msgParams, _) => { const methodRes = await sendJrpcRequest(connector, `eip155:${chainId}`, wsEmbed.EVM_METHOD_TYPES.PERSONAL_SIGN, [msgParams.data, msgParams.from]); return methodRes; }, processTypedMessageV4: async msgParams => { const methodRes = await sendJrpcRequest(connector, `eip155:${chainId}`, wsEmbed.EVM_METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4, [msgParams.from, msgParams.data]); return methodRes; }, // EIP-5792: wallet_getCapabilities processGetCapabilities: async params => { const capabilities = await sendJrpcRequest(connector, `eip155:${chainId}`, ethereumControllers.EIP_5792_METHODS.WALLET_GET_CAPABILITIES, params); return capabilities; }, // EIP-5792: wallet_sendCalls processSendCalls: async params => { const results = await sendJrpcRequest(connector, `eip155:${chainId}`, ethereumControllers.EIP_5792_METHODS.WALLET_SEND_CALLS, [params]); return results; }, // EIP-5792: wallet_getCallsStatus processGetCallsStatus: async batchId => { const results = await sendJrpcRequest(connector, `eip155:${chainId}`, ethereumControllers.EIP_5792_METHODS.WALLET_GET_CALLS_STATUS, [batchId]); return results; }, // EIP-5792: wallet_showCallsStatus processShowCallsStatus: async batchId => { await sendJrpcRequest(connector, `eip155:${chainId}`, ethereumControllers.EIP_5792_METHODS.WALLET_SHOW_CALLS_STATUS, [batchId]); } }; } async function switchChain({ connector, chainId, newChainId }) { await sendJrpcRequest(connector, `eip155:${chainId}`, "wallet_switchEthereumChain", [{ chainId: newChainId }]); } async function addChain({ connector, chainId, chainConfig }) { if (!chainConfig) { throw auth.providerErrors.custom({ message: "Chain config is required", code: 4902 }); } const formattedChainId = utils.formatChainId(chainConfig.chainId); const formattedChainConfig = _objectSpread(_objectSpread({}, chainConfig), {}, { chainId: formattedChainId }); await sendJrpcRequest(connector, `eip155:${chainId}`, "wallet_addEthereumChain", [formattedChainConfig]); } exports.addChain = addChain; exports.getAccounts = getAccounts; exports.getEthProviderHandlers = getEthProviderHandlers; exports.getSolanaAccounts = getSolanaAccounts; exports.sendJrpcRequest = sendJrpcRequest; exports.switchChain = switchChain;