Skip to main content

Principal

Module for interacting with Principals (users and canisters).

Principals are used to identify entities that can interact with the Internet Computer. These entities are either users or canisters.

Example textual representation of Principals:

un4fu-tqaaa-aaaab-qadjq-cai

In Motoko, there is a primitive Principal type called Principal. As an example of where you might see Principals, you can access the Principal of the caller of your shared function.

shared(msg) func foo() {
let caller : Principal = msg.caller;
};

Then, you can use this module to work with the Principal.

Import from the base library to use this module.

import Principal "mo:base/Principal";

Type Principal

type Principal = Prim.Types.Principal

Function fromActor

func fromActor(a : actor {  }) : Principal

Get the Principal identifier of an actor.

Example:

actor MyCanister {
func getPrincipal() : Principal {
let principal = Principal.fromActor(MyCanister);
}
}

Function toBlob

func toBlob(p : Principal) : Blob

Convert a Principal to its Blob (bytes) representation.

Example:

let principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let blob = Principal.toBlob(principal); // => \00\00\00\00\00\30\00\D3\01\01

Function fromBlob

func fromBlob(b : Blob) : Principal

Converts a Blob (bytes) representation of a Principal to a Principal value.

Example:

let blob = "\00\00\00\00\00\30\00\D3\01\01" : Blob;
let principal = Principal.fromBlob(blob);
Principal.toText(principal) // => "un4fu-tqaaa-aaaab-qadjq-cai"

Function toText

func toText(p : Principal) : Text

Converts a Principal to its Text representation.

Example:

let principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
Principal.toText(principal) // => "un4fu-tqaaa-aaaab-qadjq-cai"

Function fromText

func fromText(t : Text) : Principal

Converts a Text representation of a Principal to a Principal value.

Example:

let principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
Principal.toText(principal) // => "un4fu-tqaaa-aaaab-qadjq-cai"

Function isAnonymous

func isAnonymous(p : Principal) : Bool

Checks if the given principal represents an anonymous user.

Example:

let principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
Principal.isAnonymous(principal) // => false

Function hash

func hash(principal : Principal) : Hash.Hash

Hashes the given principal by hashing its Blob representation.

Example:

let principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
Principal.hash(principal) // => 2_742_573_646

Function compare

func compare(principal1 : Principal, principal2 : Principal) : {#less; #equal; #greater}

General purpose comparison function for Principal. Returns the Order ( either #less, #equal, or #greater) of comparing principal1 with principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
Principal.compare(principal1, principal2) // => #equal

Function equal

func equal(principal1 : Principal, principal2 : Principal) : Bool

Equality function for Principal types. This is equivalent to principal1 == principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
ignore Principal.equal(principal1, principal2);
principal1 == principal2 // => true

Note: The reason why this function is defined in this library (in addition to the existing == operator) is so that you can use it as a function value to pass to a higher order function. It is not possible to use == as a function value at the moment.

Example:

import Buffer "mo:base/Buffer";

let buffer1 = Buffer.Buffer<Principal>(3);
let buffer2 = Buffer.Buffer<Principal>(3);
Buffer.equal(buffer1, buffer2, Principal.equal) // => true

Function notEqual

func notEqual(principal1 : Principal, principal2 : Principal) : Bool

Inequality function for Principal types. This is equivalent to principal1 != principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
ignore Principal.notEqual(principal1, principal2);
principal1 != principal2 // => false

Note: The reason why this function is defined in this library (in addition to the existing != operator) is so that you can use it as a function value to pass to a higher order function. It is not possible to use != as a function value at the moment.

Function less

func less(principal1 : Principal, principal2 : Principal) : Bool

"Less than" function for Principal types. This is equivalent to principal1 < principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
ignore Principal.less(principal1, principal2);
principal1 < principal2 // => false

Note: The reason why this function is defined in this library (in addition to the existing < operator) is so that you can use it as a function value to pass to a higher order function. It is not possible to use < as a function value at the moment.

Function lessOrEqual

func lessOrEqual(principal1 : Principal, principal2 : Principal) : Bool

"Less than or equal to" function for Principal types. This is equivalent to principal1 <= principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
ignore Principal.lessOrEqual(principal1, principal2);
principal1 <= principal2 // => true

Note: The reason why this function is defined in this library (in addition to the existing <= operator) is so that you can use it as a function value to pass to a higher order function. It is not possible to use <= as a function value at the moment.

Function greater

func greater(principal1 : Principal, principal2 : Principal) : Bool

"Greater than" function for Principal types. This is equivalent to principal1 > principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
ignore Principal.greater(principal1, principal2);
principal1 > principal2 // => false

Note: The reason why this function is defined in this library (in addition to the existing > operator) is so that you can use it as a function value to pass to a higher order function. It is not possible to use > as a function value at the moment.

Function greaterOrEqual

func greaterOrEqual(principal1 : Principal, principal2 : Principal) : Bool

"Greater than or equal to" function for Principal types. This is equivalent to principal1 >= principal2.

Example:

let principal1 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
ignore Principal.greaterOrEqual(principal1, principal2);
principal1 >= principal2 // => true

Note: The reason why this function is defined in this library (in addition to the existing >= operator) is so that you can use it as a function value to pass to a higher order function. It is not possible to use >= as a function value at the moment.