Web3.py Patterns: Address Mining

Each new Ethereum address you create is a long jumbled string of letters and numbers preceded by 0x – until it's not!

You may have seen some vanity addresses in wild, i.e., an Ethereum address that spells out a word or has many repeating characters. The way these are created is dead simple: brute force.

Generating a wallet is free, requires no interaction with the blockchain, and can even be done offline. If you want a custom Ethereum address, just generate new ones until your desired pattern shows up! We call this "address mining."

A quick Python example to mine an address starting with 0xabc:

from web3 import Web3

w3 = Web3()

while True:
   acct = w3.eth.account.create()
   if acct.address.startswith("0xabc"):
      print(f"public address: {acct.address}")
      print(f"private key: {acct.key.hex()}")

# public address: 0xabc63486f9...
# private key: 0xb3138d6b67...

That's all there is to it. Once you have the private key, you can import that into MetaMask or your wallet of choice and you should see the public address match the one your script printed out.

A couple of notes before you run off and mine some addresses:

  • It would be very straightforward to publish or use a vanity address tool, but I discourage it for security reasons; it would also be straightforward for a bad actor to send off any successfully mined keys to a private server. Just write the script yourself. 🤝
  • The create function we used accepts input for use as extra entropy, i.e., extra randomness to make your account more difficult to reproduce. Add some unique input in there and treat it like a password. For example: w3.eth.account.create("4%das$adA1r28hlnk"). 🔒
  • Ethereum addresses are hexadecimal numbers, meaning they include letters a through f and numbers 0 through 9. Don't go mining for a g. 😉
  • Note that the more characters you want to mine for, the longer it will take – by an order of magnitude! More than a few characters and you may be waiting for days to randomly generate that sequence. ⏰

Happy mining!