ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

Add utility to get ENS name owner

Open eggplantzzz opened this issue 3 years ago • 2 comments

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

eggplantzzz avatar Jul 06 '22 15:07 eggplantzzz

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?

ricmoo avatar Jul 14 '22 01:07 ricmoo

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.

eggplantzzz avatar Jul 14 '22 15:07 eggplantzzz