buffer-utils.ts 1.11 KB
Newer Older
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
/* External Imports */
import { BigNumber } from 'ethers'

/**
 * Converts a string or buffer to a '0x'-prefixed hex string.
 * @param buf String or buffer to convert.
 * @returns '0x'-prefixed string.
 */
export const toHexString = (buf: Buffer | string): string => {
  return '0x' + fromHexString(buf).toString('hex')
}

/**
 * Converts a '0x'-prefixed string to a buffer.
 * @param str '0x'-prefixed string to convert.
 * @returns Hex buffer.
 */
export const fromHexString = (str: string | Buffer): Buffer => {
  if (typeof str === 'string' && str.startsWith('0x')) {
    return Buffer.from(str.slice(2), 'hex')
  }

  return Buffer.from(str)
}

export const toHexString32 = (
  input: Buffer | string | number,
  padRight = false
): string => {
  if (typeof input === 'number') {
    input = BigNumber.from(input).toHexString()
  }

  input = toHexString(input).slice(2)
  return '0x' + (padRight ? input.padEnd(64, '0') : input.padStart(64, '0'))
}

export const getHexSlice = (
  input: Buffer | string,
  start: number,
  length: number
): string => {
  return toHexString(fromHexString(input).slice(start, start + length))
}