Diff
Not logged in

Differences From Artifact [145c951da5]:

To Artifact [607e501e11]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

221
222
223
224
225
226
227
228
229
230
231
232
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.1.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// 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.


use error::RoutingError;
use maidsafe_utilities::serialisation::{serialise, serialised_size};
use rust_sodium::crypto::sign::{self, PublicKey, SecretKey, Signature};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Debug, Formatter};
use super::{DataIdentifier, NO_OWNER_PUB_KEY};
use utils;
use xor_name::XorName;

/// Maximum allowed size for a Structured Data to grow to
pub const MAX_STRUCTURED_DATA_SIZE_IN_BYTES: u64 = 102400;

/// Mutable structured data.
///
/// These types may be stored unsigned with previous and current owner keys
/// set to the same keys. Updates require a signature to validate.
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, RustcDecodable, RustcEncodable)]
pub struct StructuredData {
    type_tag: u64,
    name: XorName,
    data: Vec<u8>,
    version: u64,
    owners: BTreeSet<PublicKey>,
    signatures: BTreeMap<PublicKey, Signature>,
................................................................................
               owners: BTreeSet<PublicKey>)
               -> Result<StructuredData, RoutingError> {
        if owners.len() > 1 {
            return Err(RoutingError::InvalidOwners);
        }

        Ok(StructuredData {
            type_tag: type_tag,
            name: name,
            data: data,
            version: version,
            owners: owners,
            signatures: BTreeMap::new(),
        })
    }

    /// Replaces this data item with the given updated version if the update is valid, otherwise
    /// returns an error.
    ///
    /// To transfer ownership, the current owner signs over the data and increase `version` by one.
    pub fn replace_with_other(&mut self, other: StructuredData) -> Result<(), RoutingError> {
................................................................................
               self.version,
               utils::format_binary_array(&self.data[..]),
               self.owners,
               self.signatures)
    }
}

#[derive(RustcEncodable)]
struct SerialisableStructuredData<'a> {
    type_tag: Vec<u8>,
    name: XorName,
    data: &'a [u8],
    version: Vec<u8>,
    owners: &'a BTreeSet<PublicKey>,
}

#[cfg(test)]
mod tests {

    use data;
    use rand;
    use rust_sodium::crypto::sign;
    use std::collections::BTreeSet;
    use super::*;
    use xor_name::XorName;

    #[test]
    fn single_owner() {
        let keys = sign::gen_keypair();
        let mut owner_keys = BTreeSet::new();
        owner_keys.insert(keys.0);
................................................................................
                let data = match structured_data.data_to_sign() {
                    Ok(data) => data,
                    Err(error) => panic!("Error: {:?}", error),
                };
                assert!(data::verify_signatures(&owner_keys,
                                                &data,
                                                structured_data.get_signatures())
                    .is_err());
                assert!(structured_data.add_signature(&keys).is_ok());
                assert!(data::verify_signatures(&owner_keys,
                                                &data,
                                                structured_data.get_signatures())
                    .is_ok());
            }
            Err(error) => panic!("Error: {:?}", error),
        }
    }

    #[test]
    fn single_owner_other_signature() {
................................................................................
                let data = match structured_data.data_to_sign() {
                    Ok(data) => data,
                    Err(error) => panic!("Error: {:?}", error),
                };
                assert!(data::verify_signatures(&owner_keys,
                                                &data,
                                                structured_data.get_signatures())
                    .is_err());
            }
            Err(error) => panic!("Error: {:?}", error),
        }
    }

    #[test]
    fn transfer_ownership() {







|
|








>





<










|







 







|
|
|
|
|
|
|







 







|










>




<







 







|




|







 







|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

226
227
228
229
230
231
232
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// 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.

use super::{DataIdentifier, NO_OWNER_PUB_KEY};
use error::RoutingError;
use maidsafe_utilities::serialisation::{serialise, serialised_size};
use rust_sodium::crypto::sign::{self, PublicKey, SecretKey, Signature};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Debug, Formatter};

use utils;
use xor_name::XorName;

/// Maximum allowed size for a Structured Data to grow to
pub const MAX_STRUCTURED_DATA_SIZE_IN_BYTES: u64 = 102400;

/// Mutable structured data.
///
/// These types may be stored unsigned with previous and current owner keys
/// set to the same keys. Updates require a signature to validate.
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Deserialize, Serialize)]
pub struct StructuredData {
    type_tag: u64,
    name: XorName,
    data: Vec<u8>,
    version: u64,
    owners: BTreeSet<PublicKey>,
    signatures: BTreeMap<PublicKey, Signature>,
................................................................................
               owners: BTreeSet<PublicKey>)
               -> Result<StructuredData, RoutingError> {
        if owners.len() > 1 {
            return Err(RoutingError::InvalidOwners);
        }

        Ok(StructuredData {
               type_tag: type_tag,
               name: name,
               data: data,
               version: version,
               owners: owners,
               signatures: BTreeMap::new(),
           })
    }

    /// Replaces this data item with the given updated version if the update is valid, otherwise
    /// returns an error.
    ///
    /// To transfer ownership, the current owner signs over the data and increase `version` by one.
    pub fn replace_with_other(&mut self, other: StructuredData) -> Result<(), RoutingError> {
................................................................................
               self.version,
               utils::format_binary_array(&self.data[..]),
               self.owners,
               self.signatures)
    }
}

#[derive(Serialize)]
struct SerialisableStructuredData<'a> {
    type_tag: Vec<u8>,
    name: XorName,
    data: &'a [u8],
    version: Vec<u8>,
    owners: &'a BTreeSet<PublicKey>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use data;
    use rand;
    use rust_sodium::crypto::sign;
    use std::collections::BTreeSet;

    use xor_name::XorName;

    #[test]
    fn single_owner() {
        let keys = sign::gen_keypair();
        let mut owner_keys = BTreeSet::new();
        owner_keys.insert(keys.0);
................................................................................
                let data = match structured_data.data_to_sign() {
                    Ok(data) => data,
                    Err(error) => panic!("Error: {:?}", error),
                };
                assert!(data::verify_signatures(&owner_keys,
                                                &data,
                                                structured_data.get_signatures())
                                .is_err());
                assert!(structured_data.add_signature(&keys).is_ok());
                assert!(data::verify_signatures(&owner_keys,
                                                &data,
                                                structured_data.get_signatures())
                                .is_ok());
            }
            Err(error) => panic!("Error: {:?}", error),
        }
    }

    #[test]
    fn single_owner_other_signature() {
................................................................................
                let data = match structured_data.data_to_sign() {
                    Ok(data) => data,
                    Err(error) => panic!("Error: {:?}", error),
                };
                assert!(data::verify_signatures(&owner_keys,
                                                &data,
                                                structured_data.get_signatures())
                                .is_err());
            }
            Err(error) => panic!("Error: {:?}", error),
        }
    }

    #[test]
    fn transfer_ownership() {