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

Work toplevel alias 3 #745

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions packages/toplevel-alias/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

59 changes: 59 additions & 0 deletions packages/toplevel-alias/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# TopLevelAliasRegistry

## Overview

The top-level alias registry is a `dm3` protocol extension to provide a decentralized register of names/domains that are mapped (via CCIP) to an ENS subdomain. The registry provides a mapping of top-level domains to their aliases in ENS.

## Project Overview

`TopLevelAliasRegistry` is a Solidity smart contract designed for managing a registry of aliases mapped to names. The contract is built to ensure that aliases follow a specific format and are managed by an owner, providing a secure and efficient way to handle alias registration.

### Key Features

- **Alias Management:** Allows setting of aliases for given names with specific format rules.
- **Ownership Control:** Utilizes OpenZeppelin's `Ownable` contract to restrict certain functionalities to the contract owner.
- **Format Validation:** Enforces aliases must not start with a dot, be at least a certain length, and follow a specific pattern.

## Contract Functions

### setAlias

- **Description:** Sets an alias for a given name (= top-level).
- **Access:** Restricted to the contract owner.
- **Parameters:**
- `_name`: The name (top-level domain) to map the alias to. Must not be empty and not start with ".".
- `_alias`: The alias to be set for the given name. Must not start with a dot and follow the format rules.

### Other Functions

- **owner**: Returns the owner of the contract.
- **transferOwnership**: Allows the current owner to transfer control of the contract to a new owner.

## Format Rules for Aliases

- The alias must not start with a dot (e.g., `example` and not `.example`).
- It must have at least three characters before the dot and at least two characters after the dot.
- The total length of the alias must be within the predefined maximum limit.

## Testing the Contract

The contract comes with a suite of tests to verify its functionalities. To run these tests:

1. **Start Hardhat Test Environment:**

```sh
npx hardhat test
```

This command will execute the tests defined in the `test` directory of the project.

2. **Review Test Output:**
After running the tests, you'll see the output in the terminal, indicating whether each test has passed or failed along with any error messages.

## Contributing

Contributions to the `TopLevelAliasRegistry` sub-project of the `dm3` project are welcome. Please ensure that any major changes are discussed via issues before submitting a pull request.

## License

This project is licensed under the BSD License - see the LICENSE file of the `dm3` main project for details.
57 changes: 57 additions & 0 deletions packages/toplevel-alias/contracts/TopLevelAliasRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: BDS-2
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

/**
* @title TopLevelAliasRegistry
* @dev A contract for managing a registry of aliases mapped to _toplevels.
* This contract allows the owner to set aliases for given _toplevels,
* have at least three characters before '.' and 2 after, do not exceed a maximum length, and are not empty.
*/
contract TopLevelAliasRegistry is Ownable {
// Public mapping of _toplevels to aliases
mapping(string => string) public aliases;

// Maximum allowed length for an alias
uint256 private constant MAX_ALIAS_LENGTH = 50;

// Event emitted when an alias is set
event AliasSet(string indexed _toplevel, string _alias);

/**
* @dev Constructor that sets the specified address as the owner of the contract.
* @param initialOwner The address to be set as the initial owner of the contract.
*/
constructor(address initialOwner) Ownable(initialOwner) {
}

/**
* @dev Sets an alias for a given name, ensuring it meets various criteria including ENS validity.
* Only the owner of the contract can call this function.
* Validates that the name is not empty, the alias length is within limits
*
* @param _toplevel The toplevel name to map the alias to. Must not be empty.
* @param _alias The alias to be set for the given toplevel. Must meet the criteria.
*/
function setAlias(string memory _toplevel, string memory _alias) public onlyOwner {
require(bytes(_toplevel).length > 0, "Toplevel cannot be empty");
require(bytes(_alias).length >= 6 && bytes(_alias).length <= MAX_ALIAS_LENGTH, "Alias length is invalid"); // min. 3 chars + "." + "min. 2 chars"
require (bytes(_alias)[0] != '.', "Alias must not start with a dot");
require (bytes(_toplevel)[0] != '.', "Toplevel must not start with a dot");

aliases[_toplevel] = _alias;
emit AliasSet(_toplevel, _alias);
}

/**
* @dev Checks if an alias exists for the given toplevel.
* @param _toplevel The toplevel to check for an alias.
* @return bool True if an alias exists for the toplevel, false otherwise.
*/
function existsAlias(string memory _toplevel) public view returns (bool) {
return bytes(aliases[_toplevel]).length > 0;
}
}


13 changes: 13 additions & 0 deletions packages/toplevel-alias/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HardhatUserConfig } from 'hardhat/config';
import '@nomicfoundation/hardhat-toolbox';

const config: HardhatUserConfig = {
solidity: '0.8.20',
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: 'USD',
gasPrice: 20,
},
};

export default config;
Loading
Loading