-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get channel based entitlements for a specific permission from contrac…
…ts (#4) In order to mimic how we currently evaluate joinspace permissions in river node now, this gives us a list of entitlement datas per (channel, permission). Refactor also of existing endpoint into a queryable interface to separate concerns in the contracts. --------- Co-authored-by: Giuseppe Rodriguez <[email protected]>
- Loading branch information
1 parent
ec2e03c
commit 0b2d214
Showing
149 changed files
with
2,519 additions
and
1,353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
contracts/scripts/deployments/facets/DeployEntitlementDataQueryable.s.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
//interfaces | ||
|
||
//libraries | ||
|
||
//contracts | ||
import {Deployer} from "contracts/scripts/common/Deployer.s.sol"; | ||
import {FacetHelper} from "contracts/test/diamond/Facet.t.sol"; | ||
import {EntitlementDataQueryable} from "contracts/src/spaces/facets/entitlements/extensions/EntitlementDataQueryable.sol"; | ||
|
||
contract DeployEntitlementDataQueryable is Deployer, FacetHelper { | ||
// FacetHelper | ||
constructor() { | ||
addSelector( | ||
EntitlementDataQueryable.getEntitlementDataByPermission.selector | ||
); | ||
addSelector( | ||
EntitlementDataQueryable.getChannelEntitlementDataByPermission.selector | ||
); | ||
} | ||
|
||
// Deploying | ||
function versionName() public pure override returns (string memory) { | ||
return "entitlementDataQueryable"; | ||
} | ||
|
||
function __deploy( | ||
uint256 deployerPK, | ||
address | ||
) public override returns (address) { | ||
vm.startBroadcast(deployerPK); | ||
EntitlementDataQueryable facet = new EntitlementDataQueryable(); | ||
vm.stopBroadcast(); | ||
return address(facet); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
contracts/src/spaces/facets/entitlements/extensions/EntitlementDataQueryable.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
// interfaces | ||
import {IEntitlementDataQueryable} from "contracts/src/spaces/facets/entitlements/extensions/IEntitlementDataQueryable.sol"; | ||
import {IRolesBase} from "contracts/src/spaces/facets/roles/IRoles.sol"; | ||
import {IEntitlement} from "contracts/src/spaces/entitlements/IEntitlement.sol"; | ||
import {IRuleEntitlement} from "contracts/src/spaces/entitlements/rule/IRuleEntitlement.sol"; | ||
import {IUserEntitlement} from "contracts/src/spaces/entitlements/user/IUserEntitlement.sol"; | ||
|
||
// libraries | ||
import {ChannelService} from "contracts/src/spaces/facets/channels/ChannelService.sol"; | ||
|
||
// contracts | ||
import {RolesBase} from "contracts/src/spaces/facets/roles/RolesBase.sol"; | ||
import {Facet} from "contracts/src/diamond/facets/Facet.sol"; | ||
|
||
contract EntitlementDataQueryable is | ||
IRolesBase, | ||
IEntitlementDataQueryable, | ||
RolesBase, | ||
Facet | ||
{ | ||
function getEntitlementDataByPermission( | ||
string calldata permission | ||
) external view returns (EntitlementData[] memory) { | ||
Role[] memory roles = _getRolesWithPermission(permission); | ||
return _getEntitlements(roles); | ||
} | ||
|
||
function getChannelEntitlementDataByPermission( | ||
bytes32 channelId, | ||
string calldata permission | ||
) external view returns (EntitlementData[] memory) { | ||
Role[] memory roles = _getChannelRolesWithPermission(channelId, permission); | ||
return _getEntitlements(roles); | ||
} | ||
|
||
// ============================================================= | ||
// Internal | ||
// ============================================================= | ||
function _getChannelRolesWithPermission( | ||
bytes32 channelId, | ||
string calldata permission | ||
) internal view returns (Role[] memory) { | ||
uint256[] memory channelRoles = ChannelService.getRolesByChannel(channelId); | ||
|
||
uint256 roleCount = 0; | ||
uint256[] memory matchedRoleIds = new uint256[](channelRoles.length); | ||
|
||
bytes32 requestedPermission = keccak256(abi.encodePacked(permission)); | ||
|
||
// Count the number of roles that have the requested permission and record their ids. | ||
for (uint256 i = 0; i < channelRoles.length; i++) { | ||
Role memory role = _getRoleById(channelRoles[i]); | ||
if (role.disabled) { | ||
continue; | ||
} | ||
// Check if the role has the requested permission. | ||
for (uint256 j = 0; j < role.permissions.length; j++) { | ||
if (keccak256(bytes(role.permissions[j])) == requestedPermission) { | ||
matchedRoleIds[roleCount] = role.id; | ||
roleCount++; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Assemble the roles that have the requested permission for the specified channel. | ||
Role[] memory roles = new Role[](roleCount); | ||
for (uint256 i = 0; i < roleCount; i++) { | ||
roles[i] = _getRoleById(matchedRoleIds[i]); | ||
} | ||
|
||
return roles; | ||
} | ||
|
||
function _getEntitlements( | ||
Role[] memory roles | ||
) internal view returns (EntitlementData[] memory) { | ||
uint256 entitlementCount = 0; | ||
for (uint256 i = 0; i < roles.length; i++) { | ||
Role memory role = roles[i]; | ||
if (!role.disabled) { | ||
entitlementCount += role.entitlements.length; | ||
} | ||
} | ||
|
||
EntitlementData[] memory entitlementData = new EntitlementData[]( | ||
entitlementCount | ||
); | ||
|
||
entitlementCount = 0; | ||
|
||
for (uint256 i = 0; i < roles.length; i++) { | ||
Role memory role = roles[i]; | ||
if (!role.disabled) { | ||
for (uint256 j = 0; j < role.entitlements.length; j++) { | ||
IEntitlement entitlement = IEntitlement(role.entitlements[j]); | ||
if (!entitlement.isCrosschain()) { | ||
IUserEntitlement ue = IUserEntitlement(address(entitlement)); | ||
entitlementData[entitlementCount] = EntitlementData( | ||
ue.moduleType(), | ||
ue.getEntitlementDataByRoleId(role.id) | ||
); | ||
} else { | ||
IRuleEntitlement re = IRuleEntitlement(address(entitlement)); | ||
entitlementData[entitlementCount] = EntitlementData( | ||
re.moduleType(), | ||
re.getEntitlementDataByRoleId(role.id) | ||
); | ||
} | ||
entitlementCount++; | ||
} | ||
} | ||
} | ||
return entitlementData; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
contracts/src/spaces/facets/entitlements/extensions/IEntitlementDataQueryable.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
// interfaces | ||
|
||
// libraries | ||
|
||
// contracts | ||
interface IEntitlementDataQueryableBase { | ||
struct EntitlementData { | ||
string entitlementType; | ||
bytes entitlementData; | ||
} | ||
} | ||
|
||
interface IEntitlementDataQueryable is IEntitlementDataQueryableBase { | ||
// Entitlement data pertaining to all roles in the space. | ||
function getEntitlementDataByPermission( | ||
string calldata permission | ||
) external view returns (EntitlementData[] memory); | ||
|
||
// Entitlement data pertaining to all roles assigned to a channel. | ||
function getChannelEntitlementDataByPermission( | ||
bytes32 channelId, | ||
string calldata permission | ||
) external view returns (EntitlementData[] memory); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.