sidan_csl_rs/core/utils/
vote.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use cardano_serialization_lib::{self as csl, JsError};

use crate::model::{Credential, VoteKind, Voter};

pub fn to_csl_voter(voter: Voter) -> Result<csl::Voter, JsError> {
    match voter {
        Voter::ConstitutionalCommitteeHotCred(cred) => match cred {
            Credential::KeyHash(key_hash) => {
                Ok(csl::Voter::new_constitutional_committee_hot_credential(
                    &csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(&key_hash)?),
                ))
            }
            Credential::ScriptHash(script_hash) => {
                Ok(csl::Voter::new_constitutional_committee_hot_credential(
                    &csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(&script_hash)?),
                ))
            }
        },
        Voter::DRepId(drep_id) => {
            let drep = csl::DRep::from_bech32(&drep_id).unwrap();
            let drep_credential = if drep.to_script_hash().is_some() {
                csl::Credential::from_scripthash(&drep.to_script_hash().unwrap())
            } else if drep.to_key_hash().is_some() {
                csl::Credential::from_keyhash(&drep.to_key_hash().unwrap())
            } else {
                return Err(JsError::from_str(
                    "Error occured when deserializing DrepId to either script hash or key hash",
                ));
            };
            Ok(csl::Voter::new_drep_credential(&drep_credential))
        }
        Voter::StakingPoolKeyHash(key_hash) => Ok(csl::Voter::new_stake_pool_key_hash(
            &csl::Ed25519KeyHash::from_hex(&key_hash)?,
        )),
    }
}

pub fn to_csl_vote_kind(vote_kind: VoteKind) -> csl::VoteKind {
    match vote_kind {
        VoteKind::No => csl::VoteKind::No,
        VoteKind::Yes => csl::VoteKind::Yes,
        VoteKind::Abstain => csl::VoteKind::Abstain,
    }
}