safe_nd/
lib.rs

1// Copyright 2020 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10//! SAFE network data types.
11
12#![doc(
13    html_logo_url = "https://raw.githubusercontent.com/maidsafe/QA/master/Images/maidsafe_logo.png",
14    html_favicon_url = "https://maidsafe.net/img/favicon.ico",
15    test(attr(forbid(warnings)))
16)]
17// For explanation of lint checks, run `rustc -W help`.
18#![forbid(unsafe_code)]
19#![warn(
20    // TODO: add missing debug implementations for structs?
21    // missing_debug_implementations,
22    missing_docs,
23    trivial_casts,
24    trivial_numeric_casts,
25    unused_extern_crates,
26    unused_import_braces,
27    unused_qualifications,
28    unused_results
29)]
30
31mod blob;
32mod errors;
33mod identity;
34mod keys;
35mod map;
36mod messaging;
37mod money;
38mod rewards;
39mod sequence;
40mod transfer;
41mod utils;
42
43pub use blob::{
44    Address as BlobAddress, Data as Blob, Kind as BlobKind, PrivateData as PrivateBlob,
45    PublicData as PublicBlob, MAX_BLOB_SIZE_IN_BYTES,
46};
47pub use errors::{EntryError, Error, Result};
48pub use identity::{
49    client::{FullId as ClientFullId, PublicId as ClientPublicId},
50    node::{FullId as NodeFullId, NodeKeypairs, PublicId as NodePublicId},
51    PublicId,
52};
53pub use keys::{
54    BlsKeypair, BlsKeypairShare, BlsProof, BlsProofShare, Ed25519Proof, Keypair, Proof, Proven,
55    PublicKey, Signature, SignatureShare,
56};
57pub use map::{
58    Action as MapAction, Address as MapAddress, Data as Map, Entries as MapEntries,
59    EntryActions as MapEntryActions, Kind as MapKind, PermissionSet as MapPermissionSet,
60    SeqData as SeqMap, SeqEntries as MapSeqEntries, SeqEntryAction as MapSeqEntryAction,
61    SeqEntryActions as MapSeqEntryActions, SeqValue as MapSeqValue, UnseqData as UnseqMap,
62    UnseqEntries as MapUnseqEntries, UnseqEntryAction as MapUnseqEntryAction,
63    UnseqEntryActions as MapUnseqEntryActions, Value as MapValue, Values as MapValues,
64};
65pub use messaging::{
66    Account, AccountRead, AccountWrite, Address, AdultDuties, AuthCmd, AuthQuery,
67    AuthorisationKind, BlobRead, BlobWrite, Cmd, CmdError, DataAuthKind, DataCmd, DataQuery, Duty,
68    ElderDuties, Event, MapRead, MapWrite, Message, MessageId, MiscAuthKind, MoneyAuthKind,
69    MsgEnvelope, MsgSender, NodeCmd, NodeCmdError, NodeDataCmd, NodeDataError, NodeDataQuery,
70    NodeDataQueryResponse, NodeDuties, NodeEvent, NodeQuery, NodeQueryResponse, NodeRewardError,
71    NodeRewardQuery, NodeRewardQueryResponse, NodeSystemCmd, NodeTransferCmd, NodeTransferError,
72    NodeTransferQuery, NodeTransferQueryResponse, Query, QueryResponse, SequenceRead,
73    SequenceWrite, TransferCmd, TransferError, TransferQuery, TryFromError, MAX_LOGIN_PACKET_BYTES,
74};
75pub use money::Money;
76pub use rewards::{RewardCounter, Work};
77
78pub use sequence::{
79    Action as SequenceAction, Address as SequenceAddress, Data as Sequence,
80    DataWriteOp as SequenceDataWriteOp, Entries as SequenceEntries, Entry as SequenceEntry,
81    Index as SequenceIndex, Kind as SequenceKind, Permissions as SequencePermissions,
82    Policy as SequencePolicy, PrivatePermissions as SequencePrivatePermissions,
83    PrivatePolicy as SequencePrivatePolicy, PrivateSeqData,
84    PublicPermissions as SequencePublicPermissions, PublicPolicy as SequencePublicPolicy,
85    PublicSeqData, User as SequenceUser, WriteOp as SequenceWriteOp,
86};
87use serde::{Deserialize, Serialize};
88pub use sha3::Sha3_512 as Ed25519Digest;
89pub use transfer::*;
90pub use utils::verify_signature;
91
92use std::{fmt::Debug, net::SocketAddr};
93use xor_name::XorName;
94
95/// Object storing a data variant.
96#[allow(clippy::large_enum_variant)]
97#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Serialize, Deserialize, Debug)]
98pub enum Data {
99    /// Blob.
100    Immutable(Blob),
101    /// MutableData.
102    Mutable(Map),
103    /// Sequence.
104    Sequence(Sequence),
105}
106
107impl Data {
108    /// Returns true if published.
109    pub fn is_pub(&self) -> bool {
110        match *self {
111            Self::Immutable(ref idata) => idata.is_pub(),
112            Self::Mutable(_) => false,
113            Self::Sequence(ref sequence) => sequence.is_pub(),
114        }
115    }
116
117    /// Returns true if unpublished.
118    pub fn is_unpub(&self) -> bool {
119        !self.is_pub()
120    }
121}
122
123impl From<Blob> for Data {
124    fn from(data: Blob) -> Self {
125        Self::Immutable(data)
126    }
127}
128
129impl From<Map> for Data {
130    fn from(data: Map) -> Self {
131        Self::Mutable(data)
132    }
133}
134
135impl From<Sequence> for Data {
136    fn from(data: Sequence) -> Self {
137        Self::Sequence(data)
138    }
139}
140
141/// Permissions for an app stored by the Client Handlers.
142#[derive(
143    Copy, Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Default, Debug,
144)]
145pub struct AppPermissions {
146    /// Whether this app has permissions to perform data mutations.
147    pub data_mutations: bool,
148    /// Whether this app has permissions to transfer money.
149    pub transfer_money: bool,
150    /// Whether this app has permissions to read the account balance.
151    pub read_balance: bool,
152    /// Whether this app has permissions to read the account transfer history.
153    pub read_transfer_history: bool,
154}
155
156/// Handshake requests sent from clients to vaults to establish new connections and verify a client's
157/// key (to prevent replay attacks).
158#[derive(Serialize, Deserialize)]
159pub enum HandshakeRequest {
160    /// Sent by clients as an initial bootstrap request, and then for subsequent bootstrap attempts.
161    Bootstrap(PublicKey),
162    /// Sent to destination nodes as a response to `HandshakeResponse::Join`.
163    Join(PublicKey),
164    /// Response to `HandshakeResponse::Challenge` sent by a vault.
165    ChallengeResult(Signature),
166}
167
168/// Handshake responses sent from vaults to clients.
169#[allow(clippy::large_enum_variant)]
170#[derive(Serialize, Deserialize)]
171pub enum HandshakeResponse {
172    /// Sent by nodes when a client should attempt to connect to the section that's closest to
173    /// its destination (section managing the client's account).
174    Rebootstrap(Vec<(XorName, SocketAddr)>),
175    /// Sent by nodes when a client reaches its destination section.
176    Join(Vec<(XorName, SocketAddr)>),
177    /// Sent by nodes as a response to a valid `HandshakeRequest::Join`.
178    Challenge(PublicKey, Vec<u8>),
179    /// Sent by nodes as a response to an invalid `HandshakeRequest::Join` (when a client attempts to join a wrong section).
180    InvalidSection,
181}