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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Copyright 2019 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.

mod login_packet;

pub use self::login_packet::{LoginPacket, MAX_LOGIN_PACKET_BYTES};
use crate::{
    AData, ADataAddress, ADataAppendOperation, ADataIndex, ADataOwner, ADataPubPermissions,
    ADataUnpubPermissions, ADataUser, AppPermissions, Coins, Error, IData, IDataAddress, MData,
    MDataAddress, MDataEntryActions, MDataPermissionSet, PublicKey, Response, TransactionId,
    XorName,
};
use serde::{Deserialize, Serialize};
use std::fmt;

/// The type of a `Request`.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
pub enum RequestType {
    /// Request is a Get for public data.
    PublicGet,
    /// Request is a Get for private data.
    PrivateGet,
    /// Request is a Mutation.
    Mutation,
    /// Request is a Transaction.
    Transaction,
}

/// RPC Request that is sent to vaults.
#[allow(clippy::large_enum_variant)]
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub enum Request {
    //
    // ===== Immutable Data =====
    //
    /// Put ImmutableData.
    PutIData(IData),
    /// Get ImmutableData.
    GetIData(IDataAddress),
    /// Delete unpublished ImmutableData.
    DeleteUnpubIData(IDataAddress),
    //
    // ===== Mutable Data =====
    //
    /// Put MutableData.
    PutMData(MData),
    /// Get MutableData.
    GetMData(MDataAddress),
    /// Get MutableData value.
    GetMDataValue {
        /// MutableData address.
        address: MDataAddress,
        /// Key to get.
        key: Vec<u8>,
    },
    /// Delete MutableData.
    DeleteMData(MDataAddress),
    /// Get MutableData shell.
    GetMDataShell(MDataAddress),
    /// Get MutableData version.
    GetMDataVersion(MDataAddress),
    /// List MutableData entries.
    ListMDataEntries(MDataAddress),
    /// List MutableData keys.
    ListMDataKeys(MDataAddress),
    /// List MutableData values.
    ListMDataValues(MDataAddress),
    /// Set MutableData user permissions.
    SetMDataUserPermissions {
        /// MutableData address.
        address: MDataAddress,
        /// User to set permissions for.
        user: PublicKey,
        /// New permissions.
        permissions: MDataPermissionSet,
        /// Version to set.
        version: u64,
    },
    /// Delete MutableData user permissions.
    DelMDataUserPermissions {
        /// MutableData address.
        address: MDataAddress,
        /// User to delete permissions for.
        user: PublicKey,
        /// Version to delete.
        version: u64,
    },
    /// List MutableData permissions.
    ListMDataPermissions(MDataAddress),
    /// Get MutableData permissions for a user.
    ListMDataUserPermissions {
        /// MutableData address.
        address: MDataAddress,
        /// User to get permissions for.
        user: PublicKey,
    },
    /// Mutate MutableData entries.
    MutateMDataEntries {
        /// MutableData address.
        address: MDataAddress,
        /// Mutation actions to perform.
        actions: MDataEntryActions,
    },
    //
    // ===== Append Only Data =====
    //
    /// Put a new AppendOnlyData onto the network.
    PutAData(AData),
    /// Get AppendOnlyData from the network.
    GetAData(ADataAddress),
    /// Get AppendOnlyData shell at a certain point in history (`data_index` refers to the list of
    /// data).
    GetADataShell {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Index of the data at which to get the shell.
        data_index: ADataIndex,
    },
    /// Delete an unpublished `AppendOnlyData`.
    ///
    /// This operation MUST return an error if applied to published AppendOnlyData. Only the current
    /// owner(s) can perform this action.
    DeleteAData(ADataAddress),
    /// Get a range of entries from an AppendOnlyData object on the network.
    GetADataRange {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Range of entries to fetch.
        ///
        /// For example, get 10 last entries:
        /// range: (Index::FromEnd(10), Index::FromEnd(0))
        ///
        /// Get all entries:
        /// range: (Index::FromStart(0), Index::FromEnd(0))
        ///
        /// Get first 5 entries:
        /// range: (Index::FromStart(0), Index::FromStart(5))
        range: (ADataIndex, ADataIndex),
    },
    /// Get AppendOnlyData value.
    GetADataValue {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Key to get.
        key: Vec<u8>,
    },
    /// Get current indices: data, owners, permissions.
    GetADataIndices(ADataAddress),
    /// Get an entry with the current index.
    GetADataLastEntry(ADataAddress),
    /// List all permissions at the provided index.
    GetADataPermissions {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Permissions index.
        permissions_index: ADataIndex,
    },
    /// Get published permissions for a specified user(s).
    GetPubADataUserPermissions {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Permissions index.
        permissions_index: ADataIndex,
        /// User to get permissions for.
        user: ADataUser,
    },
    /// Get unpublished permissions for a specified user(s).
    GetUnpubADataUserPermissions {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Permissions index.
        permissions_index: ADataIndex,
        /// User to get permissions for.
        public_key: PublicKey,
    },
    /// Get owners at the provided index.
    GetADataOwners {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Onwers index.
        owners_index: ADataIndex,
    },
    /// Add a new published `permissions` entry.
    AddPubADataPermissions {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Published permissions.
        permissions: ADataPubPermissions,
        /// Index to add to.
        permissions_index: u64,
    },
    /// Add a new unpublished `permissions` entry.
    AddUnpubADataPermissions {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// Unpublished permissions.
        permissions: ADataUnpubPermissions,
        /// Index to add to.
        permissions_index: u64,
    },
    /// Add a new `owners` entry. Only the current owner(s) can perform this action.
    SetADataOwner {
        /// AppendOnlyData address.
        address: ADataAddress,
        /// New owner.
        owner: ADataOwner,
        /// Owners index.
        owners_index: u64,
    },
    /// Append sequenced AppendOnlyData at the given index.
    AppendSeq {
        /// Entries to append.
        append: ADataAppendOperation,
        /// Index.
        index: u64,
    },
    /// Append unsequenced AppendOnlyData.
    AppendUnseq(ADataAppendOperation),
    //
    // ===== Coins =====
    //
    /// Balance transfer.
    TransferCoins {
        /// The destination to transfer to.
        destination: XorName,
        /// The amount in coins to transfer.
        amount: Coins,
        /// The ID of the transaction.
        transaction_id: TransactionId,
    },
    /// Get current wallet balance.
    GetBalance,
    /// Create a new coin balance.
    CreateBalance {
        /// The new owner of the balance.
        new_balance_owner: PublicKey,
        /// The new balance amount in coins.
        amount: Coins,
        /// The ID of the transaction.
        transaction_id: TransactionId,
    },
    //
    // ===== Login Packet =====
    //
    /// Create a login packet.
    CreateLoginPacket(LoginPacket),
    /// Create a login packet for a given user and transfer some initial coins.
    CreateLoginPacketFor {
        /// The new owner of the login packet.
        new_owner: PublicKey,
        /// The new balance amount in coins.
        amount: Coins,
        /// The ID of the transaction.
        transaction_id: TransactionId,
        /// The new login packet.
        new_login_packet: LoginPacket,
    },
    /// Update a login packet.
    UpdateLoginPacket(LoginPacket),
    /// Get an encrypted login packet.
    GetLoginPacket(XorName),
    //
    // ===== Client (Owner) to SrcElders =====
    //
    /// List authorised keys and version stored by Elders.
    ListAuthKeysAndVersion,
    /// Insert an authorised key (for an app, user, etc.).
    InsAuthKey {
        /// Authorised key to be inserted
        key: PublicKey,
        /// Incremented version
        version: u64,
        /// Permissions
        permissions: AppPermissions,
    },
    /// Delete an authorised key.
    DelAuthKey {
        /// Authorised key to be deleted
        key: PublicKey,
        /// Incremented version
        version: u64,
    },
}

