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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Copyright 2020 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// http://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::{
    constants::{CONTENT_ADDED_SIGN, CONTENT_DELETED_SIGN},
    helpers::{gen_timestamp_nanos, get_subnames_host_path_and_version},
    nrs_map::NrsMap,
    xorurl::{SafeContentType, SafeDataType},
    Error, Result, Safe, SafeApp, XorUrl, XorUrlEncoder,
};
use log::{debug, info, warn};
use safe_nd::XorName;
use std::collections::BTreeMap;
use tiny_keccak::sha3_256;

// Type tag to use for the NrsMapContainer stored on AppendOnlyData
const NRS_MAP_TYPE_TAG: u64 = 1_500;

const ERROR_MSG_NO_NRS_MAP_FOUND: &str = "No NRS Map found at this address";

// Raw data stored in the SAFE native data type for a NRS Map Container
type NrsMapRawData = Vec<(Vec<u8>, Vec<u8>)>;

// List of public names uploaded with details if they were added, updated or deleted from NrsMaps
pub type ProcessedEntries = BTreeMap<String, (String, String)>;

#[allow(dead_code)]
impl Safe {
    pub fn parse_url(url: &str) -> Result<XorUrlEncoder> {
        let sanitised_url = sanitised_url(url);
        debug!("Attempting to decode url: {}", sanitised_url);
        XorUrlEncoder::from_url(&sanitised_url).or_else(|err| {
            info!(
                "Falling back to NRS. XorUrl decoding failed with: {:?}",
                err
            );

            let (sub_names, host_str, path, version) =
                get_subnames_host_path_and_version(&sanitised_url)?;
            let hashed_host = xorname_from_nrs_string(&host_str)?;

            let encoded_xor = XorUrlEncoder::new(
                hashed_host,
                NRS_MAP_TYPE_TAG,
                SafeDataType::PublishedSeqAppendOnlyData,
                SafeContentType::NrsMapContainer,
                Some(&path),
                Some(sub_names),
                version,
            )?;

            Ok(encoded_xor)
        })
    }

    // Parses a safe:// URL and returns all the info in a XorUrlEncoder instance.
    // It also returns a second XorUrlEncoder if the URL was resolved as NRS-URL,
    // this second XorUrlEncoder instance contains the information of the parsed NRS-URL.
    pub fn parse_and_resolve_url(
        &self,
        url: &str,
    ) -> Result<(XorUrlEncoder, Option<XorUrlEncoder>)> {
        let xorurl_encoder = Safe::parse_url(url)?;
        if xorurl_encoder.content_type() == SafeContentType::NrsMapContainer {
            let (_version, nrs_map) = self.nrs_map_container_get(&url).map_err(|_| {
                Error::InvalidInput(
                    "The location couldn't be resolved from the NRS URL provided".to_string(),
                )
            })?;
            let xorurl = nrs_map.resolve_for_subnames(xorurl_encoder.sub_names())?;
            Ok((XorUrlEncoder::from_url(&xorurl)?, Some(xorurl_encoder)))
        } else {
            Ok((xorurl_encoder, None))
        }
    }

    pub fn nrs_map_container_add(
        &mut self,
        name: &str,
        link: &str,
        default: bool,
        hard_link: bool,
        dry_run: bool,
    ) -> Result<(u64, XorUrl, ProcessedEntries, NrsMap)> {
        info!("Adding to NRS map...");
        // GET current NRS map from name's TLD
        let (xorurl_encoder, _) = validate_nrs_name(name)?;
        let xorurl = xorurl_encoder.to_string()?;
        let (version, mut nrs_map) = self.nrs_map_container_get(&xorurl)?;
        debug!("NRS, Existing data: {:?}", nrs_map);

        let link = nrs_map.nrs_map_update_or_create_data(name, link, default, hard_link)?;
        let mut processed_entries = ProcessedEntries::new();
        processed_entries.insert(name.to_string(), (CONTENT_ADDED_SIGN.to_string(), link));

        debug!("The new NRS Map: {:?}", nrs_map);
        if !dry_run {
            // Append new version of the NrsMap in the Published AppendOnlyData (NRS Map Container)
            let nrs_map_raw_data = gen_nrs_map_raw_data(&nrs_map)?;
            self.safe_app.append_seq_append_only_data(
                nrs_map_raw_data,
                version + 1,
                xorurl_encoder.xorname(),
                xorurl_encoder.type_tag(),
            )?;
        }

        Ok((version + 1, xorurl, processed_entries, nrs_map))
    }

