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
use serde::{de::DeserializeOwned, Serialize};
use ssi_data_integrity::{CryptographicSuite, DataIntegrity};
use ssi_jwt::{ClaimSet, RegisteredClaims};

mod decode;
mod encode;

pub use decode::*;
pub use encode::*;

pub trait FromJwtClaims: Sized {
    type Error;

    fn from_jwt_claims(jwt: impl ClaimSet) -> Result<Self, Self::Error>;
}

pub trait ToJwtClaims {
    type Error;

    fn to_jwt_claims(&self) -> Result<RegisteredClaims, Self::Error>;
}

impl<S> FromJwtClaims for DataIntegrity<super::SpecializedJsonCredential, S>
where
    S: CryptographicSuite + TryFrom<ssi_data_integrity::Type>,
    S::VerificationMethod: DeserializeOwned,
    S::ProofOptions: DeserializeOwned,
    S::Signature: DeserializeOwned,
{
    type Error = JwtVcDecodeError;

    /// Decodes a verifiable credential from the claims of a JWT following the
    /// standard [JWT-VC encoding].
    ///
    /// [JWT-VC encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
    fn from_jwt_claims(jwt: impl ClaimSet) -> Result<Self, JwtVcDecodeError> {
        decode_jwt_vc_claims(jwt)
    }
}

impl<S: CryptographicSuite> ToJwtClaims for DataIntegrity<super::SpecializedJsonCredential, S>
where
    S::VerificationMethod: Serialize,
    S::ProofOptions: Serialize,
    S::Signature: Serialize,
{
    type Error = JwtVcEncodeError;

    /// Encodes this verifiable credential into the claims of a JWT following
    /// the standard [JWT-VC encoding].
    ///
    /// [JWT-VC encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVcEncodeError> {
        encode_jwt_vc_claims(self)
    }
}

impl ToJwtClaims for super::SpecializedJsonCredential {
    type Error = JwtVcEncodeError;

    /// Encodes this credential into the claims of a JWT following the standard
    /// [JWT-VC encoding].
    ///
    /// [JWT-VC encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVcEncodeError> {
        encode_jwt_vc_claims(self)
    }
}

impl<C: DeserializeOwned, S> FromJwtClaims for DataIntegrity<super::JsonPresentation<C>, S>
where
    S: CryptographicSuite + TryFrom<ssi_data_integrity::Type>,
    S::VerificationMethod: DeserializeOwned,
    S::ProofOptions: DeserializeOwned,
    S::Signature: DeserializeOwned,
{
    type Error = JwtVpDecodeError;

    /// Decodes a verifiable presentation from the claims of a JWT following the
    /// standard [JWT-VP encoding].
    ///
    /// [JWT-VP encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
    fn from_jwt_claims(jwt: impl ClaimSet) -> Result<Self, JwtVpDecodeError> {
        decode_jwt_vp_claims(jwt)
    }
}

impl<C: Serialize, S: CryptographicSuite> ToJwtClaims
    for DataIntegrity<super::JsonPresentation<C>, S>
where
    S::VerificationMethod: Serialize,
    S::ProofOptions: Serialize,
    S::Signature: Serialize,
{
    type Error = JwtVpEncodeError;

    /// Encodes this verifiable presentation into the claims of a JWT following
    /// the standard [JWT-VP encoding].
    ///
    /// [JWT-VP encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVpEncodeError> {
        encode_jwt_vp_claims(self)
    }
}

impl<C: Serialize> ToJwtClaims for super::JsonPresentation<C> {
    type Error = JwtVpEncodeError;

    /// Encodes this presentation into the claims of a JWT following the
    /// standard [JWT-VP encoding].
    ///
    /// [JWT-VP encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVpEncodeError> {
        encode_jwt_vp_claims(self)
    }
}