Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
Merge 10731f8 into e3f2e33
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Apr 21, 2021
2 parents e3f2e33 + 10731f8 commit a394d22
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ version = "16.1.0"
bincode = "1.2.1"
bytes = "1.0.1"
cookie-factory = "0.3.1"
crdts = "4.3.0"
crdts = "6.3.2"
hex_fmt = "~0.3.0"
multibase = "~0.8.0"
rand = "~0.7.3"
rand_core = "~0.5.1"
rmp-serde = "~0.15.1"
serde_bytes = "0.11.5"
signature = "1.1.0"
sn_data_types = "~0.18.0"
sn_data_types = "~0.18.2"
thiserror = "1.0.23"
threshold_crypto = "~0.4.0"
xor_name = "1.1.10"

[dependencies.ed25519-dalek]
version = "1.0.0-pre.4"
version = "1.0.0"
features = [ "serde" ]

[dependencies.serde]
Expand Down
14 changes: 14 additions & 0 deletions src/client/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use super::{
blob::{BlobRead, BlobWrite},
map::{MapRead, MapWrite},
register::{RegisterRead, RegisterWrite},
sequence::{SequenceRead, SequenceWrite},
AuthorisationKind, CmdError, Error, QueryResponse,
};
Expand All @@ -29,6 +30,8 @@ pub enum DataCmd {
Map(MapWrite),
/// Sequence write operation
Sequence(SequenceWrite),
/// Register write operation
Register(RegisterWrite),
}

impl DataCmd {
Expand All @@ -40,6 +43,7 @@ impl DataCmd {
Blob(c) => c.error(error),
Map(c) => c.error(error),
Sequence(c) => c.error(error),
Register(c) => c.error(error),
}
}
/// Returns the type of authorisation needed for the cuest.
Expand All @@ -49,6 +53,7 @@ impl DataCmd {
Blob(c) => c.authorisation_kind(),
Map(c) => c.authorisation_kind(),
Sequence(c) => c.authorisation_kind(),
Register(c) => c.authorisation_kind(),
}
}

Expand All @@ -59,6 +64,7 @@ impl DataCmd {
Blob(c) => c.dst_address(),
Map(c) => c.dst_address(),
Sequence(c) => c.dst_address(),
Register(c) => c.dst_address(),
}
}

Expand All @@ -68,6 +74,7 @@ impl DataCmd {
Self::Blob(write) => write.owner(),
Self::Map(write) => write.owner(),
Self::Sequence(write) => write.owner(),
Self::Register(write) => write.owner(),
}
}
}
Expand All @@ -79,6 +86,7 @@ impl fmt::Debug for DataCmd {
Blob(c) => write!(formatter, "{:?}", c),
Map(c) => write!(formatter, "{:?}", c),
Sequence(c) => write!(formatter, "{:?}", c),
Register(c) => write!(formatter, "{:?}", c),
}
}
}
Expand All @@ -93,6 +101,8 @@ pub enum DataQuery {
Map(MapRead),
/// TODO: docs
Sequence(SequenceRead),
/// TODO: docs
Register(RegisterRead),
}

impl DataQuery {
Expand All @@ -104,6 +114,7 @@ impl DataQuery {
Blob(q) => q.error(error),
Map(q) => q.error(error),
Sequence(q) => q.error(error),
Register(q) => q.error(error),
}
}

Expand All @@ -114,6 +125,7 @@ impl DataQuery {
Blob(q) => q.authorisation_kind(),
Map(q) => q.authorisation_kind(),
Sequence(q) => q.authorisation_kind(),
Register(q) => q.authorisation_kind(),
}
}

