Struct candid::Principal

source ·
pub struct Principal { /* private fields */ }
Expand description

Generic ID on Internet Computer.

Principals are generic identifiers for canisters, users and possibly other concepts in the future. As far as most uses of the IC are concerned they are opaque binary blobs with a length between 0 and 29 bytes, and there is intentionally no mechanism to tell canister ids and user ids apart.

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_principal::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_principal::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§

source§

impl Principal

source

pub const MAX_LENGTH_IN_BYTES: usize = 29usize

source

pub const fn management_canister() -> Principal

Construct a Principal of the IC management canister

source

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

Construct a self-authenticating ID from public key

source

pub const fn anonymous() -> Principal

Construct an anonymous ID.

source

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

Construct a Principal from a slice of bytes.

§Panics

Panics if the slice is longer than 29 bytes.

source

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

Construct a Principal from a slice of bytes.

source

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

Parse a Principal from text representation.

source

pub fn to_text(&self) -> String

Convert Principal to text representation.

source

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

Return the Principal’s underlying slice of bytes.

Trait Implementations§

source§

impl<'a> Arbitrary<'a> for Principal

source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Principal, Error>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

impl AsRef<[u8]> for Principal

source§

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

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl CandidType for Principal

source§

fn _ty() -> Type

source§

fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where S: Serializer,

source§

fn ty() -> Type

source§

fn id() -> TypeId

source§

impl Clone for Principal

source§

fn clone(&self) -> Principal

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Principal

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Principal

source§

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

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Principal

source§

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

Formats the value using the given formatter. Read more
source§

impl FromStr for Principal

§

type Err = PrincipalError

The associated error which can be returned from parsing.
source§

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

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

impl Hash for Principal

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

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

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

impl Ord for Principal

source§

fn cmp(&self, other: &Principal) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Principal

source§

fn eq(&self, other: &Principal) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Principal

source§

fn partial_cmp(&self, other: &Principal) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl Serialize for Principal

source§

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

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<&[u8]> for Principal

§

type Error = PrincipalError

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl TryFrom<&Vec<u8>> for Principal

§

type Error = PrincipalError

The type returned in the event of a conversion error.
source§

fn try_from( bytes: &Vec<u8> ) -> Result<Principal, <Principal as TryFrom<&Vec<u8>>>::Error>

Performs the conversion.
source§

impl TryFrom<&str> for Principal

§

type Error = PrincipalError

The type returned in the event of a conversion error.
source§

fn try_from(s: &str) -> Result<Principal, <Principal as TryFrom<&str>>::Error>

Performs the conversion.
source§

impl TryFrom<Vec<u8>> for Principal

§

type Error = PrincipalError

The type returned in the event of a conversion error.
source§

fn try_from( bytes: Vec<u8> ) -> Result<Principal, <Principal as TryFrom<Vec<u8>>>::Error>

Performs the conversion.
source§

impl Copy for Principal

source§

impl Eq for Principal

source§

impl StructuralPartialEq for Principal

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

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

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

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

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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