If you’d like to deploy ENS on your own network, or deploy your own copy of ENS on a public network, this guide shows you how. If you want to use an existing ENS deployment, see Resolving Names, Managing Names, and Registering & Renewing Names instead.
The majority of smart contrats are published as npm modules (eg: ENS registry) which you can require at migration.
const ENS = require(`@ensdomains/ens/build/contracts/ENSRegistry.sol`)
Including within your smart contract is as follows
ENS contains only interface while
ENSRegistry includes the actual implementation.
First, you need to deploy ENS’s central component, the registry.
const ens = await ENS.new();
Once deployed, you will have a fresh ENS registry, whose root node is owned by the account that submitted the transaction. This account has total control over the ENS registry - it can create and replace any node in the entire tree.
From here, it's possible to create and manage names by directly interacting with the registry, as described in Managing Names. However, you will probably want to deploy a resolver, and you may want to deploy a registrar so other users can register names.
For most purposes, it's convenient to have a general-purpose resolver available that users can easily use for their names. Deploying one is straightforward:
const publicResolver = await PublicResolver.new(ens.address);
For ease of use, we can give this resolver a name:
const namehash = require('eth-ens-namehash');await ens.setSubnodeOwner("0x0000000000000000000000000000000000000000", sha3("resolver"), accounts);await ens.setResolver(namehash.hash("resolver"), publicResolver.address);await publicResolver.setAddr(namehash.hash("resolver"), publicResolver.address);
Above, we first create a new top-level domain, "resolver", then set its resolver address to our newly deployed public resolver. Finally, we set up an address record for "resolver", pointing back to the resolver address. In effect, the resolver is answering queries about its own address. After this, anyone can find the public resolver at the special ENS name "resolver".
In order to allow others to register domains without your direct intervention, you will need to deploy a registrar contract, and set it as the owner of a top-level domain that you want it to assign subdomains of. Here, we'll use the simple FIFSRegistrar, and set it as the owner of the top-level domain test:
const sha3 = require('web3-utils').sha3;const testRegistrar = await FIFSRegistrar.new(ens.address, namehash.hash("test"));ens.setSubnodeOwner("0x0000000000000000000000000000000000000000", sha3("test"), testRegistrar);
Any user can now register domains under .test by calling the
await testRegistrar.register(sha3("alice"), accounts);
After calling the above function,
accounts now has ownership of the name alice.test.
If you wish to enable reverse resolution on your deployment, you will need to deploy the reverse registrar:
const reverseRegistrar = await ReverseRegistrar.new(ens.address, publicResolver.address);await ens.setSubnodeOwner("0x0000000000000000000000000000000000000000", sha3("reverse"), accounts);await ens.setSubnodeOwner(namehash.hash("reverse"), sha3("addr"), reverseRegistrar.address);