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
pub use indy_data_types::anoncreds::cred_def::{
    CredentialDefinition, CredentialDefinitionData, CredentialDefinitionV1, SignatureType,
    CL_SIGNATURE_TYPE,
};

use super::constants::{CRED_DEF, GET_CRED_DEF};
use super::did::ShortDidValue;
use super::{get_sp_key_marker, ProtocolVersion, RequestType};
use crate::common::error::prelude::*;

#[derive(Serialize, Debug)]
pub struct CredDefOperation {
    #[serde(rename = "ref")]
    pub _ref: i32,
    pub data: CredentialDefinitionData,
    #[serde(rename = "type")]
    pub _type: String,
    pub signature_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl CredDefOperation {
    pub fn new(data: CredentialDefinitionV1) -> CredDefOperation {
        CredDefOperation {
            _ref: data.schema_id.0.parse::<i32>().unwrap_or(0),
            signature_type: data.signature_type.to_str().to_string(),
            data: data.value,
            tag: if data.tag.is_empty() {
                None
            } else {
                Some(data.tag.clone())
            },
            _type: Self::get_txn_type().to_string(),
        }
    }
}

impl RequestType for CredDefOperation {
    fn get_txn_type<'a>() -> &'a str {
        CRED_DEF
    }
}

#[derive(Serialize, PartialEq, Debug)]
pub struct GetCredDefOperation {
    #[serde(rename = "type")]
    pub _type: String,
    #[serde(rename = "ref")]
    pub _ref: i32,
    pub signature_type: String,
    pub origin: ShortDidValue,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl GetCredDefOperation {
    pub fn new(
        _ref: i32,
        signature_type: String,
        origin: ShortDidValue,
        tag: Option<String>,
    ) -> GetCredDefOperation {
        GetCredDefOperation {
            _type: Self::get_txn_type().to_string(),
            _ref,
            signature_type,
            origin,
            tag,
        }
    }
}

impl RequestType for GetCredDefOperation {
    fn get_txn_type<'a>() -> &'a str {
        GET_CRED_DEF
    }

    fn get_sp_key(&self, protocol_version: ProtocolVersion) -> VdrResult<Option<Vec<u8>>> {
        let marker = get_sp_key_marker(3, protocol_version);
        let tag = if protocol_version == ProtocolVersion::Node1_3 {
            None
        } else {
            self.tag.clone()
        };
        let tag = tag
            .map(|t| format!(":{}", t))
            .unwrap_or_else(|| "".to_owned());
        Ok(Some(
            format!(
                "{}:{}:{}:{}{}",
                &*self.origin, marker, self.signature_type, self._ref, tag
            )
            .as_bytes()
            .to_vec(),
        ))
    }
}