sidan_csl_rs/core/utils/
aiken.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::{
    core::common::{bytes_to_hex, hex_to_bytes},
    csl::{
        JsError, {PlutusData, PlutusDatumSchema, PlutusList, PlutusScript},
    },
    model::JsVecString,
    *,
};

use self::model::BuilderDataType;

pub fn apply_double_cbor_encoding(script: &str) -> Result<String, JsError> {
    let bytes: Vec<u8> = 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(JsError::from(err)),
    }
}

#[test]
fn test_apply_double_cbor_encoding() {
    let script =
      "584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441";
    assert_eq!(
      apply_double_cbor_encoding(script).unwrap(),
      "5847584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441"
  );
}

#[wasm_bindgen]
pub fn js_apply_params_to_script(
    plutus_script: &str,
    params: JsVecString,
    param_type: BuilderDataType,
) -> Result<String, JsError> {
    let mut params_to_apply: Vec<&str> = vec![];
    for param in params.iter() {
        params_to_apply.push(param);
    }
    let param_script = apply_params_to_script(plutus_script, &params_to_apply, param_type)?;
    Ok(param_script)
}

pub fn apply_params_to_script(
    plutus_script: &str,
    params_to_apply: &[&str],
    param_type: BuilderDataType,
) -> Result<String, JsError> {
    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 params_to_apply {
        match param_type {
            BuilderDataType::JSON => {
                let plutus_data =
                    PlutusData::from_json(param, PlutusDatumSchema::DetailedSchema).unwrap();
                plutus_list.add(&plutus_data);
            }
            BuilderDataType::CBOR => {
                let plutus_data = PlutusData::from_hex(param).unwrap();
                plutus_list.add(&plutus_data);
            }
        }
    }
    let bytes = apply_params_to_plutus_script(&plutus_list, plutus_script)?.to_bytes();
    Ok(bytes_to_hex(&bytes))
}

pub fn apply_params_to_plutus_script(
    params: &PlutusList,
    plutus_script: PlutusScript,
) -> Result<PlutusScript, JsError> {
    match uplc::tx::apply_params_to_script(&params.to_bytes(), &plutus_script.bytes()) {
        Ok(res) => Ok(PlutusScript::new(res)),
        Err(err) => Err(JsError::from_str(&err.to_string())),
    }
}