UNPKG

@ledgerhq/live-common

Version:
114 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.objectToURLSearchParams = exports.stripHexPrefix = exports.isHexPrefixed = exports.safeUrl = exports.getInitialURL = exports.getClientHeaders = exports.addParamsToURL = exports.isWalletAPIERC20TokenCurrency = exports.isWalletAPITokenCurrency = exports.isWalletAPICryptoCurrency = exports.isWalletAPISupportedCurrency = void 0; const currencies_1 = require("../currencies"); const constants_1 = require("./constants"); const helpers_1 = require("../helpers"); function isWalletAPISupportedCurrency(currency) { if ((0, currencies_1.isCryptoCurrency)(currency)) { return (0, helpers_1.includes)(constants_1.WALLET_API_FAMILIES, currency.family); } if ((0, currencies_1.isTokenCurrency)(currency)) { return (0, helpers_1.includes)(constants_1.WALLET_API_FAMILIES, currency.parentCurrency.family); } return false; } exports.isWalletAPISupportedCurrency = isWalletAPISupportedCurrency; function isWalletAPICryptoCurrency(currency) { return currency.type === "CryptoCurrency"; } exports.isWalletAPICryptoCurrency = isWalletAPICryptoCurrency; function isWalletAPITokenCurrency(currency) { return currency.type === "TokenCurrency"; } exports.isWalletAPITokenCurrency = isWalletAPITokenCurrency; function isWalletAPIERC20TokenCurrency(currency) { return currency.standard === "ERC20"; } exports.isWalletAPIERC20TokenCurrency = isWalletAPIERC20TokenCurrency; 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); } } } } exports.addParamsToURL = addParamsToURL; function getClientHeaders(params) { return { "x-ledger-host": params.client, "x-ledger-host-theme": params.theme, }; } exports.getClientHeaders = getClientHeaders; 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; }; 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(); } }; exports.getInitialURL = getInitialURL; const safeUrl = (url) => { try { return new URL(url); } catch { return null; } }; exports.safeUrl = safeUrl; // Copied from https://www.npmjs.com/package/ethereumjs-util 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"; }; exports.isHexPrefixed = isHexPrefixed; // Copied from https://www.npmjs.com/package/ethereumjs-util const stripHexPrefix = (str) => { if (typeof str !== "string") throw new Error(`[stripHexPrefix] input must be type 'string', received ${typeof str}`); return (0, exports.isHexPrefixed)(str) ? str.slice(2) : str; }; exports.stripHexPrefix = stripHexPrefix; 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; } exports.objectToURLSearchParams = objectToURLSearchParams; //# sourceMappingURL=helpers.js.map