-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add L2 reverse registrar contract #265
base: dev
Are you sure you want to change the base?
Changes from 10 commits
23c4a72
c763636
21c5d36
29fb8d7
7d632cb
0c67b45
1e464bd
27854e4
669081c
fea6ecb
4ef442f
c9274e3
de9959a
a104c83
2b66f4f
7530428
37eeec4
eea2bd9
569f07a
d25e521
5ed5fa9
369cc4a
6ed6d5f
4136a75
26fc75a
4ab3a85
3c93765
9bfdad0
6a86103
dfb000d
dc6e94d
9eb42f1
cbba61f
3b9e8d7
27ad314
d2315a9
a97255a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pragma solidity >=0.8.4; | ||
|
||
interface IL2ReverseRegistrar { | ||
function setName(string memory name) external returns (bytes32); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This point still stands There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original reverseRegistrar does this: https://github.com/ensdomains/ens-contracts/blob/staging/contracts/reverseRegistrar/IReverseRegistrar.sol There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it the contract you added several months ago? What was the intention at that time? If it's wrong then it should be fixed now unless it requires some sort of complex migration work as the change itself is not so critical |
||
|
||
function setNameForAddrWithSignature( | ||
address addr, | ||
string memory name, | ||
address relayer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking if someone wanted to be more restrictive on who could send the tx, but maybe it doesn't matter given that all of the arguments are set within the signature. |
||
uint256 signatureExpiry, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if, rather than using expiry, we should use inception? That gives us an ordering over signatures and we know that we can ignore earlier ones if we have a later one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by inception? Do you mean a nonce-based system? |
||
bytes memory signature | ||
) external returns (bytes32); | ||
|
||
function setNameForAddrWithSignatureAndOwnable( | ||
address contractAddr, | ||
makoto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address owner, | ||
string memory name, | ||
address relayer, | ||
uint256 signatureExpiry, | ||
bytes memory signature | ||
) external returns (bytes32); | ||
|
||
function node(address addr) external view returns (bytes32); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.17 <0.9.0; | ||
|
||
import {ENS} from "../registry/ENS.sol"; | ||
import {IL2ReverseRegistrar} from "../reverseRegistrar/IL2ReverseRegistrar.sol"; | ||
|
||
contract L2ReverseClaimer { | ||
constructor( | ||
address l2ReverseRegistrarAddr, | ||
ENS reverseRegistrar, | ||
makoto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address claimant | ||
) { | ||
IL2ReverseRegistrar reverseRegistrar = IL2ReverseRegistrar( | ||
l2ReverseRegistrarAddr | ||
); | ||
//reverseRegistrar.setName(claimant); | ||
makoto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
// TODO: do we need a way of claiming a reverse node | ||
// so that contracts can delegate ownership to an EoA/Smartcontract? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
pragma solidity >=0.8.4; | ||
|
||
import "../registry/ENS.sol"; | ||
import "./IL2ReverseRegistrar.sol"; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol"; | ||
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; | ||
import "../root/Controllable.sol"; | ||
import "./profiles/L2NameResolver.sol"; | ||
import "./profiles/L2TextResolver.sol"; | ||
import "./profiles/L2ReverseResolverBase.sol"; | ||
|
||
error InvalidSignature(); | ||
|
||
contract L2ReverseRegistrar is | ||
Ownable, | ||
IL2ReverseRegistrar, | ||
L2ReverseResolverBase, | ||
L2NameResolver, | ||
L2TextResolver | ||
{ | ||
using ECDSA for bytes32; | ||
|
||
event ReverseClaimed(address indexed addr, bytes32 indexed node); | ||
event DefaultResolverChanged(L2NameResolver indexed resolver); | ||
|
||
uint256 constant EXPIRY = 1 days; | ||
|
||
/** | ||
* @dev Constructor | ||
*/ | ||
constructor(bytes32 L2ReverseNode) L2ReverseResolverBase(L2ReverseNode) {} | ||
|
||
modifier authorised(address addr) override(L2ReverseResolverBase) { | ||
isAuthorised(addr); | ||
_; | ||
} | ||
|
||
function isAuthorised(address addr) internal view override returns (bool) { | ||
require( | ||
jefflau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addr == msg.sender || ownsContract(addr, msg.sender), | ||
"ReverseRegistrar: Caller is not a controller or authorised by address or the address itself" | ||
); | ||
} | ||
|
||
/** | ||
* @dev Sets the name for an addr using a signature that can be verified with ERC1271. | ||
* @param addr The reverse record to set | ||
* @param name The name of the reverse record | ||
* @param relayer The relayer of the transaction. Can be address(0) if the user does not want to restrict | ||
* @param signatureExpiry The resolver of the reverse node | ||
* @param signature The resolver of the reverse node | ||
* @return The ENS node hash of the reverse record. | ||
*/ | ||
function setNameForAddrWithSignature( | ||
address addr, | ||
string memory name, | ||
address relayer, | ||
uint256 signatureExpiry, | ||
bytes memory signature | ||
) public override returns (bytes32) { | ||
bytes32 labelHash = sha3HexAddress(addr); | ||
bytes32 reverseNode = keccak256( | ||
abi.encodePacked(L2_REVERSE_NODE, labelHash) | ||
); | ||
|
||
bytes32 hash = keccak256( | ||
abi.encodePacked( | ||
IL2ReverseRegistrar.setNameForAddrWithSignature.selector, | ||
addr, | ||
name, | ||
relayer, | ||
signatureExpiry | ||
) | ||
); | ||
|
||
bytes32 message = hash.toEthSignedMessageHash(); | ||
|
||
if ( | ||
jefflau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
!SignatureChecker.isValidSignatureNow(addr, message, signature) || | ||
(relayer != address(0) && relayer != msg.sender) || | ||
signatureExpiry < block.timestamp || | ||
signatureExpiry > block.timestamp + EXPIRY | ||
) { | ||
revert InvalidSignature(); | ||
} | ||
|
||
_setName(reverseNode, name); | ||
return reverseNode; | ||
} | ||
|
||
/** | ||
* @dev Sets the name for a contract that is owned by a SCW using a signature | ||
* @param contractAddr The reverse node to set | ||
* @param owner The owner of the contract (via Ownable) | ||
* @param name The name of the reverse record | ||
* @param relayer The relayer of the transaction. Can be address(0) if the user does not want to restrict | ||
* @param signatureExpiry The resolver of the reverse node | ||
* @param signature The signature of an address that will return true on isValidSignature for the owner | ||
* @return The ENS node hash of the reverse record. | ||
*/ | ||
function setNameForAddrWithSignatureAndOwnable( | ||
address contractAddr, | ||
address owner, | ||
string memory name, | ||
address relayer, | ||
uint256 signatureExpiry, | ||
bytes memory signature | ||
) public returns (bytes32) { | ||
bytes32 labelHash = sha3HexAddress(contractAddr); | ||
bytes32 reverseNode = keccak256( | ||
abi.encodePacked(L2_REVERSE_NODE, labelHash) | ||
); | ||
|
||
bytes32 hash = keccak256( | ||
jefflau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
abi.encodePacked( | ||
IL2ReverseRegistrar.setNameForAddrWithSignature.selector, | ||
contractAddr, | ||
owner, | ||
name, | ||
relayer, | ||
signatureExpiry | ||
) | ||
); | ||
|
||
bytes32 message = hash.toEthSignedMessageHash(); | ||
|
||
if ( | ||
ownsContract(contractAddr, owner) && | ||
SignatureChecker.isValidERC1271SignatureNow( | ||
owner, | ||
message, | ||
signature | ||
) | ||
) { | ||
_setName(reverseNode, name); | ||
return reverseNode; | ||
} | ||
|
||
revert InvalidSignature(); | ||
} | ||
|
||
/** | ||
* @dev Sets the `name()` record for the reverse ENS record associated with | ||
* the calling account. | ||
* @param name The name to set for this address. | ||
* @return The ENS node hash of the reverse record. | ||
*/ | ||
function setName(string memory name) public override returns (bytes32) { | ||
return setNameForAddr(msg.sender, name); | ||
} | ||
|
||
/** | ||
* @dev Sets the `name()` record for the reverse ENS record associated with | ||
* the addr provided account. | ||
* @param name The name to set for this address. | ||
* @return The ENS node hash of the reverse record. | ||
*/ | ||
|
||
function setNameForAddr( | ||
address addr, | ||
string memory name | ||
) public authorised(addr) returns (bytes32) { | ||
bytes32 labelHash = sha3HexAddress(addr); | ||
bytes32 node = keccak256(abi.encodePacked(L2_REVERSE_NODE, labelHash)); | ||
_setName(node, name); | ||
return node; | ||
} | ||
|
||
/** | ||
* @dev Returns the node hash for a given account's reverse records. | ||
* @param addr The address to hash | ||
* @return The ENS node hash. | ||
*/ | ||
function node(address addr) public view override returns (bytes32) { | ||
return | ||
keccak256(abi.encodePacked(L2_REVERSE_NODE, sha3HexAddress(addr))); | ||
} | ||
|
||
function ownsContract( | ||
address contractAddr, | ||
address addr | ||
) internal view returns (bool) { | ||
try Ownable(contractAddr).owner() returns (address owner) { | ||
return owner == addr; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function supportsInterface( | ||
bytes4 interfaceID | ||
) | ||
public | ||
view | ||
override(L2NameResolver, L2TextResolver, L2ReverseResolverBase) | ||
returns (bool) | ||
{ | ||
return | ||
interfaceID == type(IL2ReverseRegistrar).interfaceId || | ||
super.supportsInterface(interfaceID); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# L2 Reverse Registrar | ||
|
||
## Summary | ||
|
||
The L2 Reverse registrar is a combination of a resolver and a reverse registrar that allows the name to be set for a particular reverse node. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some more docs would be good! |
||
|
||
## Resolver Profiles | ||
|
||
Profiles are separate from resolvers as the authorised modifier needs the address, rather than the node | ||
jefflau marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.4; | ||
|
||
import "./L2ReverseResolverBase.sol"; | ||
import "../../resolvers/profiles/INameResolver.sol"; | ||
|
||
abstract contract L2NameResolver is INameResolver, L2ReverseResolverBase { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this different to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the problem I had was, I needed access to the address not the node in authorisation. And the original resolver uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given we call |
||
mapping(uint64 => mapping(bytes32 => string)) versionable_names; | ||
|
||
/** | ||
* Sets the name associated with an ENS node, for reverse records. | ||
* May only be called by the owner of that node in the ENS registry. | ||
* @param node The node to update. | ||
* @param newName name record | ||
*/ | ||
function _setName(bytes32 node, string memory newName) internal virtual { | ||
versionable_names[recordVersions[node]][node] = newName; | ||
emit NameChanged(node, newName); | ||
} | ||
|
||
/** | ||
* Returns the name associated with an ENS node, for reverse records. | ||
* Defined in EIP181. | ||
* @param node The ENS node to query. | ||
* @return The associated name. | ||
*/ | ||
function name( | ||
bytes32 node | ||
) external view virtual override returns (string memory) { | ||
return versionable_names[recordVersions[node]][node]; | ||
} | ||
|
||
function supportsInterface( | ||
bytes4 interfaceID | ||
) public view virtual override returns (bool) { | ||
return | ||
interfaceID == type(INameResolver).interfaceId || | ||
super.supportsInterface(interfaceID); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.4; | ||
|
||
import "@openzeppelin/contracts/utils/introspection/ERC165.sol"; | ||
import "../../resolvers/profiles/IVersionableResolver.sol"; | ||
|
||
abstract contract L2ReverseResolverBase is ERC165 { | ||
mapping(bytes32 => uint64) internal recordVersions; | ||
event VersionChanged(bytes32 indexed node, uint64 newVersion); | ||
bytes32 public immutable L2_REVERSE_NODE; | ||
|
||
bytes32 constant lookup = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment explaining what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually not 100% sure, this is lifted from the original reverse registar There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Arachnid do you know what this is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just '0123456789abcdef' in hex form; it allows quickly mapping from a number from 0-15 to a hexadecimal character. |
||
0x3031323334353637383961626364656600000000000000000000000000000000; | ||
|
||
function isAuthorised(address addr) internal view virtual returns (bool); | ||
|
||
constructor(bytes32 l2ReverseNode) { | ||
L2_REVERSE_NODE = l2ReverseNode; | ||
} | ||
|
||
modifier authorised(address addr) virtual { | ||
require(isAuthorised(addr)); | ||
_; | ||
} | ||
|
||
/** | ||
* Increments the record version associated with an ENS node. | ||
* May only be called by the owner of that node in the ENS registry. | ||
* @param addr The node to update. | ||
*/ | ||
function clearRecords(address addr) public virtual authorised(addr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add test for this function as the implementation defers a lot from the L1 equivalent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation code of |
||
bytes32 labelHash = sha3HexAddress(addr); | ||
bytes32 reverseNode = keccak256( | ||
abi.encodePacked(L2_REVERSE_NODE, labelHash) | ||
); | ||
recordVersions[reverseNode]++; | ||
emit VersionChanged(reverseNode, recordVersions[reverseNode]); | ||
} | ||
|
||
function supportsInterface( | ||
bytes4 interfaceID | ||
) public view virtual override returns (bool) { | ||
return | ||
interfaceID == type(IVersionableResolver).interfaceId || | ||
super.supportsInterface(interfaceID); | ||
} | ||
|
||
/** | ||
* @dev An optimised function to compute the sha3 of the lower-case | ||
* hexadecimal representation of an Ethereum address. | ||
* @param addr The address to hash | ||
* @return ret The SHA3 hash of the lower-case hexadecimal encoding of the | ||
* input address. | ||
*/ | ||
function sha3HexAddress(address addr) internal pure returns (bytes32 ret) { | ||
assembly { | ||
for { | ||
let i := 40 | ||
} gt(i, 0) { | ||
|
||
} { | ||
i := sub(i, 1) | ||
mstore8(i, byte(and(addr, 0xf), lookup)) | ||
addr := div(addr, 0x10) | ||
i := sub(i, 1) | ||
mstore8(i, byte(and(addr, 0xf), lookup)) | ||
addr := div(addr, 0x10) | ||
} | ||
|
||
ret := keccak256(0, 40) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be added to .gitignore.