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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// This file is part of Anagolay Foundation.

// Copyright (C) 2019-2022 Anagolay Foundation.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use cid::{multihash::MultihashGeneric, Cid};
use codec::{Decode, Encode};
use core::{any::type_name_of_val, str::pattern::Pattern};
use multibase::Base;
use multihash::{Blake3_256, Code, Hasher};
use sp_runtime::RuntimeDebug;
use sp_std::{vec, vec::Vec};

/// Private package used to hide the types that are not supposed to be referenced by dependent
/// crates
mod private {
  use super::Characters;
  use cid::Cid;
  use codec::{Decode, Encode};
  use core::convert::TryFrom;
  use sp_std::{convert::From, str, vec, vec::Vec};

  /// Generic ID, this is the content identifier of the payload, like worflow or proof. It's a
  /// multibase encoded CID string. It must be in a private module, aliased by the types of each
  /// respective entity id, since it's used in [`AnagolayVersionData`] to refer to any entity id.
  ///
  /// Id aliases are also an important indicator of what type of id is expected in which place:
  /// instead of writing documentation we can immediately show the user what the storage or the
  /// data model expects.
  ///
  /// It follows NewType pattern to provide conversion to and from Vec<u8> for (de)serialization but
  /// also to provide additional behaviour, like validation.
  #[derive(Encode, Decode, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)]
  #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
  pub struct GenericId(pub Vec<u8>);

  impl From<GenericId> for Vec<u8> {
    fn from(from: GenericId) -> Vec<u8> {
      from.0
    }
  }

  impl From<Vec<u8>> for GenericId {
    fn from(from: Vec<u8>) -> GenericId {
      GenericId(from)
    }
  }

  impl Default for GenericId {
    fn default() -> Self {
      vec![].into()
    }
  }

  impl GenericId {
    pub fn from(str: &str) -> Self {
      GenericId(str.as_bytes().to_vec())
    }

    /// Validate the CID. It will require a string to be multibase-decoded and then parsed as CID
    ///
    /// # Return
    /// An unit result if the validation is successful, a `Character` error with a description in
    /// case it fails
    pub fn validate(&self) -> Result<(), Characters> {
      multibase::decode(str::from_utf8(&self.0).unwrap())
        .map(|decoded| Cid::try_from(decoded.1).map_err(|_| Characters::from("Invalid CID")))
        .map(|_| ())
        .map_err(|_| Characters::from("Cannot decode CID"))
    }
  }
}
/// NewType pattern to handle strings.
/// It conveniently allows concatenation and deals with (de)serialization as well.
///
/// # Example
///
/// ```
/// use anagolay_support::Characters;
///
/// let chars: Characters = "hello".into();
///
/// assert_eq!(5, chars.len());
/// assert_eq!("hello2world", chars.concat_u8(2u8).concat("world").as_str());
/// ```
#[derive(Encode, Decode, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct Characters(Vec<u8>);

impl From<&str> for Characters {
  fn from(from: &str) -> Characters {
    Characters(from.as_bytes().to_vec())
  }
}

impl From<Vec<u8>> for Characters {
  fn from(from: Vec<u8>) -> Characters {
    Characters(from)
  }
}

impl Default for Characters {
  fn default() -> Self {
    vec![].into()
  }
}

impl Characters {
  /// Convenience method to create `Characters` from a string slice when the result type is implicit
  ///
  /// # Example
  ///
  /// ```
  /// use anagolay_support::Characters;
  /// let chars = Characters::from("hello");
  /// ```
  ///
  /// Most of the time, it's convenient to use `str.into()`
  ///
  /// ```
  /// use anagolay_support::Characters;
  /// let chars: Characters = "hello".into();
  /// ```
  pub fn from(str: &str) -> Self {
    Characters(str.as_bytes().to_vec())
  }

  /// # Return
  /// The `Characters` representation as a string slice
  pub fn as_str(&self) -> &str {
    sp_std::str::from_utf8(self.0.as_slice()).unwrap()
  }

  /// Append an unsigned integer to this `Characters`
  ///
  /// # Arguments
  /// * uint - The unsigned integer to append
  ///
  /// # Return
  /// This `Characters` with the argument appended
  pub fn concat_u8(mut self, uint: u8) -> Self {
    let mut n = uint;
    if n == 0 {
      self.0.append(&mut b"0".to_vec());
    } else {
      let mut buffer = [0u8; 100];
      let mut i = 0;
      while n > 0 {
        buffer[i] = (n % 10) as u8 + b'0';
        n /= 10;
        i += 1;
      }
      let slice = &mut buffer[..i];
      slice.reverse();
      self.0.append(&mut slice.to_vec());
    }
    self
  }

