简体   繁体   English

ETH ENS Web3 - 如何获得注册人

[英]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我已经按照代码片段获取“控制器”(域的所有者),但我需要获取提供的 ENS 名称的“注册人”

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您能否指导我如何获得提供的 ENS 名称的“注册人”,例如 jtimberlake.eth

Web3 is a steaming pile. Web3 是一堆热气腾腾的东西。 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.这就是为什么方法名称现在在 Web3.js 中完全没有意义的原因——它从未更新过,而且一开始就没有用处。

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.您可以使用下面的 getRegistrant 函数从其名称中获取 ENS 域的令牌 ID。 https://docs.ens.domains/dapp-developer-guide/ens-as-nft https://docs.ens.domains/dapp-developer-guide/ens-as-nft

The name variable in the docs is superfluous and does nothing.文档中的 name 变量是多余的,什么都不做。 You will need to instantiate ethersjs (npm install ethers) to get the ethers methods to work.您将需要实例化 ethersjs (npm install ethers) 以使 ethers 方法工作。 You have to use this crazy number of functions because the token ID of an ENS domain/NFT is a uint256.你必须使用这么多的函数,因为 ENS 域/NFT 的令牌 ID 是 uint256。 JavaScript hates those natively. JavaScript 本身就讨厌这些。

The web3 method to find the controller also still works well if you ever need that.如果您需要,查找控制器的 web3 方法也仍然可以正常工作。 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
        })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web3 不将 web3.eth.Contract 视为构造函数 - Web3 not treating web3.eth.Contract as a contructor 如何实现智能合约添加eth地址记录以在solidity中引用特定的ENS子域名 - How to implement smart contract to add eth address record to refer the specific ENS subdomain name in solidity 如何使用 ensjs 获取 ens 用户名 - How to get ens username using ensjs 如何使用 web3 获取钱包中的所有地址? - how i can get all the addresses in wallet using web3? 如何使用 web3 js 获取交易明细中的代币转移? - How to get tokens transferred in transaction details using web3 js? 我们如何使用Nodejs从web3获取数据 - How can we get Data from web3 using Nodejs Web3 1.0:创建合同后,`web3.eth.call(tx)`返回什么? - Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation? MetaMask Web3 object 不支持没有回调参数的同步方法,例如 eth_sendTransaction - The MetaMask Web3 object does not support synchronous methods like eth_sendTransaction without a callback parameter 如何获取登录表单以接受已成功提交的新注册用户 ID 和密码? - How to get a login form to accept a new registrant userid and password that was submitted successfully? 如何在反应中使用 webpack 和 web3? - how to use webpack and web3 in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM