[−][src]Struct ic_cdk::export::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() -> Principal[src]
pub fn self_authenticating<P>(public_key: P) -> Principal where
P: AsRef<[u8]>, [src]
P: AsRef<[u8]>,
Right now we are enforcing a Twisted Edwards Curve 25519 point as the public key.
pub fn anonymous() -> Principal[src]
An anonymous Principal.
pub fn from_text<S>(text: S) -> Result<Principal, PrincipalError> where
S: AsRef<str>, [src]
S: AsRef<str>,
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 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]
&self,
serializer: S
) -> Result<(), <S as Serializer>::Error> where
S: Serializer,
pub fn ty() -> Type[src]
impl Clone for Principal[src]
impl Debug for Principal[src]
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]
deserializer: D
) -> Result<Principal, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
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.
pub fn from_str(s: &str) -> Result<Principal, <Principal as FromStr>::Err>[src]
impl Hash for Principal[src]
pub fn hash<__H>(&self, state: &mut __H) where
__H: Hasher, [src]
__H: Hasher,
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
impl Ord for Principal[src]
pub fn cmp(&self, other: &Principal) -> Ordering[src]
#[must_use]pub fn max(self, other: Self) -> Self1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self1.50.0[src]
impl PartialEq<Principal> for Principal[src]
impl PartialOrd<Principal> for Principal[src]
pub fn partial_cmp(&self, other: &Principal) -> Option<Ordering>[src]
pub fn lt(&self, other: &Principal) -> bool[src]
pub fn le(&self, other: &Principal) -> bool[src]
pub fn gt(&self, other: &Principal) -> bool[src]
pub fn ge(&self, other: &Principal) -> bool[src]
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]
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
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.
pub fn try_from(
bytes: &[u8]
) -> Result<Principal, <Principal as TryFrom<&'_ [u8]>>::Error>[src]
bytes: &[u8]
) -> Result<Principal, <Principal as TryFrom<&'_ [u8]>>::Error>
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]
bytes: &Vec<u8, Global>
) -> Result<Principal, <Principal as TryFrom<&'_ Vec<u8, Global>>>::Error>
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]
s: &str
) -> Result<Principal, <Principal as TryFrom<&'_ str>>::Error>
impl TryFrom<Vec<u8, Global>> for Principal[src]
Vector TryFrom. The slice and array version of this trait are defined below.
Auto Trait Implementations
impl RefUnwindSafe for Principal[src]
impl Send for Principal[src]
impl Sync for Principal[src]
impl Unpin for Principal[src]
impl UnwindSafe for Principal[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>, [src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToHex for T where
T: AsRef<[u8]>, [src]
T: AsRef<[u8]>,
pub fn encode_hex<U>(&self) -> U where
U: FromIterator<char>, [src]
U: FromIterator<char>,
pub fn encode_hex_upper<U>(&self) -> U where
U: FromIterator<char>, [src]
U: FromIterator<char>,
impl<T> ToOwned for T where
T: Clone, [src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn clone_into(&self, target: &mut T)[src]
impl<T> ToString for T where
T: Display + ?Sized, [src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
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]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,