vade_didcomm/protocols/issue_credential/
datatypes.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::datatypes::Data;
6
7pub const ISSUE_CREDENTIAL_PROTOCOL_URL: &str = "https://didcomm.org/issue-credential/1.0";
8
9#[derive(Serialize, Deserialize, Clone, Debug)]
10#[serde(rename_all = "camelCase")]
11pub struct IssuerCredentialReq {
12    pub r#type: String,
13    pub from: Option<String>,
14    pub to: Option<String>,
15    pub credential_data: Option<CredentialData>,
16}
17
18/// CredentialAttach struct contains common fields which are required by
19/// offer-credential/request-credential/issue-credential messages for attachment.
20#[derive(Serialize, Deserialize, Clone, Debug)]
21pub struct CredentialAttach {
22    pub id: String,
23    pub mime_type: String,
24    pub data: Data,
25}
26
27/// CredentialProposal struct contains fields required by propose-credential message.
28#[derive(Serialize, Deserialize, Clone, Debug)]
29pub struct CredentialProposal {
30    pub id: String,
31    pub comment: String,
32    pub schema_issuer_did: String,
33    pub schema_id: String,
34    pub schema_name: String,
35    pub schema_version: String,
36    pub cred_def_id: String,
37    pub issuer_did: String,
38}
39
40/// Attribute struct is required for Credential Preview.
41#[derive(Serialize, Deserialize, Clone, Debug)]
42pub struct Attribute {
43    pub name: String,
44    pub mime_type: String,
45    pub value: String,
46}
47
48/// CredentialPreview struct contains fields required for offer-credential and propose-credential message.
49#[derive(Serialize, Deserialize, Clone, Debug)]
50pub struct CredentialPreview {
51    pub r#type: String,
52    pub attributes: Vec<Attribute>,
53}
54
55/// CredentialData struct is the general structure which contains all optional fields
56/// required for all messages of Issue Credential protocol.
57#[derive(Serialize, Deserialize, Clone, Debug)]
58pub struct CredentialData {
59    #[serde(skip_serializing_if = "Option::is_none")]
60    // credential_preview is sent only with offer-credential, propose-credential
61    pub credential_preview: Option<CredentialPreview>,
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub comment: Option<String>,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub data_attach: Option<Vec<CredentialAttach>>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub credential_proposal: Option<CredentialProposal>,
68}
69
70// properties for ProblemReport messages that are not part of the default DIDComm message set
71#[derive(Serialize, Deserialize, Clone, Debug)]
72pub struct ProblemReportData {
73    pub user_type: UserType,
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub description: Option<String>,
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub problem_items: Option<String>,
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub who_retries: Option<String>,
80    #[serde(skip_serializing_if = "Option::is_none")]
81    pub fix_hint: Option<String>,
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub impact: Option<String>,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub r#where: Option<String>,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub noticed_time: Option<String>,
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub tracking_uri: Option<String>,
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub escalation_uri: Option<String>,
92}
93
94/// Problem report structure contains fields which are required for reporting problem
95#[derive(Serialize, Deserialize, Clone, Debug)]
96pub struct ProblemReport {
97    pub r#type: String,
98    pub from: Option<String>,
99    pub to: Option<Vec<String>>,
100    pub id: String,
101    pub thid: Option<String>,
102    pub body: ProblemReportData,
103}
104
105// properties for Ack messages that are not part of the default DIDComm message set
106#[derive(Serialize, Deserialize, Clone, Debug)]
107pub struct AckData {
108    pub status: AckStatus,
109    pub user_type: UserType,
110}
111
112/// Ack structure contains fields which are sent as acknowledgment of received credential
113#[derive(Serialize, Deserialize, Clone, Debug)]
114pub struct Ack {
115    pub from: Option<String>,
116    pub to: Option<Vec<String>>,
117    pub r#type: String,
118    pub id: String,
119    pub thid: Option<String>,
120    pub body: AckData,
121}
122
123#[derive(Serialize, Deserialize, Clone, Debug)]
124pub enum AckStatus {
125    OK,
126    FAIL,
127    PENDING,
128}
129
130#[derive(Serialize, Deserialize, Clone, Debug)]
131pub enum State {
132    SendProposeCredential,
133    ReceiveProposeCredential,
134    SendOfferCredential,
135    ReceiveOfferCredential,
136    SendRequestCredential,
137    ReceiveRequestCredential,
138    SendIssueCredential,
139    ReceiveIssueCredential,
140    ProblemReported,
141    Acknowledged,
142    Unknown,
143}
144
145impl fmt::Display for State {
146    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147        write!(f, "{:?}", self)
148    }
149}
150
151impl std::str::FromStr for State {
152    type Err = String;
153
154    fn from_str(s: &str) -> Result<Self, Self::Err> {
155        match s {
156            "SendProposeCredential" => Ok(State::SendProposeCredential),
157            "ReceiveProposeCredential" => Ok(State::ReceiveProposeCredential),
158            "SendOfferCredential" => Ok(State::SendOfferCredential),
159            "ReceiveOfferCredential" => Ok(State::ReceiveOfferCredential),
160            "SendRequestCredential" => Ok(State::SendRequestCredential),
161            "ReceiveRequestCredential" => Ok(State::ReceiveRequestCredential),
162            "SendIssueCredential" => Ok(State::SendIssueCredential),
163            "ReceiveIssueCredential" => Ok(State::ReceiveIssueCredential),
164            "ProblemReported" => Ok(State::ProblemReported),
165            "Acknowledged" => Ok(State::Acknowledged),
166            _ => Ok(State::Unknown),
167        }
168    }
169}
170
171#[derive(Serialize, Deserialize, Clone, Debug)]
172pub enum UserType {
173    Issuer,
174    Holder,
175    None,
176}
177
178impl fmt::Display for UserType {
179    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180        write!(f, "{:?}", self)
181    }
182}