All files bech32-util.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 4/4
100% Lines 20/20

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 74 75 76 77 78 79                          1x           171x 171x 171x   171x 171x 11995x 11995x   11995x 9097x 9097x       171x 68x     171x                       1x 3x                     1x 32x                       1x 31x    
/**
 * Converts a Buffer into a a word array and optionally pads bits.
 * This function is needed because the default bech32 fromWords/toWords
 * functions do not allow us to optionally include/exclude padding.
 * The encoder needs to pad bits.  The decoder does not care about the
 * padding bits.
 *
 * @param data input data
 * @param inBits number of bits in, usually 5 or 8
 * @param outBits number of bits out, usualy 8 or 5
 * @param pad indicates if output should be padded
 * @return converted words
 */
export function convertWords(
    data: number[] | Buffer,
    inBits: number,
    outBits: number,
    pad: boolean,
): number[] {
    let value = 0;
    let bits = 0;
    const maxV = (1 << outBits) - 1;
 
    const result: number[] = [];
    for (let i = 0; i < data.length; ++i) {
        value = (value << inBits) | data[i];
        bits += inBits;
 
        while (bits >= outBits) {
            bits -= outBits;
            result.push((value >> bits) & maxV);
        }
    }
 
    if (pad && bits > 0) {
        result.push((value << (outBits - bits)) & maxV);
    }
 
    return result;
}
 
/**
 * Calculates the number of words needed to store the number.
 * In bech32, each word is 5 bits (2^5 = 32). To properly
 * encode a number, we need to determine how many words
 * are necessary to encode the value.
 *
 * @param num integer value
 * @returns number of words
 */
export function sizeofNum(num: number): number {
    return Math.ceil(Math.log2(num) / 5);
}
 
/**
 * Calculates the number of words needed to store
 * the supplied number of bits. In bech32, each words is 5 bits
 * (2^5 = 32).
 *
 * @param bits number of bits
 * @returns number of words
 */
export function sizeofBits(bits: number): number {
    return Math.ceil(bits / 5);
}
 
/**
 * Calculates the number of words needeed to store
 * the supplied number of bytes. In bech32, each words is 5 bits
 * (2^5 = 32). We first convert the bytes to bits and then
 * convert to words.
 *
 * @param bytes
 * @returns number of words
 */
export function sizeofBytes(bytes: number): number {
    return sizeofBits(bytes * 8);
}