UNPKG

@web3auth/wallet-connect-v2-adapter

Version:
184 lines (174 loc) 6.36 kB
'use strict'; var base = require('@web3auth/base'); let DEFAULT_EIP155_METHODS = /*#__PURE__*/function (DEFAULT_EIP155_METHODS) { DEFAULT_EIP155_METHODS["ETH_SEND_TRANSACTION"] = "eth_sendTransaction"; DEFAULT_EIP155_METHODS["ETH_SIGN_TRANSACTION"] = "eth_signTransaction"; DEFAULT_EIP155_METHODS["ETH_SIGN"] = "eth_sign"; DEFAULT_EIP155_METHODS["PERSONAL_SIGN"] = "personal_sign"; DEFAULT_EIP155_METHODS["ETH_SIGN_TYPED_DATA"] = "eth_signTypedData"; DEFAULT_EIP155_METHODS["ETH_SIGN_TYPED_DATA_V3"] = "eth_signTypedData_v3"; DEFAULT_EIP155_METHODS["ETH_SIGN_TYPED_DATA_V4"] = "eth_signTypedData_v4"; DEFAULT_EIP155_METHODS["ADD_ETHEREUM_CHAIN"] = "wallet_addEthereumChain"; DEFAULT_EIP155_METHODS["SWITCH_ETHEREUM_CHAIN"] = "wallet_switchEthereumChain"; return DEFAULT_EIP155_METHODS; }({}); let DEFAULT_SOLANA_METHODS = /*#__PURE__*/function (DEFAULT_SOLANA_METHODS) { DEFAULT_SOLANA_METHODS["SIGN_TRANSACTION"] = "solana_signTransaction"; DEFAULT_SOLANA_METHODS["SIGN_MESSAGE"] = "solana_signMessage"; return DEFAULT_SOLANA_METHODS; }({}); let DEFAULT_EIP_155_EVENTS = /*#__PURE__*/function (DEFAULT_EIP_155_EVENTS) { DEFAULT_EIP_155_EVENTS["ETH_CHAIN_CHANGED"] = "chainChanged"; DEFAULT_EIP_155_EVENTS["ETH_ACCOUNTS_CHANGED"] = "accountsChanged"; return DEFAULT_EIP_155_EVENTS; }({}); let DEFAULT_SOLANA_EVENTS = /*#__PURE__*/function (DEFAULT_SOLANA_EVENTS) { DEFAULT_SOLANA_EVENTS["SOL_CHAIN_CHANGED"] = "chainChanged"; DEFAULT_SOLANA_EVENTS["SOL_ACCOUNTS_CHANGED"] = "accountsChanged"; return DEFAULT_SOLANA_EVENTS; }({}); const SOLANA_CAIP_CHAIN_MAP = { "0x65": "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", "0x66": "4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z", "0x67": "EtWTRABZaYq6iMfeYKouRu166VU2xqa1" }; /** * Extracts a name for the site from the DOM */ const getSiteName = window => { const { document } = window; const siteName = document.querySelector('head > meta[property="og:site_name"]'); if (siteName) { return siteName.content; } const metaTitle = document.querySelector('head > meta[name="title"]'); if (metaTitle) { return metaTitle.content; } if (document.title && document.title.length > 0) { return document.title; } return window.location.hostname; }; /** * Returns whether the given image URL exists * @param url - the url of the image * @returns - whether the image exists */ function imgExists(url) { return new Promise((resolve, reject) => { try { const img = document.createElement("img"); img.onload = () => resolve(true); img.onerror = () => resolve(false); img.src = url; } catch (e) { reject(e); } }); } /** * Extracts an icon for the site from the DOM */ async function getSiteIcon(window) { const { document } = window; // Use the site's favicon if it exists let icon = document.querySelector('head > link[rel="shortcut icon"]'); if (icon && (await imgExists(icon.href))) { return icon.href; } // Search through available icons in no particular order icon = Array.from(document.querySelectorAll('head > link[rel="icon"]')).find(_icon => Boolean(_icon.href)) || null; if (icon && (await imgExists(icon.href))) { return icon.href; } return null; } /** * Gets site metadata and returns it * */ const getSiteMetadata = async () => ({ name: getSiteName(window), icon: await getSiteIcon(window) }); const getNamespacesFromChains = chains => { const supportedNamespaces = []; chains.forEach(chainId => { const [namespace] = chainId.split(":"); if (!supportedNamespaces.includes(namespace)) { supportedNamespaces.push(namespace); } }); return supportedNamespaces; }; const getSupportedMethodsByNamespace = namespace => { switch (namespace) { case base.CHAIN_NAMESPACES.EIP155: return Object.values(DEFAULT_EIP155_METHODS); case base.CHAIN_NAMESPACES.SOLANA: return Object.values(DEFAULT_SOLANA_METHODS); default: throw new Error(`No default methods for namespace: ${namespace}`); } }; const getSupportedEventsByNamespace = namespace => { switch (namespace) { case base.CHAIN_NAMESPACES.EIP155: return Object.values(DEFAULT_EIP_155_EVENTS); case base.CHAIN_NAMESPACES.SOLANA: return Object.values(DEFAULT_SOLANA_EVENTS); default: throw new Error(`No default events for namespace: ${namespace}`); } }; const getRequiredNamespaces = chains => { const selectedNamespaces = getNamespacesFromChains(chains); return Object.fromEntries(selectedNamespaces.map(namespace => [namespace, { methods: getSupportedMethodsByNamespace(namespace), chains: chains.filter(chain => chain.startsWith(namespace)), events: getSupportedEventsByNamespace(namespace) }])); }; const getWalletConnectV2Settings = async (namespace, chainIds, projectID) => { if (namespace === base.CHAIN_NAMESPACES.EIP155 || namespace === base.CHAIN_NAMESPACES.SOLANA) { const appMetadata = await getSiteMetadata(); const adapterSettings = { walletConnectInitOptions: { projectId: projectID, relayUrl: "wss://relay.walletconnect.com", metadata: { name: appMetadata.name, description: appMetadata.name, url: window.location.origin, icons: [appMetadata.icon || ""] } } }; const chainNamespaces = chainIds.map(chainId => { return `${namespace}:${namespace === base.CHAIN_NAMESPACES.SOLANA ? SOLANA_CAIP_CHAIN_MAP[chainId] : parseInt(chainId, 16)}`; }); const loginSettings = { optionalNamespaces: getRequiredNamespaces(chainNamespaces) }; return { adapterSettings, loginSettings }; } throw new Error(`Unsupported chain namespace: ${namespace}`); }; exports.DEFAULT_EIP155_METHODS = DEFAULT_EIP155_METHODS; exports.DEFAULT_EIP_155_EVENTS = DEFAULT_EIP_155_EVENTS; exports.DEFAULT_SOLANA_EVENTS = DEFAULT_SOLANA_EVENTS; exports.DEFAULT_SOLANA_METHODS = DEFAULT_SOLANA_METHODS; exports.SOLANA_CAIP_CHAIN_MAP = SOLANA_CAIP_CHAIN_MAP; exports.getNamespacesFromChains = getNamespacesFromChains; exports.getRequiredNamespaces = getRequiredNamespaces; exports.getSupportedEventsByNamespace = getSupportedEventsByNamespace; exports.getSupportedMethodsByNamespace = getSupportedMethodsByNamespace; exports.getWalletConnectV2Settings = getWalletConnectV2Settings;