hex.js 569 B

12345678910111213141516
  1. import { assertU8, fromUint8 } from './fallback/_utils.js'
  2. import * as js from './fallback/hex.js'
  3. const { toHex: webHex } = Uint8Array.prototype // Modern engines have this
  4. export function toHex(arr) {
  5. assertU8(arr)
  6. if (arr.length === 0) return ''
  7. if (webHex && arr.toHex === webHex) return arr.toHex()
  8. return js.toHex(arr)
  9. }
  10. // Unlike Buffer.from(), throws on invalid input
  11. export const fromHex = Uint8Array.fromHex
  12. ? (str, format = 'uint8') => fromUint8(Uint8Array.fromHex(str), format)
  13. : (str, format = 'uint8') => fromUint8(js.fromHex(str), format)