-
Notifications
You must be signed in to change notification settings - Fork 4
/
ETHRegistrarController.sol
282 lines (248 loc) · 7.96 KB
/
ETHRegistrarController.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
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
pragma solidity >=0.8.4;
import "./BaseRegistrarImplementation.sol";
import "./StringUtils.sol";
import "../resolvers/Resolver.sol";
import "../registry/ReverseRegistrar.sol";
import "./IETHRegistrarController.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "../wrapper/INameWrapper.sol";
/**
* @dev A registrar controller for registering and renewing names at fixed cost.
*/
contract ETHRegistrarController is Ownable, IETHRegistrarController {
using StringUtils for *;
using Address for address;
uint256 public constant MIN_REGISTRATION_DURATION = 28 days;
bytes32 private constant ETH_NODE =
0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae;
BaseRegistrarImplementation immutable base;
IPriceOracle public immutable prices;
uint256 public immutable minCommitmentAge;
uint256 public immutable maxCommitmentAge;
ReverseRegistrar public immutable reverseRegistrar;
INameWrapper public immutable nameWrapper;
mapping(bytes32 => uint256) public commitments;
event NameRegistered(
string name,
bytes32 indexed label,
address indexed owner,
uint256 baseCost,
uint256 premium,
uint256 expires
);
event NameRenewed(
string name,
bytes32 indexed label,
uint256 cost,
uint256 expires
);
constructor(
BaseRegistrarImplementation _base,
IPriceOracle _prices,
uint256 _minCommitmentAge,
uint256 _maxCommitmentAge,
ReverseRegistrar _reverseRegistrar,
INameWrapper _nameWrapper
) {
require(_maxCommitmentAge > _minCommitmentAge);
base = _base;
prices = _prices;
minCommitmentAge = _minCommitmentAge;
maxCommitmentAge = _maxCommitmentAge;
reverseRegistrar = _reverseRegistrar;
nameWrapper = _nameWrapper;
}
function rentPrice(string memory name, uint256 duration)
public
view
override
returns (IPriceOracle.Price memory price)
{
bytes32 label = keccak256(bytes(name));
price = prices.price(name, base.nameExpires(uint256(label)), duration);
}
function valid(string memory name) public pure returns (bool) {
return name.strlen() >= 3;
}
function available(string memory name) public view override returns (bool) {
bytes32 label = keccak256(bytes(name));
return valid(name) && base.available(uint256(label));
}
function makeCommitment(
string memory name,
address owner,
uint256 duration,
bytes32 secret,
address resolver,
bytes[] calldata data,
bool reverseRecord,
uint32 fuses,
uint64 wrapperExpiry
) public pure override returns (bytes32) {
bytes32 label = keccak256(bytes(name));
if (data.length > 0) {
require(
resolver != address(0),
"ETHRegistrarController: resolver is required when data is supplied"
);
}
return
keccak256(
abi.encode(
label,
owner,
duration,
resolver,
data,
secret,
reverseRecord,
fuses,
wrapperExpiry
)
);
}
function commit(bytes32 commitment) public override {
require(commitments[commitment] + maxCommitmentAge < block.timestamp);
commitments[commitment] = block.timestamp;
}
function register(
string calldata name,
address owner,
uint256 duration,
bytes32 secret,
address resolver,
bytes[] calldata data,
bool reverseRecord,
uint32 fuses,
uint64 wrapperExpiry
) public payable override {
IPriceOracle.Price memory price = rentPrice(name, duration);
require(
msg.value >= (price.base + price.premium),
"ETHRegistrarController: Not enough ether provided"
);
_consumeCommitment(
name,
duration,
makeCommitment(
name,
owner,
duration,
secret,
resolver,
data,
reverseRecord,
fuses,
wrapperExpiry
)
);
uint256 expires = nameWrapper.registerAndWrapETH2LD(
name,
owner,
duration,
resolver,
fuses,
wrapperExpiry
);
_setRecords(resolver, keccak256(bytes(name)), data);
if (reverseRecord) {
_setReverseRecord(name, resolver, msg.sender);
}
emit NameRegistered(
name,
keccak256(bytes(name)),
owner,
price.base,
price.premium,
expires
);
if (msg.value > (price.base + price.premium)) {
payable(msg.sender).transfer(
msg.value - (price.base + price.premium)
);
}
}
function renew(string calldata name, uint256 duration)
external
payable
override
{
bytes32 label = keccak256(bytes(name));
IPriceOracle.Price memory price = rentPrice(name, duration);
require(
msg.value >= price.base,
"ETHController: Not enough Ether provided for renewal"
);
uint256 expires = base.renew(uint256(label), duration);
if (msg.value > price.base) {
payable(msg.sender).transfer(msg.value - price.base);
}
emit NameRenewed(name, label, msg.value, expires);
}
function withdraw() public {
payable(owner()).transfer(address(this).balance);
}
function supportsInterface(bytes4 interfaceID)
external
pure
returns (bool)
{
return
interfaceID == type(IERC165).interfaceId ||
interfaceID == type(IETHRegistrarController).interfaceId;
}
/* Internal functions */
function _consumeCommitment(
string memory name,
uint256 duration,
bytes32 commitment
) internal {
// Require a valid commitment (is old enough and is committed)
require(
commitments[commitment] + minCommitmentAge <= block.timestamp,
"ETHRegistrarController: Commitment is not valid"
);
// If the commitment is too old, or the name is registered, stop
require(
commitments[commitment] + maxCommitmentAge > block.timestamp,
"ETHRegistrarController: Commitment has expired"
);
require(available(name), "ETHRegistrarController: Name is unavailable");
delete (commitments[commitment]);
require(duration >= MIN_REGISTRATION_DURATION);
}
function _setRecords(
address resolver,
bytes32 label,
bytes[] calldata data
) internal {
// use hardcoded .eth namehash
bytes32 nodehash = keccak256(abi.encodePacked(ETH_NODE, label));
for (uint256 i = 0; i < data.length; i++) {
// check first few bytes are namehash
bytes32 txNamehash = bytes32(data[i][4:36]);
require(
txNamehash == nodehash,
"ETHRegistrarController: Namehash on record do not match the name being registered"
);
resolver.functionCall(
data[i],
"ETHRegistrarController: Failed to set Record"
);
}
}
function _setReverseRecord(
string memory name,
address resolver,
address owner
) internal {
reverseRegistrar.setNameForAddr(
msg.sender,
owner,
resolver,
string.concat(name, ".eth")
);
}
}