web5_rust/agent/
commands.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
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
406
use super::Error;

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

use crate::dids::signing::{SignedObject, Signer};
use crate::dids::{DidResolver, Endpoint, Did};
use crate::dwn::structs::DwnResponse;

use simple_crypto::SecretKey;
use serde::Serialize;
use uuid::Uuid;

#[derive(Serialize, Debug, Clone)]
pub struct EnsureEmpty {
    responses: Responses
}

impl EnsureEmpty {
    pub fn new(responses: Responses) -> Self {
        EnsureEmpty{responses}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for EnsureEmpty {
    async fn process(
        self: Box<Self>, uuid: Uuid, _: Endpoint, _: &mut CompilerMemory
    ) -> Result<Tasks<'a>, Error> {
        for response in self.responses {
            if response.downcast_ref::<()>().is_some() {} else {
                (*response.downcast::<DwnResponse>()?).into_empty()?;
            }
        }
        Task::completed(uuid, ())
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct CreatePrivate {}
impl CreatePrivate {
    pub fn create(
        ep: Endpoint, mem: &mut CompilerMemory, record: Record, p_opts: Option<&PermissionOptions>
    ) -> Result<(PermissionSet, AgentRequest), Error> {
        let protocol = mem.get_protocol(&record.protocol)?;
        let perms = mem.key.get_perms(&record.path, Some(protocol))?;
        let min_perms = protocol.subset_permission(perms.clone(), None)?;
        let req = AgentRequest::create_private(0, perms.clone(), p_opts, protocol, record.payload)?;

        mem.record_info.insert((ep, record.path.clone()), (protocol.clone(), perms));

        Ok((min_perms, req))
    }

    pub fn create_child(
        ep: Endpoint, mem: &mut CompilerMemory,
        parent_perms: &PermissionSet, child_perms: &PermissionSet, index: usize
    ) -> Result<(AgentRequest, AgentRequest), Error> {
        let index_key = (ep, parent_perms.path.clone());
        let index = mem.create_index.get(&index_key).copied().unwrap_or(index);
        mem.create_index.insert(index_key, index+1);

        let index_perms = mem.key.get_perms(&parent_perms.path.index(), None)?;
        let index_req = AgentRequest::update_index(index_perms, index+1)?;
        let child_req = AgentRequest::create_private_child(0, parent_perms, child_perms, index)?;
        Ok((index_req, child_req))
    }
}

#[derive(Serialize, Debug, Clone)]
pub enum ReadPrivate {
    #[allow(non_camel_case_types)]
    child(RecordPath, usize),
    ChildInfo(Responses, usize),
    #[allow(non_camel_case_types)]
    path(RecordPath),
    #[allow(non_camel_case_types)]
    new(Box<PermissionSet>, bool),
    Complete(Responses, Box<PermissionSet>, bool),
}

impl ReadPrivate {
    fn read_private(
        perms: &PermissionSet, mem: &CompilerMemory, response: &DwnResponse
    ) -> Result<Option<PrivateRecord>, Error> {
        let discover = perms.discover.public_key();
        let create = perms.create.public_key();
        let read = perms.read.secret_key().ok_or(Error::invalid_auth("Read"))?;

        if let DwnResponse::ReadPrivate(item) = response {
            if let Some(item) = item {
                let dc = read.decrypt(&item.payload)?;
                let signed = serde_json::from_slice::<SignedObject<PrivateRecord>>(&dc)?;
                let mut record = signed.verify_with_key(&create)?;
                let protocol = mem.get_protocol(&record.protocol)?;
                let perms = protocol.trim_permission(perms.clone());
                let delete = perms.delete.as_ref().map(|d| d.public_key());
                perms.validate(&record.perms)?;
                protocol.validate_payload(&record.payload)?;
                protocol.validate_permission(&record.perms)?;
                if item.discover != discover || item.delete != delete {
                    return Err(Error::bad_response("Internal and External Key Mismatch"));
                }
                record.perms = record.perms.combine(perms)?;
                Ok(Some(record))
            } else {Ok(None)}
        } else {Err(Error::bad_response(&format!("Expected ReadPrivate(_) got {:?}", response)))}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for ReadPrivate {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::child(parent_path, index) => {
                let callback = move |r: Responses| {Self::ChildInfo(r, index)};
                Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                    Task::ready(ep, ReadInfo::new(parent_path, PermissionOptions::read_child()))
                ])
            },
            Self::ChildInfo(mut responses, index) => {
                let info = *responses.remove(0).downcast::<RecordInfo>()?;
                let perms = info.1.pointer(index)?;
                Task::next(uuid, ep, Self::new(Box::new(perms), true))
            },
            Self::path(path) => {
                if path.is_empty() {
                    let protocol = SystemProtocols::root();
                    Task::completed(uuid, Some(Box::new(
                        PrivateRecord::new(mem.key.get_perms_from_slice(&[], Some(&protocol))?, protocol.uuid(), Vec::new())
                    )))
                } else {
                    let perms = mem.key.get_perms(&path, None)?;
                    Task::next(uuid, ep, Self::new(Box::new(perms), true))
                }
            },
            Self::new(perms, resolve) => {
                let req = AgentRequest::read_private(perms.discover())?;
                let callback = move |r: Responses| {Self::Complete(r, Box::new(*perms), resolve)};
                Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                    Task::Request(ep, req)
                ])
            }
            Self::Complete(mut results, perms, resolve) => {
                let res = results.remove(0).downcast::<DwnResponse>()?;
                let record = Self::read_private(&perms, mem, &res).ok().flatten();
                let record = if let Some(record) = record {
                    if resolve && record.protocol == SystemProtocols::perm_pointer().uuid() {
                        let perms: PermissionSet = serde_json::from_slice(&record.payload)?;
                        let req = AgentRequest::read_private(perms.discover())?;
                        let callback = move |r: Responses| {Self::Complete(r, Box::new(perms), false)};
                        return Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
                            Task::Request(ep, req)
                        ]);
                    }
                    if let Ok(protocol) = mem.get_protocol(&record.protocol) {
                        mem.record_info.insert(
                            (ep.clone(), perms.path.clone()),
                            (protocol.clone(), record.perms.clone())
                        );
                    }
                    Some(Box::new(record))
                } else {None};
                Task::completed(uuid, record)
            },
        }
    }
}

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

