whisky_pallas/wrapper/transaction_body/
transaction_input.rs

1use pallas::crypto::hash::Hash;
2use pallas::ledger::primitives::conway::TransactionInput as PallasTransactionInput;
3use pallas::ledger::primitives::Fragment;
4use whisky_common::WError;
5
6#[derive(Hash, Eq, Debug, PartialEq, Clone)]
7pub struct TransactionInput {
8    pub inner: PallasTransactionInput,
9}
10
11impl TransactionInput {
12    pub fn new(transaction_id: &str, index: u64) -> Result<Self, WError> {
13        let digest: Hash<32> = transaction_id.parse().map_err(|_| {
14            WError::new(
15                "WhiskyPallas - Serializing transaction input:",
16                "Invalid transaction id length",
17            )
18        })?;
19
20        let inner = PallasTransactionInput {
21            transaction_id: digest,
22            index,
23        };
24
25        Ok(Self { inner })
26    }
27
28    pub fn encode(&self) -> Result<String, WError> {
29        self.inner
30            .encode_fragment()
31            .map(|bytes| hex::encode(bytes))
32            .map_err(|_| {
33                WError::new(
34                    "WhiskyPallas - Encoding transaction input:",
35                    "Failed to encode fragment",
36                )
37            })
38    }
39
40    pub fn decode_bytes(bytes: &[u8]) -> Result<Self, WError> {
41        let inner = PallasTransactionInput::decode_fragment(bytes).map_err(|e| {
42            WError::new(
43                "WhiskyPallas - Decoding transaction input:",
44                &format!("Fragment decode error: {}", e.to_string()),
45            )
46        })?;
47        Ok(Self { inner })
48    }
49}