hex.js 597 B

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