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_platform::Principal;
let text = "aaaaa-aa"; // The management canister ID.
let principal = Principal::from_text(text).expect("Could not decode the principal.");
assert!(principal.as_slice().is_empty());
assert_eq!(principal.to_text(), text);
Serialization is always available. It supports serializing to a byte buffer for non-human readable serializer, and a string version for human readable serializers.
Implementations§
Source§impl Principal
impl Principal
Sourcepub const MAX_LENGTH_IN_BYTES: usize = 29
pub const MAX_LENGTH_IN_BYTES: usize = 29
Maximum significant identity length.
Sourcepub const MAX_LENGTH_IN_TEXT: usize = 63
pub const MAX_LENGTH_IN_TEXT: usize = 63
Maximum canonical text length, including dashes and checksum.
Sourcepub const fn management_canister() -> Principal
pub const fn management_canister() -> Principal
Construct a Principal of the IC management canister
Sourcepub fn self_authenticating<P>(public_key: P) -> Principal
pub fn self_authenticating<P>(public_key: P) -> Principal
Construct a self-authenticating ID from public key
Sourcepub const fn from_slice(slice: &[u8]) -> Principal
pub const fn from_slice(slice: &[u8]) -> Principal
Sourcepub const fn try_from_slice(slice: &[u8]) -> Result<Principal, PrincipalError>
pub const fn try_from_slice(slice: &[u8]) -> Result<Principal, PrincipalError>
Construct a Principal from a slice of bytes.
Sourcepub fn from_text<S>(text: S) -> Result<Principal, PrincipalError>
pub fn from_text<S>(text: S) -> Result<Principal, PrincipalError>
Parse a Principal from text representation.
Sourcepub const fn as_slice(&self) -> &[u8] ⓘ
pub const fn as_slice(&self) -> &[u8] ⓘ
Returns the Principal’s significant bytes as a slice: the first
len bytes of the backing array, without the trailing
zero padding.
Use as_fixed_bytes to get the full,
fixed-size backing array instead.
Sourcepub const fn len(&self) -> u8
pub const fn len(&self) -> u8
Returns the number of significant bytes in the Principal, between
0 and MAX_LENGTH_IN_BYTES inclusive.
This equals the length of the slice returned by
as_slice.
Sourcepub const fn as_fixed_bytes(&self) -> &[u8; 29]
pub const fn as_fixed_bytes(&self) -> &[u8; 29]
Returns a reference to the Principal’s fixed-size backing array.
The array is always MAX_LENGTH_IN_BYTES
long; bytes from index len onwards are always zero.
Prefer as_slice for the significant bytes without
padding.