Blockchain domain names are domain names launched as smart contracts on public blockchains.
What sets them apart from traditional domain names (like .com) is that blockchain domains are stored by their owners in their wallet like cryptocurrency, and no third party can change or remove them.
Also, there are no renewal fees for Unstoppable Domains names, so once you own the domain, you own it for life.
To resolve a .crypto domain, we will need to
- Tokenize the domain
- Configure Ethers.js library
- Make a call and fetch the data
Let’s visualize the resolution process using some of the simplest tools a web developer has: knowledge of
Initialize the project folder
The first thing we need to do is create a folder with three files inside: index.html, index.js, and ethers.js.
$ mkdir crypto-resolution
$ cd crypto-resolution
$ touch index.html index.js ethers.js
Your project folder should look exactly like the following:
Let’s open the index.html file and build out the layout for our app.
To create a positive UI experience for the user, we’ll need to build an input bar, a button to trigger the resolution and a
<div> to display our records.
We will need the keccak_256 hash function to calculate ERC-721 token ID for the .crypto domain. To see a full description of this process, read our namehashing article.
We can open
index.htmlin a browser to make sure everything is connected and launches.
Tokenize your domain by namehashing
Namehashing is an algorithm that tokenizes your domain name in a way that the .crypto smart contract can understand.
To tokenize our domain, we’ll need to split the domain name by the “.” character into separate labels, reverse the array, and reduce it to a single hash. We can do this by implementing a recursive hash function.
We’ll also want to implement an
arrayToHex function to get the result as a string, as well as a wrapper function namehash.
Here is the list of expected results with some inputs:
Configure the Ethers.js library
To talk with any blockchain contract using
ethers.js, we need to know the following:
- Contract address
- Contract ABI
- Ethereum provider
Let’s add the following information to our ethers.js file:
For the scope of this project, we will only need to use the getData function from the Unstoppable Domains contract.
Next, we’ll need to create a contract instance and create a function to query our contract.
By inspecting the contract’s getData function interface, we can see that it requires from us an array of keys and a tokenId. We can get the tokenId by calling the namehash function from above.
Although any string can be stored as a key under the domain, Unstoppable Domains has standardized the keys across many applications.
For this tutorial, we will be looking up the following records:
Make the call to the contract
Let’s update our resolve function to use the namehash and then look up the desired record keys from the input domain name. We’ll then want to print the result in the console to inspect in more detail.
If we try to resolve the brad.crypto domain with the above keys, we should see the following in the console:
data is an array containing all resolved records in the same order in which they were queried. In this case, the first argument is a BTC address and the last one is an ETH address attached to the domain.
Display the records
Since this is a simple example, we won’t get too fancy. We’ll just create a span element for each record containing its key and value, its owner address, and its resolver address. We’ll also want to set the font to red if the record is not found.
Before we test it out, let’s make our lives a little easier by implementing a function to combine keys and results into one object.
Now we can easily show the records on our page:
If we are successful, we should see the following on our page:
Set up error notifications
Now that we have made a successful call, let’s deal with all possible errors that might come up during the resolution.
To do this, we’ll want to create a function to place an error in our records div. We’ll also want to add a boolean argument
cleanDom to remove everything from the records div before we place an error for display.
We can easily identify the possible errors by playing around with the app in its current state.
Once we’ve identified the errors, we will need to update the callback to the
fetchContractData function to show the errors to the user.
Now you can resolve any .crypto domain and display the appropriate error message to your users. Just open the index.html file in your browser and play around with the results to get a better sense of the types of errors that may come up and the types of error messages you might want to include.
For example, you can try to resolve the following domains:
If you have any questions, please visit our Unstoppable Domains Developer Community on Discord.