Skip to content

Commit

Permalink
Merge pull request #645 from etherisc/feature/add-missing-instance-fu…
Browse files Browse the repository at this point in the history
…nctions-585

Feature/add missing instance functions #585
  • Loading branch information
matthiaszimmermann authored Sep 3, 2024
2 parents bc7a257 + a28dfa0 commit 5b936e6
Show file tree
Hide file tree
Showing 81 changed files with 4,185 additions and 2,774 deletions.
446 changes: 261 additions & 185 deletions contracts/authorization/AccessAdmin.sol

Large diffs are not rendered by default.

227 changes: 220 additions & 7 deletions contracts/authorization/AccessAdminLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,57 @@ import {AccessManagedUpgradeable} from "@openzeppelin/contracts-upgradeable/acce
import {IAccess} from "./IAccess.sol";
import {IAccessAdmin} from "./IAccessAdmin.sol";
import {IAuthorization} from "./IAuthorization.sol";
import {IComponent} from "../shared/IComponent.sol";
import {IRegistry} from "../registry/IRegistry.sol";
import {IService} from "../shared/IService.sol";
import {IServiceAuthorization} from "./IServiceAuthorization.sol";

import {ContractLib} from "../shared/ContractLib.sol";
import {ObjectType} from "../type/ObjectType.sol";
import {RoleId} from "../type/RoleId.sol";
import {RoleId, RoleIdLib} from "../type/RoleId.sol";
import {SelectorLib} from "../type/Selector.sol";
import {Str, StrLib} from "../type/String.sol";
import {TimestampLib} from "../type/Timestamp.sol";
import {VersionPart} from "../type/Version.sol";
import {VersionPart, VersionPartLib} from "../type/Version.sol";


library AccessAdminLib { // ACCESS_ADMIN_LIB

string public constant TOKEN_HANDLER_SUFFIX = "Th";
string public constant ROLE_SUFFIX = "_Role";

uint64 public constant SERVICE_DOMAIN_ROLE_FACTOR = 100;
uint64 public constant COMPONENT_ROLE_FACTOR = 1000;
uint64 public constant COMPONENT_ROLE_MAX = 19000;

uint64 public constant CORE_ROLE_MIN = 100;
uint64 public constant SERVICE_ROLE_MIN = 1000; // + service domain * SERVICE_ROLE_FACTOR + release
uint64 public constant SERVICE_ROLE_FACTOR = 1000;
uint64 public constant INSTANCE_ROLE_MIN = 100000;

// MUST match with Authorization.COMPONENT_ROLE_MIN
uint64 public constant COMPONENT_ROLE_MIN = 110000;

uint64 public constant CUSTOM_ROLE_MIN = 1000000;


function getSelectors(
IAccess.FunctionInfo[] memory functions
)
public
pure
returns (
bytes4[] memory selectors
)
{
uint256 n = functions.length;
selectors = new bytes4[](n);
for (uint256 i = 0; i < n; i++) {
selectors[i] = functions[i].selector.toBytes4();
}
}


function checkRoleCreation(
IAccessAdmin accessAdmin,
RoleId roleId,
Expand Down Expand Up @@ -54,6 +92,7 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
}
}


function checkTargetCreation(
IAccessAdmin accessAdmin,
address target,
Expand Down Expand Up @@ -97,7 +136,6 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
revert IAccessAdmin.ErrorAccessAdminTargetAuthorityMismatch(accessAdmin.authority(), targetAuthority);
}
}

}


Expand All @@ -106,6 +144,7 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
address authorization,
ObjectType expectedDomain,
VersionPart expectedRelease,
bool expectServiceAuthorization,
bool checkAlreadyInitialized
)
public
Expand All @@ -118,8 +157,14 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
}