#[async_trait::async_trait]
impl<'a> Command<'a> for ReadInfo {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(path, p_opts) => {
                match mem.record_info.get(&(ep.clone(), path.clone())) {
                    Some(info) if info.clone().1.subset(&p_opts).is_ok() => {
                        Task::completed(uuid, info.clone())
                    },
                    _ => {
                        Task::waiting(uuid, ep.clone(), Callback::new(Self::Complete), vec![
                            Task::ready(ep, ReadPrivate::path(path))
                        ])
                    }
                }
            }
            Self::Complete(mut results) => {
                let record = *results.remove(0).downcast::<Option<Box<PrivateRecord>>>()?;
                if let Some(record) = record {
                    Task::completed(uuid, (mem.get_protocol(&record.protocol)?.clone(), record.perms))
                } else {Err(Error::not_found("Record information"))}
            },
        }
    }
}

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

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

#[async_trait::async_trait]
impl<'a> Command<'a> for ReadParent {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory
    ) -> Result<Tasks<'a>, Error> {
        let parent_path = self.path.parent()?;
        if parent_path.is_empty() {
            let protocol = SystemProtocols::root();
            Task::completed(uuid, Some(Box::new(
                PrivateRecord::new(mem.key.get_perms_from_slice(&[], Some(&protocol))?, protocol.uuid(), Vec::new())
            )))
        } else {
            let perms = mem.key.get_perms(&parent_path, None)?;
            Task::next(uuid, ep, ReadPrivate::new(Box::new(perms), true))
        }
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct NextIndex {
    discover_child: SecretKey,
    index: usize,
    results: Option<Responses>,
}

