did_toolkit/
document.rs

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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use crate::{did::DID, jwk::JWK, multibase::MultiBase, registry::Registry, url::URL};
use anyhow::anyhow;
use either::Either;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeSet, fmt::Display, hash::Hash, str::FromStr};
use url::Url;

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum VerificationMethodType {
    JWK2020,
    ECDSASECP256K12019,
    Ed255192018,
    Bls12381G12020,
    Bls12381G22020,
    PGP2021,
    ECDSASECP256K1Recovery2020,
    VerifiableCondition2021,
}

impl Default for VerificationMethodType {
    fn default() -> Self {
        VerificationMethodType::JWK2020
    }
}

impl FromStr for VerificationMethodType {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "JsonWebKey2020" => Ok(Self::JWK2020),
            "EcdsaSecp256k1VerificationKey2019" => Ok(Self::ECDSASECP256K12019),
            "Ed25519VerificationKey2018" => Ok(Self::Ed255192018),
            "Bls12381G1Key2020" => Ok(Self::Bls12381G12020),
            "Bls12381G2Key2020" => Ok(Self::Bls12381G22020),
            "PgpVerificationKey2021" => Ok(Self::PGP2021),
            "EcdsaSecp256k1RecoveryMethod2020" => Ok(Self::ECDSASECP256K1Recovery2020),
            "VerifiableCondition2021" => Ok(Self::VerifiableCondition2021),
            _ => Err(anyhow!("Property does not match")),
        }
    }
}

impl Display for VerificationMethodType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::JWK2020 => "JsonWebKey2020",
            Self::ECDSASECP256K12019 => "EcdsaSecp256k1VerificationKey2019",
            Self::Ed255192018 => "Ed25519VerificationKey2018",
            Self::Bls12381G12020 => "Bls12381G1Key2020",
            Self::Bls12381G22020 => "Bls12381G2Key2020",
            Self::PGP2021 => "PgpVerificationKey2021",
            Self::ECDSASECP256K1Recovery2020 => "EcdsaSecp256k1RecoveryMethod2020",
            Self::VerifiableCondition2021 => "VerifiableCondition2021",
        })
    }
}

#[derive(Clone, Default, Debug, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct VerificationMethod {
    pub id: URL,
    pub controller: DID,
    #[serde(rename = "type")]
    pub typ: VerificationMethodType,
    #[serde(rename = "publicKeyJwk", skip_serializing_if = "Option::is_none")]
    pub public_key_jwk: Option<JWK>,
    #[serde(rename = "publicKeyMultibase", skip_serializing_if = "Option::is_none")]
    pub public_key_multibase: Option<MultiBase>,
}

impl PartialEq for VerificationMethod {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.controller == other.controller
            && self.typ == other.typ
            && self.public_key_jwk == other.public_key_jwk
            && self.public_key_multibase == other.public_key_multibase
    }
}

// fixate the "key" for the hash on the verification method id. We don't want the rest considered,
// so we can constrain uniqueness on the id, not, say, the key material.
impl Hash for VerificationMethod {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state)
    }
}

impl VerificationMethod {
    /// Determines if a verification method is valid. To be valid, it must only contain one public
    /// key.
    pub fn valid(&self) -> Result<(), anyhow::Error> {
        if self.public_key_jwk.is_some() && self.public_key_multibase.is_some() {
            return Err(anyhow!(
                "Verification method {} provided both JWK and multibase keys",
                self.id
            ));
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
/// It's important to note here that the document that describes these is not very well formed.
/// <https://www.w3.org/TR/did-spec-registries/#service-types>
pub enum ServiceType {
    /// <https://www.w3.org/TR/did-spec-registries/#credentialregistry>
    CredentialRegistry,
    /// <https://identity.foundation/.well-known/resources/did-configuration/#linked-domain-service-endpoint>
    LinkedDomains,
    // there are others (such as DIDCommMessaging) that I did not supply here because they don't
    // appear to be finished.
}

impl Display for ServiceType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::LinkedDomains => "LinkedDomains",
            Self::CredentialRegistry => "CredentialRegistry",
        })
    }
}

