关于ENS
以太坊域名服务(ENS)类似于DNS。但它通过一种更安全和且去中心化的方式来管理网站域名,而且可以通过它发送ETH和ERC-20代币。
ENS注册表记录所有域和子域,以及所有者的详细信息和指向解析器的链接;而解析器是另一个智能合约,处理从名称到地址或其他类型资源的转换,反之亦然。
ENS的运行类似于互联网的DNS,因为它有一个分级系统,允许域名所有者完全控制任何子域。
web3.eth.ens文档地址:https://web3js.readthedocs.io/en/v1.5.2/web3-eth-ens.html#eth-ens
ENS文档地址:https://docs.ens.domains/
ENS开源地址:https://github.com/ensdomains
ens-subgraph开源地址:https://github.com/graphprotocol/ens-subgraph
合约分布
ETHRegistrarController
DefaultReverseResolver
PublicResolver
ReverseRegistrar
BaseRegistrarImplementation
ENSRegistryWithFallback
BulkRenewal
工具类代码块
const ETHRegistrarControllerContract = new web3.eth.Contract(
ETHRegistrarControllerAbi,
ETHRegistrarControllerAddress
)
const ReverseRegistrarContract = new web3.eth.Contract(
ReverseRegistraAbi,
ReverseRegistraAddress
)
randomString(length) {
var str =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = "";
for (var i = length; i > 0; --i)
result += str[Math.floor(Math.random() * str.length)];
return result;
}
获取域名价格
const name = "xxx.xyz".split('.')[0]//只取域名前缀xxx
ETHRegistrarControllercontract.methods
.rentPrice(name, 31536000)//获取一年的价格
.call()
.then((res) => {
});
注册域名
提交注册申请
let bytes = ''
const name = "xxx.xyz".split('.')[0]//只取域名前缀
ETHRegistrarControllerContract.methods
.makeCommitmentWithConfig(
name,
window.ethereum.selectedAddress,
Web3.utils.sha3(randomString(12)),
PublicResolverAddress,
window.ethereum.selectedAddress
)
.call()
.then((res) => {
bytes = res;
});
ETHRegistrarControllerContract.methods
.commit(bytes)
.send({ from: window.ethereum.selectedAddress })
.on("receipt", function (receipt) {
})
需等待1min
完成注册
ETHRegistrarControllerContract.contract.methods
.registerWithConfig(
name,
window.ethereum.selectedAddress,
31556952 * years,//注册年数,单位s
Web3.utils.sha3(randomString(12)),
PublicResolverAddress,
window.ethereum.selectedAddress
)
.send({
from: window.ethereum.selectedAddress,
value: price * 1.2 * years,//价格*年数
})
设置主 ENS 名称
**ReverseRegistrarContract**.methods
.setName('xxxx.xyz')
.send({ from: window.ethereum.selectedAddress })
.on("receipt", function (receipt) {
})
设置解析记录
const PublicResolverContract = new Web3.eth.Contract(
PublicResolverAbi,
resolverAddress,//ENSRegistryWithFallbackContract.resolver
)
var domainHash = namehash.hash(
'xxx.xyz'//ens域名地址
);
PublicResolverContract.methods
.setAddr(domainHash, coinType, "0x" + formatsByName["ETH"].decoder(inputAddress).toString("hex"))
.on("receipt", function (receipt) {
})
获取数据
通过合约获取数据
获取owner和resolver
var namehash = require("eth-ens-namehash");
const web3 = new Web3(window.ethereum)
const ENSRegistryWithFallbackContract = new web3.eth.Contract(
ABI,
ENSRegistryWithFallbackAddress
);
var domainHash = namehash.hash(
'xxx.xyz'//ens域名地址
);
ENSRegistryWithFallbackContract.methods
.owner(domainHash)
.call().then(res => {
})
ENSRegistryWithFallbackContract.methods
.resolver(domainHash)
.call().then(res => {
})
获取ens域名解析记录
var domainHash = namehash.hash(
'xxx.xyz'//ens域名地址
);
const PublicResolverContract = new Web3.eth.Contract(
PublicResolverAbi,
resolverAddress,//ENSRegistryWithFallbackContract.resolver
)
PublicResolverContract.methods
.addr(domainHash, coinType)//coinTypes在graph可查,看下方graph代码块
.call()
.then((res) => {
})
通过graph获取数据
数据基本都用graph查询
获取注册信息
const name = "xxx.xyz".split('.')[0]//只取域名前缀
const id = Web3.utils.sha3(name)
axios.post(
this.GLOBAL.netdata.api_url,
{
query: `
{
registration(id: "${id}") {
id
registrationDate
domain {
name
}
registrant {
id
}
}
}
`
}
).then((res) => {
});
获取域名信息
var domainHash = namehash.hash(
'xxx.xyz'//ens域名地址
);
axios.post(
graphUrl,
{
query: `
{
domain(id: "${domainHash}") {
id
name
resolver {
coinTypes
texts
}
}
}
`
}
).then((res) => {
});
通过ethers.js调用
ethers.js原生支持ens
const provider = new ethers.providers.JsonRpcProvider('https://rinkeby.infura.io/v3/3dd7eb5d146740f4a47e136531e7795e', {
chainId: 4,
name: 'rinkeby',
ensAddress: ensAddress
})
const resolver = await provider.getResolver('xxx.xyz')
const x = await resolver.getAddress(60)//参数是coinType
// 获取余额
const resolver = await provider.getBalance('xxx.xyz')
获取其他值阅读ethjs文档:https://docs.ethers.io/v5/api/providers/provider/#EnsResolver
库
对于批量获取,可以使用ensjs库
@ensdomains/ensjs:https://www.npmjs.com/package/@ensdomains/ensjs