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

feat(BACK-7910): implement handling of nested fields #128

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 docs/pages/usage_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Descriptors can be manipulated in 2 forms:
- References have been inlined
- Constants have been inlined
- Field definitions have been inlined
- Nested fields have been flattened where possible
- Selectors have been converted to 4 bytes form
This form is the most adapted to be used by tools and applications.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
from erc7730.model.input.metadata import InputMetadata
from erc7730.model.metadata import EnumDefinition
from erc7730.model.paths import ROOT_DATA_PATH, ContainerPath, DataPath
from erc7730.model.paths import ROOT_DATA_PATH, Array, ArrayElement, ArraySlice, ContainerPath, DataPath, Field
from erc7730.model.paths.path_ops import data_or_container_path_concat, data_path_concat
from erc7730.model.resolved.context import (
ResolvedContract,
Expand Down Expand Up @@ -70,6 +70,7 @@ class ERC7730InputToResolved(ERC7730Converter[InputERC7730Descriptor, ResolvedER
- References have been inlined
- Constants have been inlined
- Field definitions have been inlined
- Nested fields have been flattened where possible
- Selectors have been converted to 4 bytes form
"""

Expand Down Expand Up @@ -385,7 +386,7 @@ def _resolve_fields(
for input_format in fields:
if (resolved_field := cls._resolve_field(prefix, input_format, definitions, enums, constants, out)) is None:
return None
resolved_fields.append(resolved_field)
resolved_fields.extend(resolved_field)
return resolved_fields

@classmethod
Expand All @@ -397,16 +398,28 @@ def _resolve_field(
enums: dict[Id, EnumDefinition],
constants: ConstantProvider,
out: OutputAdder,
) -> ResolvedField | None:
) -> list[ResolvedField] | None:
resolved_fields: list[ResolvedField] = []
match field:
case InputReference():
return resolve_reference(prefix, field, definitions, enums, constants, out)
if (resolved_field := resolve_reference(prefix, field, definitions, enums, constants, out)) is None:
return None
resolved_fields.append(resolved_field)
case InputFieldDescription():
return cls._resolve_field_description(prefix, field, enums, constants, out)
if (resolved_field := cls._resolve_field_description(prefix, field, enums, constants, out)) is None:
return None
resolved_fields.append(resolved_field)
case InputNestedFields():
return cls._resolve_nested_fields(prefix, field, definitions, enums, constants, out)
if (
resolved_nested_fields := cls._resolve_nested_fields(
prefix, field, definitions, enums, constants, out
)
) is None:
return None
resolved_fields.extend(resolved_nested_fields)
case _:
assert_never(field)
return resolved_fields

@classmethod
def _resolve_nested_fields(
Expand All @@ -417,7 +430,7 @@ def _resolve_nested_fields(
enums: dict[Id, EnumDefinition],
constants: ConstantProvider,
out: OutputAdder,
) -> ResolvedNestedFields | None:
) -> list[ResolvedNestedFields | ResolvedFieldDescription] | None:
path: DataPath
match constants.resolve_path(fields.path, out):
case None:
Expand All @@ -432,7 +445,22 @@ def _resolve_nested_fields(
case _:
assert_never(fields.path)

if (resolved_fields := cls._resolve_fields(path, fields.fields, definitions, enums, constants, out)) is None:
if (
resolved_fields := cls._resolve_fields(
prefix=path, fields=fields.fields, definitions=definitions, enums=enums, constants=constants, out=out
)
) is None:
return None

return ResolvedNestedFields(path=path, fields=resolved_fields)
match path.elements[-1]:
case Field() | ArrayElement():
return resolved_fields
case ArraySlice():
return out.error(
title="Invalid nested fields",
message="Using nested fields on an array slice is not allowed.",
)
case Array():
return [ResolvedNestedFields(path=path, fields=resolved_fields)]
case _:
assert_never(path.elements[-1])
1 change: 1 addition & 0 deletions src/erc7730/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def lint(
- References inlined
- Constants inlined
- Field definitions inlined
- Nested fields flattened where possible
- Selectors converted to 4 bytes form

See `erc7730 schema resolved` for the resolved descriptor schema.
Expand Down
1 change: 1 addition & 0 deletions src/erc7730/model/input/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- References have not been inlined
- Constants have not been inlined
- Field definitions have not been inlined
- Nested fields have been flattened where possible
- Selectors have been converted to 4 bytes form
"""

Expand Down
1 change: 1 addition & 0 deletions src/erc7730/model/resolved/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
- References have been inlined
- Constants have been inlined
- Field definitions have been inlined
- Nested fields have been flattened where possible
- Selectors have been converted to 4 bytes form
"""
2 changes: 2 additions & 0 deletions src/erc7730/model/resolved/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- References have been inlined
- Constants have been inlined
- Field definitions have been inlined
- Nested fields have been flattened where possible
- Selectors have been converted to 4 bytes form
"""

Expand All @@ -30,6 +31,7 @@ class ResolvedERC7730Descriptor(Model):
- References have been inlined
- Constants have been inlined
- Field definitions have been inlined
- Nested fields have been flattened where possible
- Selectors have been converted to 4 bytes form

Specification: https://github.com/LedgerHQ/clear-signing-erc7730-registry/tree/master/specs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"$schema": "../../../registries/clear-signing-erc7730-registry/specs/erc7730-v1.schema.json",
"context": {
"eip712": {
"deployments": [
{
"chainId": 1,
"address": "0x0000000000000000000000000000000000000000"
}
],
"schemas": [
{
"primaryType": "TestPrimaryType",
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"TestSubtype": [
{
"name": "subparam1",
"type": "string"
},
{
"name": "subparam2",
"type": "string"
}
],
"TestPrimaryType": [
{
"name": "param1",
"type": "TestSubtype[]"
}
]
}
}
]
}
},
"metadata": {},
"display": {
"formats": {
"TestPrimaryType": {
"fields": [
{
"path": "param1.[]",
"fields": [
{
"path": "subparam1",
"label": "Subparam 1",
"format": "raw"
},
{
"path": "subparam2",
"label": "Subparam 2",
"format": "raw"
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"context": {
"eip712": {
"deployments": [{ "chainId": 1, "address": "0x0000000000000000000000000000000000000000" }],
"schemas": [
{
"primaryType": "TestPrimaryType",
"types": {
"EIP712Domain": [
{ "name": "name", "type": "string" },
{ "name": "chainId", "type": "uint256" },
{ "name": "verifyingContract", "type": "address" }
],
"TestSubtype": [{ "name": "subparam1", "type": "string" }, { "name": "subparam2", "type": "string" }],
"TestPrimaryType": [{ "name": "param1", "type": "TestSubtype[]" }]
}
}
]
}
},
"metadata": { "enums": {} },
"display": {
"formats": {
"TestPrimaryType": {
"fields": [
{
"path": { "type": "data", "absolute": true, "elements": [{ "type": "field", "identifier": "param1" }, { "type": "array" }] },
"fields": [
{
"path": {
"type": "data",
"absolute": true,
"elements": [{ "type": "field", "identifier": "param1" }, { "type": "array" }, { "type": "field", "identifier": "subparam1" }]
},
"label": "Subparam 1",
"format": "raw"
},
{
"path": {
"type": "data",
"absolute": true,
"elements": [{ "type": "field", "identifier": "param1" }, { "type": "array" }, { "type": "field", "identifier": "subparam2" }]
},
"label": "Subparam 2",
"format": "raw"
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"$schema": "../../../registries/clear-signing-erc7730-registry/specs/erc7730-v1.schema.json",
"context": {
"eip712": {
"deployments": [
{
"chainId": 1,
"address": "0x0000000000000000000000000000000000000000"
}
],
"schemas": [
{
"primaryType": "TestPrimaryType",
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"TestSubtype": [
{
"name": "subparam1",
"type": "string"
},
{
"name": "subparam2",
"type": "string"
}
],
"TestPrimaryType": [
{
"name": "param1",
"type": "TestSubtype"
}
]
}
}
]
}
},
"metadata": {},
"display": {
"formats": {
"TestPrimaryType": {
"fields": [
{
"path": "param1",
"fields": [
{
"path": "subparam1",
"label": "Subparam 1",
"format": "raw"
},
{
"path": "subparam2",
"label": "Subparam 2",
"format": "raw"
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"context": {
"eip712": {
"deployments": [{ "chainId": 1, "address": "0x0000000000000000000000000000000000000000" }],
"schemas": [
{
"primaryType": "TestPrimaryType",
"types": {
"EIP712Domain": [
{ "name": "name", "type": "string" },
{ "name": "chainId", "type": "uint256" },
{ "name": "verifyingContract", "type": "address" }
],
"TestSubtype": [{ "name": "subparam1", "type": "string" }, { "name": "subparam2", "type": "string" }],
"TestPrimaryType": [{ "name": "param1", "type": "TestSubtype" }]
}
}
]
}
},
"metadata": { "enums": {} },
"display": {
"formats": {
"TestPrimaryType": {
"fields": [
{
"path": {
"type": "data",
"absolute": true,
"elements": [{ "type": "field", "identifier": "param1" }, { "type": "field", "identifier": "subparam1" }]
},
"label": "Subparam 1",
"format": "raw"
},
{
"path": {
"type": "data",
"absolute": true,
"elements": [{ "type": "field", "identifier": "param1" }, { "type": "field", "identifier": "subparam2" }]
},
"label": "Subparam 2",
"format": "raw"
}
]
}
}
}
}
Loading
Loading