Skip to content

Commit 6a70a60

Browse files
Added hex.ts
1 parent 6b5b98c commit 6a70a60

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/primitives/hex.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)