ENSIP-3: Reverse Resolution
Specifies a TLD, registrar, and resolver interface for reverse resolution of Ethereum addresses using ENS (formerly EIP-181).
Author
Nick Johnson <[email protected]>
Status
Final
Submitted
2016-12-01

Abstract

This ENSIP specifies a TLD, registrar, and resolver interface for reverse resolution of Ethereum addresses using ENS. This permits associating a human-readable name with any Ethereum blockchain address. Resolvers can be certain that the reverse record was published by the owner of the Ethereum address in question.

Motivation

While name services are mostly used for forward resolution - going from human-readable identifiers to machine-readable ones - there are many use-cases in which reverse resolution is useful as well:
  • Applications that allow users to monitor accounts benefit from showing the name of an account instead of its address, even if it was originally added by address.
  • Attaching metadata such as descriptive information to an address allows retrieving this information regardless of how the address was originally discovered.
  • Anyone can configure a name to resolve to an address, regardless of ownership of that address. Reverse records allow the owner of an address to claim a name as authoritative for that address.

Specification

Reverse ENS records are stored in the ENS hierarchy in the same fashion as regular records, under a reserved domain, addr.reverse. To generate the ENS name for a given account's reverse records, convert the account to hexadecimal representation in lower-case, and append addr.reverse. For instance, the ENS registry's address at 0x112234455c3a32fd11230c42e7bccd4a84e02010 has any reverse records stored at 112234455c3a32fd11230c42e7bccd4a84e02010.addr.reverse.
Note that this means that contracts wanting to do dynamic reverse resolution of addresses will need to perform hex encoding in the contract.

Registrar

The owner of the addr.reverse domain will be a registrar that permits the caller to take ownership of the reverse record for their own address. It provides the following methods:

function claim(address owner) returns (bytes32 node)

When called by account x, instructs the ENS registry to transfer ownership of the name hex(x) + '.addr.reverse' to the provided address, and return the namehash of the ENS record thus transferred.
Allowing the caller to specify an owner other than themselves for the relevant node facilitates contracts that need accurate reverse ENS entries delegating this to their creators with a minimum of code inside their constructor:
1
reverseRegistrar.claim(msg.sender)
Copied!

function claimWithResolver(address owner, address resolver) returns (bytes32 node)

When called by account x, instructs the ENS registry to set the resolver of the name hex(x) + '.addr.reverse' to the specified resolver, then transfer ownership of the name to the provided address, and return the namehash of the ENS record thus transferred. This method facilitates setting up a custom resolver and owner in fewer transactions than would be required if calling claim.

function setName(string name) returns (bytes32 node)

When called by account x, sets the resolver for the name hex(x) + '.addr.reverse' to a default resolver, and sets the name record on that name to the specified name. This method facilitates setting up simple reverse records for users in a single transaction.

Resolver interface

A new resolver interface is defined, consisting of the following method:
1
function name(bytes32 node) constant returns (string);
Copied!
Resolvers that implement this interface must return a valid ENS name for the requested node, or the empty string if no name is defined for the requested node.
The interface ID of this interface is 0x691f3431.
Future ENSIPs may specify more record types appropriate to reverse ENS records.

Appendix 1: Registrar implementation

