import { getConfig } from '../main'; const Wallet = require('ethereumjs-wallet'); const EthUtil = require('ethereumjs-util'); export enum MimeType { SVG = 'image/svg+xml', TINYSVG = 'image/tinysvg', PNG = 'image/png', JPG = 'image/jpeg', GIF = 'image/gif', WEBP = 'image/webp', MP4 = 'video/mp4', WEBM = 'video/webm', OGG = 'video/ogg', FLAC = 'audio/flac', MP3 = 'audio/mpeg', WAV = 'audio/wav', OGA = 'audio/ogg', JSON = 'application/json', TEXT = 'text/plain', HTML = 'text/html', CSS = 'text/css', JS = 'text/javascript', WOFF = 'font/woff', WOFF2 = 'font/woff2', TTF = 'font/ttf', EOT = 'font/eot', OTF = 'font/otf', ICO = 'image/x-icon', PDF = 'application/pdf', ZIP = 'application/zip', RAR = 'application/x-rar-compressed', TAR = 'application/x-tar', GZ = 'application/gzip', } export const chainIds = { mainnet: 1, ropsten: 3, rinkeby: 4, goerli: 5, kovan: 42, polygon: 137, mumbai: 80001, bsc: 56, bscTestnet: 97, ganache: 1337, }; //gets the current http url address of the website export const getCurrentURI = () => { let location = window.location.href; let split = location.split('?'); //no get if (split[1] !== undefined) location = split[0]; split = location.split('#'); //no href if (split[1] !== undefined) location = split[0]; if (location.at(-1) !== '/') return location + '/'; return location; }; //gets a current linkable location of the current page +-*+*-+ export const getCurrentLinkableLocation = (addEndingSlash = true) => { let host = window.location.host; if (getCurrentURI().indexOf('https://') === -1) host = 'http://' + host; else host = 'https://' + host; if (!host.endsWith('/')) host = host + '/'; return addEndingSlash ? host : host.substring(0, host.length - 1); }; export const isLocalhost = () => { return window.location.hostname === 'localhost'; }; export const isApiAlive = async () => { try { let res = await fetch(getApiEndpoint('/meta/alive')); return res.status === 200; } catch (err) { return false; } }; export const getMimeType = (extension: string) => { return MimeType[ (extension?.toUpperCase() || 'PNG') as keyof typeof MimeType ]; }; export const apiGet = async ( route: string, params: any = {}, forceDeveloper?: boolean, abortController?: AbortController ) => { let url = getApiEndpoint(route, forceDeveloper); let query = Object.keys(params) .map((k) => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) .join('&'); if (query.length > 0) url = url + '?' + query; let res = await fetch(url, { signal: abortController?.signal, }); if (res.status === 200) return await res.json(); else throw new Error(await res.json()); }; /** * Use this to get the projects full name * @param name * @param version * @param network * @returns */ export const toProjectFullName = ( name: string, version: string, network: string ) => { return `${name}@${version}_${network}`; }; /** * * @returns */ export const isProduction = () => { if (!globalThis?.process) warning( `No globalThis.process object found. You need to webpack process and expose node_env for production to be true. You can also set env in your infinitymint config` ); //work out if this current react app is in production mode return globalThis?.process?.env?.NODE_ENV === 'production'; }; export const warning = (...args: any[]) => { console.warn('[infinitymint]', ...args); }; export const log = (...args: any[]) => { console.log('[infinitymint]', ...args); if (getConfig()?.console) getConfig().console.log(...args); }; export const getAddressFromPrivateKey = (privateKey: string) => { const privateKeyBuffer = EthUtil.toBuffer(privateKey); const wallet = Wallet['default'].fromPrivateKey(privateKeyBuffer); const address = wallet.getAddressString(); return address; }; export const apiFetch = async ( route: string, options?: RequestInit, forceDeveloper?: boolean ) => { let url = getApiEndpoint(route, forceDeveloper); let res = await fetch(url, options); if (res.status === 200) return res.json(); else throw new Error(res.statusText); }; export const unpackColours = (colours) => { const extraColours = colours.pop(); const seedNumber = colours.pop(); const unpackedColours = []; // Is a colour let lastColour; // Get colour const getColour = (baseColour, i) => { const colour = Number.parseInt(baseColour); const r = colour >> 16; const g = colour >> 8; const b = colour >> 24; const seedNumberR = seedNumber >> 8; const seedNumberG = seedNumber >> 16; const seedNumberB = seedNumber >> 24; const seedNumberN = (seedNumber >> 32) ^ i; const combination = parseInt( ( (r * seedNumberR + g * seedNumberG + b * seedNumberB) * seedNumberN ).toString() ); return combination % 0xff_ff_ff; }; for (const [index, value] of colours.entries()) { if (index === 0 || index % 2 === 0) { lastColour = value; continue; } if (index % 2 === 1 && lastColour !== undefined) { for (let i = 0; i < Number.parseInt(value); i++) { unpackedColours.push(getColour(lastColour, i)); } } } for (let i = 0; i < extraColours; i++) { unpackedColours.push(getColour(lastColour, i)); } return unpackedColours; }; export const getApiEndpoint = ( route: string, forceDeveloper: boolean = false ) => { let config = getConfig(); config.api = config.api || {}; if (route[0] !== '/') route = `/${route}`; let apiURL: string; if (!forceDeveloper && isProduction()) apiURL = config.api?.production || config.api?.developer || 'http://localhost:9000'; apiURL = config.api?.developer || config.api?.production || 'http://localhost:9000'; return `${apiURL}${route}`; }; export const cutLongString = (str: string, length = 20) => { if (str.length > length) { return str.substring(0, length) + '..'; } return str; };