Skip to content

Commit

Permalink
feat: add isValidHexAddress function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Jul 6, 2023
1 parent 47b757d commit 4d8a802
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/hex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
assertIsStrictHexString,
isHexString,
isStrictHexString,
isValidHexAddress,
remove0x,
} from './hex';

Expand Down Expand Up @@ -151,6 +152,54 @@ describe('assertIsStrictHexString', () => {
});
});

describe('isValidHexAddress', () => {
describe('with allowNonPrefixed option set to true', () => {
it.each([
'0000000000000000000000000000000000000000',
'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
'0x0000000000000000000000000000000000000000',
])('returns true for a valid prefixed hex address', (hexString) => {
expect(isValidHexAddress(hexString)).toBe(true);
});

it.each([
'0000000000000000000000000000000000000000',
'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
])('returns true for a valid non-prefixed hex address', (hexString) => {
expect(isValidHexAddress(hexString)).toBe(true);
});
});

describe('with allowNonPrefixed option set to false', () => {
it.each([
'0000000000000000000000000000000000000000',
'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
])('returns false for a valid non-prefixed hex address', (hexString) => {
expect(isValidHexAddress(hexString, { allowNonPrefixed: false })).toBe(
false,
);
});
});

it.each([
'12345g',
'1234567890abcdefg',
'1234567890abcdefG',
'1234567890abcdefABCDEFg',
'1234567890abcdefABCDEF1234567890abcdefABCDEFg',
'0x',
'0x0',
'0x12345g',
'0x1234567890abcdefg',
'0x1234567890abcdefG',
'0x1234567890abcdefABCDEFg',
'0x1234567890abcdefABCDEF1234567890abcdefABCDEFg',
])('returns false for an invalid hex address', (hexString) => {
expect(isValidHexAddress(hexString)).toBe(false);
});
});

describe('add0x', () => {
it('adds a 0x-prefix to a string', () => {
expect(add0x('12345')).toBe('0x12345');
Expand Down
24 changes: 24 additions & 0 deletions src/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ export function assertIsStrictHexString(value: unknown): asserts value is Hex {
);
}

/**
* Validates that the input is a hex address. This utility by default
* will return true for hex strings that meet the length requirement
* of a hex address, but are not necessarily prefixed with `0x`.
*
* @param possibleAddress - Input parameter to check against.
* @param options - The validation options.
* @param options.allowNonPrefixed - If true will allow addresses without `0x` prefix.`
* @returns Whether or not the input is a valid hex address.
*/
export function isValidHexAddress(
possibleAddress: string,
{ allowNonPrefixed = true } = {},
) {
const addressToCheck = allowNonPrefixed
? add0x(possibleAddress)
: possibleAddress;
if (!isHexString(addressToCheck)) {
return false;
}

return is(addressToCheck, pattern(string(), /^0x[0-9a-fA-F]{40}$/u));
}

/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
* prefix, it is returned as-is.
Expand Down

0 comments on commit 4d8a802

Please sign in to comment.