Overview
The eth_account module provides tools for working with Ethereum private keys and accounts. It enables key management, message signing, and transaction handling without requiring a connection to an Ethereum node.
Key Features
- Private key generation and management
- Message signing and verification
- Transaction signing
- Ethereum account creation
- Hierarchical Deterministic (HD) wallet support
Account Class
The Account class is the primary interface for Ethereum account operations.
Methods
create(extra_entropy='')
Creates a new private key and returns it as a LocalAccount object.
Parameters:
extra_entropy(str/bytes/int): Additional randomness for key generation
Example:
from eth_account import Account
acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
print(acct.address) # Returns the Ethereum addresscreate_with_mnemonic()
Generates a new private key with a mnemonic phrase for recovery.
Example:
Account.enable_unaudited_hdwallet_features()
acct, mnemonic = Account.create_with_mnemonic()decrypt(keyfile_json, password)
Decrypts an encrypted private key.
encrypt(private_key, password)
Encrypts a private key for secure storage.
from_key(private_key)
Creates an account object from an existing private key.
from_mnemonic(mnemonic)
Generates an account from a mnemonic phrase.
recover_message()
Recovers the address from a signed message.
recover_transaction()
Recovers the address from a signed transaction.
sign_message()
Signs a message with a private key.
sign_transaction()
Signs a transaction with a private key.
sign_typed_data()
Signs EIP-712 typed data.
Security Considerations
- Important: Never share your private keys
- Use strong passwords for encrypted keys
- Store mnemonics securely
- Enable HD wallet features cautiously as they're experimental
Usage Examples
Creating an Account
from eth_account import Account
account = Account.create()
print(f"Address: {account.address}")
print(f"Private Key: {account.key.hex()}")Signing a Message
from eth_account.messages import encode_defunct
message = encode_defunct(text="Hello Ethereum")
signed = Account.sign_message(message, private_key)Signing a Transaction
tx = {
'to': recipient_address,
'value': amount_in_wei,
'gas': 21000,
'gasPrice': gas_price,
'nonce': nonce,
'chainId': chain_id
}
signed_tx = Account.sign_transaction(tx, private_key)Frequently Asked Questions
How do I securely store my private key?
๐ Learn about secure key storage best practices
What's the difference between a private key and a mnemonic?
A private key is a single cryptographic key, while a mnemonic is a human-readable phrase that can generate multiple private keys.
Can I use the same account on different chains?
Yes, but be aware that the same address may have different balances and states on different chains.
How do I recover an account from a mnemonic?
account = Account.from_mnemonic("your mnemonic phrase here")What's the safest way to handle private keys?
๐ Explore hardware wallet options
Best Practices
- Always back up your private keys/mnemonics securely
- Use hardware wallets for large amounts
- Double-check addresses before sending transactions
- Keep your software updated
- Be cautious of phishing attempts
Conclusion
The eth_account library provides comprehensive tools for Ethereum account management. By following security best practices and understanding the available methods, developers can safely integrate Ethereum accounts into their applications.
This version:
1. Organizes content logically with clear headings
2. Includes relevant code examples
3. Adds an FAQ section
4. Incorporates SEO-friendly keywords naturally
5. Removes unnecessary technical details while preserving core functionality
6. Adds engaging anchor text as specified