ENS 

The aim of ENS is to provide a better and more secure user experience when dealing with Ethereum addresses or Swarm hashes. It allows the user to register and associate an address or hash with a more user-friendly string, such as packttoken.eth. This is similar to the way in which DNS maps a user-friendly URL to an IP address.

We will register a test domain on the Rinkeby testnet, which supports .test domains, but not the .eth domains supported by the mainnet and the Ropsten testnet. This is to our benefit—registering a .test domain is a much quicker process than registering the .eth equivalent, as it doesn't involve going through an auction process. It should be noted that the .test domain expires after 28 days.

ENS is composed of a series of smart contracts, which we will describe briefly, as follows:

  • ENS root contract: This contract keeps track of the registrars that control the top-level .eth and .test domains, and of which resolver contract to use for which domain.
  • Registrar contracts: These contracts are similar to their DNS namesakes, and are responsible for administering a particular domain. There is a separate registrar for the .eth domain on the mainnet, .test on Ropsten, and .test on Rinkeby.
  • Resolver contracts: These contracts are responsible for the actual mapping between Ethereum addresses or Swarm content hashes and the user-friendly domain names.

The first step is to register our .test domain on Rinkeby. To begin, we need to download a JavaScript file that contains certain contract ABI definitions and helper functions that will simplify the overall registration process. The file can be found at https://github.com/ensdomains/ens/blob/master/ensutils-testnet.js.

The contents should be copied into a local file which we will access later.

We will be working on the Rinkeby testnet, but the file that we have downloaded contains the addresses associated with the Ropsten testnet ENS contracts, so we'll need to change it to point to the equivalent contracts on Rinkeby.

On line 220, change the line to point to the Rinkeby ENS root contract, as follows:

var ens = ensContract.at('0xe7410170f87102df0055eb195163a03b7f2bff4a');

The second change that we need to make is to the address associated with the Rinkeby public resolver contract. On line 1,314, change it to point to the following address:

var publicResolver = resolverContract.at('0x5d20cf83cb385e06d2f2a892f9322cd4933eacdc');

Registering our domain requires a running Geth node connected to the Rinkeby network, which we used in Chapter 8, Creating an ICO,for deploying our ICO contracts. If it hasn't been left running, restart the Geth node using the following command, and allow it to sync to the latest block:

geth --networkid=4 --datadir=$HOME/.rinkeby --rpc --cache=1024 --bootnodes=enode://a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf@52.169.42.101:30303

From a second Terminal, we now need to attach to the running client and load the edited .js file with the abstractions of our Rinkeby ENS contracts:

$ geth attach ~/.rinkeby/geth.ipc
> loadScript("./ensutils-testnet.js")
true

We will now have access to the relevant functions inside both the registrar and resolver contracts deployed on Rinkeby. First, check that the name you want to use is available:

> testRegistrar.expiryTimes(web3.sha3("packt_ico"))
0

This will return a timestamp equal to the time at which the name expires. A zero timestamp means that the name is available.

We can then register the name with the registrar contract, first ensuring that we have a funded account that our Geth client can access:

> testRegistrar.register(web3.sha3("packt_ico"), eth.accounts[0], {from: eth.accounts[0]})
"0xe0397a6e518ce37d939a629cba3470d8bdd432d980531f368449149d40f7ba92"

This will return a transaction hash that can be checked on EtherScan, for inclusion in the blockchain. Once included, we can query the registrar contract to check the expiry time and owner account:

> testRegistrar.expiryTimes(web3.sha3("packt_ico"))
1538514668

> ens.owner(namehash("packt_ico.test"))
"0x396ebfd1a0ec6e6cefe6035acf487900a10fcf56"

We now own an ENS domain name, but it doesn't yet point to anything useful. To do that, we need to use the public resolver contract whose address we also added to ensutils-testnet.js.

The next step is to create a transaction to the public resolver contract, in order to associate our Swarm file hash with our new domain name. Note that 0x must be added to the front of our hash in order for the contract to parse it correctly:

> publicResolver.setContent(namehash("packt_ico.test"), '0x2a504aac8d02f7715bea19c6c19b5a2be8f7ab9442297b2b64bbb04736de9699', {from: eth.accounts[0]});
"0xaf51ba63dcedb0f5c44817f9fd6219544a1d6124552a369e297b6bb67f064dc7"

So far, we have registered our domain name with the public registrar contract and set a public resolver to map our domain name to the Swarm hash.

The next connection to make is to tell the ENS root contract the address of the resolver to use for our domain name:

> ens.setResolver(namehash("packt_ico.test"), publicResolver.address, {from: eth.accounts[0]})
"0xe24b4c35f1dadb97b5e00d7e1a6bfdf4b053be2f2b78291aecb8117eaa8eeb11"

We can check whether this has been successful by querying the ENS root contract:

> ens.resolver(namehash("packt_ico.test"))
"0x5d20cf83cb385e06d2f2a892f9322cd4933eacdc"

The final piece of the puzzle is to tell our local Swarm client how to find and use the correct resolver contract. To do this, we need to start it by using the --ens-api option, which tells Swarm how to resolve ENS addresses. In this case, we pass an IPC path connecting to our Geth client, which is itself connected to the Rinkeby network where our ENS contract resides.

As a part of this command, we also pass the address of Rinkeby's ENS root contract:

swarm --bzzaccount <your_rinkeby_account> --ens-api 0xe7410170f87102df0055eb195163a03b7f2bff4a@/home/<your_home_directory>/.rinkeby/geth.ipc --datadir ~/.rinkeby

Our site can now be viewed at the following local URL: http://localhost:8500/bzz:/packt_ico.test/.

If we wanted to make our new domain publicly accessible, rather than just accessible on our local machine, we would need to register a .eth domain on the mainnet. At present we are accessing our website using our .test domain through our local Swarm client, which we've connected to our Rinkeby Geth client.

The public Swarm gateway, however, is connected to the Ethereum mainnet, so it can only access the mainnet ENS contracts, and not those on Rinkeby.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset