Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

Background

ERC-1185 proposes a way to store and retrieve DNS records through ENS resolver contracts.

This article translates and explains the proposal below.

ERC-1185 was in Review as of December 30, 2023.

I also write about other ERCs.

Overview

ERC-1185 defines an Ethereum Name Service (ENS) resolver interface for storing and looking up Domain Name System (DNS) records on Ethereum. ENS gives applications human-readable names for addresses and other resources. The proposal extends it to support DNS data, with the goal of providing a more decentralized way to manage records for internet domain names.

The proposal applies blockchain technology to internet naming. Smart contracts implement the relevant ENS operations. This profile defines the DNS-record operations of one type of ENS resolver.

DNS

The Domain Name System identifies internet services and devices. It resolves a memorable domain name such as www.example.com to an IP address such as 192.0.2.1 that computers and network equipment can use. It resembles a directory that associates names with phone numbers. You can type a name into a browser instead of memorizing the server's IP address.

DNS hierarchy and server roles
  • Root servers
    • At the top of the hierarchy, they return referrals to the name servers for top-level domains such as .com, .net, and .org.
  • TLD servers
    • Each top-level domain (TLD) has servers that provide information about names delegated within it.
  • Authoritative DNS servers
    • They serve records for the zones they manage, which may include records or delegation information for subdomains.
  • Recursive DNS servers
    • They query the relevant servers on a client's behalf and return the final answer.
    • Internet service providers (ISPs) often offer recursive resolvers.
DNS resolution
  1. A user enters a domain name in the browser.
  2. The device sends a query to its configured recursive resolver.
  3. When necessary, the resolver queries root, TLD, and authoritative servers to find the IP address.
  4. The resolver returns the address to the device, and the browser connects to the server at that address.
  5. The device may cache the result to resolve the same name faster on a later visit.
Why DNS matters
  • Usability
    • People can use names they remember to access internet resources.
  • Connectivity
    • DNS helps servers and devices worldwide communicate reliably.
  • Security
    • DNSSEC allows a validating resolver to verify the origin and integrity of signed DNS data.

Efficient, accurate DNS resolution underpins everyday online activity.

Motivation

ENS offers a way to manage domain names that separates name ownership from the provision of records. This separation lets owners control the information associated with their names.

Owners can use smart contracts to assign subdomains to people or organizations. The proposal's motivation highlights irrevocable assignments when the ownership contract enforces them. This gives owners more flexibility in administering subdomains.

ENS provides a programmable naming system. Applications can combine its naming features with contracts that implement standards such as ERC-20 or ERC-721 to build more flexible workflows.

Specification

The DNS resolver profile builds on ERC-137. Traditional DNS groups records into zones. Updating an entire zone on Ethereum whenever one record changes could incur substantial transaction fees. The proposal instead groups records into resource record sets (RRsets). Records for a particular domain, name, and resource type form a set. For example, the A records for www.example.com in the example.com domain make up one RRset. Updates can target just the affected sets.

For more on ERC-137, see the article below.

[ERC137] How ENS associates names with addresses

The record-set model does not fully support features that depend on full zones, such as zone transfers or DNSSEC. A separate resolver profile could store whole zones, but potentially high update costs put that approach outside this proposal.

The profile defines two operations for setting or clearing data and two for querying it.

setDNSRecords(bytes32 node, bytes data)

Adds, replaces, or deletes one or more records for an ENS node. Its function selector is 0x0af179d7. The node is the namehash of the fully qualified ENS domain as defined by ERC-137, and data contains one or more DNS records in wire format.

A record with empty data signals deletion. Records in the same RRset must appear consecutively in the payload. If a payload supplies the same RRset in separate runs, a later run overwrites an earlier one.

clearDNSZone(bytes32 node)

Deletes all DNS records for the specified ENS node. Its function selector is 0xad5780af. Pass the namehash of the fully qualified ENS domain to clear. This lets a caller remove the records for a whole zone in one operation.

dnsRecords(bytes32 node, bytes32 name, uint16 resource)

Returns records matching a node, name, and resource type. The function selector listed in the proposal is 0x2461e851. The node is the namehash of the fully qualified ENS domain, name is the keccak256() hash of the DNS wire-format record name, and resource is the resource record type ID.

It returns matching records in DNS wire format or empty bytes if no records match. The proposal's text uses the plural dnsRecords, but the reference implementation below uses the singular dnsRecord. Verify the deployed resolver's ABI before calling it.

