Skip to main content

canic_core/api/rpc/
mod.rs

1mod capability;
2
3use crate::{
4    cdk::{candid::CandidType, types::Principal},
5    dto::{
6        capability::{RootCapabilityEnvelopeV1, RootCapabilityResponseV1},
7        error::Error,
8        rpc::{CreateCanisterParent, CreateCanisterResponse, UpgradeCanisterResponse},
9    },
10    ids::CanisterRole,
11    workflow::rpc::request::RpcRequestWorkflow,
12};
13
14///
15/// RpcApi
16///
17/// Public, user-callable wrappers for Canic's internal RPC workflows.
18///
19/// These functions:
20/// - form part of the public API surface
21/// - are safe to call from downstream canister `lib.rs` code
22/// - return [`Error`] suitable for IC boundaries
23///
24/// Internally, they delegate to workflow-level RPC implementations,
25/// preserving the layering:
26///
27///   user canister -> api -> workflow -> ops -> infra
28///
29/// Workflow returns internal [`InternalError`]; conversion to [`Error`]
30/// happens exclusively at this API boundary.
31///
32
33pub struct RpcApi;
34
35impl RpcApi {
36    /// Dispatch the full root capability envelope verifier/orchestrator path.
37    pub async fn response_capability_v1_root(
38        envelope: RootCapabilityEnvelopeV1,
39    ) -> Result<RootCapabilityResponseV1, Error> {
40        capability::response_capability_v1_root(envelope).await
41    }
42
43    /// Dispatch the non-root structural cycles capability path.
44    pub async fn response_capability_v1_nonroot(
45        envelope: RootCapabilityEnvelopeV1,
46    ) -> Result<RootCapabilityResponseV1, Error> {
47        capability::response_capability_v1_nonroot(envelope).await
48    }
49
50    pub async fn create_canister_request<A>(
51        canister_role: &CanisterRole,
52        parent: CreateCanisterParent,
53        extra: Option<A>,
54    ) -> Result<CreateCanisterResponse, Error>
55    where
56        A: CandidType + Send + Sync,
57    {
58        RpcRequestWorkflow::create_canister_request(canister_role, parent, extra)
59            .await
60            .map_err(Error::from)
61    }
62
63    pub async fn upgrade_canister_request(
64        canister_pid: Principal,
65    ) -> Result<UpgradeCanisterResponse, Error> {
66        RpcRequestWorkflow::upgrade_canister_request(canister_pid)
67            .await
68            .map_err(Error::from)
69    }
70
71    pub async fn response_capability_v1(
72        envelope: RootCapabilityEnvelopeV1,
73    ) -> Result<RootCapabilityResponseV1, Error> {
74        capability::response_capability_v1_root(envelope).await
75    }
76}