// check contract type matches
if (!ContractLib.supportsInterface(authorization, type(IAuthorization).interfaceId)) {
revert IAccessAdmin.ErrorAccessAdminNotAuthorization(authorization);
if (expectServiceAuthorization) {
if (!ContractLib.supportsInterface(authorization, type(IServiceAuthorization).interfaceId)) {
revert IAccessAdmin.ErrorAccessAdminNotServiceAuthorization(authorization);
}
} else {
if (!ContractLib.supportsInterface(authorization, type(IAuthorization).interfaceId)) {
revert IAccessAdmin.ErrorAccessAdminNotAuthorization(authorization);
}
}

// check domain matches
Expand All @@ -144,6 +189,8 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
public
view
{
checkRegistry(registry);

ObjectType tagetType = IRegistry(registry).getObjectInfo(target).objectType;
if (tagetType.eqz()) {
revert IAccessAdmin.ErrorAccessAdminNotRegistered(target);
Expand All @@ -154,7 +201,161 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
}
}

function toRole(RoleId adminRoleId, IAccessAdmin.RoleType roleType, uint32 maxMemberCount, string memory name)

function checkRegistry(
address registry
)
public
view
{
if (!ContractLib.isRegistry(registry)) {
revert IAccessAdmin.ErrorAccessAdminNotRegistry(registry);
}
}


function getServiceRoleId(
address serviceAddress,
IAccess.TargetType serviceTargetType
)
public
view
returns (RoleId serviceRoleId)
{
IService service = IService(serviceAddress);

if (serviceTargetType == IAccess.TargetType.Service) {
return RoleIdLib.toServiceRoleId(service.getDomain(), service.getRelease());
} else if (serviceTargetType == IAccess.TargetType.GenericService) {
return RoleIdLib.toGenericServiceRoleId(service.getDomain());
}

revert IAccessAdmin.ErrorAccessAdminInvalidServiceType(serviceAddress, serviceTargetType);
}


function getVersionedServiceRoleId(
ObjectType serviceDomain,
VersionPart release
)
public
pure
returns (RoleId serviceRoleId)
{
return RoleIdLib.toRoleId(
SERVICE_ROLE_MIN + SERVICE_ROLE_FACTOR * serviceDomain.toInt() + release.toInt());
}


function getGenericServiceRoleId(
ObjectType serviceDomain
)
public
pure
returns (RoleId serviceRoleId)
{
return RoleIdLib.toRoleId(
SERVICE_ROLE_MIN + SERVICE_ROLE_FACTOR * serviceDomain.toInt() + VersionPartLib.releaseMax().toInt());
}


function getCustomRoleId(uint64 index)
public
pure
returns (RoleId customRoleId)
{
return RoleIdLib.toRoleId(CUSTOM_ROLE_MIN + index);
}


function isCustomRole(RoleId roleId)
public
pure
returns (bool)
{
return roleId.toInt() >= CUSTOM_ROLE_MIN;
}


function getTargetRoleId(
address target,
IAccess.TargetType targetType,
uint64 index
)
public
view
returns (RoleId targetRoleId)
{
if (targetType == IAccess.TargetType.Core) {
return RoleIdLib.toRoleId(CORE_ROLE_MIN + index);
}

if (targetType == IAccess.TargetType.Service || targetType == IAccess.TargetType.GenericService ) {
return getServiceRoleId(target, targetType);
}

if (targetType == IAccess.TargetType.Instance) {
return RoleIdLib.toRoleId(INSTANCE_ROLE_MIN + index);
}

if (targetType == IAccess.TargetType.Component) {
return RoleIdLib.toRoleId(COMPONENT_ROLE_MIN + index);
}

if (targetType == IAccess.TargetType.Custom) {
return RoleIdLib.toRoleId(CUSTOM_ROLE_MIN + index);
}

revert IAccessAdmin.ErrorAccessAdminInvalidTargetType(target, targetType);
}


function getTokenHandler(
address target,
string memory targetName,
IAccess.TargetType targetType
)
public
view
returns (
address tokenHandler,
string memory tokenHandlerName
)
{
// not component or core (we need to check core because of staking)
if (targetType != IAccess.TargetType.Component && targetType != IAccess.TargetType.Core) {
return (address(0), "");
}

// not contract
if (!ContractLib.isContract(target)) {
return (address(0), "");
}

// not component
if (!ContractLib.supportsInterface(target, type(IComponent).interfaceId)) {
return (address(0), "");
}

tokenHandler = address(IComponent(target).getTokenHandler());
tokenHandlerName = string(abi.encodePacked(targetName, TOKEN_HANDLER_SUFFIX));
}


function toRoleName(string memory name) public pure returns (string memory) {
return string(
abi.encodePacked(
name,
ROLE_SUFFIX));
}


function toRole(
RoleId adminRoleId,
IAccessAdmin.RoleType roleType,
uint32 maxMemberCount,
string memory name
)
public
view
returns (IAccess.RoleInfo memory)
Expand All @@ -169,11 +370,23 @@ library AccessAdminLib { // ACCESS_ADMIN_LIB
});
}

function toFunction(bytes4 selector, string memory name)

function toFunction(
bytes4 selector,
string memory name
)
public
view
returns (IAccess.FunctionInfo memory)
{
if(selector == bytes4(0)) {
revert IAccessAdmin.ErrorAccessAdminSelectorZero();
}

if(bytes(name).length == 0) {
revert IAccessAdmin.ErrorAccessAdminFunctionNameEmpty();
}

return IAccess.FunctionInfo({
name: StrLib.toStr(name),
selector: SelectorLib.toSelector(selector),
Expand Down
Loading

0 comments on commit 5b936e6

Please sign in to comment.