hasDNSRecords(bytes32 node, bytes32 name)

Checks whether any DNS records exist for a node and name. Its function selector is 0x4cbf6ba4. The node is the namehash of the fully qualified ENS domain, and name is the keccak256() hash of the record name in DNS wire format. It returns true if records exist and false otherwise.

These operations let applications update only the DNS records that need changing, clear a zone in one call, or check and retrieve data stored through ENS.

Additional Notes

DNS acts as a directory for internet names. Traditional DNS starts from a hierarchy, where a parent zone can delegate names below it. A registry for a TLD such as .com or .org, for instance, administers delegations for names registered directly under that TLD.

ENS likewise uses hierarchical names, but its on-chain registry and resolver contracts expose a different ownership and lookup model. Anyone can query records for a name when a resolver exposes them, and key holders can manage the names they control under the relevant contracts' rules.

Applications can combine ENS with other smart contract interfaces, including ERC-20 and ERC-721. These interfaces are examples of what applications can use alongside ENS. They are not requirements for storing or retrieving DNS records under ERC-1185.

Compatibility

The proposal identifies no backward compatibility issues.

Implementation

javascript
pragma solidity ^0.7.4;
import "../ResolverBase.sol";
import "@ensdomains/dnssec-oracle/contracts/RRUtils.sol";

