All files word-cursor.ts

96.3% Statements 26/27
80% Branches 4/5
100% Functions 7/7
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 531x   1x         38x 38x       94x       102x 102x 102x 102x 211x 211x   102x       44x 44x       199x 199x 199x 427x 427x   199x 199x       115x 115x 115x       146x      
import * as bech32Util from "./bech32-util";
 
export class WordCursor {
    public words: number[];
    public position: number;
 
    constructor(words: number[] = []) {
        this.words = words;
        this.position = 0;
    }
 
    public get wordsRemaining(): number {
        return this.words.length - this.position;
    }
 
    public writeUIntBE(val: number, wordLen: number) {
        Iif (!wordLen) throw new Error("wordLen must be provided");
        const words = new Array(wordLen);
        const maxV = (1 << 5) - 1;
        for (let i = wordLen - 1; i >= 0; i--) {
            words[i] = val & maxV;
            val >>= 5;
        }
        this._merge(words);
    }
 
    public writeBytes(buf: Buffer, pad: boolean = true) {
        const words = bech32Util.convertWords(buf, 8, 5, pad);
        this._merge(words);
    }
 
    public readUIntBE(numWords: number) {
        const words = this.words.slice(this.position, this.position + numWords);
        let val = 0;
        for (const word of words) {
            val <<= 5;
            val |= word;
        }
        this.position += numWords;
        return val;
    }
 
    public readBytes(numWords: number, pad: boolean = false) {
        const words = this.words.slice(this.position, this.position + numWords);
        this.position += numWords;
        return Buffer.from(bech32Util.convertWords(words, 5, 8, pad));
    }
 
    private _merge(words: number[]) {
        this.words = this.words.concat(words);
    }
}