All files HashValue.ts

17.65% Statements 3/17
0% Branches 0/6
0% Functions 0/8
17.65% Lines 3/17

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 911x   1x     1x                                                                                                                                                                          
import { StreamReader } from '@node-dlc/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 {
    if (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): string {
    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));
  }
}