Skip to content

Commit ee0cfc9

Browse files
committed
Merge branch 'development'
2 parents f8e2516 + 41877c1 commit ee0cfc9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2016
-279
lines changed

docs/api/typed_data.rst

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,65 @@ Parameter
1515
:members:
1616
:undoc-members:
1717
:exclude-members: __init__
18+
:member-order: bysource
19+
20+
-------------------
21+
StandardParameter
22+
-------------------
23+
24+
25+
.. autoclass:: starknet_py.utils.typed_data.StandardParameter
26+
:members:
27+
:undoc-members:
28+
:exclude-members: __init__
29+
:member-order: bysource
30+
31+
-------------------
32+
EnumParameter
33+
-------------------
34+
35+
.. autoclass:: starknet_py.utils.typed_data.EnumParameter
36+
:members:
37+
:undoc-members:
38+
:exclude-members: __init__
39+
:member-order: bysource
40+
41+
-------------------
42+
MerkleTreeParameter
43+
-------------------
44+
45+
.. autoclass:: starknet_py.utils.typed_data.MerkleTreeParameter
46+
:members:
47+
:undoc-members:
48+
:exclude-members: __init__
49+
:member-order: bysource
1850

1951
--------------
20-
StarkNetDomain
52+
Domain
2153
--------------
2254

23-
.. autoclass:: starknet_py.net.models.typed_data.StarkNetDomain
55+
.. autoclass:: starknet_py.utils.typed_data.Domain
2456
:members:
2557
:undoc-members:
58+
:exclude-members: __init__
59+
:member-order: bysource
60+
61+
---------
62+
BasicType
63+
---------
64+
65+
.. autoclass:: starknet_py.utils.typed_data.BasicType
66+
:members:
67+
:undoc-members:
68+
:exclude-members: __init__
69+
:member-order: bysource
70+
71+
----------
72+
PresetType
73+
----------
74+
75+
.. autoclass:: starknet_py.utils.typed_data.PresetType
76+
:members:
77+
:undoc-members:
78+
:exclude-members: __init__
79+
:member-order: bysource

docs/migration_guide.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
Migration guide
22
===============
33

4+
******************************
5+
0.23.0 Migration guide
6+
******************************
7+
8+
Version 0.23.0 of **starknet.py** comes with support for `SNIP-12 <https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md>`_!
9+
10+
0.23.0 Targeted versions
11+
------------------------
12+
13+
- Starknet - `0.13.1.1 <https://docs.starknet.io/documentation/starknet_versions/version_notes/#version0.13.1.1>`_
14+
- RPC - `0.7.1 <https://github.com/starkware-libs/starknet-specs/releases/tag/v0.7.1>`_
15+
16+
0.23.0 Breaking changes
17+
-----------------------
18+
19+
.. currentmodule:: starknet_py.utils.typed_data
20+
21+
1. :class:`StarkNetDomain` has been renamed to :class:`Domain`
22+
2. :class:`TypedData` field ``domain`` has been changed from ``dict`` to :class:`Domain`
23+
3. :class:`Parameter` is now abstract - :class:`StandardParameter`, :class:`EnumParameter` and :class:`MerkleTreeParameter` should be used
24+
25+
0.23.0 Minor changes
26+
-----------------------
27+
28+
.. currentmodule:: starknet_py.net.account.account
29+
30+
1. :meth:`Account.sign_message` now accepts parameter ``typed_data`` as both :class:`~starknet_py.utils.typed_data.TypedData` and :class:`~starknet_py.net.models.typed_data.TypedDataDict`
31+
2. :meth:`Account.verify_message` now accepts parameter ``typed_data`` as both :class:`~starknet_py.utils.typed_data.TypedData` and :class:`~starknet_py.net.models.typed_data.TypedDataDict`
32+
3. :meth:`~starknet_py.net.signer.stark_curve_signer.KeyPair.from_keystore` has been added
33+
434
******************************
535
0.22.0 Migration guide
636
******************************

