ockam_api 0.93.0

Ockam's request-response API
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::fmt::Display;
use std::time::Duration;

use minicbor::{CborLen, Decode, Encode};
use serde::Serialize;

use ockam::identity::models::{ChangeHistory, CredentialAndPurposeKey};
use ockam::identity::{Identifier, SecureChannel, SecureChannelListener};
use ockam::Message;
use ockam_core::flow_control::FlowControlId;
use ockam_core::{cbor_encode_preallocate, route, Address, Decodable, Encodable, Encoded, Result};
use ockam_multiaddr::MultiAddr;

use crate::colors::color_primary;
use crate::nodes::registry::SecureChannelInfo;
use crate::output::Output;
use crate::terminal::fmt;
use crate::ReverseLocalConverter;

//Requests

/// Request body when instructing a node to create a Secure Channel
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct CreateSecureChannelRequest {
    #[n(1)] pub addr: MultiAddr,
    #[n(2)] pub authorized_identifiers: Option<Vec<Identifier>>,
    #[n(4)] pub timeout: Option<Duration>,
    #[n(5)] pub identity_name: Option<String>,
    #[n(6)] pub credential: Option<CredentialAndPurposeKey>,
}

impl Encodable for CreateSecureChannelRequest {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for CreateSecureChannelRequest {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}
impl CreateSecureChannelRequest {
    pub fn new(
        addr: &MultiAddr,
        authorized_identifiers: Option<Vec<Identifier>>,
        identity_name: Option<String>,
        credential: Option<CredentialAndPurposeKey>,
    ) -> Self {
        Self {
            addr: addr.to_owned(),
            authorized_identifiers,
            timeout: None,
            identity_name,
            credential,
        }
    }
}

/// Request body when instructing a node to delete a Secure Channel
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct DeleteSecureChannelRequest {
    #[n(1)] pub channel: Address,
}

impl Encodable for DeleteSecureChannelRequest {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for DeleteSecureChannelRequest {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}
impl DeleteSecureChannelRequest {
    pub fn new(channel: &Address) -> Self {
        Self {
            channel: channel.to_owned(),
        }
    }
}

/// Request body when instructing a node to show a Secure Channel
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct ShowSecureChannelRequest {
    #[n(1)] pub channel: Address,
}

impl Encodable for ShowSecureChannelRequest {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for ShowSecureChannelRequest {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}
impl ShowSecureChannelRequest {
    pub fn new(channel: &Address) -> Self {
        Self {
            channel: channel.to_owned(),
        }
    }
}

/// Request body when instructing a node to delete a Secure Channel Listener
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct DeleteSecureChannelListenerRequest {
    #[n(1)] pub addr: Address,
}

impl Encodable for DeleteSecureChannelListenerRequest {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for DeleteSecureChannelListenerRequest {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}
impl DeleteSecureChannelListenerRequest {
    pub fn new(addr: &Address) -> Self {
        Self {
            addr: addr.to_owned(),
        }
    }
}

/// Request body to show a Secure Channel Listener
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct ShowSecureChannelListenerRequest {
    #[n(1)] pub addr: Address,
}

impl Encodable for ShowSecureChannelListenerRequest {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for ShowSecureChannelListenerRequest {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}

impl ShowSecureChannelListenerRequest {
    pub fn new(addr: &Address) -> Self {
        Self {
            addr: addr.to_owned(),
        }
    }
}

// Responses

/// Response body when instructing a node to create a Secure Channel
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct CreateSecureChannelResponse {
    #[n(1)] pub addr: Address,
    #[n(2)] pub flow_control_id: FlowControlId,
}

impl Encodable for CreateSecureChannelResponse {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for CreateSecureChannelResponse {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}

impl CreateSecureChannelResponse {
    pub fn new(secure_channel: SecureChannel) -> Self {
        Self {
            addr: secure_channel.encryptor_address().to_string().into(),
            flow_control_id: secure_channel.flow_control_id().clone(),
        }
    }

    pub fn flow_control_id(&self) -> FlowControlId {
        self.flow_control_id.clone()
    }

