Skip to content

Commit aefc710

Browse files
Change KeyPair type from int to Hash (#1157)
* add string as a valid type in KeyPair constructor and .from_private_key method * fix assignment in constructor * add asserts to test * mention change in migration guide * review changes * fighting with codecov vol. 1 * CI check * ci chech * fix poetry version to 1.5.1
1 parent 0243f05 commit aefc710

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Install poetry
2525
run: |
2626
python -m pip install --upgrade pip
27-
pip install poetry
27+
pip install poetry==1.5.1
2828
poetry config installer.modern-installation false
2929
3030
- name: Set up Python 3.9

docs/migration_guide.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ This version of starknet.py brings support Starknet 0.12.1 and `RPC v0.4.0 <http
3939

4040
6. Added fields to dataclasses that previously were missing (e.g. ``contract_address_salt`` in :class:`DeployTransaction`).
4141

42-
.. currentmodule:; starknet_py.cairo.felt
42+
.. currentmodule:: starknet_py.cairo.felt
4343

4444
7. :func:`decode_shortstring` now is returned without ``\x00`` in front of the decoded string.
4545

46+
.. currentmodule:: starknet_py.net.signer.stark_curve_signer
47+
48+
8. :class:`KeyPair` and :meth:`KeyPair.from_private_key` now can accept keys in string representation.
4649

4750
0.18.0 Bugfixes
4851
---------------

starknet_py/net/signer/stark_curve_signer.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
compute_transaction_hash,
1212
)
1313
from starknet_py.hash.utils import message_signature, private_to_stark_key
14+
from starknet_py.net.client_models import Hash
1415
from starknet_py.net.models import AddressRepresentation, StarknetChainId, parse_address
1516
from starknet_py.net.models.transaction import (
1617
AccountTransaction,
@@ -28,8 +29,21 @@ class KeyPair:
2829
private_key: int
2930
public_key: int
3031

32+
def __init__(self, private_key: Hash, public_key: Hash):
33+
if isinstance(private_key, str):
34+
self.private_key = int(private_key, 0)
35+
else:
36+
self.private_key = private_key
37+
38+
if isinstance(public_key, str):
39+
self.public_key = int(public_key, 0)
40+
else:
41+
self.public_key = public_key
42+
3143
@staticmethod
32-
def from_private_key(key: int) -> "KeyPair":
44+
def from_private_key(key: Hash) -> "KeyPair":
45+
if isinstance(key, str):
46+
key = int(key, 0)
3347
return KeyPair(private_key=key, public_key=private_to_stark_key(key))
3448

3549

starknet_py/net/signer/test_stark_curve_signer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ def test_sign_transaction(transaction):
5757
assert len(signature) > 0
5858
assert all(isinstance(i, int) for i in signature)
5959
assert all(i != 0 for i in signature)
60+
61+
62+
def test_key_pair():
63+
key_pair = KeyPair(public_key="0x123", private_key="0x456")
64+
65+
assert isinstance(key_pair.public_key, int)
66+
assert isinstance(key_pair.private_key, int)

starknet_py/tests/e2e/docs/quickstart/test_creating_account.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ async def test_creating_account():
2424

2525
# There is another way of creating key_pair
2626
key_pair = KeyPair.from_private_key(key=123)
27+
# or
28+
key_pair = KeyPair.from_private_key(key="0x123")
2729

2830
# Instead of providing key_pair it is possible to specify a signer
2931
signer = StarkCurveSigner("0x1234", key_pair, StarknetChainId.TESTNET)

0 commit comments

Comments
 (0)