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§

§

impl Principal

pub const MAX_LENGTH_IN_BYTES: usize = 29usize

pub const fn management_canister() -> Principal

Construct a Principal of the IC management canister

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

Construct a self-authenticating ID from public key

pub const fn anonymous() -> Principal

Construct an anonymous ID.

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.

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

Construct a Principal from a slice of bytes.

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

Parse a Principal from text representation.

pub fn to_text(&self) -> String

Convert Principal to text representation.

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

Return the Principal’s underlying slice of bytes.

Trait Implementations§

§

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

§

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
§

impl AsRef<[u8]> for Principal

§

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 as Serializer>::Error>
where S: Serializer,

source§

fn ty() -> Type

source§

fn id() -> TypeId

§

impl Clone for Principal

§

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
§

impl Debug for Principal

§

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

Formats the value using the given formatter. Read more
§

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

§

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
§

impl Display for Principal

§

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

Formats the value using the given formatter. Read more
§

impl FromStr for Principal

§

type Err = PrincipalError

The associated error which can be returned from parsing.
§

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

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

impl Hash for Principal

§

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
§

impl Ord for Principal

§

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
§

impl PartialEq for Principal

§

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.
§

impl PartialOrd for Principal

§

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
§

impl Serialize for Principal

§

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
§

impl TryFrom<&[u8]> for Principal

§

type Error = PrincipalError

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

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

Performs the conversion.
§

impl TryFrom<&Vec<u8>> for Principal

§

type Error = PrincipalError

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

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

Performs the conversion.
§

impl TryFrom<&str> for Principal

§

type Error = PrincipalError

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

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

Performs the conversion.
§

impl TryFrom<Vec<u8>> for Principal

§

type Error = PrincipalError

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

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

Performs the conversion.
§

impl Copy for Principal

§

impl Eq for Principal

§

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> Fake for T

source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromDhall for T

source§

fn from_dhall(v: &Value) -> Result<T, Error>

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> ToDhall for T
where T: Serialize,

source§

fn to_dhall(&self, ty: Option<&SimpleType>) -> Result<Value, Error>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

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

source§

impl<T> RuleType for T
where T: Copy + Debug + Eq + Hash + Ord,