dfinity / motoko-base

The Motoko base library
Apache License 2.0
484 stars 99 forks source link

feat: Blob.from{Type}() functions #242

Closed davidp94 closed 3 years ago

davidp94 commented 3 years ago

Hello there;

I'd like to leverage the CertifiedData API and I've some trouble to convert basic types to Blob;

I think it would be awesome to have something like : Blob.fromNat(xx) Blob.fromNat16 ... (and vice-versa?)

cc. @nomeata

Thank you in advance; David

nomeata commented 3 years ago

We have indeed a shortage of Blob conversions, or general encoding/decoding functions, partly becaues the Blob type’s future is still a bit in the air – we might want to merge it with [Nat8], which would make certain things easier.

For using these CertifiedData API calls, using Blob.fromNat(…) would only help if you if the whole caniser only ever wants to certify a single number (or a few, if you concatenate them), because you only get to store 32 bytes this way.

The intended use of this API is to store a cryptographic hash (e.g. SHA256). There is an implementation of that in https://github.com/enzoh/motoko-sha, but it works with [Nat8], not Blob (so see above). And you’d still have to encode your data structures into a blob first.

This is a bit of a construction site at the moment, sorry.

davidp94 commented 3 years ago

We have indeed a shortage of Blob conversions, or general encoding/decoding functions, partly becaues the Blob type’s future is still a bit in the air – we might want to merge it with [Nat8], which would make certain things easier.

For using these CertifiedData API calls, using Blob.fromNat(…) would only help if you if the whole caniser only ever wants to certify a single number (or a few, if you concatenate them), because you only get to store 32 bytes this way.

The intended use of this API is to store a cryptographic hash (e.g. SHA256). There is an implementation of that in https://github.com/enzoh/motoko-sha, but it works with [Nat8], not Blob (so see above). And you’d still have to encode your data structures into a blob first.

This is a bit of a construction site at the moment, sorry.

I was thinking of storing the lastIndex of an event then some sort of merkle tree root hash; I will give it a try in Rust maybe.

Thank you for the fast response

I tried to return [Nat8] as Blob but it did not work.

nomeata commented 3 years ago

I tried to return [Nat8] as Blob but it did not work.

Yes, not supported yet, and there is no easy work-around at the moment.

nomeata commented 3 years ago

I’ll add conversion functions between [Nat8] and Blob for the next release. It’s not gonna be pretty or particularly fast, but at least it should be possible to builds Blobs that way.

davidp94 commented 3 years ago

Thanks !

On Thu 8 Apr 2021 at 09:12 Joachim Breitner @.***> wrote:

I’ll add conversion functions between [Nat8] and Blob for the next release. It’s not gonna be pretty or particularly fast, but at least it should be possible to builds Blobs that way.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dfinity/motoko-base/issues/242#issuecomment-815478182, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGFVWNMUYQODURB4OA5WC7TTHVCLDANCNFSM42P756KQ .

nomeata commented 3 years ago

BTW, @davidp94, you might be interested in this libary: https://github.com/nomeata/motoko-merkle-tree

davidp94 commented 3 years ago

BTW, @davidp94, you might be interested in this libary: https://github.com/nomeata/motoko-merkle-tree

Thanks I already looked at it over the week-end; nice one 👍

I will implement this when fetching events; I will add a merkle path to the response + certified root hash;

nomeata commented 3 years ago

Great! If you have questions, maybe best to ping me on forum.dfinity.org