Edit this page

Namehashing

Namehashing is an algorithm that converts a domain name in a classical format (like www.example.crypto) to ERC-721 token ID. All .crypto contracts accept a domain name as a method argument in the form of an ERC-721 token. Namehashing is defined as a part of the EIP-137 standard. See the standard for a text description of the algorithm.

To verify an implementation of the namehash algorithm, use the following reference table:

Domain Name ERC721 Token
. 0x0000000000000000000000000000000000000000000000000000000000000000
crypto 0x0f4a10a4f46c288cea365fcf45cccf0e9d901b945b9829ccdb54c10dc3cb7a6f
example.crypto 0xd584c5509c6788ad9d9491be8ba8b4422d05caf62674a98fbf8a9988eeadfb7e
www.example.crypto 0x3ae54ac25ccd63401d817b6d79a4a56ae7f79a332fe77a98fa0c9d10adf9b2a1
a.b.c.crypto 0x353ea3e0449067382e0ea7934767470170dcfa9c49b1be0fe708adc4b1f9cf13

Generating a Domain Namehash

You can generate the namehash of a domain using any of the Resolution Libraries, Resolution CLI, or Resolution Service. You can also use online tools to calculate the namehash of the domain.

JavaScriptJavaSwiftGolangResolution CLI
Copy
Copied
const {default: Resolution} = require('@unstoppabledomains/resolution');
// obtain a key by following this document https://docs.unstoppabledomains.com/domain-distribution-and-management/quickstart/retrieve-an-api-key/#api-key
const resolution = new Resolution({ apiKey: "<api_key>" });
let namehash = resolution.namehash("brad.crypto", "UNS");
Copy
Copied
import com.unstoppabledomains.resolution.Resolution;
// obtain a key by following this document https://docs.unstoppabledomains.com/domain-distribution-and-management/quickstart/retrieve-an-api-key/#api-key. See https://github.com/unstoppabledomains/resolution-java for more initialization options
DomainResolution resolution = new Resolution("<api_key>");
String namehash = resolution.getNamehash("brad.crypto", "UNS");
Copy
Copied
import UnstoppableDomainsResolution

// obtain a key by following this document https://docs.unstoppabledomains.com/domain-distribution-and-management/quickstart/retrieve-an-api-key/#api-key. See https://github.com/unstoppabledomains/resolution-swift for more initialization options
guard let resolution = try? Resolution(apiKey: "<api_key>") else {
  print ("Init of Resolution instance failed...")
  return
}

let namehash = try resolution.namehash(domain: "brad.crypto")
Copy
Copied
package main

import (
    "fmt"
    "github.com/unstoppabledomains/resolution-go/v3"
)

func main() {
    // obtain a key by following this document https://docs.unstoppabledomains.com/domain-distribution-and-management/quickstart/retrieve-an-api-key/#api-key. See https://github.com/unstoppabledomains/resolution-go for more initialization options
    uns, _ := resolution.NewUnsBuilder().SetUdClient("<api_key>").Build()
    namehash, _ := uns.Namehash("brad.crypto")
    fmt.Println("The namehash for brad.crypto is", namehash)
}
Copy
Copied
$ resolution namehash -d brad.crypto
"0x756e4e998dbffd803c21d23b06cd855cdc7a4b57706c95964a37e24b47c10fc9"
info

The JavaScript and Java Resolution Libraries require a Naming Service parameter to generate namehashes. This specifies the name service that manages the domain name, and the value must either be "UNS" or "ZNS".

Reverse Lookup

Fundamentally, namehashing is a one-way operation. It recursively hashes the labels using the SHA-256 hash function. If one possesses a precomputed table of all hashes and corresponding domains reverse lookups are possible. This table can be reconstructed using the events on the CNS and UNS Registry NewURI event.

JSON RPC

The UNS and CNS metadata APIs track all domain names with their corresponding namehash. That makes it possible to obtain an original domain name from token metadata which can be retrieved from the token metadata URI.

The token metadata URI can be retrieved via ETH RPC call to ProxyReader#tokenURI. This works for CNS and UNS.

Now, the domain name along with other metadata can be retrieved by performing a simple GET request using the token metadata URI.

For example:

Copy
Copied
// Get a proxy reader contract instance using web3 or ethers
let proxyReaderContractInstance = new Contract(address, abi);

// call the tokenURI method
let tokenUri = await proxyReaderContractInstance.tokenURI("0x756e4e998dbffd803c21d23b06cd855cdc7a4b57706c95964a37e24b47c10fc9");

// GET data from URI
let metadataResponse = await fetch(tokenUri);
// Parse it as json
let metadata = await metadataResponse.json();

// Retrieve the domain name from metadata
console.log(metadata.name);

NewURI Events

When a domain is created its name and token ID are logged using a NewURI event. This event can be looked up in order to reverse the namehash of a domain. This approach also works for CNS and UNS.

For an implementation example, see the unhash function of the resolution library.

Note that regardless of the way a namehash is reversed, it should always be forward-validated by hashing the retrieved domain name. In order to ensure the integrity of the retrieved domain name.

Public API

Unstoppable Domains maintains a public-facing API which can be used to obtain the information of a domain using its namehash. To learn more, visit the Get Metadata for a Domain documentation.