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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Copyright 2019 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// 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::helpers::get_subnames_host_path_and_version;
use super::xorurl_media_types::{MEDIA_TYPE_CODES, MEDIA_TYPE_STR};
use super::{Error, ResultReturn};
use log::debug;
use multibase::{decode, encode, Base};
use safe_nd::{XorName, XOR_NAME_LEN};
use serde::{Deserialize, Serialize};
use std::fmt;

const SAFE_URL_PROTOCOL: &str = "safe://";
const XOR_URL_VERSION_1: u64 = 0x1; // TODO: consider using 16 bits
const XOR_URL_STR_MAX_LENGTH: usize = 44;
const XOR_NAME_BYTES_OFFSET: usize = 4; // offset where to find the XoR name bytes

// The XOR-URL type
pub type XorUrl = String;

// We encode the content type that a XOR-URL is targetting, this allows the consumer/user to
// treat the content in particular ways when the content requires it.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum SafeContentType {
    Raw,
    Wallet,
    FilesContainer,
    NrsMapContainer,
    MediaType(String),
}

impl std::fmt::Display for SafeContentType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl SafeContentType {
    #[allow(dead_code)]
    pub fn from_u16(value: u16) -> ResultReturn<SafeContentType> {
        match value {
            0 => Ok(SafeContentType::Raw),
            1 => Ok(SafeContentType::Wallet),
            2 => Ok(SafeContentType::FilesContainer),
            3 => Ok(SafeContentType::NrsMapContainer),
            _other => Err(Error::InvalidInput("Invalid Media-type code".to_string())),
        }
    }

    #[allow(dead_code)]
    pub fn value(&self) -> ResultReturn<u16> {
        match &*self {
            SafeContentType::Raw => Ok(0),
            SafeContentType::Wallet => Ok(1),
            SafeContentType::FilesContainer => Ok(2),
            SafeContentType::NrsMapContainer => Ok(3),
            SafeContentType::MediaType(media_type) => match MEDIA_TYPE_CODES.get(media_type) {
                Some(media_type_code) => Ok(*media_type_code),
                None => Err(Error::Unexpected("Unsupported Media-type".to_string())),
            },
        }
    }
}

// We also encode the native SAFE data type where the content is being stored on the SAFE Network,
// this allows us to fetch the targetted data using the corresponding API, regardless of the
// data that is being held which is identified by the SafeContentType instead.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum SafeDataType {
    SafeKey = 0x00,
    PublishedImmutableData = 0x01,
    UnpublishedImmutableData = 0x02,
    SeqMutableData = 0x03,
    UnseqMutableData = 0x04,
    PublishedSeqAppendOnlyData = 0x05,
    PublishedUnseqAppendOnlyData = 0x06,
    UnpublishedSeqAppendOnlyData = 0x07,
    UnpublishedUnseqAppendOnlyData = 0x08,
}

impl std::fmt::Display for SafeDataType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl SafeDataType {
    #[allow(dead_code)]
    pub fn from_u64(value: u64) -> ResultReturn<SafeDataType> {
        match value {
            0 => Ok(SafeDataType::SafeKey),
            1 => Ok(SafeDataType::PublishedImmutableData),
            2 => Ok(SafeDataType::UnpublishedImmutableData),
            3 => Ok(SafeDataType::SeqMutableData),
            4 => Ok(SafeDataType::UnseqMutableData),
            5 => Ok(SafeDataType::PublishedSeqAppendOnlyData),
            6 => Ok(SafeDataType::PublishedUnseqAppendOnlyData),
            7 => Ok(SafeDataType::UnpublishedSeqAppendOnlyData),
            8 => Ok(SafeDataType::UnpublishedUnseqAppendOnlyData),
            _ => Err(Error::InvalidInput("Invalid SafeDataType code".to_string())),
        }
    }
}

