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 2d array bug of abiSchemaToJsonSchema in web3-validator #6836

Merged
merged 9 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ Documentation:

- Fixed an issue with detecting Uint8Array (#6486)

## [Unreleased]
## [Unreleased]

### Fixed

- Multi-dimensional arrays(with a fix length) are now handled properly when parsing ABIs (#6798)
68 changes: 37 additions & 31 deletions packages/web3-validator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,37 +205,43 @@ export const abiSchemaToJsonSchema = (
nestedTuple.$id = abiName;
(lastSchema.items as JsonSchema[]).push(nestedTuple);
} else if (baseType === 'tuple' && isArray) {
const arraySize = arraySizes[0];
const item: JsonSchema = {
$id: abiName,
type: 'array',
items: abiSchemaToJsonSchema(abiComponents, abiName),
maxItems: arraySize,
minItems: arraySize,
};

if (arraySize < 0) {
delete item.maxItems;
delete item.minItems;
}

(lastSchema.items as JsonSchema[]).push(item);
// iterate over arraySizes array to each dimension
for (let i = 0; i < arraySizes.length; i++) {
EtlesL marked this conversation as resolved.
Show resolved Hide resolved
const arraySize = arraySizes[i];
const item: JsonSchema = {
$id: abiName,
type: 'array',
items: abiSchemaToJsonSchema(abiComponents, abiName),
maxItems: arraySize,
minItems: arraySize,
};

if (arraySize < 0) {
delete item.maxItems;
delete item.minItems;
}

(lastSchema.items as JsonSchema[]).push(item);
}
} else if (isArray) {
const arraySize = arraySizes[0];
const item: JsonSchema = {
type: 'array',
$id: abiName,
items: convertEthType(String(baseType)),
minItems: arraySize,
maxItems: arraySize,
};

if (arraySize < 0) {
delete item.maxItems;
delete item.minItems;
}

(lastSchema.items as JsonSchema[]).push(item);
// iterate over arraySizes array to each dimension
for (let i = 0; i < arraySizes.length; i++) {
const arraySize = arraySizes[i];
const item: JsonSchema = {
type: 'array',
$id: abiName,
items: convertEthType(String(baseType)),
minItems: arraySize,
maxItems: arraySize,
};

if (arraySize < 0) {
delete item.maxItems;
delete item.minItems;
}

(lastSchema.items as JsonSchema[]).push(item);
}
} else if (Array.isArray(lastSchema.items)) {
// Array of non-tuple items
lastSchema.items.push({ $id: abiName, ...convertEthType(abiType) });
Expand Down Expand Up @@ -505,4 +511,4 @@ export function ensureIfUint8Array<T = any>(data: T) {
return Uint8Array.from(data as unknown as Uint8Array);
}
return data;
}
}
Loading