Skip to content

Commit

Permalink
feat: implemented check of members in Context contract and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 1, 2024
1 parent cf5a494 commit ce422bd
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 267 deletions.
2 changes: 1 addition & 1 deletion contracts/icp/context-config/context_contract.did
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ service : () -> {
privileges : (blob, vec blob) -> (
vec record { blob; vec ICCapability },
) query;
proxy_contract : (blob) -> (text) query;
proxy_contract : (blob) -> (principal) query;
set_proxy_code : (blob) -> (Result);
}
2 changes: 1 addition & 1 deletion contracts/icp/context-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod types;
pub struct Context {
pub application: Guard<ICApplication>,
pub members: Guard<Vec<ICContextIdentity>>,
pub proxy: Guard<String>,
pub proxy: Guard<Principal>,
}

pub struct ContextConfigs {
Expand Down
13 changes: 6 additions & 7 deletions contracts/icp/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn add_context(
})
}

async fn deploy_proxy_contract(context_id: &ICContextId) -> Result<String, String> {
async fn deploy_proxy_contract(context_id: &ICContextId) -> Result<Principal, String> {
// Get the proxy code
let proxy_code = CONTEXT_CONFIGS
.with(|configs| configs.borrow().proxy_code.clone())
Expand All @@ -112,7 +112,7 @@ async fn deploy_proxy_contract(context_id: &ICContextId) -> Result<String, Strin
}),
};

let (canister_record,) = ic_cdk::api::management_canister::main::create_canister(
let (canister_record,) = create_canister(
create_args,
500_000_000_000_000u128,
)
Expand All @@ -132,11 +132,11 @@ async fn deploy_proxy_contract(context_id: &ICContextId) -> Result<String, Strin
arg: init_args,
};

ic_cdk::api::management_canister::main::install_code(install_args)
install_code(install_args)
.await
.map_err(|e| format!("Failed to install code: {:?}", e))?;

Ok(canister_id.to_string())
Ok(canister_id)
}

