Skip to content

Commit

Permalink
Add mock API for blob
Browse files Browse the repository at this point in the history
Signed-off-by: lovesh <[email protected]>
  • Loading branch information
lovesh committed May 12, 2020
1 parent 223052f commit 082ccd7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
73 changes: 73 additions & 0 deletions runtime/src/blob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate as dock;

use frame_support::{
decl_error, decl_module, decl_storage, dispatch::DispatchError,
dispatch::DispatchResult, ensure, fail,
};
use system::ensure_signed;
use codec::{Decode, Encode};

use crate::did::{self, Did, DidSignature};

/// Size of the blob id in bytes
pub const ID_BYTE_SIZE: usize = 32;
/// Maximum size of the blob in bytes
// implementer may choose to implement this as a dynamic config option settable with the `parameter_type!` macro
pub const BLOB_MAX_BYTE_SIZE: usize = 1024;

/// The type of the blob id
pub type Id = [u8; ID_BYTE_SIZE];

/// When a new blob is being registered, the following object is sent
/// When a blob is queried, the following object is returned.
#[derive(Encode, Decode, Clone, PartialEq, Debug)]
pub struct Blob {
id: dock::blob::Id,
blob: Vec<u8>,
author: dock::did::Did,
}

pub trait Trait: system::Trait + did::Trait {}

decl_error! {
/// Error for the token module.
pub enum Error for Module<T: Trait> {
/// The blob is greater than `BLOB_MAX_BYTE_SIZE`
BlobTooBig,
/// There is already a blob with same id
BlobAlreadyExists,
/// There is no such DID registered
DidDoesNotExist,
/// Signature verification failed while adding blob
InvalidSig
}
}

/// For each blob id, its author's DID and the blob is stored in the map
decl_storage! {
trait Store for Module<T: Trait> as BlobModule {
Blobs get(fn id): map hasher(blake2_128_concat) dock::blob::Id => Option<(dock::did::Did, Vec<u8>)>;
}
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {

type Error = Error<T>;

/// Register a new blob after ensuring blob with the same id is not registered and then
/// verifying `did`'s signature on the blob
/// `schema_detail` contains the id, author DID and the blob. The size of blob should be at
/// most `BLOB_MAX_BYTE_SIZE` bytes. The `blob` is wrapped in a [StateChange][statechange] before
/// serializing for signature verification.
/// `signature` is the signature of the blob author on the `blob`
// TODO: Use weight proportional to blob size
#[weight = 10_000]
pub fn new(origin, blob: dock::blob::Blob, signature: dock::did::DidSignature) -> DispatchResult {
ensure_signed(origin)?;
// TODO: Write the API
Ok(())
}

}
}
2 changes: 1 addition & 1 deletion runtime/src/did.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{BlockNumber, StateChange};
use crate as dock;
use super::{BlockNumber, StateChange};
use codec::{Decode, Encode};
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, dispatch::DispatchError,
Expand Down
7 changes: 6 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern crate alloc;

pub mod did;
pub mod revoke;
mod blob;
mod template;

use codec::{Decode, Encode};
Expand Down Expand Up @@ -90,6 +91,7 @@ pub enum StateChange {
Revoke(revoke::Revoke),
UnRevoke(revoke::UnRevoke),
RemoveRegistry(revoke::RemoveRegistry),
Blob(blob::Blob)
}

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
Expand Down Expand Up @@ -266,6 +268,8 @@ impl did::Trait for Runtime {

impl revoke::Trait for Runtime {}

impl blob::Trait for Runtime {}

construct_runtime!(
pub enum Runtime where
Block = Block,
Expand All @@ -283,7 +287,8 @@ construct_runtime!(
// Used for the module template in `./template.rs`
TemplateModule: template::{Module, Call, Storage, Event<T>},
DIDModule: did::{Module, Call, Storage, Event},
Revoke: revoke::{Module, Call, Storage}
Revoke: revoke::{Module, Call, Storage},
BlobModule: blob::{Module, Call, Storage}
}
);

Expand Down
6 changes: 4 additions & 2 deletions runtime/src/revoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ decl_module! {
///
/// Returns an error if `proof` does not satisfy the policy requirements of the registy
/// referenced by `revoke.registry_id`.
// TODO: Use correct weight
// TODO: Use weight proportional to number of revoked credentials and in future consider
// no. of DIDs in PAuth
#[weight = 10_000]
pub fn revoke(
origin,
Expand All @@ -163,7 +164,8 @@ decl_module! {
///
/// Returns an error if `proof` does not satisfy the policy requirements of the registy
/// referenced by `unrevoke.registry_id`.
// TODO: Use correct weight
// TODO: Use weight proportional to number of unrevoked credentials and in future consider
// no. of DIDs in PAuth
#[weight = 10_000]
pub fn unrevoke(
origin,
Expand Down

0 comments on commit 082ccd7

Please sign in to comment.