Skip to content

Commit 87b4b19

Browse files
miohtamafselmo
authored andcommitted
Explain private keys and account funding
- Some basics about private keys and using ETH as a gas fee. This information is unknown to many new developers. - Newsfragment.
1 parent 9f08272 commit 87b4b19

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

docs/web3.eth.account.rst

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,55 @@ Using private keys usually involves ``w3.eth.account`` in one way or another. Re
5555
or see a full list of things you can do in the docs for
5656
:class:`eth_account.Account <eth_account.account.Account>`.
5757

58-
Read a private key from an environment variable
59-
-----------------------------------------------
58+
Creating a Private Key
59+
----------------------
60+
61+
Each Ethereum address has a matchign private key. To create a new Ethereum
62+
account you can just generate a random number that acts as a private key.
63+
64+
- A private key is just a random unguessable, or cryptograpgically safe, 256-bit integer number
65+
66+
- A valid private key is > 0 and < max private key value (a number above the elliptic curve order FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141)
67+
68+
- Private keys do not have checksums or anything like that
69+
70+
To create a private key using Web3.py and command line you can do:
71+
72+
```shell
73+
python -c "from web3 import Web3; w3 = Web3(); acc = w3.eth.account.create(); print(f'private key={w3.toHex(acc.privateKey)}, account={acc.address}')"
74+
```
75+
76+
Which outputs a new private key and an account pair:
77+
78+
```
79+
private key=0x480c4aec9fa..., account=0x9202a9d5D2d129CB400a40e00aC822a53ED81167
80+
```
81+
82+
- *Never store private key with your source*. Use environment variables
83+
to store the key. Read more below.
84+
85+
- You can also import the raw hex private key to MetaMask and any other
86+
wallet - the private key can be shared between your Python code
87+
and any number of wallets.
88+
89+
Funding a New Account
90+
---------------------
91+
92+
If you create a private key it comes with its own Ethereum address.
93+
By default, the balance of this address is zero.
94+
Before you can send any transactions with your account,
95+
you need to top up.
96+
97+
- For a local test environment, any environment is boostrapped with accounts that have ETH on them. Move
98+
ETH from default accounts to your newly created account.
99+
100+
- For public mainnet, you need to buy ETH in a cryptocurrency exchange
101+
102+
- For a testnet, you need to [use a testnet faucet](https://faucet.paradigm.xyz/)
103+
104+
105+
Reading a Private Key from an Environment Variable
106+
--------------------------------------------------
60107

61108
In this example we pass the private key to our Python application in an
62109
`environment variable <https://en.wikipedia.org/wiki/Environment_variable>`_.

newsfragments/2722.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document improvements for private key info and account funding.

0 commit comments

Comments
 (0)