mithril_common/messages/message_parts/
signer.rs

1use anyhow::Context;
2use serde::{Deserialize, Serialize};
3use std::fmt::{Debug, Formatter};
4
5use crate::{
6    StdError, StdResult,
7    crypto_helper::{KesPeriod, ProtocolOpCert, ProtocolSignerVerificationKeySignature},
8    entities::{
9        HexEncodedOpCert, HexEncodedVerificationKey, HexEncodedVerificationKeySignature, PartyId,
10        Signer, SignerWithStake, Stake,
11    },
12};
13
14/// Signer with Stake Message
15#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
16pub struct SignerWithStakeMessagePart {
17    /// The unique identifier of the signer
18    ///
19    /// Used only for testing when SPO pool id is not certified
20    pub party_id: PartyId,
21
22    /// The public key used to authenticate signer signature
23    pub verification_key: HexEncodedVerificationKey,
24
25    /// The encoded signer 'Mithril verification key' signature (signed by the
26    /// Cardano node KES secret key).
27    ///
28    /// None is used only for testing when SPO pool id is not certified
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub verification_key_signature: Option<HexEncodedVerificationKeySignature>,
31
32    /// The encoded operational certificate of stake pool operator attached to
33    /// the signer node.
34    ///
35    /// None is used only for testing when SPO pool id is not certified
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub operational_certificate: Option<HexEncodedOpCert>,
38
39    /// The KES period used to compute the verification key signature
40    // TODO: This KES period should not be used as is and should probably be
41    //       within an allowed range of KES periods for the epoch.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub kes_period: Option<KesPeriod>,
44
45    /// The signer stake
46    pub stake: Stake,
47}
48
49impl SignerWithStakeMessagePart {
50    /// Convert a set of signers into message parts
51    pub fn from_signers(signers: Vec<SignerWithStake>) -> Vec<Self> {
52        signers.into_iter().map(|signer| signer.into()).collect()
53    }
54
55    /// Convert a set of signer message parts into a set of signers with stake
56    pub fn try_into_signers(messages: Vec<Self>) -> StdResult<Vec<SignerWithStake>> {
57        messages
58            .into_iter()
59            .map(SignerWithStakeMessagePart::try_into)
60            .collect()
61    }
62}
63
64impl TryInto<SignerWithStake> for SignerWithStakeMessagePart {
65    type Error = StdError;
66
67    fn try_into(self) -> Result<SignerWithStake, Self::Error> {
68        let verification_key_signature: Option<ProtocolSignerVerificationKeySignature> = self
69            .verification_key_signature
70            .map(|f| f.try_into())
71            .transpose()
72            .with_context(|| {
73                format!(
74                    "Error while parsing verification key signature message, party_id = '{}'",
75                    self.party_id
76                )
77            })?;
78        let operational_certificate: Option<ProtocolOpCert> = self
79            .operational_certificate
80            .map(|f| f.try_into())
81            .transpose()
82            .with_context(|| {
83                format!(
84                    "Error while parsing operational certificate message, party_id = '{}'.",
85                    self.party_id
86                )
87            })?;
88        let value = SignerWithStake {
89            party_id: self.party_id,
90            verification_key: self.verification_key.try_into()?,
91            verification_key_signature,
92            kes_period: self.kes_period,
93            operational_certificate,
94            stake: self.stake,
95        };
96        Ok(value)
97    }
98}
99
100impl From<SignerWithStake> for SignerWithStakeMessagePart {
101    fn from(value: SignerWithStake) -> Self {
102        Self {
103            party_id: value.party_id,
104            verification_key: value.verification_key.try_into().unwrap(),
105            verification_key_signature: value
106                .verification_key_signature
107                .map(|k| k.try_into().unwrap()),
108            operational_certificate: value
109                .operational_certificate
110                .map(|op_cert| (op_cert.try_into().unwrap())),
111            kes_period: value.kes_period,
112            stake: value.stake,
113        }
114    }
115}
116
117impl Debug for SignerWithStakeMessagePart {
118    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
119        let should_be_exhaustive = f.alternate();
120        let mut debug = f.debug_struct("Signer");
121        debug.field("party_id", &self.party_id).field("stake", &self.stake);
122
123        match should_be_exhaustive {
124            true => debug
125                .field(
126                    "verification_key",
127                    &format_args!("{:?}", self.verification_key),
128                )
129                .field(
130                    "verification_key_signature",
131                    &format_args!("{:?}", self.verification_key_signature),
132                )
133                .field(
134                    "operational_certificate",
135                    &format_args!("{:?}", self.operational_certificate),
136                )
137                .field("kes_period", &format_args!("{:?}", self.kes_period))
138                .finish(),
139            false => debug.finish_non_exhaustive(),
140        }
141    }
142}
143
144/// Signer Message
145#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
146pub struct SignerMessagePart {
147    /// The unique identifier of the signer
148    ///
149    /// Used only for testing when SPO pool id is not certified
150    pub party_id: PartyId,
151
152    /// The public key used to authenticate signer signature
153    pub verification_key: HexEncodedVerificationKey,
154
155    /// The encoded signer 'Mithril verification key' signature (signed by the
156    /// Cardano node KES secret key).
157    ///
158    /// None is used only for testing when SPO pool id is not certified
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub verification_key_signature: Option<HexEncodedVerificationKeySignature>,
161
162    /// The encoded operational certificate of stake pool operator attached to
163    /// the signer node.
164    ///
165    /// None is used only for testing when SPO pool id is not certified
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub operational_certificate: Option<HexEncodedOpCert>,
168
169    /// The KES period used to compute the verification key signature
170    // TODO: This KES period should not be used as is and should probably be
171    //       within an allowed range of KES periods for the epoch.
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub kes_period: Option<KesPeriod>,
174}
175
176impl SignerMessagePart {
177    /// Convert a set of signer message parts into a set of signers
178    pub fn try_into_signers(messages: Vec<Self>) -> StdResult<Vec<Signer>> {
179        messages.into_iter().map(SignerMessagePart::try_into).collect()
180    }
181
182    /// Convert a set of signers into message parts
183    pub fn from_signers(signers: Vec<Signer>) -> Vec<Self> {
184        signers.into_iter().map(|signer| signer.into()).collect()
185    }
186}
187
188impl TryInto<Signer> for SignerMessagePart {
189    type Error = StdError;
190
191    fn try_into(self) -> Result<Signer, Self::Error> {
192        let verification_key_signature: Option<ProtocolSignerVerificationKeySignature> = self
193            .verification_key_signature
194            .map(|f| f.try_into())
195            .transpose()
196            .with_context(|| {
197                format!(
198                    "Error while parsing verification key signature message, party_id = '{}'",
199                    self.party_id
200                )
201            })?;
202        let operational_certificate: Option<ProtocolOpCert> = self
203            .operational_certificate
204            .map(|f| f.try_into())
205            .transpose()
206            .with_context(|| {
207                format!(
208                    "Error while parsing operational certificate message, party_id = '{}'.",
209                    self.party_id
210                )
211            })?;
212        let value = Signer {
213            party_id: self.party_id,
214            verification_key: self.verification_key.try_into()?,
215            verification_key_signature,
216            kes_period: self.kes_period,
217            operational_certificate,
218        };
219        Ok(value)
220    }
221}
222
223impl From<Signer> for SignerMessagePart {
224    fn from(value: Signer) -> Self {
225        Self {
226            party_id: value.party_id,
227            verification_key: value.verification_key.try_into().unwrap(),
228            verification_key_signature: value
229                .verification_key_signature
230                .map(|k| k.try_into().unwrap()),
231            operational_certificate: value
232                .operational_certificate
233                .map(|op_cert| (op_cert.try_into().unwrap())),
234            kes_period: value.kes_period,
235        }
236    }
237}
238
239impl Debug for SignerMessagePart {
240    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
241        let should_be_exhaustive = f.alternate();
242        let mut debug = f.debug_struct("Signer");
243        debug.field("party_id", &self.party_id);
244
245        match should_be_exhaustive {
246            true => debug
247                .field(
248                    "verification_key",
249                    &format_args!("{:?}", self.verification_key),
250                )
251                .field(
252                    "verification_key_signature",
253                    &format_args!("{:?}", self.verification_key_signature),
254                )
255                .field(
256                    "operational_certificate",
257                    &format_args!("{:?}", self.operational_certificate),
258                )
259                .field("kes_period", &format_args!("{:?}", self.kes_period))
260                .finish(),
261            false => debug.finish_non_exhaustive(),
262        }
263    }
264}
265#[cfg(test)]
266mod tests {
267    use super::*;
268
269    mod golden_protocol_key_encodings {
270        use super::*;
271
272        fn golden_signer_message_part_with_json_hex_encoding() -> SignerMessagePart {
273            SignerMessagePart {
274                    party_id: "pool1m8crhnqj5k2kyszf5j2scshupystyxc887zdfrpzh6ty6eun4fx"
275                        .to_string(),
276                    verification_key: "7b22766b223a5b3138342c3134352c3230382c3138382c31342c342c3230342c3135392c33322c3234332c37352c3137392c38322c3133352c3235342c3135372c33312c35392c33382c3131302c3133362c3232352c3233342c3132332c34372c3130322c34322c3132352c3138392c31372c3136322c3234342c37352c3234382c3139352c3232372c3131362c3139322c3135322c39302c34312c32372c3235312c3137322c35332c3137382c3231342c35362c32302c3232372c3139372c31392c3234322c3138362c3130312c322c37332c3234332c31342c3230342c3136342c3133342c3136322c3233332c32392c3131342c33302c3136372c3230372c3137332c36382c362c37362c38302c3233342c36352c36342c3137332c3231372c3232392c34382c3133342c31322c39352c3138362c3233382c3135302c3139322c3138302c3139322c31302c312c3136322c3131372c3131322c3132325d2c22706f70223a5b3137382c3231342c3231342c3139332c3137382c3134372c34332c3132362c31362c3231362c3231352c3133322c3136342c3134362c382c3233382c3234352c38322c3232342c3233372c3234392c3134322c3135372c3232392c32372c3135392c3132312c3233312c3234382c3131312c38352c38352c35302c3139382c3233362c39312c3133302c3133352c36322c3133302c3132352c3134372c3136392c3134352c39352c33352c37382c3235332c3135302c3232352c3232352c3232372c38332c3132352c32382c3137332c3130382c3234312c3230302c37332c3134342c36322c31322c3232392c3134332c3134332c37352c37342c3133352c3135382c3139362c3139362c3232342c3232382c38382c3130352c3132342c34372c37362c3234382c3234342c38362c3136332c3232312c3134372c3134382c36352c3232382c31352c37392c3138352c39302c39362c3139322c3138392c3233365d7d".to_string(),
277                    verification_key_signature: Some(
278                        "7b227369676d61223a7b227369676d61223a7b227369676d61223a7b227369676d61223a7b227369676d61223a7b227369676d61223a5b32312c3131392c302c3232382c3130322c3231362c3137372c3133302c34362c3131362c3132302c3235322c3232342c31322c34332c3139302c32322c3131372c3231362c3132342c3234302c3137332c31392c3234312c3138342c3230392c36342c3130352c39392c3233342c36312c31302c37312c36302c33392c3133392c39382c3133342c3133322c3135372c3133382c33372c3232322c3130362c3131312c37382c3132302c37382c39332c39382c32382c35332c3231322c3138332c3135322c37312c3135312c36332c3136382c3138372c33372c3232372c3133352c31345d2c226c68735f706b223a5b3135372c32312c33362c32392c32302c33342c31372c3233392c31332c38392c33372c3132302c32312c3135352c3233362c3234302c39352c3138392c3136362c3131332c3231332c39302c3138382c34312c3137312c33372c3131312c3138302c3234332c33312c35322c36385d2c227268735f706b223a5b3136302c3234332c39382c34352c342c3232312c36342c3131372c34382c3230332c3138362c34312c39352c31342c3135382c3232382c3137352c3232322c3131362c34342c38342c39372c3136372c3132372c38302c3139372c3234352c3136372c3139332c3139362c3135372c3137375d7d2c226c68735f706b223a5b3138332c3135352c36352c3234322c3233342c34332c3136372c3139322c31362c38322c3130322c39382c3133372c3139362c32382c38312c3232312c38312c36352c322c39332c37302c38342c3136382c3137392c3138372c3130322c32382c3234342c3138322c3132332c3134385d2c227268735f706b223a5b3135352c36392c3134302c32382c3137342c33382c3134392c36302c3137382c3232372c35342c3231312c3130362c3133312c36382c3135362c35392c3138302c35332c382c3138382c3233312c3137332c312c3137312c3131392c33322c3134372c3233342c34322c3136332c3131305d7d2c226c68735f706b223a5b3134312c3137322c3137372c38352c39302c3131312c3135332c3234362c3233332c3139362c3130352c3138342c35342c3234372c3133342c3135382c3232362c3230372c32392c3136332c31392c33342c312c3230372c3232322c37322c3136362c32332c31382c3137342c31362c39365d2c227268735f706b223a5b31322c3231322c3139372c3130382c3233302c38332c33372c3139342c3135382c3135362c38362c3138362c33322c3234352c3234342c34302c3234342c3138302c3136382c3233302c362c3137382c3138362c32322c36382c33392c3230322c35352c3130302c3232392c3138392c3232315d7d2c226c68735f706b223a5b33362c3235302c322c3232392c3233342c3136352c32372c36332c3132302c3137382c342c3138302c3235312c3134372c35302c3134382c3233372c3135342c33382c3134352c3131342c37342c3231312c34382c3133362c3231332c31382c3138322c3230332c3233302c31352c3133345d2c227268735f706b223a5b3139332c3134342c3231332c362c3235352c3137372c3131302c3131302c3231332c3137352c332c32382c3135382c3231362c3137332c32362c3232352c38392c34362c3133382c34392c37342c36332c34332c3134342c34382c34392c39352c35372c31392c3132392c34335d7d2c226c68735f706b223a5b3135362c38312c3130352c3134312c3230392c322c3137352c38332c38332c37342c3138332c3130312c3131362c3137362c31382c3233362c31382c3232332c3233372c3233332c3139332c35342c32382c3231332c302c3133352c3135372c33342c37352c3230352c34382c3133385d2c227268735f706b223a5b33382c3234382c35322c32302c35352c3131382c3138372c33392c3137362c3231332c33342c332c3231342c322c33312c3131382c3232342c3132392c34322c3136332c39342c3130382c3131362c35352c3231332c36372c3133322c39362c3232392c3132322c39362c3136385d7d2c226c68735f706b223a5b3132312c3134342c33312c3131302c3234392c3234342c3139382c3235342c3139312c36322c39312c31372c3135322c3135312c3233322c3130302c3130392c39362c3230322c3234392c3139382c3230342c39332c372c3131372c3233362c3132382c36362c38392c3231342c3133392c3134375d2c227268735f706b223a5b3234362c3136362c3230342c3135302c3139362c39312c3135382c3133312c3133382c3130332c3234352c34392c3134352c3133302c32322c3132362c3134372c39352c32332c39332c31332c3230392c3133312c34392c3138322c34362c3135332c35372c33372c3130332c3235332c3234325d7d".to_string(),
279                    ),
280                    operational_certificate: Some(
281                        "5b5b5b35312c36322c392c3230302c3230392c34312c3234352c3230372c3135392c3139392c31342c372c38322c3230332c3234302c312c3132392c3138372c3131392c3232312c3133362c3234372c38392c3132382c3232382c3133332c302c39382c31322c3232382c3137382c3233345d2c31362c313139302c5b3231302c3134382c37332c3136332c3232322c3233332c3138302c33372c3133312c3235342c392c3230352c3135382c3134392c31342c37302c39322c372c3233352c3231342c3131312c35322c3131362c34312c3131382c362c3132392c312c3130362c312c39342c3233332c3131352c3137332c3130302c3133392c3131342c3130392c31352c31342c3233332c34332c3137392c3137342c35302c31302c3135302c39372c3132372c3138322c31362c372c3131322c3234352c34382c3134312c38342c3130322c342c32352c3231312c3134342c3230322c345d5d2c5b3133312c3135352c37322c35372c3134372c3231382c3137332c36382c3139312c3234322c3138392c3234372c32372c3235342c3134382c3232352c35332c31312c36392c3135372c3138322c38302c3233342c3133312c3233342c33392c3130322c32312c322c332c36352c3139305d5d".to_string(),
282                    ),
283                    kes_period: Some(6)
284                }
285        }
286
287        fn golden_signer_message_part_with_bytes_hex_encoding() -> SignerMessagePart {
288            SignerMessagePart {
289                    party_id: "pool1m8crhnqj5k2kyszf5j2scshupystyxc887zdfrpzh6ty6eun4fx"
290                        .to_string(),
291                    verification_key: "b891d0bc0e04cc9f20f34bb35287fe9d1f3b266e88e1ea7b2f662a7dbd11a2f44bf8c3e374c0985a291bfbac35b2d63814e3c513f2ba650249f30ecca486a2e91d721ea7cfad44064c50ea4140add9e530860c5fbaee96c0b4c00a01a275707ab2d6d6c1b2932b7e10d8d784a49208eef552e0edf98e9de51b9f79e7f86f555532c6ec5b82873e827d93a9915f234efd96e1e1e3537d1cad6cf1c849903e0ce58f8f4b4a879ec4c4e0e458697c2f4cf8f456a3dd939441e40f4fb95a60c0bdec".to_string(),
292                    verification_key_signature: Some(
293                        "157700e466d8b1822e7478fce00c2bbe1675d87cf0ad13f1b8d1406963ea3d0a473c278b6286849d8a25de6a6f4e784e5d621c35d4b79847973fa8bb25e3870e9d15241d142211ef0d592578159becf05fbda671d55abc29ab256fb4f31f3444a0f3622d04dd407530cbba295f0e9ee4afde742c5461a77f50c5f5a7c1c49db1b79b41f2ea2ba7c01052666289c41c51dd5141025d4654a8b3bb661cf4b67b949b458c1cae26953cb2e336d36a83449c3bb43508bce7ad01ab772093ea2aa36e8dacb1555a6f99f6e9c469b836f7869ee2cf1da3132201cfde48a61712ae10600cd4c56ce65325c29e9c56ba20f5f428f4b4a8e606b2ba164427ca3764e5bddd24fa02e5eaa51b3f78b204b4fb933294ed9a2691724ad33088d512b6cbe60f86c190d506ffb16e6ed5af031c9ed8ad1ae1592e8a314a3f2b9030315f3913812b9c51698dd102af53534ab76574b012ec12dfede9c1361cd500879d224bcd308a26f834143776bb27b0d52203d6021f76e0812aa35e6c7437d5438460e57a60a879901f6ef9f4c6febf3e5b119897e8646d60caf9c6cc5d0775ec804259d68b93f6a6cc96c45b9e838a67f5319182167e935f175d0dd18331b62e99392567fdf2".to_string(),
294                    ),
295                    operational_certificate: Some(
296                        "82845820333e09c8d129f5cf9fc70e0752cbf00181bb77dd88f75980e48500620ce4b2ea101904a65840d29449a3dee9b42583fe09cd9e950e465c07ebd66f347429760681016a015ee973ad648b726d0f0ee92bb3ae320a96617fb6100770f5308d54660419d390ca045820839b483993daad44bff2bdf71bfe94e1350b459db650ea83ea276615020341be".to_string(),
297                    ),
298                    kes_period: Some(6)
299                }
300        }
301
302        mod signer {
303            use super::*;
304
305            fn golden_message_with_json_hex_encoding() -> SignerMessagePart {
306                golden_signer_message_part_with_json_hex_encoding()
307            }
308
309            fn golden_message_with_bytes_hex_encoding() -> SignerMessagePart {
310                golden_signer_message_part_with_bytes_hex_encoding()
311            }
312
313            #[test]
314            fn restorations_from_json_hex_and_bytes_hex_give_same_signer() {
315                let signer_from_json_hex: Signer =
316                    golden_message_with_json_hex_encoding().try_into().unwrap();
317
318                let signer_from_bytes_hex: Signer =
319                    golden_message_with_bytes_hex_encoding().try_into().unwrap();
320
321                assert_eq!(signer_from_json_hex, signer_from_bytes_hex);
322            }
323        }
324
325        mod signer_with_stake {
326            use super::*;
327
328            fn golden_message_with_json_hex_encoding() -> SignerWithStakeMessagePart {
329                let signer_message_part = golden_signer_message_part_with_json_hex_encoding();
330
331                SignerWithStakeMessagePart {
332                    party_id: signer_message_part.party_id,
333                    verification_key: signer_message_part.verification_key,
334                    verification_key_signature: signer_message_part.verification_key_signature,
335                    operational_certificate: signer_message_part.operational_certificate,
336                    kes_period: signer_message_part.kes_period,
337                    stake: 123,
338                }
339            }
340
341            fn golden_message_with_bytes_hex_encoding() -> SignerWithStakeMessagePart {
342                let signer_message_part = golden_signer_message_part_with_bytes_hex_encoding();
343
344                SignerWithStakeMessagePart {
345                    party_id: signer_message_part.party_id,
346                    verification_key: signer_message_part.verification_key,
347                    verification_key_signature: signer_message_part.verification_key_signature,
348                    operational_certificate: signer_message_part.operational_certificate,
349                    kes_period: signer_message_part.kes_period,
350                    stake: 123,
351                }
352            }
353
354            #[test]
355            fn restorations_from_json_hex_and_bytes_hex_give_same_signer() {
356                let signer_from_json_hex: SignerWithStake =
357                    golden_message_with_json_hex_encoding().try_into().unwrap();
358
359                let signer_from_bytes_hex: SignerWithStake =
360                    golden_message_with_bytes_hex_encoding().try_into().unwrap();
361
362                assert_eq!(signer_from_json_hex, signer_from_bytes_hex);
363            }
364        }
365    }
366}