whisky_pallas/wrapper/transaction_body/
voter.rs

1use std::str::FromStr;
2
3use pallas::crypto::hash::Hash;
4use pallas::ledger::primitives::conway::Voter as PallasVoter;
5use pallas::ledger::primitives::Fragment;
6use whisky_common::WError;
7
8pub enum VoterKind {
9    ConstitutionalCommitteScript { script_hash: String },
10    ConstitutionalCommitteKey { key_hash: String },
11    DrepScript { script_hash: String },
12    DrepKey { key_hash: String },
13    StakePoolKey { pool_key_hash: String },
14}
15
16#[derive(Debug, PartialEq, Eq, Clone)]
17pub struct Voter {
18    pub inner: PallasVoter,
19}
20
21impl Voter {
22    pub fn new(voter: VoterKind) -> Result<Self, WError> {
23        let pallas_voter = match voter {
24            VoterKind::ConstitutionalCommitteScript { script_hash } => {
25                PallasVoter::ConstitutionalCommitteeScript(
26                    Hash::<28>::from_str(&script_hash).map_err(|e| {
27                        WError::new("Voter::new", &format!("Invalid script hash length: {}", e))
28                    })?,
29                )
30            }
31
32            VoterKind::ConstitutionalCommitteKey { key_hash } => {
33                PallasVoter::ConstitutionalCommitteeKey(Hash::<28>::from_str(&key_hash).map_err(
34                    |e| WError::new("Voter::new", &format!("Invalid key hash length: {}", e)),
35                )?)
36            }
37
38            VoterKind::DrepScript { script_hash } => {
39                PallasVoter::DRepScript(Hash::<28>::from_str(&script_hash).map_err(|e| {
40                    WError::new("Voter::new", &format!("Invalid script hash length: {}", e))
41                })?)
42            }
43
44            VoterKind::DrepKey { key_hash } => {
45                PallasVoter::DRepKey(Hash::<28>::from_str(&key_hash).map_err(|e| {
46                    WError::new("Voter::new", &format!("Invalid key hash length: {}", e))
47                })?)
48            }
49
50            VoterKind::StakePoolKey { pool_key_hash } => {
51                PallasVoter::StakePoolKey(Hash::<28>::from_str(&pool_key_hash).map_err(|e| {
52                    WError::new(
53                        "Voter::new",
54                        &format!("Invalid pool key hash length: {}", e),
55                    )
56                })?)
57            }
58        };
59
60        Ok(Self {
61            inner: pallas_voter,
62        })
63    }
64
65    pub fn encode(&self) -> Result<String, WError> {
66        let encoded = self
67            .inner
68            .encode_fragment()
69            .map_err(|e| WError::new("Voter::encode", &format!("Fragment encode error: {}", e)))?;
70        Ok(hex::encode(encoded))
71    }
72
73    pub fn decode_bytes(bytes: &[u8]) -> Result<Self, WError> {
74        let inner = PallasVoter::decode_fragment(&bytes).map_err(|e| {
75            WError::new(
76                "Voter::decode_bytes",
77                &format!("Fragment decode error: {}", e),
78            )
79        })?;
80        Ok(Self { inner })
81    }
82}