Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isHex and isHexStrict for some edge cases and enrich their tests #5655

Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -923,6 +923,10 @@ should use 4.0.1-alpha.0 for testing.

- Use Uuid for the response id, to fix the issue "Responses get mixed up due to conflicting payload IDs" (#5373).

#### web3-validator

- Fix `isHex`returning `false` for `-123`, fix `isHexStrict` returning `true` for `-0x`, and fix `isHex` returning `true` for empty strings `` (#5373).

#### web3-eth-abi

- Fix ContractMethodOutputParameters type to support output object types by index and string key. Also, it returns void if ABI doesn't have outputs and returns exactly one type if the output array has only one element. (#5631)
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-validator/CHANGELOG.md
Expand Up @@ -42,3 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed direct function `toJSON()` in `Web3ValidatorError` class as its available via base class (#5435)

## [Unreleased]

### Fixed

- Fix `isHex`returning `false` for `-123`, fix `isHexStrict` returning `true` for `-0x`, and fix `isHex` returning `true` for empty strings `` (#5373).
4 changes: 2 additions & 2 deletions packages/web3-validator/src/validation/string.ts
Expand Up @@ -23,12 +23,12 @@ import { ValidInputTypes } from '../types';
export const isString = (value: ValidInputTypes) => typeof value === 'string';

export const isHexStrict = (hex: ValidInputTypes) =>
typeof hex === 'string' && /^(-)?0x[0-9a-f]*$/i.test(hex);
typeof hex === 'string' && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved

export const isHex = (hex: ValidInputTypes): boolean =>
typeof hex === 'number' ||
typeof hex === 'bigint' ||
(typeof hex === 'string' && /^(-0x|0x)?[0-9a-f]*$/i.test(hex));
(typeof hex === 'string' && /^((-0x|0x|-)?[0-9a-f]+|(0x))$/i.test(hex));

export const isHexString8Bytes = (value: string, prefixed = true) =>
prefixed ? isHexStrict(value) && value.length === 18 : isHex(value) && value.length === 16;
Expand Down
12 changes: 9 additions & 3 deletions packages/web3-validator/test/fixtures/validation.ts
Expand Up @@ -132,11 +132,16 @@ export const validHexStrictDataWithNumber: [string, number | bigint][] = [
export const validHexStrictData: any[] = [
...validHexStrictDataWithNumber.map(tuple => tuple[0]),
'-0xdec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
'0x',
];

export const invalidHexData: any[] = [
'Heeäööä👅D34ɝɣ24Єͽ',
'-1000',
'',
'-',
'-0x',
'x',
'0x0x',
'0xH',
'I have 100£',
'\u0000',
Expand All @@ -152,10 +157,11 @@ export const invalidHexData: any[] = [
export const invalidHexStrictData: any[] = [
...invalidHexData,
'45',
'',
'-45',
'0',
1,
BigInt(12),
BigInt(''),
BigInt(-255),
-42,
4.2,
Expand All @@ -164,7 +170,7 @@ export const invalidHexStrictData: any[] = [
export const validHexData: any[] = [
...validHexStrictData,
'45',
'',
'-45',
'0',
1,
BigInt(12),
Expand Down
5 changes: 4 additions & 1 deletion packages/web3-validator/test/unit/utils.test.ts
Expand Up @@ -92,7 +92,10 @@ describe('utils', () => {
);

it.each(validHexStrictData)('valid hex strings', input => {
expect(numberToHex(input)).toEqual((input as string).toLowerCase());
expect(numberToHex(input)).toEqual(
// if input is '' then numberToHex would return "0x0"
(input === '' ? '0x0' : (input as string)).toLowerCase(),
);
});

it.each(validStringNumbersWithHex)('valid string numbers', (input, res) => {
Expand Down