impl Request {
    /// Get the `RequestType` of this `Request`.
    pub fn get_type(&self) -> RequestType {
        use Request::*;

        match *self {
            // IData requests

            GetIData(address) => {
                if address.is_pub() {
                    RequestType::PublicGet
                } else {
                    RequestType::PrivateGet
                }
            }

            // AData requests

            GetAData(address)
            | GetADataShell { address, .. }
            | GetADataRange { address, .. }
            | GetADataValue { address, .. }
            | GetADataIndices(address)
            | GetADataLastEntry(address)
            | GetADataPermissions { address, .. }
            | GetPubADataUserPermissions { address, .. }
            | GetUnpubADataUserPermissions { address, .. }
            | GetADataOwners { address, .. } => {
                if address.is_pub() {
                    RequestType::PublicGet
                } else {
                    RequestType::PrivateGet
                }
            }

            // MData requests (always unpub)

            GetMData(_)
            | GetMDataValue { .. }
            | GetMDataShell(_)
            | GetMDataVersion(_)
            | ListMDataEntries(_)
            | ListMDataKeys(_)
            | ListMDataValues(_)
            | ListMDataPermissions(_)
                | ListMDataUserPermissions { .. } => RequestType::PrivateGet,

            // Coins
            GetBalance |
            // Login packet
            GetLoginPacket(..) |
            // Client (Owner) to SrcElders
            ListAuthKeysAndVersion => RequestType::PrivateGet,

            // Transaction

            // Coins
            TransferCoins { .. } | CreateBalance { .. } |
            // Login Packet
            CreateLoginPacketFor { .. } => {
                RequestType::Transaction
            }

            // Mutation

            // IData
            PutIData(_) |
            DeleteUnpubIData(_) |
            // MData
            PutMData(_) |
            DeleteMData(_) |
            SetMDataUserPermissions { .. } |
            DelMDataUserPermissions { .. } |
            MutateMDataEntries { .. } |
            // AData
            PutAData(_) |
            DeleteAData(_) |
            AddPubADataPermissions { .. } |
            AddUnpubADataPermissions { .. } |
            SetADataOwner { .. } |
            AppendSeq { .. } |
            AppendUnseq(_) |
            // Login Packet
            CreateLoginPacket { .. } |
            UpdateLoginPacket { .. } |
            // Client (Owner) to SrcElders
            InsAuthKey { .. } |
            DelAuthKey { .. } => RequestType::Mutation,
        }
    }

