Struct ic_types::principal::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]
impl Principal
[src]pub const fn management_canister() -> Self
[src]
pub const fn management_canister() -> Self
[src]An empty principal that marks the system canister.
pub fn self_authenticating<P: AsRef<[u8]>>(public_key: P) -> 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 const fn from_slice(bytes: &[u8]) -> Self
[src]
pub const fn from_slice(bytes: &[u8]) -> Self
[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<Self, PrincipalError>
[src]
pub const fn try_from_slice(bytes: &[u8]) -> Result<Self, PrincipalError>
[src]Attempt to decode a slice into a Principal.
pub fn from_text<S: AsRef<str>>(text: S) -> Result<Self, PrincipalError>
[src]
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).
Trait Implementations
impl<'de> Deserialize<'de> for Principal
[src]
impl<'de> Deserialize<'de> for Principal
[src]fn deserialize<D: Deserializer<'de>>(
deserializer: D
) -> Result<Principal, D::Error>
[src]
fn deserialize<D: Deserializer<'de>>(
deserializer: D
) -> Result<Principal, D::Error>
[src]Deserialize this value from the given Serde deserializer. Read more
impl Ord for Principal
[src]
impl Ord for Principal
[src]impl PartialOrd<Principal> for Principal
[src]
impl PartialOrd<Principal> for Principal
[src]fn partial_cmp(&self, other: &Principal) -> Option<Ordering>
[src]
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]
#[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]
#[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
impl TryFrom<Vec<u8, Global>> for Principal
[src]
impl TryFrom<Vec<u8, Global>> for Principal
[src]Vector TryFrom. The slice and array version of this trait are defined below.
impl Eq for Principal
[src]
impl StructuralEq for Principal
[src]
impl StructuralPartialEq for Principal
[src]
Auto Trait Implementations
impl RefUnwindSafe for Principal
impl Send for Principal
impl Sync for Principal
impl Unpin for Principal
impl UnwindSafe for Principal
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self
impl<T> ToHex for T where
T: AsRef<[u8]>,
[src]
impl<T> ToHex for T where
T: AsRef<[u8]>,
[src]pub fn encode_hex<U>(&self) -> U where
U: FromIterator<char>,
[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]
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]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
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]
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> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,