faster-eth-abi 5.2.20__cp313-cp313-win32.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.
Potentially problematic release.
This version of faster-eth-abi might be problematic. Click here for more details.
- faster_eth_abi/__init__.py +12 -0
- faster_eth_abi/_codec.cp313-win32.pyd +0 -0
- faster_eth_abi/_codec.py +83 -0
- faster_eth_abi/_decoding.cp313-win32.pyd +0 -0
- faster_eth_abi/_decoding.py +299 -0
- faster_eth_abi/_encoding.cp313-win32.pyd +0 -0
- faster_eth_abi/_encoding.py +251 -0
- faster_eth_abi/_grammar.cp313-win32.pyd +0 -0
- faster_eth_abi/_grammar.py +375 -0
- faster_eth_abi/abi.cp313-win32.pyd +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.cp313-win32.pyd +0 -0
- faster_eth_abi/constants.py +7 -0
- faster_eth_abi/decoding.py +584 -0
- faster_eth_abi/encoding.py +746 -0
- faster_eth_abi/exceptions.py +127 -0
- faster_eth_abi/from_type_str.cp313-win32.pyd +0 -0
- faster_eth_abi/from_type_str.py +141 -0
- faster_eth_abi/grammar.py +172 -0
- faster_eth_abi/io.py +107 -0
- faster_eth_abi/packed.cp313-win32.pyd +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__.cp313-win32.pyd +0 -0
- faster_eth_abi/tools/__init__.py +3 -0
- faster_eth_abi/tools/_strategies.cp313-win32.pyd +0 -0
- faster_eth_abi/tools/_strategies.py +243 -0
- faster_eth_abi/typing.py +4627 -0
- faster_eth_abi/utils/__init__.cp313-win32.pyd +0 -0
- faster_eth_abi/utils/__init__.py +0 -0
- faster_eth_abi/utils/numeric.cp313-win32.pyd +0 -0
- faster_eth_abi/utils/numeric.py +117 -0
- faster_eth_abi/utils/padding.cp313-win32.pyd +0 -0
- faster_eth_abi/utils/padding.py +22 -0
- faster_eth_abi/utils/string.cp313-win32.pyd +0 -0
- faster_eth_abi/utils/string.py +19 -0
- faster_eth_abi/utils/validation.cp313-win32.pyd +0 -0
- faster_eth_abi/utils/validation.py +18 -0
- faster_eth_abi-5.2.20.dist-info/METADATA +136 -0
- faster_eth_abi-5.2.20.dist-info/RECORD +46 -0
- faster_eth_abi-5.2.20.dist-info/WHEEL +5 -0
- faster_eth_abi-5.2.20.dist-info/top_level.txt +2 -0
- faster_eth_abi__mypyc.cp313-win32.pyd +0 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Private helpers for ABI type string grammar and parsing, intended for C compilation.
|
|
3
|
+
|
|
4
|
+
This file exists because the original grammar.py is not ready to be fully compiled to C.
|
|
5
|
+
This module contains functions and logic that we do wish to compile.
|
|
6
|
+
"""
|
|
7
|
+
import re
|
|
8
|
+
from typing import (
|
|
9
|
+
Any,
|
|
10
|
+
Final,
|
|
11
|
+
Generic,
|
|
12
|
+
Literal,
|
|
13
|
+
NewType,
|
|
14
|
+
NoReturn,
|
|
15
|
+
Optional,
|
|
16
|
+
Tuple,
|
|
17
|
+
TypeVar,
|
|
18
|
+
Union,
|
|
19
|
+
cast,
|
|
20
|
+
final,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
from eth_typing.abi import (
|
|
24
|
+
TypeStr,
|
|
25
|
+
)
|
|
26
|
+
from parsimonious.nodes import (
|
|
27
|
+
Node,
|
|
28
|
+
)
|
|
29
|
+
from typing_extensions import (
|
|
30
|
+
Self,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from faster_eth_abi.exceptions import (
|
|
34
|
+
ABITypeError,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
TYPE_ALIASES: Final = {
|
|
38
|
+
"int": "int256",
|
|
39
|
+
"uint": "uint256",
|
|
40
|
+
"fixed": "fixed128x18",
|
|
41
|
+
"ufixed": "ufixed128x18",
|
|
42
|
+
"function": "bytes24",
|
|
43
|
+
"byte": "bytes1",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
TYPE_ALIAS_RE: Final = re.compile(
|
|
47
|
+
rf"\b({'|'.join(map(re.escape, TYPE_ALIASES.keys()))})\b"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Arrlist = Tuple[Union[int, Tuple[int, ...]], ...]
|
|
52
|
+
IntSubtype = NewType("IntSubtype", int)
|
|
53
|
+
FixedSubtype = NewType("FixedSubtype", Tuple[int, int])
|
|
54
|
+
Subtype = Union[IntSubtype, FixedSubtype]
|
|
55
|
+
TSub = TypeVar("TSub", IntSubtype, FixedSubtype, Literal[None])
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ABIType:
|
|
59
|
+
"""
|
|
60
|
+
Base class for results of type string parsing operations.
|
|
61
|
+
|
|
62
|
+
Notes
|
|
63
|
+
-----
|
|
64
|
+
Users are unable to subclass this class. If your use case requires subclassing,
|
|
65
|
+
you will need to stick to the original `eth-abi`.
|
|
66
|
+
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
arrlist: Final[Optional[Arrlist]]
|
|
70
|
+
node: Final[Optional[Node]]
|
|
71
|
+
|
|
72
|
+
__slots__ = ("arrlist", "node")
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self, arrlist: Optional[Arrlist] = None, node: Optional[Node] = None
|
|
76
|
+
) -> None:
|
|
77
|
+
self.arrlist = arrlist
|
|
78
|
+
"""
|
|
79
|
+
The list of array dimensions for a parsed type. Equal to ``None`` if
|
|
80
|
+
type string has no array dimensions.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
self.node = node
|
|
84
|
+
"""
|
|
85
|
+
The parsimonious ``Node`` instance associated with this parsed type.
|
|
86
|
+
Used to generate error messages for invalid types.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __repr__(self) -> str: # pragma: no cover
|
|
90
|
+
return f"<{type(self).__qualname__} {self.to_type_str()!r}>"
|
|
91
|
+
|
|
92
|
+
def __eq__(self, other: Any) -> bool:
|
|
93
|
+
# Two ABI types are equal if their string representations are equal
|
|
94
|
+
return type(self) is type(other) and self.to_type_str() == other.to_type_str()
|
|
95
|
+
|
|
96
|
+
def to_type_str(self) -> TypeStr: # pragma: no cover
|
|
97
|
+
"""
|
|
98
|
+
Returns the string representation of an ABI type. This will be equal to
|
|
99
|
+
the type string from which it was created.
|
|
100
|
+
"""
|
|
101
|
+
raise NotImplementedError("Must implement `to_type_str`")
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def item_type(self) -> Self:
|
|
105
|
+
"""
|
|
106
|
+
If this type is an array type, equal to an appropriate
|
|
107
|
+
:class:`~faster_eth_abi.grammar.ABIType` instance for the array's items.
|
|
108
|
+
"""
|
|
109
|
+
raise NotImplementedError("Must implement `item_type`")
|
|
110
|
+
|
|
111
|
+
def validate(self) -> None: # pragma: no cover
|
|
112
|
+
"""
|
|
113
|
+
Validates the properties of an ABI type against the solidity ABI spec:
|
|
114
|
+
|
|
115
|
+
https://solidity.readthedocs.io/en/develop/abi-spec.html
|
|
116
|
+
|
|
117
|
+
Raises :class:`~faster_eth_abi.exceptions.ABITypeError` if validation fails.
|
|
118
|
+
"""
|
|
119
|
+
raise NotImplementedError("Must implement `validate`")
|
|
120
|
+
|
|
121
|
+
@final
|
|
122
|
+
def invalidate(self, error_msg: str) -> NoReturn:
|
|
123
|
+
# Invalidates an ABI type with the given error message. Expects that a
|
|
124
|
+
# parsimonious node was provided from the original parsing operation
|
|
125
|
+
# that yielded this type.
|
|
126
|
+
node = self.node
|
|
127
|
+
|
|
128
|
+
raise ABITypeError(
|
|
129
|
+
f"For '{node.text}' type at column {node.start + 1} "
|
|
130
|
+
f"in '{node.full_text}': {error_msg}"
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
@final
|
|
134
|
+
@property
|
|
135
|
+
def is_array(self) -> bool:
|
|
136
|
+
"""
|
|
137
|
+
Equal to ``True`` if a type is an array type (i.e. if it has an array
|
|
138
|
+
dimension list). Otherwise, equal to ``False``.
|
|
139
|
+
"""
|
|
140
|
+
return self.arrlist is not None
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def is_dynamic(self) -> bool:
|
|
144
|
+
"""
|
|
145
|
+
Equal to ``True`` if a type has a dynamically sized encoding.
|
|
146
|
+
Otherwise, equal to ``False``.
|
|
147
|
+
"""
|
|
148
|
+
raise NotImplementedError("Must implement `is_dynamic`")
|
|
149
|
+
|
|
150
|
+
@final
|
|
151
|
+
@property
|
|
152
|
+
def _has_dynamic_arrlist(self) -> bool:
|
|
153
|
+
return self.is_array and any(len(dim) == 0 for dim in self.arrlist)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
TComp = TypeVar("TComp", bound=ABIType)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class TupleType(ABIType):
|
|
160
|
+
"""
|
|
161
|
+
Represents the result of parsing a tuple type string e.g. "(int,bool)".
|
|
162
|
+
|
|
163
|
+
Notes
|
|
164
|
+
-----
|
|
165
|
+
Users are unable to subclass this class. If your use case requires subclassing,
|
|
166
|
+
you will need to stick to the original `eth-abi`.
|
|
167
|
+
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
__slots__ = ("components",)
|
|
171
|
+
|
|
172
|
+
def __init__(
|
|
173
|
+
self,
|
|
174
|
+
components: Tuple[TComp, ...],
|
|
175
|
+
arrlist: Optional[Arrlist] = None,
|
|
176
|
+
*,
|
|
177
|
+
node: Optional[Node] = None,
|
|
178
|
+
) -> None:
|
|
179
|
+
super().__init__(arrlist, node)
|
|
180
|
+
|
|
181
|
+
self.components: Final = components
|
|
182
|
+
"""
|
|
183
|
+
A tuple of :class:`~faster_eth_abi.grammar.ABIType` instances for each of the
|
|
184
|
+
tuple type's components.
|
|
185
|
+
"""
|
|
186
|
+
|
|
187
|
+
def to_type_str(self) -> TypeStr:
|
|
188
|
+
components = f"({','.join(c.to_type_str() for c in self.components)})"
|
|
189
|
+
|
|
190
|
+
if isinstance(arrlist := self.arrlist, tuple):
|
|
191
|
+
return components + "".join(map(repr, map(list, arrlist)))
|
|
192
|
+
else:
|
|
193
|
+
return components
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def item_type(self) -> Self:
|
|
197
|
+
if not self.is_array:
|
|
198
|
+
raise ValueError(
|
|
199
|
+
f"Cannot determine item type for non-array type '{self.to_type_str()}'"
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
arrlist = cast(Arrlist, self.arrlist)[:-1] or None
|
|
203
|
+
cls = type(self)
|
|
204
|
+
if cls is TupleType:
|
|
205
|
+
return cast(Self, TupleType(self.components, arrlist, node=self.node))
|
|
206
|
+
else:
|
|
207
|
+
return cls(self.components, arrlist, node=self.node)
|
|
208
|
+
|
|
209
|
+
def validate(self) -> None:
|
|
210
|
+
for c in self.components:
|
|
211
|
+
c.validate()
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def is_dynamic(self) -> bool:
|
|
215
|
+
if self._has_dynamic_arrlist:
|
|
216
|
+
return True
|
|
217
|
+
|
|
218
|
+
return any(c.is_dynamic for c in self.components)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class BasicType(ABIType, Generic[TSub]):
|
|
222
|
+
"""
|
|
223
|
+
Represents the result of parsing a basic type string e.g. "uint", "address",
|
|
224
|
+
"ufixed128x19[][2]".
|
|
225
|
+
|
|
226
|
+
Notes
|
|
227
|
+
-----
|
|
228
|
+
Users are unable to subclass this class. If your use case requires subclassing,
|
|
229
|
+
you will need to stick to the original `eth-abi`.
|
|
230
|
+
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
__slots__ = ("base", "sub")
|
|
234
|
+
|
|
235
|
+
def __init__(
|
|
236
|
+
self,
|
|
237
|
+
base: str,
|
|
238
|
+
sub: Optional[TSub] = None,
|
|
239
|
+
arrlist: Optional[Arrlist] = None,
|
|
240
|
+
*,
|
|
241
|
+
node: Optional[Node] = None,
|
|
242
|
+
) -> None:
|
|
243
|
+
super().__init__(arrlist, node)
|
|
244
|
+
|
|
245
|
+
self.base: Final = base
|
|
246
|
+
"""The base of a basic type e.g. "uint" for "uint256" etc."""
|
|
247
|
+
|
|
248
|
+
self.sub: Final = sub
|
|
249
|
+
"""
|
|
250
|
+
The sub type of a basic type e.g. ``256`` for "uint256" or ``(128, 18)``
|
|
251
|
+
for "ufixed128x18" etc. Equal to ``None`` if type string has no sub
|
|
252
|
+
type.
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
def to_type_str(self) -> TypeStr:
|
|
256
|
+
sub, arrlist = self.sub, self.arrlist
|
|
257
|
+
|
|
258
|
+
if isinstance(sub, int):
|
|
259
|
+
substr = str(sub)
|
|
260
|
+
elif isinstance(sub, tuple):
|
|
261
|
+
substr = "x".join(map(str, sub))
|
|
262
|
+
else:
|
|
263
|
+
substr = ""
|
|
264
|
+
|
|
265
|
+
if isinstance(arrlist, tuple):
|
|
266
|
+
return self.base + substr + "".join(map(repr, map(list, arrlist)))
|
|
267
|
+
else:
|
|
268
|
+
return self.base + substr
|
|
269
|
+
|
|
270
|
+
@property
|
|
271
|
+
def item_type(self) -> Self:
|
|
272
|
+
if not self.is_array:
|
|
273
|
+
raise ValueError(
|
|
274
|
+
f"Cannot determine item type for non-array type '{self.to_type_str()}'"
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
cls = type(self)
|
|
278
|
+
arrlist = cast(Arrlist, self.arrlist)[:-1] or None
|
|
279
|
+
if cls is BasicType:
|
|
280
|
+
return cast(Self, BasicType(self.base, self.sub, arrlist, node=self.node))
|
|
281
|
+
else:
|
|
282
|
+
return cls(self.base, self.sub, arrlist, node=self.node)
|
|
283
|
+
|
|
284
|
+
@property
|
|
285
|
+
def is_dynamic(self) -> bool:
|
|
286
|
+
if self._has_dynamic_arrlist:
|
|
287
|
+
return True
|
|
288
|
+
|
|
289
|
+
base = self.base
|
|
290
|
+
if base == "string":
|
|
291
|
+
return True
|
|
292
|
+
|
|
293
|
+
if base == "bytes" and self.sub is None:
|
|
294
|
+
return True
|
|
295
|
+
|
|
296
|
+
return False
|
|
297
|
+
|
|
298
|
+
def validate(self) -> None:
|
|
299
|
+
base, sub = self.base, self.sub
|
|
300
|
+
|
|
301
|
+
# Check validity of string type
|
|
302
|
+
if base == "string":
|
|
303
|
+
if sub is not None:
|
|
304
|
+
self.invalidate("string type cannot have suffix")
|
|
305
|
+
|
|
306
|
+
# Check validity of bytes type
|
|
307
|
+
elif base == "bytes":
|
|
308
|
+
if not (sub is None or isinstance(sub, int)):
|
|
309
|
+
self.invalidate(
|
|
310
|
+
"bytes type must have either no suffix or a numerical suffix"
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
if isinstance(sub, int) and sub > 32:
|
|
314
|
+
self.invalidate("maximum 32 bytes for fixed-length bytes")
|
|
315
|
+
|
|
316
|
+
# Check validity of integer type
|
|
317
|
+
elif base in ("int", "uint"):
|
|
318
|
+
if not isinstance(sub, int):
|
|
319
|
+
self.invalidate("integer type must have numerical suffix")
|
|
320
|
+
|
|
321
|
+
if sub < 8 or sub > 256:
|
|
322
|
+
self.invalidate("integer size out of bounds (max 256 bits)")
|
|
323
|
+
|
|
324
|
+
if sub % 8 != 0:
|
|
325
|
+
self.invalidate("integer size must be multiple of 8")
|
|
326
|
+
|
|
327
|
+
# Check validity of fixed type
|
|
328
|
+
elif base in ("fixed", "ufixed"):
|
|
329
|
+
if not isinstance(sub, tuple):
|
|
330
|
+
self.invalidate(
|
|
331
|
+
"fixed type must have suffix of form <bits>x<exponent>, "
|
|
332
|
+
"e.g. 128x19",
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
bits, minus_e = sub
|
|
336
|
+
|
|
337
|
+
if bits < 8 or bits > 256:
|
|
338
|
+
self.invalidate("fixed size out of bounds (max 256 bits)")
|
|
339
|
+
|
|
340
|
+
if bits % 8 != 0:
|
|
341
|
+
self.invalidate("fixed size must be multiple of 8")
|
|
342
|
+
|
|
343
|
+
if minus_e < 1 or minus_e > 80:
|
|
344
|
+
self.invalidate(
|
|
345
|
+
f"fixed exponent size out of bounds, {minus_e} must be in 1-80"
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
# Check validity of hash type
|
|
349
|
+
elif base == "hash":
|
|
350
|
+
if not isinstance(sub, int):
|
|
351
|
+
self.invalidate("hash type must have numerical suffix")
|
|
352
|
+
|
|
353
|
+
# Check validity of address type
|
|
354
|
+
elif base == "address":
|
|
355
|
+
if sub is not None:
|
|
356
|
+
self.invalidate("address cannot have suffix")
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
BytesType = BasicType[IntSubtype]
|
|
360
|
+
FixedType = BasicType[FixedSubtype]
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def normalize(type_str: TypeStr) -> TypeStr:
|
|
364
|
+
"""
|
|
365
|
+
Normalizes a type string into its canonical version e.g. the type string
|
|
366
|
+
'int' becomes 'int256', etc.
|
|
367
|
+
|
|
368
|
+
:param type_str: The type string to be normalized.
|
|
369
|
+
:returns: The canonical version of the input type string.
|
|
370
|
+
"""
|
|
371
|
+
return TYPE_ALIAS_RE.sub(__normalize, type_str)
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def __normalize(match: "re.Match[str]") -> str:
|
|
375
|
+
return TYPE_ALIASES[match.group(0)]
|
|
Binary file
|
faster_eth_abi/abi.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Final,
|
|
3
|
+
)
|
|
4
|
+
|
|
5
|
+
from faster_eth_abi.codec import (
|
|
6
|
+
ABICodec,
|
|
7
|
+
)
|
|
8
|
+
from faster_eth_abi.registry import (
|
|
9
|
+
registry,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
default_codec: Final = ABICodec(registry)
|
|
13
|
+
|
|
14
|
+
encode: Final = default_codec.encode
|
|
15
|
+
decode: Final = default_codec.decode
|
|
16
|
+
is_encodable: Final = default_codec.is_encodable
|
|
17
|
+
is_encodable_type: Final = default_codec.is_encodable_type
|
faster_eth_abi/base.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Base classes for encoder and decoder implementations.
|
|
2
|
+
|
|
3
|
+
Defines the foundational interface and validation logic for all coder classes.
|
|
4
|
+
"""
|
|
5
|
+
from typing import (
|
|
6
|
+
Any,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BaseCoder:
|
|
11
|
+
"""
|
|
12
|
+
Base class for all encoder and decoder classes.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
is_dynamic = False
|
|
16
|
+
|
|
17
|
+
def __init__(self, **kwargs: Any) -> None:
|
|
18
|
+
cls = type(self)
|
|
19
|
+
|
|
20
|
+
# Ensure no unrecognized kwargs were given
|
|
21
|
+
for key, value in kwargs.items():
|
|
22
|
+
if not hasattr(cls, key):
|
|
23
|
+
raise AttributeError(
|
|
24
|
+
"Property {key} not found on {cls_name} class. "
|
|
25
|
+
"`{cls_name}.__init__` only accepts keyword arguments which are "
|
|
26
|
+
"present on the {cls_name} class.".format(
|
|
27
|
+
key=key,
|
|
28
|
+
cls_name=cls.__name__,
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
setattr(self, key, value)
|
|
32
|
+
|
|
33
|
+
# Validate given combination of kwargs
|
|
34
|
+
self.validate()
|
|
35
|
+
|
|
36
|
+
def validate(self) -> None:
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
@classmethod
|
|
40
|
+
def from_type_str(cls, type_str, registry): # pragma: no cover
|
|
41
|
+
"""
|
|
42
|
+
Used by :any:`ABIRegistry` to get an appropriate encoder or decoder
|
|
43
|
+
instance for the given type string and type registry.
|
|
44
|
+
"""
|
|
45
|
+
raise NotImplementedError("Must implement `from_type_str`")
|