1
2
3
4
5
6
7
# generate mnemonic code
from eth_account import Account
Account.enable_unaudited_hdwallet_features()
def gen_mnemonic_code() -> str:
_, mnemonic = Account.create_with_mnemonic()
return mnemonic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# generate private key from mnemonic code
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_account.hdaccount import ETHEREUM_DEFAULT_PATH
rpc_url = 'https://mainnet.infura.io/v3/<your-infura-key>'
w3 = Web3(Web3.HTTPProvider(rpc_url))
w3.eth.account.enable_unaudited_hdwallet_features()
def from_mnemonic(mnemonic: str, idx: int = 0) -> LocalAccount:
account: LocalAccount = w3.eth.account.from_mnemonic(
mnemonic,
account_path=f'{ETHEREUM_DEFAULT_PATH}/{idx}'
)
return account