Skip to content

Commit 5cf1fb7

Browse files
miohtamawolovim
andauthored
An example how to manage local private key using env (#2380)
* An example how to manage local private key using env How to manage private keys using environment variable, how to generate them, and some security warnings. * Fix extra period Inserted by the editor? * add content tweaks and newsfragment Co-authored-by: Marc Garreau <[email protected]>
1 parent 81427e6 commit 5cf1fb7

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

docs/web3.eth.account.rst

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Hosted Private Key
3939
not your Ether" in the wise words of Andreas Antonopoulos.
4040

4141
Some Common Uses for Local Private Keys
42-
-------------------------------------------
42+
---------------------------------------
4343

4444
A very common reason to work with local private keys is to interact
4545
with a hosted node.
@@ -55,6 +55,59 @@ 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+
-----------------------------------------------
60+
61+
In this example we pass the private key to our Python application in an
62+
`environment variable <https://en.wikipedia.org/wiki/Environment_variable>`_.
63+
This private key is then added to the transaction signing keychain
64+
with ``Signing`` middleware.
65+
66+
If unfamiliar, note that you can `export your private keys from Metamask and other wallets <https://metamask.zendesk.com/hc/en-us/articles/360015289632-How-to-Export-an-Account-Private-Key>`_.
67+
68+
.. warning ::
69+
70+
- **Never** share your private keys.
71+
- **Never** put your private keys in source code.
72+
- **Never** commit private keys to a Git repository.
73+
74+
Example ``account_test_script.py``
75+
76+
.. code-block:: python
77+
78+
import os
79+
from eth_account import Account
80+
from eth_account.signers.local import LocalAccount
81+
from web3.auto import w3
82+
from web3.middleware import construct_sign_and_send_raw_middleware
83+
84+
private_key = os.environ.get("PRIVATE_KEY")
85+
assert private_key is not None, "You must set PRIVATE_KEY environment variable"
86+
assert private_key.startswith("0x"), "Private key must start with 0x hex prefix"
87+
88+
account: LocalAccount = Account.from_key(private_key)
89+
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(account))
90+
91+
print(f"Your hot wallet address is {account.address}")
92+
93+
Example how to run this in UNIX shell:
94+
95+
.. code-block:: shell
96+
97+
# Generate a new 256-bit random integer using openssl UNIX command that acts as a private key.
98+
# You can also do:
99+
# python -c "from web3 import Web3; w3 = Web3(); acc = w3.eth.account.create(); print(f'private key={w3.toHex(acc.key)}, account={acc.address}')"
100+
# Store this in a safe place, like in your password manager.
101+
export PRIVATE_KEY=0x`openssl rand -hex 32`
102+
103+
# Run our script
104+
python account_test_script.py
105+
106+
This will print::
107+
108+
Your hot wallet address is 0x27C8F899bb69E1501BBB96d09d7477a2a7518918
109+
110+
58111
.. _extract_geth_pk:
59112

60113
Extract private key from geth keyfile

newsfragments/2380.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document reading private keys from environment variables

0 commit comments

Comments
 (0)