UNPKG

biuauthui

Version:
186 lines (176 loc) 5.96 kB
import { get, post } from "@toruslabs/http-helpers"; import { OPENLOGIN_NETWORK_TYPE } from "@toruslabs/openlogin-utils"; import { IAdapter, log, LoginMethodConfig, WALLET_ADAPTERS } from "@web3auth/base"; import CID from "cids"; import { OPENLOGIN_PROVIDERS, OPENLOGIN_PROVIDERS_NAMES, PASSWORDLESS_BACKEND } from "./config"; export const getAdapterSocialLogins = ( adapterName: string, adapter: IAdapter<unknown>, loginMethodsConfig: LoginMethodConfig = {} ): LoginMethodConfig => { const finalLoginMethodsConfig: LoginMethodConfig = {}; if (adapterName === WALLET_ADAPTERS.OPENLOGIN) { OPENLOGIN_PROVIDERS.forEach((loginMethod) => { const currentLoginMethodConfig = loginMethodsConfig[loginMethod] || { name: OPENLOGIN_PROVIDERS_NAMES[loginMethod], showOnMobile: true, showOnModal: true, showOnDesktop: true, }; finalLoginMethodsConfig[loginMethod] = { ...currentLoginMethodConfig }; }); log.debug("OpenLogin login method ui config", finalLoginMethodsConfig); } else { throw new Error(`${adapterName} is not a valid adapter`); } return finalLoginMethodsConfig; }; export async function validateImageUrl(url: string): Promise<boolean> { return new Promise((resolve, reject) => { const img = new Image(); img.src = url; if (img.complete) { resolve(true); } else { img.addEventListener("load", () => { resolve(true); }); img.addEventListener("error", () => { reject(); }); } }); } export async function getNetworkIconId(ticker: string): Promise<string> { const fallbackId = "network-default"; if (!ticker) return fallbackId; try { const url = `https://images.web3auth.io/network-${ticker.toLowerCase()}.svg`; const isValid = await validateImageUrl(url); if (isValid) { return `network-${ticker.toLowerCase()}`; } return fallbackId; } catch { return fallbackId; } } export const getPasswordlessBackendUrl = (web3AuthNetwork: OPENLOGIN_NETWORK_TYPE) => { return PASSWORDLESS_BACKEND[web3AuthNetwork] ?? PASSWORDLESS_BACKEND.mainnet; }; export const getUserCountry = async (web3AuthNetwork: OPENLOGIN_NETWORK_TYPE): Promise<{ country: string; dialCode: string } | null> => { try { const result = await get<{ data: { country: string; dial_code: string } }>(`${getPasswordlessBackendUrl(web3AuthNetwork)}/api/v2/user/location`); if (result && result.data.country) return { country: result.data.country, dialCode: result.data.dial_code }; return null; } catch (error) { log.error("error getting user country", error); return null; } }; export const validatePhoneNumber = async (phoneNumber: string, web3AuthNetwork: OPENLOGIN_NETWORK_TYPE): Promise<string | boolean> => { try { const result = await post<{ success: boolean; parsed_number: string }>( `${getPasswordlessBackendUrl(web3AuthNetwork)}/api/v2/phone_number/validate`, { phone_number: phoneNumber, } ); if (result && result.success) return result.parsed_number; return false; } catch (error: unknown) { log.error("error validating phone number", error); if ((error as Response).status === 400) { return false; } // sending true because we don't want the user to be stuck on a flow // if there is an error with the api or something went wrong. return true; } }; export const languageMap = { en: "english", de: "german", ja: "japanese", ko: "korean", zh: "mandarin", es: "spanish", fr: "french", pt: "portuguese", nl: "dutch", }; export function handleFixIpfs(img = "") { let newImg = img; if (newImg.indexOf("ipfs://") > -1) { let url = newImg.split("//")[1]; if (url.startsWith("Qm") && url.indexOf("/") > 0) { const cid = new CID(url.split("/")[0]); url = cid.toV1().toString(); return `https://${url}.ipfs.nftstorage.link/${newImg.split("//")[1].split("/")[1]}`; } if (url.startsWith("Qm")) { const cid = new CID(url); newImg = cid.toV1().toString(); // return `https://${img}.ipfs.nftstorage.link`; return `https://thedial.infura-ipfs.io/ipfs/${newImg}`; // return `https://gateway.pinata.cloud/ipfs/${url}` // return `https://cloudflare-ipfs.com/ipfs/${url}` } return `https://${url}.ipfs.nftstorage.link`; } return newImg; } const imageCache: { [p: string]: HTMLImageElement } = {}; export function isNft(contract?: string) { if (!contract) return false; if (contract.indexOf("0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") > -1) return false; return true; } export const handleGetImageUrl = (url: string) => { if (!url) { return ""; } if (imageCache[url]) { return imageCache[url].src; } const img = new Image(); img.src = handleFixIpfs(url); imageCache[url] = img; return img.src; }; export function normalizeHexAddress(address: string | Buffer): string { if (!address) { return ""; } const addressString = typeof address === "object" && !("toLowerCase" in address) ? address.toString("hex") : address; if (typeof addressString === "string") { const noPrefix = addressString.replace(/^0x/, ""); const even = noPrefix.length % 2 === 0 ? noPrefix : `0${noPrefix}`; return `0x${Buffer.from(even, "hex").toString("hex")}`; } return ""; } export function isProbablyEVMAddress(str: string | undefined): boolean { if (!str) { return false; } if (normalizeHexAddress(str).startsWith("0x") && str.length === 42) { return true; } return false; } export function truncateAddress(address: string | undefined): string { if (!address) { return ""; } if (isProbablyEVMAddress(address)) { if (address?.length < 14) { return address; } return `${address.slice(0, 6)}${address.slice(-4)}`; } if (address.length > 42) { return `${address.slice(0, 6)}${address.slice(-6)}`; } return address; }