Diff
Not logged in

Differences From Artifact [25f2937d3f]:

To Artifact [a9a3d52548]:


     1      1   // Copyright 2015 MaidSafe.net limited.
     2      2   //
     3      3   // This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
     4      4   // version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
     5      5   // licence you accepted on initial access to the Software (the "Licences").
     6      6   //
     7      7   // By contributing code to the SAFE Network Software, or to this project generally, you agree to be
     8         -// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.  This, along with the
     9         -// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
            8  +// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
            9  +// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
    10     10   //
    11     11   // Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
    12     12   // under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    13     13   // KIND, either express or implied.
    14     14   //
    15     15   // Please review the Licences for the specific language governing permissions and limitations
    16     16   // relating to use of the SAFE Network Software.
    17     17   
    18     18   use core::id::{IdType, RevocationIdType};
    19     19   use routing::XorName;
    20     20   use rust_sodium::crypto::{box_, sign};
    21     21   use rust_sodium::crypto::hash::sha256;
    22     22   
    23         -/// PublicIdType
           23  +/// `PublicIdType`
    24     24   ///
    25     25   /// #Examples
    26     26   ///
    27     27   /// ```
    28     28   /// use ::safe_core::core::id::{IdType, RevocationIdType, MaidTypeTags, PublicIdType};
    29     29   ///
    30         -/// let revocation_maid = RevocationIdType::new::<MaidTypeTags>();
    31         -/// let maid = IdType::new(&revocation_maid);
           30  +/// let revocation_maid = RevocationIdType::new::<MaidTypeTags>(None);
           31  +/// let maid = IdType::new(&revocation_maid, None);
    32     32   /// let _public_maid  = PublicIdType::new(&maid, &revocation_maid);
    33     33   /// ```
    34         -#[derive(Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable)]
           34  +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
    35     35   pub struct PublicIdType {
    36     36       type_tag: u64,
    37     37       public_keys: (sign::PublicKey, box_::PublicKey),
    38     38       revocation_public_key: sign::PublicKey,
    39     39       signature: sign::Signature,
    40     40   }
    41     41   