This registrar, written in Solidity, implements the specifications outlined above.
1
pragma solidity ^0.4.10;
2
3
import "./AbstractENS.sol";
4
5
contract Resolver {
6
function setName(bytes32 node, string name) public;
7
}
8
9
/**
10
* @dev Provides a default implementation of a resolver for reverse records,
11
* which permits only the owner to update it.
12
*/
13
contract DefaultReverseResolver is Resolver {
14
AbstractENS public ens;
15
mapping(bytes32=>string) public name;
16
17
/**
18
* @dev Constructor
19
* @param ensAddr The address of the ENS registry.
20
*/
21
function DefaultReverseResolver(AbstractENS ensAddr) {
22
ens = ensAddr;
23
}
24
25
/**
26
* @dev Only permits calls by the reverse registrar.
27
* @param node The node permission is required for.
28
*/
29
modifier owner_only(bytes32 node) {
30
require(msg.sender == ens.owner(node));
31
_;
32
}
33
34
/**
35
* @dev Sets the name for a node.
36
* @param node The node to update.
37
* @param _name The name to set.
38
*/
39
function setName(bytes32 node, string _name) public owner_only(node) {
40
name[node] = _name;
41
}
42
}
43
44
contract ReverseRegistrar {
45
// namehash('addr.reverse')
46
bytes32 constant ADDR_REVERSE_NODE = 0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2;
47
48
AbstractENS public ens;
49
Resolver public defaultResolver;
50
51
/**
52
* @dev Constructor
53
* @param ensAddr The address of the ENS registry.
54
* @param resolverAddr The address of the default reverse resolver.
55
*/
56
function ReverseRegistrar(AbstractENS ensAddr, Resolver resolverAddr) {
57
ens = ensAddr;
58
defaultResolver = resolverAddr;
59
}
60
61
/**
62
* @dev Transfers ownership of the reverse ENS record associated with the
63
* calling account.
64
* @param owner The address to set as the owner of the reverse record in ENS.
65
* @return The ENS node hash of the reverse record.
66
*/
67
function claim(address owner) returns (bytes32 node) {
68
return claimWithResolver(owner, 0);
69
}
70
71
/**
72
* @dev Transfers ownership of the reverse ENS record associated with the
73
* calling account.
74
* @param owner The address to set as the owner of the reverse record in ENS.
75
* @param resolver The address of the resolver to set; 0 to leave unchanged.
76
* @return The ENS node hash of the reverse record.
77
*/
78
function claimWithResolver(address owner, address resolver) returns (bytes32 node) {
79
var label = sha3HexAddress(msg.sender);
80
node = sha3(ADDR_REVERSE_NODE, label);
81
var currentOwner = ens.owner(node);
82
83
// Update the resolver if required
84
if(resolver != 0 && resolver != ens.resolver(node)) {
85
// Transfer the name to us first if it's not already
86
if(currentOwner != address(this)) {
87
ens.setSubnodeOwner(ADDR_REVERSE_NODE, label, this);
88
currentOwner = address(this);
89
}
90
ens.setResolver(node, resolver);
91
}
92
93
// Update the owner if required
94
if(currentOwner != owner) {
95
ens.setSubnodeOwner(ADDR_REVERSE_NODE, label, owner);
96
}
97
98
return node;
99
}
100
101
/**
102
* @dev Sets the `name()` record for the reverse ENS record associated with
103
* the calling account. First updates the resolver to the default reverse
104
* resolver if necessary.
105
* @param name The name to set for this address.
106
* @return The ENS node hash of the reverse record.
107
*/
108
function setName(string name) returns (bytes32 node) {
109
node = claimWithResolver(this, defaultResolver);
110
defaultResolver.setName(node, name);
111
return node;
112
}
113
114
/**
115
* @dev Returns the node hash for a given account's reverse records.
116
* @param addr The address to hash
117
* @return The ENS node hash.
118
*/
119
function node(address addr) constant returns (bytes32 ret) {
120
return sha3(ADDR_REVERSE_NODE, sha3HexAddress(addr));
121
}
122
123
/**
124
* @dev An optimised function to compute the sha3 of the lower-case
125
* hexadecimal representation of an Ethereum address.
126
* @param addr The address to hash
127
* @return The SHA3 hash of the lower-case hexadecimal encoding of the
128
* input address.
129
*/
130
function sha3HexAddress(address addr) private returns (bytes32 ret) {
131
addr; ret; // Stop warning us about unused variables
132
assembly {
133
let lookup := 0x3031323334353637383961626364656600000000000000000000000000000000
134
let i := 40
135
loop:
136
i := sub(i, 1)
137
mstore8(i, byte(and(addr, 0xf), lookup))
138
addr := div(addr, 0x10)
139
i := sub(i, 1)
140
mstore8(i, byte(and(addr, 0xf), lookup))
141
addr := div(addr, 0x10)
142
jumpi(loop, i)
143
ret := sha3(0, 40)
144
}
145
}
146
}
Copied!
Copyright and related rights waived via CC0.