Why am I getting [0, 0, 0, 0, 0, 0, 0, 1, 1, 1] as principal?

I need to know the Principal of my canister from a function within it. In a local replica, if it makes a difference (I assume it should work the same locally and in mainnet?).

I’m defining it as

actor class SomeName(_network : Types.Network) = this {

then inside a function within it:

public shared (message) func create_user() : async User {

I’m calling Principal.fromActor(this);

but I get an anonymous principal I guess:

1. Principal

  1. _arr: Uint8Array(10) [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, buffer: ArrayBuffer(10), byteLength: 10, byteOffset: 0, length: 10, Symbol(Symbol.toStringTag): 'Uint8Array']
  2. _isPrincipal: true

How do I make it return the host canister’s Principal?

Why am I getting [0, 0, 0, 0, 0, 0, 0, 1, 1, 1] as principal?

I’m not sure what is going on, but that should work and return the right principal. It may only work inside of a function as it is not set until the initialization is over. Are you running this locally or in moc as a test without the replica?

Locally, with a replica and internet-identity running in parallel.

Have you gotten than 000111 principal under any circumstances?

Actually, Principal.toText(Principal.fromActor(this)); decodes to "rrkah-fqaaa-aaaaa-aaaaq-cai", which is indeed my backend canister id.

Does that mean that size 10 array with 0s and 1s is correct? As in maybe it’s just the first canister deployed in my replica and therefore just [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]? Or are valid principals always larger?

They system canisters just count up from some base number. They are incremental and you’ll notice that when you generate canisters locally you’ll see the same strings show up over and over.

1 Like

Because that’s the [Nat8] representation of one of the first locally deployed canisters in every replica. It’s normal and expected.