0

I want 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.

From the examples, if we run:

cardano-cli transaction hash-script-data --script-data-value 42

We get the following hash:

9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b

However if we try to do a simple SHA256 hash of a 42 value we get:

73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049

Also tried in the Plutus object format:

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

but none of those match the hash generated by cardano-cli.

Also tried innumerous other alternatives but obviously I'm missing something, anyone knows what's the process for generating this hash?

Thank you!

1 Answer 1

3

The missing step would be converting the ScriptData into CBOR prior to hashing.

In the case of ScriptDataNumber 42 the CBOR Hex is "182a" which is a bit simplified for Numbers vs more complex ScriptData. The CBOR Jump Table shows that 0x18 indicates unsigned integer (one-byte uint8_t follows) followed by 2a which is 42 in hex.

Or using Haskell:

let plutusData = PlutusCore.Data.I 42
print $ Base16.encode $ BSL.toStrict $ Codec.Serialise.serialise plutusData
-- "182a"
print $ Cardano.Api.hashScriptData $ Cardano.Api.Shelley.fromPlutusData plutusData
-- "9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b"

and using libsodiumjs as example to hash:

var libsodiumWrappers = require("libsodium-wrappers");
var _sodium = libsodiumWrappers;

(async () => {
  await _sodium.ready;
  const sodium = _sodium;
  var h = sodium.crypto_generichash(32, sodium.from_hex('182a'));
  console.log(sodium.to_hex(h));
})();

enter image description here

1
  • Thanks, that's exactly what I was looking for! Regarding objects/json files, are we supposed to also convert to CBOR? For example a datum object like {"constructor": 0,"fields": [{"int": 4}]}, I transform into CBOR: a24b636f6e7374727563746f7200466669656c647381a143696e7404 and when I hash it I get 5677bf1048dad6dce36483d2bec0fafc71f5dc9b01a2b1326c6443c2cd264b50, but if I do the same with cardano-cli I get 8bb54ceaee2f57731094a2e96b8ad87bcc8988b1fa838e8c833eb3b72eae29a1. Is there any additional step in this case?
    – glneto
    Commented Nov 2, 2021 at 22:12

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