/
UniversalResolver.sol
594 lines (544 loc) · 19.1 KB
/
UniversalResolver.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.17 <0.9.0;
import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {LowLevelCallUtils} from "./LowLevelCallUtils.sol";
import {ENS} from "../registry/ENS.sol";
import {IExtendedResolver} from "../resolvers/profiles/IExtendedResolver.sol";
import {Resolver, INameResolver, IAddrResolver} from "../resolvers/Resolver.sol";
import {NameEncoder} from "./NameEncoder.sol";
import {BytesUtils} from "../wrapper/BytesUtils.sol";
import {HexUtils} from "./HexUtils.sol";
error OffchainLookup(
address sender,
string[] urls,
bytes callData,
bytes4 callbackFunction,
bytes extraData
);
error ResolverNotFound();
error ResolverWildcardNotSupported();
struct MulticallData {
bytes name;
bytes[] data;
string[] gateways;
bytes4 callbackFunction;
bool isWildcard;
address resolver;
bytes metaData;
bool[] failures;
}
struct OffchainLookupCallData {
address sender;
string[] urls;
bytes callData;
}
struct OffchainLookupExtraData {
bytes4 callbackFunction;
bytes data;
}
interface BatchGateway {
function query(
OffchainLookupCallData[] memory data
) external returns (bool[] memory failures, bytes[] memory responses);
}
/**
* The Universal Resolver is a contract that handles the work of resolving a name entirely onchain,
* making it possible to make a single smart contract call to resolve an ENS name.
*/
contract UniversalResolver is ERC165, Ownable {
using Address for address;
using NameEncoder for string;
using BytesUtils for bytes;
using HexUtils for bytes;
string[] public batchGatewayURLs;
ENS public immutable registry;
constructor(address _registry, string[] memory _urls) {
registry = ENS(_registry);
batchGatewayURLs = _urls;
}
function setGatewayURLs(string[] memory _urls) public onlyOwner {
batchGatewayURLs = _urls;
}
/**
* @dev Performs ENS name resolution for the supplied name and resolution data.
* @param name The name to resolve, in normalised and DNS-encoded form.
* @param data The resolution data, as specified in ENSIP-10.
* @return The result of resolving the name.
*/
function resolve(
bytes calldata name,
bytes memory data
) external view returns (bytes memory, address) {
return
_resolveSingle(
name,
data,
batchGatewayURLs,
this.resolveSingleCallback.selector,
""
);
}
function resolve(
bytes calldata name,
bytes[] memory data
) external view returns (bytes[] memory, address) {
return resolve(name, data, batchGatewayURLs);
}
function resolve(
bytes calldata name,
bytes memory data,
string[] memory gateways
) external view returns (bytes memory, address) {
return
_resolveSingle(
name,
data,
gateways,
this.resolveSingleCallback.selector,
""
);
}
function resolve(
bytes calldata name,
bytes[] memory data,
string[] memory gateways
) public view returns (bytes[] memory, address) {
return
_resolve(name, data, gateways, this.resolveCallback.selector, "");
}
function _resolveSingle(
bytes calldata name,
bytes memory data,
string[] memory gateways,
bytes4 callbackFunction,
bytes memory metaData
) public view returns (bytes memory, address) {
bytes[] memory dataArr = new bytes[](1);
dataArr[0] = data;
(bytes[] memory results, address resolver) = _resolve(
name,
dataArr,
gateways,
callbackFunction,
metaData
);
return (results[0], resolver);
}
function _resolve(
bytes calldata name,
bytes[] memory data,
string[] memory gateways,
bytes4 callbackFunction,
bytes memory metaData
) internal view returns (bytes[] memory results, address resolverAddress) {
(Resolver resolver, , uint256 finalOffset) = findResolver(name);
resolverAddress = address(resolver);
if (resolverAddress == address(0)) {
revert ResolverNotFound();
}
bool isWildcard = finalOffset != 0;
results = _multicall(
MulticallData(
name,
data,
gateways,
callbackFunction,
isWildcard,
resolverAddress,
metaData,
new bool[](data.length)
)
);
}
function reverse(
bytes calldata reverseName
) external view returns (string memory, address, address, address) {
return reverse(reverseName, batchGatewayURLs);
}
/**
* @dev Performs ENS name reverse resolution for the supplied reverse name.
* @param reverseName The reverse name to resolve, in normalised and DNS-encoded form. e.g. b6E040C9ECAaE172a89bD561c5F73e1C48d28cd9.addr.reverse
* @return The resolved name, the resolved address, the reverse resolver address, and the resolver address.
*/
function reverse(
bytes calldata reverseName,
string[] memory gateways
) public view returns (string memory, address, address, address) {
bytes memory encodedCall = abi.encodeCall(
INameResolver.name,
reverseName.namehash(0)
);
(
bytes memory resolvedReverseData,
address reverseResolverAddress
) = _resolveSingle(
reverseName,
encodedCall,
gateways,
this.reverseCallback.selector,
""
);
return
getForwardDataFromReverse(
resolvedReverseData,
reverseResolverAddress,
gateways
);
}
function getForwardDataFromReverse(
bytes memory resolvedReverseData,
address reverseResolverAddress,
string[] memory gateways
) internal view returns (string memory, address, address, address) {
string memory resolvedName = abi.decode(resolvedReverseData, (string));
(bytes memory encodedName, bytes32 namehash) = resolvedName
.dnsEncodeName();
bytes memory encodedCall = abi.encodeCall(IAddrResolver.addr, namehash);
bytes memory metaData = abi.encode(
resolvedName,
reverseResolverAddress
);
(bytes memory resolvedData, address resolverAddress) = this
._resolveSingle(
encodedName,
encodedCall,
gateways,
this.reverseCallback.selector,
metaData
);
address resolvedAddress = abi.decode(resolvedData, (address));
return (
resolvedName,
resolvedAddress,
reverseResolverAddress,
resolverAddress
);
}
function resolveSingleCallback(
bytes calldata response,
bytes calldata extraData
) external view returns (bytes memory, address) {
(bytes[] memory results, address resolver, , ) = _resolveCallback(
response,
extraData,
this.resolveSingleCallback.selector
);
return (results[0], resolver);
}
function resolveCallback(
bytes calldata response,
bytes calldata extraData
) external view returns (bytes[] memory, address) {
(bytes[] memory results, address resolver, , ) = _resolveCallback(
response,
extraData,
this.resolveCallback.selector
);
return (results, resolver);
}
function reverseCallback(
bytes calldata response,
bytes calldata extraData
) external view returns (string memory, address, address, address) {
(
bytes[] memory resolvedData,
address resolverAddress,
string[] memory gateways,
bytes memory metaData
) = _resolveCallback(
response,
extraData,
this.reverseCallback.selector
);
if (metaData.length > 0) {
(string memory resolvedName, address reverseResolverAddress) = abi
.decode(metaData, (string, address));
address resolvedAddress = abi.decode(resolvedData[0], (address));
return (
resolvedName,
resolvedAddress,
reverseResolverAddress,
resolverAddress
);
}
return
getForwardDataFromReverse(
resolvedData[0],
resolverAddress,
gateways
);
}
function supportsInterface(
bytes4 interfaceId
) public view virtual override returns (bool) {
return
interfaceId == type(IExtendedResolver).interfaceId ||
super.supportsInterface(interfaceId);
}
function _resolveCallback(
bytes calldata response,
bytes calldata extraData,
bytes4 callbackFunction
)
internal
view
returns (bytes[] memory, address, string[] memory, bytes memory)
{
MulticallData memory multicallData;
multicallData.callbackFunction = callbackFunction;
(bool[] memory failures, bytes[] memory responses) = abi.decode(
response,
(bool[], bytes[])
);
OffchainLookupExtraData[] memory extraDatas;
(
multicallData.isWildcard,
multicallData.resolver,
multicallData.gateways,
multicallData.metaData,
extraDatas
) = abi.decode(
extraData,
(bool, address, string[], bytes, OffchainLookupExtraData[])
);
require(responses.length <= extraDatas.length);
multicallData.data = new bytes[](extraDatas.length);
multicallData.failures = new bool[](extraDatas.length);
uint256 offchainCount = 0;
for (uint256 i = 0; i < extraDatas.length; i++) {
if (extraDatas[i].callbackFunction == bytes4(0)) {
// This call did not require an offchain lookup; use the previous input data.
multicallData.data[i] = extraDatas[i].data;
} else {
if (failures[offchainCount]) {
multicallData.failures[i] = true;
multicallData.data[i] = responses[offchainCount];
} else {
multicallData.data[i] = abi.encodeWithSelector(
extraDatas[i].callbackFunction,
responses[offchainCount],
extraDatas[i].data
);
}
offchainCount = offchainCount + 1;
}
}
return (
_multicall(multicallData),
multicallData.resolver,
multicallData.gateways,
multicallData.metaData
);
}
/**
* @dev Makes a call to `target` with `data`. If the call reverts with an `OffchainLookup` error, wraps
* the error with the data necessary to continue the request where it left off.
* @param target The address to call.
* @param data The data to call `target` with.
* @return offchain Whether the call reverted with an `OffchainLookup` error.
* @return returnData If `target` did not revert, contains the return data from the call to `target`. Otherwise, contains a `OffchainLookupCallData` struct.
* @return extraData If `target` did not revert, is empty. Otherwise, contains a `OffchainLookupExtraData` struct.
* @return result Whether the call succeeded.
*/
function callWithOffchainLookupPropagation(
address target,
bytes memory data
)
internal
view
returns (
bool offchain,
bytes memory returnData,
OffchainLookupExtraData memory extraData,
bool result
)
{
result = LowLevelCallUtils.functionStaticCall(address(target), data);
uint256 size = LowLevelCallUtils.returnDataSize();
if (result) {
return (
false,
LowLevelCallUtils.readReturnData(0, size),
extraData,
true
);
}
// Failure
if (size >= 4) {
bytes memory errorId = LowLevelCallUtils.readReturnData(0, 4);
// Offchain lookup. Decode the revert message and create our own that nests it.
bytes memory revertData = LowLevelCallUtils.readReturnData(
4,
size - 4
);
if (bytes4(errorId) == OffchainLookup.selector) {
(
address wrappedSender,
string[] memory wrappedUrls,
bytes memory wrappedCallData,
bytes4 wrappedCallbackFunction,
bytes memory wrappedExtraData
) = abi.decode(
revertData,
(address, string[], bytes, bytes4, bytes)
);
if (wrappedSender == target) {
returnData = abi.encode(
OffchainLookupCallData(
wrappedSender,
wrappedUrls,
wrappedCallData
)
);
extraData = OffchainLookupExtraData(
wrappedCallbackFunction,
wrappedExtraData
);
return (true, returnData, extraData, false);
}
} else {
returnData = bytes.concat(errorId, revertData);
return (false, returnData, extraData, false);
}
}
}
/**
* @dev Finds a resolver by recursively querying the registry, starting at the longest name and progressively
* removing labels until it finds a result.
* @param name The name to resolve, in DNS-encoded and normalised form.
* @return resolver The Resolver responsible for this name.
* @return namehash The namehash of the full name.
* @return finalOffset The offset of the first label with a resolver.
*/
function findResolver(
bytes calldata name
) public view returns (Resolver, bytes32, uint256) {
(
address resolver,
bytes32 namehash,
uint256 finalOffset
) = findResolver(name, 0);
return (Resolver(resolver), namehash, finalOffset);
}
function findResolver(
bytes calldata name,
uint256 offset
) internal view returns (address, bytes32, uint256) {
uint256 labelLength = uint256(uint8(name[offset]));
if (labelLength == 0) {
return (address(0), bytes32(0), offset);
}
uint256 nextLabel = offset + labelLength + 1;
bytes32 labelHash;
if (
labelLength == 66 &&
// 0x5b == '['
name[offset + 1] == 0x5b &&
// 0x5d == ']'
name[nextLabel - 1] == 0x5d
) {
// Encrypted label
(labelHash, ) = bytes(name[offset + 2:nextLabel - 1])
.hexStringToBytes32(0, 64);
} else {
labelHash = keccak256(name[offset + 1:nextLabel]);
}
(
address parentresolver,
bytes32 parentnode,
uint256 parentoffset
) = findResolver(name, nextLabel);
bytes32 node = keccak256(abi.encodePacked(parentnode, labelHash));
address resolver = registry.resolver(node);
if (resolver != address(0)) {
return (resolver, node, offset);
}
return (parentresolver, node, parentoffset);
}
function _hasExtendedResolver(
address resolver
) internal view returns (bool) {
try
Resolver(resolver).supportsInterface{gas: 50000}(
type(IExtendedResolver).interfaceId
)
returns (bool supported) {
return supported;
} catch {
return false;
}
}
function _multicall(
MulticallData memory multicallData
) internal view returns (bytes[] memory results) {
uint256 length = multicallData.data.length;
uint256 offchainCount = 0;
OffchainLookupCallData[]
memory callDatas = new OffchainLookupCallData[](length);
OffchainLookupExtraData[]
memory extraDatas = new OffchainLookupExtraData[](length);
results = new bytes[](length);
bool isCallback = multicallData.name.length == 0;
bool hasExtendedResolver = _hasExtendedResolver(multicallData.resolver);
if (multicallData.isWildcard && !hasExtendedResolver) {
revert ResolverWildcardNotSupported();
}
for (uint256 i = 0; i < length; i++) {
bytes memory item = multicallData.data[i];
bool failure = multicallData.failures[i];
if (failure) {
results[i] = item;
continue;
}
if (!isCallback && hasExtendedResolver) {
item = abi.encodeCall(
IExtendedResolver.resolve,
(multicallData.name, item)
);
}
(
bool offchain,
bytes memory returnData,
OffchainLookupExtraData memory extraData,
bool success
) = callWithOffchainLookupPropagation(multicallData.resolver, item);
if (offchain) {
callDatas[offchainCount] = abi.decode(
returnData,
(OffchainLookupCallData)
);
extraDatas[i] = extraData;
offchainCount += 1;
continue;
}
if (success && hasExtendedResolver) {
// if this is a successful resolve() call, unwrap the result
returnData = abi.decode(returnData, (bytes));
}
results[i] = returnData;
extraDatas[i].data = multicallData.data[i];
}
if (offchainCount == 0) {
return results;
}
// Trim callDatas if offchain data exists
assembly {
mstore(callDatas, offchainCount)
}
revert OffchainLookup(
address(this),
multicallData.gateways,
abi.encodeWithSelector(BatchGateway.query.selector, callDatas),
multicallData.callbackFunction,
abi.encode(
multicallData.isWildcard,
multicallData.resolver,
multicallData.gateways,
multicallData.metaData,
extraDatas
)
);
}
}