Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing Plutus Nothing and Just Maybe values. #55

Open
Riley-Kilgore opened this issue Apr 28, 2022 · 8 comments
Open

Serializing Plutus Nothing and Just Maybe values. #55

Riley-Kilgore opened this issue Apr 28, 2022 · 8 comments
Labels
question Further information is requested

Comments

@Riley-Kilgore
Copy link

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?

@dcSpark dcSpark deleted a comment Apr 28, 2022
@rooooooooob
Copy link
Contributor

@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
Copy link
Contributor

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

@SebastienGllmt SebastienGllmt added the question Further information is requested label Apr 29, 2022
@rooooooooob
Copy link
Contributor

rooooooooob commented Apr 29, 2022

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
Copy link
Author

Riley-Kilgore commented Apr 29, 2022

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
Copy link
Author

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
Copy link
Contributor

@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
Copy link
Contributor

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
Copy link
Author

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants