Skip to content

Commit 4d8a802

Browse files
committed
feat: add isValidHexAddress function
1 parent 47b757d commit 4d8a802

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/hex.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
assertIsStrictHexString,
55
isHexString,
66
isStrictHexString,
7+
isValidHexAddress,
78
remove0x,
89
} from './hex';
910

@@ -151,6 +152,54 @@ describe('assertIsStrictHexString', () => {
151152
});
152153
});
153154

155+
describe('isValidHexAddress', () => {
156+
describe('with allowNonPrefixed option set to true', () => {
157+
it.each([
158+
'0000000000000000000000000000000000000000',
159+
'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
160+
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
161+
'0x0000000000000000000000000000000000000000',
162+
])('returns true for a valid prefixed hex address', (hexString) => {
163+
expect(isValidHexAddress(hexString)).toBe(true);
164+
});
165+
166+
it.each([
167+
'0000000000000000000000000000000000000000',
168+
'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
169+
])('returns true for a valid non-prefixed hex address', (hexString) => {
170+
expect(isValidHexAddress(hexString)).toBe(true);
171+
});
172+
});
173+
174+
describe('with allowNonPrefixed option set to false', () => {
175+
it.each([
176+
'0000000000000000000000000000000000000000',
177+
'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
178+
])('returns false for a valid non-prefixed hex address', (hexString) => {
179+
expect(isValidHexAddress(hexString, { allowNonPrefixed: false })).toBe(
180+
false,
181+
);
182+
});
183+
});
184+
185+
it.each([
186+
'12345g',
187+
'1234567890abcdefg',
188+
'1234567890abcdefG',
189+
'1234567890abcdefABCDEFg',
190+
'1234567890abcdefABCDEF1234567890abcdefABCDEFg',
191+
'0x',
192+
'0x0',
193+
'0x12345g',
194+
'0x1234567890abcdefg',
195+
'0x1234567890abcdefG',
196+
'0x1234567890abcdefABCDEFg',
197+
'0x1234567890abcdefABCDEF1234567890abcdefABCDEFg',
198+
])('returns false for an invalid hex address', (hexString) => {
199+
expect(isValidHexAddress(hexString)).toBe(false);
200+
});
201+
});
202+
154203
describe('add0x', () => {
155204
it('adds a 0x-prefix to a string', () => {
156205
expect(add0x('12345')).toBe('0x12345');

src/hex.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ export function assertIsStrictHexString(value: unknown): asserts value is Hex {
5555
);
5656
}
5757

58+
/**
59+
* Validates that the input is a hex address. This utility by default
60+
* will return true for hex strings that meet the length requirement
61+
* of a hex address, but are not necessarily prefixed with `0x`.
62+
*
63+
* @param possibleAddress - Input parameter to check against.
64+
* @param options - The validation options.
65+
* @param options.allowNonPrefixed - If true will allow addresses without `0x` prefix.`
66+
* @returns Whether or not the input is a valid hex address.
67+
*/
68+
export function isValidHexAddress(
69+
possibleAddress: string,
70+
{ allowNonPrefixed = true } = {},
71+
) {
72+
const addressToCheck = allowNonPrefixed
73+
? add0x(possibleAddress)
74+
: possibleAddress;
75+
if (!isHexString(addressToCheck)) {
76+
return false;
77+
}
78+
79+
return is(addressToCheck, pattern(string(), /^0x[0-9a-fA-F]{40}$/u));
80+
}
81+
5882
/**
5983
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
6084
* prefix, it is returned as-is.

0 commit comments

Comments
 (0)