简体   繁体   中英

ETH ENS Web3 - How to get Registrant

I've following code snippet to get the " Controller " (The owner of the domain) but I need to get the " Registrant " of provided ENS name

const Web3 = require("web3")
const web3 = new Web3("https://cloudflare-eth.com");
 var ens = web3.eth.ens;


    var names = ['jtimberlake.eth', 'usman.eth'];

    (async () => {
        for (let domainName of names) {
           // console.log('checking: ' + domainName);
            const addr = await getDomain(domainName);
            console.log(addr);
        }
      
    })();


    async function getDomain(word) {
        try {
            const addr = await ens.getAddress(`${word}`)
           // console.log(addr);
            return addr;
        } catch (err) {
            console.error(err);
            return;
        }
    }

Can you please guide how I can get the " Registrant " of provided ENS name eg jtimberlake.eth

Web3 is a steaming pile. It doesn't do it with its methods. The registrant used to be called the deed owner, and the controller the owner. Now it is registrant and controller. That's why the method name makes no sense at all now in Web3.js - it never got updated, and never was useful for this in the first place.

The good news is there is a simple way. You can derive the token ID of the ENS domain from its name with the getRegistrant function below. https://docs.ens.domains/dapp-developer-guide/ens-as-nft

The name variable in the docs is superfluous and does nothing. You will need to instantiate ethersjs (npm install ethers) to get the ethers methods to work. You have to use this crazy number of functions because the token ID of an ENS domain/NFT is a uint256. JavaScript hates those natively.

The web3 method to find the controller also still works well if you ever need that. I suggest putting it in another function.

const getRegistrant = (domainName) => {
    const BigNumber = ethers.BigNumber
    const utils = ethers.utils
    const labelHash = utils.keccak256(utils.toUtf8Bytes(domainName))
    const derivedTokenId = BigNumber.from(labelHash).toString()
    //You need to instantiate the ENSRegistrarContract with its ABI and address. e.g. const ENSRegistrarContract = new web3.eth.Contract(ABI, ADDRESS)
    ENSRegistrarContract.methods.ownerOf(derivedTokenId).call()
        .then(function(registrant) {
            console.log(domainName + "is owned by: " + registrant)
            return registrant
        })
}

const getController = (domainName) => {
        //getOwner fetches the controller of a domain confusingly.
        web3.eth.ens.getOwner(domainName).then(function(controller) {
            console.log(domainName + "is controlled by: " + controller)
            return controller
        })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM