diff --git a/Cargo.lock b/Cargo.lock index 26fa450..cab7020 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -697,7 +697,7 @@ dependencies = [ [[package]] name = "sidan-csl-rs" -version = "0.1.4" +version = "0.1.5" dependencies = [ "cardano-serialization-lib", "getrandom", diff --git a/Cargo.toml b/Cargo.toml index b835eb4..2ef8eb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sidan-csl-rs" -version = "0.1.4" +version = "0.1.5" edition = "2021" license = "MIT" description = "Wrapper around the cardano-serialization-lib for easier transaction building, heavily inspired by cardano-cli APIs" diff --git a/src/utils/aiken.rs b/src/utils/aiken.rs index 6acde09..a58d19c 100644 --- a/src/utils/aiken.rs +++ b/src/utils/aiken.rs @@ -1,7 +1,8 @@ use cardano_serialization_lib::{ error::JsError, - plutus::{PlutusList, PlutusScript}, + plutus::{PlutusData, PlutusDatumSchema, PlutusList, PlutusScript}, }; +use std::error::Error; #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] use noop_proc_macro::wasm_bindgen; @@ -9,6 +10,65 @@ use noop_proc_macro::wasm_bindgen; #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; +use super::parser::{bytes_to_hex, hex_to_bytes}; + +pub fn apply_double_cbor_encoding(script: &str) -> Result> { + let bytes: Vec = hex_to_bytes(script).unwrap(); + + match PlutusScript::from_bytes(bytes.clone()) { + Ok(single_encoded_script) => { + match PlutusScript::from_bytes(single_encoded_script.bytes()) { + Ok(_) => Ok(script.to_string()), + Err(_) => { + let bytes = PlutusScript::new(bytes).to_bytes(); + let new_script = bytes_to_hex(&bytes); + Ok(new_script) + } + } + } + Err(err) => Err(Box::new(err)), + } +} + +#[test] +fn test_apply_double_cbor_encoding() { + let script = + "584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441"; + assert_eq!( + apply_double_cbor_encoding(script).unwrap(), + "5847584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441" + ); +} + +#[wasm_bindgen] +pub fn apply_params_to_script( + params_to_apply: Vec, + plutus_script: String, +) -> Result> { + let double_encoded_script = apply_double_cbor_encoding(&plutus_script).unwrap(); + let plutus_script = + PlutusScript::from_bytes(hex_to_bytes(&double_encoded_script).unwrap()).unwrap(); + let mut plutus_list = PlutusList::new(); + for param in ¶ms_to_apply { + let plutus_data = PlutusData::from_json(param, PlutusDatumSchema::DetailedSchema).unwrap(); + plutus_list.add(&plutus_data); + } + let bytes = apply_params_to_plutus_script(&plutus_list, plutus_script)?.to_bytes(); + Ok(bytes_to_hex(&bytes)) +} + +#[test] +fn test_apply_params_to_script() { + use serde_json::{json, to_string}; + let script = + "584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441"; + let params = vec![to_string(&json!({ "bytes": "1234"})).unwrap()]; + assert_eq!( + apply_params_to_script(params, script.to_string()).unwrap(), + "584f584d010000332323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157449801034212340001" + ); +} + #[wasm_bindgen] pub fn apply_params_to_plutus_script( params: &PlutusList,