faster-eth-abi 5.2.25__cp314-cp314-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- faster_eth_abi/__init__.py +17 -0
- faster_eth_abi/_codec.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/_codec.py +83 -0
- faster_eth_abi/_decoding.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/_decoding.py +371 -0
- faster_eth_abi/_encoding.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/_encoding.py +514 -0
- faster_eth_abi/_grammar.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/_grammar.py +389 -0
- faster_eth_abi/abi.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/abi.py +17 -0
- faster_eth_abi/base.py +45 -0
- faster_eth_abi/codec.py +2809 -0
- faster_eth_abi/constants.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/constants.py +7 -0
- faster_eth_abi/decoding.py +555 -0
- faster_eth_abi/encoding.py +738 -0
- faster_eth_abi/exceptions.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/exceptions.py +127 -0
- faster_eth_abi/from_type_str.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/from_type_str.py +141 -0
- faster_eth_abi/grammar.py +179 -0
- faster_eth_abi/io.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/io.py +137 -0
- faster_eth_abi/packed.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/packed.py +19 -0
- faster_eth_abi/py.typed +0 -0
- faster_eth_abi/registry.py +758 -0
- faster_eth_abi/tools/__init__.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/tools/__init__.py +3 -0
- faster_eth_abi/tools/_strategies.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/tools/_strategies.py +247 -0
- faster_eth_abi/typing.py +4627 -0
- faster_eth_abi/utils/__init__.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/utils/__init__.py +0 -0
- faster_eth_abi/utils/localcontext.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/utils/localcontext.py +49 -0
- faster_eth_abi/utils/numeric.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/utils/numeric.py +117 -0
- faster_eth_abi/utils/padding.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/utils/padding.py +22 -0
- faster_eth_abi/utils/string.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/utils/string.py +19 -0
- faster_eth_abi/utils/validation.cpython-314-i386-linux-gnu.so +0 -0
- faster_eth_abi/utils/validation.py +18 -0
- faster_eth_abi-5.2.25.dist-info/METADATA +134 -0
- faster_eth_abi-5.2.25.dist-info/RECORD +50 -0
- faster_eth_abi-5.2.25.dist-info/WHEEL +7 -0
- faster_eth_abi-5.2.25.dist-info/top_level.txt +2 -0
- faster_eth_abi__mypyc.cpython-314-i386-linux-gnu.so +0 -0
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
"""Classes for ABI encoding logic.
|
|
2
|
+
|
|
3
|
+
Implements classes and functions for serializing Python values into binary data
|
|
4
|
+
according to ABI type specifications.
|
|
5
|
+
"""
|
|
6
|
+
import abc
|
|
7
|
+
from decimal import (
|
|
8
|
+
Decimal,
|
|
9
|
+
)
|
|
10
|
+
from functools import (
|
|
11
|
+
cached_property,
|
|
12
|
+
lru_cache,
|
|
13
|
+
)
|
|
14
|
+
from numbers import (
|
|
15
|
+
Number,
|
|
16
|
+
)
|
|
17
|
+
from types import (
|
|
18
|
+
MethodType,
|
|
19
|
+
)
|
|
20
|
+
from typing import (
|
|
21
|
+
Any,
|
|
22
|
+
Callable,
|
|
23
|
+
ClassVar,
|
|
24
|
+
Final,
|
|
25
|
+
NoReturn,
|
|
26
|
+
Optional,
|
|
27
|
+
Sequence,
|
|
28
|
+
Tuple,
|
|
29
|
+
Type,
|
|
30
|
+
final,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from faster_eth_utils import (
|
|
34
|
+
is_address,
|
|
35
|
+
is_boolean,
|
|
36
|
+
is_bytes,
|
|
37
|
+
is_integer,
|
|
38
|
+
is_number,
|
|
39
|
+
is_text,
|
|
40
|
+
to_canonical_address,
|
|
41
|
+
)
|
|
42
|
+
from typing_extensions import (
|
|
43
|
+
Self,
|
|
44
|
+
TypeGuard,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
from faster_eth_abi._encoding import (
|
|
48
|
+
encode_bytestring,
|
|
49
|
+
encode_elements,
|
|
50
|
+
encode_elements_dynamic,
|
|
51
|
+
encode_fixed_bigendian,
|
|
52
|
+
encode_fixed_smallendian,
|
|
53
|
+
encode_signed,
|
|
54
|
+
encode_signed_fixed,
|
|
55
|
+
encode_text,
|
|
56
|
+
encode_tuple,
|
|
57
|
+
encode_tuple_all_dynamic,
|
|
58
|
+
encode_tuple_no_dynamic,
|
|
59
|
+
encode_tuple_no_dynamic_funcs,
|
|
60
|
+
encode_unsigned_fixed,
|
|
61
|
+
int_to_big_endian,
|
|
62
|
+
validate_array,
|
|
63
|
+
validate_fixed,
|
|
64
|
+
validate_packed_array,
|
|
65
|
+
validate_sized_array,
|
|
66
|
+
validate_tuple,
|
|
67
|
+
)
|
|
68
|
+
from faster_eth_abi.base import (
|
|
69
|
+
BaseCoder,
|
|
70
|
+
)
|
|
71
|
+
from faster_eth_abi.exceptions import (
|
|
72
|
+
EncodingTypeError,
|
|
73
|
+
IllegalValue,
|
|
74
|
+
ValueOutOfBounds,
|
|
75
|
+
)
|
|
76
|
+
from faster_eth_abi.from_type_str import (
|
|
77
|
+
parse_tuple_type_str,
|
|
78
|
+
parse_type_str,
|
|
79
|
+
)
|
|
80
|
+
from faster_eth_abi.utils.numeric import (
|
|
81
|
+
TEN,
|
|
82
|
+
compute_signed_fixed_bounds,
|
|
83
|
+
compute_signed_integer_bounds,
|
|
84
|
+
compute_unsigned_fixed_bounds,
|
|
85
|
+
compute_unsigned_integer_bounds,
|
|
86
|
+
)
|
|
87
|
+
from faster_eth_abi.utils.string import (
|
|
88
|
+
abbr,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class BaseEncoder(BaseCoder, metaclass=abc.ABCMeta):
|
|
93
|
+
"""
|
|
94
|
+
Base class for all encoder classes. Subclass this if you want to define a
|
|
95
|
+
custom encoder class. Subclasses must also implement
|
|
96
|
+
:any:`BaseCoder.from_type_str`.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
@abc.abstractmethod
|
|
100
|
+
def encode(self, value: Any) -> bytes: # pragma: no cover
|
|
101
|
+
"""
|
|
102
|
+
Encodes the given value as a sequence of bytes. Should raise
|
|
103
|
+
:any:`exceptions.EncodingError` if ``value`` cannot be encoded.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
@abc.abstractmethod
|
|
107
|
+
def validate_value(self, value: Any) -> None:
|
|
108
|
+
"""
|
|
109
|
+
Checks whether or not the given value can be encoded by this encoder.
|
|
110
|
+
If the given value cannot be encoded, must raise
|
|
111
|
+
:any:`exceptions.EncodingError`.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
@classmethod
|
|
115
|
+
def invalidate_value(
|
|
116
|
+
cls,
|
|
117
|
+
value: Any,
|
|
118
|
+
exc: Type[Exception] = EncodingTypeError,
|
|
119
|
+
msg: Optional[str] = None,
|
|
120
|
+
) -> NoReturn:
|
|
121
|
+
"""
|
|
122
|
+
Throws a standard exception for when a value is not encodable by an
|
|
123
|
+
encoder.
|
|
124
|
+
"""
|
|
125
|
+
raise exc(
|
|
126
|
+
f"Value `{abbr(value)}` of type {type(value)} cannot be encoded by "
|
|
127
|
+
f"{cls.__name__}{'' if msg is None else (': ' + msg)}"
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
def __call__(self, value: Any) -> bytes:
|
|
131
|
+
return self.encode(value)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class TupleEncoder(BaseEncoder):
|
|
135
|
+
encoders: Tuple[BaseEncoder, ...] = ()
|
|
136
|
+
|
|
137
|
+
def __init__(self, encoders: Tuple[BaseEncoder, ...], **kwargs: Any) -> None:
|
|
138
|
+
super().__init__(encoders=encoders, **kwargs)
|
|
139
|
+
|
|
140
|
+
self._is_dynamic: Final = tuple(
|
|
141
|
+
getattr(e, "is_dynamic", False) for e in self.encoders
|
|
142
|
+
)
|
|
143
|
+
self.is_dynamic = any(self._is_dynamic)
|
|
144
|
+
|
|
145
|
+
validators = []
|
|
146
|
+
for encoder in self.encoders:
|
|
147
|
+
try:
|
|
148
|
+
validator = encoder.validate_value
|
|
149
|
+
except AttributeError:
|
|
150
|
+
validators.append(encoder)
|
|
151
|
+
else:
|
|
152
|
+
validators.append(validator)
|
|
153
|
+
|
|
154
|
+
self.validators: Final[Callable[[Any], None]] = tuple(validators)
|
|
155
|
+
|
|
156
|
+
if type(self).encode is TupleEncoder.encode:
|
|
157
|
+
encode_func = (
|
|
158
|
+
encode_tuple_all_dynamic
|
|
159
|
+
if self.encoders and all(self._is_dynamic)
|
|
160
|
+
else encode_tuple_no_dynamic_funcs.get(
|
|
161
|
+
len(self.encoders),
|
|
162
|
+
encode_tuple_no_dynamic,
|
|
163
|
+
)
|
|
164
|
+
if not self.is_dynamic
|
|
165
|
+
else encode_tuple
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
self.encode = MethodType(encode_func, self)
|
|
169
|
+
|
|
170
|
+
def validate(self) -> None:
|
|
171
|
+
super().validate()
|
|
172
|
+
|
|
173
|
+
if self.encoders is None:
|
|
174
|
+
raise ValueError("`encoders` may not be none")
|
|
175
|
+
|
|
176
|
+
@final
|
|
177
|
+
def validate_value(self, value: Sequence[Any]) -> None:
|
|
178
|
+
validate_tuple(self, value)
|
|
179
|
+
|
|
180
|
+
def encode(self, values: Sequence[Any]) -> bytes:
|
|
181
|
+
return encode_tuple(self, values)
|
|
182
|
+
|
|
183
|
+
def __call__(self, values: Sequence[Any]) -> bytes:
|
|
184
|
+
return self.encode(values)
|
|
185
|
+
|
|
186
|
+
@parse_tuple_type_str
|
|
187
|
+
def from_type_str(cls, abi_type, registry):
|
|
188
|
+
encoders = tuple(
|
|
189
|
+
registry.get_encoder(c.to_type_str()) for c in abi_type.components
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
return cls(encoders=encoders)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class FixedSizeEncoder(BaseEncoder):
|
|
196
|
+
value_bit_size: int = None # type: ignore [assignment]
|
|
197
|
+
data_byte_size: int = None # type: ignore [assignment]
|
|
198
|
+
encode_fn: Callable[..., Any] = None # type: ignore [assignment]
|
|
199
|
+
type_check_fn: Callable[..., bool] = None # type: ignore [assignment]
|
|
200
|
+
is_big_endian: bool = None # type: ignore [assignment]
|
|
201
|
+
|
|
202
|
+
def validate(self) -> None:
|
|
203
|
+
super().validate()
|
|
204
|
+
|
|
205
|
+
value_bit_size = self.value_bit_size
|
|
206
|
+
if value_bit_size is None:
|
|
207
|
+
raise ValueError("`value_bit_size` may not be none")
|
|
208
|
+
data_byte_size = self.data_byte_size
|
|
209
|
+
if data_byte_size is None:
|
|
210
|
+
raise ValueError("`data_byte_size` may not be none")
|
|
211
|
+
if self.encode_fn is None:
|
|
212
|
+
raise ValueError("`encode_fn` may not be none")
|
|
213
|
+
if self.is_big_endian is None:
|
|
214
|
+
raise ValueError("`is_big_endian` may not be none")
|
|
215
|
+
|
|
216
|
+
if value_bit_size % 8 != 0:
|
|
217
|
+
raise ValueError(
|
|
218
|
+
f"Invalid value bit size: {value_bit_size}. Must be a multiple of 8"
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
if value_bit_size > data_byte_size * 8:
|
|
222
|
+
raise ValueError("Value byte size exceeds data size")
|
|
223
|
+
|
|
224
|
+
def validate_value(self, value: Any) -> None:
|
|
225
|
+
raise NotImplementedError("Must be implemented by subclasses")
|
|
226
|
+
|
|
227
|
+
def encode(self, value: Any) -> bytes:
|
|
228
|
+
self.validate_value(value)
|
|
229
|
+
encode_fn = self.encode_fn
|
|
230
|
+
if encode_fn is None:
|
|
231
|
+
raise AssertionError("`encode_fn` is None")
|
|
232
|
+
if self.is_big_endian:
|
|
233
|
+
return encode_fixed_bigendian(value, encode_fn, self.data_byte_size)
|
|
234
|
+
return encode_fixed_smallendian(value, encode_fn, self.data_byte_size)
|
|
235
|
+
|
|
236
|
+
__call__ = encode
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class Fixed32ByteSizeEncoder(FixedSizeEncoder):
|
|
240
|
+
data_byte_size = 32
|
|
241
|
+
|
|
242
|
+
def encode(self, value: Any) -> bytes:
|
|
243
|
+
self.validate_value(value)
|
|
244
|
+
encode_fn = self.encode_fn
|
|
245
|
+
if encode_fn is None:
|
|
246
|
+
raise AssertionError("`encode_fn` is None")
|
|
247
|
+
return encode_fixed_bigendian(value, encode_fn, self.data_byte_size)
|
|
248
|
+
|
|
249
|
+
__call__ = encode
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
class BooleanEncoder(Fixed32ByteSizeEncoder):
|
|
253
|
+
value_bit_size = 8
|
|
254
|
+
is_big_endian = True
|
|
255
|
+
|
|
256
|
+
@classmethod
|
|
257
|
+
def validate_value(cls, value: Any) -> None:
|
|
258
|
+
if not is_boolean(value):
|
|
259
|
+
cls.invalidate_value(value)
|
|
260
|
+
|
|
261
|
+
@classmethod
|
|
262
|
+
def encode_fn(cls, value: bool) -> bytes:
|
|
263
|
+
if value is True:
|
|
264
|
+
return b"\x01"
|
|
265
|
+
elif value is False:
|
|
266
|
+
return b"\x00"
|
|
267
|
+
else:
|
|
268
|
+
raise ValueError("Invariant")
|
|
269
|
+
|
|
270
|
+
@parse_type_str("bool")
|
|
271
|
+
def from_type_str(cls, abi_type, registry):
|
|
272
|
+
return cls()
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
class PackedBooleanEncoder(BooleanEncoder):
|
|
276
|
+
data_byte_size = 1
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class NumberEncoder(Fixed32ByteSizeEncoder):
|
|
280
|
+
is_big_endian = True
|
|
281
|
+
bounds_fn: Callable[[int], Tuple[Number, Number]] = None # type: ignore [assignment]
|
|
282
|
+
illegal_value_fn: Callable[[Any], bool] = None # type: ignore [assignment]
|
|
283
|
+
type_check_fn: Callable[[Any], bool] = None # type: ignore [assignment]
|
|
284
|
+
|
|
285
|
+
@cached_property
|
|
286
|
+
def bounds(self) -> Tuple[Number, Number]:
|
|
287
|
+
return self.bounds_fn(self.value_bit_size)
|
|
288
|
+
|
|
289
|
+
@cached_property
|
|
290
|
+
def lower_bound(self) -> Number:
|
|
291
|
+
return self.bounds[0]
|
|
292
|
+
|
|
293
|
+
@cached_property
|
|
294
|
+
def upper_bound(self) -> Number:
|
|
295
|
+
return self.bounds[1]
|
|
296
|
+
|
|
297
|
+
def validate(self) -> None:
|
|
298
|
+
super().validate()
|
|
299
|
+
|
|
300
|
+
if self.bounds_fn is None:
|
|
301
|
+
raise ValueError("`bounds_fn` cannot be null")
|
|
302
|
+
if self.type_check_fn is None:
|
|
303
|
+
raise ValueError("`type_check_fn` cannot be null")
|
|
304
|
+
|
|
305
|
+
def validate_value(self, value: Any) -> None:
|
|
306
|
+
type_check_fn = self.type_check_fn
|
|
307
|
+
if type_check_fn is None:
|
|
308
|
+
raise AssertionError("`type_check_fn` is None")
|
|
309
|
+
if not type_check_fn(value):
|
|
310
|
+
self.invalidate_value(value)
|
|
311
|
+
|
|
312
|
+
illegal_value_fn = self.illegal_value_fn
|
|
313
|
+
illegal_value = illegal_value_fn is not None and illegal_value_fn(value)
|
|
314
|
+
if illegal_value:
|
|
315
|
+
self.invalidate_value(value, exc=IllegalValue)
|
|
316
|
+
|
|
317
|
+
if value < self.lower_bound or value > self.upper_bound:
|
|
318
|
+
self.invalidate_value(
|
|
319
|
+
value,
|
|
320
|
+
exc=ValueOutOfBounds,
|
|
321
|
+
msg=f"Cannot be encoded in {self.value_bit_size} bits. Must be bounded "
|
|
322
|
+
f"between [{self.lower_bound}, {self.upper_bound}].",
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
class UnsignedIntegerEncoder(NumberEncoder):
|
|
327
|
+
encode_fn = staticmethod(int_to_big_endian)
|
|
328
|
+
bounds_fn = staticmethod(compute_unsigned_integer_bounds)
|
|
329
|
+
type_check_fn = staticmethod(is_integer)
|
|
330
|
+
|
|
331
|
+
@parse_type_str("uint")
|
|
332
|
+
def from_type_str(cls, abi_type, registry):
|
|
333
|
+
return cls(value_bit_size=abi_type.sub)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
class UnsignedIntegerEncoderCached(UnsignedIntegerEncoder):
|
|
337
|
+
encode: Final[Callable[[int], bytes]]
|
|
338
|
+
maxsize: Final[Optional[int]]
|
|
339
|
+
|
|
340
|
+
def __init__(self, maxsize: Optional[int] = None, **kwargs: Any) -> None:
|
|
341
|
+
super().__init__(**kwargs)
|
|
342
|
+
self.maxsize = maxsize
|
|
343
|
+
self.encode = lru_cache(maxsize=maxsize)(self.encode)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
encode_uint_256 = UnsignedIntegerEncoder(value_bit_size=256, data_byte_size=32)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
class PackedUnsignedIntegerEncoder(UnsignedIntegerEncoder):
|
|
350
|
+
@parse_type_str("uint")
|
|
351
|
+
def from_type_str(cls, abi_type, registry):
|
|
352
|
+
abi_subtype = abi_type.sub
|
|
353
|
+
return cls(
|
|
354
|
+
value_bit_size=abi_subtype,
|
|
355
|
+
data_byte_size=abi_subtype // 8,
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
class PackedUnsignedIntegerEncoderCached(PackedUnsignedIntegerEncoder):
|
|
360
|
+
encode: Final[Callable[[int], bytes]]
|
|
361
|
+
maxsize: Final[Optional[int]]
|
|
362
|
+
|
|
363
|
+
def __init__(self, maxsize: Optional[int] = None, **kwargs: Any) -> None:
|
|
364
|
+
super().__init__(**kwargs)
|
|
365
|
+
self.maxsize = maxsize
|
|
366
|
+
self.encode = lru_cache(maxsize=maxsize)(self.encode)
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
class SignedIntegerEncoder(NumberEncoder):
|
|
370
|
+
bounds_fn = staticmethod(compute_signed_integer_bounds)
|
|
371
|
+
type_check_fn = staticmethod(is_integer)
|
|
372
|
+
|
|
373
|
+
@cached_property
|
|
374
|
+
def modulus(self) -> int:
|
|
375
|
+
return 2**self.value_bit_size
|
|
376
|
+
|
|
377
|
+
def encode_fn(self, value: int) -> bytes:
|
|
378
|
+
return int_to_big_endian(value % self.modulus)
|
|
379
|
+
|
|
380
|
+
def encode(self, value: int) -> bytes:
|
|
381
|
+
self.validate_value(value)
|
|
382
|
+
return encode_signed(value, self.encode_fn, self.data_byte_size)
|
|
383
|
+
|
|
384
|
+
__call__ = encode
|
|
385
|
+
|
|
386
|
+
@parse_type_str("int")
|
|
387
|
+
def from_type_str(cls, abi_type, registry):
|
|
388
|
+
return cls(value_bit_size=abi_type.sub)
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
class SignedIntegerEncoderCached(SignedIntegerEncoder):
|
|
392
|
+
encode: Final[Callable[[int], bytes]]
|
|
393
|
+
maxsize: Final[Optional[int]]
|
|
394
|
+
|
|
395
|
+
def __init__(self, maxsize: Optional[int] = None, **kwargs: Any) -> None:
|
|
396
|
+
super().__init__(**kwargs)
|
|
397
|
+
self.maxsize = maxsize
|
|
398
|
+
self.encode = lru_cache(maxsize=maxsize)(self.encode)
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
class PackedSignedIntegerEncoder(SignedIntegerEncoder):
|
|
402
|
+
@parse_type_str("int")
|
|
403
|
+
def from_type_str(cls, abi_type, registry):
|
|
404
|
+
return cls(
|
|
405
|
+
value_bit_size=abi_type.sub,
|
|
406
|
+
data_byte_size=abi_type.sub // 8,
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
class PackedSignedIntegerEncoderCached(PackedSignedIntegerEncoder):
|
|
411
|
+
encode: Final[Callable[[int], bytes]]
|
|
412
|
+
maxsize: Final[Optional[int]]
|
|
413
|
+
|
|
414
|
+
def __init__(self, maxsize: Optional[int] = None, **kwargs: Any) -> None:
|
|
415
|
+
super().__init__(**kwargs)
|
|
416
|
+
self.maxsize = maxsize
|
|
417
|
+
self.encode = lru_cache(maxsize=maxsize)(self.encode)
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
class BaseFixedEncoder(NumberEncoder):
|
|
421
|
+
frac_places: int = None # type: ignore [assignment]
|
|
422
|
+
|
|
423
|
+
@staticmethod
|
|
424
|
+
def type_check_fn(value: Any) -> TypeGuard[Number]:
|
|
425
|
+
return is_number(value) and not isinstance(value, float)
|
|
426
|
+
|
|
427
|
+
@staticmethod
|
|
428
|
+
def illegal_value_fn(value: Number) -> bool:
|
|
429
|
+
return isinstance(value, Decimal) and (value.is_nan() or value.is_infinite())
|
|
430
|
+
|
|
431
|
+
@cached_property
|
|
432
|
+
def denominator(self) -> Decimal:
|
|
433
|
+
return TEN**self.frac_places
|
|
434
|
+
|
|
435
|
+
@cached_property
|
|
436
|
+
def precision(self) -> Decimal:
|
|
437
|
+
return TEN**-self.frac_places
|
|
438
|
+
|
|
439
|
+
def validate_value(self, value):
|
|
440
|
+
super().validate_value(value)
|
|
441
|
+
validate_fixed(self, value)
|
|
442
|
+
|
|
443
|
+
def validate(self) -> None:
|
|
444
|
+
super().validate()
|
|
445
|
+
|
|
446
|
+
frac_places = self.frac_places
|
|
447
|
+
if frac_places is None:
|
|
448
|
+
raise ValueError("must specify `frac_places`")
|
|
449
|
+
|
|
450
|
+
if frac_places <= 0 or frac_places > 80:
|
|
451
|
+
raise ValueError("`frac_places` must be in range (0, 80]")
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
class UnsignedFixedEncoder(BaseFixedEncoder):
|
|
455
|
+
def bounds_fn(self, value_bit_size):
|
|
456
|
+
return compute_unsigned_fixed_bounds(self.value_bit_size, self.frac_places)
|
|
457
|
+
|
|
458
|
+
def encode_fn(self, value: Decimal) -> bytes:
|
|
459
|
+
return encode_unsigned_fixed(self, value)
|
|
460
|
+
|
|
461
|
+
@parse_type_str("ufixed")
|
|
462
|
+
def from_type_str(cls, abi_type, registry):
|
|
463
|
+
value_bit_size, frac_places = abi_type.sub
|
|
464
|
+
|
|
465
|
+
return cls(
|
|
466
|
+
value_bit_size=value_bit_size,
|
|
467
|
+
frac_places=frac_places,
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
class PackedUnsignedFixedEncoder(UnsignedFixedEncoder):
|
|
472
|
+
@parse_type_str("ufixed")
|
|
473
|
+
def from_type_str(cls, abi_type, registry):
|
|
474
|
+
value_bit_size, frac_places = abi_type.sub
|
|
475
|
+
|
|
476
|
+
return cls(
|
|
477
|
+
value_bit_size=value_bit_size,
|
|
478
|
+
data_byte_size=value_bit_size // 8,
|
|
479
|
+
frac_places=frac_places,
|
|
480
|
+
)
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
class SignedFixedEncoder(BaseFixedEncoder):
|
|
484
|
+
def bounds_fn(self, value_bit_size):
|
|
485
|
+
return compute_signed_fixed_bounds(self.value_bit_size, self.frac_places)
|
|
486
|
+
|
|
487
|
+
@cached_property
|
|
488
|
+
def modulus(self) -> int:
|
|
489
|
+
return 2**self.value_bit_size
|
|
490
|
+
|
|
491
|
+
def encode_fn(self, value: Decimal) -> bytes:
|
|
492
|
+
return encode_signed_fixed(self, value)
|
|
493
|
+
|
|
494
|
+
def encode(self, value: Decimal) -> bytes:
|
|
495
|
+
self.validate_value(value)
|
|
496
|
+
return encode_signed(value, self.encode_fn, self.data_byte_size)
|
|
497
|
+
|
|
498
|
+
__call__ = encode
|
|
499
|
+
|
|
500
|
+
@parse_type_str("fixed")
|
|
501
|
+
def from_type_str(cls, abi_type, registry):
|
|
502
|
+
value_bit_size, frac_places = abi_type.sub
|
|
503
|
+
|
|
504
|
+
return cls(
|
|
505
|
+
value_bit_size=value_bit_size,
|
|
506
|
+
frac_places=frac_places,
|
|
507
|
+
)
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
class PackedSignedFixedEncoder(SignedFixedEncoder):
|
|
511
|
+
@parse_type_str("fixed")
|
|
512
|
+
def from_type_str(cls, abi_type, registry):
|
|
513
|
+
value_bit_size, frac_places = abi_type.sub
|
|
514
|
+
|
|
515
|
+
return cls(
|
|
516
|
+
value_bit_size=value_bit_size,
|
|
517
|
+
data_byte_size=value_bit_size // 8,
|
|
518
|
+
frac_places=frac_places,
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
class AddressEncoder(Fixed32ByteSizeEncoder):
|
|
523
|
+
value_bit_size = 20 * 8
|
|
524
|
+
encode_fn = staticmethod(to_canonical_address)
|
|
525
|
+
is_big_endian = True
|
|
526
|
+
|
|
527
|
+
@classmethod
|
|
528
|
+
def validate_value(cls, value: Any) -> None:
|
|
529
|
+
if not is_address(value):
|
|
530
|
+
cls.invalidate_value(value)
|
|
531
|
+
|
|
532
|
+
def validate(self) -> None:
|
|
533
|
+
super().validate()
|
|
534
|
+
|
|
535
|
+
if self.value_bit_size != 20 * 8:
|
|
536
|
+
raise ValueError("Addresses must be 160 bits in length")
|
|
537
|
+
|
|
538
|
+
@parse_type_str("address")
|
|
539
|
+
def from_type_str(cls, abi_type, registry):
|
|
540
|
+
return cls()
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
class PackedAddressEncoder(AddressEncoder):
|
|
544
|
+
data_byte_size = 20
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
class BytesEncoder(Fixed32ByteSizeEncoder):
|
|
548
|
+
is_big_endian = False
|
|
549
|
+
|
|
550
|
+
def encode(self, value: Any) -> bytes:
|
|
551
|
+
self.validate_value(value)
|
|
552
|
+
encode_fn = self.encode_fn
|
|
553
|
+
if encode_fn is None:
|
|
554
|
+
raise AssertionError("`encode_fn` is None")
|
|
555
|
+
return encode_fixed_smallendian(value, encode_fn, self.data_byte_size)
|
|
556
|
+
|
|
557
|
+
__call__ = encode
|
|
558
|
+
|
|
559
|
+
@cached_property
|
|
560
|
+
def value_byte_size(self) -> int:
|
|
561
|
+
return self.value_bit_size // 8
|
|
562
|
+
|
|
563
|
+
def validate_value(self, value: Any) -> None:
|
|
564
|
+
if not is_bytes(value):
|
|
565
|
+
self.invalidate_value(value)
|
|
566
|
+
|
|
567
|
+
if len(value) > self.value_byte_size:
|
|
568
|
+
self.invalidate_value(
|
|
569
|
+
value,
|
|
570
|
+
exc=ValueOutOfBounds,
|
|
571
|
+
msg=f"exceeds total byte size for bytes{self.value_byte_size} encoding",
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
@staticmethod
|
|
575
|
+
def encode_fn(value: bytes) -> bytes:
|
|
576
|
+
return value
|
|
577
|
+
|
|
578
|
+
@parse_type_str("bytes")
|
|
579
|
+
def from_type_str(cls, abi_type, registry):
|
|
580
|
+
return cls(value_bit_size=abi_type.sub * 8)
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
class PackedBytesEncoder(BytesEncoder):
|
|
584
|
+
@parse_type_str("bytes")
|
|
585
|
+
def from_type_str(cls, abi_type, registry):
|
|
586
|
+
return cls(
|
|
587
|
+
value_bit_size=abi_type.sub * 8,
|
|
588
|
+
data_byte_size=abi_type.sub,
|
|
589
|
+
)
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
class ByteStringEncoder(BaseEncoder):
|
|
593
|
+
is_dynamic = True
|
|
594
|
+
|
|
595
|
+
@classmethod
|
|
596
|
+
def validate_value(cls, value: Any) -> None:
|
|
597
|
+
if not is_bytes(value):
|
|
598
|
+
cls.invalidate_value(value)
|
|
599
|
+
|
|
600
|
+
@classmethod
|
|
601
|
+
def encode(cls, value: bytes) -> bytes:
|
|
602
|
+
cls.validate_value(value)
|
|
603
|
+
return encode_bytestring(value)
|
|
604
|
+
|
|
605
|
+
__call__: ClassVar[Callable[[Type[Self], bytes], bytes]] = encode
|
|
606
|
+
|
|
607
|
+
@parse_type_str("bytes")
|
|
608
|
+
def from_type_str(cls, abi_type, registry):
|
|
609
|
+
return cls() # type: ignore [misc]
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
class PackedByteStringEncoder(ByteStringEncoder):
|
|
613
|
+
is_dynamic = False
|
|
614
|
+
|
|
615
|
+
@classmethod
|
|
616
|
+
def encode(cls, value: bytes) -> bytes:
|
|
617
|
+
cls.validate_value(value)
|
|
618
|
+
return value
|
|
619
|
+
|
|
620
|
+
__call__ = encode
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
class TextStringEncoder(BaseEncoder):
|
|
624
|
+
is_dynamic = True
|
|
625
|
+
|
|
626
|
+
@classmethod
|
|
627
|
+
def validate_value(cls, value: Any) -> None:
|
|
628
|
+
if not is_text(value):
|
|
629
|
+
cls.invalidate_value(value)
|
|
630
|
+
|
|
631
|
+
@classmethod
|
|
632
|
+
def encode(cls, value: str) -> bytes:
|
|
633
|
+
cls.validate_value(value)
|
|
634
|
+
return encode_text(value)
|
|
635
|
+
|
|
636
|
+
__call__: ClassVar[Callable[[Type[Self], str], bytes]] = encode
|
|
637
|
+
|
|
638
|
+
@parse_type_str("string")
|
|
639
|
+
def from_type_str(cls, abi_type, registry):
|
|
640
|
+
return cls() # type: ignore [misc]
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
class PackedTextStringEncoder(TextStringEncoder):
|
|
644
|
+
is_dynamic = False
|
|
645
|
+
|
|
646
|
+
@classmethod
|
|
647
|
+
def encode(cls, value: str) -> bytes:
|
|
648
|
+
cls.validate_value(value)
|
|
649
|
+
return value.encode("utf-8")
|
|
650
|
+
|
|
651
|
+
__call__ = encode
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
class BaseArrayEncoder(BaseEncoder):
|
|
655
|
+
item_encoder: BaseEncoder = None
|
|
656
|
+
|
|
657
|
+
def validate(self) -> None:
|
|
658
|
+
super().validate()
|
|
659
|
+
|
|
660
|
+
if self.item_encoder is None:
|
|
661
|
+
raise ValueError("`item_encoder` may not be none")
|
|
662
|
+
|
|
663
|
+
def validate_value(self, value: Any) -> None:
|
|
664
|
+
validate_array(self, value)
|
|
665
|
+
|
|
666
|
+
def encode_elements(self, value: Sequence[Any]) -> bytes:
|
|
667
|
+
self.validate_value(value)
|
|
668
|
+
return encode_elements(self.item_encoder, value)
|
|
669
|
+
|
|
670
|
+
@parse_type_str(with_arrlist=True)
|
|
671
|
+
def from_type_str(cls, abi_type, registry):
|
|
672
|
+
item_encoder = registry.get_encoder(abi_type.item_type.to_type_str())
|
|
673
|
+
|
|
674
|
+
array_spec = abi_type.arrlist[-1]
|
|
675
|
+
if len(array_spec) == 1:
|
|
676
|
+
# If array dimension is fixed
|
|
677
|
+
return SizedArrayEncoder(
|
|
678
|
+
array_size=array_spec[0],
|
|
679
|
+
item_encoder=item_encoder,
|
|
680
|
+
)
|
|
681
|
+
else:
|
|
682
|
+
# If array dimension is dynamic
|
|
683
|
+
return DynamicArrayEncoder(item_encoder=item_encoder)
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
class PackedArrayEncoder(BaseArrayEncoder):
|
|
687
|
+
array_size: Optional[int] = None
|
|
688
|
+
|
|
689
|
+
def validate_value(self, value: Any) -> None:
|
|
690
|
+
validate_packed_array(self, value)
|
|
691
|
+
|
|
692
|
+
def encode(self, value: Sequence[Any]) -> bytes:
|
|
693
|
+
return encode_elements(self.item_encoder, value)
|
|
694
|
+
|
|
695
|
+
__call__ = encode
|
|
696
|
+
|
|
697
|
+
@parse_type_str(with_arrlist=True)
|
|
698
|
+
def from_type_str(cls, abi_type, registry):
|
|
699
|
+
item_encoder = registry.get_encoder(abi_type.item_type.to_type_str())
|
|
700
|
+
|
|
701
|
+
array_spec = abi_type.arrlist[-1]
|
|
702
|
+
if len(array_spec) == 1:
|
|
703
|
+
return cls(
|
|
704
|
+
array_size=array_spec[0],
|
|
705
|
+
item_encoder=item_encoder,
|
|
706
|
+
)
|
|
707
|
+
else:
|
|
708
|
+
return cls(item_encoder=item_encoder)
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
class SizedArrayEncoder(BaseArrayEncoder):
|
|
712
|
+
array_size: int = None # type: ignore [assignment]
|
|
713
|
+
|
|
714
|
+
def __init__(self, **kwargs: Any) -> None:
|
|
715
|
+
super().__init__(**kwargs)
|
|
716
|
+
|
|
717
|
+
self.is_dynamic = self.item_encoder.is_dynamic
|
|
718
|
+
|
|
719
|
+
def validate(self) -> None:
|
|
720
|
+
super().validate()
|
|
721
|
+
|
|
722
|
+
if self.array_size is None:
|
|
723
|
+
raise ValueError("`array_size` may not be none")
|
|
724
|
+
|
|
725
|
+
def validate_value(self, value: Any) -> None:
|
|
726
|
+
validate_sized_array(self, value)
|
|
727
|
+
|
|
728
|
+
def encode(self, value: Sequence[Any]) -> bytes:
|
|
729
|
+
return encode_elements(self.item_encoder, value)
|
|
730
|
+
|
|
731
|
+
__call__ = encode
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
class DynamicArrayEncoder(BaseArrayEncoder):
|
|
735
|
+
is_dynamic = True
|
|
736
|
+
|
|
737
|
+
def encode(self, value: Sequence[Any]) -> bytes:
|
|
738
|
+
return encode_elements_dynamic(self.item_encoder, value)
|