mithril_common/messages/message_parts/
signer.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#[cfg(any(test, feature = "test_tools"))]
use crate::test_utils::fake_keys;
use crate::{
    crypto_helper::{KESPeriod, ProtocolOpCert, ProtocolSignerVerificationKeySignature},
    entities::{
        HexEncodedOpCert, HexEncodedVerificationKey, HexEncodedVerificationKeySignature, PartyId,
        Signer, SignerWithStake, Stake,
    },
    StdError, StdResult,
};
use anyhow::Context;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};

/// Signer with Stake Message
#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct SignerWithStakeMessagePart {
    /// The unique identifier of the signer
    // TODO: Should be removed once the signer certification is fully deployed
    pub party_id: PartyId,

    /// The public key used to authenticate signer signature
    pub verification_key: HexEncodedVerificationKey,

    /// The encoded signer 'Mithril verification key' signature (signed by the
    /// Cardano node KES secret key).
    // TODO: Option should be removed once the signer certification is fully
    //       deployed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verification_key_signature: Option<HexEncodedVerificationKeySignature>,

    /// The encoded operational certificate of stake pool operator attached to
    /// the signer node.
    // TODO: Option should be removed once the signer certification is fully
    //       deployed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operational_certificate: Option<HexEncodedOpCert>,

    /// The KES period used to compute the verification key signature
    // TODO: This KES period should not be used as is and should probably be
    //       within an allowed range of KES periods for the epoch.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kes_period: Option<KESPeriod>,

    /// The signer stake
    pub stake: Stake,
}

impl SignerWithStakeMessagePart {
    cfg_test_tools! {
        /// Return a dummy test entity (test-only).
        pub fn dummy() -> Self {
            Self {
                party_id: "pool1m8crhnqj5k2kyszf5j2scshupystyxc887zdfrpzh6ty6eun4fx".to_string(),
                verification_key: fake_keys::signer_verification_key()[0].to_string(),
                verification_key_signature: Some(
                    fake_keys::signer_verification_key_signature()[0].to_string(),
                ),
                operational_certificate: Some(fake_keys::operational_certificate()[0].to_string()),
                kes_period: Some(6),
                stake: 234,
            }
        }
    }

    /// Convert a set of signers into message parts
    pub fn from_signers(signers: Vec<SignerWithStake>) -> Vec<Self> {
        signers.into_iter().map(|signer| signer.into()).collect()
    }

    /// Convert a set of signer message parts into a set of signers with stake
    pub fn try_into_signers(messages: Vec<Self>) -> StdResult<Vec<SignerWithStake>> {
        messages
            .into_iter()
            .map(SignerWithStakeMessagePart::try_into)
            .collect()
    }
}

impl TryInto<SignerWithStake> for SignerWithStakeMessagePart {
    type Error = StdError;

    fn try_into(self) -> Result<SignerWithStake, Self::Error> {
        let verification_key_signature: Option<ProtocolSignerVerificationKeySignature> = self
            .verification_key_signature
            .map(|f| f.try_into())
            .transpose()
            .with_context(|| {
                format!(
                    "Error while parsing verification key signature message, party_id = '{}'",
                    self.party_id
                )
            })?;
        let operational_certificate: Option<ProtocolOpCert> = self
            .operational_certificate
            .map(|f| f.try_into())
            .transpose()
            .with_context(|| {
                format!(
                    "Error while parsing operational certificate message, party_id = '{}'.",
                    self.party_id
                )
            })?;
        let value = SignerWithStake {
            party_id: self.party_id,
            verification_key: self.verification_key.try_into()?,
            verification_key_signature,
            kes_period: self.kes_period,
            operational_certificate,
            stake: self.stake,
        };
        Ok(value)
    }
}

impl From<SignerWithStake> for SignerWithStakeMessagePart {
    fn from(value: SignerWithStake) -> Self {
        Self {
            party_id: value.party_id,
            verification_key: value.verification_key.try_into().unwrap(),
            verification_key_signature: value
                .verification_key_signature
                .map(|k| k.try_into().unwrap()),
            operational_certificate: value
                .operational_certificate
                .map(|op_cert| (op_cert.try_into().unwrap())),
            kes_period: value.kes_period,
            stake: value.stake,
        }
    }
}

impl Debug for SignerMessagePart {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let should_be_exhaustive = f.alternate();
        let mut debug = f.debug_struct("Signer");
        debug.field("party_id", &self.party_id);

