web5_rust/agent/
scripts.rs

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
use super::Error;

use super::compiler::CompilerMemory;
use super::permission::PermissionOptions;
use super::traits::Command;
use super::structs::{
    PrivateRecord,
    AgentRequest,
    RecordInfo,
    RecordPath,
    Responses,
    Callback,
    Record,
    Tasks,
    Task,
};
use super::commands;

use crate::dids::Endpoint;
use crate::dids::signing::Signer;
use crate::dwn::structs::{DwnResponse, PublicRecord};

use simple_database::database::{Filters, SortOptions, Index};
use simple_database::Indexable;

use serde::Serialize;
use uuid::Uuid;

#[derive(Serialize, Clone, Debug)]
pub enum CreatePrivate<'a> {
    #[allow(non_camel_case_types)]
    new(Record, Option<&'a PermissionOptions>),
    GetIndex(Responses, Record, Option<&'a PermissionOptions>),
    CreateRecord(Responses, Record, Option<&'a PermissionOptions>, Box<RecordInfo>),
}

#[async_trait::async_trait]
impl<'a> Command<'a> for CreatePrivate<'a> {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(record, p_opts) => {
                let path = record.path.clone();
                let parent_path = path.parent()?;

                let callback = move |r: Responses| {
                    Self::GetIndex(r, record, p_opts)
                };
                Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                    Task::ready(ep.clone(), commands::ReadInfo::new(
                        parent_path.clone(), PermissionOptions::create_child()
                    )),
                    Task::ready(ep.clone(), commands::ReadIndex::new(parent_path)),
                    Task::ready(ep, commands::Exists::path(path)),
                ])
            },
            Self::GetIndex(mut results, record, p_opts) => {
                if *results.remove(2).downcast::<bool>()? {
                    //TODO: Check if record is identical return conflict if otherwise
                    return Task::completed(uuid, ());
                }
                let index = *results.remove(1).downcast::<usize>()?;
                let info = *results.remove(0).downcast::<RecordInfo>()?;

                let discover_child = info.1.discover_child()?;

                let callback = move |r: Responses| {
                    Self::CreateRecord(r, record, p_opts, Box::new(info))
                };
                Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                    Task::ready(ep, commands::NextIndex::new(discover_child, index, None)),
                ])
            },
            Self::CreateRecord(mut results, record, p_opts, info) => {
                let index = *results.remove(0).downcast::<usize>()?;

                //Validate Parent Child
                let parent_protocol = &info.0;
                parent_protocol.validate_child(&record.protocol)?;

                let (min_perms, req) = commands::CreatePrivate::create(ep.clone(), mem, record, p_opts)?;
                let (i_req, c_req) = commands::CreatePrivate::create_child(ep.clone(), mem, &info.1, &min_perms, index)?;

                Task::waiting(uuid, ep.clone(), Callback::new(commands::EnsureEmpty::new), vec![
                    Task::Request(ep.clone(), i_req),
                    Task::Request(ep.clone(), c_req),
                    Task::Request(ep, req),
                ])
            }
        }
    }
}


#[derive(Serialize, Debug, Clone)]
pub enum ReadPrivate {
    #[allow(non_camel_case_types)]
    new(RecordPath),
    #[allow(non_camel_case_types)]
    child(RecordPath, usize),
    Complete(Responses),
}

#[async_trait::async_trait]
impl<'a> Command<'a> for ReadPrivate {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, _: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(path) => {
                Task::waiting(uuid, ep.clone(), Callback::new(Self::Complete), vec![
                    Task::ready(ep, commands::ReadPrivate::path(path))
                ])
            },
            Self::child(path, index) => {
                Task::waiting(uuid, ep.clone(), Callback::new(Self::Complete), vec![
                    Task::ready(ep, commands::ReadPrivate::child(path, index))
                ])
            },
            Self::Complete(mut results) => {
                let pr = results.remove(0).downcast::<(Option<Box<PrivateRecord>>, bool)>()?.0;
                Task::completed(uuid, pr.map(|pr| (*pr).into_record()))
            },
        }
    }
}

#[derive(Serialize, Debug, Clone)]
pub enum UpdatePrivate<'a> {
    #[allow(non_camel_case_types)]
    new(Record, Option<&'a PermissionOptions>),
    UpdateOrCreate(Responses, Record, Option<&'a PermissionOptions>),
}

#[async_trait::async_trait]
impl<'a> Command<'a> for UpdatePrivate<'a> {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(record, p_opts) => {
                let parent_path = record.path.parent()?;
                let record_path = record.path.clone();
                let callback = move |r: Responses| {Self::UpdateOrCreate(r, record, p_opts)};
                Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                    Task::ready(ep.clone(), commands::ReadInfo::new(
                        parent_path.clone(), PermissionOptions::create_child()
                    )),
                    Task::ready(ep.clone(), commands::ReadIndex::new(parent_path)),
                    Task::ready(ep, commands::ReadPrivate::path(record_path)),
                ])
            },
            Self::UpdateOrCreate(mut r, record, p_opts) => {
                match r.remove(2).downcast::<(Option<Box<PrivateRecord>>, bool)>()?.0 {
                    Some(e_record) => {
                        let protocol = mem.get_protocol(&record.protocol)?;
                        let perms = e_record.perms;
                        let req = AgentRequest::update_private(0, perms.clone(), p_opts, protocol, record.payload)?;
                        Task::waiting(uuid, ep.clone(), Callback::new(commands::EnsureEmpty::new),
                            vec![Task::Request(ep, req)]
                        )
                    },
                    None => {
                        r.insert(2, Box::new(false));
                        Task::next(uuid, ep,  CreatePrivate::GetIndex(r, record, p_opts))
                    }
                }
            },
        }
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct DeletePrivate {
    path: RecordPath
}