  /// Concatenate a string slice to this `Characters`
  ///
  /// # Arguments
  /// * other - The string slice to concatenate
  ///
  /// # Return
  /// This `Characters` with the argument concatenated
  pub fn concat(mut self, other: &str) -> Self {
    self.0.append(&mut other.as_bytes().to_vec());
    self
  }

  /// # Return
  /// This `Characters` length
  pub fn len(&self) -> usize {
    self.0.len()
  }

  /// Splits this `Characters` using pat as delimeter
  ///
  /// # Arguments
  /// * pat - Pattern to use as delimiter for the split
  ///
  /// # Return
  /// Collection of splits of this `Characters` using pat as delimeter
  pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Vec<Characters> {
    self.as_str().split(pat).map(|split| split.into()).collect()
  }
}

/// Placeholder for SSI and DID
pub type CreatorId = Characters;

/// The type of the values in the `ArtifactsByArtifactId` storage
pub type ArtifactId = private::GenericId;

/// The type used for an Operation ID
pub type OperationId = private::GenericId;

/// The type used for a Workflow ID
pub type WorkflowId = private::GenericId;

/// The type used for any entity Version ID
pub type VersionId = private::GenericId;

/// List of equipment that needs workflows generated
#[derive(Encode, Decode, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum ForWhat {
  /// We are creating it For what? This can be a part of the group
  GENERIC, // 0
  PHOTO,       // 1
  CAMERA,      // 2
  LENS,        // 3
  SMARTPHONE,  // 4
  USER,        // 5
  SYS,         // 6
  FLOWCONTROL, // 7
}

impl Default for ForWhat {
  fn default() -> Self {
    ForWhat::GENERIC
  }
}

/// Info, this is what gets stored. The Generic `A` is usally the `AccountId` and `B` is
/// `BlockNumber`
#[derive(Default, Encode, Decode, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct AnagolayRecord<T, A, B> {
  pub record: T,
  pub account_id: A,
  pub block_number: B,
}

/// The trait for the data field of an Anagolay entity.
pub trait AnagolayStructureData: Default + Encode + Clone + PartialEq + Eq {
  /// Computes cid of the data, after encoding it using parity SCALE codec
  ///
  /// # Examples
  ///
  /// ```
  /// use codec::{Decode, Encode};
  /// use anagolay_support::{AnagolayStructureData, AnagolayStructureExtra, Characters};
  /// # use anagolay_support::ArtifactId;
  ///
  /// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
  /// struct EntityData {
  ///   text: Vec<u8>
  /// };
  ///
  /// impl Default for EntityData {
  ///   fn default() -> Self {
  ///     EntityData {
  ///       text: b"".to_vec()
  ///     }
  ///   }
  /// }
  ///
  /// impl AnagolayStructureData for EntityData {
  ///   fn validate(&self) -> Result<(), Characters> {
  ///      Ok(())
  ///   }
  /// }
  ///
  /// let entity = EntityData {
  ///   text: b"hello".to_vec()
  /// };
  ///
  /// let cid = entity.to_cid();
  /// # assert_eq!(ArtifactId::from("bafkr4iac2luovbttsv5iftbg2zl4okalixafa2vjwtbmf6exgwiuvukhmi"), cid);
  /// ```
  fn to_cid(&self) -> private::GenericId {
    let hash = MultihashGeneric::wrap(
      Code::Blake3_256.into(),
      Blake3_256::digest(self.encode().as_slice()).as_ref(),
    )
    .unwrap();

    // RAW codec from the multiformats
    const RAW: u64 = 0x55;
    let cid = Cid::new_v1(RAW, hash);

    // create the string slice like `bafk...`
    let cid_str = multibase::encode(Base::Base32Lower, cid.to_bytes());

    // make the string slice into vec bytes, usually we use that
    private::GenericId(cid_str.into_bytes())
  }

  fn validate(&self) -> Result<(), Characters>;
}

/// The trait for the extra field of an Anagolay entity
pub trait AnagolayStructureExtra: Clone + PartialEq + Eq {}

/// Generic structure representing an Anagolay entity
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct AnagolayStructure<T: AnagolayStructureData, U: AnagolayStructureExtra> {
  pub id: private::GenericId,
  pub data: T,
  pub extra: Option<U>,
}

