-
Notifications
You must be signed in to change notification settings - Fork 140
/
base.py
258 lines (221 loc) · 6.96 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from abc import (
ABC,
abstractmethod,
)
import hashlib
import os
from typing import (
Any,
Dict,
List,
Optional,
Tuple,
Union,
)
from ckzg import (
blob_to_kzg_commitment,
compute_blob_kzg_proof,
load_trusted_setup,
)
from eth_typing import (
HexStr,
)
from eth_utils import (
is_bytes,
is_string,
to_bytes,
to_int,
)
from eth_utils.curried import (
apply_formatter_to_array,
apply_formatters_to_dict,
apply_one_of_formatters,
hexstr_if_str,
)
from eth_utils.toolz import (
identity,
merge,
)
from hexbytes import (
HexBytes,
)
from pydantic import (
BaseModel,
ConfigDict,
computed_field,
field_validator,
)
from eth_account._utils.validation import (
LEGACY_TRANSACTION_FORMATTERS,
)
TYPED_TRANSACTION_FORMATTERS = merge(
LEGACY_TRANSACTION_FORMATTERS,
{
"chainId": hexstr_if_str(to_int),
"type": hexstr_if_str(to_int),
"accessList": apply_formatter_to_array(
apply_formatters_to_dict(
{
"address": apply_one_of_formatters(
(
(is_string, hexstr_if_str(to_bytes)),
(is_bytes, identity),
)
),
"storageKeys": apply_formatter_to_array(hexstr_if_str(to_int)),
}
),
),
"maxPriorityFeePerGas": hexstr_if_str(to_int),
"maxFeePerGas": hexstr_if_str(to_int),
"maxFeePerBlobGas": hexstr_if_str(to_int),
"blobVersionedHashes": apply_formatter_to_array(hexstr_if_str(to_bytes)),
},
)
# import TRUSTED_SETUP from ./kzg_trusted_setup.txt
TRUSTED_SETUP = os.path.join(
os.path.dirname(__file__), "blob_transactions", "kzg_trusted_setup.txt"
)
VERSIONED_HASH_VERSION_KZG = b"\x01"
class _BlobDataElement(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
data: HexBytes
def as_hexbytes(self) -> HexBytes:
return self.data
def as_bytes(self) -> bytes:
return bytes(self.data)
def as_hexstr(self) -> HexStr:
return HexStr(f"0x{self.as_bytes().hex()}")
class Blob(_BlobDataElement):
"""
Represents a Blob.
"""
@field_validator("data")
def validate_data(cls, v: Union[HexBytes, bytes]) -> Union[HexBytes, bytes]:
if len(v) != 4096 * 32:
raise ValueError(
"Invalid Blob size. Blob data must be comprised of 4096 32-byte "
"field elements."
)
return v
class BlobKZGCommitment(_BlobDataElement):
"""
Represents a Blob KZG Commitment.
"""
@field_validator("data")
def validate_commitment(cls, v: Union[HexBytes, bytes]) -> Union[HexBytes, bytes]:
if len(v) != 48:
raise ValueError("Blob KZG Commitment must be 48 bytes long.")
return v
class BlobProof(_BlobDataElement):
"""
Represents a Blob Proof.
"""
@field_validator("data")
def validate_proof(cls, v: Union[HexBytes, bytes]) -> Union[HexBytes, bytes]:
if len(v) != 48:
raise ValueError("Blob Proof must be 48 bytes long.")
return v
class BlobVersionedHash(_BlobDataElement):
"""
Represents a Blob Versioned Hash.
"""
@field_validator("data")
def validate_versioned_hash(
cls, v: Union[HexBytes, bytes]
) -> Union[HexBytes, bytes]:
if len(v) != 32:
raise ValueError("Blob Versioned Hash must be 32 bytes long.")
if v[:1] != VERSIONED_HASH_VERSION_KZG:
raise ValueError(
"Blob Versioned Hash must start with the KZG version byte."
)
return v
class BlobPooledTransactionData(BaseModel):
"""
Represents the blob data for a type 3 `PooledTransaction` as defined by
EIP-4844. This class takes blobs as bytes and computes the corresponding
commitments, proofs, and versioned hashes.
"""
_versioned_hash_version_kzg: bytes = VERSIONED_HASH_VERSION_KZG
_versioned_hashes: Optional[List[BlobVersionedHash]] = None
_commitments: Optional[List[BlobKZGCommitment]] = None
_proofs: Optional[List[BlobProof]] = None
blobs: List[Blob]
def _kzg_to_versioned_hash(self, kzg_commitment: BlobKZGCommitment) -> bytes:
return (
self._versioned_hash_version_kzg
+ hashlib.sha256(kzg_commitment.data).digest()[1:]
)
@field_validator("blobs")
def validate_blobs(cls, v: List[Blob]) -> List[Blob]:
if len(v) == 0:
raise ValueError("Blob transactions must contain at least one blob.")
return v
# type ignored bc mypy does not support decorated properties
# https://github.com/python/mypy/issues/1362
@computed_field # type: ignore
@property
def versioned_hashes(self) -> List[BlobVersionedHash]:
if self._versioned_hashes is None:
self._versioned_hashes = [
BlobVersionedHash(
data=HexBytes(self._kzg_to_versioned_hash(commitment))
)
for commitment in self.commitments
]
return self._versioned_hashes
# type ignored bc mypy does not support decorated properties
# https://github.com/python/mypy/issues/1362
@computed_field # type: ignore
@property
def commitments(self) -> List[BlobKZGCommitment]:
if self._commitments is None:
self._commitments = [
BlobKZGCommitment(
data=HexBytes(
blob_to_kzg_commitment(
blob.data, load_trusted_setup(TRUSTED_SETUP)
)
)
)
for blob in self.blobs
]
return self._commitments
# type ignored bc mypy does not support decorated properties
# https://github.com/python/mypy/issues/1362
@computed_field # type: ignore
@property
def proofs(self) -> List[BlobProof]:
if self._proofs is None:
self._proofs = [
BlobProof(
data=HexBytes(
compute_blob_kzg_proof(
blob.data,
commitment.data,
load_trusted_setup(TRUSTED_SETUP),
)
)
)
for blob, commitment in zip(self.blobs, self.commitments)
]
return self._proofs
class _TypedTransactionImplementation(ABC):
"""
Abstract class that every typed transaction must implement.
Should not be imported or used by clients of the library.
"""
blob_data: Optional[BlobPooledTransactionData] = None
@abstractmethod
def hash(self) -> bytes:
pass
@abstractmethod
def payload(self) -> bytes:
pass
@abstractmethod
def as_dict(self) -> Dict[str, Any]:
pass
@abstractmethod
def vrs(self) -> Tuple[int, int, int]:
pass