# All files Base58.ts

14.81% Statements 4/27
0% Branches 0/4
0% Functions 0/3
16.67% Lines 4/24

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
    641x

    1x

    1x
    1x

     import { bigToBufBE } from '@node-dlc/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 =
        '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

      /**
       * Encodes a buffer into a base58 string
       */
      public static encode(buf: Buffer): string {
        let n = BigInt('0x' + buf.toString('hex'));

        // encode into base58
        let str = '';
        while (n > BigInt(0)) {
          const mod = n % BigInt(58);
          str = Base58.alphabet[Number(mod)] + str;
          n = n / BigInt(58);
        }

        // add leading zeros
        for (const val of buf) {
          if (val !== 0) break;
          str = '1' + str;
        }

        return str;
      }

      /**
       * Decodes the base58 string into a buffer
       */
      public static decode(str: string): Buffer {
        // determine leading zero bytes which will be prepended
        let prefix = 0;
        for (let i = 0; i < str.length; i++) {
          if (str[i] === '1') prefix += 1;
          else break;
        }

        // decode from base58
        let n = BigInt(0);
        for (const char of str) {
          n *= BigInt(58);
          n += BigInt(Base58.alphabet.indexOf(char));
        }

        return Buffer.concat([Buffer.alloc(prefix), bigToBufBE(n)]);
      }
    }