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
//! The confidential docs pallet provides the backend services and metadata
//! storage for the confidential docs solution

#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

// #[cfg(feature = "runtime-benchmarks")]
// mod benchmarking;

mod functions;
pub mod types;

#[frame_support::pallet]
pub mod pallet {
  //! Provides the backend services and metadata storage for the confidential docs solution
  use crate::types::*;
  use frame_support::pallet_prelude::*;
  use frame_system::pallet_prelude::*;

  const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

  #[pallet::config]
  pub trait Config: frame_system::Config {
    type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

    type RemoveOrigin: EnsureOrigin<Self::RuntimeOrigin>;

    /// Maximum number of confidential documents that a user can own
    #[pallet::constant]
    type MaxOwnedDocs: Get<u32>;
    /// Maximum number of confidential documents that a user can share
    #[pallet::constant]
    type MaxSharedFromDocs: Get<u32>;
    /// Maximum number of confidential documents that can be shared to a user
    #[pallet::constant]
    type MaxSharedToDocs: Get<u32>;
    /// Minimum length for a document name
    #[pallet::constant]
    type DocNameMinLen: Get<u32>;
    /// Maximum length for a document name
    #[pallet::constant]
    type DocNameMaxLen: Get<u32>;
    /// Minimum length for a document description
    #[pallet::constant]
    type DocDescMinLen: Get<u32>;
    /// Maximum length for a document description
    #[pallet::constant]
    type DocDescMaxLen: Get<u32>;
    /// Minimum length for a groupName
    #[pallet::constant]
    type GroupNameMinLen: Get<u32>;
    /// Maximum length for a groupName
    #[pallet::constant]
    type GroupNameMaxLen: Get<u32>;
    /// Maximum groups a user can belong to
    #[pallet::constant]
    type MaxMemberGroups: Get<u32>;
  }

  #[pallet::pallet]
  #[pallet::storage_version(STORAGE_VERSION)]
  #[pallet::generate_store(pub(super) trait Store)]
  pub struct Pallet<T>(_);

  #[pallet::storage]
  #[pallet::getter(fn vaults)]
  pub(super) type Vaults<T: Config> = StorageMap<
    _,
    Blake2_256,
    UserId, //user identifier
    Vault<T>,
    OptionQuery,
  >;

  #[pallet::storage]
  #[pallet::getter(fn public_keys)]
  pub(super) type PublicKeys<T: Config> =
    StorageMap<_, Blake2_256, T::AccountId, PublicKey, OptionQuery>;

  #[pallet::storage]
  #[pallet::getter(fn users_ids)]
  pub(super) type UserIds<T: Config> = StorageMap<_, Identity, [u8; 32], UserId, OptionQuery>;

  #[pallet::storage]
  #[pallet::getter(fn owned_docs)]
  pub(super) type OwnedDocs<T: Config> = StorageMap<_, Blake2_256, CID, OwnedDoc<T>, OptionQuery>;

  #[pallet::storage]
  #[pallet::getter(fn owned_docs_by_owner)]
  pub(super) type OwnedDocsByOwner<T: Config> =
    StorageMap<_, Blake2_256, T::AccountId, BoundedVec<CID, T::MaxOwnedDocs>, ValueQuery>;

  #[pallet::storage]
  #[pallet::getter(fn shared_docs)]
  pub(super) type SharedDocs<T: Config> = StorageMap<_, Blake2_256, CID, SharedDoc<T>, OptionQuery>;

  #[pallet::storage]
  #[pallet::getter(fn shared_docs_by_to)]
  pub(super) type SharedDocsByTo<T: Config> =
    StorageMap<_, Blake2_256, T::AccountId, BoundedVec<CID, T::MaxSharedToDocs>, ValueQuery>;

  #[pallet::storage]
  #[pallet::getter(fn shared_docs_by_from)]
  pub(super) type SharedDocsByFrom<T: Config> =
    StorageMap<_, Blake2_256, T::AccountId, BoundedVec<CID, T::MaxSharedFromDocs>, ValueQuery>;

  #[pallet::storage]
  #[pallet::getter(fn groups)]
  pub(super) type Groups<T: Config> = StorageMap<
    _,
    Blake2_256,
    // Group Account Id
    T::AccountId,
    Group<T>,
    OptionQuery,
  >;

  #[pallet::storage]
  #[pallet::getter(fn group_members)]
  pub(super) type GroupMembers<T: Config> = StorageDoubleMap<
    _,
    Blake2_256,
    // Group Account Id
    T::AccountId,
    Blake2_256,
    // Member Account Id
    T::AccountId,
    GroupMember<T>,
    OptionQuery,
  >;

