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 validation uint int sizes #6434

Merged
merged 16 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,4 @@ Documentation:
## Fixed

- Multi-dimensional arrays are now handled properly when parsing ABIs
- Validator will now properly handle all valid numeric type sizes: intN / uintN where 8 <= N <= 256 and N % 8 == 0
3 changes: 1 addition & 2 deletions packages/web3-validator/src/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const formats: { [key: string]: (data: unknown) => boolean } = {
string: (data: unknown) => isString(data as ValidInputTypes),
};
// generate formats for all numbers types
for (let i = 3; i <= 8; i += 1) {
const bitSize = 2 ** i;
for (let bitSize = 8; bitSize <= 256; bitSize += 8) {
formats[`int${bitSize}`] = data => isInt(data as ValidInputTypes, { bitSize });
formats[`uint${bitSize}`] = data => isUInt(data as ValidInputTypes, { bitSize });
}
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
}

if (schema?.format) {
if (!formats[schema.format]) {
throw new Error(`format ${schema.format} is unsupported`);

Check warning on line 71 in packages/web3-validator/src/validator.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-validator/src/validator.ts#L71

Added line #L71 was not covered by tests
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
}

return z.any().refine(formats[schema.format], (value: unknown) => ({
params: { value, format: schema.format },
}));
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-validator/test/config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
},
verbose: false,
collectCoverage: false,
coverageReporters: ['json'],
Expand Down
35 changes: 34 additions & 1 deletion packages/web3-validator/test/fixtures/abi_to_json_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type AbiToJsonSchemaCase = {
data: Record<string, unknown> | Array<unknown>;
};
};
export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [
const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [
{
title: 'single param uint',
abi: {
Expand Down Expand Up @@ -1659,3 +1659,36 @@ export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [
},
},
];

function generateSingleParamNumericCase(type: string, bitSize: number) {
return {
title: `single param ${type}${bitSize}`,
abi: {
fullSchema: [{ name: 'a', type: `${type}${bitSize}` }],
shortSchema: [`${type}${bitSize}`],
data: [12],
},
json: {
fullSchema: {
type: 'array',
items: [{ $id: 'a', format: `${type}${bitSize}`, required: true }],
minItems: 1,
maxItems: 1,
},
shortSchema: {
type: 'array',
items: [{ $id: '/0/0', format: `${type}${bitSize}`, required: true }],
minItems: 1,
maxItems: 1,
},
data: [12],
},
};
}

for (let i = 256; i >= 8; i -= 8) {
abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('int', i));
abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('uint', i));
}

export { abiToJsonSchemaCases };
16 changes: 10 additions & 6 deletions packages/web3-validator/test/unit/web3_validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,16 @@ describe('web3-validator', () => {
});
});
describe('validateJsonSchema', () => {
it.each(abiToJsonSchemaCases.slice(0, 5))('should pass for valid data', abi => {
const jsonSchema = abi.json;
expect(
validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data),
).toBeUndefined();
});
// only single param test cases
it.each(abiToJsonSchemaCases.slice(0, 69))(
`$title - should pass for valid data`,
abi => {
const jsonSchema = abi.json;
expect(
validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data),
).toBeUndefined();
},
);

it('should throw', () => {
expect(() => {
Expand Down
Loading