Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script_data_hash computation. #512

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions pallas-txbuilder/src/transaction/model.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use pallas_addresses::Address as PallasAddress;
use pallas_codec::minicbor;
use pallas_crypto::{
hash::{Hash, Hasher},
key::ed25519,
};
use pallas_primitives::{babbage, Fragment};
use pallas_primitives::{alonzo::{PlutusData, Redeemer}, babbage, conway::CostMdls, Fragment};
use pallas_wallet::PrivateKey;

use std::{collections::HashMap, ops::Deref};
Expand Down Expand Up @@ -340,8 +341,41 @@ impl StagingTransaction {
self
}

// TODO: script_data_hash computation
pub fn script_data_hash(mut self, hash: Hash<32>) -> Self {
pub fn script_data_hash(mut self, redeemers: Vec<Redeemer>,datums: Option<Vec<PlutusData>>, costmodel: CostMdls) -> Self{

let mut buf = Vec::new();
if redeemers.len() == 0 && datums.is_some() {
buf.push(0xA0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the value that changed from 0x80 -> 0xA0 for Conway Era, correct?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and is just a side effect of encoding an empty map of redeemers.

To correctly handle this, the different era implementations of redeemers should have era-specific serialization code; because Chang+1 will mean the redeemers now must be encoded as a map.

Copy link
Member

@scarmuega scarmuega Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of handling cbor bytes directly, it would be easier / safer to have a struct ScriptData that implements cbor encoding and contains the underlying data parts (empty maps and such nuances would be handled by the data type).

This struct could live inside primitives crate and get versioned by era (babbage:ScriptDataHash, conway:ScriptData, etc).

if let Some(d) = datums {
let datum_bytes: [Vec<u8>; 1] = [d.encode_fragment().unwrap()];
buf.extend(datum_bytes[0].clone());
}
buf.push(0xA0);
} else {
let redeemer_bytes: [Vec<u8>; 1] = [redeemers.encode_fragment().unwrap()];
buf.extend(&redeemer_bytes[0]);

if let Some(d) = datums {
let datum_bytes: [Vec<u8>; 1] = [d.encode_fragment().unwrap()];
buf.extend(datum_bytes[0].clone());
}

let mut language_view = vec![];
if let Some(plutus_v1_model) = &costmodel.plutus_v1 {
let plutusv1 = [(0, plutus_v1_model.to_vec())];
minicbor::encode(plutusv1, &mut language_view).unwrap();
buf.extend(language_view);
} else if let Some(plutus_v2_model) = &costmodel.plutus_v2 {
let plutusv2 = [(1, plutus_v2_model.to_vec())];
minicbor::encode(plutusv2, &mut language_view).unwrap();
buf.extend(language_view);
} else if let Some(plutus_v3_model) = &costmodel.plutus_v3 {
let plutusv3 = [(2, plutus_v3_model.to_vec())];
minicbor::encode(plutusv3, &mut language_view).unwrap();
buf.extend(language_view);
}
}
let hash = Hasher::<256>::hash(buf.as_ref());
self.script_data_hash = Some(Bytes32(*hash));
self
}
Expand Down