Skip to content

Commit 63e1b71

Browse files
authored
Merge pull request #201 from alessandrokonrad/hashes
Hashes
2 parents 791c211 + c148e1d commit 63e1b71

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

rust/src/plutus.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ impl PlutusData {
375375
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
376376
pub struct PlutusList(Vec<PlutusData>);
377377

378+
to_from_bytes!(PlutusList);
379+
378380
#[wasm_bindgen]
379381
impl PlutusList {
380382
pub fn new() -> Self {
@@ -475,6 +477,8 @@ impl RedeemerTag {
475477
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
476478
pub struct Redeemers(Vec<Redeemer>);
477479

480+
to_from_bytes!(Redeemers);
481+
478482
#[wasm_bindgen]
479483
impl Redeemers {
480484
pub fn new() -> Self {

rust/src/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,30 @@ pub fn hash_auxiliary_data(auxiliary_data: &AuxiliaryData) -> AuxiliaryDataHash
803803
pub fn hash_transaction(tx_body: &TransactionBody) -> TransactionHash {
804804
TransactionHash::from(crypto::blake2b256(tx_body.to_bytes().as_ref()))
805805
}
806+
#[wasm_bindgen]
807+
pub fn hash_plutus_data(plutus_data: &PlutusData) -> DataHash {
808+
DataHash::from(blake2b256(&plutus_data.to_bytes()))
809+
}
810+
#[wasm_bindgen]
811+
pub fn hash_script_data(redeemers: &Redeemers, cost_models: &Costmdls, datums: Option<PlutusList>) -> ScriptDataHash {
812+
/*
813+
; script data format:
814+
; [ redeemers | datums | language views ]
815+
; The redeemers are exactly the data present in the transaction witness set.
816+
; Similarly for the datums, if present. If no datums are provided, the middle
817+
; field is an empty string.
818+
*/
819+
let mut buf = Vec::new();
820+
buf.extend(redeemers.to_bytes());
821+
if let Some(d) = &datums {
822+
buf.extend(d.to_bytes());
823+
} else {
824+
let empty_string = "";
825+
buf.extend(empty_string.as_bytes());
826+
}
827+
buf.extend(cost_models.to_bytes());
828+
ScriptDataHash::from(blake2b256(&buf))
829+
}
806830

807831
// wasm-bindgen can't accept Option without clearing memory, so we avoid exposing this in WASM
808832
pub fn internal_get_implicit_input(

0 commit comments

Comments
 (0)