Skip to content

Commit

Permalink
docs + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Oct 23, 2024
1 parent c639c25 commit 2971c2b
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 193 deletions.
29 changes: 25 additions & 4 deletions contracts/reverseRegistrar/IL2ReverseResolver.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

/// @notice Interface for the L2 reverse resolver
interface IL2ReverseResolver {
/// @notice Thrown when the specified address is not the owner of the contract
error NotOwnerOfContract();

/// @notice Sets the `name()` record for the reverse ENS record associated with
/// the calling account.
/// @param name The name to set
/// @return The ENS node hash of the reverse record
function setName(string memory name) external returns (bytes32);

/// @notice Sets the `name()` record for the reverse ENS record associated with
/// the addr provided account.
/// Can be used if the addr is a contract that is owned by an SCA.
/// @param addr The address to set the name for
/// @param name The name to set
/// @return The ENS node hash of the reverse record
function setNameForAddr(
address addr,
string memory name
) external returns (bytes32);

/// @notice Sets the `name()` record for the reverse ENS record associated with
/// the contract provided that is owned with `Ownable`.
/// @param contractAddr The address of the contract to set the name for
/// @param owner The owner of the contract (via Ownable)
/// @param name The name to set
/// @param signatureExpiry The expiry of the signature
/// @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,
uint256 inceptionDate,
bytes memory signature
string calldata name,
uint256 signatureExpiry,
bytes calldata signature
) external returns (bytes32);
}
69 changes: 68 additions & 1 deletion contracts/reverseRegistrar/IReverseRegistrar.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

