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
use std::{borrow::Cow, collections::HashMap, sync::Arc};

use iref::{Iri, IriBuf};
use ssi_claims_core::{MessageSignatureError, ProofValidationError, SignatureError};
use ssi_crypto::algorithm::SignatureAlgorithmType;
use ssi_jwk::JWK;
use static_iref::iri;

mod controller;
mod methods;
mod reference;
mod signature;
mod verification;

pub use controller::*;
pub use methods::*;
pub use reference::*;
pub use signature::*;
pub use verification::*;

#[doc(hidden)]
pub use ssi_core;

/// IRI of the RDF property associated to the `controller` term found in a
/// verification method.
pub const CONTROLLER_IRI: &Iri = iri!("https://w3id.org/security#controller");

/// Expected verification method type.
#[derive(Debug, Clone)]
pub enum ExpectedType {
    One(String),
    Many(Vec<String>),
}

impl From<String> for ExpectedType {
    fn from(value: String) -> Self {
        Self::One(value)
    }
}

/// Verification method.
pub trait VerificationMethod: Clone {
    /// Identifier of the verification method.
    fn id(&self) -> &Iri;

    /// Returns the IRI of the verification method controller.
    fn controller(&self) -> Option<&Iri>; // Should be an URI.
}

#[derive(Debug, thiserror::Error)]
pub enum VerificationMethodResolutionError {
    #[error("unknown key")]
    UnknownKey,

    /// Invalid key identifier.
    #[error("invalid key id `{0}`")]
    InvalidKeyId(String),

    /// Not a verification method.
    #[error("id `{0}` is not referring to a verification method")]
    NotAVerificationMethod(String),

    /// Unsupported key identifier.
    #[error("unsupported key id `{0}`")]
    UnsupportedKeyId(String),

    #[error("missing verification method")]
    MissingVerificationMethod,

    #[error(transparent)]
    InvalidVerificationMethod(#[from] InvalidVerificationMethod),

    /// Verifier internal error.
    #[error("internal error: {0}")]
    InternalError(String),
}

impl From<VerificationMethodResolutionError> for ProofValidationError {
    fn from(value: VerificationMethodResolutionError) -> Self {
        match value {
            VerificationMethodResolutionError::MissingVerificationMethod => Self::MissingPublicKey,
            e => Self::Other(e.to_string()),
        }
    }
}

impl From<VerificationMethodResolutionError> for SignatureError {
    fn from(value: VerificationMethodResolutionError) -> Self {
        match value {
            VerificationMethodResolutionError::MissingVerificationMethod => Self::MissingSigner,
            e => Self::other(e),
        }
    }
}

pub trait VerificationMethodSet: VerificationMethod {
    type TypeSet: VerificationMethodTypeSet;

    fn type_set() -> Self::TypeSet;
}

pub trait VerificationMethodTypeSet: 'static + Send + Sync {
    fn pick(&self) -> Option<&str>;
    fn contains(&self, ty: &str) -> bool;
}

impl VerificationMethodTypeSet for &'static str {
    fn contains(&self, ty: &str) -> bool {
        ty == *self
    }

    fn pick(&self) -> Option<&str> {
        Some(self)
    }
}

impl VerificationMethodTypeSet for &'static [&'static str] {
    fn contains(&self, ty: &str) -> bool {
        self.iter().any(|&t| t == ty)
    }

    fn pick(&self) -> Option<&str> {
        self.first().copied()
    }
}

#[derive(Default)]
pub struct ResolutionOptions {
    /// Accepted verification method types.
    pub accept: Option<Box<dyn VerificationMethodTypeSet>>,
}

pub trait VerificationMethodResolver {
    /// Verification method type.
    type Method: Clone;

    /// Resolve the verification method reference.
    #[allow(async_fn_in_trait)]
    async fn resolve_verification_method_with(
        &self,
        issuer: Option<&Iri>,
        method: Option<ReferenceOrOwnedRef<'_, Self::Method>>,
        options: ResolutionOptions,
    ) -> Result<Cow<Self::Method>, VerificationMethodResolutionError>;

    /// Resolve the verification method reference with the default options.
    #[allow(async_fn_in_trait)]
    async fn resolve_verification_method(
        &self,
        issuer: Option<&Iri>,
        method: Option<ReferenceOrOwnedRef<'_, Self::Method>>,
    ) -> Result<Cow<Self::Method>, VerificationMethodResolutionError> {
        self.resolve_verification_method_with(issuer, method, Default::default())
            .await
    }
}

impl<'t, T: VerificationMethodResolver> VerificationMethodResolver for &'t T {
    type Method = T::Method;

    async fn resolve_verification_method_with(
        &self,
        issuer: Option<&Iri>,
        method: Option<ReferenceOrOwnedRef<'_, T::Method>>,
        options: ResolutionOptions,
    ) -> Result<Cow<T::Method>, VerificationMethodResolutionError> {
        T::resolve_verification_method_with(self, issuer, method, options).await
    }
}

impl<M: VerificationMethod> VerificationMethodResolver for HashMap<IriBuf, M> {
    type Method = M;

