cml_chain_wasm/crypto/
hash.rs

1use wasm_bindgen::prelude::{wasm_bindgen, JsError};
2
3use crate::{
4    auxdata::AuxiliaryData,
5    plutus::{CostModels, PlutusData, Redeemers},
6    transaction::{TransactionBody, TransactionWitnessSet},
7    utils::LanguageList,
8    PlutusDataList,
9};
10
11use cml_crypto_wasm::{AuxiliaryDataHash, DatumHash, ScriptDataHash, TransactionHash};
12
13use cml_chain::NonemptySet;
14
15#[wasm_bindgen]
16pub fn hash_auxiliary_data(auxiliary_data: &AuxiliaryData) -> AuxiliaryDataHash {
17    cml_chain::crypto::hash::hash_auxiliary_data(auxiliary_data.as_ref()).into()
18}
19
20#[wasm_bindgen]
21pub fn hash_transaction(tx_body: &TransactionBody) -> TransactionHash {
22    cml_chain::crypto::hash::hash_transaction(tx_body.as_ref()).into()
23}
24
25#[wasm_bindgen]
26pub fn hash_plutus_data(plutus_data: &PlutusData) -> DatumHash {
27    cml_chain::crypto::hash::hash_plutus_data(plutus_data.as_ref()).into()
28}
29
30/// Calculates the hash for script data (no plutus scripts) if it is necessary.
31/// Returns None if it was not necessary (no datums/redeemers) to include.
32///
33/// Most users will not directly need this as when using the builders
34/// it will be invoked for you.
35///
36/// Note: This WASM binding does not work with non-standard witness set
37/// encodings. If you created the witness set manually this is not an issue
38/// but for constructing it from deserializing a transaction/witness then
39/// please use calc_script_data_hash_from_witness()
40#[wasm_bindgen]
41pub fn hash_script_data(
42    redeemers: &Redeemers,
43    cost_models: &CostModels,
44    datums: Option<PlutusDataList>,
45    //    encoding: Option<TransactionWitnessSetEncoding>,
46) -> ScriptDataHash {
47    let datums = datums.map(|datums| NonemptySet::from(Into::<Vec<_>>::into(datums)));
48    cml_chain::crypto::hash::hash_script_data(
49        redeemers.as_ref(),
50        cost_models.as_ref(),
51        datums.as_ref(),
52        None,
53    )
54    .into()
55}
56
57/// Calculates the hash for script data (with plutus scripts) if it is necessary.
58/// Returns None if it was not necessary (no datums/redeemers) to include.
59///
60/// Most users will not directly need this as when using the builders
61/// it will be invoked for you.
62///
63/// Note: This WASM binding does not work with non-standard witness set
64/// encodings. If you created the witness set manually this is not an issue
65/// but for constructing it from deserializing a transaction/witness then
66/// please use calc_script_data_hash_from_witness()
67#[wasm_bindgen]
68pub fn calc_script_data_hash(
69    redeemers: &Redeemers,
70    datums: &PlutusDataList,
71    cost_models: &CostModels,
72    used_langs: &LanguageList,
73    //    encoding: Option<TransactionWitnessSetEncoding>,
74) -> Result<Option<ScriptDataHash>, JsError> {
75    cml_chain::crypto::hash::calc_script_data_hash(
76        redeemers.as_ref(),
77        &NonemptySet::from(Into::<Vec<_>>::into(datums.clone())),
78        cost_models.as_ref(),
79        used_langs.as_ref(),
80        None,
81    )
82    .map(|sdh| sdh.map(Into::into))
83    .map_err(Into::into)
84}
85
86/// Calculates the hash for script data from a witness if it is necessary.
87/// Returns None if it was not necessary (no datums/redeemers) to include.
88///
89/// Most users will not directly need this as when using the builders
90/// it will be invoked for you.
91#[wasm_bindgen]
92pub fn calc_script_data_hash_from_witness(
93    witnesses: &TransactionWitnessSet,
94    cost_models: &CostModels,
95) -> Result<Option<ScriptDataHash>, JsError> {
96    cml_chain::crypto::hash::calc_script_data_hash_from_witness(
97        witnesses.as_ref(),
98        cost_models.as_ref(),
99    )
100    .map(|sdh| sdh.map(Into::into))
101    .map_err(Into::into)
102}