#[derive(Debug, Clone)]
pub struct XorUrlEncoder {
    encoding_version: u64, // currently only v1 supported
    xorname: XorName,
    type_tag: u64,
    data_type: SafeDataType,
    content_type: SafeContentType,
    path: String,
    sub_names: Vec<String>,
    content_version: Option<u64>,
}

impl XorUrlEncoder {
    pub fn new(
        xorname: XorName,
        type_tag: u64,
        data_type: SafeDataType,
        content_type: SafeContentType,
        path: Option<&str>,
        sub_names: Option<Vec<String>>,
        content_version: Option<u64>,
    ) -> ResultReturn<Self> {
        if let SafeContentType::MediaType(ref media_type) = content_type {
            if !XorUrlEncoder::is_media_type_supported(media_type) {
                return Err(Error::InvalidMediaType(format!(
                        "Media-type '{}' not supported. You can use 'SafeContentType::Raw' as the 'content_type' for this type of content",
                        media_type
                    )));
            }
        }

        Ok(Self {
            encoding_version: XOR_URL_VERSION_1,
            xorname,
            type_tag,
            data_type,
            content_type,
            path: path.unwrap_or_else(|| "").to_string(),
            sub_names: sub_names.unwrap_or_else(|| vec![]),
            content_version,
        })
    }

    // A non-member utility function to check if a media-type is currently supported by XOR-URL encoding
    pub fn is_media_type_supported(media_type: &str) -> bool {
        MEDIA_TYPE_CODES.get(media_type).is_some()
    }

    // A non-member encoder function for convinience in some cases
    #[allow(clippy::too_many_arguments)]
    pub fn encode(
        xorname: XorName,
        type_tag: u64,
        data_type: SafeDataType,
        content_type: SafeContentType,
        path: Option<&str>,
        sub_names: Option<Vec<String>>,
        content_version: Option<u64>,
        base: &str,
    ) -> ResultReturn<String> {
        let xorurl_encoder = XorUrlEncoder::new(
            xorname,
            type_tag,
            data_type,
            content_type,
            path,
            sub_names,
            content_version,
        )?;
        xorurl_encoder.to_base(base)
    }

    pub fn from_url(xorurl: &str) -> ResultReturn<Self> {
        let (sub_names, cid_str, path, content_version) =
            get_subnames_host_path_and_version(&xorurl)?;

        let (_base, xorurl_bytes): (Base, Vec<u8>) = decode(&cid_str)
            .map_err(|err| Error::InvalidXorUrl(format!("Failed to decode XOR-URL: {:?}", err)))?;

        // let's do a sanity check before analysing byte by byte
        if xorurl_bytes.len() > XOR_URL_STR_MAX_LENGTH {
            return Err(Error::InvalidXorUrl(format!(
                "Invalid XOR-URL, encoded string too long: {} bytes",
                xorurl_bytes.len()
            )));
        }

        // let's make sure we support the XOR_URL version
        let u8_version: u8 = xorurl_bytes[0];
        let encoding_version: u64 = u64::from(u8_version);
        if encoding_version != XOR_URL_VERSION_1 {
            return Err(Error::InvalidXorUrl(format!(
                "Invalid or unsupported XOR-URL encoding version: {}",
                encoding_version
            )));
        }

        let mut content_type_bytes = [0; 2];
        content_type_bytes[0..].copy_from_slice(&xorurl_bytes[1..3]);
        let content_type = match u16::from_be_bytes(content_type_bytes) {
            0 => SafeContentType::Raw,
            1 => SafeContentType::Wallet,
            2 => SafeContentType::FilesContainer,
            3 => SafeContentType::NrsMapContainer,
            other => match MEDIA_TYPE_STR.get(&other) {
                Some(media_type_str) => SafeContentType::MediaType(media_type_str.to_string()),
                None => {
                    return Err(Error::InvalidXorUrl(format!(
                        "Invalid content type encoded in the XOR-URL string: {}",
                        other
                    )))
                }
            },
        };

        debug!(
            "Attempting to match content type of URL: {}, {:?}",
            &xorurl, content_type
        );

        let data_type = match xorurl_bytes[3] {
            0 => SafeDataType::SafeKey,
            1 => SafeDataType::PublishedImmutableData,
            2 => SafeDataType::UnpublishedImmutableData,
            3 => SafeDataType::SeqMutableData,
            4 => SafeDataType::UnseqMutableData,
            5 => SafeDataType::PublishedSeqAppendOnlyData,
            6 => SafeDataType::PublishedUnseqAppendOnlyData,
            7 => SafeDataType::UnpublishedSeqAppendOnlyData,
            8 => SafeDataType::UnpublishedUnseqAppendOnlyData,
            other => {
                return Err(Error::InvalidXorUrl(format!(
                    "Invalid SAFE data type encoded in the XOR-URL string: {}",
                    other
                )))
            }
        };

        let type_tag_offset = XOR_NAME_BYTES_OFFSET + XOR_NAME_LEN; // offset where to find the type tag bytes

        let mut xorname = XorName::default();
        xorname
            .0
            .copy_from_slice(&xorurl_bytes[XOR_NAME_BYTES_OFFSET..type_tag_offset]);

        let type_tag_bytes_len = xorurl_bytes.len() - type_tag_offset;

        let mut type_tag_bytes = [0; 8];
        type_tag_bytes[8 - type_tag_bytes_len..].copy_from_slice(&xorurl_bytes[type_tag_offset..]);
        let type_tag: u64 = u64::from_be_bytes(type_tag_bytes);

        Ok(Self {
            encoding_version,
            xorname,
            type_tag,
            data_type,
            content_type,
            path: path.to_string(),
            sub_names,
            content_version,
        })
    }

