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