The ENSRegistry contract is the implementation of the ENS interface and serves as the central contract used to look up resolvers and owners for domains. It maintains a mapping of domain nodes to their ownership, resolver, and TTL records.
Overview
ENSRegistry is the foundational contract of the Ethereum Name Service. It:
- Stores ownership, resolver, and TTL information for all ENS domains
- Provides access control through owner-based authorization
- Supports operator delegation for managing records
- Enables hierarchical domain structure through subnode creation
Import
import '@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol';
Constructor
Constructs a new ENS registry. The deployer becomes the owner of the root node (0x0).
State Variables
records
mapping(bytes32 => Record) records
Maps node hashes to their record information.
Record struct:
struct Record {
address owner;
address resolver;
uint64 ttl;
}
operators
mapping(address => mapping(address => bool)) operators
Maps owner addresses to operator addresses and their approval status.
Modifiers
authorised
modifier authorised(bytes32 node)
Permits modifications only by the owner of the specified node or an approved operator.
Functions
setRecord
function setRecord(
bytes32 node,
address owner,
address resolver,
uint64 ttl
) external virtual override
Sets the record for a node.
The address of the new owner
The address of the resolver
setSubnodeRecord
function setSubnodeRecord(
bytes32 node,
bytes32 label,
address owner,
address resolver,
uint64 ttl
) external virtual override
Sets the record for a subnode.
The hash of the label specifying the subnode
The address of the new owner
The address of the resolver
setOwner
function setOwner(
bytes32 node,
address owner
) public virtual override authorised(node)
Transfers ownership of a node to a new address. May only be called by the current owner of the node or an approved operator.
The node to transfer ownership of
The address of the new owner
Emits: Transfer(node, owner)
setSubnodeOwner
function setSubnodeOwner(
bytes32 node,
bytes32 label,
address owner
) public virtual override authorised(node) returns (bytes32)
Transfers ownership of a subnode keccak256(node, label) to a new address. May only be called by the owner of the parent node.
The hash of the label specifying the subnode
The address of the new owner
Returns: The subnode hash keccak256(abi.encodePacked(node, label))
Emits: NewOwner(node, label, owner)
setResolver
function setResolver(
bytes32 node,
address resolver
) public virtual override authorised(node)
Sets the resolver address for the specified node.
The address of the resolver
Emits: NewResolver(node, resolver)
setTTL
function setTTL(
bytes32 node,
uint64 ttl
) public virtual override authorised(node)
Sets the TTL for the specified node.
Emits: NewTTL(node, ttl)
setApprovalForAll
function setApprovalForAll(
address operator,
bool approved
) external virtual override
Enables or disables approval for a third party (“operator”) to manage all of msg.sender’s ENS records.
Address to add to the set of authorized operators
True if the operator is approved, false to revoke approval
Emits: ApprovalForAll(msg.sender, operator, approved)
owner
function owner(
bytes32 node
) public view virtual override returns (address)
Returns the address that owns the specified node.
Returns: The address of the owner. Returns address(0x0) if the owner is the registry itself.
resolver
function resolver(
bytes32 node
) public view virtual override returns (address)
Returns the address of the resolver for the specified node.
Returns: The address of the resolver
ttl
function ttl(bytes32 node) public view virtual override returns (uint64)
Returns the TTL of a node and any records associated with it.
Returns: The TTL of the node in seconds
recordExists
function recordExists(
bytes32 node
) public view virtual override returns (bool)
Returns whether a record has been imported to the registry.
Returns: True if the record exists (owner is not address(0x0))
isApprovedForAll
function isApprovedForAll(
address owner,
address operator
) external view virtual override returns (bool)
Queries if an address is an authorized operator for another address.
The address that owns the records
The address that acts on behalf of the owner
Returns: True if operator is an approved operator for owner, false otherwise
Usage Examples
Deployment
Creating Subdomains
Operator Delegation
import '@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol';
// Deploy the registry
ENSRegistry registry = new ENSRegistry();
// The deployer now owns the root node
// and can create top-level domains
import '@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol';
contract SubdomainManager {
ENSRegistry public registry;
bytes32 public rootNode;
constructor(ENSRegistry _registry, bytes32 _rootNode) {
registry = _registry;
rootNode = _rootNode;
}
function createSubdomain(
string memory label,
address owner,
address resolver
) public {
bytes32 labelHash = keccak256(bytes(label));
// Create subdomain with owner and resolver
registry.setSubnodeRecord(
rootNode,
labelHash,
owner,
resolver,
0 // TTL
);
}
}
import '@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol';
contract OperatorExample {
ENSRegistry public registry;
constructor(ENSRegistry _registry) {
registry = _registry;
}
function delegateManagement(address operator) public {
// Allow operator to manage all your ENS records
registry.setApprovalForAll(operator, true);
}
function revokeManagement(address operator) public {
// Revoke operator's management rights
registry.setApprovalForAll(operator, false);
}
function checkOperator(
address owner,
address operator
) public view returns (bool) {
return registry.isApprovedForAll(owner, operator);
}
}