Cardano-cli hash-script-data

Hi there,

I was wondering what the formats are allowed for the json that one can hash with the cardano-cli. An simple file.json example that one can hash is

{
   "constructor":0,
   "fields":[
      {
         "int": 4
      }
   ]
}

Then this gives

cardano-cli transaction hash-script-data --script-data-file file.json 
8bb54ceaee2f57731094a2e96b8ad87bcc8988b1fa838e8c833eb3b72eae29a1

What do these objects represent? What is a constructor and why is it 0? What other keys are allowed. I guess all types that can be represented by the BuiltinData type in the plutus.

The best documentation that I know of for this is the source code, which defines the JSON encoding for ScriptData.

The tags "int" and "bytes" encode those primitives. The tag "list" encodes [ScriptData] and the tag "map" encodes [(ScriptData,ScriptData)]. The "constructor" tag with a "fields" tag encodes Haskell constructors, where the integer following "constructor" is the zero-based index to the constructor that is being used; the fields are encoded in order. Use the template Haskell PlutusTx.makeIsDataIndexed or PlutusTx.unsafeMakeIsData to create the encoding for a custom data type.

For example, for the type data Example = This Integer | That, the value This 5 becomes {"constructor": 0, "fields": [{"int": 5}]} and That becomes {"constructor": 1, "fields": []}.

2 Likes

The official Cardano API documentation can be difficult to navigate but it is worth bookmarking all the same. Think of it as a reference for when you know what you are looking for. It is generated from the code itself using Haddock which is similar to other automated documentation tools like Doxygen, Javadocs, etc.

https://input-output-hk.github.io/cardano-node/index.html

2 Likes
1 Like

This is very helpful. So for datum wherein I have in my plutus the following:
data DatumData = DatumData { ownerKey :: PubKeyHash , ownerAmt :: !Integer } and where I apply the unsafeMakeIsData, etc…

would the proper json be:

edit: whoops I meant list here:

{“list”: [(“thepubkeyhash”,10)]}

or something like:

{“constructor”: 0, “fields”: [{“bytes”: “thepubkeyhash”}, {“int”: 10}]}

or something else?

1 Like

Figured it out, here’s an example of the code from my json file:

{"fields": [{"fields": [{"bytes": "thepubkeyhash"}, {"int": 10}], "constructor": 0}], "constructor": 0}

This successfully matches up.

1 Like

So the next question I have, is how to go about seeing the embedded datum for a particular transaction (which you know has the embed)

First you need to retrieve the bytes of the datum. A couple of approaches are to (1) use a web service like cardanoscan.io, (2) query the chain index, (3) query the node.

Once you have the bytes of datum, you can use fromBuiltinData to deserialize it to your custom type.

2 Likes

Awesome, thank you!