-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTinyENS.sol
65 lines (53 loc) · 2.35 KB
/
TinyENS.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.21;
/// @notice Interface for TinyENS.
interface ITinyENS {
/// @notice Register a new ENS name and link it to an address.
/// @param name The ENS name to register.
function register(string memory name) external;
/// @notice Update the ENS name linked to an address.
/// @param newName The new ENS name.
function update(string memory newName) external;
/// @notice Resolve the address associated with a given ENS name.
/// @param name The ENS name to resolve.
/// @return The address associated with the ENS name.
function resolve(string memory name) external view returns (address);
/// @notice Reverse resolve an address to its associated ENS name.
/// @param targetAddress The target address to reverse resolve.
/// @return The ENS name associated with the address.
function reverse(address targetAddress) external view returns (string memory);
}
/// @title Tiny Ethereum Name Service
/// @author leovct
/// @notice Map human-readable names like 'vitalik.eth' to machine-readable identifiers such as
/// Ethereum addresses like '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' and support the reverse
/// resolution.
contract TinyENS is ITinyENS {
/// @notice Thrown when trying to register a name already registered.
error AlreadyRegistered();
/// @notice Map ENS names to addresses.
mapping(string => address) private registry;
/// @notice Map addresses to ENS names.
mapping(address => string) private reverseRegistry;
modifier notRegistered(string memory name) {
if (registry[name] != address(0)) revert AlreadyRegistered();
_;
}
function register(string memory name) public notRegistered(name) {
registry[name] = msg.sender;
reverseRegistry[msg.sender] = name;
}
function update(string memory newName) external notRegistered(newName) {
// Unlink the old name and the address.
string memory oldName = reverseRegistry[msg.sender];
registry[oldName] = address(0);
// Register the new name.
register(newName);
}
function resolve(string memory name) external view returns (address) {
return registry[name];
}
function reverse(address targetAddress) external view returns (string memory) {
return reverseRegistry[targetAddress];
}
}