|
9 | 9 |
|
10 | 10 | from starknet_py.net.models.typed_data import Revision |
11 | 11 | from starknet_py.tests.e2e.fixtures.constants import TYPED_DATA_DIR |
12 | | -from starknet_py.utils.typed_data import Domain, Parameter, TypedData, get_hex |
| 12 | +from starknet_py.utils.typed_data import ( |
| 13 | + BasicType, |
| 14 | + Domain, |
| 15 | + Parameter, |
| 16 | + TypedData, |
| 17 | + get_hex, |
| 18 | +) |
13 | 19 |
|
14 | 20 |
|
15 | 21 | class CasesRev0(Enum): |
@@ -197,15 +203,80 @@ def _make_typed_data(included_type: str, revision: Revision): |
197 | 203 |
|
198 | 204 |
|
199 | 205 | @pytest.mark.parametrize( |
200 | | - "included_type", |
| 206 | + "included_type, revision", |
| 207 | + [ |
| 208 | + ("", Revision.V1), |
| 209 | + ("myType*", Revision.V1) |
| 210 | + ], |
| 211 | +) |
| 212 | +def test_invalid_type_names(included_type: str, revision: Revision): |
| 213 | + with pytest.raises(ValueError): |
| 214 | + _make_typed_data(included_type, revision) |
| 215 | + |
| 216 | + |
| 217 | +@pytest.mark.parametrize( |
| 218 | + "included_type, revision", |
201 | 219 | [ |
202 | | - "felt", |
203 | | - "felt*", |
204 | | - "string", |
205 | | - "selector", |
206 | | - "merkletree" |
| 220 | + (BasicType.FELT.value, Revision.V0), |
| 221 | + (BasicType.STRING.value, Revision.V0), |
| 222 | + (BasicType.SELECTOR.value, Revision.V0), |
| 223 | + (BasicType.MERKLE_TREE.value, Revision.V0), |
| 224 | + (BasicType.FELT.value, Revision.V1), |
| 225 | + (BasicType.STRING.value, Revision.V1), |
| 226 | + (BasicType.SELECTOR.value, Revision.V1), |
| 227 | + (BasicType.MERKLE_TREE.value, Revision.V1), |
| 228 | + (BasicType.SHORT_STRING.value, Revision.V1), |
207 | 229 | ], |
208 | 230 | ) |
209 | | -def test_invalid_types(included_type: str): |
| 231 | +def test_types_redefinition(included_type: str, revision: Revision): |
210 | 232 | with pytest.raises(ValueError, match=f"Reserved type name: {included_type}"): |
211 | | - _make_typed_data(included_type, Revision.V1) |
| 233 | + _make_typed_data(included_type, revision) |
| 234 | + |
| 235 | + |
| 236 | +def test_custom_type_definition(): |
| 237 | + _make_typed_data("myType", Revision.V0) |
| 238 | + |
| 239 | + |
| 240 | +@pytest.mark.parametrize( |
| 241 | + "revision", |
| 242 | + list(Revision), |
| 243 | +) |
| 244 | +def test_missing_domain_type(revision: Revision): |
| 245 | + domain = domain_v0 if revision == Revision.V0 else domain_v1 |
| 246 | + |
| 247 | + with pytest.raises(ValueError, match=f"Types must contain '{domain.separator_name}'."): |
| 248 | + TypedData( |
| 249 | + types={}, |
| 250 | + primary_type="felt", |
| 251 | + domain=domain, |
| 252 | + message={}, |
| 253 | + ) |
| 254 | + |
| 255 | + |
| 256 | +def test_dangling_type(): |
| 257 | + with pytest.raises(ValueError, match="Dangling types are not allowed. Unreferenced type dangling was found."): |
| 258 | + TypedData( |
| 259 | + types={ |
| 260 | + **domain_type_v1, |
| 261 | + "dangling": [], |
| 262 | + "mytype": [] |
| 263 | + }, |
| 264 | + primary_type="mytype", |
| 265 | + domain=domain_v1, |
| 266 | + message={"mytype": 1}, |
| 267 | + ) |
| 268 | + |
| 269 | + |
| 270 | +def test_missing_dependency(): |
| 271 | + typed_data = TypedData( |
| 272 | + types={ |
| 273 | + **domain_type_v1, |
| 274 | + "house": [Parameter(name="fridge", type="ice cream")] |
| 275 | + }, |
| 276 | + primary_type="house", |
| 277 | + domain=domain_v1, |
| 278 | + message={"fridge": 1}, |
| 279 | + ) |
| 280 | + |
| 281 | + with pytest.raises(ValueError, match=r"Type \[ice cream\] is not defined in types."): |
| 282 | + typed_data.struct_hash("house", {"fridge": 1}) |
0 commit comments