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
use commons::models::approval_signature::Acceptance;
use commons::models::event::Event;
use commons::models::event_request::{RequestData, RequestPayload};
use commons::models::signature::Signature;
use commons::{models::event_request::EventRequest};
use serde::{Serialize, Deserialize};
use utoipa::ToSchema;

mod api;
mod error;
mod inner_api;

pub use api::{ApiModuleInterface, NodeAPI, API};
use commons::models::state::SubjectData;
pub use error::ApiError;

#[derive(Debug, Clone)]
pub enum APICommands {
    GetAllSubjects(GetAllSubjects),
    GetAllGovernances,
    GetSingleSubject(GetSingleSubject),
    GetEventsOfSubject(GetEventsOfSubject),
    GetSignatures(GetSignatures),
    SimulateEvent(CreateEvent),
    VoteResolve(Acceptance, String),
    CreateRequest(CreateRequest),
    ExternalRequest(ExternalEventRequest),
    GetPendingRequests,
    GetSingleRequest(String),
    Shutdown,
}

#[derive(Debug, Clone)]
pub struct GetAllSubjects {
    pub namespace: String,
    pub from: Option<usize>,
    pub quantity: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct GetSingleSubject {
    pub subject_id: String,
}

#[derive(Debug, Clone)]
pub struct CreateEvent {
    pub subject_id: String,
    pub payload: RequestPayload,
}

#[derive(Debug, Clone)]
pub struct CreateSubject {
    pub governance_id: String,
    pub schema_id: String,
    pub namespace: String,
    pub payload: RequestPayload,
}

/// Specification of the different types of available requests
#[derive(Debug, Clone)]
pub enum CreateRequest {
    Create(CreateType),
    State(StateType),
}

/// Request for a Create type event 
#[derive(Debug, Clone)]
pub struct CreateType {
    pub governance_id: String,
    pub schema_id: String,
    pub namespace: String,
    pub payload: RequestPayload,
}

/// Request for a State type event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateType {
    pub subject_id: String,
    pub payload: RequestPayload,
}

#[derive(Debug, Clone)]
pub struct CreateGovernance {
    pub payload: RequestPayload,
}

#[derive(Debug, Clone)]
pub struct GetEventsOfSubject {
    pub subject_id: String,
    pub from: Option<i64>,
    pub quantity: Option<i64>,
}

#[derive(Debug, Clone)]
pub struct GetSignatures {
    pub subject_id: String,
    pub sn: u64,
    pub from: Option<usize>,
    pub quantity: Option<usize>,
}

#[derive(Debug, Clone)]
pub enum APIResponses {
    GetAllSubjects(Result<Vec<SubjectData>, ApiError>),
    GetAllGovernances(Result<Vec<SubjectData>, ApiError>),
    GetSingleSubject(Result<SubjectData, ApiError>),
    GetEventsOfSubject(Result<Vec<Event>, ApiError>),
    GetSignatures(Result<Vec<Signature>, ApiError>),
    SimulateEvent(Result<SubjectData, ApiError>),
    VoteResolve(Result<(), ApiError>),
    GetPendingRequests(Result<Vec<EventRequest>, ApiError>),
    GetSingleRequest(Result<EventRequest, ApiError>),
    CreateRequest(Result<RequestData, ApiError>),
    ExternalRequest(Result<RequestData, ApiError>),
    ShutdownCompleted,
}

/// Data structure of a externa event request
#[derive(Debug, Clone, ToSchema, Serialize, Deserialize)]
pub struct ExternalEventRequest {
    pub request: StateType,
    pub timestamp: i64,
    pub signature: SignatureRequest,
}

/// Signature of a external event request
#[derive(Debug, Clone, PartialEq, ToSchema, Serialize, Deserialize)]
pub struct SignatureRequest {
    pub content: SignatureRequestContent,
    pub signature: String, // SignatureIdentifier,
}

/// Content of the signature of a external event request
#[derive(Debug, Clone, PartialEq, ToSchema, Serialize, Deserialize)]
pub struct SignatureRequestContent {
    pub signer: String,             // KeyIdentifier,
    pub event_content_hash: String, // DigestIdentifier,
    pub timestamp: i64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Eq, Deserialize, ToSchema)]
#[allow(non_snake_case)]
pub struct StateRequestBodyUpper {
    pub State: StateRequestBody,
}

#[derive(Debug, Clone, PartialEq, Serialize, Eq, Deserialize, ToSchema)]
pub struct StateRequestBody {
    pub subject_id: String,
    pub payload: Payload,
}

#[derive(Debug, Clone, PartialEq, Serialize, Eq, Deserialize, ToSchema)]
pub enum Payload {
    #[schema(value_type = Object)]
    Json(serde_json::Value),
    #[schema(value_type = Object)]
    JsonPatch(serde_json::Value),
}

impl Into<RequestPayload> for Payload {
    fn into(self) -> RequestPayload {
        match self {
            Self::Json(data) => RequestPayload::Json(serde_json::to_string(&data).unwrap()),
            Self::JsonPatch(data) => {
                RequestPayload::JsonPatch(serde_json::to_string(&data).unwrap())
            }
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ExternalEventRequestBody {
    pub request: StateRequestBodyUpper,
    pub timestamp: i64,
    pub signature: SignatureRequest,
}

impl Into<ExternalEventRequest> for ExternalEventRequestBody {
    fn into(self) -> ExternalEventRequest {
        ExternalEventRequest {
            request: StateType {
                subject_id: self.request.State.subject_id,
                payload: self.request.State.payload.into(),
            },
            timestamp: self.timestamp,
            signature: self.signature,
        }
    }
}