diff --git a/contracts/modules/AddressAliasRegistry.sol b/contracts/modules/AddressAliasRegistry.sol index c395726d..5c504ac2 100644 --- a/contracts/modules/AddressAliasRegistry.sol +++ b/contracts/modules/AddressAliasRegistry.sol @@ -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]); } } } @@ -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_]; } } diff --git a/contracts/modules/interfaces/IAddressAliasRegistry.sol b/contracts/modules/interfaces/IAddressAliasRegistry.sol index 83c839ba..02b46486 100644 --- a/contracts/modules/interfaces/IAddressAliasRegistry.sol +++ b/contracts/modules/interfaces/IAddressAliasRegistry.sol @@ -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); @@ -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); } diff --git a/tests/modules/AddressAliasRegistry.t.sol b/tests/modules/AddressAliasRegistry.t.sol index 857adf7f..09ed394f 100644 --- a/tests/modules/AddressAliasRegistry.t.sol +++ b/tests/modules/AddressAliasRegistry.t.sol @@ -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);