Skip to content
Merged
Changes from 3 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
24 changes: 24 additions & 0 deletions src/lean_spec/types/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ class Uint16Vector2(SSZVector):
data: Tuple[SSZType, ...] = Field(default_factory=tuple)
"""The immutable data stored in the vector."""

@field_serializer("data", when_used="json")
def _serialize_vector_data(self, value: Tuple[SSZType, ...]) -> list[Any]:
"""Serialize vector elements to JSON, preserving custom type serialization."""
from lean_spec.subspecs.koalabear import Fp

result: list[Any] = []
for item in value:
# For BaseBytes subclasses, manually add 0x prefix
if isinstance(item, BaseBytes):
result.append("0x" + item.hex())
# For Fp field elements, extract the value attribute
elif isinstance(item, Fp):
result.append(item.value)
else:
# For other types (Uint, etc.), convert to int
# BaseUint inherits from int, so this cast is safe
result.append(item)
return result

@field_validator("data", mode="before")
@classmethod
def _validate_vector_data(cls, v: Any) -> Tuple[SSZType, ...]:
Expand Down Expand Up @@ -188,11 +207,16 @@ class Uint64List32(SSZList):
@field_serializer("data", when_used="json")
def _serialize_data(self, value: Tuple[SSZType, ...]) -> list[Any]:
"""Serialize list elements to JSON, preserving custom type serialization."""
from lean_spec.subspecs.koalabear import Fp

result: list[Any] = []
for item in value:
# For BaseBytes subclasses, manually add 0x prefix
if isinstance(item, BaseBytes):
result.append("0x" + item.hex())
# For Fp field elements, extract the value attribute
elif isinstance(item, Fp):
result.append(item.value)
else:
# For other types (Uint, etc.), convert to int
# BaseUint inherits from int, so this cast is safe
Expand Down
Loading