feat(admin): Add role validation with bitwise AND check - #795
Open
Fayyo wants to merge 2 commits into
Open
Conversation
- Add bitflags dependency for efficient bitwise role operations - Introduce RoleFlags enum with unique bit positions for each role - Implement validate_role_not_granted() using bitwise AND check - Add grant_role_checked() for atomic validate-and-grant - Add get_roles_bitmask() for batch role querying - Add AdminError::RoleAlreadyGranted (code 7) - Comprehensive unit tests covering happy paths and error states Closes BCPathway#775
- Add AdminError::UnauthorizedDeployer (code 8) - Add require_deployer() utility to admin module - Protect init_storage() in admin with deployer check - Protect initialize() in token, wrapper, vesting contracts - Add comprehensive unit tests for deployer check and double-init failure Closes BCPathway#772
|
@Fayyo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
@Fayyo return my checks and resolve conflicts |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds validation check for role assignments using bitwise AND operations. This prevents granting duplicate roles to the same address and provides efficient batch role querying capabilities.
Implementation
Core Changes
Added
bitflagsdependency tocontracts/admin/Cargo.tomlfor type-safe bitwise operationsNew
RoleFlagsenum (contracts/admin/src/lib.rs:206-256):Admin=1<<0,Minter=1<<1,SuperAdmin=1<<2,Pauser=1<<3from_role(),bits(), andis_set(mask, role)methodsFrom<Role>andFrom<RoleFlags>foru32conversionsNew public functions:
validate_role_not_granted(env, role, address)- Validates role is not already granted using bitwise AND checkgrant_role_checked(env, caller, role, address)- Atomic validate-and-grant operationget_roles_bitmask(env, address)- Returns combined bitmask of all roles for efficient batch queriesNew error code:
AdminError::RoleAlreadyGranted = 7Acceptance Criteria Met
✅ Code compiles and adheres to Soroban/Rust/TypeScript best practices
#[repr(u32)]for stable discriminant valuesAdminErrorenum✅ Unit tests cover both happy paths and expected error states
test_validate_role_not_granted_succeeds_when_role_not_held- Happy pathtest_validate_role_not_granted_fails_when_role_already_held- Duplicate role errortest_validate_role_not_granted_fails_when_admin_role_held- Admin implies all rolestest_validate_role_not_granted_rejects_zero_address- Zero address validationtest_grant_role_checked_succeeds_when_role_not_held- Atomic grant successtest_grant_role_checked_fails_when_role_already_held- Atomic grant failuretest_grant_role_checked_fails_when_admin_role_held- Admin role implies all rolestest_role_flags_bitwise_operations- Bitwise flag operationstest_get_roles_bitmask_returns_zero_for_no_roles- Empty masktest_get_roles_bitmask_returns_correct_mask_for_single_role- Single roletest_get_roles_bitmask_returns_combined_mask_for_multiple_roles- Multiple rolestest_get_roles_bitmask_includes_admin_role- Admin role bitmasktest_get_roles_bitmask_returns_zero_for_zero_address- Zero address edge casetest_get_roles_bitmask_enables_bitwise_role_checks- Bitwise query usage✅ All CI/CD pipelines pass successfully
Usage Example
Closes #775