        match should_be_exhaustive {
            true => debug
                .field(
                    "verification_key",
                    &format_args!("{:?}", self.verification_key),
                )
                .field(
                    "verification_key_signature",
                    &format_args!("{:?}", self.verification_key_signature),
                )
                .field(
                    "operational_certificate",
                    &format_args!("{:?}", self.operational_certificate),
                )
                .field("kes_period", &format_args!("{:?}", self.kes_period))
                .finish(),
            false => debug.finish_non_exhaustive(),
        }
    }
}

/// Signer Message
#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct SignerMessagePart {
    /// The unique identifier of the signer
    // TODO: Should be removed once the signer certification is fully deployed
    pub party_id: PartyId,

    /// The public key used to authenticate signer signature
    pub verification_key: HexEncodedVerificationKey,

    /// The encoded signer 'Mithril verification key' signature (signed by the
    /// Cardano node KES secret key).
    // TODO: Option should be removed once the signer certification is fully
    //       deployed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verification_key_signature: Option<HexEncodedVerificationKeySignature>,

    /// The encoded operational certificate of stake pool operator attached to
    /// the signer node.
    // TODO: Option should be removed once the signer certification is fully
    //       deployed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operational_certificate: Option<HexEncodedOpCert>,

    /// The KES period used to compute the verification key signature
    // TODO: This KES period should not be used as is and should probably be
    //       within an allowed range of KES periods for the epoch.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kes_period: Option<KESPeriod>,
}

impl SignerMessagePart {
    /// Convert a set of signer message parts into a set of signers
    pub fn try_into_signers(messages: Vec<Self>) -> StdResult<Vec<Signer>> {
        messages
            .into_iter()
            .map(SignerMessagePart::try_into)
            .collect()
    }

    /// Convert a set of signers into message parts
    pub fn from_signers(signers: Vec<Signer>) -> Vec<Self> {
        signers.into_iter().map(|signer| signer.into()).collect()
    }

    cfg_test_tools! {
        /// Return a dummy test entity (test-only).
        pub fn dummy() -> Self {
            Self {
                party_id: "pool1m8crhnqj5k2kyszf5j2scshupystyxc887zdfrpzh6ty6eun4fx".to_string(),
                verification_key: fake_keys::signer_verification_key()[0].to_string(),
                verification_key_signature: Some(
                    fake_keys::signer_verification_key_signature()[0].to_string(),
                ),
                operational_certificate: Some(fake_keys::operational_certificate()[0].to_string()),
                kes_period: Some(6),
            }
        }
    }
}

impl TryInto<Signer> for SignerMessagePart {
    type Error = StdError;

    fn try_into(self) -> Result<Signer, Self::Error> {
        let verification_key_signature: Option<ProtocolSignerVerificationKeySignature> = self
            .verification_key_signature
            .map(|f| f.try_into())
            .transpose()
            .with_context(|| {
                format!(
                    "Error while parsing verification key signature message, party_id = '{}'",
                    self.party_id
                )
            })?;
        let operational_certificate: Option<ProtocolOpCert> = self
            .operational_certificate
            .map(|f| f.try_into())
            .transpose()
            .with_context(|| {
                format!(
                    "Error while parsing operational certificate message, party_id = '{}'.",
                    self.party_id
                )
            })?;
        let value = Signer {
            party_id: self.party_id,
            verification_key: self.verification_key.try_into()?,
            verification_key_signature,
            kes_period: self.kes_period,
            operational_certificate,
        };
        Ok(value)
    }
}

impl From<Signer> for SignerMessagePart {
    fn from(value: Signer) -> Self {
        Self {
            party_id: value.party_id,
            verification_key: value.verification_key.try_into().unwrap(),
            verification_key_signature: value
                .verification_key_signature
                .map(|k| k.try_into().unwrap()),
            operational_certificate: value
                .operational_certificate
                .map(|op_cert| (op_cert.try_into().unwrap())),
            kes_period: value.kes_period,
        }
    }
}

impl Debug for SignerWithStakeMessagePart {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let should_be_exhaustive = f.alternate();
        let mut debug = f.debug_struct("Signer");
        debug
            .field("party_id", &self.party_id)
            .field("stake", &self.stake);

        match should_be_exhaustive {
            true => debug
                .field(
                    "verification_key",
                    &format_args!("{:?}", self.verification_key),
                )
                .field(
                    "verification_key_signature",
                    &format_args!("{:?}", self.verification_key_signature),
                )
                .field(
                    "operational_certificate",
                    &format_args!("{:?}", self.operational_certificate),
                )
                .field("kes_period", &format_args!("{:?}", self.kes_period))
                .finish(),
            false => debug.finish_non_exhaustive(),
        }
    }
}