I wanted to understand a bit better the hash-script-data functionality from cardano-cli. I checked the source code but had no luck in understanding how this hash is made.
Thanks for the references! I was able to reproduce the hash from cardano-cli using libsodium generic-hash and converting the 42 number into CBOR (182a) prior to hashing.
I was trying to do the same for objects though, without luck:
I convert it to CBOR A26B636F6E7374727563746F7200666669656C647381A163696E7404 and then try to hash it and I get 9aa23c572ebf5d39113f516d4bbf69d1b9650e55ea63cdd7fb4ee77975def5b0, while if I put that data in a json file and use cardano-cli transaction hash-script-data --script-data-file ./tests/sample.json I get 8bb54ceaee2f57731094a2e96b8ad87bcc8988b1fa838e8c833eb3b72eae29a1
Also tried using bytes instead of UTF string in the CBOR encoding: A24B636F6E7374727563746F7200466669656C647381A143696E7404
which gives me 5677bf1048dad6dce36483d2bec0fafc71f5dc9b01a2b1326c6443c2cd264b50
Even when reading the source code I’m not sure what I may be missing here
I haven’t tracked down the complete answer, which I believe involves finding the schema for how PlutusData is serialized—the information will be in the ledger spec or its implementation.
A partial answer is that one shouldn’t serialize text like "construction". That is too verbose and it is very likely that there is a custom, succinct serialization that is closely related to the CBOR you generated. I’ll poke around in the specs or source code and post here when I find the answer.
That makes sense and is already very helpful! Made me find this which may contain the answer about how PlutusData is encoded into CBOR definitely looks like my encoding is not correct.
I’ll check that in more detail tomorrow and post here my findings,
Alright, looking at the source code it seems the constructor is converted into a tag, based on its index. The values are then converted into a list, which makes the CBOR encoding to be much smaller, makes much more sense.
What I get from the object I mentioned is: D8798104 (Equivalent to 121([4])) - Tag 121, since constructor value is 0 (121 + 0) and a list with values.
Hash turns it into 034eb48ee254d7e58ff9fda1398f8086b9a8c392e63322960137e6c30bbd7bcf, which is still different than cardano-cli output 8bb54ceaee2f57731094a2e96b8ad87bcc8988b1fa838e8c833eb3b72eae29a1
Still seems like a step forward though, feels like we’re getting closer
If anyone has any idea what I may be missing here let me know
Can you shed some light because I’m missing something.
Using follwoing code
CborWriter w = new CborWriter();
w.WriteUInt32(42);
var encoded = w.Encode();
var data = Sodium.Utilities.BinaryToHex(encoded);
Console.WriteLine(data);
var res = Sodium.GenericHash.Hash(encoded, null, 32);
Console.WriteLine(Sodium.Utilities.BinaryToHex(res));
I’m able to get same hash as for running:
cardano-cli transaction hash-script-data --script-data-value 42
9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b
Now I have a json file with simple data: {“fields”:[],“constructor”:0}
if I execute cli
cardano-cli transaction hash-script-data --script-data-file cos.json 923918e403bf43c34b4ef6b48eb2ee04babed17320d8d1b9ff9ad086e86f44ec
if I execute:
cardano-cli transaction hash-script-data --script-data-value {“constructor”:0,“fields”:} e91e9c37cad5a5215f210776134143165a4c438a46cc0d82e5111611805c3864
However if I execute it in c#:
CborWriter cw = new CborWriter();
cw.WriteStartMap(null);
cw.WriteTextString("constructor");
cw.WriteUInt32(0);
cw.WriteTextString("fields");
cw.WriteStartArray(null);
cw.WriteEndArray();
cw.WriteEndMap();
var complexEncoded = cw.Encode();
var dataComplex = Sodium.Utilities.BinaryToHex(complexEncoded);
Console.WriteLine(dataComplex);
var complexRes = Sodium.GenericHash.Hash(complexEncoded, null, 32);
Console.WriteLine(Sodium.Utilities.BinaryToHex(complexRes));
Does it? I wasn’t able to reproduce it with 10.0.0-beta.8. What argument did you provide to cardano-cli transaction hash-script-data to get the same hash as with the serialization lib snippet you posted?