Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,37 @@ web3.utils.toWei('0.1');
// 4.x
web3.utils.toWei('0.1', 'ether');
```

## Validation functions

Validation functions has been moved to the new package `web3-validator`. Actually, you can still import them from `web3-util`. But they are marked as "deprecated" and you are encouraged to import them from `web3-validator`.

However, there are changes for the following:

### `isHex` and `isHexStrict` validation functions

There is a fix, and some edge-cases-changes for those 2 functions but the overall functionality stayed the same. And here is exactly whet changed:

#### `isHex` now returns `true` for all negative numbers

```ts
isHex('-123'); // in 1.x used to return `false`. But changed in 4.x to return `true`
// `true`
```

#### `isHex` now returns `false` for an empty string

```ts
isHex(''); // in 1.x used to return `true`. But changed in 4.x to return `false`
// `false`
```

#### `isHex` and `isHexStrict` now returns `false` for `'-0x'`

```ts
isHex('-0x'); // in 1.x used to return `true`. But changed in 4.x to return `false`
// `false`

isHexStrict('-0x'); // in 1.x used to return `true`. But changed in 4.x to return `false`
// `false`
```
4 changes: 4 additions & 0 deletions packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);

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
13 changes: 10 additions & 3 deletions packages/web3-validator/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,17 @@ export const validHexStrictDataWithNumber: [string, number | bigint][] = [
export const validHexStrictData: any[] = [
...validHexStrictDataWithNumber.map(tuple => tuple[0]),
'-0xdec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
'0x',
'0X',
];

export const invalidHexData: any[] = [
'Heeäööä👅D34ɝɣ24Єͽ',
'-1000',
'',
'-',
'-0x',
'x',
'0x0x',
'0xH',
'I have 100£',
'\u0000',
Expand All @@ -152,10 +158,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 +171,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
Original file line number Diff line number Diff line change
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