Cyclic Redundancy Checks


This package can be useful in situations where a canister needs to get its own identifier.

import Array "mo:base/Array";
import CRC8 "../vendor/crc/src/CRC8";
import Hex "../vendor/hex/src/Hex";
import Iter "mo:base/Iter";
import Prim "mo:prim";

actor {

  public func proxy() : async Text {
    await whoami();

  public shared {
    caller = caller;
  } func whoami() : async Text {
    let base = Iter.toArray<Word8>(Prim.blobOfPrincipal(caller).bytes());
    let crc8 = CRC8.crc8(base);
    Hex.encode(Array.append<Word8>(base, [crc8]));

You can return caller directly as Principal. When displaying the principal in text, the system will append CRC8 automatically.

public shared { caller = caller } func whoami() : async Principal { caller }

This works in JavaScript. dfx cannot decode Principal properly but will support it soon.


Ah, you mean if I simply want to print it to the command-line, but I don’t want to do that. I need to pass this result to another canister that will instantiate an actor from it. Would be cool if we had Prim.principalToText that performs the hex encoding and tacks on the CRC.


Ah, that’s even simpler then.

import P "mo:base/Principal";
actor Self {
    public func greet() : async Principal {
1 Like

Ah, yes! That is super helpful! I also want to go the other way around though.

let principal = .. : Principal;
// Don't I need to convert the principal to text here?
let counter = actor principal : actor {
  get() : async Nat;
count = await counter.get();

are you building a crypto lib?


1 Like

very interesting, is this something you can easily build out of the box with motoko on the icp? i‘m especially thinking of the „distributed“ part of this, i‘m guessing you won‘t manually decide on which replica to store what?

1 Like

Yeah exactly. The implementation is pretty bare bones at the moment, but yes, you can create several instances of this canister and register them with one another. The maximum amount of memory a canister can consume is 4GB according to the WASM spec. With the DHT, you should be able to store an unbounded amount data since you can just keep adding more canisters.


is there an easy out of the box way to get the amount of memory consumed by the canister already or would need to implement some sort of counter yourself?

1 Like

Great question! I know there is a WASM opcode called current_memory that returns the current memory size in units of pages, where the page size is 64kB, but I’m not sure if there is a Motoko function to return it. Maybe @claudio knows?


You can use Prim.rts_memory_size()