diff --git a/CHANGELOG.md b/CHANGELOG.md index bc91c060afe..2b2f21f1784 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md b/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md index 21d2009b9b8..093eed34f4c 100644 --- a/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md +++ b/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md @@ -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` +``` diff --git a/packages/web3-validator/CHANGELOG.md b/packages/web3-validator/CHANGELOG.md index 2f9ef019bdb..519e85a0bc1 100644 --- a/packages/web3-validator/CHANGELOG.md +++ b/packages/web3-validator/CHANGELOG.md @@ -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). diff --git a/packages/web3-validator/src/validation/string.ts b/packages/web3-validator/src/validation/string.ts index b932898de9a..eb07993c301 100644 --- a/packages/web3-validator/src/validation/string.ts +++ b/packages/web3-validator/src/validation/string.ts @@ -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; diff --git a/packages/web3-validator/test/fixtures/validation.ts b/packages/web3-validator/test/fixtures/validation.ts index 2e7dd416ca0..39177e59e71 100644 --- a/packages/web3-validator/test/fixtures/validation.ts +++ b/packages/web3-validator/test/fixtures/validation.ts @@ -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', @@ -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, @@ -164,7 +171,7 @@ export const invalidHexStrictData: any[] = [ export const validHexData: any[] = [ ...validHexStrictData, '45', - '', + '-45', '0', 1, BigInt(12), diff --git a/packages/web3-validator/test/unit/utils.test.ts b/packages/web3-validator/test/unit/utils.test.ts index d25e8e803ea..e3b667f905e 100644 --- a/packages/web3-validator/test/unit/utils.test.ts +++ b/packages/web3-validator/test/unit/utils.test.ts @@ -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) => {