How to get the address and all the information about an ENS name Ethers JS
In this tutorial, we are going to learn how to get the address of an ENS name and other information like the avatar, addresses from other blockchains, other the email using Ethers JS.
In this tutorial, we are going to learn how to get the address of an ENS name and other information like the avatar, addresses from other blockchains, other the email using Ethers JS.
ENS names are getting more and more used and they associate a unique name that ends with .eth
to an address and information about a person like their email, their addresses on other blockchains, their socials, a website URL and other things.
Here is the code to get the address of an ENS name:
const ethers = require("ethers")
const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE)
const ensName = "ricmoo.eth" // example in the Ethers docs
const address = await provider.resolveName(ensName)
To get the address associated to an ENS name, we use the resolveName
function and pass the ENS name in the parameters.
It returns a promise and when that promise resolves, it returns the address associated with that name or null
if it was not found or if the name doesn't exist.
How to get the ENS name of an address
If you want to do the opposite and check what ENS name is associated with an address, you can use lookupAddress
:
const address = "0x5555763613a12D8F3e73be831DFf8598089d3dCa"
const name = await provider.lookupAddress(address)
// name will be "ricmoo.eth"
How to get all the information available about an ENS name
If you want more information about an ENS name, you can get the resolver for that name and use that resolver to get other kinds of information.
To do that, we use the getResolver
function:
const ensName = "ricmoo.eth"
const resolver = await provider.getResolver(ensName);
That function takes in parameter an ENS name and returns a Promise that resolves to an EnsResolver
object.
The resolver
has 2 properties, name
which is the ENS name, and address
for the address associated with the ENS name.
The EnsResolver
gives you the following functions:
getAvatar
to get the URL of the avatar. It can either be an avatar the owner has set or an NFT that is owned by the address of that ENS name.
The returned object has 2 properties,url
andlinkage
which is an array that explains how the avatar was resolvedgetAddress
to get the address of the ENS name for the blockchain passed in parameters (Ethereum by default).
In the parameters you can pass the blockchain of the address you want to get and that parameter is a number from this list: https://eips.ethereum.org/EIPS/eip-2304#address-encodinggetText
to get the personal information of an ENS name that the owner decided to share. It takes in parameter a key and it returns the data for that key ornull
if there is no data.
For example, if you pass"email"
it will return the email of the ENS name if the owner decided to share it. You can also pass"com.github"
to get the Github profile of an ENS name or"com.twitter"
to get the Twitter account.
The list of keys you can pass is here: https://eips.ethereum.org/EIPS/eip-634#specificationgetContentHash
to get the hash of the content that the owner is hosting on a protocol like IPFS or Swarm. More information about that here: https://eips.ethereum.org/EIPS/eip-1577
All of these functions are asynchronous and return promises that contain the data you want when they resolve and if the data is not found or doesn't exist, it returns null
.
And that's it 🎉
Thank you for reading this article