# Encode Principal to Candid arguments

**URL:** <https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465>\
**Category:** Developers\
**Created:** [January 24, 2022, 7:57pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465 "2022-01-24T19:57:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 7:57pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/1 "2022-01-24T19:57:55Z")

</div>

I have got a manager canister that creates user canisters. These user are actor that takes a principal as argument.

```auto
actor class UserBucket(owner: Principal) = this {
   ...

```

So far, so good.

Now, through the manager I am trying to implement a function that can update the code of these user canisters.

```auto
public func installCode(canisterId: Principal, owner: Principal, wasmModule: Blob): async() {
            await IC.install_code({
                arg = Blob.toArray(Principal.toBlob(owner));
                wasm_module = wasmModule;
                mode = #upgrade;
                canister_id = canisterId;
            });
        };

```

After double checking that all parameters are effectively correctly set I am facing following error when I try to update the code:

> Canister a3ukg-3yaaa-aaaai-aa6ta-cai trapped explicitly: IDL error: missing magic bytes

If I get it correctly, it means that my `arg`, the owner principal currently converted to `[Nat8]`, is not correctly encoded as valid Candid arguments.

Do I get it right?

How do I encode the principal as valid candid arguments?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 24, 2022, 8:31pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/2 "2022-01-24T20:31:01Z")

</div>

You can import actor class directly, see [examples/Map.mo at master · dfinity/examples · GitHub](https://github.com/dfinity/examples/blob/master/motoko/classes/src/map/Map.mo)

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 8:36pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/3 "2022-01-24T20:36:03Z")

</div>

Mmmh not sure to understand how is it related? The actor class works fine, I try to upgrade the WASM code of existing canisters that have been generated on the fly. I try to use the [install\_code](https://github.com/dfinity/motoko-playground/blob/3cf294725acf2213ca5e7c01bc59115781ab8ac4/service/pool/IC.mo#L31) feature (as in the motoko playground).

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 24, 2022, 8:41pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/4 "2022-01-24T20:41:48Z")

</div>

Ah, so the wasm module is not generated from the actor class? In that case, maybe you can pass arg as an argument as well, and use `didc` to encode the principal to a candid blob.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 8:45pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/5 "2022-01-24T20:45:58Z")

</div>

> [@chenyan](#):
>
> use `didc` to encode the principal to a candid blob

yeah I think it’s what I need but I don’t know how?

how to “encode a principal to a candid blob”?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 24, 2022, 9:00pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/6 "2022-01-24T21:00:01Z")

</div>

From CLI, you can run `didc encode '(principal "aaaaa-aa")' -f blob`

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 9:02pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/7 "2022-01-24T21:02:30Z")

</div>

I am writing a script in JavaScript (sorry should have had mentioned that earlier) and the backend is in Motoko.

Found something in the playground and candid, so trying the following:

```auto
import { IDL } from "@dfinity/candid";

const arg = IDL.encode([IDL.Principal], owner);

```

but I get an error

> Cannot mix BigInt and other types, use explicit conversions

`owner` is a principal

```auto
console.log(JSON.stringify(owner))

// {"_arr":{"0":154,"1":1 .... "28":2},"_isPrincipal":true}

```

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 24, 2022, 9:07pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/8 "2022-01-24T21:07:53Z")

</div>

> [@peterparker](#):
>
> `const arg = IDL.encode([IDL.Principal], owner);`

The second argument is an array as well, `[owner]`

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 9:19pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/9 "2022-01-24T21:19:11Z")

</div>

Nice 👍

```auto
const arg = IDL.encode([IDL.Principal], [owner]);
// -> Uint8Array(38) [68, 73, 68, 76....65, 2]

```

Now I should check how I can send it to my canister. I used a `Blob` but it does not seem happy with it.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 9:22pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/10 "2022-01-24T21:22:33Z")

</div>

Shit, I think it may have worked out!!!

My manager access point

```auto
public shared({ caller }) func installCode(canisterId: Principal, owner: Blob, wasmModule: Blob): async() {
        await canisterUtils.installCode(canisterId, owner, wasmModule);
    };

```

My utils

```auto
public func installCode(canisterId: Principal, owner: Blob, wasmModule: Blob): async() {
            await ic.install_code({
                arg = owner;
                wasm_module = wasmModule;
                mode = #upgrade;
                canister_id = canisterId;
            });
        };

```

The js call

```auto
const arg = IDL.encode([IDL.Principal], [owner]);
await actor.installCode(bucketId, [...arg], [...new Uint8Array(buffer)]);

```

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 24, 2022, 9:32pm UTC](https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465/11 "2022-01-24T21:32:12Z")

</div>

Gosh it indeed works out!

I just added a “hello world” function in the WASM and used above function to update the user canister code 👉[ic.rocks](https://ic.rocks/principal/a3ukg-3yaaa-aaaai-aa6ta-cai) now displays the new function 🥳

Thanks for the help @chenyan
