Skip to content

Commit

Permalink
chore: improve API
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 28, 2024
1 parent b949077 commit 3b64893
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/idempotent-proxy-canister/idempotent-proxy-canister.did
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type InitArgs = record {
proxy_token_refresh_interval : nat64;
subnet_size : nat64;
};
type Result = variant { Ok; Err : text };
type Result_1 = variant { Ok : StateInfo; Err };
type Result = variant { Ok : bool; Err : text };
type Result_1 = variant { Ok; Err : text };
type Result_2 = variant { Ok : StateInfo; Err : text };
type StateInfo = record {
freezing_threshold : nat64;
ecdsa_key_name : text;
Expand All @@ -53,18 +54,20 @@ type UpgradeArgs = record {
subnet_size : opt nat64;
};
service : (opt ChainArgs) -> {
admin_add_callers : (vec principal) -> (Result);
admin_add_managers : (vec principal) -> (Result);
admin_remove_callers : (vec principal) -> (Result);
admin_remove_managers : (vec principal) -> (Result);
admin_set_agents : (vec Agent) -> (Result);
get_state : () -> (Result_1) query;
admin_add_caller : (principal) -> (Result);
admin_add_callers : (vec principal) -> (Result_1);
admin_add_managers : (vec principal) -> (Result_1);
admin_remove_callers : (vec principal) -> (Result_1);
admin_remove_managers : (vec principal) -> (Result_1);
admin_set_agents : (vec Agent) -> (Result_1);
get_state : () -> (Result_2) query;
is_caller : (principal) -> (Result) query;
parallel_call_all_ok : (CanisterHttpRequestArgument) -> (HttpResponse);
parallel_call_any_ok : (CanisterHttpRequestArgument) -> (HttpResponse);
parallel_call_cost : (CanisterHttpRequestArgument) -> (nat) query;
proxy_http_request : (CanisterHttpRequestArgument) -> (HttpResponse);
proxy_http_request_cost : (CanisterHttpRequestArgument) -> (nat) query;
validate_admin_add_managers : (vec principal) -> (Result);
validate_admin_remove_managers : (vec principal) -> (Result);
validate_admin_set_agents : (vec Agent) -> (Result);
validate_admin_add_managers : (vec principal) -> (Result_1);
validate_admin_remove_managers : (vec principal) -> (Result_1);
validate_admin_set_agents : (vec Agent) -> (Result_1);
}
7 changes: 6 additions & 1 deletion src/idempotent-proxy-canister/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct StateInfo {
}

#[ic_cdk::query]
fn get_state() -> Result<StateInfo, ()> {
fn get_state() -> Result<StateInfo, String> {
let s = store::state::with(|s| StateInfo {
ecdsa_key_name: s.ecdsa_key_name.clone(),
proxy_token_public_key: s.proxy_token_public_key.clone(),
Expand All @@ -47,6 +47,11 @@ fn get_state() -> Result<StateInfo, ()> {
Ok(s)
}

#[ic_cdk::query]
fn is_caller(id: Principal) -> Result<bool, String> {
store::state::with(|s| Ok(s.allowed_callers.contains(&id)))
}

#[ic_cdk::query]
async fn proxy_http_request_cost(req: CanisterHttpRequestArgument) -> u128 {
let calc = store::state::cycles_calculator();
Expand Down
11 changes: 10 additions & 1 deletion src/idempotent-proxy-canister/src/api_admin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use candid::Principal;
use ic_cose_types::validate_principals;
use ic_cose_types::{validate_principals, ANONYMOUS};
use std::collections::BTreeSet;

use crate::{agent, is_controller, is_controller_or_manager, store, tasks};
Expand All @@ -22,6 +22,15 @@ fn admin_remove_managers(args: BTreeSet<Principal>) -> Result<(), String> {
})
}

#[ic_cdk::update(guard = "is_controller_or_manager")]
fn admin_add_caller(id: Principal) -> Result<bool, String> {
if id == ANONYMOUS {
Err("anonymous caller cannot be added".to_string())?;
}

store::state::with_mut(|r| Ok(r.allowed_callers.insert(id)))
}

#[ic_cdk::update(guard = "is_controller_or_manager")]
fn admin_add_callers(mut args: BTreeSet<Principal>) -> Result<(), String> {
validate_principals(&args)?;
Expand Down

0 comments on commit 3b64893

Please sign in to comment.