whisky_js/wasm/
tx_parser.rs

1use crate::*;
2use serde_json::json;
3use wasm::WasmResult;
4use whisky_common::*;
5use whisky_csl::*;
6
7#[wasm_bindgen]
8pub fn js_parse_tx_body(tx_hex: &str, resolved_utxos: &JsVecString) -> WasmResult {
9    let mut deserialized_utxos: Vec<UTxO> = Vec::new();
10    for utxo_json in resolved_utxos {
11        match serde_json::from_str(utxo_json.as_str()) {
12            Ok(utxo) => deserialized_utxos.push(utxo),
13            Err(e) => {
14                return WasmResult::new_error(
15                    "failure".to_string(),
16                    format!("Error in decoding UTXO: {:?}", e),
17                );
18            }
19        }
20    }
21    let mut tx_parser = CSLParser::new();
22    let tx_parser = tx_parser.parse(tx_hex, &deserialized_utxos);
23    let builder_body = tx_parser.map(|parser| {
24        let builder_body = parser.tx_body.clone();
25        json!(builder_body).to_string()
26    });
27    WasmResult::from_result(builder_body)
28}
29
30#[wasm_bindgen]
31pub fn js_get_tx_outs_utxo(tx_hex: &str) -> WasmResult {
32    let get_tx_outs_utxo = || -> Result<String, WError> {
33        let tx_outs = CSLParser::extract_output_utxos(tx_hex)?;
34        Ok(json!(tx_outs).to_string())
35    };
36    let res = get_tx_outs_utxo();
37    WasmResult::from_result(res)
38}
39
40#[wasm_bindgen]
41pub fn js_get_required_inputs_to_resolve(tx_hex: &str) -> WasmResult {
42    let get_required_inputs = || -> Result<String, WError> {
43        let required_inputs = CSLParser::extract_all_required_inputs(tx_hex)?;
44        Ok(json!(required_inputs).to_string())
45    };
46    let res = get_required_inputs();
47    WasmResult::from_result(res)
48}