All files utils.js

28.57% Statements 2/7
0% Branches 0/4
25% Functions 1/4
28.57% Lines 2/7

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    2x                                                       2x                                
import bsv from 'bsv';
 
export const Utils = {
  /**
   * Helper function for encoding strings to hex
   *
   * @param string
   * @returns {string}
   */
  hexEncode(string) {
    return '0x' + Buffer.from(string).toString('hex');
  },
 
  /**
   * Helper function for encoding strings to hex
   *
   * @param hexString
   * @param encoding
   * @returns {string}
   */
  hexDecode(hexString, encoding = 'utf8') {
    return Buffer.from(hexString.replace('0x', ''), 'hex').toString(encoding);
  },
 
  /**
   * Helper function to generate a random nonce
   *
   * @returns {string}
   */
  getRandomString(length = 32) {
    return bsv.crypto.Random.getRandomBuffer(length).toString('hex');
  },
 
  /**
   * Test whether the given string is hex
   *
   * @param value
   * @returns {boolean}
   */
  isHex(value) {
    if (typeof value !== 'string') {
      return false
    }
    return /^[0-9a-fA-F]+$/.test(value)
  },
};