The ENS library is to be used in dapps and possibly other tools that need to connect to the set of ENS contracts on the Ethereum blockchain. It should give them all the tools they need to resolve and set records for their ENS names, as well as abstracting implementation details including, but not limited to:
- namehashes (could be provided as an advanced option)
- resolvers (unless it's absolutely required for custom resolvers)
- using the graph for getting textRecord or non-eth address keys
- decoders/encoders of addresses
Takes either a string, for connecting directly to infura, alternatively, can take a web3 provider object
import ENS from ‘ensLib’
const ens = new ENS({
provider: web3.currentProvider,
registryAddress: registry // optional, if it recognises the network, it will use a hardcoded list of registry addresses otherwise will error and ask user to provider a registry address
})
ens.getName()
- Gets the name object using a namehash or ens nameens.getResolver()
- Gets the resolver object at certain address, which can be then be asked what name you want from that resolver.ens.setReverseRecord()
- Sets the reverse record for the current account
const name = ens.name(‘jefflau.eth’)
const owner = await name.getOwner()
const resolver = await name.getResolver()
const address = await name.getAddr(‘eth’)
const contenthash = await name.getContenthash()
const parent = await name.getParent() //eth
const textRecord = await name.getTextRecord(‘url’)
// ‘http://awesome.com'
const textRecords = await name.getAllTextRecords()
// [{ key: ‘url’, value: ‘http://awesome.com'}]
const coin = await name.getAddress(‘eth’)
// { key: ‘ETH’, coinId: 50, value: ‘0x000….’}
const coin = await name.getAddress(50)
// { key: ‘ETH’, coinId: 50, value: ‘0x000….’}
const coins = await name.getAllAddresses()
// [{ key: ‘ETH’, value: ‘0x000….’}, {key: "BTC", value: '14abc...']
const tx = await name.setResolver() // by default sets it to the public resolver with no arguments
const tx = await name.setResolver(customResolverAddr)
const tx = await name.setOwner(addr)
const tx = await name.setSubnodeOwner(label, addr)
const tx = await name.createSubdomain(label) //sets the owner to yourself
const tx = await name.setRecords({
contentHash: 'ipfs://...',
addresses: [{key: 'ETH', value: "0x123..." }],
textRecords: [{ key: "url", value: "http://..."}]
})
//TODO possible get all records convenience function
const registrarDetails = name.getRegistrarDetails() //errors if isn't .eth
{
expiryDate: DateObject,
gracePeriodEnds: DateObject,
registrant: "0x123..."
}
const resolver = ens.getResolver('0x123')
const name = await resolver.name('vitalik.eth') // same as ens.getName, but only returns names that are using this resolver
const resolverAddress = resolver.address // 0x123... (not async)
name.getAddr('eth') // 0x123...
name.getContenthash() // ipfs://
ens.getReverseRecord()
ens.setReverseRecord('vitalik.eth')
const resolver = await name.getOwner()
is probablyconst owner