poetry.lock

Lines changed: 365 additions & 118 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "starknet-py"
3-
version = "0.22.0"
3+
version = "0.23.0"
44
description = "A python SDK for Starknet"
55
authors = ["Tomasz Rejowski <tomasz.rejowski@swmansion.com>", "Jakub Ptak <jakub.ptak@swmansion.com>"]
66
include = ["starknet_py", "starknet_py/utils/crypto/libcrypto_c_exports.*"]
@@ -17,17 +17,18 @@ documentation = "https://starknetpy.rtfd.io/"
1717
python = ">=3.8, <3.13"
1818
asgiref = "^3.4.1"
1919
marshmallow = "^3.15.0"
20-
marshmallow-oneofschema = "3.0.1"
20+
marshmallow-oneofschema = "3.1.1"
2121
typing-extensions = "^4.3.0"
22-
marshmallow-dataclass = "<8.7.0"
22+
marshmallow-dataclass = "<8.8.0"
2323
poseidon-py = "0.1.4"
2424
lark = "^1.1.5"
2525
aiohttp = "^3.8.4"
2626
sphinx = { version = ">=4.3.1,<8.0.0", optional = true }
27-
enum-tools = { extras = ["sphinx"], version = "0.11.0", optional = true }
28-
furo = { version = "^2023.5.20", optional = true }
27+
enum-tools = { extras = ["sphinx"], version = "0.12.0", optional = true }
28+
furo = { version = "^2024.5.6", optional = true }
2929
pycryptodome = "^3.17"
3030
crypto-cpp-py = "1.4.4"
31+
eth-keyfile = "^0.8.1"
3132

3233
[tool.poetry.extras]
3334
docs = ["sphinx", "enum-tools", "furo"]

starknet_py/abi/v0/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from marshmallow import Schema, fields
2-
from marshmallow_oneofschema import OneOfSchema
2+
from marshmallow_oneofschema.one_of_schema import OneOfSchema
33

