Skip to content

Commit

Permalink
add support for embedded datums in output
Browse files Browse the repository at this point in the history
  • Loading branch information
twwu123 committed Aug 30, 2024
1 parent 15849da commit 561a44c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/sidan-csl-rs/src/core/core_csl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/sidan-csl-rs/src/core/tx_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions packages/sidan-csl-rs/src/core/utils/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
Expand Down
4 changes: 3 additions & 1 deletion packages/sidan-csl-rs/src/model/tx_builder_types/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use serde::{Deserialize, Serialize};
pub enum Datum {
Inline(String),
Hash(String),
Embedded(String),
}

impl Datum {
pub fn get_inner(&self) -> &str {
match self {
Datum::Inline(s) => s,
Datum::Hash(s) => s,
Datum::Embedded(s) => s,
}
}
}
}
29 changes: 29 additions & 0 deletions packages/whisky/src/builder/tx_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 33 additions & 2 deletions packages/whisky/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 != *"");
}
}

0 comments on commit 561a44c

Please sign in to comment.