In this tutorial, we will create an Ethereum wallet address using Python and also return our private key and mnemonic phrase.
Mnemonic Phrase
A mnemonic phrase or a seed phrase is a group of words, that gets generated when a new wallet has been created. The mnemonic phrase can be used to restore or gain access to your wallet. So treat this phrase as you would treat passwords and keep them safe.
Dependencies
First, we will install our Python packages required for creating an ethereum wallet:
pip install eth_keys
pip install eth-hash[pycryptodome]
pip install bip-utils
pip install mnemonic
Create a Wallet
We will use Python to create the wallet and return the following:
Ethereum Address
Mnemonic Phrase
Private Key
Please note that the mnemonic phrase and private key should be stored safely as if someone gains access to this, they can gain access to your wallet.
from mnemonic import Mnemonic
from bip_utils import Bip39SeedGenerator, Bip44Coins, Bip44, Bip44Changes
# Define a dictionary where we will store our values
body = {}
# Generate a random mnemonic in english
mnemo = Mnemonic("english")
mnemonic = mnemo.generate(256)
body['mnemonic'] = mnemonic
# Generate the seed from the mnemonic
seed = Bip39SeedGenerator(mnemonic).Generate()
# Generate the Bip44 wallet (Ethereum)
wallet = Bip44.FromSeed(seed, Bip44Coins.ETHEREUM)
# Get the first account
account = wallet.Purpose().Coin().Account(0).Change(Bip44Changes.CHAIN_EXT).AddressIndex(0)
# Extract the private key from the account
private_key = account.PrivateKey().Raw().ToHex()
body['private_key'] = private_key
# Extract the address from the account
address = account.PublicKey().ToAddress()
body['address'] = address
# Print the stored values
print(body)
In my example, I am defining this code in create_ethereum_wallet.py
and running it:
python3 create_ethereum_wallet.py
The following content gets returned:
{
"mnemonic": "something foobar foobar foobar purple foobar focus foobar lion foobar bag foobar foobar foobar security foobar foobar ladder foobar foobar foobar foobar foobar where",
"private_key": "0000000000000000000000000000000000000000000000000000000000000000",
"address": "0xC35f7DCe60e3f0C22c5E55eCAdDF917Ee4576891"
}
I have mixed up some of the output for security reasons, but we can see that the account exists:
Retrieve the Private Key from Mnemonic
We can also retrieve the private key and address when we provide the mnemonic seed phrase:
from mnemonic import Mnemonic
from bip_utils import Bip39SeedGenerator, Bip44Coins, Bip44, Bip44Changes
# Set your mnemonic seed phrase
mnemonic_phrase = "something foobar foobar foobar purple foobar focus foobar lion foobar bag foobar foobar foobar security foobar foobar ladder foobar foobar foobar foobar foobar where"
# Define a dictionary where we will store our values
body = {}
# Generate seed from mnemonic
seed = Bip39SeedGenerator(mnemonic).Generate()
# Create a Bip44 wallet
wallet = Bip44.FromSeed(seed, Bip44Coins.ETHEREUM)
# Get the first account
account = wallet.Purpose().Coin().Account(0).Change(Bip44Changes.CHAIN_EXT).AddressIndex(0)
# Extract the private key from the account
private_key = account.PrivateKey().Raw().ToHex()
body['private_key'] = private_key
# Extract the address from the account
address = account.PublicKey().ToAddress()
body['address'] = address
# Print the stored values
print(body)
Let's say we store this code to recover_ethereum_address.py
and we run it:
python3 recover_ethereum_address.py
Then the response should be something like:
{
"private_key": "0000000000000000000000000000000000000000000000000000000000000000",
"address": "0xC35f7DCe60e3f0C22c5E55eCAdDF917Ee4576891"
}
Thank You
Thanks for reading, feel free to check out my website, feel free to subscribe to my newsletter or follow me at @ruanbekker on Twitter.