Skip to content

Commit

Permalink
Merge pull request #5 from OriumNetwork/multiple-roles
Browse files Browse the repository at this point in the history
proposal more generic
  • Loading branch information
karacurt authored Jul 14, 2023
2 parents c200cc2 + 1524e7c commit 9c7df89
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
42 changes: 21 additions & 21 deletions contracts/RolesRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ struct RoleData {

contract RolesRegistry is IRolesRegistry {

// owner => user => nftAddress => tokenId => role => struct(expirationDate, data)
// owner => user => tokenAddress => tokenId => role => struct(expirationDate, data)
mapping(address => mapping(address => mapping(address => mapping(uint256 => mapping(bytes32 => RoleData))))) public roleAssignments;

// owner => nftAddress => tokenId => role => user
// owner => tokenAddress => tokenId => role => user
mapping(address => mapping(address => mapping(uint256 => mapping(bytes32 => address)))) public lastRoleAssignment;

modifier validExpirationDate(uint64 _expirationDate) {
Expand All @@ -24,53 +24,53 @@ contract RolesRegistry is IRolesRegistry {

function grantRole(
bytes32 _role,
address _account,
address _nftAddress,
address _grantee,
address _tokenAddress,
uint256 _tokenId,
uint64 _expirationDate,
bytes calldata _data
) external validExpirationDate(_expirationDate) {
roleAssignments[msg.sender][_account][_nftAddress][_tokenId][_role] = RoleData(_expirationDate, _data);
lastRoleAssignment[msg.sender][_nftAddress][_tokenId][_role] = _account;
emit RoleGranted(_role, _account, _expirationDate, _nftAddress, _tokenId, _data);
roleAssignments[msg.sender][_grantee][_tokenAddress][_tokenId][_role] = RoleData(_expirationDate, _data);
lastRoleAssignment[msg.sender][_tokenAddress][_tokenId][_role] = _grantee;
emit RoleGranted(_role, _grantee, _expirationDate, _tokenAddress, _tokenId, _data);
}

function revokeRole(
bytes32 _role,
address _account,
address _nftAddress,
address _grantee,
address _tokenAddress,
uint256 _tokenId
) external {
delete roleAssignments[msg.sender][_account][_nftAddress][_tokenId][_role];
delete lastRoleAssignment[msg.sender][_nftAddress][_tokenId][_role];
emit RoleRevoked(_role, _account, _nftAddress, _tokenId);
delete roleAssignments[msg.sender][_grantee][_tokenAddress][_tokenId][_role];
delete lastRoleAssignment[msg.sender][_tokenAddress][_tokenId][_role];
emit RoleRevoked(_role, _grantee, _tokenAddress, _tokenId);
}

function hasRole(
bytes32 _role,
address _owner,
address _account,
address _nftAddress,
address _granter,
address _grantee,
address _tokenAddress,
uint256 _tokenId,
bool _supportsMultipleAssignments
) external view returns (bool) {
bool isValid = roleAssignments[_owner][_account][_nftAddress][_tokenId][_role].expirationDate > block.timestamp;
bool isValid = roleAssignments[_granter][_grantee][_tokenAddress][_tokenId][_role].expirationDate > block.timestamp;

if(_supportsMultipleAssignments){
return isValid;
} else {
return isValid && lastRoleAssignment[_owner][_nftAddress][_tokenId][_role] == _account;
return isValid && lastRoleAssignment[_granter][_tokenAddress][_tokenId][_role] == _grantee;
}
}

function roleData(
bytes32 _role,
address _owner,
address _account,
address _nftAddress,
address _granter,
address _grantee,
address _tokenAddress,
uint256 _tokenId
) external view returns (uint64 expirationDate_, bytes memory data_) {
RoleData memory _roleData = roleAssignments[_owner][_account][_nftAddress][_tokenId][_role];
RoleData memory _roleData = roleAssignments[_granter][_grantee][_tokenAddress][_tokenId][_role];
return (_roleData.expirationDate, _roleData.data);
}
}
78 changes: 39 additions & 39 deletions contracts/interfaces/orium/IRolesRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,92 @@

pragma solidity 0.8.9;

/// @notice The ERC-721 Roles Registry interface enables granting and revoking temporary roles for ERC-721 tokens.
/// @notice The Roles Registry interface enables granting and revoking temporary roles for tokens.
interface IRolesRegistry {

/// @notice Emitted when a role is assigned to a user.
/// @param _role The role identifier.
/// @param _account The user that receives the role assignment.
/// @param _grantee The user that receives the role assignment.
/// @param _expirationDate The expiration date of the role assignment.
/// @param _nftAddress The ERC721 token address.
/// @param _tokenId The ERC721 token identifier.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _data Any additional data about the role assignment.
event RoleGranted(
bytes32 indexed _role,
address indexed _account,
uint64 indexed _expirationDate,
address _nftAddress,
uint256 _tokenId,
bytes32 _role,
address _grantee,
uint64 _expirationDate,
address indexed _tokenAddress,
uint256 indexed _tokenId,
bytes _data
);

/// @notice Revokes a role from a user.
/// @param _role The role identifier.
/// @param _account The user that receives the role revocation.
/// @param _nftAddress The ERC721 token address.
/// @param _tokenId The ERC-721 token identifier.
/// @param _grantee The user that receives the role revocation.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
event RoleRevoked(
bytes32 indexed _role,
address indexed _account,
address _nftAddress,
bytes32 _role,
address _grantee,
address indexed _tokenAddress,
uint256 indexed _tokenId
);

/// @notice Grants a role to a user.
/// @param _role The role identifier.
/// @param _account The user that receives the role assignment.
/// @param _nftAddress The ERC721 token address.
/// @param _tokenId The ERC721 token identifier.
/// @param _grantee The user that receives the role assignment.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _expirationDate The expiration date of the role assignment.
/// @param _data Any additional data about the role assignment.
function grantRole(
bytes32 _role,
address _account,
address _nftAddress,
address _grantee,
address _tokenAddress,
uint256 _tokenId,
uint64 _expirationDate,
bytes calldata _data
) external;

/// @notice Revokes a role from a user.
/// @param _role The role identifier.
/// @param _account The user that receives the role revocation.
/// @param _nftAddress The ERC721 token address.
/// @param _tokenId The ERC-721 token identifier.
/// @param _grantee The user that receives the role revocation.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
function revokeRole(
bytes32 _role,
address _account,
address _nftAddress,
address _grantee,
address _tokenAddress,
uint256 _tokenId
) external;

/// @notice Checks if a user has a role.
/// @param _role The role identifier.
/// @param _owner The role creator
/// @param _account The user that receives the role.
/// @param _nftAddress The ERC721 token address.
/// @param _tokenId The ERC-721 token identifier.
/// @param _granter The role creator
/// @param _grantee The user that receives the role.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
/// @param _supportsMultipleAssignments if false, will return true only if account is the last role grantee
function hasRole(
bytes32 _role,
address _owner,
address _account,
address _nftAddress,
address _granter,
address _grantee,
address _tokenAddress,
uint256 _tokenId,
bool _supportsMultipleAssignments
) external view returns (bool);

/// @notice Returns the custom data and expiration date of a role assignment.
/// @param _role The role identifier.
/// @param _owner The role creator
/// @param _account The user that receives the role.
/// @param _nftAddress The ERC721 token address.
/// @param _tokenId The ERC-721 token identifier.
/// @param _granter The role creator
/// @param _grantee The user that receives the role.
/// @param _tokenAddress The token address.
/// @param _tokenId The token identifier.
function roleData(
bytes32 _role,
address _owner,
address _account,
address _nftAddress,
address _granter,
address _grantee,
address _tokenAddress,
uint256 _tokenId
) external view returns (uint64 expirationDate_, bytes memory data_);

Expand Down

0 comments on commit 9c7df89

Please sign in to comment.