    #[allow(dead_code)]
    pub fn encoding_version(&self) -> u64 {
        self.encoding_version
    }

    pub fn data_type(&self) -> SafeDataType {
        self.data_type.clone()
    }

    pub fn content_type(&self) -> SafeContentType {
        self.content_type.clone()
    }

    pub fn xorname(&self) -> XorName {
        self.xorname
    }

    pub fn type_tag(&self) -> u64 {
        self.type_tag
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    #[allow(dead_code)]
    pub fn set_path(&mut self, path: &str) {
        if path.is_empty() || path.starts_with('/') {
            self.path = path.to_string();
        } else {
            self.path = format!("/{}", path);
        }
    }

    pub fn sub_names(&self) -> Vec<String> {
        self.sub_names.to_vec()
    }

    pub fn content_version(&self) -> Option<u64> {
        self.content_version
    }

    pub fn set_content_version(&mut self, version: Option<u64>) {
        self.content_version = version;
    }

    // XOR-URL encoding format (var length from 36 to 44 bytes):
    // 1 byte for encoding version
    // 2 bytes for content type (enough to start including some MIME types also)
    // 1 byte for SAFE native data type
    // 32 bytes for XoR Name
    // and up to 8 bytes for type_tag
    // query param "v=" is treated as the content version
    pub fn to_string(&self) -> ResultReturn<String> {
        self.to_base("")
    }

    pub fn to_base(&self, base: &str) -> ResultReturn<String> {
        // let's set the first byte with the XOR-URL format version
        let mut cid_vec: Vec<u8> = vec![XOR_URL_VERSION_1 as u8];

        // add the content type bytes
        let content_type: u16 = match &self.content_type {
            SafeContentType::Raw => 0x0000,
            SafeContentType::Wallet => 0x0001,
            SafeContentType::FilesContainer => 0x0002,
            SafeContentType::NrsMapContainer => 0x0003,
            SafeContentType::MediaType(media_type) => match MEDIA_TYPE_CODES.get(media_type) {
                Some(media_type_code) => *media_type_code,
                None => {
                    return Err(Error::Unexpected(format!(
                        "Failed to encode Media-type '{}'",
                        media_type
                    )))
                }
            },
        };
        cid_vec.extend_from_slice(&content_type.to_be_bytes());

        // push the SAFE data type byte
        cid_vec.push(self.data_type.clone() as u8);

        let sub_names = if !self.sub_names.is_empty() {
            format!("{}.", self.sub_names.join("."))
        } else {
            "".to_string()
        };

        // add the xorname 32 bytes
        cid_vec.extend_from_slice(&self.xorname.0);

        // let's get non-zero bytes only from th type_tag
        let start_byte: usize = (self.type_tag.leading_zeros() / 8) as usize;
        // add the non-zero bytes of type_tag
        cid_vec.extend_from_slice(&self.type_tag.to_be_bytes()[start_byte..]);

        let base_encoding = match base {
            "base32z" => Base::Base32z,
            "base32" => Base::Base32,
            "base64" => Base::Base64,
            base => {
                if !base.is_empty() {
                    println!(
                        "Base encoding '{}' not supported for XOR-URL. Using default 'base32z'.",
                        base
                    );
                }
                Base::Base32z
            }
        };
        let cid_str = encode(base_encoding, cid_vec);
        let xorurl = format!("{}{}{}{}", SAFE_URL_PROTOCOL, sub_names, cid_str, self.path);

        match self.content_version {
            Some(v) => Ok(format!("{}?v={}", xorurl, v)),
            None => Ok(xorurl),
        }
    }
}

impl fmt::Display for XorUrlEncoder {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        let str = self.to_string().map_err(|_| fmt::Error)?;
        write!(fmt, "{}", str)
    }
}

