# Problem with Blob vs. \[Nat8\] from candid interface in Motoko

**URL:** <https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037>\
**Category:** Motoko\
**Tags:** candid\
**Created:** [June 16, 2024, 5:57pm UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037 "2024-06-16T17:57:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![f0i](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/f0i/32/2327_2.png) [@f0i](https://forum.dfinity.org/u/f0i)\
**Post date:** [June 16, 2024, 5:57pm UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/1 "2024-06-16T17:57:02Z")

</div>

I’ve been trying to setup a local ledger canister for testing following the [docs “ICP ledger local setup”](https://internetcomputer.org/docs/current/developer-docs/defi/icp-tokens/ledger-local-setup/).

However I ran into the problem that candid doesn’t have a dedicated type for Blobs and just uses [Nat8] instead. There is a thread about this here: [Imported Motoko canister has wrong type, [Nat8] instead of Blob](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312).

By converting between Blob and [Nat8], I could make the backend compile (`dfx build backend`), but it still fails when I try to build the frontend which depends on the backend.

I created a repository to show the error and added the full error messages to the README there.

> **[GitHub - f0i/motoko-blob-nat8](https://github.com/f0i/motoko-blob-nat8)**
>
> Contribute to f0i/motoko-blob-nat8 development by creating an account on GitHub.

Is there a way to fix this without using the [“gross”, “hacky workaround” of casting to another actor type](https://forum.dfinity.org/t/imported-motoko-canister-has-wrong-type-nat8-instead-of-blob/21312/4)?

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [June 16, 2024, 10:49pm UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/2 "2024-06-16T22:49:00Z")

</div>

Typically blob and [Nat8] kind of “just work” in motoko public shared endpoints. I noticed in your week that it is saying that ?subaccount isn’t [Nat8] which means you are missing an opt somewhere. If this is in typescript, perhaps you need an array around your UInt8array? Arrays of one element are used to simulate opt such that subaccount: is null and [[1,1,…2,2]] is an opt blob/ opt [nat8]

---

<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:** [June 16, 2024, 10:58pm UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/3 "2024-06-16T22:58:19Z")

</div>

What error do you get when building the frontend? As you mentioned, the backend can be fixed by replacing `[Nat8]` to `Blob` type.

```auto
@@ -8,7 +8,7 @@
- type Subaccount = [Nat8];
+ type Subaccount = Blob;
@@ -74,8 +74,8 @@
- //return Blob.fromArray(Buffer.toArray(buffer));
- return Buffer.toArray(buffer);
+ return Blob.fromArray(Buffer.toArray(buffer));
+ //return Buffer.toArray(buffer);
@@ -100,12 +100,12 @@
- memo : ?[Nat8];
+ memo : ?Blob;
- //from_subaccount = sender.subaccount;
- from_subaccount = Blob.fromArray(sender.subaccount);
+ from_subaccount = sender.subaccount;
+ //from_subaccount = Blob.fromArray(sender.subaccount);

```

After this patch, I see this error while building the frontend, which doesn’t seem to be related to the backend code?

```auto
src/App.tsx:15:36 - error TS2304: Cannot find name 'loadAuctions'.

15 <button onClick={loadAuctions} className="">
                                      ~~~~~~~~~~~~

Found 1 error in src/App.tsx:15

```

---

<div class="post-metadata">

**Author:** ![f0i](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/f0i/32/2327_2.png) [@f0i](https://forum.dfinity.org/u/f0i)\
**Post date:** [June 17, 2024, 10:32am UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/4 "2024-06-17T10:32:54Z")

</div>

There have been some errors from trying to simplify the issue from the original project. I fixed the frontend code, and the missing opt.

However, I still get the same error. Calling `dfx build backend` succeeds, but `dfx build frontend`, which depends on the backend,shows an error inside the Motoko file `wallet.mo`

```auto
/workspaces/motoko-blob-nat8/src/backend/wallet.mo:83.35-83.42: type error [M0096], expression of type
  {owner : Principal; subaccount : ?[Nat8]}
cannot produce expected type
  {owner : Principal; subaccount : ?Subaccount}

```

How can this file be ok when building the backend, but not when building the frontend?

#### Here are some more things I tried

If I change to Blob, I get the same error for both `dfx build backend` and `dfx build frontend`.

```auto
/workspaces/motoko-blob-nat8/src/backend/wallet.mo:83.35-83.42: type error [M0096], expression of type
  {owner : Principal; subaccount : ?Blob}
cannot produce expected type
  {owner : Principal; subaccount : ?Subaccount}

```

* * *

When I removed the subaccount and just pass null, both backend and frontend can be build.  
But when I add a subaccount I get the above error. Here is the [diff between the version wihtout and with subaccounts](https://github.com/f0i/motoko-blob-nat8/commit/1d0039a64fb8d41dd9b83732d286fed8c871a7b0).

* * *

I also tried to building inside code spaces to avoid any issues from cached artifacts of previous builds: [https://youtu.be/\_zFwSqXgGjs?t=140](https://youtu.be/_zFwSqXgGjs?t=140)

---

<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:** [June 17, 2024, 3:54pm UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/5 "2024-06-17T15:54:32Z")

</div>

Everything works fine on my side when changing `[Nat8]` to `Blob`:

```auto
diff --git a/src/backend/wallet.mo b/src/backend/wallet.mo
index b49b8c4..92ce91b 100644
--- a/src/backend/wallet.mo
+++ b/src/backend/wallet.mo
@@ -8,15 +8,15 @@ import Ledger "canister:ckusdc_ledger";

 module {

- type Subaccount = [Nat8];
- public type Account = { owner : Principal; subaccount : ?[Nat8] };
+ type Subaccount = Blob;
+ public type Account = { owner : Principal; subaccount : ?Blob };
   public type TokenInfo = { name : Text; decimals : Nat8; fee : Nat };
   public type AccountBlob = { owner : Principal; subaccount : ?Blob };

   public func accountForUser(self : actor {}, user : Principal) : Account {
     let account = {
       owner = Principal.fromActor(self);
- subaccount : ?[Nat8] = ?principalToSubaccount(user);
+ subaccount = ?principalToSubaccount(user);
     };
     return account;
   };
@@ -74,7 +74,7 @@ module {
       buffer.add(0);
     };
     assert (buffer.size() == 32);
- return Buffer.toArray(buffer);
+ return Blob.fromArray(Buffer.toArray(buffer));
     //return Buffer.toArray(buffer);
   };

```

---

<div class="post-metadata">

**Author:** ![f0i](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/f0i/32/2327_2.png) [@f0i](https://forum.dfinity.org/u/f0i)\
**Post date:** [June 18, 2024, 10:54am UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/6 "2024-06-18T10:54:13Z")

</div>

Thank you for spending the time to look into this. I wonder if there is some difference in the environment or commands you used to build the project.  
I tried again with the patch in [Github Codespaces](https://github.com/codespaces) which provides a clean Debian installation. Still get the same error.

$ dfx --version  
dfx 0.20.1  
$ moc-wrapper --version  
Motoko compiler 0.11.1 (source xqq6zkb5-mhafjk7m-n1samsmj-l4gq1143)  
$ mops --version  
CLI 0.44.1  
API 1.2

Here is another video with the patch applied, from creating the codespace until getting the error (Link is to 3:15 where the actual build command starts): [https://youtu.be/-2ZvrNMOPyA?t=195](https://youtu.be/-2ZvrNMOPyA?t=195)

I will use the workaround by casting to another actor in my project for now to not be stuck.

---

<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:** [June 18, 2024, 5:41pm UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/7 "2024-06-18T17:41:50Z")

</div>

I see. I get the same error by using dfx 0.20.1. But if you upgrade to dfx 0.20.2, everything works fine. Not sure what changed between the dfx versions. You may also need to run `npm install` to get the typescript.

---

<div class="post-metadata">

**Author:** ![f0i](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/f0i/32/2327_2.png) [@f0i](https://forum.dfinity.org/u/f0i)\
**Post date:** [June 19, 2024, 7:35am UTC](https://forum.dfinity.org/t/problem-with-blob-vs-nat8-from-candid-interface-in-motoko/32037/8 "2024-06-19T07:35:07Z")

</div>

I can confirm that it works as expected with dfx 0.20.2. 👍  
Didn’t know there was a newer version as [https://internetcomputer.org/install.sh](https://internetcomputer.org/install.sh) still downloads 0.20.1 by default.

Just for reference if someone else runs into this, here is the current version numbers:

$ dfx --version  
dfx 0.20.2  
$ moc-wrapper --version  
Motoko compiler 0.11.0 (source mhbjd2y7-6x090wpf-ilgy86qg-50gxdf1b)

The motoko compiler seems to be downgraded from 0.11.1 to 0.11.0.  
moc 0.11.1 still seems to works in combination of dfx 0.20.2.
