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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use chain_core::{
    packer::Codec,
    property::{DeserializeFromSlice, ReadError, Serialize, WriteError},
};
use chain_crypto::{Ed25519, PublicKey};
use std::{
    fmt::{self, Debug, Display},
    str::FromStr,
};
use thiserror::Error;

/// committee identifier
///
/// this value is used to identify a committee member on chain
/// as well as to use as input for the vote casting payload.
///
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct CommitteeId([u8; CommitteeId::COMMITTEE_ID_SIZE]);

impl CommitteeId {
    pub const COMMITTEE_ID_SIZE: usize = 32;

    /// returns the identifier encoded in hexadecimal string
    pub fn to_hex(self) -> String {
        hex::encode(self.0)
    }

    /// read the identifier from the hexadecimal string
    pub fn from_hex(s: &str) -> Result<Self, hex::FromHexError> {
        let mut bytes = [0; Self::COMMITTEE_ID_SIZE];
        hex::decode_to_slice(s, &mut bytes)?;
        Ok(CommitteeId(bytes))
    }

    pub fn public_key(&self) -> PublicKey<Ed25519> {
        (*self).into()
    }
}

/* Conversion ************************************************************** */

impl From<PublicKey<Ed25519>> for CommitteeId {
    fn from(key: PublicKey<Ed25519>) -> Self {
        Self::try_from(key.as_ref()).unwrap()
    }
}

impl From<[u8; Self::COMMITTEE_ID_SIZE]> for CommitteeId {
    fn from(id: [u8; Self::COMMITTEE_ID_SIZE]) -> Self {
        Self(id)
    }
}

impl From<CommitteeId> for [u8; CommitteeId::COMMITTEE_ID_SIZE] {
    fn from(id: CommitteeId) -> Self {
        id.0
    }
}

impl From<CommitteeId> for PublicKey<Ed25519> {
    fn from(id: CommitteeId) -> Self {
        PublicKey::from_binary(id.0.as_ref())
            .expect("CommitteeId should be a valid Ed25519 public key")
    }
}

/// error that can be received when converting a slice into a
/// [`CommitteeId`].
///
/// [`CommitteeId`]: ./struct.CommitteeId.html
///
#[derive(Debug, Error)]
#[error("Not enough bytes, expected {expected} but received {received}")]
pub struct TryFromCommitteeIdError {
    expected: usize,
    received: usize,
}

impl<'a> TryFrom<&'a [u8]> for CommitteeId {
    type Error = TryFromCommitteeIdError;
    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        if value.len() != Self::COMMITTEE_ID_SIZE {
            Err(TryFromCommitteeIdError {
                expected: Self::COMMITTEE_ID_SIZE,
                received: value.len(),
            })
        } else {
            let mut committee_id = Self([0; Self::COMMITTEE_ID_SIZE]);
            committee_id.0.copy_from_slice(value);
            Ok(committee_id)
        }
    }
}

/* AsRef ******************************************************************* */

impl AsRef<[u8]> for CommitteeId {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

/* Display ***************************************************************** */

impl Display for CommitteeId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_hex())
    }
}

impl Debug for CommitteeId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("CommitteeId").field(&self.to_hex()).finish()
    }
}

/* FromStr ***************************************************************** */

impl FromStr for CommitteeId {
    type Err = hex::FromHexError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_hex(s)
    }
}

/* Ser/De ****************************************************************** */

impl Serialize for CommitteeId {
    fn serialized_size(&self) -> usize {
        self.as_ref().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        codec.put_bytes(self.as_ref())
    }
}

impl DeserializeFromSlice for CommitteeId {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let slice = codec.get_slice(Self::COMMITTEE_ID_SIZE)?;
        Self::try_from(slice).map_err(|err| ReadError::StructureInvalid(err.to_string()))
    }
}

#[cfg(any(test, feature = "property-test-api"))]
mod tests {
    use super::*;
    use quickcheck::{Arbitrary, Gen};
    use quickcheck_macros::quickcheck;

    impl Arbitrary for CommitteeId {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let mut bytes = [0; Self::COMMITTEE_ID_SIZE];
            g.fill_bytes(&mut bytes);
            Self(bytes)
        }
    }

    #[quickcheck]
    fn to_from_hex(committee_id: CommitteeId) -> bool {
        let s = committee_id.to_hex();
        let d = CommitteeId::from_hex(&s).expect("decode hexadecimal committee id");

        committee_id == d
    }

    #[quickcheck]
    fn display_parse(committee_id: CommitteeId) -> bool {
        let s = committee_id.to_string();
        let d = s.parse().expect("decode hexadecimal committee id");

        committee_id == d
    }

    #[quickcheck]
    fn serialize_readable(committee_id: CommitteeId) -> bool {
        let b_got = committee_id.serialize_as_vec().unwrap();
        let result = CommitteeId::deserialize_from_slice(&mut Codec::new(b_got.as_slice()))
            .expect("decode the committee ID");
        committee_id == result
    }
}