    /// # Create a NrsMapContainer.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use rand::distributions::Alphanumeric;
    /// # use rand::{thread_rng, Rng};
    /// # use unwrap::unwrap;
    /// # use safe_api::Safe;
    /// # let mut safe = Safe::default();
    /// # safe.connect("", Some("fake-credentials")).unwrap();
    /// let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();
    /// let file_xorurl = safe.files_put_published_immutable(&vec![], None, false).unwrap();
    /// let (xorurl, _processed_entries, nrs_map_container) = safe.nrs_map_container_create(&rand_string, &file_xorurl, true, false, false).unwrap();
    /// assert!(xorurl.contains("safe://"))
    /// ```
    pub fn nrs_map_container_create(
        &mut self,
        name: &str,
        link: &str,
        default: bool,
        hard_link: bool,
        dry_run: bool,
    ) -> Result<(XorUrl, ProcessedEntries, NrsMap)> {
        info!("Creating an NRS map");
        let (_, nrs_url) = validate_nrs_name(name)?;
        if self.nrs_map_container_get(&nrs_url).is_ok() {
            Err(Error::ContentError(
                "NRS name already exists. Please use 'nrs add' command to add sub names to it"
                    .to_string(),
            ))
        } else {
            let mut nrs_map = NrsMap::default();
            let link = nrs_map.nrs_map_update_or_create_data(&name, link, default, hard_link)?;
            let mut processed_entries = ProcessedEntries::new();
            processed_entries.insert(name.to_string(), (CONTENT_ADDED_SIGN.to_string(), link));

            debug!("The new NRS Map: {:?}", nrs_map);
            if dry_run {
                Ok(("".to_string(), processed_entries, nrs_map))
            } else {
                let (_, public_name, _, _) = get_subnames_host_path_and_version(&nrs_url)?;
                let nrs_xorname = xorname_from_nrs_string(&public_name)?;
                debug!(
                    "XorName for \"{:?}\" is \"{:?}\"",
                    &public_name, &nrs_xorname
                );

                // Store the NrsMapContainer in a Published AppendOnlyData
                let nrs_map_raw_data = gen_nrs_map_raw_data(&nrs_map)?;
                let xorname = self.safe_app.put_seq_append_only_data(
                    nrs_map_raw_data,
                    Some(nrs_xorname),
                    NRS_MAP_TYPE_TAG,
                    None,
                )?;

                let xorurl = XorUrlEncoder::encode(
                    xorname,
                    NRS_MAP_TYPE_TAG,
                    SafeDataType::PublishedSeqAppendOnlyData,
                    SafeContentType::NrsMapContainer,
                    None,
                    None,
                    None,
                    self.xorurl_base,
                )?;

                Ok((xorurl, processed_entries, nrs_map))
            }
        }
    }

    pub fn nrs_map_container_remove(
        &mut self,
        name: &str,
        dry_run: bool,
    ) -> Result<(u64, XorUrl, ProcessedEntries, NrsMap)> {
        info!("Removing from NRS map...");
        // GET current NRS map from &name TLD
        let (xorurl_encoder, _) = validate_nrs_name(name)?;
        let xorurl = xorurl_encoder.to_string()?;
        let (version, mut nrs_map) = self.nrs_map_container_get(&xorurl)?;
        debug!("NRS, Existing data: {:?}", nrs_map);

        let removed_link = nrs_map.nrs_map_remove_subname(name)?;
        let mut processed_entries = ProcessedEntries::new();
        processed_entries.insert(
            name.to_string(),
            (CONTENT_DELETED_SIGN.to_string(), removed_link),
        );

        debug!("The new NRS Map: {:?}", nrs_map);
        if !dry_run {
            // Append new version of the NrsMap in the Published AppendOnlyData (NRS Map Container)
            let nrs_map_raw_data = gen_nrs_map_raw_data(&nrs_map)?;
            self.safe_app.append_seq_append_only_data(
                nrs_map_raw_data,
                version + 1,
                xorurl_encoder.xorname(),
                xorurl_encoder.type_tag(),
            )?;
        }

        Ok((version + 1, xorurl, processed_entries, nrs_map))
    }

