UNPKG

@ledgerhq/live-common

Version:
100 lines 3.35 kB
import { isCryptoCurrency, isTokenCurrency } from "../currencies"; import { WALLET_API_FAMILIES } from "./constants"; import { includes } from "../helpers"; export function isWalletAPISupportedCurrency(currency) { if (isCryptoCurrency(currency)) { return includes(WALLET_API_FAMILIES, currency.family); } if (isTokenCurrency(currency)) { return includes(WALLET_API_FAMILIES, currency.parentCurrency.family); } return false; } export function isWalletAPICryptoCurrency(currency) { return currency.type === "CryptoCurrency"; } export function isWalletAPITokenCurrency(currency) { return currency.type === "TokenCurrency"; } export function isWalletAPIERC20TokenCurrency(currency) { return currency.standard === "ERC20"; } export function addParamsToURL(url, inputs) { if (inputs) { const keys = Object.keys(inputs); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = inputs[key]; if (value !== undefined) { url.searchParams.set(key, value); } } } } export function getClientHeaders(params) { return { "x-ledger-host": params.client, "x-ledger-host-theme": params.theme, }; } const isWhitelistedDomain = (url, whitelistedDomains) => { const isValid = whitelistedDomains.reduce((acc, whitelistedDomain) => acc ? acc : new RegExp(whitelistedDomain).test(url), false); if (!isValid) { console.error("#isWhitelistedDomain:: invalid URL: url is not whitelisted"); } return isValid; }; export const getInitialURL = (inputs, manifest) => { try { if (inputs?.goToURL && isWhitelistedDomain(inputs?.goToURL, manifest.domains)) { return inputs?.goToURL; } const url = new URL(manifest.url); addParamsToURL(url, inputs); if (manifest.params) { url.searchParams.set("params", JSON.stringify(manifest.params)); } return url.toString(); } catch (e) { if (e instanceof Error) console.error(e.message); return manifest.url.toString(); } }; export const safeUrl = (url) => { try { return new URL(url); } catch { return null; } }; // Copied from https://www.npmjs.com/package/ethereumjs-util export const isHexPrefixed = (str) => { if (typeof str !== "string") { throw new Error(`[isHexPrefixed] input must be type 'string', received type ${typeof str}`); } return str[0] === "0" && str[1] === "x"; }; // Copied from https://www.npmjs.com/package/ethereumjs-util export const stripHexPrefix = (str) => { if (typeof str !== "string") throw new Error(`[stripHexPrefix] input must be type 'string', received ${typeof str}`); return isHexPrefixed(str) ? str.slice(2) : str; }; export function objectToURLSearchParams(obj) { const searchParams = new URLSearchParams(); Object.entries(obj).forEach(([key, value]) => { if (value !== undefined && value !== null) { if (typeof value === "object") { searchParams.append(key, JSON.stringify(value)); } else { searchParams.append(key, String(value)); } } }); return searchParams; } //# sourceMappingURL=helpers.js.map