#[test]
fn test_xorurl_base32_encoding() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        0xa632_3c4d_4a32,
        SafeDataType::PublishedImmutableData,
        SafeContentType::Raw,
        None,
        None,
        None,
        "base32"
    ));
    let base32_xorurl =
        "safe://biaaaatcmrtgq2tmnzyheydcmrtgq2tmnzyheydcmrtgq2tmnzyheydcmvggi6e2srs";
    assert_eq!(xorurl, base32_xorurl);
}

#[test]
fn test_xorurl_base32z_encoding() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        0,
        SafeDataType::PublishedImmutableData,
        SafeContentType::Raw,
        None,
        None,
        None,
        "base32z"
    ));
    let base32z_xorurl = "safe://hbyyyyncj1gc4dkptz8yhuycj1gc4dkptz8yhuycj1gc4dkptz8yhuycj1";
    assert_eq!(xorurl, base32z_xorurl);
}

#[test]
fn test_xorurl_base64_encoding() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        4_584_545,
        SafeDataType::PublishedSeqAppendOnlyData,
        SafeContentType::FilesContainer,
        None,
        None,
        None,
        "base64"
    ));
    let base64_xorurl = "safe://mQACBTEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyRfRh";
    assert_eq!(xorurl, base64_xorurl);
    let xorurl_encoder = unwrap!(XorUrlEncoder::from_url(&base64_xorurl));
    assert_eq!(base64_xorurl, unwrap!(xorurl_encoder.to_base("base64")));
    assert_eq!("", xorurl_encoder.path());
    assert_eq!(XOR_URL_VERSION_1, xorurl_encoder.encoding_version());
    assert_eq!(xorname, xorurl_encoder.xorname());
    assert_eq!(4_584_545, xorurl_encoder.type_tag());
    assert_eq!(
        SafeDataType::PublishedSeqAppendOnlyData,
        xorurl_encoder.data_type()
    );
    assert_eq!(
        SafeContentType::FilesContainer,
        xorurl_encoder.content_type()
    );
}

#[test]
fn test_xorurl_default_base_encoding() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let base32z_xorurl = "safe://hbyyyyncj1gc4dkptz8yhuycj1gc4dkptz8yhuycj1gc4dkptz8yhuycj1";
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        0,
        SafeDataType::PublishedImmutableData,
        SafeContentType::Raw,
        None,
        None,
        None,
        "" // forces it to use the default
    ));
    assert_eq!(xorurl, base32z_xorurl);
}