    /// Creates a Response containing an error, with the Response variant corresponding to the
    /// Request variant.
    pub fn error_response(&self, error: Error) -> Response {
        use Request::*;

        match *self {
            // IData
            GetIData(_) => Response::GetIData(Err(error)),
            // MData
            GetMData(_) => Response::GetMData(Err(error)),
            GetMDataValue { .. } => Response::GetMDataValue(Err(error)),
            GetMDataShell(_) => Response::GetMDataShell(Err(error)),
            GetMDataVersion(_) => Response::GetMDataVersion(Err(error)),
            ListMDataEntries(_) => Response::ListMDataEntries(Err(error)),
            ListMDataKeys(_) => Response::ListMDataKeys(Err(error)),
            ListMDataValues(_) => Response::ListMDataValues(Err(error)),
            ListMDataPermissions(_) => Response::ListMDataPermissions(Err(error)),
            ListMDataUserPermissions { .. } => Response::ListMDataUserPermissions(Err(error)),
            // AData
            GetAData(_) => Response::GetAData(Err(error)),
            GetADataShell { .. } => Response::GetADataShell(Err(error)),
            GetADataValue { .. } => Response::GetADataValue(Err(error)),
            GetADataRange { .. } => Response::GetADataRange(Err(error)),
            GetADataIndices(_) => Response::GetADataIndices(Err(error)),
            GetADataLastEntry(_) => Response::GetADataLastEntry(Err(error)),
            GetADataPermissions { .. } => Response::GetADataPermissions(Err(error)),
            GetPubADataUserPermissions { .. } => Response::GetPubADataUserPermissions(Err(error)),
            GetUnpubADataUserPermissions { .. } => {
                Response::GetUnpubADataUserPermissions(Err(error))
            }
            GetADataOwners { .. } => Response::GetADataOwners(Err(error)),
            // Coins
            GetBalance => Response::GetBalance(Err(error)),
            // Login Packet
            GetLoginPacket(..) => Response::GetLoginPacket(Err(error)),
            // Client (Owner) to SrcElders
            ListAuthKeysAndVersion => Response::ListAuthKeysAndVersion(Err(error)),

            // Transaction

            // Coins
            TransferCoins { .. } | CreateBalance { .. }
            // Login Packet
            | CreateLoginPacketFor { .. } => Response::Transaction(Err(error)),

            // Mutation

            // IData
            PutIData(_) |
            DeleteUnpubIData(_) |
            // MData
            PutMData(_) |
            DeleteMData(_) |
            SetMDataUserPermissions { .. } |
            DelMDataUserPermissions { .. } |
            MutateMDataEntries { .. } |
            // AData
            PutAData(_) |
            DeleteAData(_) |
            AddPubADataPermissions { .. } |
            AddUnpubADataPermissions { .. } |
            SetADataOwner { .. } |
            AppendSeq { .. } |
            AppendUnseq(_) |
            // Login Packet
            CreateLoginPacket { .. } |
            UpdateLoginPacket { .. } |
            // Client (Owner) to SrcElders
            InsAuthKey { .. } |
            DelAuthKey { .. } => Response::Mutation(Err(error)),

        }
    }
}

