From 625d3b7a296021760ec518c10baded73779cb4aa Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 2 Aug 2026 14:27:04 -0700 Subject: [PATCH] Add hex typing. --- src/index.ts | 3 +++ src/lib/convert/hex.ts | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/index.ts b/src/index.ts index bbc9941..5170ad1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,9 @@ import { Wallet } from './lib/wallet' declare global { type Bytes = Uint8Array | Buffer + type Hex = [HexByte] + type HexByte = `${HexChar}${HexChar}` + type HexChar = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' } export { diff --git a/src/lib/convert/hex.ts b/src/lib/convert/hex.ts index f37d4de..f148e60 100644 --- a/src/lib/convert/hex.ts +++ b/src/lib/convert/hex.ts @@ -2,6 +2,16 @@ //! SPDX-License-Identifier: GPL-3.0-or-later export const hex = Object.freeze({ + /** + * Check if input is a valid hexadecimal string. + * + * @param {unknown} input - Variable to check + * @returns True if input is hex, else false + */ + is (input: unknown): input is Hex { + return typeof input === 'string' && /([0-9a-f]{2})+/.test(input) + }, + /** * Convert a hexadecimal string to an ArrayBuffer. The result must be at least * one byte and no more than 4 GiB. -- 2.52.0