whisky_pallas/wrapper/transaction_body/
gov_action_id.rs

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