44
from starknet_py.abi.v0.shape import (
55
CONSTRUCTOR_ENTRY,

starknet_py/abi/v1/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from marshmallow import Schema, fields
2-
from marshmallow_oneofschema import OneOfSchema
2+
from marshmallow_oneofschema.one_of_schema import OneOfSchema
33

44
from starknet_py.abi.v1.shape import (
55
ENUM_ENTRY,

starknet_py/abi/v2/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from marshmallow import Schema, fields
2-
from marshmallow_oneofschema import OneOfSchema
2+
from marshmallow_oneofschema.one_of_schema import OneOfSchema
33

44
from starknet_py.abi.v2.shape import (
55
CONSTRUCTOR_ENTRY,

starknet_py/contract.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def parsed_abi(self) -> Union[AbiV0, AbiV1, AbiV2]:
8080
return AbiParserV0(self.abi).parse()
8181

8282
@staticmethod
83-
def from_abi(address: int, abi: ABI, cairo_version: int = 0) -> ContractData:
83+
def from_abi(address: int, abi: ABI, cairo_version: int = 1) -> ContractData:
8484
"""
8585
Create ContractData from ABI.
8686
@@ -162,7 +162,7 @@ class DeclareResult(SentTransaction):
162162
"""
163163

164164
_account: BaseAccount = None # pyright: ignore
165-
_cairo_version: int = 0
165+
_cairo_version: int = 1
166166

167167
class_hash: int = None # pyright: ignore
168168
"""Class hash of the declared contract."""
@@ -524,7 +524,7 @@ def __init__(
524524
contract_data: ContractData,
525525
client: Client,
526526
account: Optional[BaseAccount],
527-
cairo_version: int = 0,
527+
cairo_version: int = 1,
528528
*,
529529
interface_name: Optional[str] = None,
530530
):
@@ -729,7 +729,7 @@ def __init__(
729729
abi: list,
730730
provider: Union[BaseAccount, Client],
731731
*,
732-
cairo_version: int = 0,
732+
cairo_version: int = 1,
733733
):
734734
"""
735735
Should be used instead of ``from_address`` when ABI is known statically.
@@ -1067,10 +1067,10 @@ def compute_address(
10671067
deployer_address: int = 0,
10681068
) -> int:
10691069
"""
1070-
Computes address for given contract.
1070+
Computes address for given Cairo 0 contract.
10711071
10721072
:param salt: int
1073-
:param compiled_contract: String containing compiled contract.
1073+
:param compiled_contract: String containing compiled Cairo 0 contract.
10741074
:param constructor_args: A ``list`` or ``dict`` of arguments for the constructor.
10751075
:param deployer_address: Address of the deployer (if not provided default 0 is used).
10761076
@@ -1079,7 +1079,9 @@ def compute_address(
10791079

10801080
compiled = create_compiled_contract(compiled_contract)
10811081
assert compiled.abi is not None
1082-
translated_args = translate_constructor_args(compiled.abi, constructor_args)
1082+
translated_args = translate_constructor_args(
1083+
compiled.abi, constructor_args, cairo_version=0
1084+
)
10831085
return compute_address(
10841086
salt=salt,
10851087
class_hash=compute_class_hash(compiled),
@@ -1105,7 +1107,7 @@ def _make_functions(
11051107
contract_data: ContractData,
11061108
client: Client,
11071109
account: Optional[BaseAccount],
1108-
cairo_version: int = 0,
1110+
cairo_version: int = 1,
11091111
) -> FunctionsRepository:
11101112
repository = {}
11111113
implemented_interfaces = [

starknet_py/hash/hash_method.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from enum import Enum
2+
from typing import List
3+
4+
from poseidon_py.poseidon_hash import poseidon_hash, poseidon_hash_many
5+
6+
from starknet_py.hash.utils import compute_hash_on_elements, pedersen_hash
7+
8+
9+
class HashMethod(Enum):
10+
"""
11+
Enum representing hash method.
12+
"""
13+
14+
PEDERSEN = "pedersen"
15+
POSEIDON = "poseidon"
16+
17+
def hash(self, left: int, right: int):
18+
if self == HashMethod.PEDERSEN:
19+
return pedersen_hash(left, right)
20+
if self == HashMethod.POSEIDON:
21+
return poseidon_hash(left, right)
22+
raise ValueError(f"Unsupported hash method: {self}.")
23+
24+
def hash_many(self, values: List[int]):
25+
if self == HashMethod.PEDERSEN:
26+
return compute_hash_on_elements(values)
27+
if self == HashMethod.POSEIDON:
28+
return poseidon_hash_many(values)
29+
raise ValueError(f"Unsupported hash method: {self}.")

starknet_py/net/account/account.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
InvokeV3,
3737
TypeAccountTransaction,
3838
)
39-
from starknet_py.net.models.typed_data import TypedData
39+
from starknet_py.net.models.typed_data import TypedDataDict
4040
from starknet_py.net.signer import BaseSigner
4141
from starknet_py.net.signer.stark_curve_signer import KeyPair, StarkCurveSigner
4242
from starknet_py.serialization.data_serializers.array_serializer import ArraySerializer
@@ -49,7 +49,7 @@
4949
)
5050
from starknet_py.utils.iterable import ensure_iterable
5151
from starknet_py.utils.sync import add_sync_methods
52-
from starknet_py.utils.typed_data import TypedData as TypedDataDataclass
52+
from starknet_py.utils.typed_data import TypedData
5353

5454

5555
@add_sync_methods
@@ -584,13 +584,18 @@ async def execute_v3(
584584
)
585585
return await self._client.send_transaction(execute_transaction)
586586

587-
def sign_message(self, typed_data: TypedData) -> List[int]:
588-
typed_data_dataclass = TypedDataDataclass.from_dict(typed_data)
587+
def sign_message(self, typed_data: Union[TypedData, TypedDataDict]) -> List[int]:
588+
if isinstance(typed_data, TypedData):
589+
return self.signer.sign_message(typed_data, self.address)
590+
typed_data_dataclass = TypedData.from_dict(typed_data)
589591
return self.signer.sign_message(typed_data_dataclass, self.address)
590592

591-
def verify_message(self, typed_data: TypedData, signature: List[int]) -> bool:
592-
typed_data_dataclass = TypedDataDataclass.from_dict(typed_data)
593-
message_hash = typed_data_dataclass.message_hash(account_address=self.address)
593+
def verify_message(
594+
self, typed_data: Union[TypedData, TypedDataDict], signature: List[int]
595+
) -> bool:
596+
if not isinstance(typed_data, TypedData):
597+
typed_data = TypedData.from_dict(typed_data)
598+
message_hash = typed_data.message_hash(account_address=self.address)
594599
return verify_message_signature(message_hash, signature, self.signer.public_key)
595600

596601
@staticmethod

starknet_py/net/account/base_account.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
InvokeV3,
2323
TypeAccountTransaction,
2424
)
25-
from starknet_py.net.models.typed_data import TypedData
25+
from starknet_py.net.models.typed_data import TypedDataDict
2626

2727

2828
class BaseAccount(ABC):
@@ -314,19 +314,21 @@ async def execute_v3(
314314
"""
315315

316316
@abstractmethod
317-
def sign_message(self, typed_data: TypedData) -> List[int]:
317+
def sign_message(self, typed_data: TypedDataDict) -> List[int]:
318318
"""
319319
Sign an TypedData TypedDict for off-chain usage with the Starknet private key and return the signature.
320320
This adds a message prefix, so it can't be interchanged with transactions.
321+
Both v0 and v1 domain revision versions are supported.
321322
322323
:param typed_data: TypedData TypedDict to be signed.
323324
:return: The signature of the TypedData TypedDict.
324325
"""
325326

326327
@abstractmethod
327-
def verify_message(self, typed_data: TypedData, signature: List[int]) -> bool:
328+
def verify_message(self, typed_data: TypedDataDict, signature: List[int]) -> bool:
328329
"""
329330
Verify a signature of a TypedData dict on Starknet.
331+
Both v0 and v1 domain revision versions are supported.
330332
331333
:param typed_data: TypedData TypedDict to be verified.
332334
:param signature: signature of the TypedData TypedDict.

starknet_py/net/models/typed_data.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@
22
TypedDict structures for TypedData
33
"""
44

5-
from typing import Any, Dict, List, TypedDict, Union
5+
from typing import Any, Dict, List, Optional, TypedDict
66

7+
from starknet_py.net.schemas.common import Revision
78

8-
class Parameter(TypedDict):
9+
10+
class ParameterDict(TypedDict):
911
"""
1012
TypedDict representing a Parameter object
1113
"""
1214

1315
name: str
1416
type: str
17+
contains: Optional[str]
1518

1619

17-
class StarkNetDomain(TypedDict):
20+
class DomainDict(TypedDict):
1821
"""
19-
TypedDict representing a StarkNetDomain object
22+
TypedDict representing a domain object (both StarkNetDomain, StarknetDomain).
2023
"""
2124

2225
name: str
2326
version: str
24-
chainId: Union[str, int]
27+
chainId: str
28+
revision: Optional[Revision]
2529

2630

27-
class TypedData(TypedDict):
31+
class TypedDataDict(TypedDict):
2832
"""
2933
TypedDict representing a TypedData object
3034
"""
3135

32-
types: Dict[str, List[Parameter]]
36+
types: Dict[str, List[ParameterDict]]
3337
primaryType: str
34-
domain: StarkNetDomain
38+
domain: DomainDict
3539
message: Dict[str, Any]
40+
41+
42+
class TypeContext(TypedDict):
43+
parent: str
44+
key: str

0 commit comments

Comments
 (0)