Newer
Older
CrypticOreWallet / src / utils / currency.ts
@Drew Lemmy Drew Lemmy on 24 Feb 2021 2 KB fix: responsiveness tweaks on dashboard
// Copyright (c) 2020-2021 Drew Lemmy
// This file is part of KristWeb 2 under GPL-3.0.
// Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt
import { memoize, escapeRegExp, truncate, toString } from "lodash-es";

const _cleanNameSuffix = (nameSuffix: string | undefined | null): string => {
  // Ensure the name suffix is safe to put into a RegExp
  const stringSuffix = toString(nameSuffix);
  const shortSuffix = truncate(stringSuffix, { length: MAX_NAME_SUFFIX_LENGTH, omission: "" });
  const escaped = escapeRegExp(shortSuffix);
  return escaped;
};
export const cleanNameSuffix = memoize(_cleanNameSuffix);

// Cheap way to avoid RegExp DoS
const MAX_NAME_SUFFIX_LENGTH = 6;
const _getNameRegex = (nameSuffix: string | undefined | null): RegExp =>
  new RegExp(`^(?:([a-z0-9-_]{1,32})@)?([a-z0-9]{1,64}\\.${cleanNameSuffix(nameSuffix)})$`);
export const getNameRegex = memoize(_getNameRegex);

const _stripNameSuffixRegExp = (nameSuffix: string | undefined | null): RegExp =>
  new RegExp(`\\.${cleanNameSuffix(nameSuffix)}$`);
export const stripNameSuffixRegExp = memoize(_stripNameSuffixRegExp);

export const stripNameSuffix = (nameSuffix: string | undefined | null, inp: string): string =>
  inp.replace(stripNameSuffixRegExp(nameSuffix), "");

/**
 * Estimates the network mining hash-rate, returning it as a formatted string.
 *
 * TODO: Some people claimed they had a more accurate function for this. PRs
 *   welcome!
 *
 * @param work - The current block difficulty.
 * @param secondsPerBlock - The number of seconds per block, as per the sync
 *   node's configuration.
*/
export function estimateHashRate(work: number, secondsPerBlock: number): string {
  // Identical to the function from KristWeb 1
  const rate = 1 / (work / (Math.pow(256, 6)) * secondsPerBlock);
  if (rate === 0) return "0 H/s";

  const sizes = ["H", "KH", "MH", "GH", "TH"];
  const i = Math.min(Math.floor(Math.log(rate) / Math.log(1000)), sizes.length);
  return parseFloat((rate / Math.pow(1000, i)).toFixed(2)) + " " + sizes[i] + "/s";
}