impl FromStr for ServiceType {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "LinkedDomains" => Ok(Self::LinkedDomains),
            "CredentialRegistry" => Ok(Self::CredentialRegistry),
            _ => Err(anyhow!("Property does not match")),
        }
    }
}

#[derive(Clone, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ServiceEndpointProperties {
    // only used for LinkedDomains
    #[serde(skip_serializing_if = "Option::is_none")]
    pub origins: Option<BTreeSet<Url>>,

    // only used for CredentialRegistry
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registries: Option<BTreeSet<Url>>,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ServiceTypes(pub Either<ServiceType, BTreeSet<ServiceType>>);

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ServiceEndpoints(pub Either<Url, ServiceEndpointProperties>);

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ServiceEndpoint {
    pub id: Url,
    #[serde(rename = "type")]
    pub typ: ServiceTypes,
    #[serde(rename = "serviceEndpoint")]
    pub endpoint: ServiceEndpoints,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct VerificationMethodEither(pub Either<VerificationMethod, URL>);

#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct VerificationMethods(pub BTreeSet<VerificationMethodEither>);

impl VerificationMethods {
    /// Determines if the set of verification methods is valid. Takes an optional registry to
    /// lookup by [URL].
    pub fn valid(&self, registry: Option<&Registry>) -> Result<(), anyhow::Error> {
        for v in self.0.iter() {
            match &v.0 {
                Either::Left(vm) => vm.valid()?,
                Either::Right(url) => {
                    if let Some(registry) = &registry {
                        if let Some(doc) = registry.get(&url.to_did()) {
                            if let Some(vms) = doc.verification_method {
                                if vms.iter().any(|vm| &(*vm).id == url) {
                                    return Ok(());
                                } else {
                                    return Err(anyhow!("Could not locate verification method prescribed by {} in registry", url));
                                }
                            }
                        } else {
                            return Err(anyhow!(
                                "Could not retrieve DID from DID URL {} in registry",
                                url
                            ));
                        }
                    } else {
                        return Err(anyhow!("DID URL {} provided as verification method, but could not look up in registry because none was provided", url));
                    }
                }
            }
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AlsoKnownAsEither(pub Either<DID, Url>);

#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AlsoKnownAs(pub BTreeSet<AlsoKnownAsEither>);

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Controller(pub Either<DID, BTreeSet<DID>>);

impl Default for Controller {
    fn default() -> Self {
        Controller(Either::Right(BTreeSet::default()))
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Context(pub Either<Url, BTreeSet<Url>>);

impl Default for Context {
    fn default() -> Self {
        Context(Either::Right(BTreeSet::default()))
    }
}

/// The encapsulation of a decentralized identity document, or DID. This conforms to the did-core
/// spec in totality, according to the rules defined in
/// <https://www.w3.org/TR/did-core/#core-properties>. Divergence from the spec will be considered a
/// bug, unless otherwise noted.
///
/// Please see the individual properties regarding their use. Types in this module will remain
/// undocumented for brevity's sake, with the exception of methods that live on those types.
///
/// One notable thing in this implementation is use of the [either] crate with wrapping types. This
/// is used to aid in the (de)-serialization of documents properties that can consume multiple
/// switched types. Unfortunately, the spec is not very kind to users of statically-typed
/// languages, so we must take extra precautions to ensure all valid documents can be parsed. To
/// utilize most of these types, there may be an "either wrapper" as well as the [either::Either]
/// enum itself to encapsulate a type. For example, [AlsoKnownAs] encapsulates [AlsoKnownAsEither]
/// as a [BTreeSet] which then encapsulates [either::Either] types depending on which style of
/// attribute was used, as [DID]s and hypertext [url::Url]s can be used interchangeably. This
/// approach reduces memory usage and computation time by storing structs instead of raw strings
/// and "figuring it out later".
///
/// JSON-LD attributes (`@context`, specifically), are accounted for but not used by this
/// implementation. This allows you to generate documents and consume ones that follow the JSON-LD
/// specification but does not attempt to validate the document using the JSON-LD schema. See the
/// crate's README for more information regarding this decision.
///
/// [serde] crate implementations are available for all types, to ensure valid [serde_json] and
/// [ciborium] I/O, but other formats that [serde] supports should be technically possible to
/// support without issue.
///
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Document {
    /// JSON-LD @context support
    #[serde(rename = "@context", skip_serializing_if = "Option::is_none")]
    pub context: Option<Context>,
    /// The DID that this document corresponds to. Will be used as the key when storing in a
    /// [Registry]. This is called the "DID Subject" in the specification.
    pub id: DID,
    /// alsoKnownAs determines equivalence for two documents for all purposes. See
    /// <https://www.w3.org/TR/did-core/#also-known-as> for more.
    #[serde(rename = "alsoKnownAs", skip_serializing_if = "Option::is_none")]
    pub also_known_as: Option<AlsoKnownAs>,
    // controller determines if another [DID] is capable of taking actions for this [DID]. See
    // <https://www.w3.org/TR/did-core/#did-controller> for more.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub controller: Option<Controller>,
    /// [VerificationMethod]s are used to verify the identity claiming this document. See
    /// <https://www.w3.org/TR/did-core/#verification-methods> for more. Most following properties
    /// that use [VerificationMethods] may refer to this portion of the document by [URL] to add
    /// additional capabilities to a specific [VerificationMethod].
    #[serde(rename = "verificationMethod", skip_serializing_if = "Option::is_none")]
    pub verification_method: Option<BTreeSet<VerificationMethod>>,
    /// This set of [VerificationMethods] corresponds to authentication.
    /// <https://www.w3.org/TR/did-core/#authentication>
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authentication: Option<VerificationMethods>,
    /// This set of [VerificationMethods] corresponds to assertions.
    /// <https://www.w3.org/TR/did-core/#assertion>
    #[serde(rename = "assertionMethod", skip_serializing_if = "Option::is_none")]
    pub assertion_method: Option<VerificationMethods>,
    /// This set of [VerificationMethods] refers to key agreement.
    /// <https://www.w3.org/TR/did-core/#key-agreement>
    #[serde(rename = "keyAgreement", skip_serializing_if = "Option::is_none")]
    pub key_agreement: Option<VerificationMethods>,
    /// This set of [VerificationMethods] refers to capability invocation.
    /// <https://www.w3.org/TR/did-core/#capability-invocation>
    #[serde(
        rename = "capabilityInvocation",
        skip_serializing_if = "Option::is_none"
    )]
    /// This set of [VerificationMethods] refers to capability delegation.
    /// <https://www.w3.org/TR/did-core/#capability-delegation>
    pub capability_invocation: Option<VerificationMethods>,
    #[serde(
        rename = "capabilityDelegation",
        skip_serializing_if = "Option::is_none"
    )]
    pub capability_delegation: Option<VerificationMethods>,
    /// This portion of the document refers to affected services. Services are specially provided
    /// by the "DID registry": <https://www.w3.org/TR/did-spec-registries/> and rely on enums to
    /// determine how the service is treated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service: Option<BTreeSet<ServiceEndpoint>>,
}

impl Document {
    /// Determines if a document is valid. Takes an optional registry to resolve [URL]s
    pub fn valid(&self, registry: Option<&Registry>) -> Result<(), anyhow::Error> {
        if let Some(vm) = &self.verification_method {
            for v in vm.iter() {
                v.valid()?;
            }
        }

        // these are all basically the same, call the inner verification method, or do something
        // with the DID URL.
        for field in [
            &self.authentication,
            &self.assertion_method,
            &self.key_agreement,
            &self.capability_invocation,
            &self.capability_delegation,
        ] {
            if let Some(field) = field {
                field.valid(registry)?
            }
        }

        Ok(())
    }
}

mod serde_support {
    use super::{
        AlsoKnownAsEither, Context, Controller, ServiceEndpointProperties, ServiceEndpoints,
        ServiceType, ServiceTypes, VerificationMethod, VerificationMethodEither,
        VerificationMethodType,
    };
    use crate::{did::DID, url::URL};
    use either::Either;
    use serde::{de::Visitor, ser::SerializeSeq, Deserialize, Serialize, Serializer};
    use std::{collections::BTreeSet, str::FromStr};
    use url::Url;

    struct ControllerVisitor;

    impl<'de> Visitor<'de> for ControllerVisitor {
        type Value = Controller;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expecting a DID or set of DIDs")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            match DID::parse(v) {
                Ok(did) => Ok(Controller(Either::Left(did))),
                Err(e) => Err(E::custom(e)),
            }
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::SeqAccess<'de>,
        {
            let mut set = BTreeSet::default();

            while let Some(elem) = seq.next_element::<String>()? {
                match DID::parse(&elem) {
                    Ok(did) => {
                        set.insert(did);
                    }
                    Err(e) => return Err(serde::de::Error::custom(e)),
                }
            }

            Ok(Controller(Either::Right(set)))
        }
    }

    impl<'de> Deserialize<'de> for Controller {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_any(ControllerVisitor)
        }
    }

    impl Serialize for Controller {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            match &self.0 {
                Either::Left(did) => serializer.serialize_str(&did.to_string()),
                Either::Right(set) => {
                    let mut seq = serializer.serialize_seq(Some(set.len()))?;
                    for item in set {
                        seq.serialize_element(&item.to_string())?;
                    }
                    seq.end()
                }
            }
        }
    }

    struct AlsoKnownAsVisitor;

    impl<'de> Visitor<'de> for AlsoKnownAsVisitor {
        type Value = AlsoKnownAsEither;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expecting a set of inter-mixed DIDs and URLs")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            let res = match DID::parse(v) {
                Ok(did) => Either::Left(did),
                Err(_) => match Url::parse(v) {
                    Ok(url) => Either::Right(url),
                    Err(e) => return Err(serde::de::Error::custom(e)),
                },
            };

            Ok(AlsoKnownAsEither(res))
        }
    }

    impl<'de> Deserialize<'de> for AlsoKnownAsEither {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_str(AlsoKnownAsVisitor)
        }
    }

    impl Serialize for AlsoKnownAsEither {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&match &self.0 {
                Either::Left(did) => did.to_string(),
                Either::Right(url) => url.to_string(),
            })
        }
    }

    struct VerificationMethodVisitor;
    impl<'de> Visitor<'de> for VerificationMethodVisitor {
        type Value = VerificationMethodEither;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expecting a set of verification methods or DID URLs")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            match URL::parse(v) {
                Ok(url) => Ok(VerificationMethodEither(Either::Right(url))),
                Err(e) => Err(serde::de::Error::custom(e)),
            }
        }

        fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::MapAccess<'de>,
        {
            let mut vm = VerificationMethod::default();

            while let Some(key) = map.next_key::<String>()? {
                match key.as_str() {
                    "id" => vm.id = map.next_value()?,
                    "controller" => vm.controller = map.next_value()?,
                    "type" => vm.typ = map.next_value()?,
                    "publicKeyJwk" => vm.public_key_jwk = map.next_value()?,
                    "publicKeyMultibase" => vm.public_key_multibase = map.next_value()?,
                    _ => {
                        return Err(serde::de::Error::unknown_field(
                            &key,
                            &[
                                "id",
                                "controller",
                                "type",
                                "publicKeyJwk",
                                "publicKeyMultibase",
                            ],
                        ))
                    }
                }
            }

            Ok(VerificationMethodEither(Either::Left(vm)))
        }
    }

    impl<'de> Deserialize<'de> for VerificationMethodEither {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_any(VerificationMethodVisitor)
        }
    }

    impl Serialize for VerificationMethodEither {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            match &self.0 {
                Either::Left(vm) => vm.serialize(serializer),
                Either::Right(url) => url.serialize(serializer),
            }
        }
    }

    struct ServiceTypeVisitor;

    impl<'de> Visitor<'de> for ServiceTypeVisitor {
        type Value = ServiceTypes;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expecting set of service types or a single service type")
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::SeqAccess<'de>,
        {
            let mut set = BTreeSet::default();

            while let Some(elem) = seq.next_element::<String>()? {
                match ServiceType::from_str(&elem) {
                    Ok(st) => {
                        set.insert(st);
                    }
                    Err(e) => return Err(serde::de::Error::custom(e)),
                }
            }

            Ok(ServiceTypes(Either::Right(set)))
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            Ok(ServiceTypes(match ServiceType::from_str(v) {
                Ok(st) => Either::Left(st),
                Err(e) => return Err(serde::de::Error::custom(e)),
            }))
        }
    }

    impl<'de> Deserialize<'de> for ServiceTypes {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_any(ServiceTypeVisitor)
        }
    }

    impl Serialize for ServiceTypes {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            match &self.0 {
                Either::Left(typ) => typ.serialize(serializer),
                Either::Right(set) => {
                    let mut seq = serializer.serialize_seq(Some(set.len()))?;

                    for item in set {
                        seq.serialize_element(item)?;
                    }

                    seq.end()
                }
            }
        }
    }

    struct ServiceEndpointVisitor;

    impl<'de> Visitor<'de> for ServiceEndpointVisitor {
        type Value = ServiceEndpoints;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expected a service URL or service endpoint definition")
        }

        fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::MapAccess<'de>,
        {
            let mut se = ServiceEndpointProperties::default();

            while let Some(key) = map.next_key::<String>()? {
                match key.as_str() {
                    "origins" => se.origins = map.next_value()?,
                    "registries" => se.registries = map.next_value()?,
                    _ => {
                        return Err(serde::de::Error::unknown_field(
                            &key,
                            &["origins", "registries"],
                        ))
                    }
                }
            }

            Ok(ServiceEndpoints(Either::Right(se)))
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            match Url::parse(v) {
                Ok(url) => Ok(ServiceEndpoints(Either::Left(url))),
                Err(e) => Err(serde::de::Error::custom(e)),
            }
        }
    }

    impl<'de> Deserialize<'de> for ServiceEndpoints {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_any(ServiceEndpointVisitor)
        }
    }

    impl Serialize for ServiceEndpoints {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            match &self.0 {
                Either::Left(url) => serializer.serialize_str(&url.to_string()),
                Either::Right(properties) => properties.serialize(serializer),
            }
        }
    }

    struct ContextVisitor;

    impl<'de> Visitor<'de> for ContextVisitor {
        type Value = Context;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expecting a URL or set of URLs")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            match Url::parse(v) {
                Ok(res) => Ok(Context(Either::Left(res))),
                Err(e) => Err(serde::de::Error::custom(e)),
            }
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::SeqAccess<'de>,
        {
            let mut set = BTreeSet::default();

            while let Some(elem) = seq.next_element::<String>()? {
                match Url::parse(&elem) {
                    Ok(res) => {
                        set.insert(res);
                    }
                    Err(e) => return Err(serde::de::Error::custom(e)),
                }
            }

            Ok(Context(Either::Right(set)))
        }
    }

    impl<'de> Deserialize<'de> for Context {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_any(ContextVisitor)
        }
    }

    impl Serialize for Context {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            match &self.0 {
                Either::Left(url) => serializer.serialize_str(&url.to_string()),
                Either::Right(set) => set.serialize(serializer),
            }
        }
    }

    struct VerificationMethodTypeVisitor;

    impl<'de> Visitor<'de> for VerificationMethodTypeVisitor {
        type Value = VerificationMethodType;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("Expected a valid verification method type")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            match VerificationMethodType::from_str(v) {
                Ok(typ) => Ok(typ),
                Err(e) => Err(serde::de::Error::custom(e)),
            }
        }
    }

    impl<'de> Deserialize<'de> for VerificationMethodType {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            deserializer.deserialize_str(VerificationMethodTypeVisitor)
        }
    }

    impl Serialize for VerificationMethodType {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&self.to_string())
        }
    }

    impl Serialize for ServiceType {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&self.to_string())
        }
    }
}