-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add missing hashing/crypto WASM bindings Fixes #289 For script data hashing we do not pass in the witness set encodings like in the rust bindings. We might wish to expose the CBOR encodings generally, or for this API we could have something that takes in the entire witness set. This function is likely to be used more indirectly via the builders anyway. * calc_script_data_hash_from_witness() * cargo fmt --all (cargo fmt left these)
- Loading branch information
1 parent
e6f9ea8
commit cd32fb6
Showing
10 changed files
with
207 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use wasm_bindgen::prelude::{wasm_bindgen, JsError}; | ||
|
||
use crate::{ | ||
auxdata::AuxiliaryData, | ||
plutus::{CostModels, PlutusData}, | ||
transaction::{TransactionBody, TransactionWitnessSet}, | ||
utils::LanguageList, | ||
PlutusDataList, RedeemerList, | ||
}; | ||
|
||
use cml_crypto_wasm::{AuxiliaryDataHash, DatumHash, ScriptDataHash, TransactionHash}; | ||
|
||
#[wasm_bindgen] | ||
pub fn hash_auxiliary_data(auxiliary_data: &AuxiliaryData) -> AuxiliaryDataHash { | ||
cml_chain::crypto::hash::hash_auxiliary_data(auxiliary_data.as_ref()).into() | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn hash_transaction(tx_body: &TransactionBody) -> TransactionHash { | ||
cml_chain::crypto::hash::hash_transaction(tx_body.as_ref()).into() | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn hash_plutus_data(plutus_data: &PlutusData) -> DatumHash { | ||
cml_chain::crypto::hash::hash_plutus_data(plutus_data.as_ref()).into() | ||
} | ||
|
||
/// Calculates the hash for script data (no plutus scripts) if it is necessary. | ||
/// Returns None if it was not necessary (no datums/redeemers) to include. | ||
/// | ||
/// Most users will not directly need this as when using the builders | ||
/// it will be invoked for you. | ||
/// | ||
/// Note: This WASM binding does not work with non-standard witness set | ||
/// encodings. If you created the witness set manually this is not an issue | ||
/// but for constructing it from deserializing a transaction/witness then | ||
/// please use calc_script_data_hash_from_witness() | ||
#[wasm_bindgen] | ||
pub fn hash_script_data( | ||
redeemers: &RedeemerList, | ||
cost_models: &CostModels, | ||
datums: Option<PlutusDataList>, | ||
// encoding: Option<TransactionWitnessSetEncoding>, | ||
) -> ScriptDataHash { | ||
cml_chain::crypto::hash::hash_script_data( | ||
redeemers.as_ref(), | ||
cost_models.as_ref(), | ||
datums.as_ref().map(AsRef::as_ref), | ||
None, | ||
) | ||
.into() | ||
} | ||
|
||
/// Calculates the hash for script data (with plutus scripts) if it is necessary. | ||
/// Returns None if it was not necessary (no datums/redeemers) to include. | ||
/// | ||
/// Most users will not directly need this as when using the builders | ||
/// it will be invoked for you. | ||
/// | ||
/// Note: This WASM binding does not work with non-standard witness set | ||
/// encodings. If you created the witness set manually this is not an issue | ||
/// but for constructing it from deserializing a transaction/witness then | ||
/// please use calc_script_data_hash_from_witness() | ||
#[wasm_bindgen] | ||
pub fn calc_script_data_hash( | ||
redeemers: &RedeemerList, | ||
datums: &PlutusDataList, | ||
cost_models: &CostModels, | ||
used_langs: &LanguageList, | ||
// encoding: Option<TransactionWitnessSetEncoding>, | ||
) -> Result<Option<ScriptDataHash>, JsError> { | ||
cml_chain::crypto::hash::calc_script_data_hash( | ||
redeemers.as_ref(), | ||
datums.as_ref(), | ||
cost_models.as_ref(), | ||
used_langs.as_ref(), | ||
None, | ||
) | ||
.map(|sdh| sdh.map(Into::into)) | ||
.map_err(Into::into) | ||
} | ||
|
||
/// Calculates the hash for script data from a witness if it is necessary. | ||
/// Returns None if it was not necessary (no datums/redeemers) to include. | ||
/// | ||
/// Most users will not directly need this as when using the builders | ||
/// it will be invoked for you. | ||
#[wasm_bindgen] | ||
pub fn calc_script_data_hash_from_witness( | ||
witnesses: &TransactionWitnessSet, | ||
cost_models: &CostModels, | ||
) -> Result<Option<ScriptDataHash>, JsError> { | ||
cml_chain::crypto::hash::calc_script_data_hash_from_witness( | ||
witnesses.as_ref(), | ||
cost_models.as_ref(), | ||
) | ||
.map(|sdh| sdh.map(Into::into)) | ||
.map_err(Into::into) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use wasm_bindgen::prelude::{wasm_bindgen, JsError}; | ||
|
||
use crate::{byron::AddressContent, crypto::BootstrapWitness, Vkeywitness}; | ||
|
||
use cml_crypto_wasm::{PrivateKey, TransactionHash}; | ||
|
||
#[wasm_bindgen] | ||
impl BootstrapWitness { | ||
pub fn to_address(&self) -> Result<AddressContent, JsError> { | ||
self.0.to_address().map(Into::into).map_err(Into::into) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn make_vkey_witness(tx_body_hash: &TransactionHash, sk: &PrivateKey) -> Vkeywitness { | ||
cml_chain::crypto::utils::make_vkey_witness(tx_body_hash.as_ref(), sk.as_ref()).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters