whisky_csl/tx_parser/
inputs.rs
1use cardano_serialization_lib as csl;
2use whisky_common::{TxIn, UtxoInput, WError};
3
4use super::{utxo_converter::utxo_to_tx_in, CSLParser};
5
6impl CSLParser {
7 pub fn get_inputs(&self) -> &Vec<TxIn> {
8 &self.tx_body.inputs
9 }
10
11 pub fn extract_all_required_inputs(tx_hex: &str) -> Result<Vec<String>, WError> {
12 let csl_tx = csl::FixedTransaction::from_hex(tx_hex).map_err(|e| {
13 WError::new(
14 "CSLParser - extract_all_required_inputs",
15 &format!("Failed to parse transaction hex: {:?}", e),
16 )
17 })?;
18 let inputs = csl_tx.body().inputs();
19 let mut required_inputs = Vec::new();
20 for input in inputs.into_iter() {
21 required_inputs.push(format!(
22 "{}#{}",
23 input.transaction_id().to_hex(),
24 input.index()
25 ));
26 }
27 let collateral_inputs = csl_tx.body().collateral();
28 if let Some(collateral_inputs) = collateral_inputs {
29 for input in collateral_inputs.into_iter() {
30 required_inputs.push(format!(
31 "{}#{}",
32 input.transaction_id().to_hex(),
33 input.index()
34 ));
35 }
36 }
37 let reference_inputs = csl_tx.body().reference_inputs();
38 if let Some(reference_inputs) = reference_inputs {
39 for input in reference_inputs.into_iter() {
40 required_inputs.push(format!(
41 "{}#{}",
42 input.transaction_id().to_hex(),
43 input.index()
44 ));
45 }
46 }
47 Ok(required_inputs)
48 }
49
50 pub fn extract_all_required_utxo_input(tx_hex: &str) -> Result<Vec<UtxoInput>, WError> {
51 let required_inputs = CSLParser::extract_all_required_inputs(tx_hex)?;
52 required_inputs
53 .iter()
54 .map(|input| {
55 let parts: Vec<&str> = input.split('#').collect();
56 if parts.len() != 2 {
57 return Err(WError::new(
58 "CSLParser - extract_all_required_utxo_input",
59 &format!("Invalid input format: {}", input),
60 ));
61 }
62 let tx_hash = parts[0].to_string();
63 let output_index: u32 = parts[1].parse().map_err(|_| {
64 WError::new(
65 "CSLParser - extract_all_required_utxo_input",
66 "Invalid output index",
67 )
68 })?;
69 Ok(UtxoInput {
70 tx_hash,
71 output_index,
72 })
73 })
74 .collect()
75 }
76
77 pub(super) fn extract_inputs(&mut self) -> Result<(), WError> {
78 let inputs = self.csl_tx_body.inputs();
79 for (index, input) in inputs.into_iter().enumerate() {
80 let tx_in = utxo_to_tx_in(&input, &self.context, index)?;
81 self.tx_body.inputs.push(tx_in);
82 }
83 Ok(())
84 }
85}