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 dm3 name registrar #35

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: CI
on: [push]
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
name: Test on Node ${{ matrix.node }} and ${{ matrix.os }}
Expand Down Expand Up @@ -50,4 +54,4 @@ jobs:
run: bun run build

- name: Lint
run: bun run lint
run: bun run lint
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"solidity.compileUsingRemoteVersion": "v0.8.17+commit.8df45f5f"
}
12 changes: 12 additions & 0 deletions dm3-names/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts
deployments

21 changes: 21 additions & 0 deletions dm3-names/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 ENS Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions dm3-names/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# @ensdomains/crosschain-resolver

A resolver contract that is built on top of evm-verifier.

For a detailed readme and usage instructions, see the [monorepo readme](https://github.com/ensdomains/evmgateway/tree/main).


## How it is defined

When the resolver has the following storage layout,

```
contract Resolver {
// node => version
mapping(bytes32 => uint64) public recordVersions;
// versionable_addresses[recordVersions[node]][node][coinType]
// version => node => cointype
mapping(uint64 => mapping(bytes32 => mapping(uint256 => bytes))) versionable_addresses;
```

Run `yarn storage` to find out storage slot for each variable

```
// Storage slot
// ┌────────────────────────────┬──────────────────────────────┬──────────────┬
// │ contract │ state_variable │ storage_slot │
// ├────────────────────────────┼──────────────────────────────┼──────────────┼
// │ DelegatableResolver │ recordVersions │ 0 │
// │ DelegatableResolver │ versionable_abis │ 1 │
// │ DelegatableResolver │ versionable_addresses │ 2 │
```

Then define the l1 function

```
function addr(
bytes32 node,
uint256 coinType
) public view returns (bytes memory) {
EVMFetcher.newFetchRequest(verifier, target)
.getStatic(0) // storage_slot of recordVersions
.element(node)
.getDynamic(2) // storage_slot of versionable_addresses
.ref(0) // Referencing the result of `.getStatic(0)`
.element(node)
.element(coinType)
.fetch(this.addrCoinTypeCallback.selector, ''); // recordVersions
```

Storage verificaton can only verify the data of l2. When the function result needs some transformation, transform inside the callback function as follows.

```
function addrCallback(
bytes[] memory values,
bytes memory
) public pure returns (address) {
return bytesToAddress(values[1]);
}
```



## Deploying (Goerli)

Create `.env` and set the following variables


- DEPLOYER_PRIVATE_KEY
- L1_PROVIDER_URL
- L2_PROVIDER_URL
- L1_ETHERSCAN_API_KEY
- L2_ETHERSCAN_API_KEY
- OP_VERIFIER_ADDRESS=0x0c2746F20C9c97DBf718de10c04943cf408230A3

```
bun run hardhat deploy --network optimismGoerli
```

Followed by the L1 contract:

```
bun run hardhat deploy --network goerli
```


## Deployments
149 changes: 149 additions & 0 deletions dm3-names/contracts/Dm3NameRegistrar.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import {IAddrResolver} from '@ensdomains/ens-contracts/contracts/resolvers/profiles/IAddrResolver.sol';
import {INameResolver} from '@ensdomains/ens-contracts/contracts/resolvers/profiles/INameResolver.sol';
import {ITextResolver} from '@ensdomains/ens-contracts/contracts/resolvers/profiles/ITextResolver.sol';

/// @title Dm3NameRegistrar
/// @notice This contract is used for registering names in the ENS system. It is a combination of ENSResolver and ReverseRegistrar contracts. Allowing to register names and set text records for each name. By beeing compatible with ENSResolver and ReverseRegistrar

contract Dm3NameRegistrar is IAddrResolver, INameResolver, ITextResolver {
//Lookup table for hexadecimal conversion
//Taken from ENS ReverseRegistrar contract
//https://github.com/ensdomains/ens-contracts/blob/21736916300b26cb8ea1802dbf6c9ff054adaeab/contracts/reverseRegistrar/ReverseRegistrar.sol#L12
bytes32 private constant lookup =
0x3031323334353637383961626364656600000000000000000000000000000000;

// Constant for reverse node address
//Taken from ENS ReverseRegistrar contract
//https://github.com/ensdomains/ens-contracts/blob/21736916300b26cb8ea1802dbf6c9ff054adaeab/contracts/reverseRegistrar/ReverseRegistrar.sol#L12
bytes32 private constant ADDR_REVERSE_NODE =
0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2;

//Node of the L1 domain. in case of OP name that would be namehash(op.dm3.eth)
//Is not immutable so it can be retrieved from the storage using CCIP
bytes32 public PARENT_NODE;

// Mapping to store the owner of each node
mapping(bytes32 => address) public owner;

// Mapping to store the reverse record of each node
mapping(bytes32 => string) public reverse;

// Mapping to store text records for each node
mapping(bytes32 => mapping(string => string)) public texts;

// Event emitted when a name is registered
event NameRegistered(address indexed addr, string indexed name);

// Event emitted when a name is removed
event NameRemoved(address indexed addr, string indexed name);

/// @notice Constructor to set the parent node
/// @param _parentNode The parent node of the ENS
constructor(bytes32 _parentNode) {
PARENT_NODE = _parentNode;
}

/// @notice Register a name in the ENS system
/// @param _name The name to register
function register(string calldata _name) external {
string memory oldName = reverse[makeReverseNode(msg.sender)];
if (bytes(_name).length == 0) {
// Clear name if the new name is empty
delete owner[makeLabelNode(oldName)];
delete reverse[makeReverseNode(msg.sender)];
emit NameRemoved(msg.sender, oldName);
return;
}

if (bytes(oldName).length > 0) {
// Clear old name if it exists
delete owner[makeLabelNode(oldName)];
emit NameRemoved(msg.sender, oldName);
}
//set owner record
owner[makeLabelNode(_name)] = msg.sender;
//set reverse record
reverse[makeReverseNode(msg.sender)] = _name;
//emit NameRegistered event
emit NameRegistered(msg.sender, _name);
}
/// @notice Set text for a label
/// @param label The label to set the text for
/// @param key The key for the text
/// @param value The text to set
function setText(
bytes32 label,
string calldata key,
string calldata value
) external {
address owner = owner[label];
require(owner != address(0), 'Name not registered');
require(owner == msg.sender, 'Only owner');
texts[label][key] = value;
// emit TextChanged(node, key, key, value);
}
/// @notice Get the address of a node
/// @param node The node to get the address for
/// @return The address of the node
function addr(bytes32 node) external view returns (address payable) {
return payable(owner[node]);
}

/// @notice Get the name of a node
/// @param node The node to get the name for
/// @return The name of the node
function name(bytes32 node) external view returns (string memory) {
return reverse[node];
}

/// @notice Get the text of a node
/// @param node The node to get the text for
/// @param key The key for the text
/// @return The text of the node
function text(
bytes32 node,
string calldata key
) external view override returns (string memory) {
return texts[node][key];
}
/// @notice Make a label node using the PARENT_NODE
/// @param label The label to make a node for
/// @return The node of the label
function makeLabelNode(string memory label) private view returns (bytes32) {
return
keccak256(abi.encodePacked(PARENT_NODE, keccak256(bytes(label))));
}
/// @notice Make a label node used for the reverse record using the ADDR_REVERSE_NODE
function makeReverseNode(address addr) private pure returns (bytes32) {
return
keccak256(
abi.encodePacked(ADDR_REVERSE_NODE, sha3HexAddress(addr))
);
}

/// @notice Convert an address to a hexadecimal string and hash it
/// @param addr The address to convert and hash
/// @dev taken from ENS ReverseRegistrar contract
/// @dev https://github.com/ensdomains/ens-contracts/blob/21736916300b26cb8ea1802dbf6c9ff054adaeab/contracts/reverseRegistrar/ReverseRegistrar.sol#L164
function sha3HexAddress(address addr) private 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)
}
}
}
26 changes: 26 additions & 0 deletions dm3-names/contracts/deps.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/NameWrapper.sol";
import "@ensdomains/ens-contracts/contracts/ethregistrar/BaseRegistrarImplementation.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/StaticMetadataService.sol";
import "@ensdomains/l1-verifier/contracts/L1Verifier.sol";
import {ReverseRegistrar} from "@ensdomains/ens-contracts/contracts/reverseRegistrar/ReverseRegistrar.sol";
import {PublicResolver} from "@ensdomains/ens-contracts/contracts/resolvers/PublicResolver.sol";
import {DelegatableResolver} from "@ensdomains/ens-contracts/contracts/resolvers/DelegatableResolver.sol";
import {DelegatableResolverFactory} from "@ensdomains/ens-contracts/contracts/resolvers/DelegatableResolverFactory.sol";
// Storage slot
// ┌────────────────────────────┬──────────────────────────────┬──────────────┬
// │ contract │ state_variable │ storage_slot │
// ├────────────────────────────┼──────────────────────────────┼──────────────┼
// │ DelegatableResolver │ recordVersions │ 0 │
// │ DelegatableResolver │ versionable_abis │ 1 │
// │ DelegatableResolver │ versionable_addresses │ 2 │
// │ DelegatableResolver │ versionable_hashes │ 3 │
// │ DelegatableResolver │ versionable_zonehashes │ 4 │
// │ DelegatableResolver │ versionable_records │ 5 │
// │ DelegatableResolver │ versionable_nameEntriesCount │ 6 │
// │ DelegatableResolver │ versionable_interfaces │ 7 │
// │ DelegatableResolver │ versionable_names │ 8 │
// │ DelegatableResolver │ versionable_pubkeys │ 9 │
// │ DelegatableResolver │ versionable_texts │ 10 │
// │ DelegatableResolver │ operators │ 11 │
// │ DelegatableResolverFactory │ implementation │ 0 │
Loading
Loading