siera_agent/modules/
connection.rs

1use crate::error::Result;
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6/// Query filters applied to retrieving all the connections
7#[derive(Debug, Default, Serialize, Deserialize)]
8pub struct ConnectionGetAllOptions {
9    /// Optional `alias` to filter on
10    pub alias: Option<String>,
11
12    // TODO: enum
13    /// Optional `connection protocol` to filter on
14    pub connection_protocol: Option<String>,
15
16    /// Optional `invitation key` to filter on
17    pub invitation_key: Option<String>,
18
19    /// Optional `my did` to filter on
20    pub my_did: Option<String>,
21
22    // TODO: enum
23    /// Optional `state` to filter on
24    pub state: Option<String>,
25
26    /// Optional `their did` to filter on
27    pub their_did: Option<String>,
28
29    // TODO: enum
30    /// Optional `their role` to filter on
31    pub their_role: Option<String>,
32}
33
34/// Create invitation response
35#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
36pub struct Invitation {
37    /// Invitation url
38    #[serde(alias = "invitationUrl")]
39    pub invitation_url: String,
40    /// Invitation structure
41    pub invitation: Value,
42
43    /// Connection id of the invitation
44    #[serde(alias = "connection_id")]
45    pub id: String,
46}
47
48/// A single connection structure
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub struct Connection {
51    /// The connection id used for further functionality
52    #[serde(alias = "connection_id")]
53    pub id: String,
54
55    /// When the connection is created
56    #[serde(alias = "createdAt")]
57    pub created_at: String,
58
59    /// Your did used in the connection process
60    #[serde(alias = "my_did")]
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub did: Option<String>,
63
64    /// The current connection state
65    pub state: String,
66
67    /// Their role in the connection process
68    #[serde(alias = "their_role")]
69    pub role: String,
70
71    /// Auto accept state
72    #[serde(alias = "accept", alias = "autoAcceptConnection")]
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub auto_accept: Option<Value>,
75
76    /// Their label as set when the connection is initialized
77    #[serde(alias = "theirLabel")]
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub their_label: Option<String>,
80
81    /// Optional their did used in the connection proces
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub their_did: Option<String>,
84
85    /// (AFJ) verkey used
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub verkey: Option<String>,
88}
89
90/// Options supplied by the frontend for creating an invitation
91#[derive(Debug, Default)]
92pub struct ConnectionCreateInvitationOptions {
93    /// Whether we want to auto accept the connection process
94    pub auto_accept: bool,
95
96    /// Whether a QR should be outputted to the user
97    pub qr: bool,
98
99    /// Whether it should create a special invitation for the toolbox
100    pub toolbox: bool,
101
102    /// Whether the invitation is reuseable
103    pub multi_use: bool,
104
105    /// Optional custom alias for the connection
106    pub alias: Option<String>,
107}
108
109/// Options for receiving an invitation
110#[derive(Debug, Serialize, Deserialize)]
111#[serde(rename_all = "camelCase")]
112// Every field is optional here as there are some collisions between, for example, did and
113// routing_key. We rely on the cloudagent for error handling these collisions
114// Sadly we cannot skip serializing on the whole struct, we must specify it for each element
115pub struct ConnectionReceiveInvitationOptions {
116    /// Id of the connection
117    #[serde(rename = "@id")]
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub id: Option<String>,
120
121    /// Did used for the connection
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub did: Option<String>,
124
125    /// Url to an image that can be used as an avatar
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub image_url: Option<String>,
128
129    /// Label used for the connection
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub label: Option<String>,
132
133    /// Keys used by the recipient
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub recipient_keys: Option<Vec<String>>,
136
137    /// Routing keys used
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub routing_keys: Option<Vec<String>>,
140
141    /// Service endpoint to call
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub service_endpoint: Option<String>,
144}
145
146/// Generic cloudagent connection module
147#[async_trait]
148pub trait ConnectionModule {
149    /// Gets all the connections
150    async fn get_all(&self, options: ConnectionGetAllOptions) -> Result<Vec<Connection>>;
151
152    /// Get a connection by id
153    async fn get_by_id(&self, id: String) -> Result<Connection>;
154
155    /// Create an invitation
156    async fn create_invitation(
157        &self,
158        options: ConnectionCreateInvitationOptions,
159    ) -> Result<Invitation>;
160
161    /// Receive and accept a connection
162    async fn receive_invitation(
163        &self,
164        invitation: ConnectionReceiveInvitationOptions,
165    ) -> Result<Connection>;
166}