impl NextIndex {
    pub fn new(
        discover_child: SecretKey,
        index: usize,
        results: Option<Responses>,
    ) -> Self {
        NextIndex{discover_child, index, results}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for NextIndex {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, _: &mut CompilerMemory
    ) -> Result<Tasks<'a>, Error> {
        let discover_child = self.discover_child;
        let mut index = self.index;
        if let Some(mut results) = self.results {
            if *results.remove(0).downcast::<bool>()? {
                index += 1;
            } else {
                return Task::completed(uuid, index);
            }
        }
        let discover = discover_child.derive_usize(self.index)?;
        let callback = move |r: Responses| {NextIndex::new(discover_child, index, Some(r))};
        Task::waiting(uuid, ep.clone(), Callback::new(callback), vec![
            Task::Ready(ep, Box::new(Exists::new(discover)))
        ])
    }
}

#[derive(Serialize, Debug, Clone)]
pub enum Exists {
    #[allow(non_camel_case_types)]
    path(RecordPath),
    #[allow(non_camel_case_types)]
    new(SecretKey),
    Complete(Responses),
}

#[async_trait::async_trait]
impl<'a> Command<'a> for Exists {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::path(path) => {
                let discover = mem.key.get_perms(&path, None)?.discover();
                Task::next(uuid, ep, Self::new(discover))
            },
            Self::new(key) => {
                Task::waiting(uuid, ep.clone(), Callback::new(Self::Complete), vec![
                    Task::Request(ep, AgentRequest::read_private(key)?)
                ])
            },
            Self::Complete(mut responses) => {
                let response = *responses.remove(0).downcast::<DwnResponse>()?;
                let exists: bool = matches!(response, DwnResponse::ReadPrivate(Some(_)));
                Task::completed(uuid, exists)
            }
        }
    }
}

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

#[async_trait::async_trait]
impl<'a> Command<'a> for ReadIndex {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory
    ) -> Result<Tasks<'a>, Error> {
        match *self {
            Self::new(path) => {
                let perms = mem.key.get_perms(&path.index(), Some(&SystemProtocols::usize()))?;
                Task::waiting(uuid, ep.clone(), Callback::new(Self::Complete), vec![
                    Task::ready(ep, ReadPrivate::new(Box::new(perms), false))
                ])
            }
            Self::Complete(mut results) => {
                let record = results.remove(0).downcast::<Option<Box<PrivateRecord>>>()?;
                let payload = record.map(|r|
                    serde_json::from_slice::<usize>(&r.payload)
                ).transpose()?;
                Task::completed(uuid, payload.unwrap_or_default())
            },
        }
    }
}

#[derive(Serialize, Debug, Clone)]
pub struct CreateDM {
    perms: PermissionSet,
    recipient: Did,
}

impl CreateDM {
    pub fn new(perms: PermissionSet, recipient: Did) -> Self {
        CreateDM{perms, recipient}
    }
}

#[async_trait::async_trait]
impl<'a> Command<'a> for CreateDM {
    async fn process(
        self: Box<Self>, uuid: Uuid, ep: Endpoint, mem: &mut CompilerMemory<'a>
    ) -> Result<Tasks<'a>, Error> {
        let signer = Signer::Left(mem.sig_key.clone());
        let (_, com_key) = mem.did_resolver.resolve_dwn_keys(&self.recipient).await?;
        Task::waiting(uuid, ep.clone(), Callback::new(EnsureEmpty::new), vec![
            Task::Request(ep, AgentRequest::create_dm(self.perms, signer, com_key)?)
        ])
    }

    async fn get_endpoints(
        &self, _: Vec<Did>, did_resolver: &dyn DidResolver
    ) -> Result<Vec<Endpoint>, Error> {
        let endpoints = did_resolver.get_endpoints(&[self.recipient.clone()]).await?;
        if endpoints.is_empty() {panic!("Did set had no endpoints");}
        Ok(endpoints)
    }
}

//  #[derive(Serialize, Debug, Clone)]
//  pub struct ReadDM {
//      timestamp: DateTime<Utc>
//  }

//  impl ReadDM {
//      pub fn new(timestamp: DateTime<Utc>) -> Self {
//          ReadDM{timestamp}
//      }
//  }

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