All files Base58.ts

100% Statements 29/29
100% Branches 4/4
100% Functions 3/3
100% Lines 25/25

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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 741x               1x 8650x           1x 1x                             93x 93x 99x 93x       93x 93x 93x 8650x 8650x 8650x       93x                   82x 82x 136x 82x       82x 82x 7676x 7676x     82x      
import { bigFromBufBE, bigToBufBE } from "@node-lightning/bufio";
 
/**
 * Returns a tuple containing the quotient and remainder when the divident (num) is
 * divided by the divisor (mod).
 * @param num divident
 * @param mod divisor
 */
export function divmod(num: bigint, mod: bigint): [bigint, bigint] {
    return [num / mod, num % mod];
}
 
/**
 * Base58 encoding and decoding utility
 */
export class Base58 {
    public static alphabet: string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
 
    /**
     * Performs base58 encoding by following this process:
     *
     * 1. Finds the leading zeros an creates 1's that will be
     *    prefixed
     * 2. Converts each value into a base58 character by using
     *    the mod operator on the character.
     * 3. The prefix and results of encoding from the base58 characters
     *    are combined.
     * @param buf
     */
    public static encode(buf: Buffer): string {
        // count the leading zero bytes at the front
        let prefix = "";
        for (let i = 0; i < buf.length; i++) {
            if (buf[i] === 0) prefix += "1";
            else break;
        }
 
        // determine what base58 digit to use
        let num = bigFromBufBE(buf);
        let result = "";
        while (num > 0n) {
            const [n, mod] = divmod(num, 58n);
            result = Base58.alphabet[Number(mod)] + result;
            num = n;
        }
 
        // prepend all the prefixed 0's
        return prefix + result;
    }
 
    /**
     * Decodes a base58 string into its original buffer by constructing
     * a number represented in base58 and converting that number into bytes
     * @param input
     */
    public static decode(input: string): Buffer {
        // determine leading zero bytes which will be prepended
        let prefix = 0;
        for (let i = 0; i < input.length; i++) {
            if (input[i] === "1") prefix += 1;
            else break;
        }
 
        // process remaining bytes
        let num = 0n;
        for (const char of input) {
            num *= 58n;
            num += BigInt(Base58.alphabet.indexOf(char));
        }
 
        return Buffer.concat([Buffer.alloc(prefix), bigToBufBE(num)]);
    }
}