All files HashValue.ts

95% Statements 19/20
87.5% Branches 7/8
100% Functions 9/9
94.74% Lines 18/19

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1001x 1x     1x               21x                   49x 48x                   47x 47x                           156x               38x 38x                     1x             114x 110x   4x               44x                 7x 6x      
import { StreamReader } from "@node-lightning/bufio";
import { HashByteOrder } from "./HashByteOrder";
import { ICloneable } from "./ICloneable";
 
export class HashValue implements ICloneable<HashValue> {
    /**
     * Parses a hashed value in internal byte order. This is often
     * referred to as little-endian due to the block target needing to
     * be reversed.
     * @param stream
     */
    public static parse(reader: StreamReader): HashValue {
        return new HashValue(reader.readBytes(32));
    }
 
    /**
     * Parses a hashed value in RPC byte order (which is the reverse) of
     * the natural or internal byte order. This is often referred to as
     * big-endian due to the reversed block target being big-endian.
     * @param stream
     */
    public static fromRpcStream(reader: StreamReader): HashValue {
        const value = reader.readBytes(32).reverse();
        return new HashValue(value);
    }
 
    /**
     * Parses a hashed value in RPC byte order (which is the reverse) of
     * the natural or internal byte order. This is often referred to as
     * big-endian due to the reversed block target being big-endian.
     * @param value
     */
    public static fromRpc(value: string): HashValue {
        const reader = StreamReader.fromHex(value);
        return this.fromRpcStream(reader);
    }
 
    /**
     * Internal-byte-order version of the hash value
     */
    private _value: Buffer;
 
    /**
     * Constructs a new HashValue instance from the internal-byte-order
     * value provided.
     * @param value Interval-byte-order of a hash value
     */
    constructor(value: Buffer) {
        this._value = value;
    }
 
    /**
     * Returns the hash value as a hex string. Defaults to using RPC
     * (reversed/big-endian) byte order.
     */
    public toString(byteOrder: HashByteOrder = HashByteOrder.RPC): string {
        Eif (byteOrder === HashByteOrder.RPC) {
            return Buffer.from(this._value).reverse().toString("hex"); // prettier-ignore
        } else {
            return Buffer.from(this._value).toString("hex");
        }
    }
 
    /**
     * Serializes to JSON returning a hex string. Defaults to using
     * RPC (reversed/big endian) byte order.
     */
    public toJSON(byteOrder?: HashByteOrder): any {
        return this.toString(byteOrder);
    }
 
    /**
     * Serializes the hash value into an internal-byte order Buffer
     */
    public serialize(byteOrder: HashByteOrder = HashByteOrder.Internal): Buffer {
        if (byteOrder === HashByteOrder.Internal) {
            return Buffer.from(this._value);
        } else {
            return Buffer.from(this._value).reverse();
        }
    }
 
    /**
     * Deep copy clone
     */
    public clone(): HashValue {
        return new HashValue(Buffer.from(this._value));
    }
 
    /**
     * Returns true if there is byte-wise equality
     * @param other
     * @returns
     */
    public eq(other: HashValue): boolean {
        if (other === undefined) return false;
        return this._value.equals(other._value);
    }
}