/// @notice Interface for the reverse registrar
interface IReverseRegistrar {
/// @notice Emitted when a reverse record is claimed.
/// @param addr The address that the reverse record is claimed for
/// @param node The ENS node hash of the reverse record
event ReverseClaimed(address indexed addr, bytes32 indexed node);

/// @notice Emitted when the default resolver is changed.
/// @param resolver The resolver that was set
event DefaultResolverChanged(address indexed resolver);

/// @notice Thrown when the caller is not authorised to perform the action
error Unauthorised();

/// @notice Thrown when the resolver address is zero
error ResolverAddressZero();

/// @notice Sets the default resolver
/// @param resolver The resolver to set
function setDefaultResolver(address resolver) external;

/// @notice Transfers ownership of the reverse ENS record associated with the
/// calling account.
/// @param owner The address to set as the owner of the reverse record in ENS.
/// @return The ENS node hash of the reverse record
function claim(address owner) external returns (bytes32);

/// @notice Transfers ownership of the reverse ENS record associated with the
/// addr provided account.
/// @param addr The address to claim the reverse record for
/// @param owner The address to set as the owner of the reverse record
/// @param resolver The resolver of the reverse node
/// @return The ENS node hash of the reverse record
function claimForAddr(
address addr,
address owner,
address resolver
) external returns (bytes32);

/// @notice Transfers ownership of the reverse ENS record associated with the
/// addr provided account using a signature to authorise.
/// @param addr The address to claim the reverse record for
/// @param owner The address to set as the owner of the reverse record
/// @param resolver The resolver of the reverse node
/// @param signatureExpiry The expiry of the signature
/// @param signature The signature to authorise the claim
/// @return The ENS node hash of the reverse record
function claimForAddrWithSignature(
address addr,
address owner,
Expand All @@ -21,20 +56,48 @@ interface IReverseRegistrar {
bytes calldata signature
) external returns (bytes32);

/// @notice Transfers ownership of the reverse ENS record associated with the
/// calling account.
/// @param owner The address to set as the owner of the reverse record
/// @param resolver The resolver of the reverse node
/// @return The ENS node hash of the reverse record
function claimWithResolver(
address owner,
address resolver
) external returns (bytes32);

/// @notice Sets the `name()` record for the reverse ENS record associated
/// with the calling account, and updates the resolver to the
/// default reverse resolver.
/// @param name The name to set for the calling account
/// @return The ENS node hash of the reverse record
function setName(string memory name) external returns (bytes32);

/// @notice Sets the `name()` record for the reverse ENS record associated
/// with the addr provided account, and updates the resolver to the
/// resolver provided.
/// @param addr The reverse record to set
/// @param owner The owner of the reverse node
/// @param resolver The resolver of the reverse node
/// @param name The name to set for the provided address
/// @return The ENS node hash of the reverse record
function setNameForAddr(
address addr,
address owner,
address resolver,
string memory name
) external returns (bytes32);

/// @notice Sets the `name()` record for the reverse ENS record associated
/// with the addr provided account using a signature to authorise,
/// and updates the resolver to the resolver provided.
/// @param addr The reverse record to set
/// @param owner The owner of the reverse node
/// @param resolver The resolver of the reverse node
/// @param signatureExpiry The expiry of the signature
/// @param signature The signature to authorise the claim
/// @param name The name to set for the provided address
/// @return The ENS node hash of the reverse record
function setNameForAddrWithSignature(
address addr,
address owner,
Expand All @@ -44,5 +107,9 @@ interface IReverseRegistrar {
string memory name
) external returns (bytes32);

/// @notice Returns the ENS node hash for the reverse record associated with
/// the addr provided account.
/// @param addr The address to get the reverse node hash for
/// @return The ENS node hash
function node(address addr) external pure returns (bytes32);
}
32 changes: 26 additions & 6 deletions contracts/reverseRegistrar/ISignatureReverseResolver.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

/// @notice Interface for the signature reverse resolver
interface ISignatureReverseResolver {
event ReverseClaimed(address indexed addr, bytes32 indexed node);
event NameChanged(bytes32 indexed node, string name);
/// @notice Emitted when the name of a reverse record is changed.
/// @param addr The address of the reverse record
/// @param node The ENS node hash of the reverse record
/// @param name The name of the reverse record
event NameChanged(address indexed addr, bytes32 indexed node, string name);

/// @notice Sets the `name()` record for the reverse ENS record associated with
/// the addr provided account using a signature.
/// @param addr The address to set the name for
/// @param name The name of the reverse record
/// @param signatureExpiry Date when the signature expires
/// @param signature The signature from the addr
/// @return The ENS node hash of the reverse record
function setNameForAddrWithSignature(
address addr,
string memory name,
uint256 inceptionDate,
bytes memory signature
string calldata name,
uint256 signatureExpiry,
bytes calldata signature
) external returns (bytes32);

/// @notice Returns the name associated with an ENS node hash, for reverse resolution.
/// Defined in ENSIP-3.
/// @param node The ENS node hash to query.
/// @return The associated name.
function name(bytes32 node) external view returns (string memory);

/// @notice Returns the ENS node hash for the reverse record associated with
/// the addr provided account.
/// @param addr The address to get the reverse node hash for
/// @return The ENS node hash
function node(address addr) external view returns (bytes32);
}
61 changes: 18 additions & 43 deletions contracts/reverseRegistrar/L2ReverseResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";

import {ENS} from "../registry/ENS.sol";
import {INameResolver} from "../resolvers/profiles/INameResolver.sol";
import {Multicallable} from "../resolvers/Multicallable.sol";

import {IL2ReverseResolver} from "./IL2ReverseResolver.sol";
import {SignatureReverseResolver, Unauthorised} from "./SignatureReverseResolver.sol";
import {SignatureReverseResolver} from "./SignatureReverseResolver.sol";
import {SignatureUtils} from "./SignatureUtils.sol";

error NotOwnerOfContract();

/**
* A L2 reverse resolver. Deployed to each L2 chain.
*/
/// @title L2 Reverse Resolver
/// @notice An L2 reverse resolver. Deployed to each L2 chain.
contract L2ReverseResolver is
ERC165,
Multicallable,
Expand All @@ -28,35 +22,28 @@ contract L2ReverseResolver is
using SignatureUtils for bytes;
using ECDSA for bytes32;

/// @notice The addr namespace. Equal to the namehash of
/// `${coinTypeHex}.reverse`.
bytes32 public immutable L2ReverseNode;

/*
* @dev Constructor
* @param _L2ReverseNode The namespace to set. The converntion is '${coinType}.reverse'
* @param _coinType The cointype converted from the chainId of the chain this contract is deployed to.
*/
/// @notice Sets the namespace and coin type
/// @param _L2ReverseNode The namespace to set. The converntion is '${coinType}.reverse'
/// @param _coinType The cointype converted from the chainId of the chain this contract is deployed to.
constructor(
bytes32 _L2ReverseNode,
uint256 _coinType
) SignatureReverseResolver(_L2ReverseNode, _coinType) {
L2ReverseNode = _L2ReverseNode;
}

/// @dev Checks if the caller is authorised
function isAuthorised(address addr) internal view override {
if (addr != msg.sender && !ownsContract(addr, msg.sender)) {
revert Unauthorised();
}
}

/**
* @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 signatureExpiry Date when the signature expires
* @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.
*/
/// @inheritdoc IL2ReverseResolver
function setNameForAddrWithSignatureAndOwnable(
address contractAddr,
address owner,
Expand Down Expand Up @@ -87,53 +74,41 @@ contract L2ReverseResolver is

signature.validateSignatureWithExpiry(owner, message, signatureExpiry);

_setName(node, name);
emit ReverseClaimed(contractAddr, node);

_setName(contractAddr, node, name);
return node;
}

/**
* @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.
*/
/// @inheritdoc IL2ReverseResolver
function setName(string calldata 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.
* Can be used if the addr is a contract that is owned by a SCW.
* @param name The name to set for this address.
* @return The ENS node hash of the reverse record.
*/

/// @inheritdoc IL2ReverseResolver
function setNameForAddr(
address addr,
string calldata name
) public authorised(addr) returns (bytes32) {
bytes32 node = _getNamehash(addr);

_setName(node, name);
emit ReverseClaimed(addr, node);

_setName(addr, node, name);
return node;
}

/// @dev Checks if the provided contractAddr is a contract and is owned by the
/// provided addr.
function ownsContract(
address contractAddr,
address addr
) internal view returns (bool) {
if (contractAddr.code.length == 0) return false;
try Ownable(contractAddr).owner() returns (address owner) {
return owner == addr;
} catch {
return false;
}
}

/// @inheritdoc ERC165
function supportsInterface(
bytes4 interfaceID
)
Expand Down
12 changes: 10 additions & 2 deletions contracts/reverseRegistrar/L2ReverseResolverWithMigration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {L2ReverseResolver} from "./L2ReverseResolver.sol";
import {INameResolver} from "../resolvers/profiles/INameResolver.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

/// @notice An L2 reverse resolver tha allows migrating from a prior resolver.
contract L2ReverseResolverWithMigration is L2ReverseResolver, Ownable {
/// @notice The old reverse resolver
INameResolver immutable oldReverseResolver;

/// @notice Sets the namespace, coin type, and old reverse resolver
/// @param _L2ReverseNode The namespace to set. The converntion is '${coinType}.reverse'
/// @param _coinType The cointype converted from the chainId of the chain this contract is deployed to.
/// @param _oldReverseResolver The old reverse resolver
constructor(
bytes32 _L2ReverseNode,
uint256 _coinType,
Expand All @@ -17,12 +23,14 @@ contract L2ReverseResolverWithMigration is L2ReverseResolver, Ownable {
oldReverseResolver = _oldReverseResolver;
}

/// @notice Migrates the names from the old reverse resolver to the new one.
/// Only callable by the owner.
/// @param addresses The addresses to migrate
function batchSetName(address[] calldata addresses) external onlyOwner {
for (uint256 i = 0; i < addresses.length; i++) {
bytes32 node = _getNamehash(addresses[i]);
string memory name = oldReverseResolver.name(node);
_setName(node, name);
emit ReverseClaimed(addresses[i], node);
_setName(addresses[i], node, name);
}
}
}
Loading

0 comments on commit 2971c2b

Please sign in to comment.