impl<T: AnagolayStructureData, U: AnagolayStructureExtra> Default for AnagolayStructure<T, U> {
  fn default() -> Self {
    AnagolayStructure {
      id: private::GenericId::default(),
      data: T::default(),
      extra: None,
    }
  }
}

impl<T: AnagolayStructureData, U: AnagolayStructureExtra> AnagolayStructure<T, U> {
  /// Produces an Anagolay entity with no extra information
  pub fn new(data: T) -> Self {
    AnagolayStructure {
      id: data.to_cid(),
      data,
      extra: None,
    }
  }

  /// Produces an Anagolay entity with some extra information
  ///
  /// # Examples
  ///
  /// ```
  /// use codec::{Decode, Encode};
  /// use anagolay_support::{AnagolayStructure, AnagolayStructureData, AnagolayStructureExtra, Characters};
  ///
  /// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
  /// struct EntityData {
  ///   text: Vec<u8>
  /// };
  ///
  /// impl Default for EntityData {
  ///   fn default() -> Self {
  ///     EntityData {
  ///       text: b"".to_vec()
  ///     }
  ///   }
  /// }
  ///
  /// impl AnagolayStructureData for EntityData {
  ///   fn validate(&self) -> Result<(), Characters> {
  ///      Ok(())
  ///   }
  /// }
  ///
  /// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
  /// struct EntityExtra {
  ///   created_at: u64
  /// };
  ///
  /// impl AnagolayStructureExtra for EntityExtra {}
  ///
  /// type Entity = AnagolayStructure<EntityData, EntityExtra>;
  ///
  /// let entity = Entity::new_with_extra(EntityData {
  ///   text: b"hello".to_vec()
  /// }, EntityExtra {
  ///   created_at: 0
  /// });
  ///
  /// assert_eq!(b"hello".to_vec(), entity.data.text);
  /// assert!(entity.extra.is_some());
  /// assert_eq!(0, entity.extra.unwrap().created_at);
  pub fn new_with_extra(data: T, extra: U) -> Self {
    AnagolayStructure {
      id: data.to_cid(),
      data,
      extra: Some(extra),
    }
  }
}

/// Trait used as type parameter in [`AnagolayPackageStructure`], allowing different structures to
/// define the enumeration of possible artifact types depending on the specific case:
///
/// # Examples
///
/// ```
/// use codec::{Decode, Encode};
/// use anagolay_support::{AnagolayArtifactStructure, ArtifactType};
///
/// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
/// enum OperationArtifactType {
///   Wasm, Docs, Git
/// }
///
/// impl ArtifactType for OperationArtifactType {}
///
/// type OperationPackageStructure = AnagolayArtifactStructure<OperationArtifactType>;
///
/// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
/// enum ImageArtifactType {
///   Raw
/// }
///
/// impl ArtifactType for ImageArtifactType {}
///
/// type ImagePackageStructure = AnagolayArtifactStructure<ImageArtifactType>;
/// ```
pub trait ArtifactType: Encode + Decode + Clone + PartialEq + Eq {}

