[][src]Struct ic_types::principal::Principal

pub struct Principal(_);

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
// main.rsrepresentation 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 fn management_canister() -> Self[src]

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

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

pub fn anonymous() -> Self[src]

An anonymous Principal.

pub fn from_text<S: AsRef<str>>(text: S) -> Result<Self, PrincipalError>[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]

impl Clone for Principal[src]

impl Debug for Principal[src]

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

impl Display for Principal[src]

impl Eq for Principal[src]

impl FromStr for Principal[src]

type Err = PrincipalError

The associated error which can be returned from parsing.

impl Hash for Principal[src]

impl Ord for Principal[src]

impl PartialEq<Principal> for Principal[src]

impl PartialOrd<Principal> for Principal[src]

impl Serialize for Principal[src]

impl StructuralEq for Principal[src]

impl StructuralPartialEq for Principal[src]

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.

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

type Error = PrincipalError

The type returned in the event of a conversion error.

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

type Error = PrincipalError

The type returned in the event of a conversion error.

impl TryFrom<Vec<u8>> 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.

Auto Trait Implementations

Blanket Implementations

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

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

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

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

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

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

impl<T> Same<T> for T

type Output = T

Should always be Self

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

type Owned = T

The resulting type after obtaining ownership.

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

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.

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.