impl fmt::Debug for Request {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use Request::*;

        write!(
            formatter,
            "Request::{}",
            match *self {
                // IData
                PutIData(_) => "PutIData",
                GetIData(_) => "GetIData",
                DeleteUnpubIData(_) => "DeleteUnpubIData",
                // MData
                PutMData(_) => "PutMData",
                GetMData(_) => "GetMData",
                GetMDataValue { .. } => "GetMDataValue",
                DeleteMData(_) => "DeleteMData",
                GetMDataShell(_) => "GetMDataShell",
                GetMDataVersion(_) => "GetMDataVersion",
                ListMDataEntries(_) => "ListMDataEntries",
                ListMDataKeys(_) => "ListMDataKeys",
                ListMDataValues(_) => "ListMDataValues",
                SetMDataUserPermissions { .. } => "SetMDataUserPermissions",
                DelMDataUserPermissions { .. } => "DelMDataUserPermissions",
                ListMDataPermissions(_) => "ListMDataPermissions",
                ListMDataUserPermissions { .. } => "ListMDataUserPermissions",
                MutateMDataEntries { .. } => "MutateMDataEntries",
                // AData
                PutAData(_) => "PutAData",
                GetAData(_) => "GetAData",
                GetADataShell { .. } => "GetADataShell",
                GetADataValue { .. } => "GetADataValue ",
                DeleteAData(_) => "DeleteAData",
                GetADataRange { .. } => "GetADataRange",
                GetADataIndices(_) => "GetADataIndices",
                GetADataLastEntry(_) => "GetADataLastEntry",
                GetADataPermissions { .. } => "GetADataPermissions",
                GetPubADataUserPermissions { .. } => "GetPubADataUserPermissions",
                GetUnpubADataUserPermissions { .. } => "GetUnpubADataUserPermissions",
                GetADataOwners { .. } => "GetADataOwners",
                AddPubADataPermissions { .. } => "AddPubADataPermissions",
                AddUnpubADataPermissions { .. } => "AddUnpubADataPermissions",
                SetADataOwner { .. } => "SetADataOwner",
                AppendSeq { .. } => "AppendSeq",
                AppendUnseq(_) => "AppendUnseq",
                // Coins
                TransferCoins { .. } => "TransferCoins",
                GetBalance => "GetBalance",
                CreateBalance { .. } => "CreateBalance",
                // Login Packet
                CreateLoginPacket { .. } => "CreateLoginPacket",
                CreateLoginPacketFor { .. } => "CreateLoginPacketFor",
                UpdateLoginPacket { .. } => "UpdateLoginPacket",
                GetLoginPacket(..) => "GetLoginPacket",
                // Client (Owner) to SrcElders
                ListAuthKeysAndVersion => "ListAuthKeysAndVersion",
                InsAuthKey { .. } => "InsAuthKey",
                DelAuthKey { .. } => "DelAuthKey",
            }
        )
    }
}