Skip to content
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

Open
wants to merge 37 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
23c4a72
Add L2 reverse registrar contract
Jun 5, 2023
c763636
WIP
jefflau Aug 15, 2023
21c5d36
Refactor L2 Reverse registrar to incorporate the resolver within
jefflau Oct 6, 2023
29fb8d7
Add boilerplate for tests
jefflau Oct 6, 2023
7d632cb
Remove setNameForAddr()
jefflau Oct 6, 2023
0c67b45
WIP
jefflau Oct 9, 2023
1e464bd
Add setName test, remove setNameForAddr()
jefflau Oct 12, 2023
27854e4
Update OZ library to 4.9.3, add setNameWithAddrWithSignature tests
jefflau Oct 12, 2023
669081c
Add mocks to test ownable with signature
jefflau Oct 19, 2023
fea6ecb
Fix Natspec
jefflau Oct 19, 2023
4ef442f
Add signature setting for text records
jefflau Oct 19, 2023
c9274e3
Add tests for TextResolver
jefflau Oct 19, 2023
de9959a
Remove relayer
jefflau Oct 24, 2023
a104c83
Change expiry to inception date
jefflau Oct 24, 2023
2b66f4f
Add block.timestamp if first time setting
jefflau Oct 26, 2023
7530428
Merge text/name resolver into main contract
jefflau Nov 1, 2023
37eeec4
WIP multicall
jefflau Nov 6, 2023
eea2bd9
Add L2 reverse registrar deploy script
jefflau Nov 6, 2023
569f07a
Add tests for multicall
jefflau Nov 6, 2023
d25e521
Fix inceptionDate comparisons
jefflau Nov 6, 2023
5ed5fa9
Refactor setName/Text
jefflau Nov 8, 2023
369cc4a
Remove L2 ReverseClaimer.sol
jefflau Dec 6, 2023
6ed6d5f
Remove profiles in readme
jefflau Dec 6, 2023
4136a75
Remove L2 ReverseResolverBase contract and merge into registrar
jefflau Dec 6, 2023
26fc75a
Move supportsInterface below
jefflau Dec 6, 2023
4ab3a85
Test for clearRecordsWithSignature
jefflau Dec 7, 2023
3c93765
Add tests for inceptionDate
jefflau Dec 7, 2023
9bfdad0
Fix formatting
jefflau Dec 7, 2023
6a86103
Keep events together
jefflau Dec 13, 2023
dfb000d
lower case variable
jefflau Dec 13, 2023
dc6e94d
SignatureOutOfDate() + comment for lookup
jefflau Dec 18, 2023
9eb42f1
Add custom error to isAuthorised()
jefflau Dec 19, 2023
cbba61f
Move authorisation of ownable funcs to modifier
jefflau Dec 19, 2023
3b9e8d7
WIP add cointype
jefflau Dec 20, 2023
27ad314
Move to a double hash signature
jefflau Dec 20, 2023
d2315a9
Add coinType to prevent crosschain replay attacks
jefflau Dec 20, 2023
a97255a
Add namespace to deploy script
jefflau Dec 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .vscode/settings.json
Copy link
Member

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.

This file was deleted.

24 changes: 24 additions & 0 deletions contracts/reverseRegistrar/IL2ReverseRegistrar.sol
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to return bytes32 for transactions? setName on L1 doesn't return anything https://github.com/ensdomains/ens-contracts/blob/staging/contracts/resolvers/profiles/NameResolver.sol#L15 . The same applies to all set* functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This point still stands

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does relayer need to be specified here?

Copy link
Member Author

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
21 changes: 21 additions & 0 deletions contracts/reverseRegistrar/L2ReverseClaimer.sol
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?
203 changes: 203 additions & 0 deletions contracts/reverseRegistrar/L2ReverseRegistrar.sol
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);
}
}
9 changes: 9 additions & 0 deletions contracts/reverseRegistrar/README.md
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.
Copy link
Member

Choose a reason for hiding this comment

The 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
40 changes: 40 additions & 0 deletions contracts/reverseRegistrar/profiles/L2NameResolver.sol
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this different to NameResolver? Can't we avoid the need for duplication here by implementing the L2-specific functionality as a mixin at the top of the inheritance hierarchy, instead of the bottom?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 bytes32 node. Whereas the reverse registrar passes through address for authorisation. I wasn't sure how to change the modifier here without duplicating the contracts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we call _setName in the parent contract, is there any point in making these separate contracts at all, then?

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);
}
}
73 changes: 73 additions & 0 deletions contracts/reverseRegistrar/profiles/L2ReverseResolverBase.sol
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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining what lookup is?

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arachnid do you know what this is?

Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation code of clearRecords and clearRecordsWithSignature are identical (only difference in modifier). Can you refactor to avoid code duplication?

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)
}
}
}
Loading