mithril_common/messages/
register_signature.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::{Debug, Formatter};
3
4use crate::entities::{HexEncodedSingleSignature, LotteryIndex, PartyId, SignedEntityType};
5
6#[cfg(any(test, feature = "test_tools"))]
7use crate::test_utils::fake_keys;
8
9/// Message structure to register single signature through HTTP.
10#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct RegisterSignatureMessageHttp {
12    /// Signed entity type
13    #[serde(rename = "entity_type")]
14    pub signed_entity_type: SignedEntityType,
15
16    /// The unique identifier of the signer
17    pub party_id: PartyId,
18
19    /// The single signature of the digest
20    pub signature: HexEncodedSingleSignature,
21
22    /// The indexes of the won lotteries that lead to the single signature
23    #[serde(rename = "indexes")]
24    pub won_indexes: Vec<LotteryIndex>,
25
26    /// Message that is signed by the signer
27    ///
28    /// Used to buffer the signature for later if the aggregator has yet to create an open message
29    /// for the signed entity type.
30    pub signed_message: String,
31}
32
33impl RegisterSignatureMessageHttp {
34    cfg_test_tools! {
35        /// Return a dummy test entity (test-only).
36        pub fn dummy() -> Self {
37            use crate::entities::Epoch;
38            Self {
39                signed_entity_type: SignedEntityType::MithrilStakeDistribution(Epoch(5)),
40                party_id: "party_id".to_string(),
41                signature: fake_keys::single_signature()[0].to_string(),
42                won_indexes: vec![1, 3],
43                signed_message: "6a7e737c312972d2346b65ac3075696e04286d046dddaf8004121e3d5e27cc0d".to_string(),
44            }
45        }
46    }
47}
48
49impl Debug for RegisterSignatureMessageHttp {
50    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51        let is_pretty_printing = f.alternate();
52        let mut debug = f.debug_struct("RegisterSignatureMessageHttp");
53        debug
54            .field(
55                "signed_entity_type",
56                &format_args!("{:?}", self.signed_entity_type),
57            )
58            .field("party_id", &self.party_id)
59            .field("won_indexes", &format_args!("{:?}", self.won_indexes));
60
61        match is_pretty_printing {
62            true => debug.field("signature", &self.signature).finish(),
63            false => debug.finish_non_exhaustive(),
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    mod http_message {
73        use crate::entities::{CardanoDbBeacon, Epoch};
74
75        use super::*;
76
77        const CURRENT_JSON: &str = r#"{
78        "entity_type": {
79            "CardanoImmutableFilesFull": {
80                "epoch": 10,
81                "immutable_file_number": 1728
82            }
83        },
84        "party_id": "party_id",
85        "signature": "7b227369676d61223a5b3133302c3137372c31352c3232392c32342c3235312c3234372c3137312c3139362c3231302c3134332c3131332c38362c3138392c39322c35362c3131322c33332c3139332c3231322c35342c3231342c32382c3231362c3232372c3137332c3130302c3132372c3137382c34302c39382c38372c32392c3138312c3235352c3131312c3135372c3232342c3233352c34362c3130302c3136392c3233322c3138392c3235322c38322c3133392c33365d2c22696e6465786573223a5b302c312c332c342c362c382c392c31302c31312c31322c31342c31382c32312c32322c32332c32352c32362c32372c33302c33332c33342c33382c34312c34332c35302c35382c35392c36302c36312c36322c36372c36392c37312c37332c37352c37362c37372c38312c38322c38332c38342c39302c39312c39322c39332c39372c39385d2c227369676e65725f696e646578223a327d",
86        "indexes": [1, 3],
87        "signed_message": "6a7e737c312972d2346b65ac3075696e04286d046dddaf8004121e3d5e27cc0d"
88    }"#;
89
90        fn golden_message_current() -> RegisterSignatureMessageHttp {
91            RegisterSignatureMessageHttp {
92            signed_entity_type: SignedEntityType::CardanoImmutableFilesFull(
93                CardanoDbBeacon::new(*Epoch(10), 1728),
94            ),
95            party_id: "party_id".to_string(),
96            signature: "7b227369676d61223a5b3133302c3137372c31352c3232392c32342c3235312c3234372c3137312c3139362c3231302c3134332c3131332c38362c3138392c39322c35362c3131322c33332c3139332c3231322c35342c3231342c32382c3231362c3232372c3137332c3130302c3132372c3137382c34302c39382c38372c32392c3138312c3235352c3131312c3135372c3232342c3233352c34362c3130302c3136392c3233322c3138392c3235322c38322c3133392c33365d2c22696e6465786573223a5b302c312c332c342c362c382c392c31302c31312c31322c31342c31382c32312c32322c32332c32352c32362c32372c33302c33332c33342c33382c34312c34332c35302c35382c35392c36302c36312c36322c36372c36392c37312c37332c37352c37362c37372c38312c38322c38332c38342c39302c39312c39322c39332c39372c39385d2c227369676e65725f696e646578223a327d".to_string(),
97            won_indexes: vec![1, 3],
98            signed_message: "6a7e737c312972d2346b65ac3075696e04286d046dddaf8004121e3d5e27cc0d".to_string(),
99        }
100        }
101
102        #[test]
103        fn test_current_json_deserialized_into_current_message() {
104            let json = CURRENT_JSON;
105            let message: RegisterSignatureMessageHttp = serde_json::from_str(json).unwrap();
106
107            assert_eq!(golden_message_current(), message);
108        }
109
110        #[test]
111        fn test_json_until_open_api_0_1_45_deserialized_into_current_message() {
112            let json = r#"{
113            "entity_type": {
114                "CardanoImmutableFilesFull": {
115                    "network": "testnet",
116                    "epoch": 10,
117                    "immutable_file_number": 1728
118                }
119            },
120            "party_id": "party_id",
121            "signature": "7b227369676d61223a5b3133302c3137372c31352c3232392c32342c3235312c3234372c3137312c3139362c3231302c3134332c3131332c38362c3138392c39322c35362c3131322c33332c3139332c3231322c35342c3231342c32382c3231362c3232372c3137332c3130302c3132372c3137382c34302c39382c38372c32392c3138312c3235352c3131312c3135372c3232342c3233352c34362c3130302c3136392c3233322c3138392c3235322c38322c3133392c33365d2c22696e6465786573223a5b302c312c332c342c362c382c392c31302c31312c31322c31342c31382c32312c32322c32332c32352c32362c32372c33302c33332c33342c33382c34312c34332c35302c35382c35392c36302c36312c36322c36372c36392c37312c37332c37352c37362c37372c38312c38322c38332c38342c39302c39312c39322c39332c39372c39385d2c227369676e65725f696e646578223a327d",
122            "indexes": [1, 3],
123            "signed_message": "6a7e737c312972d2346b65ac3075696e04286d046dddaf8004121e3d5e27cc0d"
124        }"#;
125            let message: RegisterSignatureMessageHttp = serde_json::from_str(json).unwrap();
126
127            assert_eq!(golden_message_current(), message);
128        }
129    }
130}