ethers.js
ethers.js copied to clipboard
Add utility to get ENS name owner
Describe the Feature
I would like an ENS utility to find the owner of a specified ENS name.
Code Example
provider.getEnsNameOwner("my.ens.name");
// -> 0x1234567890123456789012345678901234567890
This is not something that is commonly needed, so rather than adding an API for it (I try to keep the API concise and for common operations, so that it doesn't get too bloated), I think a recipe in the documentation might make more sense.
For now, it can be accomplished using:
async function getOwner(provider, name) {
const ensAddr = (await provider.getNetwork()).ensAddress;
const ensAbi = [ "function owner(bytes32) view returns (address)" ];
const contract = new ethers.Contract(ensAddr, ensAbi, provider);
const addr = await contract.owner(ethers.utils.namehash(name));
if (addr === ethers.constants.AddressZero) { return null; }
return addr;
}
// Example usage:
console.log(await getOwner(provider, "ricmoo.eth"));
// "0x5555763613a12D8F3e73be831DFf8598089d3dCa"
console.log(await getOwner(provider, "ricmoo2.eth"));
// null
Does that make sense?
Thanks @ricmoo! I help maintain a tool that checks to see whether a name is owned by an address before setting the resolver. Thanks for the workaround.