2

I have a JSON format needed for PlutusData serialization (but the format is not a standard datum in a list). The datum is wrapped in Maybe monad, so you'll note the nested lists (an array inside the outermost fields).

{
  "fields": [
    {
      "fields": [
        { "bytes": "581ccb9fba6103382ffac26484e35af190bc885510f6a25ee3dbdc673bea" }
      ],
      "constructor": 0
    }
  ],
  "constructor": 0
}

The inner bytes are CBOR hex of a sample pubkeyhash. We know that to construct the inner datum we'd do:

const datumPkh = PlutusData.from_bytes(pkhCBORBytes);

to insert that in a list is also trivial:

const innerList = PlutusList.new()
innerList.add(datumPkh)

However, since we cannot add a PlutusList to a PlutusList, would we convert the innerList to bytes and read those bytes into a PlutusDatum like:

const outerList = PlutusList.new()
outerList.add(PlutusData.from_bytes(innerList.to_bytes()))

Or, should we do something else like a ConstrData's value to_bytes() and then a PlutusList.from_bytes...etc.?

We know we can't tell if the end results are the same from the hashes of the data in cardano-cli and the serialization lib as noted in: https://github.com/Emurgo/cardano-serialization-lib/issues/333 and https://github.com/Emurgo/cardano-serialization-lib/issues/247

2 Answers 2

1

Maybe this will help,

typhonjs supports creating plutusDataCbor using utility method

const plutusDataBuffer = typhonjs.utils.createPlutusDataCbor({
    constructor: 0,
    fields: [12, 12],
});

const plutusDataCBOR = plutusDataBuffer.toString('hex');

You can pass in whole JSON object to generate plutusData.

1
  • Doesn't seem to like the structure of: { "fields": [ { "fields": [ CBORBytesOfPkh ], "constructor": 0 } ], "constructor": 0 }
    – Eli Selkin
    Apr 11 at 18:16
0

Mehul was correct. @strictahq/typhonjs does work correctly and produces the same result as

cardano-cli transaction hash-script-data --script-data-file test.json

Where test.json contents are:

{ "fields": [{ "fields": [{ "bytes": "cb9fba6103382ffac26484e35af190bc885510f6a25ee3dbdc673bea" }], "constructor": 0 }], "constructor": 0 }

and JS content

import { utils } from '@stricahq/typhonjs';
import { Buffer } from 'buffer';
import { PlutusData, hash_plutus_data } from "@emurgo/cardano-serialization-lib-browser"
const pkh = Buffer.from(await getPubKeyHash());
const datum = utils.createPlutusDataCbor({
  fields: [{ fields: [pkh], constructor: 0 }],
  constructor: 0,
});
const plutusDatum = PlutusData.from_bytes(datum);
const datumHash = hash_plutus_data(data);
console.log(Buffer.from(datumHash.to_bytes()).toString('hex'))

We are still trying to determine if that pkh needs to be CBOR encoded first, but that is secondary now that the structure works and produces the same hashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.