    /// # Fetch an existing NrsMapContainer.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use safe_api::Safe;
    /// # use rand::distributions::Alphanumeric;
    /// # use rand::{thread_rng, Rng};
    /// # let mut safe = Safe::default();
    /// # safe.connect("", Some("fake-credentials")).unwrap();
    /// let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();
    /// let file_xorurl = safe.files_put_published_immutable(&vec![], Some("text/plain"), false).unwrap();
    /// let (xorurl, _processed_entries, _nrs_map) = safe.nrs_map_container_create(&rand_string, &file_xorurl, true, false, false).unwrap();
    /// let (version, nrs_map_container) = safe.nrs_map_container_get(&xorurl).unwrap();
    /// assert_eq!(version, 0);
    /// assert_eq!(nrs_map_container.get_default_link().unwrap(), file_xorurl);
    /// ```
    pub fn nrs_map_container_get(&self, url: &str) -> Result<(u64, NrsMap)> {
        debug!("Getting latest resolvable map container from: {:?}", url);
        let xorurl_encoder = Safe::parse_url(url)?;

        // Check if the URL specified a specific version of the content or simply the latest available
        let data = xorurl_encoder.content_version().map_or_else(
            || {
                self.safe_app
                    .get_latest_seq_append_only_data(xorurl_encoder.xorname(), NRS_MAP_TYPE_TAG)
            },
            |content_version| {
                let (key, value) = self
                    .safe_app
                    .get_seq_append_only_data(
                        xorurl_encoder.xorname(),
                        NRS_MAP_TYPE_TAG,
                        content_version,
                    )
                    .map_err(|_| {
                        Error::VersionNotFound(format!(
                            "Version '{}' is invalid for NRS Map Container found at \"{}\"",
                            content_version, url,
                        ))
                    })?;
                Ok((content_version, (key, value)))
            },
        );

        match data {
            Ok((version, (_key, value))) => {
                debug!("Nrs map retrieved.... v{:?}, value {:?} ", &version, &value);
                // TODO: use RDF format and deserialise it
                let nrs_map = serde_json::from_str(&String::from_utf8_lossy(&value.as_slice()))
                    .map_err(|err| {
                        Error::ContentError(format!(
                            "Couldn't deserialise the NrsMap stored in the NrsContainer: {:?}",
                            err
                        ))
                    })?;
                Ok((version, nrs_map))
            }
            Err(Error::EmptyContent(_)) => {
                warn!("Nrs container found at {:?} was empty", &url);
                Ok((0, NrsMap::default()))
            }
            Err(Error::ContentNotFound(_)) => Err(Error::ContentNotFound(
                ERROR_MSG_NO_NRS_MAP_FOUND.to_string(),
            )),
            Err(Error::VersionNotFound(msg)) => Err(Error::VersionNotFound(msg)),
            Err(err) => Err(Error::NetDataError(format!(
                "Failed to get current version: {}",
                err
            ))),
        }
    }
}

fn validate_nrs_name(name: &str) -> Result<(XorUrlEncoder, String)> {
    let sanitised_url = sanitised_url(name);
    let xorurl_encoder = Safe::parse_url(&sanitised_url)?;
    if xorurl_encoder.content_version().is_some() {
        return Err(Error::InvalidInput(format!(
            "The NRS name/subname URL cannot cannot contain a version: {}",
            sanitised_url
        )));
    };
    Ok((xorurl_encoder, sanitised_url))
}

fn xorname_from_nrs_string(name: &str) -> Result<XorName> {
    let vec_hash = sha3_256(&name.to_string().into_bytes());
    let xorname = XorName(vec_hash);
    debug!("Resulting XorName for NRS \"{}\" is: {}", name, xorname);
    Ok(xorname)
}

fn sanitised_url(name: &str) -> String {
    // FIXME: make sure we remove the starting 'safe://'
    format!("safe://{}", name.replace("safe://", ""))
}

