Skip to main content

ic_cdk/
api.rs

1//! System API bindings.
2//!
3//! This module provides Rust ergonomic bindings to the system APIs.
4//!
5//! Some APIs require more advanced handling and are organized into separate modules:
6//! * For the inter-canister calls API, see the [`call`](mod@crate::call) module.
7//! * For the stable memory management API, see the .
8//!   * The basic bindings are provided in this module including [`stable_size`], [`stable_grow`], [`stable_read`] and [`stable_write`].
9//!   * The [`stable`](crate::stable) module provides more advanced functionalities, e.g. support for `std::io` traits.
10//!
11//! APIs that are only available for `wasm32` are not included.
12//! As a result, system APIs with a numeric postfix (indicating the data bit width) are bound to names without the postfix.
13//! For example, `ic0::msg_cycles_available128` is bound to [`msg_cycles_available`], while `ic0::msg_cycles_available` has no binding.
14//!
15//! Functions that provide bindings for a single system API method share the same name as the system API.
16//! For example, `ic0::msg_reject_code` is bound to [`msg_reject_code`].
17//!
18//! Functions that wrap multiple system API methods are named using the common prefix of the wrapped methods.
19//! For example, [`msg_arg_data`] wraps both `ic0::msg_arg_data_size` and `ic0::msg_arg_data_copy`.
20
21use candid::Principal;
22use std::{convert::TryFrom, num::NonZeroU64};
23
24/// Gets the message argument data.
25pub fn msg_arg_data() -> Vec<u8> {
26    let len = ic0::msg_arg_data_size();
27    let mut buf = vec![0u8; len];
28    ic0::msg_arg_data_copy(&mut buf, 0);
29    buf
30}
31
32/// Gets the identity of the caller, which may be a canister id or a user id.
33///
34/// During canister installation or upgrade, this is the id of the user or canister requesting the installation or upgrade.
35/// During a system task (heartbeat or global timer), this is the id of the management canister.
36pub fn msg_caller() -> Principal {
37    let len = ic0::msg_caller_size();
38    let mut buf = vec![0u8; len];
39    ic0::msg_caller_copy(&mut buf, 0);
40    // Trust that the system always returns a valid principal.
41    Principal::try_from(&buf).unwrap()
42}
43
44/// Gets auxiliary data about the caller as provided by the canister with which the caller's identity is associated.
45///
46/// This only returns non-empty data if the caller is a self-authenticating principal authenticated
47/// by canister signatures (e.g. Internet Identity). Returns empty bytes when the caller is another canister.
48///
49/// The data is guaranteed to be signed by the canister returned from [`msg_caller_info_signer`],
50/// so the signer should be checked before trusting the payload.
51///
52/// ```rust,no_run
53/// use ic_cdk::api::{msg_caller_info_data, msg_caller_info_signer};
54///
55/// if msg_caller_info_signer().is_some() {
56///     let data = msg_caller_info_data();
57///     // Decode per the signer's documented format (e.g. identity attributes).
58/// }
59/// ```
60pub fn msg_caller_info_data() -> Vec<u8> {
61    let len = ic0::msg_caller_info_data_size();
62    let mut buf = vec![0u8; len];
63    ic0::msg_caller_info_data_copy(&mut buf, 0);
64    buf
65}
66
67/// Gets the canister ID of the canister that provided the caller's canister signature.
68///
69/// Returns `None` if the caller is not a self-authenticating principal authenticated by canister
70/// signatures (e.g. when the caller is another canister or no sender info was provided).
71///
72/// ```rust,no_run
73/// use ic_cdk::api::msg_caller_info_signer;
74/// use candid::Principal;
75///
76/// let trusted_issuer = Principal::from_text("rdmx6-jaaaa-aaaaa-aaadq-cai").unwrap();
77/// if msg_caller_info_signer() == Some(trusted_issuer) {
78///     // Caller's identity was attested by the trusted issuer (e.g. Internet Identity).
79/// }
80/// ```
81pub fn msg_caller_info_signer() -> Option<Principal> {
82    let len = ic0::msg_caller_info_signer_size();
83    if len == 0 {
84        return None;
85    }
86    let mut buf = vec![0u8; len];
87    ic0::msg_caller_info_signer_copy(&mut buf, 0);
88    // Trust that the system always returns a valid principal when non-empty.
89    Some(Principal::try_from(&buf).expect("msg_caller_info_signer must be a valid principal"))
90}
91
92/// Returns the reject code, if the current function is invoked as a reject callback.
93pub fn msg_reject_code() -> u32 {
94    ic0::msg_reject_code()
95}
96
97/// Gets the reject message.
98///
99/// This function can only be called in the reject callback.
100///
101/// Traps if:
102/// - There is no reject message (i.e. if `reject_code` is 0).
103/// - The message is not valid UTF-8.
104pub fn msg_reject_msg() -> String {
105    let len = ic0::msg_reject_msg_size();
106    let mut buf = vec![0u8; len];
107    ic0::msg_reject_msg_copy(&mut buf, 0);
108    String::from_utf8(buf).expect("reject message is not valid UTF-8")
109}
110
111/// Gets the deadline, in nanoseconds since 1970-01-01, after which the caller might stop waiting for a response.
112///
113/// For calls to update methods with best-effort responses and their callbacks,
114/// the deadline is computed based on the time the call was made,
115/// and the `timeout_seconds` parameter provided by the caller.
116/// In such cases, the deadline value will be converted to `NonZeroU64` and wrapped in `Some`.
117/// To get the deadline value as a `u64`, call `get()` on the `NonZeroU64` value.
118///
119/// ```rust,no_run
120/// use ic_cdk::api::msg_deadline;
121/// if let Some(deadline) = msg_deadline() {
122///     let deadline_value : u64 = deadline.get();
123/// }
124/// ```
125///
126/// For other calls (ingress messages and all calls to query and composite query methods,
127/// including calls in replicated mode), a `None` is returned.
128/// Please note that the raw `msg_deadline` system API returns 0 in such cases.
129/// This function is a wrapper around the raw system API that provides more semantic information through the return type.
130pub fn msg_deadline() -> Option<NonZeroU64> {
131    let nano_seconds = ic0::msg_deadline();
132    match nano_seconds {
133        0 => None,
134        _ => Some(NonZeroU64::new(nano_seconds).unwrap()),
135    }
136}
137
138/// Replies to the sender with the data.
139pub fn msg_reply<T: AsRef<[u8]>>(data: T) {
140    let buf = data.as_ref();
141    if !buf.is_empty() {
142        ic0::msg_reply_data_append(buf);
143    }
144    ic0::msg_reply();
145}
146
147/// Rejects the call with a diagnostic message.
148pub fn msg_reject<T: AsRef<str>>(message: T) {
149    let message = message.as_ref();
150    ic0::msg_reject(message.as_bytes());
151}
152
153/// Gets the number of cycles transferred by the caller of the current call, still available in this message.
154pub fn msg_cycles_available() -> u128 {
155    ic0::msg_cycles_available128()
156}
157
158/// Gets the amount of cycles that came back with the response as a refund
159///
160/// This function can only be used in a callback handler (reply or reject).
161/// The refund has already been added to the canister balance automatically.
162pub fn msg_cycles_refunded() -> u128 {
163    ic0::msg_cycles_refunded128()
164}
165
166/// Moves cycles from the call to the canister balance.
167///
168/// The actual amount moved will be returned.
169pub fn msg_cycles_accept(max_amount: u128) -> u128 {
170    ic0::msg_cycles_accept128(max_amount)
171}
172
173/// Burns cycles from the canister.
174///
175/// Returns the amount of cycles that were actually burned.
176pub fn cycles_burn(amount: u128) -> u128 {
177    ic0::cycles_burn128(amount)
178}
179
180/// Gets canister's own identity.
181pub fn canister_self() -> Principal {
182    let len = ic0::canister_self_size();
183    let mut buf = vec![0u8; len];
184    ic0::canister_self_copy(&mut buf, 0);
185    // Trust that the system always returns a valid principal.
186    Principal::try_from(&buf).unwrap()
187}
188
189/// Gets the current cycle balance of the canister.
190pub fn canister_cycle_balance() -> u128 {
191    ic0::canister_cycle_balance128()
192}
193
194/// Gets the current amount of cycles that is available for spending in calls and execution.
195pub fn canister_liquid_cycle_balance() -> u128 {
196    ic0::canister_liquid_cycle_balance128()
197}
198
199/// Gets the status of the canister.
200///
201/// The status is one of the following:
202/// - 1: Running
203/// - 2: Stopping
204/// - 3: Stopped
205pub fn canister_status() -> CanisterStatusCode {
206    ic0::canister_status().into()
207}
208
209/// The status of a canister.
210///
211/// See [Canister status](https://internetcomputer.org/docs/current/references/ic-interface-spec/#system-api-canister-status).
212#[derive(Debug, PartialEq, Eq, Clone, Copy)]
213#[repr(u32)]
214pub enum CanisterStatusCode {
215    /// Running.
216    Running = 1,
217    /// Stopping.
218    Stopping = 2,
219    /// Stopped.
220    Stopped = 3,
221    /// A status code that is not recognized by this library.
222    Unrecognized(u32),
223}
224
225impl From<u32> for CanisterStatusCode {
226    fn from(value: u32) -> Self {
227        match value {
228            1 => Self::Running,
229            2 => Self::Stopping,
230            3 => Self::Stopped,
231            _ => Self::Unrecognized(value),
232        }
233    }
234}
235
236impl From<CanisterStatusCode> for u32 {
237    fn from(value: CanisterStatusCode) -> Self {
238        match value {
239            CanisterStatusCode::Running => 1,
240            CanisterStatusCode::Stopping => 2,
241            CanisterStatusCode::Stopped => 3,
242            CanisterStatusCode::Unrecognized(value) => value,
243        }
244    }
245}
246
247impl PartialEq<u32> for CanisterStatusCode {
248    fn eq(&self, other: &u32) -> bool {
249        let self_as_u32: u32 = (*self).into();
250        self_as_u32 == *other
251    }
252}
253
254/// Gets the canister version.
255///
256/// See [Canister version](https://internetcomputer.org/docs/current/references/ic-interface-spec/#system-api-canister-version).
257pub fn canister_version() -> u64 {
258    ic0::canister_version()
259}
260
261/// Gets the ID of the subnet on which the canister is running.
262pub fn subnet_self() -> Principal {
263    let len = ic0::subnet_self_size();
264    let mut buf = vec![0u8; len];
265    ic0::subnet_self_copy(&mut buf, 0);
266    // Trust that the system always returns a valid principal.
267    Principal::try_from(&buf).unwrap()
268}
269
270/// Gets the name of the method to be inspected.
271///
272/// This function is only available in the `canister_inspect_message` context.
273///
274/// Traps if the method name is not valid UTF-8.
275pub fn msg_method_name() -> String {
276    let len = ic0::msg_method_name_size();
277    let mut buf = vec![0u8; len];
278    ic0::msg_method_name_copy(&mut buf, 0);
279    String::from_utf8(buf).expect("msg_method_name is not valid UTF-8")
280}
281
282/// Accepts the message in `canister_inspect_message`.
283///
284/// This function is only available in the `canister_inspect_message` context.
285/// This function traps if invoked twice.
286pub fn accept_message() {
287    ic0::accept_message();
288}
289
290/// Gets the current size of the stable memory (in WebAssembly pages).
291///
292/// One WebAssembly page is 64KiB.
293pub fn stable_size() -> u64 {
294    ic0::stable64_size()
295}
296
297/// Attempts to grow the stable memory by `new_pages` many pages containing zeroes.
298///
299/// One WebAssembly page is 64KiB.
300///
301/// If successful, returns the previous size of the memory (in pages).
302/// Otherwise, returns `u64::MAX`.
303pub fn stable_grow(new_pages: u64) -> u64 {
304    ic0::stable64_grow(new_pages)
305}
306
307/// Writes data to the stable memory location specified by an offset.
308///
309/// # Warning
310/// This will panic if `offset + buf.len()` exceeds the current size of stable memory.
311/// Call [`stable_grow`] to request more stable memory if needed.
312pub fn stable_write(offset: u64, buf: &[u8]) {
313    ic0::stable64_write(buf, offset);
314}
315
316/// Reads data from the stable memory location specified by an offset.
317///
318/// # Warning
319/// This will panic if `offset + buf.len()` exceeds the current size of stable memory.
320pub fn stable_read(offset: u64, buf: &mut [u8]) {
321    ic0::stable64_read(buf, offset);
322}
323
324/// Gets the public key (a DER-encoded BLS key) of the root key of this instance of the Internet Computer Protocol.
325///
326/// # Note
327///
328/// This traps in non-replicated mode.
329pub fn root_key() -> Vec<u8> {
330    let len = ic0::root_key_size();
331    let mut buf = vec![0u8; len];
332    ic0::root_key_copy(&mut buf, 0);
333    buf
334}
335
336/// Sets the certified data of this canister.
337///
338/// Canisters can store up to 32 bytes of data that is certified by
339/// the system on a regular basis.  One can call [`data_certificate`]
340/// function from a query call to get a certificate authenticating the
341/// value set by calling this function.
342///
343/// This function can only be called from the following contexts:
344/// - `canister_init`, `canister_pre_upgrade` and `canister_post_upgrade`
345///   hooks.
346/// - `canister_update` calls.
347/// - reply or reject callbacks.
348///
349/// # Panics
350///
351/// - This function traps if `data.len() > 32`.
352/// - This function traps if it's called from an illegal context
353///   (e.g., from a query call).
354pub fn certified_data_set<T: AsRef<[u8]>>(data: T) {
355    let buf = data.as_ref();
356    ic0::certified_data_set(buf);
357}
358
359/// When called from a query call, returns the data certificate authenticating
360/// certified data set by this canister.
361///
362/// Returns `None` if called not from a query call.
363pub fn data_certificate() -> Option<Vec<u8>> {
364    if ic0::data_certificate_present() == 0 {
365        return None;
366    }
367    let n = ic0::data_certificate_size();
368    let mut buf = vec![0u8; n];
369    ic0::data_certificate_copy(&mut buf, 0);
370    Some(buf)
371}
372
373/// Gets current timestamp, in nanoseconds since the epoch (1970-01-01)
374pub fn time() -> u64 {
375    ic0::time()
376}
377
378/// Sets global timer.
379///
380/// The canister can set a global timer to make the system
381/// schedule a call to the exported `canister_global_timer`
382/// Wasm method after the specified time.
383/// The time must be provided as nanoseconds since 1970-01-01.
384///
385/// The function returns the previous value of the timer.
386/// If no timer is set before invoking the function, then the function returns zero.
387///
388/// Passing zero as an argument to the function deactivates the timer and thus
389/// prevents the system from scheduling calls to the canister's `canister_global_timer` Wasm method.
390pub fn global_timer_set(timestamp: u64) -> u64 {
391    ic0::global_timer_set(timestamp)
392}
393
394/// Gets the value of specified performance counter.
395///
396/// See [`PerformanceCounterType`] for available counter types.
397#[inline]
398pub fn performance_counter(counter_type: impl Into<PerformanceCounterType>) -> u64 {
399    let counter_type: u32 = counter_type.into().into();
400    ic0::performance_counter(counter_type)
401}
402
403/// The type of performance counter.
404#[derive(Debug, PartialEq, Eq, Clone, Copy)]
405#[repr(u32)]
406pub enum PerformanceCounterType {
407    /// Current execution instruction counter.
408    ///
409    /// The number of WebAssembly instructions the canister has executed
410    /// since the beginning of the current Message execution.
411    InstructionCounter,
412    /// Call context instruction counter
413    ///
414    /// The number of WebAssembly instructions the canister has executed
415    /// within the call context of the current Message execution
416    /// since Call context creation.
417    /// The counter monotonically increases across all message executions
418    /// in the call context until the corresponding call context is removed.
419    CallContextInstructionCounter,
420    /// A performance counter type that is not recognized by this library.
421    Unrecognized(u32),
422}
423
424impl From<u32> for PerformanceCounterType {
425    fn from(value: u32) -> Self {
426        match value {
427            0 => Self::InstructionCounter,
428            1 => Self::CallContextInstructionCounter,
429            _ => Self::Unrecognized(value),
430        }
431    }
432}
433
434impl From<PerformanceCounterType> for u32 {
435    fn from(value: PerformanceCounterType) -> Self {
436        match value {
437            PerformanceCounterType::InstructionCounter => 0,
438            PerformanceCounterType::CallContextInstructionCounter => 1,
439            PerformanceCounterType::Unrecognized(value) => value,
440        }
441    }
442}
443
444impl PartialEq<u32> for PerformanceCounterType {
445    fn eq(&self, other: &u32) -> bool {
446        let self_as_u32: u32 = (*self).into();
447        self_as_u32 == *other
448    }
449}
450
451/// Returns the number of instructions that the canister executed since the last [entry
452/// point](https://internetcomputer.org/docs/current/references/ic-interface-spec/#entry-points).
453#[inline]
454pub fn instruction_counter() -> u64 {
455    performance_counter(0)
456}
457
458/// Returns the number of WebAssembly instructions the canister has executed
459/// within the call context of the current Message execution since
460/// Call context creation.
461///
462/// The counter monotonically increases across all message executions
463/// in the call context until the corresponding call context is removed.
464#[inline]
465pub fn call_context_instruction_counter() -> u64 {
466    performance_counter(1)
467}
468
469/// Determines if a Principal is a controller of the canister.
470pub fn is_controller(principal: &Principal) -> bool {
471    let slice = principal.as_slice();
472    match ic0::is_controller(slice) {
473        0 => false,
474        1 => true,
475        n => panic!("unexpected return value from is_controller: {n}"),
476    }
477}
478
479/// Checks if in replicated execution.
480///
481/// The canister can check whether it is currently running in replicated or non replicated execution.
482pub fn in_replicated_execution() -> bool {
483    match ic0::in_replicated_execution() {
484        0 => false,
485        1 => true,
486        n => panic!("unexpected return value from in_replicated_execution: {n}"),
487    }
488}
489
490/// Gets the amount of cycles that a canister needs to be above the freezing threshold in order to successfully make an inter-canister call.
491pub fn cost_call(method_name_size: u64, payload_size: u64) -> u128 {
492    ic0::cost_call(method_name_size, payload_size)
493}
494
495/// Gets the cycle cost of the Management canister method [`create_canister`](https://internetcomputer.org/docs/references/ic-interface-spec#ic-create_canister).
496pub fn cost_create_canister() -> u128 {
497    ic0::cost_create_canister()
498}
499
500/// Gets the cycle cost of the Management canister method [`http_request`](https://internetcomputer.org/docs/references/ic-interface-spec#ic-http_request).
501pub fn cost_http_request(request_size: u64, max_res_bytes: u64) -> u128 {
502    ic0::cost_http_request(request_size, max_res_bytes)
503}
504
505/// The error type for [`cost_sign_with_ecdsa`] and [`cost_sign_with_schnorr`].
506#[derive(thiserror::Error, Debug, Clone)]
507pub enum SignCostError {
508    /// The ECDSA/vetKD curve or Schnorr algorithm is invalid.
509    #[error("invalid curve or algorithm")]
510    InvalidCurveOrAlgorithm,
511
512    /// The key name is invalid for the provided curve or algorithm.
513    #[error("invalid key name")]
514    InvalidKeyName,
515    /// Unrecognized error.
516    ///
517    /// This error is returned when the System API returns an unrecognized error code.
518    /// Please report to ic-cdk maintainers.
519    #[error("unrecognized error: {0}")]
520    UnrecognizedError(u32),
521}
522
523/// Helper function to handle the result of a signature cost function.
524fn sign_cost_result(dst: u128, code: u32) -> Result<u128, SignCostError> {
525    match code {
526        0 => Ok(dst),
527        1 => Err(SignCostError::InvalidCurveOrAlgorithm),
528        2 => Err(SignCostError::InvalidKeyName),
529        _ => Err(SignCostError::UnrecognizedError(code)),
530    }
531}
532
533/// Gets the cycle cost of the Management canister method [`sign_with_ecdsa`](https://internetcomputer.org/docs/references/ic-interface-spec#ic-sign_with_ecdsa).
534///
535/// # Errors
536///
537/// This function will return an error if the `key_name` or the `ecdsa_curve` is invalid.
538/// The error type [`SignCostError`] provides more information about the reason of the error.
539pub fn cost_sign_with_ecdsa<T: AsRef<str>>(
540    key_name: T,
541    ecdsa_curve: u32,
542) -> Result<u128, SignCostError> {
543    let key_name = key_name.as_ref();
544    let (cost, code) = ic0::cost_sign_with_ecdsa(key_name, ecdsa_curve);
545    sign_cost_result(cost, code)
546}
547
548/// Gets the cycle cost of the Management canister method [`sign_with_schnorr`](https://internetcomputer.org/docs/references/ic-interface-spec#ic-sign_with_schnorr).
549///
550/// # Errors
551///
552/// This function will return an error if the `key_name` or the `algorithm` is invalid.
553/// The error type [`SignCostError`] provides more information about the reason of the error.
554pub fn cost_sign_with_schnorr<T: AsRef<str>>(
555    key_name: T,
556    algorithm: u32,
557) -> Result<u128, SignCostError> {
558    let key_name = key_name.as_ref();
559    let (dst, code) = ic0::cost_sign_with_schnorr(key_name, algorithm);
560    sign_cost_result(dst, code)
561}
562
563/// Gets the cycle cost of the Management canister method [`vetkd_derive_key`](https://github.com/dfinity/portal/pull/3763).
564///
565/// Later, the description will be available in [the interface spec](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-vetkd_derive_key).
566///
567/// # Errors
568///
569/// This function will return an error if the `key_name` or the `vetkd_curve` is invalid.
570/// The error type [`SignCostError`] provides more information about the reason of the error.
571pub fn cost_vetkd_derive_key<T: AsRef<str>>(
572    key_name: T,
573    vetkd_curve: u32,
574) -> Result<u128, SignCostError> {
575    let key_name = key_name.as_ref();
576    let (cost, code) = ic0::cost_vetkd_derive_key(key_name, vetkd_curve);
577    sign_cost_result(cost, code)
578}
579
580/// Gets the number of environment variables available in the canister.
581pub fn env_var_count() -> usize {
582    ic0::env_var_count()
583}
584
585/// Gets the size of the name of the environment variable at the given index.
586///
587/// # Panics
588///
589/// This function traps if:
590/// - The index is out of bounds (>= than value provided by [`env_var_count`])
591/// - The name is not valid UTF-8.
592pub fn env_var_name(index: usize) -> String {
593    let len = ic0::env_var_name_size(index);
594    let mut buf = vec![0u8; len];
595    ic0::env_var_name_copy(index, &mut buf, 0);
596    String::from_utf8(buf).expect("env_var_name is not valid UTF-8")
597}
598
599/// Checks if the environment variable with the given name exists.
600///
601/// # Panics
602///
603/// This function traps if the length of `name` exceeds `MAX_ENV_VAR_NAME_LENGTH`.
604pub fn env_var_name_exists<T: AsRef<str>>(name: T) -> bool {
605    match ic0::env_var_name_exists(name.as_ref()) {
606        0 => false,
607        1 => true,
608        n => panic!("unexpected return value from env_var_name_exists: {n}"),
609    }
610}
611
612/// Gets the value of the environment variable with the given name.
613///
614/// It's recommended to use [`env_var_name_exists`] to check if the variable exists before calling this function.
615///
616/// # Panics
617///
618/// This function traps if:
619/// - The length of `name` exceeds `MAX_ENV_VAR_NAME_LENGTH`.
620/// - The name does not match any existing environment variable.
621/// - The value is not valid UTF-8.
622pub fn env_var_value<T: AsRef<str>>(name: T) -> String {
623    let name = name.as_ref();
624    let len = ic0::env_var_value_size(name);
625    let mut buf = vec![0u8; len];
626    ic0::env_var_value_copy(name, &mut buf, 0);
627    String::from_utf8(buf).expect("env_var_value is not valid UTF-8")
628}
629
630/// Emits textual trace messages.
631///
632/// On the "real" network, these do not do anything.
633///
634/// When executing in an environment that supports debugging, this copies out the data
635/// and logs, prints or stores it in an environment-appropriate way.
636pub fn debug_print<T: AsRef<str>>(data: T) {
637    let buf = data.as_ref();
638    ic0::debug_print(buf.as_bytes());
639}
640
641/// Traps with the given message.
642///
643/// The environment may copy out the data and log, print or store it in an environment-appropriate way,
644/// or include it in system-generated reject messages where appropriate.
645pub fn trap<T: AsRef<str>>(data: T) -> ! {
646    let buf = data.as_ref();
647    ic0::trap(buf.as_bytes());
648}