Skip to content

Conversation

@alessandrokonrad
Copy link
Contributor

I was finally able to reproduce the same script data hash in the serialization-lib as in the cardano-cli.
language_views is actually not the same as costmdls and the encoding is totally different. I was able to extract the encoded language_views from the cardano-node and then concatenated redemeers + datums + language_views, hashed it and received the correct hash.

Since I don't know exactly how to create the languague_views encoding, I created a struct LanguageViews, which takes the raw bytes, that fixes at least the hash_script_data function. Maybe someone could help here to build this encoding fully in Rust.

This is the language_views encoding based on current mainnet protocol parameters:

a141005901d59f1a000302590001011a00060bc719026d00011a000249f01903e800011a000249f018201a0025cea81971f70419744d186419744d186419744d186419744d186419744d186419744d18641864186419744d18641a000249f018201a000249f018201a000249f018201a000249f01903e800011a000249f018201a000249f01903e800081a000242201a00067e2318760001011a000249f01903e800081a000249f01a0001b79818f7011a000249f0192710011a0002155e19052e011903e81a000249f01903e8011a000249f018201a000249f018201a000249f0182001011a000249f0011a000249f0041a000194af18f8011a000194af18f8011a0002377c190556011a0002bdea1901f1011a000249f018201a000249f018201a000249f018201a000249f018201a000249f018201a000249f018201a000242201a00067e23187600010119f04c192bd200011a000249f018201a000242201a00067e2318760001011a000242201a00067e2318760001011a0025cea81971f704001a000141bb041a000249f019138800011a000249f018201a000302590001011a000249f018201a000249f018201a000249f018201a000249f018201a000249f018201a000249f018201a000249f018201a00330da70101ff

Links that could help with building this encoding:
https://github.com/input-output-hk/cardano-ledger-specs/blob/d37757e273d10fb251681faa1a2a0cc1ac018384/alonzo/test/test/Test/Cardano/Ledger/Alonzo/Serialisation/Canonical.hs#L36-L43
https://github.com/input-output-hk/cardano-ledger-specs/blob/37322df14d44d1370e8a72b677815d64a92baa00/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs#L268-L276
https://github.com/input-output-hk/cardano-ledger-specs/blob/37322df14d44d1370e8a72b677815d64a92baa00/alonzo/impl/src/Cardano/Ledger/Alonzo/PParams.hs#L511-L525
https://github.com/input-output-hk/cardano-ledger-specs/blob/ec51e4fb1b17461ab612cf427b79f1742942e8cb/alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs#L172-L175

@anhdungle93
Copy link

Hi,

even after using this PR the first test case in the piece of code still fails

import {BigInt, hash_plutus_data, PlutusData, PlutusList} from '../pkg/cardano_serialization_lib';
import {Buffer} from 'buffer';

const hashData = (data: any) => (
    hash_plutus_data(PlutusData.new_list(data))
)
const toBinString = (bytes: Buffer) =>
  bytes.reduce((str, byte) => str + byte.toString(2), '');

let list = PlutusList.new();
list.add(PlutusData.new_bytes(Buffer.from('32', 'hex')));

let testHash = hashData(list);

let hashString = Buffer.from(testHash.to_bytes()).toString('hex')
console.log('non-empty list hash', hashString);

console.log(toBinString(Buffer.from(testHash.to_bytes())));
console.log('non-empty list matches expected?', hashString === 'da4a3e3ed534dcf8e1cf726b41ce8d2cb9eb2dfe9b40c98b6fbd43f83aa0a02a');

testHash = hashData(PlutusList.new());
hashString = Buffer.from(testHash.to_bytes()).toString('hex')
console.log('empty list hash', hashString);
console.log(toBinString(Buffer.from(testHash.to_bytes())));
console.log('empty matches expected?', hashString === '45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0');

@alessandrokonrad
Copy link
Contributor Author

Hi,

even after using this PR the first test case in the piece of code still fails

import {BigInt, hash_plutus_data, PlutusData, PlutusList} from '../pkg/cardano_serialization_lib';
import {Buffer} from 'buffer';

const hashData = (data: any) => (
    hash_plutus_data(PlutusData.new_list(data))
)
const toBinString = (bytes: Buffer) =>
  bytes.reduce((str, byte) => str + byte.toString(2), '');

let list = PlutusList.new();
list.add(PlutusData.new_bytes(Buffer.from('32', 'hex')));

let testHash = hashData(list);

let hashString = Buffer.from(testHash.to_bytes()).toString('hex')
console.log('non-empty list hash', hashString);

console.log(toBinString(Buffer.from(testHash.to_bytes())));
console.log('non-empty list matches expected?', hashString === 'da4a3e3ed534dcf8e1cf726b41ce8d2cb9eb2dfe9b40c98b6fbd43f83aa0a02a');

testHash = hashData(PlutusList.new());
hashString = Buffer.from(testHash.to_bytes()).toString('hex')
console.log('empty list hash', hashString);
console.log(toBinString(Buffer.from(testHash.to_bytes())));
console.log('empty matches expected?', hashString === '45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0');

hash_script_data is not the same as hash_plutus_data. This PR is about the hash_script_data function.

@vsubhuman vsubhuman self-requested a review September 24, 2021 16:06
@vsubhuman
Copy link
Member

Thank you, @alessandrokonrad !

@vsubhuman vsubhuman added this to the 9.2.0 milestone Sep 25, 2021
@rooooooooob
Copy link
Contributor

@alessandrokonrad I am not a git master so I wasn't sure the best way to go about this so I just cherry picked your commit and continued on from there in #217 I posted some questions there to confirm things

@rooooooooob
Copy link
Contributor

Should we close this PR now that #217 supersedes it?

@vsubhuman vsubhuman closed this Nov 7, 2021
@vsubhuman vsubhuman mentioned this pull request Feb 6, 2022
40 tasks
@vsubhuman vsubhuman removed this from the 10.0.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants