web3 get name from address

Viewed 1059
    import Web3 from "web3";

    
    useEffect(() => {
    const web3 = new Web3(window.ethereum);

    web3.eth.ens.getAddress("ethereum.eth").then(function (address) {
        console.log(address);
    });
    }, []);

here using this code i am getting address from ether name is there any way i can get name from address

    web3.eth.ens.getOwner("0xc74E8eFaFE54481bD109f97422AeBca607499f57").then(function (address) {
        console.log(address);
    });

I am trying above piece of code but it is not working if i input 0xc74E8eFaFE54481bD109f97422AeBca607499f57 i should get ethereum.eth please take a look how can i get this

1 Answers

As you can see from the ENS documentation, web3.js can't performed "reversed resolution", i.e. getting a ENS name from an address. However, you can do this from the ensjs API like this:

const address = '0xc74E8eFaFE54481bD109f97422AeBca607499f57';
var name = await ens.getName(address)
// Check to be sure the reverse record is correct.
if(address != await ens.name(name).getAddress()) {
  name = null;
}
Related