Expand All @@ -124,6 +136,7 @@ impl DataQuery {
Blob(q) => q.dst_address(),
Map(q) => q.dst_address(),
Sequence(q) => q.dst_address(),
Register(q) => q.dst_address(),
}
}
}
Expand All @@ -135,6 +148,7 @@ impl fmt::Debug for DataQuery {
Blob(q) => write!(formatter, "{:?}", q),
Map(q) => write!(formatter, "{:?}", q),
Sequence(q) => write!(formatter, "{:?}", q),
Register(q) => write!(formatter, "{:?}", q),
}
}
}
41 changes: 36 additions & 5 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod errors;
mod map;
mod network;
mod query;
mod register;
mod sender;
mod sequence;
mod transfer;
Expand All @@ -33,6 +34,7 @@ pub use self::{
NodeTransferQueryResponse,
},
query::Query,
register::{RegisterRead, RegisterWrite},
sender::{Address, MsgSender, TransientElderKey, TransientSectionKey},
sequence::{SequenceRead, SequenceWrite},
transfer::{TransferCmd, TransferQuery},
Expand All @@ -42,6 +44,7 @@ use crate::{MessageId, MessageType, WireMsg};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use sn_data_types::{
register::{Entry, EntryHash, Permissions, Policy, Register},
ActorHistory, Blob, Map, MapEntries, MapPermissionSet, MapValue, MapValues, PublicKey,
Sequence, SequenceEntries, SequenceEntry, SequencePermissions, SequencePrivatePolicy,
SequencePublicPolicy, Token, TransferAgreementProof, TransferValidated,
Expand Down Expand Up @@ -332,8 +335,6 @@ pub enum QueryResponse {
//
/// Get Sequence.
GetSequence(Result<Sequence>),
/// Get Sequence owners.
GetSequenceOwner(Result<PublicKey>),
/// Get Sequence entries from a range.
GetSequenceRange(Result<SequenceEntries>),
/// Get Sequence last entry.
Expand All @@ -345,6 +346,19 @@ pub enum QueryResponse {
/// Get Sequence permissions for a user.
GetSequenceUserPermissions(Result<SequencePermissions>),
//
// ===== Register Data =====
//
/// Get Register.
GetRegister(Result<Register>),
/// Get Register owners.
GetRegisterOwner(Result<PublicKey>),
/// Read Register.
ReadRegister(Result<BTreeSet<(EntryHash, Entry)>>),
/// Get public Register permissions for a user.
GetRegisterPolicy(Result<Policy>),
/// Get Register permissions for a user.
GetRegisterUserPermissions(Result<Permissions>),
//
// ===== Tokens =====
//
/// Get key balance.
Expand Down Expand Up @@ -435,12 +449,16 @@ try_from!(MapPermissionSet, ListMapUserPermissions);
try_from!(BTreeMap<PublicKey, MapPermissionSet>, ListMapPermissions);
try_from!(MapValue, GetMapValue);
try_from!(Sequence, GetSequence);
try_from!(PublicKey, GetSequenceOwner);
try_from!(SequenceEntries, GetSequenceRange);
try_from!((u64, SequenceEntry), GetSequenceLastEntry);
try_from!(SequencePublicPolicy, GetSequencePublicPolicy);
try_from!(SequencePrivatePolicy, GetSequencePrivatePolicy);
try_from!(SequencePermissions, GetSequenceUserPermissions);
try_from!(Register, GetRegister);
try_from!(PublicKey, GetRegisterOwner);
try_from!(BTreeSet<(EntryHash, Entry)>, ReadRegister);
try_from!(Policy, GetRegisterPolicy);
try_from!(Permissions, GetRegisterUserPermissions);
try_from!(Token, GetBalance);
try_from!(ActorHistory, GetHistory);

Expand Down Expand Up @@ -496,8 +514,21 @@ impl fmt::Debug for QueryResponse {
"QueryResponse::GetSequencePrivatePolicy({:?})",
ErrorDebug(res)
),
GetSequenceOwner(res) => {
write!(f, "QueryResponse::GetSequenceOwner({:?})", ErrorDebug(res))
// Register
GetRegister(res) => write!(f, "QueryResponse::GetRegister({:?})", ErrorDebug(res)),
ReadRegister(res) => {
write!(f, "QueryResponse::ReadRegister({:?})", ErrorDebug(res))
}
GetRegisterUserPermissions(res) => write!(
f,
"QueryResponse::GetRegisterUserPermissions({:?})",
ErrorDebug(res)
),
GetRegisterPolicy(res) => {
write!(f, "QueryResponse::GetRegisterPolicy({:?})", ErrorDebug(res))
}
GetRegisterOwner(res) => {
write!(f, "QueryResponse::GetRegisterOwner({:?})", ErrorDebug(res))
}
GetBalance(res) => write!(f, "QueryResponse::GetBalance({:?})", ErrorDebug(res)),
GetHistory(res) => write!(f, "QueryResponse::GetHistory({:?})", ErrorDebug(res)),
Expand Down
156 changes: 156 additions & 0 deletions src/client/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2021 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.

use super::{AuthorisationKind, CmdError, DataAuthKind, Error, QueryResponse};
use serde::{Deserialize, Serialize};
use sn_data_types::{
register::{Address, Entry, Register, RegisterOp, User},
PublicKey,
};
use std::fmt;
use xor_name::XorName;

/// Register reading queries
#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
pub enum RegisterRead {
/// Get Register from the network.
Get(Address),
/// Read last entry, or entries if there are branches, from the Register.
Read(Address),
/// List current policy
GetPolicy(Address),
/// Get current permissions for a specified user(s).
GetUserPermissions {
/// Register address.
address: Address,
/// User to get permissions for.
user: User,
},
/// Get current owner.
GetOwner(Address),
}

/// Register writing commands
#[allow(clippy::large_enum_variant)]
#[derive(Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum RegisterWrite {
/// Create a new Register on the network.
New(Register),
/// Edit the Register (insert/remove entry).
Edit(RegisterOp<Entry>),
/// Delete a private Register.
///
/// This operation MUST return an error if applied to public Register. Only the current
/// owner(s) can perform this action.
Delete(Address),
}

impl RegisterRead {
/// Creates a Response containing an error, with the Response variant corresponding to the
/// Request variant.
pub fn error(&self, error: Error) -> QueryResponse {
match *self {
RegisterRead::Get(_) => QueryResponse::GetRegister(Err(error)),
RegisterRead::Read(_) => QueryResponse::ReadRegister(Err(error)),
RegisterRead::GetPolicy(_) => QueryResponse::GetRegisterPolicy(Err(error)),
RegisterRead::GetUserPermissions { .. } => {
QueryResponse::GetRegisterUserPermissions(Err(error))
}
RegisterRead::GetOwner(_) => QueryResponse::GetRegisterOwner(Err(error)),
}
}

/// Returns the access categorisation of the request.
pub fn authorisation_kind(&self) -> AuthorisationKind {
match *self {
RegisterRead::Get(address)
| RegisterRead::Read(address)
| RegisterRead::GetPolicy(address)
| RegisterRead::GetUserPermissions { address, .. }
| RegisterRead::GetOwner(address) => {
if address.is_public() {
AuthorisationKind::Data(DataAuthKind::PublicRead)
} else {
AuthorisationKind::Data(DataAuthKind::PrivateRead)
}
}
}
}

/// Returns the address of the destination for request.
pub fn dst_address(&self) -> XorName {
match self {
RegisterRead::Get(ref address)
| RegisterRead::Read(ref address)
| RegisterRead::GetPolicy(ref address)
| RegisterRead::GetUserPermissions { ref address, .. }
| RegisterRead::GetOwner(ref address) => *address.name(),
}
}
}

impl fmt::Debug for RegisterRead {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"RegisterRead::{}",
match *self {
RegisterRead::Get(_) => "GetRegister",
RegisterRead::Read(_) => "ReadRegister",
RegisterRead::GetPolicy { .. } => "GetRegisterPolicy",
RegisterRead::GetUserPermissions { .. } => "GetUserPermissions",
RegisterRead::GetOwner { .. } => "GetOwner",
}
)
}
}

impl RegisterWrite {
/// Creates a Response containing an error, with the Response variant corresponding to the
/// Request variant.
pub fn error(&self, error: Error) -> CmdError {
CmdError::Data(error)
}

/// Returns the access categorisation of the request.
pub fn authorisation_kind(&self) -> AuthorisationKind {
AuthorisationKind::Data(DataAuthKind::Write)
}

/// Returns the address of the destination for request.
pub fn dst_address(&self) -> XorName {
match self {
RegisterWrite::New(ref data) => *data.name(),
RegisterWrite::Delete(ref address) => *address.name(),
RegisterWrite::Edit(ref op) => *op.address.name(),
}
}

/// Owner of the RegisterWrite
pub fn owner(&self) -> Option<PublicKey> {
match self {
Self::New(data) => Some(data.owner()),
_ => None,
}
}
}

impl fmt::Debug for RegisterWrite {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"RegisterWrite::{}",
match *self {
RegisterWrite::New(_) => "NewRegister",
RegisterWrite::Delete(_) => "DeleteRegister",
RegisterWrite::Edit(_) => "EditRegister",
}
)
}
}

0 comments on commit a394d22

Please sign in to comment.