INNER CODE UNIT · TypeScript

snapshotBytes

paulmillr/noble-secp256k1 · index.ts:201

const snapshotBytes = (value: TArg<Uint8Array>, title: string, length?: number): TRet<Uint8Array> =>
  cloneBytes(abytes(value, length, title));
// Callers keep values non-negative and within the requested width; padStart() won't truncate over-wide inputs.
const padh = (n: number | bigint, pad: number) => n.toString(16).padStart(pad, '0');
/** Convert byte array to hex string. */
const bytesToHex = (bytes: TArg<Uint8Array>): string => {
  let hex = '';
  for (const byte of abytes(bytes)) hex += padh(byte, 2);
  return hex;
};
/** Convert hex string to byte array. */
const hexToBytes = (hex: string): TRet<Uint8Array> => {
  const e = 'hex invalid'; // Strict ASCII hex only, with one generic error for parse failures.
  if (typeof hex !== 'string') throw new TypeError(e);
  if (hex.length % 2 || !/^[\da-f]*$/i.test(hex)) throw new RangeError(e);
  const array = new Uint8Array(hex.length / 2);
  for (let ai = 0, hi = 0; ai < array.length; ai++, hi += 2) {
    const n1 = hex.charCodeAt(hi);

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…