................................................................................
    45     45           let type_tag = revocation_id.type_tags().2;
    46     46           let public_keys = id_type.public_keys();
    47     47           let revocation_public_key = revocation_id.public_key();
    48     48           let combined_iter = (public_keys.0)
    49     49               .0
    50     50               .into_iter()
    51     51               .chain((public_keys.1)
    52         -                .0
    53         -                .into_iter()
    54         -                .chain(revocation_public_key.0.into_iter()));
           52  +                       .0
           53  +                       .into_iter()
           54  +                       .chain(revocation_public_key.0.into_iter()));
    55     55           let mut combined: Vec<u8> = Vec::new();
    56     56           for iter in combined_iter {
    57     57               combined.push(*iter);
    58     58           }
    59         -        for i in type_tag.to_string().into_bytes().into_iter() {
           59  +        for i in type_tag.to_string().into_bytes() {
    60     60               combined.push(i);
    61     61           }
    62     62           let message_length = combined.len();
    63     63   
    64         -        let signature = revocation_id.sign(&combined)
           64  +        let signature = revocation_id
           65  +            .sign(&combined)
    65     66               .into_iter()
    66     67               .skip(message_length)
    67     68               .collect::<Vec<_>>();
    68     69           let mut signature_arr = [0; sign::SIGNATUREBYTES];
    69     70   
    70         -        for it in signature.into_iter().take(sign::SIGNATUREBYTES).enumerate() {
           71  +        for it in signature
           72  +                .into_iter()
           73  +                .take(sign::SIGNATUREBYTES)
           74  +                .enumerate() {
    71     75               signature_arr[it.0] = it.1;
    72     76           }
    73     77   
    74     78           PublicIdType {
    75     79               type_tag: type_tag,
    76     80               public_keys: *public_keys,
    77     81               revocation_public_key: *revocation_id.public_key(),
................................................................................
    85     89               .0
    86     90               .into_iter()
    87     91               .chain((self.public_keys.1).0.into_iter());
    88     92           let mut combined: Vec<u8> = Vec::new();
    89     93           for iter in combined_iter {
    90     94               combined.push(*iter);
    91     95           }
    92         -        for i in self.type_tag.to_string().into_bytes().into_iter() {
           96  +        for i in self.type_tag.to_string().into_bytes() {
    93     97               combined.push(i);
    94     98           }
    95     99           for i in 0..sign::SIGNATUREBYTES {
    96    100               combined.push(self.signature.0[i]);
    97    101           }
    98    102           XorName(sha256::hash(&combined).0)
    99    103       }
................................................................................
   112    116       pub fn signature(&self) -> &sign::Signature {
   113    117           &self.signature
   114    118       }
   115    119   }
   116    120   
   117    121   #[cfg(test)]
   118    122   mod test {
          123  +    use super::*;
   119    124       use core::id::{IdType, MaidTypeTags, MpidTypeTags, Random, RevocationIdType};
   120    125       use maidsafe_utilities::serialisation::{deserialise, serialise};
   121    126       use rust_sodium::crypto::sign;
   122         -    use super::*;
   123    127   
   124    128       impl Random for PublicIdType {
   125    129           fn generate_random() -> PublicIdType {
   126         -            let revocation_maid = RevocationIdType::new::<MaidTypeTags>();
   127         -            let maid = IdType::new(&revocation_maid);
          130  +            let revocation_maid = RevocationIdType::new::<MaidTypeTags>(None);
          131  +            let maid = IdType::new(&revocation_maid, None);
   128    132               PublicIdType::new(&maid, &revocation_maid)
   129    133           }
   130    134       }
   131    135   
   132    136       #[test]
   133    137       fn create_public_mpid() {
   134         -        let revocation_mpid = RevocationIdType::new::<MpidTypeTags>();
   135         -        let mpid = IdType::new(&revocation_mpid);
          138  +        let revocation_mpid = RevocationIdType::new::<MpidTypeTags>(None);
          139  +        let mpid = IdType::new(&revocation_mpid, None);
   136    140           let _ = PublicIdType::new(&mpid, &revocation_mpid);
   137    141       }
   138    142   
   139    143       #[test]
   140    144       fn serialisation_public_maid() {
   141    145           let obj_before: PublicIdType = Random::generate_random();
   142    146   
................................................................................
   153    157           let public_maid_third = PublicIdType::generate_random();
   154    158           assert_eq!(public_maid_first, public_maid_second);
   155    159           assert!(public_maid_first != public_maid_third);
   156    160       }
   157    161   
   158    162       #[test]
   159    163       fn invariant_check() {
   160         -        let revocation_maid = RevocationIdType::new::<MaidTypeTags>();
   161         -        let maid = IdType::new(&revocation_maid);
          164  +        let revocation_maid = RevocationIdType::new::<MaidTypeTags>(None);
          165  +        let maid = IdType::new(&revocation_maid, None);
   162    166           let public_maid = PublicIdType::new(&maid, &revocation_maid);
   163    167           let type_tag = public_maid.type_tag;
   164    168           let public_id_keys = public_maid.public_keys;
   165    169           let public_revocation_key = public_maid.revocation_public_key;
   166    170           let combined_keys = (public_id_keys.0)
   167    171               .0
   168    172               .into_iter()
   169    173               .chain((public_id_keys.1)
   170         -                .0
   171         -                .into_iter()
   172         -                .chain(public_revocation_key.0
   173         -                    .into_iter()));
          174  +                       .0
          175  +                       .into_iter()
          176  +                       .chain(public_revocation_key.0.into_iter()));
   174    177           let mut combined = Vec::new();
   175    178   
   176    179           for iter in combined_keys {
   177    180               combined.push(*iter);
   178    181           }
   179         -        for i in type_tag.to_string().into_bytes().into_iter() {
          182  +        for i in type_tag.to_string().into_bytes() {
   180    183               combined.push(i);
   181    184           }
   182    185   
   183    186           let message_length = combined.len();
   184         -        let signature_vec = revocation_maid.sign(&combined)
          187  +        let signature_vec = revocation_maid
          188  +            .sign(&combined)
   185    189               .into_iter()
   186    190               .skip(message_length)
   187    191               .collect::<Vec<_>>();
   188    192   
   189    193           assert_eq!(signature_vec.len(), sign::SIGNATUREBYTES);
   190    194   
   191    195           let mut signature_arr = [0; sign::SIGNATUREBYTES];
   192    196   
   193         -        for it in signature_vec.into_iter().take(sign::SIGNATUREBYTES).enumerate() {
          197  +        for it in signature_vec
          198  +                .into_iter()
          199  +                .take(sign::SIGNATUREBYTES)
          200  +                .enumerate() {
   194    201               signature_arr[it.0] = it.1;
   195    202           }
   196    203   
   197    204           let signature = sign::Signature(signature_arr);
   198    205           assert!(&signature.0[..] == &public_maid.signature().0[..]);
   199    206       }
   200    207   }