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
use bc_components::{PublicKeyBase, ARID};
use bc_envelope::prelude::*;

use crate::{RECOVERY_METHOD_PARAM, START_RECOVERY_FUNCTION, util::{Abbrev, FlankedFunction}};

use super::{parse_request, parse_response, request_body, request_envelope, response_envelope};

//
// Request
//

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StartRecoveryRequest {
    id: ARID,
    key: PublicKeyBase,
    recovery: String,
}

impl StartRecoveryRequest {
    pub fn new(
        key: impl AsRef<PublicKeyBase>,
        recovery: impl AsRef<str>,
    ) -> Self {
        Self::new_opt(
            ARID::new(),
            key.as_ref().clone(),
            recovery.as_ref().to_string(),
        )
    }

    pub fn new_opt(id: ARID, key: PublicKeyBase, recovery: String) -> Self {
        Self {
            id,
            key,
            recovery,
        }
    }

    pub fn id(&self) -> &ARID {
        self.id.as_ref()
    }

    pub fn key(&self) -> &PublicKeyBase {
        &self.key
    }

    pub fn recovery(&self) -> &str {
        self.recovery.as_ref()
    }
}

impl EnvelopeEncodable for StartRecoveryRequest {
    fn envelope(self) -> Envelope {
        let body = request_body(START_RECOVERY_FUNCTION, self.key)
            .add_parameter(RECOVERY_METHOD_PARAM, self.recovery);
        request_envelope(self.id, body)
    }
}

impl From<StartRecoveryRequest> for Envelope {
    fn from(value: StartRecoveryRequest) -> Self {
        value.envelope()
    }
}

impl EnvelopeDecodable for StartRecoveryRequest {
    fn from_envelope(envelope: Envelope) -> anyhow::Result<Self> {
        let (id, key, body) = parse_request(START_RECOVERY_FUNCTION, envelope)?;
        let recovery: String = body.extract_object_for_parameter(RECOVERY_METHOD_PARAM)?;
        Ok(Self::new_opt(id, key, recovery))
    }
}

impl TryFrom<Envelope> for StartRecoveryRequest {
    type Error = anyhow::Error;

    fn try_from(value: Envelope) -> anyhow::Result<Self> {
        Self::from_envelope(value)
    }
}

impl EnvelopeCodable for StartRecoveryRequest {}

impl std::fmt::Display for StartRecoveryRequest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}: {} {} for key {}",
            self.id().abbrev(),
            "startRecovery".flanked_function(),
            self.recovery().abbrev(),
            self.key().abbrev()
        ))
    }
}

//
// Response
//

#[derive(Debug, Clone)]
pub struct StartRecoveryResponse {
    id: ARID,
    continuation: Envelope,
}

impl StartRecoveryResponse {
    pub fn new(id: ARID, continuation: Envelope) -> Self {
        Self { id, continuation }
    }

    pub fn id(&self) -> &ARID {
        self.id.as_ref()
    }

    pub fn continuation(&self) -> Envelope {
        self.continuation.clone()
    }
}

impl EnvelopeEncodable for StartRecoveryResponse {
    fn envelope(self) -> Envelope {
        response_envelope(self.id, Some(self.continuation))
    }
}

impl From<StartRecoveryResponse> for Envelope {
    fn from(value: StartRecoveryResponse) -> Self {
        value.envelope()
    }
}

impl EnvelopeDecodable for StartRecoveryResponse {
    fn from_envelope(envelope: Envelope) -> anyhow::Result<Self> {
        let (id, continuation) = parse_response(envelope)?;
        Ok(Self::new(id, continuation))
    }
}

impl TryFrom<Envelope> for StartRecoveryResponse {
    type Error = anyhow::Error;

    fn try_from(value: Envelope) -> anyhow::Result<Self> {
        Self::from_envelope(value)
    }
}

impl EnvelopeCodable for StartRecoveryResponse {}

impl PartialEq for StartRecoveryResponse {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self
                .continuation
                .is_identical_to(other.continuation.clone())
    }
}

impl Eq for StartRecoveryResponse {}

impl std::fmt::Display for StartRecoveryResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}: {} OK: continuation {}",
            self.id().abbrev(),
            "startRecovery".flanked_function(),
            self.continuation().abbrev()
        ))
    }
}

#[cfg(test)]
mod tests {
    use bc_components::PrivateKeyBase;
    use indoc::indoc;

    use super::*;

    fn id() -> ARID {
        ARID::from_data_ref(hex_literal::hex!(
            "8712dfac3d0ebfa910736b2a9ee39d4b68f64222a77bcc0074f3f5f1c9216d30"
        ))
        .unwrap()
    }

    #[test]
    fn test_request() {
        let recovery = "recovery".to_string();
        let new_key = PrivateKeyBase::new().public_keys();

        let request = StartRecoveryRequest::new_opt(id(), new_key, recovery);
        let request_envelope = request.clone().envelope();
        assert_eq!(
            request_envelope.format(),
            indoc! {r#"
        request(ARID(8712dfac)) [
            'body': «"startRecovery"» [
                ❰"key"❱: PublicKeyBase
                ❰"recoveryMethod"❱: "recovery"
            ]
        ]
        "#}
            .trim()
        );
        let decoded = StartRecoveryRequest::try_from(request_envelope).unwrap();
        assert_eq!(request, decoded);
    }

    #[test]
    fn test_response() {
        let continuation = "continuation";
        let response = StartRecoveryResponse::new(id(), continuation.envelope());
        let response_envelope = response.clone().envelope();
        assert_eq!(
            response_envelope.format(),
            indoc! {r#"
        response(ARID(8712dfac)) [
            'result': "continuation"
        ]
        "#}
            .trim()
        );
        let decoded = StartRecoveryResponse::try_from(response_envelope).unwrap();
        assert_eq!(response, decoded);
    }
}