diff --git a/packages/sidan-csl-rs/src/core/core_csl.rs b/packages/sidan-csl-rs/src/core/core_csl.rs index a29e4c5..1aad003 100644 --- a/packages/sidan-csl-rs/src/core/core_csl.rs +++ b/packages/sidan-csl-rs/src/core/core_csl.rs @@ -166,6 +166,11 @@ impl MeshCSL { output_builder = output_builder.with_plutus_data(&csl::PlutusData::from_hex(&data)?); } + Datum::Embedded(data) => { + let datum = &csl::PlutusData::from_hex(&data)?; + output_builder = output_builder.with_data_hash(&csl::hash_plutus_data(datum)); + self.tx_builder.add_extra_witness_datum(datum); + } }; } diff --git a/packages/sidan-csl-rs/src/core/tx_parser/mod.rs b/packages/sidan-csl-rs/src/core/tx_parser/mod.rs index 6eaeb5d..60c69b0 100644 --- a/packages/sidan-csl-rs/src/core/tx_parser/mod.rs +++ b/packages/sidan-csl-rs/src/core/tx_parser/mod.rs @@ -81,6 +81,10 @@ impl MeshTxParser { let plutus_data = Some(data); (None, plutus_data) } + Some(Datum::Embedded(data)) => { + let data_hash = Some(data); + (data_hash, None) + } None => (None, None), }; let tx_out_utxo: UTxO = UTxO { diff --git a/packages/sidan-csl-rs/src/core/utils/value.rs b/packages/sidan-csl-rs/src/core/utils/value.rs index 1a2b422..a44fab8 100644 --- a/packages/sidan-csl-rs/src/core/utils/value.rs +++ b/packages/sidan-csl-rs/src/core/utils/value.rs @@ -13,9 +13,15 @@ pub fn get_min_utxo_value(output: &Output, coins_per_utxo_size: &u64) -> Result< tx_output_builder = tx_output_builder.with_plutus_data(&csl::PlutusData::from_hex(str_data)?); } - Datum::Hash(str_data_hash) => { - tx_output_builder = - tx_output_builder.with_data_hash(&csl::DataHash::from_hex(str_data_hash)?); + Datum::Hash(str_data) => { + tx_output_builder = tx_output_builder.with_data_hash(&csl::hash_plutus_data( + &csl::PlutusData::from_hex(&str_data)?, + )); + } + Datum::Embedded(str_data) => { + tx_output_builder = tx_output_builder.with_data_hash(&csl::hash_plutus_data( + &csl::PlutusData::from_hex(&str_data)?, + )); } }, None => {} diff --git a/packages/sidan-csl-rs/src/model/tx_builder_types/datum.rs b/packages/sidan-csl-rs/src/model/tx_builder_types/datum.rs index c448ca2..796c5d6 100644 --- a/packages/sidan-csl-rs/src/model/tx_builder_types/datum.rs +++ b/packages/sidan-csl-rs/src/model/tx_builder_types/datum.rs @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize}; pub enum Datum { Inline(String), Hash(String), + Embedded(String), } impl Datum { @@ -12,6 +13,7 @@ impl Datum { match self { Datum::Inline(s) => s, Datum::Hash(s) => s, + Datum::Embedded(s) => s, } } -} \ No newline at end of file +} diff --git a/packages/whisky/src/builder/tx_out.rs b/packages/whisky/src/builder/tx_out.rs index b9257ac..51e9c1e 100644 --- a/packages/whisky/src/builder/tx_out.rs +++ b/packages/whisky/src/builder/tx_out.rs @@ -61,6 +61,35 @@ impl MeshTxBuilder { self } + /// ## Transaction building method + /// + /// Set the transaction output embedded datum value in the MeshTxBuilder instance + /// + /// ### Arguments + /// + /// * `data` - The embedded datum value + /// + /// ### Returns + /// + /// * `Self` - The MeshTxBuilder instance + pub fn tx_out_datum_embed_value(&mut self, data: &WData) -> &mut Self { + let tx_output = self.tx_output.take(); + if tx_output.is_none() { + panic!("Undefined output") + } + let mut tx_output = tx_output.unwrap(); + match data.to_cbor() { + Ok(raw_data) => { + tx_output.datum = Some(Datum::Embedded(raw_data)); + self.tx_output = Some(tx_output); + } + Err(_) => { + panic!("Error converting datum to CBOR"); + } + } + self + } + /// ## Transaction building method /// /// Set the transaction output inline datum value in the MeshTxBuilder instance diff --git a/packages/whisky/tests/integration_tests.rs b/packages/whisky/tests/integration_tests.rs index e64833d..6d67b2e 100644 --- a/packages/whisky/tests/integration_tests.rs +++ b/packages/whisky/tests/integration_tests.rs @@ -5,7 +5,7 @@ mod int_tests { model::{Asset, Budget, LanguageVersion}, }; use whisky::{ - builder::{ MeshTxBuilder, MeshTxBuilderParam, WData::JSON, WRedeemer}, + builder::{ MeshTxBuilder, MeshTxBuilderParam, WData::{self, JSON}, WRedeemer}, core::utils::merge_vkey_witnesses_to_transaction, }; @@ -462,6 +462,37 @@ mod int_tests { .complete_signing() .unwrap(); - println!("{}", unsigned_tx) + println!("{}", unsigned_tx); + assert!(mesh.core.mesh_csl.tx_hex != *""); + } + + #[test] + fn test_embedded_datum_output() { + let mut mesh = MeshTxBuilder::new(MeshTxBuilderParam { + evaluator: None, + fetcher: None, + submitter: None, + params: None, + }); + let signed_tx = mesh + .tx_in( + "2cb57168ee66b68bd04a0d595060b546edf30c04ae1031b883c9ac797967dd85", + 3, + &[Asset::new_from_str("lovelace", "9891607895")], + "addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh", + ) + .tx_out("addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh", &[Asset::new_from_str("lovelace", "2000000")]) + .tx_out_datum_embed_value(&WData::JSON(json!({ + "constructor": 0, + "fields": [] + }).to_string())) + .change_address("addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh") + .signing_key("51022b7e38be01d1cc581230e18030e6e1a3e949a1fdd2aeae5f5412154fe82b") + .complete_sync(None) + .unwrap() + .complete_signing().unwrap(); + + println!("{}", signed_tx); + assert!(mesh.core.mesh_csl.tx_hex != *""); } }