INNER CODE UNIT · TypeScript
cloneBytes
paulmillr/noble-secp256k1 · index.ts:200
const cloneBytes = (value: Uint8Array): TRet<Uint8Array> => Uint8Array.from(value);
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) {