UNPKG

@web3auth/no-modal

Version:
78 lines (74 loc) 2.09 kB
'use strict'; var eccrypto = require('@toruslabs/eccrypto'); var metadataHelpers = require('@toruslabs/metadata-helpers'); var auth = require('@web3auth/auth'); /** * 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 undefined; } function parseToken(token) { const [header, payload] = token.split("."); return { header: auth.base64toJSON(header), payload: auth.base64toJSON(payload) }; } const generateNonce = () => { return metadataHelpers.bytesToHexPrefixedString(eccrypto.generatePrivate()); }; exports.generateNonce = generateNonce; exports.getSiteIcon = getSiteIcon; exports.getSiteName = getSiteName; exports.parseToken = parseToken;