vade_jwt_vc/
datatypes.rs

1/*
2  Copyright (c) 2018-present evan GmbH.
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15*/
16
17use serde::{Deserialize, Serialize};
18use std::collections::HashMap;
19
20pub const CREDENTIAL_PROOF_PURPOSE: &str = "assertionMethod";
21pub const DEFAULT_REVOCATION_CONTEXTS: [&str; 2] = [
22    "https://www.w3.org/2018/credentials/v1",
23    "https://w3id.org/vc-revocation-list-2020/v1",
24];
25
26/// Message passed to vade containing the desired credential type.
27/// Does not perform action if type does not indicate credential type jwt.
28/// This can be done by passing "jwt" as the value for "type".
29#[derive(Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub struct TypeOptions {
32    pub r#type: Option<String>,
33}
34
35/// Metadata about a property of a credential schema
36#[derive(Serialize, Deserialize, Clone)]
37#[serde(rename_all = "camelCase")]
38pub struct SchemaProperty {
39    pub r#type: String,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub format: Option<String>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub items: Option<Vec<String>>,
44}
45
46/// AssertionProof, typically used to ensure authenticity and integrity of a verifiable credential
47#[derive(Serialize, Deserialize, Clone)]
48#[serde(rename_all = "camelCase")]
49pub struct AssertionProof {
50    pub r#type: String,
51    pub created: String,
52    pub proof_purpose: String,
53    pub verification_method: String,
54    pub jws: String,
55}
56
57/// A verifiable credential issued by an issuer upon receiving a `CredentialRequest`.
58/// Specifies the signed values, the DID of the prover/subject, the `CredentialSchema`, and the `CredentialSignature`
59/// including revocation info.
60#[derive(Serialize, Deserialize, Clone)]
61#[serde(rename_all = "camelCase")]
62pub struct Credential {
63    #[serde(rename(serialize = "@context", deserialize = "@context"))]
64    pub context: Vec<String>,
65    pub id: String,
66    pub r#type: Vec<String>,
67    pub issuer: String,
68    pub issuance_date: String,
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub valid_until: Option<String>,
71    pub credential_subject: CredentialSubject,
72    pub credential_schema: CredentialSchemaReference,
73    pub credential_status: Option<CredentialStatus>,
74    pub proof: AssertionProof,
75}
76
77/// A verifiable credential with a blind signature that still needs to be processed by the holder
78#[derive(Serialize, Deserialize, Clone)]
79#[serde(rename_all = "camelCase")]
80pub struct UnsignedCredential {
81    #[serde(rename(serialize = "@context", deserialize = "@context"))]
82    pub context: Vec<String>,
83    pub id: String,
84    pub r#type: Vec<String>,
85    pub issuer: String,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub valid_until: Option<String>,
88    pub issuance_date: String,
89    pub credential_subject: CredentialSubject,
90    pub credential_schema: CredentialSchemaReference,
91    pub credential_status: Option<CredentialStatus>,
92}
93
94/// Payload/data part of a verifiable credential.
95#[derive(Serialize, Deserialize, Clone, Debug)]
96#[serde(rename_all = "camelCase")]
97pub struct CredentialSubject {
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub id: Option<String>,
100    pub data: HashMap<String, String>,
101}
102
103/// 'credentialStatus' property of a verifiable credential containing revocation information.
104#[derive(Serialize, Deserialize, Clone)]
105#[serde(rename_all = "camelCase")]
106pub struct CredentialStatus {
107    pub id: String,
108    pub r#type: String,
109    pub revocation_list_index: String,
110    pub revocation_list_credential: String,
111}
112
113/// Result of a verify_proof call
114#[derive(Serialize, Deserialize)]
115#[serde(rename_all = "camelCase")]
116pub struct ProofVerification {
117    pub verified: bool,
118}
119
120/// Reference to a credential schema.
121#[derive(Serialize, Deserialize, Clone)]
122#[serde(rename_all = "camelCase")]
123pub struct CredentialSchemaReference {
124    pub id: String,
125    pub r#type: String,
126}
127
128/// Payload for signing an Unsigned credential
129#[derive(Serialize, Deserialize)]
130#[serde(rename_all = "camelCase")]
131pub struct IssueCredentialPayload {
132    /// The VC to sign, without any appended proof
133    pub unsigned_vc: UnsignedCredential,
134    /// DID url of the public key of the issuer used to later verify the signature
135    pub issuer_public_key_id: String,
136    /// The public key of the issuer used to later verify the signature
137    pub issuer_public_key: String,
138}
139
140/// Payload for verifying a signed Credential.
141#[derive(Serialize, Deserialize)]
142#[serde(rename_all = "camelCase")]
143pub struct VerifyProofPayload {
144    /// VC to verify
145    pub credential: Credential,
146    /// Signer address
147    pub signer_address: String,
148    /// revocation list credential
149    pub revocation_list: Option<RevocationListCredential>,
150}
151
152/// Contains necessary information to sign the data
153#[derive(Serialize, Deserialize)]
154#[serde(rename_all = "camelCase")]
155pub struct SignerOptions {
156    /// Reference to the private key, will be forwarded to external signer if available
157    pub private_key: String,
158    /// DID of the identity
159    pub identity: String,
160}
161
162/// API payload needed to create a revocation list
163#[derive(Serialize, Deserialize)]
164#[serde(rename_all = "camelCase")]
165pub struct CreateRevocationListPayload {
166    /// DID of the issuer
167    pub issuer_did: String,
168    /// DID of the issuer's public key used to verify the credential's signature
169    pub issuer_public_key_did: String,
170    /// Private key of the issuer used to sign the credential
171    pub issuer_proving_key: String,
172    /// future did id for revocation list
173    pub credential_did: String,
174}
175
176/// API payload to revoke a credential as this credential's issuer.
177#[derive(Serialize, Deserialize)]
178#[serde(rename_all = "camelCase")]
179pub struct RevokeCredentialPayload {
180    /// DID of the issuer
181    pub issuer: String,
182    /// revocation list credential
183    pub revocation_list: RevocationListCredential,
184    /// Credential ID to revoke
185    pub revocation_id: String,
186    /// DID of the issuer's public key for verifying assertion proofs
187    pub issuer_public_key_did: String,
188    /// DID of the issuer's secret key for creating assertion proofs
189    pub issuer_proving_key: String,
190}
191
192/// A revocation list credential associating verifiable credential revocation IDs to their revocation status as a bit list. See
193/// <https://w3c-ccg.github.io/vc-status-rl-2020/#revocationlist2020credential>
194#[derive(Serialize, Deserialize, Clone)]
195#[serde(rename_all = "camelCase")]
196pub struct RevocationListCredential {
197    #[serde(rename(serialize = "@context", deserialize = "@context"))]
198    pub context: Vec<String>,
199    pub id: String,
200    pub r#type: Vec<String>,
201    pub issuer: String,
202    pub issued: String,
203    pub credential_subject: RevocationListCredentialSubject,
204    pub proof: AssertionProof,
205}
206
207impl RevocationListCredential {
208    pub fn new(
209        list: UnproofedRevocationListCredential,
210        proof: AssertionProof,
211    ) -> RevocationListCredential {
212        RevocationListCredential {
213            context: list.context,
214            id: list.id,
215            r#type: list.r#type,
216            issuer: list.issuer,
217            issued: list.issued,
218            credential_subject: list.credential_subject,
219            proof,
220        }
221    }
222}
223
224/// Payload part of a revocation list credential.
225#[derive(Serialize, Deserialize, Clone)]
226#[serde(rename_all = "camelCase")]
227pub struct RevocationListCredentialSubject {
228    pub id: String,
229    pub r#type: String,
230    pub encoded_list: String,
231}
232
233/// `RevocationListCredential` without a proof (for internal use only).
234#[derive(Serialize, Deserialize, Clone)]
235#[serde(rename_all = "camelCase")]
236pub struct UnproofedRevocationListCredential {
237    #[serde(rename(serialize = "@context", deserialize = "@context"))]
238    pub context: Vec<String>,
239    pub id: String,
240    pub r#type: Vec<String>,
241    pub issuer: String,
242    pub issued: String,
243    pub credential_subject: RevocationListCredentialSubject,
244}