fn gen_nrs_map_raw_data(nrs_map: &NrsMap) -> Result<NrsMapRawData> {
    // The NrsMapContainer is an AppendOnlyData where each NRS Map version is an entry containing
    // the timestamp as the entry's key, and the serialised NrsMap as the entry's value
    // TODO: use RDF format
    let serialised_nrs_map = serde_json::to_string(nrs_map).map_err(|err| {
        Error::Unexpected(format!(
            "Couldn't serialise the NrsMap generated: {:?}",
            err
        ))
    })?;
    let now = gen_timestamp_nanos();

    Ok(vec![(
        now.into_bytes().to_vec(),
        serialised_nrs_map.as_bytes().to_vec(),
    )])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_nrs_map_container_create() {
        use crate::api::constants::FAKE_RDF_PREDICATE_LINK;
        use crate::nrs_map::DefaultRdf;
        use rand::distributions::Alphanumeric;
        use rand::{thread_rng, Rng};
        use unwrap::unwrap;

        let site_name: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();

        let mut safe = Safe::default();
        safe.connect("", Some("fake-credentials")).unwrap();

        let nrs_xorname = xorname_from_nrs_string(&site_name).unwrap();

        let (xor_url, _entries, nrs_map) = unwrap!(safe.nrs_map_container_create(
            &site_name,
            "safe://linked-from-<site_name>?v=0",
            true,
            false,
            false
        ));
        assert_eq!(nrs_map.sub_names_map.len(), 0);
        assert_eq!(
            unwrap!(nrs_map.get_default_link()),
            "safe://linked-from-<site_name>?v=0"
        );

        if let DefaultRdf::OtherRdf(def_data) = &nrs_map.default {
            assert_eq!(
                *def_data.get(FAKE_RDF_PREDICATE_LINK).unwrap(),
                "safe://linked-from-<site_name>?v=0".to_string()
            );
            assert_eq!(
                nrs_map.get_default().unwrap(),
                &DefaultRdf::OtherRdf(def_data.clone())
            );
        } else {
            panic!("No default definition map found...")
        }

        let decoder = XorUrlEncoder::from_url(&xor_url).unwrap();
        assert_eq!(nrs_xorname, decoder.xorname())
    }

    #[test]
    fn test_nrs_map_container_add() {
        use rand::distributions::Alphanumeric;
        use rand::{thread_rng, Rng};
        use unwrap::unwrap;

        let site_name: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();

        let mut safe = Safe::default();
        safe.connect("", Some("fake-credentials")).unwrap();

        let (_xor_url, _entries, nrs_map) = unwrap!(safe.nrs_map_container_create(
            &format!("b.{}", site_name),
            "safe://linked-from-<b.site_name>?v=0",
            true,
            false,
            false
        ));
        assert_eq!(nrs_map.sub_names_map.len(), 1);
        assert_eq!(
            unwrap!(nrs_map.get_default_link()),
            "safe://linked-from-<b.site_name>?v=0"
        );

        // add subname and set it as the new default too
        let (version, _xorurl, _entries, updated_nrs_map) = unwrap!(safe.nrs_map_container_add(
            &format!("a.b.{}", site_name),
            "safe://linked-from-<a.b.site_name>?v=0",
            true,
            false,
            false
        ));
        assert_eq!(version, 1);
        assert_eq!(updated_nrs_map.sub_names_map.len(), 1);
        assert_eq!(
            unwrap!(updated_nrs_map.get_default_link()),
            "safe://linked-from-<a.b.site_name>?v=0"
        );
    }

    #[test]
    fn test_nrs_map_container_add_or_remove_with_versioned_target() {
        use rand::distributions::Alphanumeric;
        use rand::{thread_rng, Rng};
        use unwrap::unwrap;

        let site_name: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();

        let mut safe = Safe::default();
        safe.connect("", Some("fake-credentials")).unwrap();

        let _ = unwrap!(safe.nrs_map_container_create(
            &format!("b.{}", site_name),
            "safe://linked-from-<b.site_name>?v=0",
            true,
            false,
            false
        ));

        let versioned_sitename = format!("safe://a.b.{}?v=6", site_name);
        match safe.nrs_map_container_add(
            &versioned_sitename,
            "safe://linked-from-<a.b.site_name>?v=0",
            true,
            false,
            false,
        ) {
            Ok(_) => panic!("Sync was unexpectdly successful"),
            Err(err) => assert_eq!(
                err,
                Error::InvalidInput(format!(
                    "The NRS name/subname URL cannot cannot contain a version: {}",
                    versioned_sitename
                ))
            ),
        };

        match safe.nrs_map_container_remove(&versioned_sitename, false) {
            Ok(_) => panic!("Sync was unexpectdly successful"),
            Err(err) => assert_eq!(
                err,
                Error::InvalidInput(format!(
                    "The NRS name/subname URL cannot cannot contain a version: {}",
                    versioned_sitename
                ))
            ),
        };
    }

    #[test]
    fn test_nrs_map_container_remove_one_of_two() {
        use rand::distributions::Alphanumeric;
        use rand::{thread_rng, Rng};
        use unwrap::unwrap;

        let site_name: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();

        let mut safe = Safe::default();
        safe.connect("", Some("fake-credentials")).unwrap();

        let (_xor_url, _entries, nrs_map) = unwrap!(safe.nrs_map_container_create(
            &format!("a.b.{}", site_name),
            "safe://linked-from-<a.b.site_name>?v=0",
            true,
            false,
            false
        ));
        assert_eq!(nrs_map.sub_names_map.len(), 1);

        let (_version, _xorurl, _entries, _updated_nrs_map) = unwrap!(safe.nrs_map_container_add(
            &format!("a2.b.{}", site_name),
            "safe://linked-from-<a2.b.site_name>?v=0",
            true,
            false,
            false
        ));

        // remove subname
        let (version, _xorurl, _entries, updated_nrs_map) =
            unwrap!(safe.nrs_map_container_remove(&format!("a.b.{}", site_name), false));
        assert_eq!(version, 2);
        assert_eq!(updated_nrs_map.sub_names_map.len(), 1);
        assert_eq!(
            unwrap!(updated_nrs_map.get_default_link()),
            "safe://linked-from-<a2.b.site_name>?v=0"
        );
    }

    #[test]
    fn test_nrs_map_container_remove_default_soft_link() {
        use rand::distributions::Alphanumeric;
        use rand::{thread_rng, Rng};
        use unwrap::unwrap;

        let site_name: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();

        let mut safe = Safe::default();
        safe.connect("", Some("fake-credentials")).unwrap();

        let (_xor_url, _entries, nrs_map) = unwrap!(safe.nrs_map_container_create(
            &format!("a.b.{}", site_name),
            "safe://linked-from-<a.b.site_name>?v=0",
            true,
            false,
            false
        ));
        assert_eq!(nrs_map.sub_names_map.len(), 1);

        // remove subname
        let (version, _xorurl, _entries, updated_nrs_map) =
            unwrap!(safe.nrs_map_container_remove(&format!("a.b.{}", site_name), false));
        assert_eq!(version, 1);
        assert_eq!(updated_nrs_map.sub_names_map.len(), 0);
        match updated_nrs_map.get_default_link() {
            Ok(_) => panic!("unexpectedly retrieved a default link"),
            Err(Error::ContentError(msg)) => assert_eq!(
                msg,
                "Default found for resolvable map (set to sub names 'a.b') cannot be resolved."
                    .to_string()
            ),
            Err(err) => panic!(format!("error returned is not the expected one: {}", err)),
        };
    }

    #[test]
    fn test_nrs_map_container_remove_default_hard_link() {
        use rand::distributions::Alphanumeric;
        use rand::{thread_rng, Rng};
        use unwrap::unwrap;

        let site_name: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();

        let mut safe = Safe::default();
        safe.connect("", Some("fake-credentials")).unwrap();

        let (_xor_url, _entries, nrs_map) = unwrap!(safe.nrs_map_container_create(
            &format!("a.b.{}", site_name),
            "safe://linked-from-<a.b.site_name>?v=0",
            true,
            true, // this sets the default to be a hard-link
            false
        ));
        assert_eq!(nrs_map.sub_names_map.len(), 1);

        // remove subname
        let (version, _xorurl, _entries, updated_nrs_map) =
            unwrap!(safe.nrs_map_container_remove(&format!("a.b.{}", site_name), false));
        assert_eq!(version, 1);
        assert_eq!(updated_nrs_map.sub_names_map.len(), 0);
        assert_eq!(
            unwrap!(updated_nrs_map.get_default_link()),
            "safe://linked-from-<a.b.site_name>?v=0"
        );
    }
}