-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add Context Groups on-chain support (storage, CRUD, membership, proxy governance) #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rtb-12
wants to merge
22
commits into
master
Choose a base branch
from
feat/context-management-proposal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c8e5575
feat(context-config): implement context groups and migration support
rtb-12 995d3d2
feat(context-config): implement group management functionality
rtb-12 5e4cb25
feat(context-config): enhance context-group management with registrat…
rtb-12 1ece103
feat(context-config): implement target application management for con…
rtb-12 65b473d
feat(context-proxy): add proxy methods for group registration and unr…
rtb-12 4209eda
fix(Cargo.toml): update calimero-context-config dependency to use git…
rtb-12 a4d4f42
refactor(context-config): streamline code formatting and improve read…
rtb-12 fa8852e
refactor(context-config): improve function signatures and test assert…
rtb-12 20645f6
feat(context-config): enhance group management with nonce tracking an…
rtb-12 c00f21a
feat(context-config): add context registration approval functionality
rtb-12 a720981
feat(context-config): add context IDs tracking for group management
rtb-12 169bee6
refactor(context-config): clear group data during context reset
rtb-12 807a9e8
feat(context-proxy): update proxy_unregister_from_group to include gr…
rtb-12 308a927
feat(context-config): add fetch_group_nonce query method
rtb-12 a01fb00
feat(context-config): add group invitation commit/reveal contract logic
rtb-12 ec1e014
feat(context-config): add group_members view and join-context-via-group
rtb-12 d067199
feat(context-config): cascade group member removal to contexts
rtb-12 521f98d
fix(groups): propagate migration method on-chain for lazy upgrade peers
rtb-12 b1a9240
feat(context-config): add group permission system with capabilities a…
rtb-12 a93be63
test(context-config): add contract integration tests for group permis…
rtb-12 5833948
fix(context-config): address three BugBot issues in group management
rtb-12 370394b
fix(context-config): clean up visibility/allowlist on unregister and …
rtb-12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| use calimero_context_config::repr::{Repr, ReprBytes}; | ||
| use calimero_context_config::types::{ContextGroupId, SignedGroupRevealPayload}; | ||
| use near_sdk::borsh; | ||
| use near_sdk::{env, require, BlockHeight, CryptoHash}; | ||
|
|
||
| use super::ContextConfigs; | ||
|
|
||
| pub type Ed25519Signature = [u8; 64]; | ||
|
|
||
| impl ContextConfigs { | ||
| /// ### Step 1: Commit Group Invitation | ||
| /// | ||
| /// Anonymously commits to a hash of a future reveal payload. | ||
| /// Prevents MEV attacks. Can be called by the joiner or a relayer. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `group_id` - The group for which the commitment is being made. | ||
| /// * `commitment_hash` - A hex-encoded SHA-256 hash of the `GroupRevealPayloadData`. | ||
| /// * `expiration_block_height` - The block height at which the commitment expires. | ||
| pub fn commit_group_invitation( | ||
| &mut self, | ||
| group_id: Repr<ContextGroupId>, | ||
| commitment_hash: String, | ||
| expiration_block_height: BlockHeight, | ||
| ) { | ||
| let group = self | ||
| .groups | ||
| .get_mut(&group_id) | ||
| .expect("Group does not exist"); | ||
|
|
||
| let hash_bytes: CryptoHash = hex::decode(&commitment_hash) | ||
| .expect("Invalid hex hash") | ||
| .try_into() | ||
| .expect("Hash must be 32 bytes"); | ||
|
|
||
| require!( | ||
| !group.invitation_commitments.contains_key(&hash_bytes), | ||
| "This commitment has already been made" | ||
| ); | ||
|
|
||
| let current_block_height: BlockHeight = env::block_height(); | ||
| require!( | ||
| current_block_height < expiration_block_height, | ||
| "This invitation is already expired" | ||
| ); | ||
|
|
||
| let _ignored = group | ||
| .invitation_commitments | ||
| .insert(hash_bytes, expiration_block_height); | ||
|
|
||
| env::log_str(&format!( | ||
| "Successfully committed the group invitation image {} in group {}", | ||
| commitment_hash, group_id | ||
| )); | ||
| } | ||
|
|
||
| /// ### Step 2: Reveal Group Invitation | ||
| /// | ||
| /// Submits the full payload to claim the group invitation. The contract verifies | ||
| /// this payload against the prior commitment and validates all signatures and permissions. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `payload` - A `SignedGroupRevealPayload` containing the original data and the | ||
| /// joiner's signature. | ||
| pub fn reveal_group_invitation(&mut self, payload: SignedGroupRevealPayload) { | ||
| let payload_data = payload.data; | ||
| let invitation = payload_data.signed_open_invitation.invitation.clone(); | ||
| let group_id = invitation.group_id; | ||
|
|
||
| require!( | ||
| invitation.protocol == "near", | ||
| "The invitation was designated for another protocol" | ||
| ); | ||
| require!( | ||
| invitation.contract_id == env::current_account_id(), | ||
| "The invitation was designated for another contract" | ||
| ); | ||
|
|
||
| let group = self | ||
| .groups | ||
| .get_mut(&group_id) | ||
| .expect("Group does not exist"); | ||
|
|
||
| // 1. Hash the revealed data to find the original commitment. | ||
| let payload_data_bytes = | ||
| borsh::to_vec(&payload_data).expect("Failed to serialize payload data"); | ||
| let payload_data_hash_vec = env::sha256(&payload_data_bytes); | ||
| let payload_data_hash: CryptoHash = payload_data_hash_vec | ||
| .clone() | ||
| .try_into() | ||
| .expect("infallible conversion"); | ||
|
|
||
| // 2. Remove the commitment (replay prevention). | ||
| let _ignored = group | ||
| .invitation_commitments | ||
| .remove(&payload_data_hash) | ||
| .expect( | ||
| "No matching commitment found. It may have expired, been invalid, or already used", | ||
| ); | ||
|
|
||
| // 3. Check expiration. | ||
| require!( | ||
| env::block_height() <= invitation.expiration_height, | ||
| "The invitation has expired." | ||
| ); | ||
|
|
||
| // 4. Check the new member is not already in the group. | ||
| require!( | ||
| !group.members.contains(&payload_data.new_member_identity), | ||
| "Member already in group" | ||
| ); | ||
| require!( | ||
| !group.admins.contains(&payload_data.new_member_identity), | ||
| "Member is already a group admin" | ||
| ); | ||
|
|
||
| // 5. Verify the joiner's signature over the payload data. | ||
| let new_member_signature_bytes: Ed25519Signature = hex::decode(&payload.invitee_signature) | ||
| .expect("Invalid hex signature for new member") | ||
| .try_into() | ||
| .expect("Invalid signature length"); | ||
| require!( | ||
| env::ed25519_verify( | ||
| &new_member_signature_bytes, | ||
| &payload_data_hash_vec, | ||
| &payload_data.new_member_identity.as_bytes() | ||
| ), | ||
| "New member's signature is invalid." | ||
| ); | ||
|
|
||
| // 6. Verify the inviter's signature over the invitation. | ||
| let inviter_identity = invitation.inviter_identity; | ||
| let inviter_signature_bytes: Ed25519Signature = | ||
| hex::decode(&payload_data.signed_open_invitation.inviter_signature) | ||
| .expect("Invalid hex inviter signature") | ||
| .try_into() | ||
| .expect("Invalid signature length"); | ||
|
|
||
| let invitation_bytes = borsh::to_vec(&invitation).expect("Failed to serialize invitation"); | ||
| require!( | ||
| env::ed25519_verify( | ||
| &inviter_signature_bytes, | ||
| &env::sha256(&invitation_bytes), | ||
| &inviter_identity.as_bytes() | ||
| ), | ||
| "Inviter's signature is invalid" | ||
| ); | ||
|
|
||
| // 7. Verify the inviter is a group admin. | ||
| require!( | ||
| group.admins.contains(&inviter_identity), | ||
| "Inviter is not a group admin" | ||
| ); | ||
|
|
||
| // 8. Prevent replay of the inviter's signature. | ||
| let inviter_signature_hash: CryptoHash = env::sha256(&inviter_signature_bytes) | ||
| .try_into() | ||
| .expect("infallible conversion"); | ||
| require!( | ||
| group.used_invitations.insert(inviter_signature_hash), | ||
| "This invitation has already been used in this group." | ||
| ); | ||
|
|
||
| // 9. Add the new member to the group. | ||
| let _ignored = group.members.insert(payload_data.new_member_identity); | ||
|
|
||
| env::log_str(&format!( | ||
| "Account {} successfully joined group {}", | ||
| Repr::new(payload_data.new_member_identity), | ||
| Repr::new(group_id) | ||
| )); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Local filesystem path patch committed in Cargo.toml
High Severity
The
[patch]section overridescalimero-context-configwith a local filesystem path (../core/crates/context/config). This breaks builds for anyone who doesn't have thecorerepo checked out at that exact relative path, including CI pipelines. The correspondingCargo.lockalso lost its gitsourceline, confirming the override is active.