We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6b5b98c commit 6a70a60Copy full SHA for 6a70a60
src/primitives/hex.ts
@@ -0,0 +1,23 @@
1
+// src/primitives/hex.ts
2
+
3
+const PURE_HEX_REGEX = /^[0-9a-fA-F]+$/
4
5
+export function assertValidHex (msg: string): void {
6
+ if (typeof msg !== 'string' || msg.length === 0 || !PURE_HEX_REGEX.test(msg)) {
7
+ throw new Error('Invalid hex string')
8
+ }
9
+}
10
11
+export function normalizeHex (msg: string): string {
12
+ assertValidHex(msg)
13
14
+ // Lowercase first
15
+ let normalized = msg.toLowerCase()
16
17
+ // Prepend "0" if odd-length
18
+ if (normalized.length % 2 === 1) {
19
+ normalized = '0' + normalized
20
21
22
+ return normalized
23
0 commit comments