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
use anyhow::Context;
use mithril_common::{
    crypto_helper::{
        ProtocolOpCert, ProtocolSignerVerificationKey, ProtocolSignerVerificationKeySignature,
    },
    entities::{CertificatePending, Signer},
    messages::{CertificatePendingMessage, SignerMessagePart, TryFromMessageAdapter},
    StdResult,
};

/// Adapter to turn [CertificatePendingMessage] instances into [CertificatePending].
pub struct FromPendingCertificateMessageAdapter;

fn to_signers(messages: &[SignerMessagePart]) -> StdResult<Vec<Signer>> {
    let mut signers: Vec<Signer> = Vec::new();

    for msg in messages {
        let signer = Signer::new(
            msg.party_id.to_owned(),
            ProtocolSignerVerificationKey::from_json_hex(&msg.verification_key)?,
            match &msg.verification_key_signature {
                Some(verification_key_signature) => Some(
                    ProtocolSignerVerificationKeySignature::from_json_hex(
                        verification_key_signature,
                    )
                    .with_context(|| {
                        format!(
                            "'FromPendingCertificateMessageAdapter' can not json hex decode the verification key signature: '{}'",
                            verification_key_signature
                        )
                    })?,
                ),
                _ => None,
            },
            match &msg.operational_certificate {
                Some(operational_certificate) => Some(
                    ProtocolOpCert::from_json_hex(operational_certificate).with_context(|| {
                        format!(
                            "'FromPendingCertificateMessageAdapter' can not json hex decode the operational certificate: '{}'",
                            operational_certificate
                        )
                    })?,
                ),
                _ => None,
            },
            msg.kes_period,
        );
        signers.push(signer);
    }

    Ok(signers)
}

impl TryFromMessageAdapter<CertificatePendingMessage, CertificatePending>
    for FromPendingCertificateMessageAdapter
{
    /// Adapter method
    fn try_adapt(message: CertificatePendingMessage) -> StdResult<CertificatePending> {
        let certificate = CertificatePending {
            epoch: message.epoch,
            signed_entity_type: message.signed_entity_type,
            protocol_parameters: message.protocol_parameters,
            next_protocol_parameters: message.next_protocol_parameters,
            signers: to_signers(&message.signers).with_context(|| {
                format!(
                    "'FromPendingCertificateMessageAdapter' can not convert the list of current signers: '{:?}'",
                    message.signers
                )
            })?,
            next_signers: to_signers(&message.next_signers).with_context(|| {
                format!(
                    "'FromPendingCertificateMessageAdapter' can not convert the list of next signers: '{:?}'",
                    message.next_signers
                )
            })?,
        };

        Ok(certificate)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn adapt_ok() {
        let message = CertificatePendingMessage::dummy();
        let epoch = message.epoch;
        let certificate_pending = FromPendingCertificateMessageAdapter::try_adapt(message).unwrap();

        assert_eq!(epoch, certificate_pending.epoch);
    }

    #[test]
    fn adapt_signers() {
        let mut message = CertificatePendingMessage::dummy();
        message.signers = vec![SignerMessagePart::dummy(), SignerMessagePart::dummy()];
        let certificate_pending = FromPendingCertificateMessageAdapter::try_adapt(message).unwrap();

        assert_eq!(2, certificate_pending.signers.len());
        assert_eq!(1, certificate_pending.next_signers.len());
    }
}