-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathL2Registry.sol
More file actions
296 lines (247 loc) · 10.7 KB
/
Copy pathL2Registry.sol
File metadata and controls
296 lines (247 loc) · 10.7 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// ***********************************************
// ▗▖ ▗▖ ▗▄▖ ▗▖ ▗▖▗▄▄▄▖ ▗▄▄▖▗▄▄▄▖▗▄▖ ▗▖ ▗▖▗▄▄▄▖
// ▐▛▚▖▐▌▐▌ ▐▌▐▛▚▞▜▌▐▌ ▐▌ █ ▐▌ ▐▌▐▛▚▖▐▌▐▌
// ▐▌ ▝▜▌▐▛▀▜▌▐▌ ▐▌▐▛▀▀▘ ▝▀▚▖ █ ▐▌ ▐▌▐▌ ▝▜▌▐▛▀▀▘
// ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▙▄▄▖▗▄▄▞▘ █ ▝▚▄▞▘▐▌ ▐▌▐▙▄▄▖
// ***********************************************
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {NameEncoder} from "@ensdomains/ens-contracts/utils/NameEncoder.sol";
import {ENSDNSUtils} from "./lib/ENSDNSUtils.sol";
import {L2Resolver} from "./L2Resolver.sol";
/// @title Durin Registry
/// @author NameStone
/// @notice Manages ENS subname registration and management on L2
/// @dev Combined Registry, BaseRegistrar and PublicResolver from the official .eth contracts
contract L2Registry is ERC721, Initializable, L2Resolver {
/*//////////////////////////////////////////////////////////////
STATE VARIABLES
//////////////////////////////////////////////////////////////*/
/// @notice The base node for the registry
/// @dev namehash of `name()`
bytes32 public baseNode;
/// @notice Total number of subnames
/// @dev Includes names at any depth
uint256 public totalSupply;
string private _tokenName;
string private _tokenSymbol;
string private _tokenBaseURI;
/// @notice Mapping of node (namehash) to name (DNS-encoded)
mapping(bytes32 node => bytes name) public names;
/// @notice Mapping of approved registrar controllers
mapping(address registrar => bool approved) public registrars;
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
/// @notice Emitted when a name is created at any level
event SubnodeCreated(bytes32 indexed node, bytes name, address owner);
/// @notice Emitted when a subnode is registered at any level
/// @dev Same event signature as the ENS Registry
event NewOwner(
bytes32 indexed parentNode,
bytes32 indexed labelhash,
address owner
);
event RegistrarAdded(address registrar);
event RegistrarRemoved(address registrar);
event BaseURIUpdated(string baseURI);
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error LabelTooShort();
error LabelTooLong(string label);
error NotAvailable(string label, bytes32 parentNode);
/*//////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
/// @dev Only the owner of the node or a registrar can call the function
modifier onlyOwnerOrRegistrar(bytes32 node) {
if (owner(node) != msg.sender && !registrars[msg.sender]) {
revert Unauthorized(node);
}
_;
}
modifier onlyOwner() {
if (owner() != msg.sender) {
revert Unauthorized(baseNode);
}
_;
}
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor() ERC721("", "") {
_disableInitializers();
}
/// @notice Initializes the registry
/// @param tokenName The parent ENS name, and name of the NFT collection
/// @param tokenSymbol The symbol of the NFT collection
/// @param baseURI The base URI of the NFT collection
/// @param admin The address that will be granted admin role
function initialize(
string calldata tokenName,
string calldata tokenSymbol,
string calldata baseURI,
address admin
) external initializer {
(bytes memory dnsEncodedName, bytes32 node) = NameEncoder.dnsEncodeName(
tokenName
);
// ERC721
_tokenName = tokenName;
_tokenSymbol = tokenSymbol;
_setBaseURI(baseURI);
// Registry
baseNode = node;
names[baseNode] = dnsEncodedName;
_safeMint(admin, uint256(node));
totalSupply++;
}
/*//////////////////////////////////////////////////////////////
PUBLIC FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Creates a subnode from a parent node and label
/// @dev Only callable by the owner of the parent node
/// @param node The parent node, e.g. `namehash("name.eth")` for "name.eth"
/// @param label The label of the subnode, e.g. "x" for "x.name.eth"
/// @param _owner The address that will own the subnode
/// @param data The encoded calldata for resolver setters
/// @return The resulting subnode, e.g. `namehash("x.name.eth")` for "x.name.eth"
function createSubnode(
bytes32 node,
string calldata label,
address _owner,
bytes[] calldata data
) external onlyOwnerOrRegistrar(node) returns (bytes32) {
bytes32 subnode = makeNode(node, label);
bytes32 labelhash = keccak256(bytes(label));
bytes memory dnsEncodedName = _addLabel(label, names[node]);
if (owner(subnode) != address(0)) {
revert NotAvailable(label, node);
}
_safeMint(_owner, uint256(subnode));
_multicall(subnode, data);
names[subnode] = dnsEncodedName;
totalSupply++;
emit NewOwner(node, labelhash, _owner);
emit SubnodeCreated(subnode, dnsEncodedName, _owner);
return subnode;
}
/// @notice Helper to derive a node from a name
/// @dev In practice, this should be performed offchain
function namehash(string calldata _name) external pure returns (bytes32) {
(, bytes32 node) = NameEncoder.dnsEncodeName(_name);
return node;
}
/// @notice Helper to decode a DNS-encoded name
/// @dev In practice, this should be performed offchain
function decodeName(
bytes calldata _name
) external pure returns (string memory) {
return ENSDNSUtils.dnsDecode(_name);
}
/// @notice Helper to derive a node from a parent node and label
/// @param parentNode The namehash of the parent, e.g. `namehash("name.eth")` for "name.eth"
/// @param label The label of the subnode, e.g. "x" for "x.name.eth"
/// @return The resulting subnode, e.g. `namehash("x.name.eth")` for "x.name.eth"
function makeNode(
bytes32 parentNode,
string calldata label
) public pure returns (bytes32) {
bytes32 labelhash = keccak256(bytes(label));
return keccak256(abi.encodePacked(parentNode, labelhash));
}
/// @notice The admin of the registry
function owner() public view returns (address) {
return owner(baseNode);
}
/// @notice Returns the address that owns the specified node
/// @dev We need this because `ERC721.ownerOf()` reverts if the token doesn't exist
function owner(bytes32 node) public view returns (address) {
return _ownerOf(uint256(node));
}
/// @notice The name of the NFT collection and base ENS name
function name() public view override returns (string memory) {
return _tokenName;
}
/// @notice The symbol of the NFT collection
function symbol() public view override returns (string memory) {
return _tokenSymbol;
}
/// @notice The base URI for NFT metadata
function _baseURI() internal view override returns (string memory) {
return _tokenBaseURI;
}
/*//////////////////////////////////////////////////////////////
ADMIN FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Adds a new registrar address
/// @param registrar The address to grant registrar role to
/// @dev Only callable by admin role
function addRegistrar(address registrar) external onlyOwner {
registrars[registrar] = true;
emit RegistrarAdded(registrar);
}
/// @notice Removes a registrar address
/// @param registrar The address to revoke registrar role from
/// @dev Only callable by admin role
function removeRegistrar(address registrar) external onlyOwner {
registrars[registrar] = false;
emit RegistrarRemoved(registrar);
}
/// @notice Sets the base URI for token metadata
/// @param baseURI The new base URI
/// @dev Only callable by admin role
function setBaseURI(string calldata baseURI) external onlyOwner {
_setBaseURI(baseURI);
}
/*//////////////////////////////////////////////////////////////
INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
function _setBaseURI(string calldata baseURI) private {
_tokenBaseURI = baseURI;
emit BaseURIUpdated(baseURI);
}
function _addLabel(
string memory label,
bytes memory _name
) private pure returns (bytes memory ret) {
if (bytes(label).length < 1) {
revert LabelTooShort();
}
if (bytes(label).length > 255) {
revert LabelTooLong(label);
}
return abi.encodePacked(uint8(bytes(label).length), label, _name);
}
/*//////////////////////////////////////////////////////////////
OVERRIDES
//////////////////////////////////////////////////////////////*/
/// @dev Returns onchain JSON if no baseURI is set
function tokenURI(
uint256 tokenId
) public view override returns (string memory) {
if (bytes(_tokenBaseURI).length == 0) {
_requireOwned(tokenId);
string memory json = string.concat(
'{"name": "',
ENSDNSUtils.dnsDecode(names[bytes32(tokenId)]),
'"}'
);
return
string.concat(
"data:application/json;base64,",
Base64.encode(bytes(json))
);
}
return super.tokenURI(tokenId);
}
function supportsInterface(
bytes4 interfaceId
) public view override(ERC721, L2Resolver) returns (bool) {
return super.supportsInterface(interfaceId);
}
}