    pub fn multiaddr(&self) -> Result<MultiAddr> {
        ReverseLocalConverter::convert_route(&route![self.addr.to_string()])
    }
}

impl Output for CreateSecureChannelResponse {
    fn item(&self) -> crate::Result<String> {
        let addr =
            ReverseLocalConverter::convert_route(&route![self.addr.to_string()])?.to_string();
        Ok(addr)
    }
}

#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct CreateSecureChannelListenerRequest {
    #[n(1)] pub addr: Address,
    #[n(2)] pub authorized_identifiers: Option<Vec<Identifier>>,
    #[n(3)] pub identity_name: Option<String>,
}

impl Encodable for CreateSecureChannelListenerRequest {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for CreateSecureChannelListenerRequest {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}
impl CreateSecureChannelListenerRequest {
    pub fn new(
        addr: &Address,
        authorized_identifiers: Option<Vec<Identifier>>,
        identity_name: Option<String>,
    ) -> Self {
        Self {
            addr: addr.to_owned(),
            authorized_identifiers,
            identity_name,
        }
    }
}

/// Response body when deleting a Secure Channel Listener
#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct DeleteSecureChannelListenerResponse {
    #[n(1)] pub addr: Address,
}

impl DeleteSecureChannelListenerResponse {
    pub fn new(addr: Address) -> Self {
        Self { addr }
    }
}

impl Encodable for DeleteSecureChannelListenerResponse {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for DeleteSecureChannelListenerResponse {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}

impl Output for SecureChannelListener {
    fn item(&self) -> crate::Result<String> {
        let addr = {
            let channel_route = route![self.address().clone()];
            let channel_multiaddr = ReverseLocalConverter::convert_route(&channel_route)?;
            channel_multiaddr.to_string()
        };
        Ok(format!("Listener at {}", color_primary(addr)))
    }
}

#[derive(Debug, Clone, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct DeleteSecureChannelResponse {
    #[n(1)] pub channel: Option<String>,
}

impl Encodable for DeleteSecureChannelResponse {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for DeleteSecureChannelResponse {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}
impl DeleteSecureChannelResponse {
    pub fn new(channel: Option<Address>) -> Self {
        Self {
            channel: channel.map(|ch| ch.to_string()),
        }
    }
}

#[derive(Debug, Clone, Encode, Decode, CborLen, Serialize, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct ShowSecureChannelResponse {
    #[n(1)] pub address: MultiAddr,
    #[n(2)] pub route: MultiAddr,
    #[n(3)] pub authorized_identifiers: Option<Vec<Identifier>>,
    #[n(4)] pub flow_control_id: FlowControlId,
    #[n(5)] pub their_identifier: Identifier,
    #[n(6)] pub their_change_history: Option<String>,
}

impl Encodable for ShowSecureChannelResponse {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for ShowSecureChannelResponse {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}

impl ShowSecureChannelResponse {
    pub fn new(info: SecureChannelInfo) -> Result<Self> {
        Ok(Self {
            address: ReverseLocalConverter::convert_address(info.sc().encryptor_address())?,
            route: ReverseLocalConverter::convert_route(info.route())?,
            authorized_identifiers: info.authorized_identifiers().map(|ids| ids.to_vec()),
            flow_control_id: info.sc().flow_control_id().clone(),
            their_identifier: info.sc().their_identifier().clone(),
            their_change_history: None,
        })
    }

    pub fn with_their_change_history(mut self, change_history: ChangeHistory) -> Result<Self> {
        self.their_change_history = Some(change_history.export_as_string()?);
        Ok(self)
    }
}

impl Display for ShowSecureChannelResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "Secure channel running at {}",
            color_primary(&self.address)
        )?;

        let route_ma = &self.route.to_string();
        writeln!(f, "With route to {}", color_primary(route_ma))?;

        if let Some(authorized) = &self.authorized_identifiers {
            writeln!(f, "{}Authorized identifiers:", fmt::INDENTATION)?;
            for id in authorized {
                writeln!(f, "{}{}{}", fmt::INDENTATION, fmt::INDENTATION, id)?;
            }
        }

        writeln!(
            f,
            "Their identifier: {}",
            color_primary(&self.their_identifier)
        )?;

        Ok(())
    }
}

impl Output for ShowSecureChannelResponse {
    fn item(&self) -> crate::Result<String> {
        Ok(self.padded_display())
    }
}

#[derive(Encode, Decode, CborLen, Debug, Default, Clone, Eq, PartialEq, Message)]
pub struct SecureChannelList(#[n(0)] pub Vec<Address>);

impl Encodable for SecureChannelList {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for SecureChannelList {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}