Struct ic_cdk::export::Principal[][src]

pub struct Principal(_);
Expand description

A principal describes the security context of an identity, namely any identity that can be authenticated along with a specific role. In the case of the Internet Computer this maps currently to the identities that can be authenticated by a canister. For example, a canister ID is a Principal. So is a user.

Note a principal is not necessarily tied with a public key-pair, yet we need at least a key-pair of a related principal to sign requests.

A Principal can be serialized to a byte array (Vec<u8>) or a text representation, but the inner structure of the byte representation is kept private.

Example of using a Principal object:

use ic_types::Principal;

let text = "aaaaa-aa";  // The management canister ID.
let principal = Principal::from_text(text).expect("Could not decode the principal.");
assert_eq!(principal.as_slice(), &[]);
assert_eq!(principal.to_text(), text);

Serialization is enabled with the “serde” feature. It supports serializing to a byte bufer for non-human readable serializer, and a string version for human readable serializers.

use ic_types::Principal;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Serialize)]
struct Data {
    id: Principal,
}

let id = Principal::from_str("2chl6-4hpzw-vqaaa-aaaaa-c").unwrap();

// JSON is human readable, so this will serialize to a textual
// representation of the Principal.
assert_eq!(
    serde_json::to_string(&Data { id: id.clone() }).unwrap(),
    r#"{"id":"2chl6-4hpzw-vqaaa-aaaaa-c"}"#
);

// CBOR is not human readable, so will serialize to bytes.
assert_eq!(
    serde_cbor::to_vec(&Data { id: id.clone() }).unwrap(),
    &[161, 98, 105, 100, 73, 239, 205, 171, 0, 0, 0, 0, 0, 1],
);

Implementations

impl Principal[src]

pub const fn management_canister() -> Principal[src]

An empty principal that marks the system canister.

pub fn self_authenticating<P>(public_key: P) -> Principal where
    P: AsRef<[u8]>, 
[src]

Right now we are enforcing a Twisted Edwards Curve 25519 point as the public key.

pub const fn anonymous() -> Principal[src]

An anonymous Principal.

pub const fn from_slice(bytes: &[u8]) -> Principal[src]

Attempt to decode a slice into a Principal.

Panics

Panics if the bytes can’t be interpreted.

Examples

const FOO: Principal = Principal::from_slice(&[0; 29]);    // normal length
const MGMT: Principal = Principal::from_slice(&[]);        // management
const OPQ: Principal = Principal::from_slice(&[4,3,2,1]);  // opaque id
const BAR: Principal = Principal::from_slice(&[0; 32]); // Fails, too long
// Fails, ends in 0x04 (anonymous), but has a prefix
const BAZ: Principal = Principal::from_slice(&[1,2,3,4]);

pub const fn try_from_slice(bytes: &[u8]) -> Result<Principal, PrincipalError>[src]

Attempt to decode a slice into a Principal.

pub fn from_text<S>(text: S) -> Result<Principal, PrincipalError> where
    S: AsRef<str>, 
[src]

Parse the text format for canister IDs (e.g., jkies-sibbb-ap6).

The text format follows the public spec (see Textual IDs section).

pub fn to_text(&self) -> String[src]

Returns this Principal’s text representation. The text representation is described in the spec.

pub fn as_slice(&self) -> &[u8][src]

Returns this Principal’s bytes.

Trait Implementations

impl AsRef<[u8]> for Principal[src]

pub fn as_ref(&self) -> &[u8][src]

Performs the conversion.

impl CandidType for Principal[src]

pub fn id() -> TypeId[src]

pub fn _ty() -> Type[src]

pub fn idl_serialize<S>(
    &self,
    serializer: S
) -> Result<(), <S as Serializer>::Error> where
    S: Serializer
[src]

fn ty() -> Type[src]

impl Clone for Principal[src]

pub fn clone(&self) -> Principal[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Principal[src]

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>[src]

Formats the value using the given formatter. Read more

impl<'de> Deserialize<'de> for Principal[src]

pub fn deserialize<D>(
    deserializer: D
) -> Result<Principal, <D as Deserializer<'de>>::Error> where
    D: Deserializer<'de>, 
[src]

Deserialize this value from the given Serde deserializer. Read more

impl Display for Principal[src]

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>[src]

Formats the value using the given formatter. Read more

impl FromStr for Principal[src]

type Err = PrincipalError

The associated error which can be returned from parsing.

pub fn from_str(s: &str) -> Result<Principal, <Principal as FromStr>::Err>[src]

Parses a string s to return a value of this type. Read more

impl Hash for Principal[src]

pub fn hash<__H>(&self, state: &mut __H) where
    __H: Hasher
[src]

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given Hasher. Read more

impl Ord for Principal[src]

pub fn cmp(&self, other: &Principal) -> Ordering[src]

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl PartialEq<Principal> for Principal[src]

pub fn eq(&self, other: &Principal) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

pub fn ne(&self, other: &Principal) -> bool[src]

This method tests for !=.

impl PartialOrd<Principal> for Principal[src]

pub fn partial_cmp(&self, other: &Principal) -> Option<Ordering>[src]

This method returns an ordering between self and other values if one exists. Read more

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Serialize for Principal[src]

pub fn serialize<S>(
    &self,
    serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
    S: Serializer
[src]

Serialize this value into the given Serde serializer. Read more

impl<'_> TryFrom<&'_ [u8]> for Principal[src]

Implement try_from for a generic sized slice.

type Error = PrincipalError

The type returned in the event of a conversion error.

pub fn try_from(
    bytes: &[u8]
) -> Result<Principal, <Principal as TryFrom<&'_ [u8]>>::Error>
[src]

Performs the conversion.

impl<'_> TryFrom<&'_ Vec<u8, Global>> for Principal[src]

type Error = PrincipalError

The type returned in the event of a conversion error.

pub fn try_from(
    bytes: &Vec<u8, Global>
) -> Result<Principal, <Principal as TryFrom<&'_ Vec<u8, Global>>>::Error>
[src]

Performs the conversion.

impl<'_> TryFrom<&'_ str> for Principal[src]

type Error = PrincipalError

The type returned in the event of a conversion error.

pub fn try_from(
    s: &str
) -> Result<Principal, <Principal as TryFrom<&'_ str>>::Error>
[src]

Performs the conversion.

impl TryFrom<Vec<u8, Global>> for Principal[src]

Vector TryFrom. The slice and array version of this trait are defined below.

type Error = PrincipalError

The type returned in the event of a conversion error.

pub fn try_from(
    bytes: Vec<u8, Global>
) -> Result<Principal, <Principal as TryFrom<Vec<u8, Global>>>::Error>
[src]

Performs the conversion.

impl Eq for Principal[src]

impl StructuralEq for Principal[src]

impl StructuralPartialEq for Principal[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToHex for T where
    T: AsRef<[u8]>, 
[src]

pub fn encode_hex<U>(&self) -> U where
    U: FromIterator<char>, 
[src]

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca) Read more

pub fn encode_hex_upper<U>(&self) -> U where
    U: FromIterator<char>, 
[src]

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA) Read more

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]