Skip to content

Commit b513b9d

Browse files
Alessandro KonradAlessandro Konrad
authored andcommitted
addes hash functions
1 parent 6238440 commit b513b9d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

rust/src/plutus.rs

Lines changed: 2 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 {

rust/src/utils.rs

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

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

0 commit comments

Comments
 (0)