ENS as NFT
When ENS .eth registrar migrated in May 2019, the .eth registrar became an ERC721 compliant nonfungable token contract, meaning that .eth registrations can be transferred in the same fashion as other NFTs.

Deriving tokenId from ENS name

The tokenId of ENS name is simply the uint256 representation of the hash of the label (vitalik for vitalik.eth).
1
const ethers = require('ethers')
2
const BigNumber = ethers.BigNumber
3
const utils = ethers.utils
4
const name = 'vitalik'
5
const labelHash = utils.keccak256(utils.toUtf8Bytes('vitalik'))
6
const tokenId = BigNumber.from(labelHash).toString()
Copied!

Deriving ENS name from tokenId

Unlike deriving tokenId, deriving ENS name from tokenId is not as easy. This is because all ENS names are stored as fixed-length hash to allow registering infinite length of names. The downside of this architecture is that you cannot directly query ENS smart contracts to return ens name using tokenId.
Our recommended way is to query via https://thegraph.com ENS subgraph. The graph decodes the hash to name as it indexes. The example code to query is as follows.
1
const ethers = require('ethers')
2
const BigNumber = ethers.BigNumber
3
const gr = require('graphql-request')
4
const { request, gql } = gr
5
const tokenId = '79233663829379634837589865448569342784712482819484549289560981379859480642508'
6
// Should return 0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc
7
const labelHash = BigNumber.from(tokenId).toHexString()
8
9
const url = 'https://api.thegraph.com/subgraphs/name/ensdomains/ens'
10
const GET_LABEL_NAME = gql`
11
query{
12
domains(first:1, where:{labelhash:"${labelHash}"}){
13
labelName
14
}
15
}`
16
17
request(url, GET_LABEL_NAME).then((data) => console.log(data))
18
// { domains: [ { labelName: 'vitalik' } ] }
Copied!
If you prefer not to rely on a third party like TheGraph, the team open-sourced ens-rainbow containing a link to the original dataset (6GB with 133 million entities) so that you can host your own ENS name decoding service.

Turning subdomain into NFT

Currently, all the subdomains nor non .eth domains are not NFT, unless the domain registrar itself supports NFT such as (dcl.eth, and .kred). If you want to turn all subdomains which you own, you have to create a registrar
    1.
    Create a registrar contract as ERC721 compliant
    2.
    Set ENS registry address (mostly when you deploy the registrar)
    3.
    Create register function which calls registry.setSubnodeOwner then mint the token making the subdomain label hash as tokenId
1
contract DCLRegistrar is ERC721Full, Ownable {
2
constructor(
3
IENSRegistry _registry,
4
) public ERC721Full("DCL Registrar", "DCLENS") {
5
// ENS registry
6
updateRegistry(_registry);
7
}
8
9
function register(
10
string memory _subdomain,
11
bytes32 subdomainLabelHash,
12
address _beneficiary,
13
uint256 _createdDate
14
) internal {
15
// Create new subdomain and assign the _beneficiary as the owner
16
registry.setSubnodeOwner(domainNameHash, subdomainLabelHash, _beneficiary);
17
// Mint an ERC721 token with the subdomain label hash as its id
18
_mint(_beneficiary, uint256(subdomainLabelHash));
19
}
20
}
Copied!
Once deployed, then you have to transfer the controller address to the contract.
For non-technical users, we are currently working on upgrading our SubdomainRegistrar which allows you to turn your subdomain into NFT without any coding.

Metadata

.eth does not have .tokenURI . However, we created a separate metadata service which NFT marketlpaces like OpenSea can fetch metadata for ENS such as registration data, expiration date, name length, etc. For more detail, please refer to the metadata documentation site.
ENS Metadata Service
Last modified 5h ago