Principal

To understand this principal module, it might be helpful to learn about Principles first.

The convention is to name the module alias after the file name it is defined in:

import Principal "mo:base/Principal";

Conversion

Function fromActor
Function fromText
Function toText
Function toBlob
Function fromBlob

Utility

Function isAnonymous
Function hash

Comparison

Function equal
Function notEqual
Function less
Function lessOrEqual
Function greater
Function greaterOrEqual
Function compare

Principal.fromActor

func fromActor(a : actor {  }) : Principal

The function fromActor takes one actor value and returns a Principal value.

import Principal "mo:base/Principal";

type Account = { owner : Principal; subaccount : ?Blob };

type Ledger = actor {
  icrc1_balance_of : shared query Account -> async Nat;
};

let ledger = actor ("ryjl3-tyaaa-aaaaa-aaaba-cai") : Ledger;

Principal.fromActor(ledger);

Principal.fromText

func fromText(t : Text) : Principal

The function fromText takes one Text value and returns a Principal value.

import Principal "mo:base/Principal";

let textualPrincipal : Text = "un4fu-tqaaa-aaaab-qadjq-cai";

Principal.fromText(textualPrincipal);

Principal.toText

func toText(p : Principal) : Text

The function toText takes one Principal value and returns a Text value.

import Principal "mo:base/Principal";

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

Principal.toText(principal);

Principal.toBlob

func toBlob(p : Principal) : Blob

The function toBlob takes one Principal value and returns a Blob value.

import Principal "mo:base/Principal";

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

Principal.toBlob(principal);

Principal.fromBlob

func fromBlob(b : Blob) : Principal

The function fromBlob takes one BLob value and returns a Principal value.

import Principal "mo:base/Principal";

let principal : Principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
let blob : Blob = Principal.toBlob(principal);

Principal.fromBlob(blob);

Principal.isAnonymous

func isAnonymous(p : Principal) : Bool

The function isAnonymous takes one Principal value and returns a Bool value.

import Principal "mo:base/Principal";

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

Principal.isAnonymous(principal);

Principal.hash

func hash(principal : Principal) : Hash.Hash

The function hash takes one Principal value and returns a Hash value.

import Principal "mo:base/Principal";

let principal = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.hash(principal);

Principal.equal

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

The function equal takes two Principal value and returns a Bool value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");
let principal2 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.equal(principal1, principal2);

Principal.notEqual

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

The function notEqual takes two Principal value and returns a Bool value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");
let principal2 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.notEqual(principal1, principal2);

Principal.less

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

The function less takes two Principal value and returns a Bool value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");
let principal2 = Principal.fromText("jr7pa-rnspg-drbf2-ooaap-gmfep-773oe-z5oua-hiycn-q3sjg-ffwie-tae");

Principal.less(principal1, principal2);

Principal.lessOrEqual

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

The function lessOrEqual takes two Principal value and returns a Bool value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");
let principal2 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.lessOrEqual(principal1, principal2);

Principal.greater

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

The function greater takes two Principal value and returns a Bool value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");
let principal2 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.greater(principal1, principal2);

Principal.greaterOrEqual

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

The function greaterOrEqual takes two Principal value and returns a Bool value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("2vxsx-fae");
let principal2 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.greaterOrEqual(principal1, principal2);

Principal.compare

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

The function compare takes two Principal value and returns an Order value.

import Principal "mo:base/Principal";

let principal1 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");
let principal2 = Principal.fromText("m7sm4-2iaaa-aaaab-qabra-cai");

Principal.compare(principal1, principal2);