Skip to content

Commit c2fb842

Browse files
Add keypair docs (#1482)
* Add keypair docs * Fix typo * Update docs/guide/generating_key_pair.rst Co-authored-by: Franciszek Job <54181625+franciszekjob@users.noreply.github.com> * Update docs/guide/generating_key_pair.rst Co-authored-by: Franciszek Job <54181625+franciszekjob@users.noreply.github.com> * Update docs/guide/generating_key_pair.rst Co-authored-by: Franciszek Job <54181625+franciszekjob@users.noreply.github.com> * Update docs/guide/generating_key_pair.rst Co-authored-by: Franciszek Job <54181625+franciszekjob@users.noreply.github.com> * Fix method imports * Add link do eth keystore file docs --------- Co-authored-by: Franciszek Job <54181625+franciszekjob@users.noreply.github.com>
1 parent 9c34c01 commit c2fb842

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

docs/guide.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Guide
99
guide/deploying_contracts
1010
guide/serialization
1111
guide/signing
12+
guide/generating_key_pair

docs/guide/generating_key_pair.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Generating a Key pair
2+
=====================
3+
4+
Key pair
5+
--------
6+
7+
The Key pair is a pair of private and public keys. The private key is used to sign transactions, and the public key is used to verify the signature.
8+
In the starknet.py you need to use the :class:`~starknet_py.net.signer.key_pair.KeyPair` class to be able to create an :class:`~starknet_py.net.account.account.Account` and :class:`~starknet_py.net.signer.stark_curve_signer.StarkCurveSigner` object.
9+
10+
Generating random key pair
11+
--------------------------
12+
13+
Method :meth:`~starknet_py.net.signer.stark_curve_signer.KeyPair.generate` allows to generate cryptographically strong pseudo-random numbers
14+
suitable for managing secrets such as account authentication, tokens, and similar.
15+
16+
.. codesnippet:: ../../starknet_py/tests/e2e/docs/guide/test_key_pair.py
17+
:language: python
18+
:dedent: 4
19+
:start-after: docs-generate: start
20+
:end-before: docs-generate: end
21+
22+
23+
Creating key pair from private Key
24+
----------------------------------
25+
26+
To create a key pair from a private key, use the :meth:`~starknet_py.net.signer.stark_curve_signer.KeyPair.from_private_key` method.
27+
28+
29+
.. codesnippet:: ../../starknet_py/tests/e2e/docs/guide/test_key_pair.py
30+
:language: python
31+
:dedent: 4
32+
:start-after: docs-from-private-key: start
33+
:end-before: docs-from-private-key: end
34+
35+
Reading key pair from keystore file
36+
-----------------------------------
37+
38+
Using :meth:`~starknet_py.net.signer.stark_curve_signer.KeyPair.from_keystore` method there is possibility to import a key pair from a keystore file.
39+
The keystore file should follow the `Ethereum keystore <https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition#scrypt>`_ format.
40+
41+
.. codesnippet:: ../../starknet_py/tests/e2e/docs/guide/test_key_pair.py
42+
:language: python
43+
:dedent: 4
44+
:start-after: docs-from-keystore: start
45+
:end-before: docs-from-keystore: end
46+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# pylint: disable=import-outside-toplevel
2+
import pytest
3+
4+
from starknet_py.constants import FIELD_PRIME
5+
6+
7+
def test_generate_key_pair():
8+
# docs-generate: start
9+
from starknet_py.net.signer.key_pair import KeyPair
10+
11+
key_pair = KeyPair.generate()
12+
# docs-generate: end
13+
14+
hex_private_key = hex(key_pair.private_key)[2:]
15+
padded_hex_private_key = hex_private_key.zfill(64)
16+
assert len(padded_hex_private_key) == 64
17+
assert key_pair.private_key < FIELD_PRIME
18+
assert key_pair.public_key < FIELD_PRIME
19+
20+
21+
def test_key_pair_from_private_key():
22+
# docs-from-private-key: start
23+
from starknet_py.net.signer.key_pair import KeyPair
24+
25+
key_pair = KeyPair.from_private_key("0x1234abcd")
26+
# docs-from-private-key: end
27+
28+
assert isinstance(key_pair.public_key, int)
29+
assert isinstance(key_pair.private_key, int)
30+
31+
32+
@pytest.mark.skip(reason="This test requires a keystore file")
33+
def test_key_pair_from_keystore():
34+
# docs-from-keystore: start
35+
from starknet_py.net.signer.key_pair import KeyPair
36+
37+
key_pair = KeyPair.from_keystore("path/to/keystore", "password")
38+
# docs-from-keystore: end
39+
40+
assert isinstance(key_pair.public_key, int)
41+
assert isinstance(key_pair.private_key, int)

0 commit comments

Comments
 (0)