Search code examples
pythonpython-3.xweb3py

How to create a Web3py account using mnemonic phrase


I'm making my own desktop BSC wallet with web3. At the moment I'm using

private_key = "private key"
account = w3.eth.account.privateKeyToAccount(private_key)

But I want to create the account using a mnemonic phrase like "hello john pizza guitar". I have been searching but I can't manage to achieve it.


Solution

  • At this moment the requested feature is not stable inside Web3.py

    • Option 1: Use some library like Ethereum Mnemonic Utils to handle your seed.
    • Option 2: Enable unaudited features in web3py
    web3 = Web3()
    web3.eth.account.enable_unaudited_hdwallet_features()
    account = web3.eth.account.from_mnemonic(my_mnemonic, account_path="m/44'/60'/0'/0/0")
    
    

    Note: The default account_path matches Ethereum and BSC as well. Iterating the last number you would get the next accounts. The account object could manage some operations

    account.address # The address for the chosen path
    account.key # Private key for that address