dcSpark / cardano-multiplatform-lib

Rust implementation of Cardano
MIT License
100 stars 36 forks source link

Serializing Plutus `Nothing` and `Just` Maybe values. #55

Open Riley-Kilgore opened 2 years ago

Riley-Kilgore commented 2 years ago

I am trying to utilize a contract that uses a Plutus Maybe in the Datum.

I am trying to serialize Nothing using the cardano-multiplatform-lib, when this is done in Haskell the cborHex output is "\128".

Is there any way currently to accomplish this?

rooooooooob commented 2 years ago

@Riley-Kilgore can you provide a more complex structure containing Nothing or Just values? That could help to reverse-engineer this a bit since I wasn't successful when looking through the haskell code (although maybe I was looking in the wrong spots). When I try with that hex string (I had to pad it since it's odd-length) I get a CBOR unsigned integer 18, which doesn't seem like it could be Nothing. If I pad the other way I get an unsigned integer 1 with a trailing negative int after.

The binary spec (CBOR spec) for plutus datums doesn't have anything that could very obviously be Nothing/Some (unless Just x encodes to x) unless they are using the constrained plutus datums in some specific way (i.e. specific tags) but that doesn't seem to be the case with that 128 cbor hex.

SebastienGllmt commented 2 years ago

I think you should be able to convert the datum to JSON and then import the JSON from CML

rooooooooob commented 2 years ago

I think you should be able to convert the datum to JSON and then import the JSON from CML

We'd have to see what they would export the JSON as. The Haskell JSON format doesn't support JSON null (or true/false) so it would really depend on how Haskell turns the Haskell Nothing into JSON I guess.

Riley-Kilgore commented 2 years ago

I think you should be able to convert the datum to JSON and then import the JSON from CML

We'd have to see what they would export the JSON as. The Haskell JSON format doesn't support JSON null (or true/false) so it would really depend on how Haskell turns the Haskell Nothing into JSON I guess.

I was able to use Data.Aeson.toJSON on Nothing. When show is then applied, Null is yielded.

Riley-Kilgore commented 2 years ago

I have not as of yet had any resolution to this.

Is there a way for me to serialize Null yielded above to be used in the way that is intended here? @rooooooooob

I have not seen json parsed by the cml, what does the usage look like for this? Or am I misunderstanding?

Thank you both for your time.

rooooooooob commented 2 years ago

@Riley-Kilgore the JSON parsing in CML is found in plutus::encode_json_str_to_plutus_datum() and plutus::decode_plutus_datum_to_json_str(). (they are free-floating functions if you're using it from js/wasm) see code here. You'll want to use the detailed schema.

I'm a bit confused on if Nothing encoding is actually intended behavior in the haskell code though as you said you get 128 as your hex, as that results in integers as the datum, which is ambiguous. I'd have to see what it looks like when you use Nothing in a more complex situation like inside of a list/map to make sure of the format they're exporting too though.

rooooooooob commented 2 years ago

I"m asking because it's possible there was some typeclass implemented without this being the intention that allowed these conversions since there's nothing that is immediately obvious as a mapping to the binary spec's definition for plutus datum, and how the hex you gave seemed odd. Although maybe that hex was actually erroneous which is why I wanted to see nothing/just being used inside of more complex structures to be sure of how they're encoding it.

Riley-Kilgore commented 2 years ago

Sorry, I have had a lot going on the past few days and was unable to get back to this thread.

The solution I have come to thus far is simply avoiding using Maybe in my Validation logic, creating my own alternative types when I need this kind of behavior.

If you'd like I can definitely go and experiment some more with Maybe serialization from Haskell and report back here -- but unless it's something of interest for you guys, you're also welcome to just close the issue. I feel that I have proper resolution at this point.

Thank you! :)