abstract contract DNSResolver is ResolverBase {
    using RRUtils for *;
    using BytesUtils for bytes;

    bytes4 constant private DNS_RECORD_INTERFACE_ID = 0xa8fa5682;
    bytes4 constant private DNS_ZONE_INTERFACE_ID = 0x5c47637c;

    // DNSRecordChanged is emitted whenever a given node/name/resource's RRSET is updated.
    event DNSRecordChanged(bytes32 indexed node, bytes name, uint16 resource, bytes record);
    // DNSRecordDeleted is emitted whenever a given node/name/resource's RRSET is deleted.
    event DNSRecordDeleted(bytes32 indexed node, bytes name, uint16 resource);
    // DNSZoneCleared is emitted whenever a given node's zone information is cleared.
    event DNSZoneCleared(bytes32 indexed node);

    // DNSZonehashChanged is emitted whenever a given node's zone hash is updated.
    event DNSZonehashChanged(bytes32 indexed node, bytes lastzonehash, bytes zonehash);

    // Zone hashes for the domains.
    // A zone hash is an ERC-1577 content hash in binary format that should point to a
    // resource containing a single zonefile.
    // node => contenthash
    mapping(bytes32=>bytes) private zonehashes;

    // Version the mapping for each zone.  This allows users who have lost
    // track of their entries to effectively delete an entire zone by bumping
    // the version number.
    // node => version
    mapping(bytes32=>uint256) private versions;

    // The records themselves.  Stored as binary RRSETs
    // node => version => name => resource => data
    mapping(bytes32=>mapping(uint256=>mapping(bytes32=>mapping(uint16=>bytes)))) private records;

    // Count of number of entries for a given name.  Required for DNS resolvers
    // when resolving wildcards.
    // node => version => name => number of records
    mapping(bytes32=>mapping(uint256=>mapping(bytes32=>uint16))) private nameEntriesCount;

    /**
     * Set one or more DNS records.  Records are supplied in wire-format.
     * Records with the same node/name/resource must be supplied one after the
     * other to ensure the data is updated correctly. For example, if the data
     * was supplied:
     *     a.example.com IN A 1.2.3.4
     *     a.example.com IN A 5.6.7.8
     *     www.example.com IN CNAME a.example.com.
     * then this would store the two A records for a.example.com correctly as a
     * single RRSET, however if the data was supplied:
     *     a.example.com IN A 1.2.3.4
     *     www.example.com IN CNAME a.example.com.
     *     a.example.com IN A 5.6.7.8
     * then this would store the first A record, the CNAME, then the second A
     * record which would overwrite the first.
     *
     * @param node the namehash of the node for which to set the records
     * @param data the DNS wire format records to set
     */
    function setDNSRecords(bytes32 node, bytes calldata data) external authorised(node) {
        uint16 resource = 0;
        uint256 offset = 0;
        bytes memory name;
        bytes memory value;
        bytes32 nameHash;
        // Iterate over the data to add the resource records
        for (RRUtils.RRIterator memory iter = data.iterateRRs(0); !iter.done(); iter.next()) {
            if (resource == 0) {
                resource = iter.dnstype;
                name = iter.name();
                nameHash = keccak256(abi.encodePacked(name));
                value = bytes(iter.rdata());
            } else {
                bytes memory newName = iter.name();
                if (resource != iter.dnstype || !name.equals(newName)) {
                    setDNSRRSet(node, name, resource, data, offset, iter.offset - offset, value.length == 0);
                    resource = iter.dnstype;
                    offset = iter.offset;
                    name = newName;
                    nameHash = keccak256(name);
                    value = bytes(iter.rdata());
                }
            }
        }
        if (name.length > 0) {
            setDNSRRSet(node, name, resource, data, offset, data.length - offset, value.length == 0);
        }
    }

    /**
     * Obtain a DNS record.
     * @param node the namehash of the node for which to fetch the record
     * @param name the keccak-256 hash of the fully-qualified name for which to fetch the record
     * @param resource the ID of the resource as per https://en.wikipedia.org/wiki/List_of_DNS_record_types
     * @return the DNS record in wire format if present, otherwise empty
     */
    function dnsRecord(bytes32 node, bytes32 name, uint16 resource) public view returns (bytes memory) {
        return records[node][versions[node]][name][resource];
    }

    /**
     * Check if a given node has records.
     * @param node the namehash of the node for which to check the records
     * @param name the namehash of the node for which to check the records
     */
    function hasDNSRecords(bytes32 node, bytes32 name) public view returns (bool) {
        return (nameEntriesCount[node][versions[node]][name] != 0);
    }

    /**
     * Clear all information for a DNS zone.
     * @param node the namehash of the node for which to clear the zone
     */
    function clearDNSZone(bytes32 node) public authorised(node) {
        versions[node]++;
        emit DNSZoneCleared(node);
    }

    /**
     * setZonehash sets the hash for the zone.
     * May only be called by the owner of that node in the ENS registry.
     * @param node The node to update.
     * @param hash The zonehash to set
     */
    function setZonehash(bytes32 node, bytes calldata hash) external authorised(node) {
        bytes memory oldhash = zonehashes[node];
        zonehashes[node] = hash;
        emit DNSZonehashChanged(node, oldhash, hash);
    }

    /**
     * zonehash obtains the hash for the zone.
     * @param node The ENS node to query.
     * @return The associated contenthash.
     */
    function zonehash(bytes32 node) external view returns (bytes memory) {
        return zonehashes[node];
    }

    function supportsInterface(bytes4 interfaceID) virtual override public pure returns(bool) {
        return interfaceID == DNS_RECORD_INTERFACE_ID ||
               interfaceID == DNS_ZONE_INTERFACE_ID ||
               super.supportsInterface(interfaceID);
    }

    function setDNSRRSet(
        bytes32 node,
        bytes memory name,
        uint16 resource,
        bytes memory data,
        uint256 offset,
        uint256 size,
        bool deleteRecord) private
    {
        uint256 version = versions[node];
        bytes32 nameHash = keccak256(name);
        bytes memory rrData = data.substring(offset, size);
        if (deleteRecord) {
            if (records[node][version][nameHash][resource].length != 0) {
                nameEntriesCount[node][version][nameHash]--;
            }
            delete(records[node][version][nameHash][resource]);
            emit DNSRecordDeleted(node, name, resource);
        } else {
            if (records[node][version][nameHash][resource].length == 0) {
                nameEntriesCount[node][version][nameHash]++;
            }
            records[node][version][nameHash][resource] = rrData;
            emit DNSRecordChanged(node, name, resource, rrData);
        }
    }
}

Security

Security depends on protecting the keys authorized to manage the ENS name. Someone who obtains a key that controls the name or has the necessary resolver permissions could change its records. Protecting those keys is therefore an important part of securing the domain.

References

Jim McDonald (@mcdee), "ERC-1185: Storage of DNS Records in ENS [DRAFT]," Ethereum Improvement Proposals, no. 1185, June 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1185.

Conclusion

ERC-1185 defines how an ENS resolver can store and retrieve DNS records. Grouping records by name and type lets a caller replace an individual RRset or clear all records for a domain when needed.

If you have questions, feel free to send me a direct message on Twitter.

Twitter @cardene777

I also publish on these sites.

https://chaldene.net/