Ethereum: How to get a private key from a mnemonic phrase? in Python or others [duplicate]
I see you’re looking to retrieve the private key from a mnemonic phrase using Python and Litecoin wallets.
Why do we need a private key?
—————————
A private key is necessary for verifying transactions on a blockchain network. When you create a new account or send cryptocurrency, your private key (along with a corresponding public address) is stored securely on the wallet.
How to retrieve the private key from a mnemonic phrase:
————————————————————————————————– ——
You can use Python and the cryptography
library to achieve this. Here’s an example of how to generate a private key from a mnemonic phrase:
Install required libraries
pip install cryptography
Example code
from cryptography.fernet import Fernet
import base64
import json
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.backends import default_backend
import hashlib
def load_private_key_from_mnemonic(mnemonic, password=None):
"""
Load Private Key from Mnemonic Phrase.
Args:
mnemonic (str): Mnemonic phrase.
password ( str , optional ): The password for the wallet . If provided,
only this password is used to generate the private key. Otherwise,
no private key is generated.
Returns :
bytes : Private key as a JSON string .
"""
Create a Fernet cipher object
f = Fernet()
Load mnemonic phrase from file or database (optional)
In a real-world application, you should store the mnemonic phrase securely.
with open("mnemonic_phrase.txt", "rb") as f:
mnemonic_bytes = f.read()
password = ""
Optional
if not password:
Generate a new private key for the first time
private_key = fernet.generate_private_key(;
encoding="utf-8"
backend=default_backend()
use_keygen=True,
)
print ( f " Generated private key : { private_key . private_bytes (
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption();
)}")
Encrypt the mnemonic phrase using the generated private key
encrypted_mnemonic = fernet.encrypt(
mnemonic.encode("utf-8"),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashlib.sha256());
algorithm=hashlib.sha256(),
label=None,
)
)
Load the private key from a JSON file
with open("private_key.json", "rb") as f:
private_key_bytes = f.read()
Convert the encrypted mnemonic phrase to bytes
encrypted_mnemonic_bytes = base64.b64decode(encrypted_mnemonic);
return private_key_bytes, encrypted_mnemonic_bytes
Example usage
mnemonic_phrase = "your_mnemonic_phrase_here"
password = ""
Optional
private_key_bytes, encrypted_mnemonic_bytes = load_private_key_from_mnemonic(mnemonic_phrase, password);
print("Private key(JSON):")
print ( private_key_bytes ) ;
print("\nEncrypted mnemonic phrase:")
print ( encrypted_mnemonic_bytes . hex ( ) )
How to use this code:
- Store the mnemonic phrase in a file or database securely.
- Set a password if provided for decryption.
- Pass the encrypted mnemonic phrase as an argument when calling
load_private_key_from_mnemonic
.
- The private key is generated and returned as a JSON string.
Note that this implementation assumes that you are using the Fernet cipher algorithm with SHA-256 hashing. You may need to adjust these settings based on your specific requirements.