whisky_csl/tx_parser/
mod.rs

1mod certificates;
2mod change_address;
3mod change_datum;
4mod collaterals;
5mod context;
6mod inputs;
7mod metadata;
8mod mints;
9mod outputs;
10mod parsable;
11mod reference_inputs;
12mod required_signatures;
13mod static_methods;
14mod utxo_converter;
15mod validity_range;
16mod votes;
17mod withdrawals;
18
19use crate::tx_parser::context::ParserContext;
20use cardano_serialization_lib::{self as csl};
21use serde::{Deserialize, Serialize};
22use whisky_common::*;
23
24#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct CSLParser {
27    pub tx_body: TxBuilderBody,
28    pub csl_tx_body: csl::TransactionBody,
29    pub csl_witness_set: csl::TransactionWitnessSet,
30    pub csl_aux_data: Option<csl::AuxiliaryData>,
31    pub context: ParserContext,
32    pub tx_hash: String,
33}
34
35impl CSLParser {
36    pub fn new() -> CSLParser {
37        CSLParser {
38            tx_body: TxBuilderBody::new(),
39            csl_tx_body: csl::TransactionBody::new_tx_body(
40                &csl::TransactionInputs::new(),
41                &csl::TransactionOutputs::new(),
42                &csl::Coin::zero(),
43            ),
44            csl_witness_set: csl::TransactionWitnessSet::new(),
45            csl_aux_data: None,
46            context: ParserContext::new(),
47            tx_hash: "".to_string(),
48        }
49    }
50
51    pub fn parse(&mut self, tx_hex: &str, resolved_utxos: &[UTxO]) -> Result<&mut Self, WError> {
52        let csl_tx = csl::FixedTransaction::from_hex(tx_hex).map_err(|e| {
53            WError::new(
54                "CSLParser - new",
55                &format!("Failed to parse transaction hex: {:?}", e),
56            )
57        })?;
58        let tx_hash = csl_tx.transaction_hash().to_hex();
59
60        let csl_tx_body = csl_tx.body();
61        let csl_witness_set = csl_tx.witness_set();
62        let csl_aux_data = csl_tx.auxiliary_data();
63
64        let tx_body = TxBuilderBody::new();
65
66        let mut context = ParserContext::new();
67        context
68            .fill_resolved_utxos(&csl_tx_body, resolved_utxos)
69            .map_err(|e| {
70                WError::new(
71                    "CSLParser - new - fill_resolved_utxos",
72                    &format!("Failed to fill resolved UTxOs: {:?}", e),
73                )
74            })?;
75        context
76            .collect_script_witnesses_from_tx_witnesses_set(csl_witness_set.clone())
77            .map_err(|e| {
78                WError::new(
79                    "CSLParser - new - collect_script_witnesses_from_tx_witnesses_set",
80                    &format!(
81                        "Failed to collect script witnesses from witness set: {:?}",
82                        e
83                    ),
84                )
85            })?;
86        context
87            .collect_script_witnesses_from_tx_body(csl_tx_body.clone())
88            .map_err(|e| {
89                WError::new(
90                    "CSLParser - new - collect_script_witnesses_from_tx_body",
91                    &format!("Failed to collect script witnesses from tx body: {:?}", e),
92                )
93            })?;
94
95        self.tx_body = tx_body;
96        self.csl_tx_body = csl_tx_body;
97        self.csl_witness_set = csl_witness_set;
98        self.csl_aux_data = csl_aux_data;
99        self.context = context;
100        self.tx_hash = tx_hash;
101
102        self.extract_inputs()
103            .map_err(WError::from_err("CSLParser - new - inputs"))?;
104        self.extract_outputs()
105            .map_err(WError::from_err("CSLParser - new - outputs"))?;
106        self.extract_collaterals()
107            .map_err(WError::from_err("CSLParser - new - collaterals"))?;
108        self.extract_required_signatures()
109            .map_err(WError::from_err("CSLParser - new - required_signatures"))?;
110        self.extract_reference_inputs()
111            .map_err(WError::from_err("CSLParser - new - reference_inputs"))?;
112        self.extract_withdrawals()
113            .map_err(WError::from_err("CSLParser - new - withdrawals"))?;
114        self.extract_mints()
115            .map_err(WError::from_err("CSLParser - new - mints"))?;
116        self.extract_change_address()
117            .map_err(WError::from_err("CSLParser - new - change_address"))?;
118        self.extract_change_datum()
119            .map_err(WError::from_err("CSLParser - new - change_datum"))?;
120        self.extract_metadata()
121            .map_err(WError::from_err("CSLParser - new - metadata"))?;
122        self.extract_validity_range()
123            .map_err(WError::from_err("CSLParser - new - validity_range"))?;
124        self.extract_certificates()
125            .map_err(WError::from_err("CSLParser - new - certificates"))?;
126        self.extract_votes()
127            .map_err(WError::from_err("CSLParser - new - votes"))?;
128
129        Ok(self)
130    }
131}