-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathL2Registrar.sol
More file actions
89 lines (72 loc) · 3.17 KB
/
L2Registrar.sol
File metadata and controls
89 lines (72 loc) · 3.17 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {StringUtils} from "@ensdomains/ens-contracts/utils/StringUtils.sol";
import {IL2Registry} from "../interfaces/IL2Registry.sol";
/// @dev This is an example registrar contract that is mean to be modified.
contract L2Registrar {
using StringUtils for string;
/// @notice Emitted when a new name is registered
/// @param label The registered label (e.g. "name" in "name.eth")
/// @param owner The owner of the newly registered name
event NameRegistered(string indexed label, address indexed owner);
/// @notice Reference to the target registry contract
IL2Registry public immutable registry;
/// @notice The chainId for the current chain
uint256 public chainId;
/// @notice The coinType for the current chain (ENSIP-11)
uint256 public immutable coinType;
/// @notice Initializes the registrar with a registry contract
/// @param _registry Address of the L2Registry contract
constructor(address _registry) {
// Save the chainId in memory (can only access this in assembly)
assembly {
sstore(chainId.slot, chainid())
}
// Calculate the coinType for the current chain according to ENSIP-11
coinType = (0x80000000 | chainId) >> 0;
// Save the registry address
registry = IL2Registry(_registry);
}
/// @notice Registers a new name
/// @param label The label to register (e.g. "name" for "name.eth")
/// @param owner The address that will own the name
function register(string calldata label, address owner) external {
bytes32 node = _labelToNode(label);
bytes memory addr = abi.encodePacked(owner); // Convert address to bytes
// Set the forward address for the current chain. This is needed for reverse resolution.
// E.g. if this contract is deployed to Base, set an address for chainId 8453 which is
// coinType 2147492101 according to ENSIP-11.
registry.setAddr(node, coinType, addr);
// Set the forward address for mainnet ETH (coinType 60) for easier debugging.
registry.setAddr(node, 60, addr);
// Register the name in the L2 registry
registry.createSubnode(
registry.baseNode(),
label,
owner,
new bytes[](0)
);
emit NameRegistered(label, owner);
}
/// @notice Checks if a given label is available for registration
/// @dev Uses try-catch to handle the ERC721NonexistentToken error
/// @param label The label to check availability for
/// @return available True if the label can be registered, false if already taken
function available(string calldata label) external view returns (bool) {
bytes32 node = _labelToNode(label);
uint256 tokenId = uint256(node);
try registry.ownerOf(tokenId) {
return false;
} catch {
if (label.strlen() >= 3) {
return true;
}
return false;
}
}
function _labelToNode(
string calldata label
) private view returns (bytes32) {
return registry.makeNode(registry.baseNode(), label);
}
}