Skip to content

Commit

Permalink
Fix multi dimensional array validation issue (#6435)
Browse files Browse the repository at this point in the history
* add ABI test case for a multi-dimensional array

* fix ABI to JSON scheme conversion handling of multi-dimensional arrays

* update changelog

---------

Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
  • Loading branch information
fullkomnun and Muhammad-Altabba committed Sep 21, 2023
1 parent 2b445bf commit 66ddb7e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ Documentation:

- Dependencies updated

## [Unreleased]
## [Unreleased]

## Fixed

- Multi-dimensional arrays are now handled properly when parsing ABIs
12 changes: 11 additions & 1 deletion packages/web3-validator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,16 @@ export const abiSchemaToJsonSchema = (
delete childSchema.minItems;
}

lastSchema.items = childSchema;
// lastSchema.items is a Schema, concat with 'childSchema'
if (!Array.isArray(lastSchema.items)) {
lastSchema.items = [lastSchema.items as JsonSchema, childSchema];
} // lastSchema.items is an empty Scheme array, set it to 'childSchema'
else if (lastSchema.items.length === 0) {
lastSchema.items = childSchema;
} // lastSchema.items is a non-empty Scheme array, append 'childSchema'
else {
lastSchema.items.push(childSchema);
}
lastSchema = childSchema;
}

Expand Down Expand Up @@ -237,6 +246,7 @@ export const abiSchemaToJsonSchema = (
...convertEthType(abiType),
});
}
lastSchema = schema;
}

return schema;
Expand Down
122 changes: 122 additions & 0 deletions packages/web3-validator/test/fixtures/abi_to_json_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1536,4 +1536,126 @@ export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [
],
},
},
{
title: 'multi-dimensional array',
abi: {
fullSchema: [
{
name: 'x1',
type: 'uint256[][]',
},
{
name: 'x2',
type: 'uint256[][]',
},
{
name: 'x3',
type: 'uint256',
},
],
shortSchema: ['uint256[][]', 'uint256[][]', 'uint256'],
data: [
[
[1, 1],
[2, 2],
],
[
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
],
42,
],
},
json: {
fullSchema: {
type: 'array',
items: [
{
type: 'array',
items: [
{
type: 'array',
$id: 'x1',
items: {
format: 'uint',
required: true,
},
},
],
},
{
type: 'array',
items: [
{
type: 'array',
$id: 'x2',
items: {
format: 'uint',
required: true,
},
},
],
},
{
$id: 'x3',
format: 'uint256',
required: true,
},
],
maxItems: 3,
minItems: 3,
},
shortSchema: {
type: 'array',
items: [
{
type: 'array',
items: [
{
type: 'array',
$id: '/0/0',
items: {
format: 'uint',
required: true,
},
},
],
},
{
type: 'array',
items: [
{
type: 'array',
$id: '/0/1',
items: {
format: 'uint',
required: true,
},
},
],
},
{
$id: '/0/2',
format: 'uint256',
required: true,
},
],
maxItems: 3,
minItems: 3,
},
data: {
x1: [
[1, 1],
[2, 2],
],
x2: [
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
],
x3: 42,
},
},
},
];

0 comments on commit 66ddb7e

Please sign in to comment.