/// Operation Version artifact
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct AnagolayArtifactStructure<T: ArtifactType> {
  /// Type of the artifact
  pub artifact_type: T,
  /// Extension of the stored file
  pub file_extension: Characters,
  /// IPFS cid
  pub ipfs_cid: ArtifactId,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
/// Extra information (non hashed) for an entity Version
pub struct AnagolayVersionExtra {
  pub created_at: u64,
}

/// Implementation of AnagolayStructureExtra trait for OperationVersionExtra
impl AnagolayStructureExtra for AnagolayVersionExtra {}

/// Version data. It contains all the needed parameters which define the entity Version and is
/// hashed to produce the Version id
///
/// # Examples
///
/// ```
/// use codec::{Decode, Encode};
/// use anagolay_support::{AnagolayStructure, AnagolayVersionData, AnagolayVersionExtra, ArtifactType};
///
/// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
/// enum OperationArtifactType {
///   Wasm, Docs, Git
/// }
/// impl ArtifactType for OperationArtifactType {}
///
/// type OperationVersionData = AnagolayVersionData<OperationArtifactType>;
/// type OperationVersion = AnagolayStructure<OperationVersionData, AnagolayVersionExtra>;
/// ```
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct AnagolayVersionData<T: ArtifactType> {
  /// The id of the Operation, Workflow or other entity to which this Version is
  /// associated. __This field is read-only__
  pub entity_id: Option<private::GenericId>,
  /// The id of the previous Operation Version for the same operation, if any.
  pub parent_id: Option<VersionId>,
  /// Collection of packages that the publisher produced
  pub artifacts: Vec<AnagolayArtifactStructure<T>>,
}

/// Implementation of Default trait for AnagolayVersionData
impl<T: ArtifactType> Default for AnagolayVersionData<T> {
  fn default() -> Self {
    AnagolayVersionData {
      entity_id: None,
      parent_id: None,
      artifacts: vec![],
    }
  }
}

/// Implementation of AnagolayStructureData trait for AnagolayVersionData
impl<T: ArtifactType> AnagolayStructureData for AnagolayVersionData<T> {
  /// Validate the following constraints:
  /// * entity_id: If present, must be a valid CID
  /// * parent_id: If present, must be a valid CID
  /// * artifacts: For each artifact, the file_extension must not be empty and the ipfs_cid must be
  ///   a valid CID
  ///
  /// # Return
  /// An unit result if the validation is successful, a `Character` error with a description in
  /// case it fails
  fn validate(&self) -> Result<(), Characters> {
    if let Some(entity_id) = &self.entity_id {
      entity_id.validate().map_err(|err| {
        Characters::from(type_name_of_val(&self))
          .concat(".entity_id: ")
          .concat(err.as_str())
      })?;
    }
    if let Some(parent_id) = &self.parent_id {
      parent_id.validate().map_err(|err| {
        Characters::from(type_name_of_val(&self))
          .concat(".parent_id: ")
          .concat(err.as_str())
      })?;
    }
    if let Some((index, artifact)) = &self
      .artifacts
      .iter()
      .enumerate()
      .find(|(_, artifact)| artifact.ipfs_cid.validate().is_err() || artifact.file_extension.len() == 0)
    {
      let message = Characters::from(type_name_of_val(&self))
        .concat(".artifacts[")
        .concat_u8(*index as u8)
        .concat("]");
      if artifact.file_extension.len() == 0 {
        return Err(message.concat(".file_extension: cannot be empty".into()));
      } else {
        artifact
          .ipfs_cid
          .validate()
          .map_err(|err| message.concat(".ipfs_cid: ").concat(err.as_str()))?;
      }
    }
    Ok(())
  }
}

/// WASM artifacts commonly produced for a published entity. The subtype should be passed as
/// parameter of the entity-defined artifact type enumeration, like in the example:
///
/// # Examples
///
/// ```
/// use codec::{Decode, Encode};
/// use anagolay_support::{ArtifactType, WasmArtifactSubType};
///
/// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
/// enum OperationArtifactType {
///   Wasm(WasmArtifactSubType), Docs, Git
/// }
/// impl ArtifactType for OperationArtifactType {}
/// #[derive(Encode, Decode, Clone, PartialEq, Eq)]
/// enum WorkflowArtifactType {
///   Wasm(WasmArtifactSubType), Docs, Git
/// }
/// impl ArtifactType for WorkflowArtifactType {}
///
/// let op_esm_artifact_type = OperationArtifactType::Wasm(WasmArtifactSubType::Esm);
/// let wf_esm_artifact_type = WorkflowArtifactType::Wasm(WasmArtifactSubType::Esm);
/// ```
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum WasmArtifactSubType {
  /// CommonJS module for the direct use in the nodejs env which doesn't have the ESM support. When
  /// Nodejs has native ESM support this should be used only for the legacy versions. Check
  /// [here](https://nodejs.org/api/esm.html) the Nodejs ESM status.
  Cjs,
  /// Native ES module, usually used with bundler software like webpack. You can use this just by
  /// including it, the wasm will be instantiated on require time. Example can be found
  /// [here](https://rustwasm.github.io/docs/wasm-bindgen/examples/hello-world.html) and official
  /// docs [here](https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#bundlers).
  /// For the official NODEJS support see [this doc](https://nodejs.org/api/esm.html)
  /// If you want to use this with nodejs, use the bundler.
  Esm,
  /// Just a compiled WASM file without any acompanied JS or `.d.ts` files. You have to do all
  /// things manual.
  Wasm,
  /// This is an ES module with manual instantiation of the wasm. It doesn't include polyfills
  /// More info is on the
  /// [wasm-pack doc website](https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#without-a-bundler)
  /// and [wasm-bindgen](https://rustwasm.github.io/docs/wasm-bindgen/reference/browser-support.html)
  /// # Example in Javascript
  ///
  /// ```javascript
  /// import init, { execute } from './op-file'
  /// async function main() {
  ///   await init() //initialize wasm
  ///   const e = execute([new Uint8Array(7)], new Map())
  ///   console.log(e.decode());
  /// }
  /// main().catch(console.error)
  /// ```
  Web,
}