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
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use ssi_verification_methods_core::GenericVerificationMethod;

use crate::{DIDBuf, DIDURLBuf, DIDURLReference, DIDURLReferenceBuf, DID, DIDURL};

use super::{
    resource::{ExtractResource, FindResource, Resource, UsesResource},
    ResourceRef,
};

/// Reference to, or value of, a verification method.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum ValueOrReference {
    Reference(DIDURLReferenceBuf),
    /// Embedded verification method.
    Value(DIDVerificationMethod),
}

impl ValueOrReference {
    pub fn id(&self) -> DIDURLReference {
        match self {
            Self::Reference(r) => r.as_did_reference(),
            Self::Value(v) => DIDURLReference::Absolute(&v.id),
        }
    }

    pub fn as_value(&self) -> Option<&DIDVerificationMethod> {
        match self {
            Self::Value(v) => Some(v),
            _ => None,
        }
    }
}

impl From<DIDURLBuf> for ValueOrReference {
    fn from(value: DIDURLBuf) -> Self {
        Self::Reference(value.into())
    }
}

impl From<DIDURLReferenceBuf> for ValueOrReference {
    fn from(value: DIDURLReferenceBuf) -> Self {
        Self::Reference(value)
    }
}

impl From<DIDVerificationMethod> for ValueOrReference {
    fn from(value: DIDVerificationMethod) -> Self {
        Self::Value(value)
    }
}

impl UsesResource for ValueOrReference {
    fn uses_resource(&self, base_id: &DID, id: &DIDURL) -> bool {
        match self {
            Self::Reference(r) => *r.resolve(base_id) == *id,
            Self::Value(v) => v.uses_resource(base_id, id),
        }
    }
}

impl FindResource for ValueOrReference {
    fn find_resource(&self, base_did: &DID, id: &DIDURL) -> Option<ResourceRef> {
        match self {
            Self::Reference(_) => None,
            Self::Value(m) => m.find_resource(base_did, id),
        }
    }
}

impl ExtractResource for ValueOrReference {
    fn extract_resource(self, base_did: &DID, id: &DIDURL) -> Option<Resource> {
        match self {
            Self::Reference(_) => None,
            Self::Value(m) => m.extract_resource(base_did, id),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct DIDVerificationMethod {
    /// Verification method identifier.
    pub id: DIDURLBuf,

    /// type [property](https://www.w3.org/TR/did-core/#dfn-did-urls) of a verification method map.
    /// Should be registered in [DID Specification
    /// registries - Verification method types](https://www.w3.org/TR/did-spec-registries/#verification-method-types).
    #[serde(rename = "type")]
    pub type_: String,

    // Note: different than when the DID Document is the subject:
    //    The value of the controller property, which identifies the
    //    controller of the corresponding private key, MUST be a valid DID.
    /// [controller](https://w3c-ccg.github.io/ld-proofs/#controller) property of a verification
    /// method map.
    ///
    /// Not to be confused with the [controller](https://www.w3.org/TR/did-core/#dfn-controller) property of a DID document.
    pub controller: DIDBuf,

    /// Verification methods properties.
    #[serde(flatten)]
    pub properties: BTreeMap<String, serde_json::Value>,
}

impl DIDVerificationMethod {
    pub fn new(
        id: DIDURLBuf,
        type_: String,
        controller: DIDBuf,
        properties: BTreeMap<String, serde_json::Value>,
    ) -> Self {
        Self {
            id,
            type_,
            controller,
            properties,
        }
    }
}

impl From<DIDVerificationMethod> for GenericVerificationMethod {
    fn from(value: DIDVerificationMethod) -> Self {
        GenericVerificationMethod {
            id: value.id.into(),
            type_: value.type_,
            controller: value.controller.into(),
            properties: value.properties,
        }
    }
}

impl UsesResource for DIDVerificationMethod {
    fn uses_resource(&self, _base_did: &DID, id: &DIDURL) -> bool {
        self.id == *id
    }
}

impl FindResource for DIDVerificationMethod {
    fn find_resource(&self, _base_did: &DID, id: &DIDURL) -> Option<ResourceRef> {
        if self.id == *id {
            Some(ResourceRef::VerificationMethod(self))
        } else {
            None
        }
    }
}

impl ExtractResource for DIDVerificationMethod {
    fn extract_resource(self, _base_did: &DID, id: &DIDURL) -> Option<Resource> {
        if self.id == *id {
            Some(Resource::VerificationMethod(self))
        } else {
            None
        }
    }
}