Skip to content

Commit

Permalink
changed "." prefix requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenkux committed Jan 11, 2024
1 parent dc392a9 commit bcb94a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
10 changes: 5 additions & 5 deletions packages/toplevel-alias/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The top-level alias registry is a `dm3` protocol extension to provide a decentra

- **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 to start with a dot, be at least a certain length, and follow a specific pattern.
- **Format Validation:** Enforces aliases must not start with a dot, be at least a certain length, and follow a specific pattern.

## Contract Functions

Expand All @@ -21,8 +21,8 @@ The top-level alias registry is a `dm3` protocol extension to provide a decentra
- **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 start with ".".
- `_alias`: The alias to be set for the given name. Must start with a dot and follow the format rules.
- `_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

Expand All @@ -31,7 +31,7 @@ The top-level alias registry is a `dm3` protocol extension to provide a decentra

## Format Rules for Aliases

- The alias must start with a dot (e.g., `.example`).
- 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.

Expand All @@ -52,7 +52,7 @@ The contract comes with a suite of tests to verify its functionalities. To run t
## 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.
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
Expand Down
16 changes: 8 additions & 8 deletions packages/toplevel-alias/contracts/TopLevelAliasRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract TopLevelAliasRegistry is Ownable {
mapping(string => string) public aliases;

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

// Event emitted when an alias is set
event AliasSet(string indexed _toplevel, string _alias);
Expand All @@ -29,25 +29,25 @@ contract TopLevelAliasRegistry is Ownable {
/**
* @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, and ends with '.eth'.
* 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 name. Must meet the criteria.
* @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 >= 7 && bytes(_alias).length <= MAX_ALIAS_LENGTH, "Alias length is invalid"); //"." + min. 3 chars + "." + "min. 2 chars"
require (bytes(_alias)[0] == '.', "Alias must start with a dot");
require (bytes(_toplevel)[0] == '.', "Toplevel must start with a dot");
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 name.
* @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 name, false otherwise.
* @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;
Expand Down
52 changes: 25 additions & 27 deletions packages/toplevel-alias/test/TopLevelAliasRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,63 +32,61 @@ describe('TopLevelAliasRegistry', function () {
it('Should let the owner set a valid alias and retrieve it', async function () {
await topLevelAliasRegistry
.connect(owner)
.setAlias('.alice', '.alice.eth');
expect(await topLevelAliasRegistry.aliases('.alice')).to.equal(
'.alice.eth',
.setAlias('alice', 'alice.eth');
expect(await topLevelAliasRegistry.aliases('alice')).to.equal(
'alice.eth',
);
});

it('Should let the owner set a valid alias and retrieve it', async function () {
await topLevelAliasRegistry
.connect(owner)
.setAlias('.alice', '.abc.io');
expect(await topLevelAliasRegistry.aliases('.alice')).to.equal(
'.abc.io',
.setAlias('alice', 'abc.io');
expect(await topLevelAliasRegistry.aliases('alice')).to.equal(
'abc.io',
);
});

it('Should let the owner set a valid long alias and retrieve it', async function () {
await topLevelAliasRegistry
.connect(owner)
.setAlias('.test', '.abc.long_name.xyz.eth');
expect(await topLevelAliasRegistry.aliases('.test')).to.equal(
'.abc.long_name.xyz.eth',
.setAlias('test', 'abc.long_name.xyz.eth');
expect(await topLevelAliasRegistry.aliases('test')).to.equal(
'abc.long_name.xyz.eth',
);
});

it('Should prevent setting an alias that is too short or too long', async function () {
await expect(
topLevelAliasRegistry.connect(owner).setAlias('.bob', '.bb.et'),
topLevelAliasRegistry.connect(owner).setAlias('bob', 'bb.et'),
).to.be.revertedWith('Alias length is invalid');

let longAlias = '.b'.repeat(101) + '.eth';
let longAlias = 'b'.repeat(47) + '.eth';
await expect(
topLevelAliasRegistry
.connect(owner)
.setAlias('.bob', longAlias),
topLevelAliasRegistry.connect(owner).setAlias('bob', longAlias),
).to.be.revertedWith('Alias length is invalid');
});

it('Should prevent setting an empty toplevel', async function () {
await expect(
topLevelAliasRegistry.connect(owner).setAlias('', '.valid.eth'),
topLevelAliasRegistry.connect(owner).setAlias('', 'valid.eth'),
).to.be.revertedWith('Toplevel cannot be empty');
});

it('Should revert if the alias does not start with a dot', async function () {
it('Should revert if the alias starts with a dot', async function () {
await expect(
topLevelAliasRegistry
.connect(owner)
.setAlias('.alice', 'alice.eth'),
).to.be.revertedWith('Alias must start with a dot');
.setAlias('alice', '.alice.eth'),
).to.be.revertedWith('Alias must not start with a dot');
});

it('Should revert if the toplevel does not start with a dot', async function () {
it('Should revert if the toplevel starts with a dot', async function () {
await expect(
topLevelAliasRegistry
.connect(owner)
.setAlias('alice', '.alice.eth'),
).to.be.revertedWith('Toplevel must start with a dot');
.setAlias('.alice', 'alice.eth'),
).to.be.revertedWith('Toplevel must not start with a dot');
});
});

Expand All @@ -97,17 +95,17 @@ describe('TopLevelAliasRegistry', function () {
await expect(
topLevelAliasRegistry
.connect(owner)
.setAlias('.alice', '.alice.eth'),
.setAlias('alice', 'alice.eth'),
)
.to.emit(topLevelAliasRegistry, 'AliasSet')
.withArgs('.alice', '.alice.eth');
.withArgs('alice', 'alice.eth');
});
});

describe('existsAlias Function', function () {
it('Should return true for an existing alias', async function () {
const toplevel = '.alice';
const alias = '.alice.eth';
const toplevel = 'alice';
const alias = 'alice.eth';
await topLevelAliasRegistry
.connect(owner)
.setAlias(toplevel, alias);
Expand All @@ -116,7 +114,7 @@ describe('TopLevelAliasRegistry', function () {
});

it('Should return false for a non-existing alias', async function () {
const nonExistentToplevel = '.bob';
const nonExistentToplevel = 'bob';
expect(await topLevelAliasRegistry.existsAlias(nonExistentToplevel))
.to.be.false;
});
Expand Down Expand Up @@ -162,7 +160,7 @@ describe('TopLevelAliasRegistry', function () {
await expect(
topLevelAliasRegistry
.connect(addr1)
.setAlias('.testname', '.testname.eth'),
.setAlias('testname', 'testname.eth'),
).to.be.revertedWithCustomError(
topLevelAliasRegistry,
'OwnableUnauthorizedAccount',
Expand Down

0 comments on commit bcb94a8

Please sign in to comment.