generate_wallet

Function

def generate_wallet(amount):
    addresses = []
    for i in list(range(0, amount)):
        account = Web3().eth.account.create()
        keystore = Web3().eth.account.encrypt(account.key.hex(), os.getenv('SECRET'))
        folder = "./data/wallets/{}".format(account.address)
        os.makedirs("data/wallets", exist_ok=True)
        os.makedirs(folder, exist_ok=True)
        open("{}/keystore".format(folder), 'w').write(json.dumps(keystore, indent=4))
        addresses.append(account)
    return addresses

Description

  • Prepare an empty list of accounts

  • Loop based on desired amount of wallets to generate

  • Create a new account/wallet

  • Encrypt the keystore for based on the SECRET found in “.env”

  • Set the wallet folder for the public key

  • Create the parent wallet folder if it doesn’t exist

  • Create the public key folder if it doesn’t exist

  • Save the account’s keystore

  • Add the account to the list

  • Return the list of accounts