Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 09f4c8b

Browse files
fullkomnunAlexMuhammad-Altabba
authored
Fix validation uint int sizes (#6434)
* add ABI test cases with single numeric parameter that have a size that is not a power of 2 * fail-fast by throwing an error if during conversion of JSON shceme to Zod 'check' function is undefined (due to unsupported type) * make sure validators are defined for all valid sized of numeric types * prettify * updated changelog * include all newly generated single param test cases in validator test suite * add schemaError and unit test * update errors * format * test converage * address feedback --------- Co-authored-by: Alex <alex.luu@mail.utoronto.ca> Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
1 parent 226b3ba commit 09f4c8b

File tree

13 files changed

+130
-12
lines changed

13 files changed

+130
-12
lines changed

packages/web3-errors/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,6 @@ Documentation:
154154

155155
- Dependencies updated
156156

157-
## [Unreleased]
157+
## [Unreleased]
158+
159+
- Added new SchemaFormatError (#6434)

packages/web3-errors/src/error_codes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,14 @@ export const ERR_INVALID_NIBBLE_WIDTH = 1014;
158158
// Validation error codes
159159
export const ERR_VALIDATION = 1100;
160160

161+
161162
// Core error codes
162163
export const ERR_CORE_HARDFORK_MISMATCH = 1101;
163164
export const ERR_CORE_CHAIN_MISMATCH = 1102;
164165

166+
// Schema error codes
167+
export const ERR_SCHEMA_FORMAT = 1200;
168+
165169
// RPC error codes (EIP-1474)
166170
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md
167171
export const ERR_RPC_INVALID_JSON = -32700;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
import { ERR_SCHEMA_FORMAT } from '../error_codes.js';
19+
import { BaseWeb3Error } from '../web3_error_base.js';
20+
21+
export class SchemaFormatError extends BaseWeb3Error {
22+
public code = ERR_SCHEMA_FORMAT;
23+
24+
public constructor(public type: string) {
25+
super(`Format for the type ${type} is unsupported`);
26+
}
27+
28+
public toJSON() {
29+
return { ...super.toJSON(), type: this.type };
30+
}
31+
32+
}

packages/web3-errors/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ export * from './errors/response_errors.js';
3030
export * from './errors/core_errors.js';
3131
export * from './errors/rpc_errors.js';
3232
export * from './errors/rpc_error_messages.js';
33+
export * from './errors/schema_errors.js';

packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ Object {
333333
}
334334
`;
335335

336+
exports[`errors SchemaFormatError should have valid json structure 1`] = `
337+
Object {
338+
"code": 1200,
339+
"innerError": undefined,
340+
"message": "Format for the type unsupported is unsupported",
341+
"name": "SchemaFormatError",
342+
"type": "unsupported",
343+
}
344+
`;
345+
336346
exports[`errors TransactionError should have valid json structure 1`] = `
337347
Object {
338348
"code": 400,

packages/web3-errors/test/unit/errors.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as signatureErrors from '../../src/errors/signature_errors';
2626
import * as transactionErrors from '../../src/errors/transaction_errors';
2727
import * as utilsErrors from '../../src/errors/utils_errors';
2828
import * as responseErrors from '../../src/errors/response_errors';
29+
import * as schemaErrors from '../../src/errors/schema_errors';
2930

3031
import { ConvertValueToString } from '../fixtures/errors';
3132
import { BaseWeb3Error } from '../../src/web3_error_base';
@@ -50,6 +51,7 @@ describe('errors', () => {
5051
...signatureErrors,
5152
...transactionErrors,
5253
...utilsErrors,
54+
...schemaErrors,
5355
})) {
5456
if (ErrorClass === transactionErrors.InvalidPropertiesForTransactionTypeError) break;
5557
// To disable error for the abstract class
@@ -379,4 +381,13 @@ describe('errors', () => {
379381
).toMatchSnapshot();
380382
});
381383
});
384+
385+
describe('SchemaFormatError', () => {
386+
it('should have valid json structure', () => {
387+
expect(
388+
new schemaErrors.SchemaFormatError("unsupported"
389+
).toJSON(),
390+
).toMatchSnapshot();
391+
});
392+
});
382393
});

packages/web3-validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,5 @@ Documentation:
153153

154154
- Multi-dimensional arrays are now handled properly when parsing ABIs (#6435)
155155
- Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506)
156+
- Validator will now properly handle all valid numeric type sizes: intN / uintN where 8 <= N <= 256 and N % 8 == 0 (#6434)
157+
- Will now throw SchemaFormatError when unsupported format is passed to `convertToZod` method (#6434)

packages/web3-validator/src/formats.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ const formats: { [key: string]: (data: unknown) => boolean } = {
4141
string: (data: unknown) => isString(data as ValidInputTypes),
4242
};
4343
// generate formats for all numbers types
44-
for (let i = 3; i <= 8; i += 1) {
45-
const bitSize = 2 ** i;
44+
for (let bitSize = 8; bitSize <= 256; bitSize += 8) {
4645
formats[`int${bitSize}`] = data => isInt(data as ValidInputTypes, { bitSize });
4746
formats[`uint${bitSize}`] = data => isUInt(data as ValidInputTypes, { bitSize });
4847
}

packages/web3-validator/src/validator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GNU Lesser General Public License for more details.
1414
You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17+
import { SchemaFormatError } from 'web3-errors';
1718
import { Web3ValidationErrorObject } from 'web3-types';
1819

1920
import { z, ZodType, ZodIssue, ZodIssueCode, ZodTypeAny } from 'zod';
@@ -67,6 +68,10 @@ const convertToZod = (schema: JsonSchema): ZodType => {
6768
}
6869

6970
if (schema?.format) {
71+
if (!formats[schema.format]) {
72+
throw new SchemaFormatError(schema.format);
73+
}
74+
7075
return z.any().refine(formats[schema.format], (value: unknown) => ({
7176
params: { value, format: schema.format },
7277
}));

packages/web3-validator/test/config/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
},
1313
moduleNameMapper: {
1414
'^(\\.{1,2}/.*)\\.js$': '$1',
15-
},
15+
},
1616
verbose: false,
1717
collectCoverage: false,
1818
coverageReporters: ['json'],

0 commit comments

Comments
 (0)