Get the wallet address from an ENS domain name

Get the wallet address from an ENS domain name

ยท

2 min read

Intro

A simple Python script retrieving the wallet address from an ENS domain name. In this example we're going to use the web3.py library. Let's go!

I just want the code

OK, if you just want the code, grab it here or ๐Ÿ‘‡

from web3 import Web3
from ens import ENS

rpc_url = "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"

# The provider is your connection to a blockchain
web3 = Web3(Web3.HTTPProvider(rpc_url))

#log if we're connected or not
print(web3.isConnected())

ns = ENS.fromWeb3(web3)

print(ns.address(input("enter your ens domain to get your wallet address\n")))

Let's build

All you need for this script is a virtual environment and the web3.py library.

Setup

To fire up a virtual environment:

python -m env ./myv

and to activate the environment:

source ./myv/bin/activate

Then go ahead and install the web3.py library:

pip install web3

Finally create a Python file to code your program:

touch get_ens_addi.py

Code

There are only 7 lines of code and I probably could've done it in 5 ๐Ÿ˜ถ....just saying.

OK, let's import the packages. Note there are 2 packages, but we don't need a separate pip install for the second one.

from web3 import Web3
from ens import ENS

Setup an RPC URL to define which blockchain we want to target and which node provider we want to use. Here I'm picking the Ethereum Mainnet and I'm using an Infura node.

rpc_url = "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"

Now let's establish our connection:

web3 = Web3(Web3.HTTPProvider(rpc_url))

We can check with the following line if we're connected properly:

print(web3.isConnected())

Almost done. We instantiate an object ns and get access to all the methods inside the ENS package.

ns = ENS.fromWeb3(web3)

Finally we can use the address method to retrieve the wallet address from an ENS domain. Over here we ask the user to enter their ENS domain and then the program returns the wallet address.

print(ns.address(input("enter your ens domain to get your wallet address\n")))

That's it!

There you have it. A simple Python program to get the wallet address from an ENS domain. This library has a ton more methods. I'll be sharing some other scripts, but you can check out the web3.py docs for more info!

ย