All files encode-pico.ts

100% Statements 21/21
100% Branches 12/12
100% Functions 3/3
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x                             1x 31x 27x 26x 26x                               27x 228x   1x                             26x 23x 19x 12x 3x    
import { hrpToPico } from "./hrp-pico";
 
/**
 * Encodes pico btc to human readable part amount. The hrp amount is defined
 * as the amount of bitcoin as a positive decimal integer with no leading 0s.
 * It is an optional number followed by an optional multiplier letter.
 *
 * The multipliers are:
 * m (milli)   0.001 bitcoin
 * u (micro)   0.000001 bitcoin
 * n (nano)    0.000000001 bitcoin
 * p (pico)    0.000000000001 bitcoin
 *
 * This means that 1msat is 10p and 1sat is 10n.
 */
export function encodePico(pico: string): string {
    if (!pico) return;
    const lsdIndex = _lsdIndex(pico);
    const hrpMultiplier = _minimumHrpMultiplier(lsdIndex);
    return (BigInt(pico) / hrpToPico(hrpMultiplier)).toString() + hrpMultiplier;
}
 
///////////////////////////////////////////////////////////////////////////////
 
/**
 * Finds the index of the least significant digit
 *
 * For example:
 *   if the value is 1002 pico, the first digit with a value is 0
 *   if the value is 1040 pico, the first digit with a value is 1
 *   if the value is 1000 pico, the first digit with a value is 3
 *
 * @return the index the least significant digit
 */
function _lsdIndex(msatStr: string): number {
    for (let i = msatStr.length - 1; i >= 0; i--) {
        if (Number(msatStr[i]) !== 0) return msatStr.length - i - 1;
    }
    throw new Error("Cannot encode 0");
}
 
/**
 * With the least significant digit we can determine the multiplier
 * that is required to miniimze the HRP.
 *
 * For instance
 *   if the value is 1002, we have a value of p
 *   if the value is 1000, we have a value of n
 *
 * @param lsdIndex
 * @return returns the string multiplier
 */
function _minimumHrpMultiplier(lsdIndex: number): string {
    if (lsdIndex < 3) return "p";
    else if (lsdIndex < 6) return "n";
    else if (lsdIndex < 9) return "u";
    else if (lsdIndex < 12) return "m";
    else return "";
}