|
| 1 | +"""Ethereum test types for serialization and encoding.""" |
| 2 | + |
| 3 | +from typing import Any, ClassVar, List |
| 4 | + |
| 5 | +import ethereum_rlp as eth_rlp |
| 6 | +from ethereum_types.numeric import Uint |
| 7 | + |
| 8 | +from ethereum_test_base_types import Bytes |
| 9 | + |
| 10 | + |
| 11 | +def to_serializable_element(v: Any) -> Any: |
| 12 | + """Return a serializable element that can be passed to `eth_rlp.encode`.""" |
| 13 | + if isinstance(v, int): |
| 14 | + return Uint(v) |
| 15 | + elif isinstance(v, bytes): |
| 16 | + return v |
| 17 | + elif isinstance(v, list): |
| 18 | + return [to_serializable_element(v) for v in v] |
| 19 | + elif isinstance(v, RLPSerializable): |
| 20 | + if v.signable: |
| 21 | + v.sign() |
| 22 | + return v.to_list(signing=False) |
| 23 | + elif v is None: |
| 24 | + return b"" |
| 25 | + raise Exception(f"Unable to serialize element {v} of type {type(v)}.") |
| 26 | + |
| 27 | + |
| 28 | +class RLPSerializable: |
| 29 | + """Class that adds RLP serialization to another class.""" |
| 30 | + |
| 31 | + rlp_override: Bytes | None = None |
| 32 | + |
| 33 | + signable: ClassVar[bool] = False |
| 34 | + rlp_fields: ClassVar[List[str]] |
| 35 | + rlp_signing_fields: ClassVar[List[str]] |
| 36 | + |
| 37 | + def get_rlp_fields(self) -> List[str]: |
| 38 | + """ |
| 39 | + Return an ordered list of field names to be included in RLP serialization. |
| 40 | +
|
| 41 | + Function can be overridden to customize the logic to return the fields. |
| 42 | +
|
| 43 | + By default, rlp_fields class variable is used. |
| 44 | +
|
| 45 | + The list can be nested list up to one extra level to represent nested fields. |
| 46 | + """ |
| 47 | + return self.rlp_fields |
| 48 | + |
| 49 | + def get_rlp_signing_fields(self) -> List[str]: |
| 50 | + """ |
| 51 | + Return an ordered list of field names to be included in the RLP serialization of the object |
| 52 | + signature. |
| 53 | +
|
| 54 | + Function can be overridden to customize the logic to return the fields. |
| 55 | +
|
| 56 | + By default, rlp_signing_fields class variable is used. |
| 57 | +
|
| 58 | + The list can be nested list up to one extra level to represent nested fields. |
| 59 | + """ |
| 60 | + return self.rlp_signing_fields |
| 61 | + |
| 62 | + def get_rlp_prefix(self) -> bytes: |
| 63 | + """ |
| 64 | + Return a prefix that has to be appended to the serialized object. |
| 65 | +
|
| 66 | + By default, an empty string is returned. |
| 67 | + """ |
| 68 | + return b"" |
| 69 | + |
| 70 | + def get_rlp_signing_prefix(self) -> bytes: |
| 71 | + """ |
| 72 | + Return a prefix that has to be appended to the serialized signing object. |
| 73 | +
|
| 74 | + By default, an empty string is returned. |
| 75 | + """ |
| 76 | + return b"" |
| 77 | + |
| 78 | + def sign(self): |
| 79 | + """Sign the current object for further serialization.""" |
| 80 | + raise NotImplementedError(f'Object "{self.__class__.__name__}" cannot be signed.') |
| 81 | + |
| 82 | + def to_list_from_fields(self, fields: List[str]) -> List[Any]: |
| 83 | + """ |
| 84 | + Return an RLP serializable list that can be passed to `eth_rlp.encode`. |
| 85 | +
|
| 86 | + Can be for signing purposes or the entire object. |
| 87 | + """ |
| 88 | + values_list: List[Any] = [] |
| 89 | + for field in fields: |
| 90 | + assert isinstance(field, str), ( |
| 91 | + f'Unable to rlp serialize field "{field}" ' |
| 92 | + f'in object type "{self.__class__.__name__}"' |
| 93 | + ) |
| 94 | + assert hasattr(self, field), ( |
| 95 | + f'Unable to rlp serialize field "{field}" ' |
| 96 | + f'in object type "{self.__class__.__name__}"' |
| 97 | + ) |
| 98 | + try: |
| 99 | + values_list.append(to_serializable_element(getattr(self, field))) |
| 100 | + except Exception as e: |
| 101 | + raise Exception( |
| 102 | + f'Unable to rlp serialize field "{field}" ' |
| 103 | + f'in object type "{self.__class__.__name__}"' |
| 104 | + ) from e |
| 105 | + return values_list |
| 106 | + |
| 107 | + def to_list(self, signing: bool = False) -> List[Any]: |
| 108 | + """ |
| 109 | + Return an RLP serializable list that can be passed to `eth_rlp.encode`. |
| 110 | +
|
| 111 | + Can be for signing purposes or the entire object. |
| 112 | + """ |
| 113 | + field_list: List[str] |
| 114 | + if signing: |
| 115 | + if not self.signable: |
| 116 | + raise Exception(f'Object "{self.__class__.__name__}" does not support signing') |
| 117 | + field_list = self.get_rlp_signing_fields() |
| 118 | + else: |
| 119 | + if self.signable: |
| 120 | + # Automatically sign signable objects during full serialization: |
| 121 | + # Ensures nested objects have valid signatures in the final RLP. |
| 122 | + self.sign() |
| 123 | + field_list = self.get_rlp_fields() |
| 124 | + |
| 125 | + return self.to_list_from_fields(field_list) |
| 126 | + |
| 127 | + def rlp_signing_bytes(self) -> Bytes: |
| 128 | + """Return the signing serialized envelope used for signing.""" |
| 129 | + return Bytes(self.get_rlp_signing_prefix() + eth_rlp.encode(self.to_list(signing=True))) |
| 130 | + |
| 131 | + def rlp(self) -> Bytes: |
| 132 | + """Return the serialized object.""" |
| 133 | + if self.rlp_override is not None: |
| 134 | + return self.rlp_override |
| 135 | + return Bytes(self.get_rlp_prefix() + eth_rlp.encode(self.to_list(signing=False))) |
| 136 | + |
| 137 | + |
| 138 | +class SignableRLPSerializable(RLPSerializable): |
| 139 | + """Class that adds RLP serialization to another class with signing support.""" |
| 140 | + |
| 141 | + signable: ClassVar[bool] = True |
| 142 | + |
| 143 | + def sign(self): |
| 144 | + """Sign the current object for further serialization.""" |
| 145 | + raise NotImplementedError(f'Object "{self.__class__.__name__}" needs to implement `sign`.') |
0 commit comments