fn update_application(
Expand Down Expand Up @@ -362,13 +362,12 @@ async fn update_proxy_contract(
// Update the proxy contract code
let install_args = InstallCodeArgument {
mode: ic_cdk::api::management_canister::main::CanisterInstallMode::Upgrade(None),
canister_id: Principal::from_text(proxy_canister_id)
.map_err(|e| format!("Invalid canister ID: {}", e))?,
canister_id: proxy_canister_id,
wasm_module: proxy_code,
arg: candid::encode_one(&context_id).map_err(|e| format!("Encoding error: {}", e))?,
};

ic_cdk::api::management_canister::main::install_code(install_args)
install_code(install_args)
.await
.map_err(|e| format!("Failed to update proxy contract: {:?}", e))?;

Expand Down
3 changes: 2 additions & 1 deletion contracts/icp/context-config/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;

use calimero_context_config::repr::ReprTransmute;
use candid::Principal;
use ic_cdk_macros::query;

use crate::types::*;
Expand Down Expand Up @@ -33,7 +34,7 @@ fn application_revision(context_id: ICContextId) -> u64 {
}

#[query]
fn proxy_contract(context_id: ICContextId) -> String {
fn proxy_contract(context_id: ICContextId) -> Principal {
CONTEXT_CONFIGS.with(|configs| {
let configs = configs.borrow();
let context = configs
Expand Down
38 changes: 23 additions & 15 deletions contracts/icp/proxy-contract/src/mutate.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
use std::collections::HashSet;

use candid::{CandidType, Principal};
use ic_cdk::api::call::CallResult;

use crate::types::*;
use crate::PROXY_CONTRACT;

async fn check_member(_signer_id: &ICSignerId) -> Result<bool, String> {
// let context_canister_id = PROXY_CONTRACT.with(|contract| {
// contract.borrow().context_config_id.clone()
// });

// let principal = identity_to_principal(&signer_id.0);
// TODO: implement this
// let call_result: CallResult<(bool,)> = ic_cdk::call(
// Principal::from_text(&context_canister_id)
// .map_err(|e| format!("Invalid context canister ID: {}", e))?,
// "is_member",
// (principal,),
// ).await.map_err(|e| format!("Failed to call context contract: {:?}", e))?;

Ok(true)
use calimero_context_config::repr::ReprTransmute;

async fn check_member(signer_id: &ICSignerId) -> Result<bool, String> {
let (context_canister_id, context_id) = PROXY_CONTRACT.with(|contract| {
(contract.borrow().context_config_id.clone(), contract.borrow().context_id.clone())
});

let identity = ICContextIdentity::new(signer_id.rt().expect("Invalid signer id"));

let args = candid::encode_args((context_id, identity)).expect("Failed to encode args");

let call_result: CallResult<(bool, )> = ic_cdk::call(
Principal::from_text(&context_canister_id)
.map_err(|e| format!("Invalid context canister ID: {}", e))?,
"has_member",
(args,),
).await;

match call_result {
Ok((is_member,)) => Ok(is_member),
Err(e) => Err(format!("Error checking membership: {:?}", e))
}
}

#[ic_cdk::update]
Expand Down
51 changes: 43 additions & 8 deletions contracts/icp/proxy-contract/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ impl ICSignerId {
pub fn new(bytes: [u8; 32]) -> Self {
Self(Identity(bytes))
}

pub fn as_bytes(&self) -> [u8; 32] {
self.0.as_bytes()
}
}

impl Default for ICSignerId {
Expand Down Expand Up @@ -97,10 +93,6 @@ impl ICContextId {
pub fn new(bytes: [u8; 32]) -> Self {
Self(Identity(bytes))
}

pub fn as_bytes(&self) -> [u8; 32] {
self.0.as_bytes()
}
}

impl Default for ICContextId {
Expand All @@ -109,6 +101,49 @@ impl Default for ICContextId {
}
}

impl ReprBytes for ICContextId {
type EncodeBytes<'a> = [u8; 32];
type DecodeBytes = [u8; 32];
type Error = LengthMismatch;

fn as_bytes(&self) -> Self::EncodeBytes<'_> {
self.0.as_bytes()
}

fn from_bytes<F>(f: F) -> repr::Result<Self, Self::Error>
where
F: FnOnce(&mut Self::DecodeBytes) -> Bs58Result<usize>,
{
Identity::from_bytes(f).map(Self)
}
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq)]
pub struct ICContextIdentity(Identity);

impl ICContextIdentity {
pub fn new(bytes: [u8; 32]) -> Self {
Self(Identity(bytes))
}
}

impl ReprBytes for ICContextIdentity {
type EncodeBytes<'a> = [u8; 32];
type DecodeBytes = [u8; 32];
type Error = LengthMismatch;

fn as_bytes(&self) -> Self::EncodeBytes<'_> {
self.0.as_bytes()
}

fn from_bytes<F>(f: F) -> repr::Result<Self, Self::Error>
where
F: FnOnce(&mut Self::DecodeBytes) -> Bs58Result<usize>,
{
Identity::from_bytes(f).map(Self)
}
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq)]
pub struct ICProposalId(pub [u8; 32]);

Expand Down
59 changes: 59 additions & 0 deletions contracts/icp/proxy-contract/tests/context_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use proxy_contract::types::{ICContextId, ICContextIdentity, ICSignerId};

#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct Request {
pub kind: RequestKind,
pub signer_id: ICSignerId,
pub timestamp_ms: u64,
}

#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub enum RequestKind {
Context(ContextRequest),
}

#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct ContextRequest {
pub context_id: ICContextId,
pub kind: ContextRequestKind,
}

#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub enum ContextRequestKind {
Add {
author_id: ICContextIdentity,
application: ICApplication,
},
AddMembers {
members: Vec<ICContextIdentity>,
},
}

#[derive(CandidType, Serialize, Deserialize, Debug, Clone)]
pub struct ICApplication {
pub id: ICApplicationId,
pub blob: ICBlobId,
pub size: u64,
pub source: String,
pub metadata: Vec<u8>,
}

#[derive(CandidType, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ICApplicationId(pub [u8; 32]);

#[derive(CandidType, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ICBlobId(pub [u8; 32]);

impl ICApplicationId {
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}

impl ICBlobId {
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}
Loading

0 comments on commit ce422bd

Please sign in to comment.