  #[pallet::storage]
  #[pallet::getter(fn member_groups)]
  pub(super) type MemberGroups<T: Config> = StorageMap<
    _,
    Blake2_256,
    // Member Account Id
    T::AccountId,
    // Group Account Ids
    BoundedVec<T::AccountId, T::MaxMemberGroups>,
    ValueQuery,
  >;

  #[pallet::event]
  #[pallet::generate_deposit(pub(super) fn deposit_event)]
  pub enum Event<T: Config> {
    /// Vault stored
    VaultStored(UserId, PublicKey, Vault<T>),
    /// Owned confidential document stored
    OwnedDocStored(OwnedDoc<T>),
    /// Owned confidential document removed
    OwnedDocRemoved(OwnedDoc<T>),
    /// Shared confidential document stored
    SharedDocStored(SharedDoc<T>),
    /// Shared confidential document metadata updated
    SharedDocUpdated(SharedDoc<T>),
    /// Shared confidential document removed
    SharedDocRemoved(SharedDoc<T>),
    /// Group created
    GroupCreated(Group<T>),
    /// Group Member added
    GroupMemberAdded(GroupMember<T>),
    /// Group Member Removed
    GroupMemberRemoved(GroupMember<T>),
  }

  #[pallet::error]
  pub enum Error<T> {
    /// Empty CID
    CIDNoneValue,
    /// Document Name is too short
    DocNameTooShort,
    /// Document Desc is too short
    DocDescTooShort,
    /// Group Name is too short
    GroupNameTooShort,
    /// Errors should have helpful documentation associated with them.
    StorageOverflow,
    /// Origin is not the owner of the user id
    NotOwnerOfUserId,
    /// Origin is not the owner of the vault
    NotOwnerOfVault,
    /// The user already has a vault
    UserAlreadyHasVault,
    /// The user already has a public key
    AccountAlreadyHasPublicKey,
    /// User is not document owner
    NotDocOwner,
    /// User is not document whom with the document was shared
    NotDocSharee,
    /// CID not found
    CIDNotFound,
    /// Document not found
    DocNotFound,
    /// The document has already been shared
    DocAlreadyShared,
    /// The group already exits
    GroupAlreadyExists,
    /// Shared with self
    DocSharedWithSelf,
    /// Account has no public key
    AccountHasNoPublicKey,
    /// Max owned documents has been exceeded
    ExceedMaxOwnedDocs,
    /// Max documents shared with the "to" account has been exceeded
    ExceedMaxSharedToDocs,
    /// Max documents shared with the "from" account has been exceeded
    ExceedMaxSharedFromDocs,
    /// Max groups an account can have has been exceeded
    ExceedMaxMemberGroups,
    /// Group does not exist
    GroupDoesNotExist,
    /// User does not have permission to perform the operation
    NoPermission,
    /// User already a group member
    UserAlreadyGroupMember,
    /// Group member does not exist
    GroupMemberDoesNotExist,
    /// Member group does not exist
    MemberGroupDoesNotExist,
    /// Member group already exits
    MemberGroupAlreadyExists,
    /// Can not add member as group owner
    CanNotAddMemberAsGroupOwner,
  }

