Skip to content

Commit

Permalink
Add addressOf and aliasOf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Jan 31, 2024
1 parent e9ed65d commit d46b528
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 29 deletions.
67 changes: 43 additions & 24 deletions contracts/modules/AddressAliasRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ contract AddressAliasRegistry is IAddressAliasRegistry {
/**
* @inheritdoc IAddressAliasRegistry
*/
function resolveAndRegister(address[] memory a)
function resolveAndRegister(address[] memory addressesOrAliases)
public
returns (address[] memory addresses, address[] memory aliases)
{
unchecked {
uint256 n = a.length;
addresses = a;
uint256 n = addressesOrAliases.length;
addresses = addressesOrAliases;
aliases = new address[](n);
for (uint256 i; i != n; ++i) {
(addresses[i], aliases[i]) = _resolveAndRegister(a[i]);
(addresses[i], aliases[i]) = _resolveAndRegister(addressesOrAliases[i]);
}
}
}
Expand Down Expand Up @@ -73,56 +73,75 @@ contract AddressAliasRegistry is IAddressAliasRegistry {
/**
* @inheritdoc IAddressAliasRegistry
*/
function resolve(address[] memory a) public view returns (address[] memory addresses, address[] memory aliases) {
function resolve(address[] memory addressesOrAliases)
public
view
returns (address[] memory addresses, address[] memory aliases)
{
unchecked {
uint256 n = a.length;
addresses = a;
uint256 n = addressesOrAliases.length;
addresses = addressesOrAliases;
aliases = new address[](n);
for (uint256 i; i != n; ++i) {
(addresses[i], aliases[i]) = _resolve(a[i]);
(addresses[i], aliases[i]) = _resolve(addressesOrAliases[i]);
}
}
}

/**
* @inheritdoc IAddressAliasRegistry
*/
function addressOf(address addressOrAlias) public view returns (address) {
// If the `aliasOrAddress` is less than or equal to `2**32 - 1`, we will consider it an alias.
return uint160(addressOrAlias) <= type(uint32).max ? _aliasToAddress[addressOrAlias] : addressOrAlias;
}

/**
* @inheritdoc IAddressAliasRegistry
*/
function aliasOf(address addressOrAlias) public view returns (address) {
return _addressToAlias[addressOf(addressOrAlias)];
}

// =============================================================
// INTERNAL / PRIVATE HELPERS
// =============================================================

/**
* @dev Returns the alias and address for `aliasOrAddress`.
* If the `aliasOrAddress` is less than `2**31 - 1`, it is treated as an alias.
* @dev Returns the alias and address for `addressOrAlias`.
* If the `addressOrAlias` is less than `2**31 - 1`, it is treated as an alias.
* Otherwise, it is treated as an address, and it's alias will be registered on-the-fly.
* @param aliasOrAddress The alias or address.
* @param addressOrAlias The alias or address.
* @return address_ The address.
* @return alias_ The alias.
*/
function _resolveAndRegister(address aliasOrAddress) internal returns (address address_, address alias_) {
// If the `aliasOrAddress` is less than or equal to `2**32 - 1`, we will consider it an alias.
if (uint160(aliasOrAddress) <= type(uint32).max) {
alias_ = aliasOrAddress;
function _resolveAndRegister(address addressOrAlias) internal returns (address address_, address alias_) {
// If the `addressOrAlias` is less than or equal to `2**32 - 1`, we will consider it an alias.
if (uint160(addressOrAlias) <= type(uint32).max) {
alias_ = addressOrAlias;
address_ = _aliasToAddress[alias_];
if (address_ == address(0)) revert AliasNotFound();
} else {
address_ = aliasOrAddress;
address_ = addressOrAlias;
alias_ = _registerAlias(address_);
}
}

/**
* @dev Returns the alias and address for `aliasOrAddress`.
* If the `aliasOrAddress` is less than `2**31 - 1`, it is treated as an alias.
* @dev Returns the alias and address for `addressOrAlias`.
* If the `addressOrAlias` is less than `2**31 - 1`, it is treated as an alias.
* Otherwise, it is treated as an address.
* @param aliasOrAddress The alias or address.
* @param addressOrAlias The alias or address.
* @return address_ The address.
* @return alias_ The alias.
*/
function _resolve(address aliasOrAddress) internal view returns (address address_, address alias_) {
// If the `aliasOrAddress` is less than or equal to `2**32 - 1`, we will consider it an alias.
if (uint160(aliasOrAddress) <= type(uint32).max) {
alias_ = aliasOrAddress;
function _resolve(address addressOrAlias) internal view returns (address address_, address alias_) {
// If the `addressOrAlias` is less than or equal to `2**32 - 1`, we will consider it an alias.
if (uint160(addressOrAlias) <= type(uint32).max) {
alias_ = addressOrAlias;
address_ = _aliasToAddress[alias_];
} else {
address_ = aliasOrAddress;
address_ = addressOrAlias;
alias_ = _addressToAlias[address_];
}
}
Expand Down
27 changes: 22 additions & 5 deletions contracts/modules/interfaces/IAddressAliasRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ interface IAddressAliasRegistry {
/**
* @dev Resolve the addresses or aliases.
* If an address does not have an aliases, an alias will be registered for it.
* @param a An array of addresses, which can be aliases.
* @param addressesOrAliases An array of addresses, which can be aliases.
* @return addresses The resolved addresses.
* @return aliases The aliases for the addresses.
*/
function resolveAndRegister(address[] memory a)
function resolveAndRegister(address[] memory addressesOrAliases)
external
returns (address[] memory addresses, address[] memory aliases);

Expand All @@ -56,11 +56,28 @@ interface IAddressAliasRegistry {

/**
* @dev Resolve the addresses or aliases.
* If an address does not have an aliases, it's corresponding returned alias will be zero.
* If an address does not have an alias, it's corresponding returned alias will be zero.
* If an alias does not have an address, it's corresponding returned address will be zero.
* @param a An array of addresses, which can be aliases.
* @param addressesOrAliases An array of addresses, which can be aliases.
* @return addresses The resolved addresses.
* @return aliases The aliases for the addresses.
*/
function resolve(address[] memory a) external view returns (address[] memory addresses, address[] memory aliases);
function resolve(address[] memory addressesOrAliases)
external
view
returns (address[] memory addresses, address[] memory aliases);

/**
* @dev Resolve the address or alias.
* @param addressesOrAliases An address or alias.
* @return The resolved address.
*/
function addressOf(address addressesOrAliases) external view returns (address);

/**
* @dev Resolve the address or alias.
* @param addressesOrAliases An address or alias.
* @return The resolved alias.
*/
function aliasOf(address addressesOrAliases) external view returns (address);
}
7 changes: 7 additions & 0 deletions tests/modules/AddressAliasRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ contract AddressAliasRegistryTests is TestConfigV2_1 {
(r0.addresses, r0.aliases) = aar.resolve(new address[](addresses.length));
assertEq(r0.aliases, new address[](addresses.length));
assertEq(r0.addresses, new address[](addresses.length));
address a = addresses.length > 0 ? addresses[0] : address(0);
assertEq(aar.addressOf(a), a);
assertEq(uint160(aar.aliasOf(a)), 0);
}
(r0.addresses, r0.aliases) = aar.resolveAndRegister(addresses);
(r1.addresses, r1.aliases) = aar.resolve(addresses);
if (addresses.length != 0) {
assertEq(uint160(r0.aliases[0]), 1);
assertEq(uint160(r1.aliases[0]), 1);
address a = addresses[0];
assertEq(aar.addressOf(a), a);
assertEq(aar.addressOf(aar.aliasOf(a)), a);
assertEq(uint160(aar.aliasOf(a)), 1);
}
assertEq(r1.aliases, r0.aliases);
assertEq(r1.addresses, r0.addresses);
Expand Down

0 comments on commit d46b528

Please sign in to comment.