#[test]
fn test_xorurl_decoding() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let type_tag: u64 = 0x0eef;
    let xorurl_encoder = unwrap!(XorUrlEncoder::new(
        xorname,
        type_tag,
        SafeDataType::PublishedImmutableData,
        SafeContentType::Raw,
        None,
        None,
        None,
    ));
    assert_eq!("", xorurl_encoder.path());
    assert_eq!(XOR_URL_VERSION_1, xorurl_encoder.encoding_version());
    assert_eq!(xorname, xorurl_encoder.xorname());
    assert_eq!(type_tag, xorurl_encoder.type_tag());
    assert_eq!(
        SafeDataType::PublishedImmutableData,
        xorurl_encoder.data_type()
    );
    assert_eq!(SafeContentType::Raw, xorurl_encoder.content_type());
}

#[test]
fn test_xorurl_decoding_with_path() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let type_tag: u64 = 0x0eef;
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        type_tag,
        SafeDataType::PublishedSeqAppendOnlyData,
        SafeContentType::Wallet,
        None,
        None,
        None,
        "base32z"
    ));

    let xorurl_with_path = format!("{}/subfolder/file", xorurl);
    let xorurl_encoder_with_path = unwrap!(XorUrlEncoder::from_url(&xorurl_with_path));
    assert_eq!(
        xorurl_with_path,
        unwrap!(xorurl_encoder_with_path.to_base("base32z"))
    );
    assert_eq!("/subfolder/file", xorurl_encoder_with_path.path());
    assert_eq!(
        XOR_URL_VERSION_1,
        xorurl_encoder_with_path.encoding_version()
    );
    assert_eq!(xorname, xorurl_encoder_with_path.xorname());
    assert_eq!(type_tag, xorurl_encoder_with_path.type_tag());
    assert_eq!(
        SafeDataType::PublishedSeqAppendOnlyData,
        xorurl_encoder_with_path.data_type()
    );
    assert_eq!(
        SafeContentType::Wallet,
        xorurl_encoder_with_path.content_type()
    );
}

#[test]
fn test_xorurl_decoding_with_subname() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let type_tag: u64 = 0x0eef;
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        type_tag,
        SafeDataType::PublishedImmutableData,
        SafeContentType::NrsMapContainer,
        None,
        Some(vec!("sub".to_string())),
        None,
        "base32z"
    ));

    let xorurl_with_subname = xorurl.to_string();
    assert!(xorurl_with_subname.contains("safe://sub."));
    let xorurl_encoder_with_subname = unwrap!(XorUrlEncoder::from_url(&xorurl_with_subname));
    assert_eq!(
        xorurl_with_subname,
        unwrap!(xorurl_encoder_with_subname.to_base("base32z"))
    );
    assert_eq!("", xorurl_encoder_with_subname.path());
    assert_eq!(1, xorurl_encoder_with_subname.encoding_version());
    assert_eq!(xorname, xorurl_encoder_with_subname.xorname());
    assert_eq!(type_tag, xorurl_encoder_with_subname.type_tag());
    assert_eq!(vec!("sub"), xorurl_encoder_with_subname.sub_names());
    assert_eq!(
        SafeContentType::NrsMapContainer,
        xorurl_encoder_with_subname.content_type()
    );
}

#[test]
fn test_xorurl_encoding_decoding_with_media_type() {
    use unwrap::unwrap;
    let xorname = XorName(*b"12345678901234567890123456789012");
    let type_tag: u64 = 0x4c2f;
    let xorurl = unwrap!(XorUrlEncoder::encode(
        xorname,
        type_tag,
        SafeDataType::PublishedImmutableData,
        SafeContentType::MediaType("text/html".to_string()),
        None,
        None,
        None,
        "base32z"
    ));

    let xorurl_encoder = unwrap!(XorUrlEncoder::from_url(&xorurl));
    assert_eq!(
        SafeContentType::MediaType("text/html".to_string()),
        xorurl_encoder.content_type()
    );
}