  #[pallet::call]
  impl<T: Config> Pallet<T> {
    /// Create/Update a vault
    ///
    /// Creates/Updates the calling user's vault and sets their public cipher key
    /// .
    /// ### Parameters:
    /// - `origin`: The user that is configuring their vault
    /// - `user_id`: User identifier generated from their login method, their address if using
    /// native login or user id if using SSO
    /// - `public key`: The users cipher public key
    /// - `cid`: The IPFS CID that contains the vaults data
    #[pallet::call_index(1)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn set_vault(
      origin: OriginFor<T>,
      user_id: UserId,
      public_key: PublicKey,
      cid: CID,
    ) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_set_vault(who, user_id, public_key, cid)
    }

    /// Create/Update an owned document
    ///
    /// Creates a new owned document or updates an existing owned document's metadata
    /// .
    /// ### Parameters:
    /// - `origin`: The user that is creating/updating an owned document
    /// - `owned_doc`: Metadata related to the owned document
    #[pallet::call_index(2)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn set_owned_document(origin: OriginFor<T>, owned_doc: OwnedDoc<T>) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_set_owned_document(who, owned_doc)
    }

    /// Remove an owned document
    ///
    /// Removes an owned document
    /// .
    /// ### Parameters:
    /// - `origin`: The owner of the document
    /// - `cid`: of the document to be removed
    #[pallet::call_index(3)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn remove_owned_document(origin: OriginFor<T>, cid: CID) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_remove_owned_document(who, cid)
    }

    /// Share a document
    ///
    /// Creates a shared document
    /// .
    /// ### Parameters:
    /// - `origin`: The user that is creating the shared document
    /// - `shared_doc`: Metadata related to the shared document
    #[pallet::call_index(4)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn share_document(origin: OriginFor<T>, shared_doc: SharedDoc<T>) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_share_document(who, shared_doc)
    }

    /// Update share document metadata
    ///
    /// Updates share document metadata, only the user with which the document
    /// was shared can update it
    /// .
    /// ### Parameters:
    /// - `origin`: The "to" user of the shared document
    /// - `shared_doc`: Metadata related to the shared document
    #[pallet::call_index(5)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn update_shared_document_metadata(
      origin: OriginFor<T>,
      shared_doc: SharedDoc<T>,
    ) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_update_shared_document_metadata(who, shared_doc)
    }

    /// Remove a shared document
    ///
    /// Removes a shared document, only the user with whom the document was
    /// is able to remove it
    /// .
    /// ### Parameters:
    /// - `origin`: The "to" user of the shared document
    /// - `cid`: of the document to be removed
    #[pallet::call_index(6)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn remove_shared_document(origin: OriginFor<T>, cid: CID) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_remove_shared_document(who, cid)
    }

    /// Create a group
    ///
    /// Creates a group that enables the sharing of documents with multiple users
    /// .
    /// ### Parameters:
    /// - `origin`: The user that is creating the group
    /// - `group`: AccountId of the group
    /// - `name`: Name of the group
    /// - `public_key`: Public key of the group
    /// - `cid`: cid of the document containing the private key of the group
    #[pallet::call_index(7)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn create_group(
      origin: OriginFor<T>,
      group: T::AccountId,
      name: GroupName<T>,
      public_key: PublicKey,
      cid: CID,
    ) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_create_group(who, group, name, public_key, cid)
    }

    /// Add group member
    ///
    /// Adds a member to a group only the owner and admins can add members
    /// .
    /// ### Parameters:
    /// - `origin`: The user that is adding the member to the group
    /// - `group_member`: GroupMember object containg the details of the member
    #[pallet::call_index(8)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn add_group_member(origin: OriginFor<T>, group_member: GroupMember<T>) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_add_group_member(who, group_member)
    }

    /// Remove group member
    ///
    /// Removes a member from a group the owner can remove any member, an admin only users
    /// added by them selfs
    /// .
    /// ### Parameters:
    /// - `origin`: The user that is removing the member from the group
    /// - `group`: AccountId of the group
    /// - `member`: AccountId of the user to remove from the group
    #[pallet::call_index(9)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn remove_group_member(
      origin: OriginFor<T>,
      group: T::AccountId,
      member: T::AccountId,
    ) -> DispatchResult {
      let who = ensure_signed(origin)?;
      Self::do_remove_group_member(who, group, member)
    }

    /// Kill all the stored data.
    ///
    /// This function is used to kill ALL the stored data.
    /// Use with caution!
    ///
    /// ### Parameters:
    /// - `origin`: The user who performs the action.
    ///
    /// ### Considerations:
    /// - This function is only available to the `admin` with sudo access.
    #[pallet::call_index(10)]
    #[pallet::weight(Weight::from_ref_time(10_000) + T::DbWeight::get().writes(1))]
    pub fn kill_storage(origin: OriginFor<T>) -> DispatchResult {
      T::RemoveOrigin::ensure_origin(origin.clone())?;
      let _ = <Vaults<T>>::clear(1000, None);
      let _ = <PublicKeys<T>>::clear(1000, None);
      let _ = <OwnedDocs<T>>::clear(1000, None);
      let _ = <OwnedDocsByOwner<T>>::clear(1000, None);
      let _ = <SharedDocs<T>>::clear(1000, None);
      let _ = <SharedDocsByTo<T>>::clear(1000, None);
      let _ = <SharedDocsByFrom<T>>::clear(1000, None);
      let _ = <Groups<T>>::clear(1000, None);
      let _ = <GroupMembers<T>>::clear(1000, None);
      let _ = <MemberGroups<T>>::clear(1000, None);
      Ok(())
    }
  }
}