impl DeletePrivate {
    pub fn new(path: RecordPath) -> Self {
        DeletePrivate{path}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for DeletePrivate {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        let perms = mem.key.get_perms(&self.path, None)?;
        Task::waiting(uuid, ep.clone(), Callback::new(commands::EnsureEmpty::new), vec![
            Task::Request(ep, AgentRequest::delete_private(0, &perms)?)
        ])
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct CreatePublic {
    record: PublicRecord,
    signer: Option<Signer>,
}

impl CreatePublic {
    pub fn new(record: PublicRecord, signer: Option<Signer>) -> Self {
        CreatePublic{record, signer}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for CreatePublic {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        let protocol = mem.get_protocol(&self.record.protocol)?;
        protocol.validate_payload(&self.record.payload)?;
        let signer = self.signer.unwrap_or(Signer::Left(mem.sig_key.clone()));
        Task::waiting(uuid, ep.clone(), Callback::new(commands::EnsureEmpty::new), vec![
            Task::Request(ep, AgentRequest::create_public(0, self.record, signer)?)
        ])
    }
}

#[derive(Serialize, Debug, Clone)]
pub enum ReadPublic {
    #[allow(non_camel_case_types)]
    new(Filters, Option<SortOptions>),
    Completed(Responses, Filters, Option<SortOptions>)
}

#[async_trait::async_trait]
impl<'a> Command<'a> for ReadPublic {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(filters, sort_options) => {
                //TODO: I suspect that if sort options contains a field not in the filters it will crash the dwn
                let req = AgentRequest::read_public(filters.clone(), sort_options.clone())?;
                let callback = move |r: Responses| {Self::Completed(r, filters, sort_options)};
                Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                    Task::Request(ep, req)
                ])
            },
            Self::Completed(mut response, filters, sort_options) => {
                let response = *response.remove(0).downcast::<DwnResponse>()?;
                if let DwnResponse::ReadPublic(mut records) = response {
                    if let Some(sort_options) = sort_options {
                        sort_options.sort(&mut records)?;
                    }
                    let records = futures::future::join_all(records.into_iter().map(|item| async {
                        item.0.verify(mem.did_resolver, None).await.ok()?;
                        if !filters.filter(&item.secondary_keys()) {return None;}
                        let record = item.0.unwrap();
                        let protocol = mem.get_protocol(&record.protocol).ok()?;
                        protocol.validate_payload(&record.payload).ok()?;
                        Some(record)
                    })).await;
                    let records = records.into_iter().flatten().collect::<Vec<_>>();
                    Task::completed(uuid, records)
                } else {Err(Error::bad_response("Expected ReadPublic"))}
            }
        }
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct UpdatePublic {
    record: PublicRecord,
    signer: Option<Signer>,
}

impl UpdatePublic {
    pub fn new(record: PublicRecord, signer: Option<Signer>) -> Self {
        UpdatePublic{record, signer}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for UpdatePublic {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        let protocol = mem.get_protocol(&self.record.protocol)?;
        protocol.validate_payload(&self.record.payload)?;
        let signer = self.signer.unwrap_or(Signer::Left(mem.sig_key.clone()));
        Task::waiting(uuid, ep.clone(), Callback::new(commands::EnsureEmpty::new), vec![
            Task::Request(ep, AgentRequest::update_public(0, self.record, signer)?)
        ])
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct DeletePublic {
    uuid: Uuid,
    signer: Option<Signer>
}

impl DeletePublic {
    pub fn new(uuid: Uuid, signer: Option<Signer>) -> Self {
        DeletePublic{uuid, signer}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for DeletePublic {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        let signer = self.signer.unwrap_or(Signer::Left(mem.sig_key.clone()));
        Task::waiting(uuid, ep.clone(), Callback::new(commands::EnsureEmpty::new), vec![
            Task::Request(ep, AgentRequest::delete_public(0, self.uuid, signer)?)
        ])
    }
}

#[derive(Serialize, Debug, Clone)]
pub enum Scan {
    #[allow(non_camel_case_types)]
    new(RecordPath, usize),
    Completed(Responses),
}

#[async_trait::async_trait]
impl<'a> Command<'a> for Scan {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, _: &mut CompilerMemory
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(path, start) => {
                Task::waiting(uuid, ep.clone(), Callback::new(Self::Completed), vec![
                    Task::ready(ep, commands::Scan::new(path, start))
                ])
            },
            Self::Completed(mut responses) => {
                let records = *responses.remove(0).downcast::<Vec<PrivateRecord>>()?;
                Task::completed(uuid,
                    records.into_iter().map(|pr| pr.into_record()).collect::<Vec<_>>()
                )
            }
        }
    }
}