    async fn resolve_verification_method_with(
        &self,
        _issuer: Option<&Iri>,
        method: Option<ReferenceOrOwnedRef<'_, Self::Method>>,
        _options: ResolutionOptions,
    ) -> Result<Cow<Self::Method>, VerificationMethodResolutionError> {
        match method {
            Some(ReferenceOrOwnedRef::Owned(method)) => Ok(Cow::Owned(method.clone())),
            Some(ReferenceOrOwnedRef::Reference(iri)) => match self.get(iri) {
                Some(method) => Ok(Cow::Borrowed(method)),
                None => Err(VerificationMethodResolutionError::UnknownKey),
            },
            None => Err(VerificationMethodResolutionError::MissingVerificationMethod),
        }
    }
}

pub trait SigningMethod<S, A: SignatureAlgorithmType>: VerificationMethod {
    fn sign_bytes(
        &self,
        secret: &S,
        algorithm: A::Instance,
        bytes: &[u8],
    ) -> Result<Vec<u8>, MessageSignatureError>;

    fn sign_bytes_multi(
        &self,
        secret: &S,
        algorithm: A::Instance,
        messages: &[Vec<u8>],
    ) -> Result<Vec<u8>, MessageSignatureError> {
        match messages.split_first() {
            Some((message, [])) => self.sign_bytes(secret, algorithm, message),
            // Some(_) => Err(MessageSignatureError::TooManyMessages),
            Some(_) => todo!(),
            None => Err(MessageSignatureError::MissingMessage),
        }
    }
}

pub struct MethodWithSecret<M: VerificationMethod, S> {
    pub method: M,
    pub secret: Arc<S>,
}

impl<M: VerificationMethod, S> MethodWithSecret<M, S> {
    pub fn new(method: M, secret: Arc<S>) -> Self {
        Self { method, secret }
    }
}

impl<A: SignatureAlgorithmType, M: SigningMethod<S, A>, S> MessageSigner<A>
    for MethodWithSecret<M, S>
{
    async fn sign(
        self,
        algorithm: A::Instance,
        message: &[u8],
    ) -> Result<Vec<u8>, MessageSignatureError> {
        self.method.sign_bytes(&self.secret, algorithm, message)
    }

    async fn sign_multi(
        self,
        algorithm: <A as SignatureAlgorithmType>::Instance,
        messages: &[Vec<u8>],
    ) -> Result<Vec<u8>, MessageSignatureError> {
        self.method
            .sign_bytes_multi(&self.secret, algorithm, messages)
    }
}

pub trait TypedVerificationMethod: VerificationMethod {
    fn expected_type() -> Option<ExpectedType>;

    fn type_match(ty: &str) -> bool;

    fn type_(&self) -> &str;
}

pub trait LinkedDataVerificationMethod {
    fn quads(&self, quads: &mut Vec<rdf_types::Quad>) -> rdf_types::Object;
}

impl<'a, T: LinkedDataVerificationMethod> LinkedDataVerificationMethod for &'a T {
    fn quads(&self, quads: &mut Vec<rdf_types::Quad>) -> rdf_types::Object {
        T::quads(*self, quads)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum InvalidVerificationMethod {
    #[error("invalid verification method IRI `{0}`")]
    InvalidIri(String),

    #[error("invalid verification method type IRI `{0}`")]
    InvalidTypeIri(IriBuf),

    #[error("invalid verification method type name `{0}`, expected `{1}`")]
    InvalidTypeName(String, String),

    #[error("missing verification method required property `{0}`")]
    MissingProperty(String),

    #[error("invalid verification method property `{0}`")]
    InvalidProperty(String),

    #[error("ambiguous public key")]
    AmbiguousPublicKey,

    #[error("unsupported method type `{0}`")]
    UnsupportedMethodType(String),
}

impl InvalidVerificationMethod {
    pub fn invalid_type_iri(iri: &Iri) -> Self {
        Self::InvalidTypeIri(iri.to_owned())
    }

    pub fn invalid_type_name(name: &str, expected: &str) -> Self {
        Self::InvalidTypeName(name.to_owned(), expected.to_owned())
    }

    pub fn missing_property(name: &str) -> Self {
        Self::MissingProperty(name.to_owned())
    }

    pub fn invalid_property(name: &str) -> Self {
        Self::InvalidProperty(name.to_owned())
    }
}

impl From<InvalidVerificationMethod> for ProofValidationError {
    fn from(_value: InvalidVerificationMethod) -> Self {
        Self::InvalidKey
    }
}

impl From<InvalidVerificationMethod> for SignatureError {
    fn from(value: InvalidVerificationMethod) -> Self {
        Self::other(value)
    }
}

/// Verification method that can be turned into a JSON Web Key.
pub trait JwkVerificationMethod: VerificationMethod {
    fn to_jwk(&self) -> Cow<JWK>;
}

/// Verification method that *may* be turned into a JSON Web Key.
pub trait MaybeJwkVerificationMethod: VerificationMethod {
    fn try_to_jwk(&self) -> Option<Cow<JWK>>;
}

impl<M: JwkVerificationMethod> MaybeJwkVerificationMethod for M {
    fn try_to_jwk(&self) -> Option<Cow<JWK>> {
        Some(M::to_jwk(self))
    }
}