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_hex: String,
33    pub tx_hash: String,
34}
35
36impl CSLParser {
37    pub fn new() -> CSLParser {
38        CSLParser {
39            tx_body: TxBuilderBody::new(),
40            csl_tx_body: csl::TransactionBody::new_tx_body(
41                &csl::TransactionInputs::new(),
42                &csl::TransactionOutputs::new(),
43                &csl::Coin::zero(),
44            ),
45            csl_witness_set: csl::TransactionWitnessSet::new(),
46            csl_aux_data: None,
47            context: ParserContext::new(),
48            tx_hex: "".to_string(),
49            tx_hash: "".to_string(),
50        }
51    }
52
53    pub fn new_with_body(tx_hex: &str) -> Result<CSLParser, WError> {
54        let csl_tx = csl::FixedTransaction::from_hex(tx_hex).map_err(|e| {
55            WError::new(
56                "CSLParser - new",
57                &format!("Failed to parse transaction hex: {:?}", e),
58            )
59        })?;
60        let mut parser = CSLParser {
61            tx_body: TxBuilderBody::new(),
62            csl_tx_body: csl::TransactionBody::new_tx_body(
63                &csl::TransactionInputs::new(),
64                &csl::TransactionOutputs::new(),
65                &csl::Coin::zero(),
66            ),
67            csl_witness_set: csl::TransactionWitnessSet::new(),
68            csl_aux_data: None,
69            context: ParserContext::new(),
70            tx_hex: tx_hex.to_string(),
71            tx_hash: "".to_string(),
72        };
73
74        let tx_hash = csl_tx.transaction_hash().to_hex();
75        let csl_tx_body = csl_tx.body();
76        let csl_witness_set = csl_tx.witness_set();
77        let csl_aux_data = csl_tx.auxiliary_data();
78
79        parser.csl_tx_body = csl_tx_body;
80        parser.csl_witness_set = csl_witness_set;
81        parser.csl_aux_data = csl_aux_data;
82        parser.tx_hash = tx_hash;
83
84        Ok(parser)
85    }
86
87    pub fn parse(&mut self, tx_hex: &str, resolved_utxos: &[UTxO]) -> Result<&mut Self, WError> {
88        let csl_tx = csl::FixedTransaction::from_hex(tx_hex).map_err(|e| {
89            WError::new(
90                "CSLParser - new",
91                &format!("Failed to parse transaction hex: {:?}", e),
92            )
93        })?;
94        let tx_hash = csl_tx.transaction_hash().to_hex();
95
96        let csl_tx_body = csl_tx.body();
97        let csl_witness_set = csl_tx.witness_set();
98        let csl_aux_data = csl_tx.auxiliary_data();
99
100        let tx_body = TxBuilderBody::new();
101
102        let mut context = ParserContext::new();
103        context
104            .fill_resolved_utxos(&csl_tx_body, resolved_utxos)
105            .map_err(|e| {
106                WError::new(
107                    "CSLParser - new - fill_resolved_utxos",
108                    &format!("Failed to fill resolved UTxOs: {:?}", e),
109                )
110            })?;
111        context
112            .collect_script_witnesses_from_tx_witnesses_set(csl_witness_set.clone())
113            .map_err(|e| {
114                WError::new(
115                    "CSLParser - new - collect_script_witnesses_from_tx_witnesses_set",
116                    &format!(
117                        "Failed to collect script witnesses from witness set: {:?}",
118                        e
119                    ),
120                )
121            })?;
122        context
123            .collect_script_witnesses_from_tx_body(csl_tx_body.clone())
124            .map_err(|e| {
125                WError::new(
126                    "CSLParser - new - collect_script_witnesses_from_tx_body",
127                    &format!("Failed to collect script witnesses from tx body: {:?}", e),
128                )
129            })?;
130
131        self.tx_body = tx_body;
132        self.csl_tx_body = csl_tx_body;
133        self.csl_witness_set = csl_witness_set;
134        self.csl_aux_data = csl_aux_data;
135        self.context = context;
136        self.tx_hex = tx_hex.to_string();
137        self.tx_hash = tx_hash;
138
139        self.extract_inputs()
140            .map_err(WError::from_err("CSLParser - new - inputs"))?;
141        self.extract_outputs()
142            .map_err(WError::from_err("CSLParser - new - outputs"))?;
143        self.extract_collaterals()
144            .map_err(WError::from_err("CSLParser - new - collaterals"))?;
145        self.extract_required_signatures()
146            .map_err(WError::from_err("CSLParser - new - required_signatures"))?;
147        self.extract_reference_inputs()
148            .map_err(WError::from_err("CSLParser - new - reference_inputs"))?;
149        self.extract_withdrawals()
150            .map_err(WError::from_err("CSLParser - new - withdrawals"))?;
151        self.extract_mints()
152            .map_err(WError::from_err("CSLParser - new - mints"))?;
153        self.extract_change_address()
154            .map_err(WError::from_err("CSLParser - new - change_address"))?;
155        self.extract_change_datum()
156            .map_err(WError::from_err("CSLParser - new - change_datum"))?;
157        self.extract_metadata()
158            .map_err(WError::from_err("CSLParser - new - metadata"))?;
159        self.extract_validity_range()
160            .map_err(WError::from_err("CSLParser - new - validity_range"))?;
161        self.extract_certificates()
162            .map_err(WError::from_err("CSLParser - new - certificates"))?;
163        self.extract_votes()
164            .map_err(WError::from_err("CSLParser - new - votes"))?;
165
166        Ok(self)
167    }
168}