DNS Example

Requirements Specification

(derived from SDD/Goals):
  1. 1.
    The DNS contract shall allow any user to register a name that has not already been registered to another owner. (sha256[:3] is c84d)
  2. 2.
    The DNS contract shall reject any request to register a name that is already registered to another user. (sha256[:3] is 35a4)
  3. 3.
    The DNS contract shall allow the owner of a domain to change the resolved address. (sha256[:3] is e532)
  4. 4.
    The DNS contract shall allow anyone to perform a lookup of a registered domain’s resolved address. (sha256[:3] is fffc)

Implementation:

DNS.sol
1
/** @title DNS Registrar */
2
contract DNS {
3
// Store the owner of a particular domain
4
mapping(string => address) public domainOwner;
5
6
// Store the resolution of a particular domain
7
/// @imp fffc `public` provides access method for domain resolver
8
mapping(string => address) public domainResolution;
9
10
/** @title setDomainResolution Allows user to set resolution for address
11
* @param _domain Domain name as ASCII string
12
* @param _resolution Address domain will resolve to
13
*/
14
function setDomainResolution(
15
string _domain,
16
address _resolution
17
)
18
public
19
{
20
/// @imp c84d Check if name has been previously registered
21
if (domainOwner[_domain] != 0x0)
22
{
23
/// @imp 35a4 Reject if domain has been registered to another user
24
require(domainOwner[_domain] == msg.sender);
25
}
26
else // domain owner is unset for this domain
27
{
28
/// @imp c84d Keep track of newly registered domain
29
domainOwner[_domain] = msg.sender;
30
}
31
32
/// @imp c84d Allow user to set resolution for newly registered domain
33
/// @imp e532 Allow user to change resolution for a domain they own
34
domainResolution[_domain] = _resolution;
35
}
36
}
Copied!