Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge f755ebb into 37a1b5f
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoadeli committed May 21, 2015
2 parents 37a1b5f + f755ebb commit 047b460
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 238 deletions.
8 changes: 4 additions & 4 deletions src/data/immutable_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
// relating to use of the SAFE Network Software.

use cbor;
use cbor::CborTagEncode;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl Sendable for ImmutableData {
fn serialised_contents(&self)->Vec<u8> {
let mut e = cbor::Encoder::from_memory();
e.encode(&[&self]).unwrap();
e.into_bytes()
e.into_bytes()
}

fn refresh(&self)->bool {
Expand Down Expand Up @@ -102,7 +102,7 @@ mod test {
use super::*;
use cbor::{ Encoder, Decoder};
use rustc_serialize::{Decodable, Encodable};
use Random;
use Random;
use rand;
use routing::sendable::Sendable;

Expand Down Expand Up @@ -142,7 +142,7 @@ mod test {
let mut d = Decoder::from_bytes(e.as_bytes());
let obj_after: ImmutableData = d.decode().next().unwrap().unwrap();

assert_eq!(obj_before, obj_after);
assert_eq!(obj_before, obj_after);
}

#[test]
Expand Down
20 changes: 8 additions & 12 deletions src/data/structured_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
// relating to use of the SAFE Network Software.

use cbor;
use cbor::CborTagEncode;
Expand All @@ -31,7 +31,7 @@ pub struct StructuredData {

impl Sendable for StructuredData {
fn name(&self) -> NameType {
self.name.clone()
self.name.clone()
}

fn type_tag(&self)->u64 {
Expand All @@ -41,13 +41,13 @@ impl Sendable for StructuredData {
fn serialised_contents(&self)->Vec<u8> {
let mut e = cbor::Encoder::from_memory();
e.encode(&[&self]).unwrap();
e.into_bytes()
e.into_bytes()
}

fn owner(&self) -> Option<NameType> {
Some(self.owner.clone())
}

fn refresh(&self)->bool {
false
}
Expand All @@ -58,11 +58,7 @@ impl Sendable for StructuredData {
impl StructuredData {
/// An instance of the StructuredData can be created by invoking the new()
pub fn new(name: NameType, owner: NameType, value: Vec<Vec<NameType>>) -> StructuredData {
StructuredData {
name: name,
owner: owner,
value: value,
}
StructuredData {name: name, owner: owner, value: value}
}

/// Returns the value
Expand Down Expand Up @@ -100,7 +96,7 @@ mod test {
use cbor::{ Encoder, Decoder };
use rustc_serialize::{Decodable, Encodable};
use routing;
use routing::NameType;
use routing::NameType;
use routing::sendable::Sendable;
use Random;
use rand;
Expand All @@ -127,8 +123,8 @@ mod test {

#[test]
fn creation() {
let structured_data = StructuredData::generate_random();
let data = StructuredData::new(structured_data.name(), structured_data.owner().unwrap(), structured_data.get_value());
let structured_data = StructuredData::generate_random();
let data = StructuredData::new(structured_data.name(), structured_data.owner().unwrap(), structured_data.get_value());
assert_eq!(data, structured_data);
}

Expand Down
17 changes: 16 additions & 1 deletion src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
// relating to use of the SAFE Network Software.

use sodiumoxide::crypto;
use routing::NameType;

///
/// Returns true if both slices are equal in length, and have equal contents
Expand Down Expand Up @@ -49,6 +52,18 @@ macro_rules! convert_to_array {
}};
}

///
/// Calculate and returns NameType using Public signing & encryption kets
///
pub fn calculate_name(public_keys: &(crypto::sign::PublicKey, crypto::asymmetricbox::PublicKey)) -> NameType {
let combined_iter = (public_keys.0).0.into_iter().chain((public_keys.1).0.into_iter());
let mut combined: Vec<u8> = Vec::new();
for iter in combined_iter {
combined.push(*iter);
}
NameType(crypto::hash::sha512::hash(&combined).0)
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
59 changes: 18 additions & 41 deletions src/id/an_mpid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
// relating to use of the SAFE Network Software.

use cbor::CborTagEncode;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
Expand All @@ -36,37 +36,22 @@ use cbor;
/// let (pub_sign_key, sec_sign_key) = sodiumoxide::crypto::sign::gen_keypair();
/// let (pub_asym_key, sec_asym_key) = sodiumoxide::crypto::asymmetricbox::gen_keypair();
/// // Create AnMpid
/// let an_mpid = maidsafe_types::AnMpid::new((pub_sign_key, pub_asym_key), (sec_sign_key, sec_asym_key), routing::NameType([3u8; 64]));
/// let an_mpid = maidsafe_types::AnMpid::new((pub_sign_key, pub_asym_key), (sec_sign_key, sec_asym_key));
/// // Retrieving the values
/// let ref publicKeys = an_mpid.get_public_keys();
/// let ref secret_keys = an_mpid.get_secret_keys();
/// let ref name = an_mpid.get_name();
/// let name = an_mpid.get_name();
/// ```
///
#[derive(Clone)]
pub struct AnMpid {
public_keys: (crypto::sign::PublicKey, crypto::asymmetricbox::PublicKey),
secret_keys: (crypto::sign::SecretKey, crypto::asymmetricbox::SecretKey),
name: NameType,
secret_keys: (crypto::sign::SecretKey, crypto::asymmetricbox::SecretKey)
}

impl Sendable for AnMpid {
fn name(&self) -> NameType {
let sign_arr = &(&self.public_keys.0).0;
let asym_arr = &(&self.public_keys.1).0;

let mut arr_combined = [0u8; 64 * 2];

for i in 0..sign_arr.len() {
arr_combined[i] = sign_arr[i];
}
for i in 0..asym_arr.len() {
arr_combined[64 + i] = asym_arr[i];
}

let digest = crypto::hash::sha512::hash(&arr_combined);

NameType(digest.0)
self.get_name()
}

fn type_tag(&self)->u64 {
Expand All @@ -76,11 +61,11 @@ impl Sendable for AnMpid {
fn serialised_contents(&self)->Vec<u8> {
let mut e = cbor::Encoder::from_memory();
e.encode(&[&self]).unwrap();
e.into_bytes()
e.into_bytes()
}

fn owner(&self) -> Option<NameType> {
Some(self.name.clone())
Some(self.name().clone())
}

fn refresh(&self)->bool {
Expand All @@ -92,8 +77,8 @@ impl Sendable for AnMpid {

impl fmt::Debug for AnMpid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AnMpid {{ public_keys:({:?}, {:?}), secret_keys:({:?}, {:?}), name: {:?} }}", self.public_keys.0 .0.to_vec(), self.public_keys.1 .0.to_vec(),
self.secret_keys.0 .0.to_vec(), self.secret_keys.1 .0.to_vec(), self.name)
write!(f, "AnMpid {{ public_keys:({:?}, {:?}), secret_keys:({:?}, {:?}) }}", self.public_keys.0 .0.to_vec(), self.public_keys.1 .0.to_vec(),
self.secret_keys.0 .0.to_vec(), self.secret_keys.1 .0.to_vec())
}
}

Expand All @@ -102,20 +87,15 @@ impl PartialEq for AnMpid {
// secret keys are mathematically linked, just check public ones
let pub1_equal = slice_equal(&self.public_keys.0 .0, &other.public_keys.0 .0);
let pub2_equal = slice_equal(&self.public_keys.1 .0, &other.public_keys.1 .0);
return pub1_equal && pub2_equal && self.name == other.name;
return pub1_equal && pub2_equal;
}
}

impl AnMpid {
/// Invoked to create an instance of AnMpid
pub fn new(public_keys: (crypto::sign::PublicKey, crypto::asymmetricbox::PublicKey),
secret_keys: (crypto::sign::SecretKey, crypto::asymmetricbox::SecretKey),
name_type: NameType) -> AnMpid {
AnMpid {
public_keys: public_keys,
secret_keys: secret_keys,
name: name_type
}
secret_keys: (crypto::sign::SecretKey, crypto::asymmetricbox::SecretKey)) -> AnMpid {
AnMpid {public_keys: public_keys, secret_keys: secret_keys }
}
/// Returns the PublicKeys
pub fn get_public_keys(&self) -> &(crypto::sign::PublicKey, crypto::asymmetricbox::PublicKey) {
Expand All @@ -126,8 +106,8 @@ impl AnMpid {
&self.secret_keys
}
/// Returns the name
pub fn get_name(&self) -> &NameType {
&self.name
pub fn get_name(&self) -> NameType {
calculate_name(&self.public_keys)
}
}

Expand All @@ -140,15 +120,14 @@ impl Encodable for AnMpid {
pub_sign_vec.as_ref(),
pub_asym_vec.as_ref(),
sec_sign_vec.as_ref(),
sec_asym_vec.as_ref(),
&self.name)).encode(e)
sec_asym_vec.as_ref())).encode(e)
}
}

impl Decodable for AnMpid {
fn decode<D: Decoder>(d: &mut D)-> Result<AnMpid, D::Error> {
try!(d.read_u64());
let (pub_sign_vec, pub_asym_vec, sec_sign_vec, sec_asym_vec, name) : (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>, NameType) = try!(Decodable::decode(d));
let (pub_sign_vec, pub_asym_vec, sec_sign_vec, sec_asym_vec) : (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>) = try!(Decodable::decode(d));

let pub_sign_arr = convert_to_array!(pub_sign_vec, crypto::sign::PUBLICKEYBYTES);
let pub_asym_arr = convert_to_array!(pub_asym_vec, crypto::asymmetricbox::PUBLICKEYBYTES);
Expand All @@ -163,7 +142,7 @@ impl Decodable for AnMpid {
crypto::asymmetricbox::PublicKey(pub_asym_arr.unwrap()));
let sec_keys = (crypto::sign::SecretKey(sec_sign_arr.unwrap()),
crypto::asymmetricbox::SecretKey(sec_asym_arr.unwrap()));
Ok(AnMpid::new(pub_keys, sec_keys, name))
Ok(AnMpid::new(pub_keys, sec_keys))
}
}

Expand All @@ -173,7 +152,6 @@ mod test {
use cbor::{ Encoder, Decoder };
use Random;
use sodiumoxide::crypto;
use routing;

impl Random for AnMpid {
fn generate_random() -> AnMpid {
Expand All @@ -182,7 +160,6 @@ mod test {
AnMpid {
public_keys: (sign_pub_key, asym_pub_key),
secret_keys: (sign_sec_key, asym_sec_key),
name: routing::test_utils::Random::generate_random()
}
}
}
Expand All @@ -207,5 +184,5 @@ mod test {
assert_eq!(an_mpid_first, an_mpid_second);
assert!(an_mpid_first != an_mpid_third);
}

}
7 changes: 3 additions & 4 deletions src/id/maid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
// relating to use of the SAFE Network Software.

use cbor::CborTagEncode;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
Expand All @@ -27,7 +27,6 @@ use std::fmt;
///
/// #Examples
/// ```
/// use maidsafe_types::Random;
/// use maidsafe_types::{Maid, AnMaid};
/// // Creating new Maid
/// let maid: Maid = Maid::new(&AnMaid::new());
Expand All @@ -45,7 +44,7 @@ pub struct Maid {
impl Maid {
/// Invoked to create an instance of Maid
/// Symmetric public and secret keys of AnMaid and used to construct the Maid
pub fn new(an_maid: &AnMaid) -> Maid {
pub fn new(an_maid: &AnMaid) -> Maid {
let asym_keys = crypto::asymmetricbox::gen_keypair();

Maid {
Expand Down Expand Up @@ -134,7 +133,7 @@ mod test {
use super::super::AnMaid;
use sodiumoxide::crypto;
use Random;
use rand;
use rand;
use rand::Rng;

impl Random for Maid {
Expand Down
Loading

0 comments on commit 047b460

Please sign in to comment.