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
use sha2::{Digest, Sha256};

use super::constants::{ATTRIB, GET_ATTR};
use super::did::ShortDidValue;
use super::{get_sp_key_marker, ProtocolVersion, RequestType};
use crate::common::error::VdrResult;

#[derive(Serialize, PartialEq, Debug)]
pub struct AttribOperation {
    #[serde(rename = "type")]
    pub _type: String,
    pub dest: ShortDidValue,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hash: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub raw: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enc: Option<String>,
}

impl AttribOperation {
    pub fn new(
        dest: ShortDidValue,
        hash: Option<String>,
        raw: Option<String>,
        enc: Option<String>,
    ) -> AttribOperation {
        AttribOperation {
            _type: Self::get_txn_type().to_string(),
            dest,
            hash,
            raw,
            enc,
        }
    }
}

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

#[derive(Serialize, PartialEq, Debug)]
pub struct GetAttribOperation {
    #[serde(rename = "type")]
    pub _type: String,
    pub dest: ShortDidValue,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub raw: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hash: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enc: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seq_no: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<u64>,
}

impl GetAttribOperation {
    pub fn new(
        dest: ShortDidValue,
        raw: Option<String>,
        hash: Option<String>,
        enc: Option<String>,
        seq_no: Option<i32>,
        timestamp: Option<u64>,
    ) -> GetAttribOperation {
        GetAttribOperation {
            _type: Self::get_txn_type().to_string(),
            dest,
            raw,
            hash,
            enc,
            seq_no,
            timestamp,
        }
    }
}

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

    fn get_sp_key(&self, protocol_version: ProtocolVersion) -> VdrResult<Option<Vec<u8>>> {
        if let Some(attr_name) = self
            .raw
            .as_ref()
            .or(self.enc.as_ref())
            .or(self.hash.as_ref())
        {
            let marker = get_sp_key_marker(1, protocol_version);
            let hash = Sha256::digest(attr_name.as_bytes());
            return Ok(Some(
                format!("{}:{}:{}", &*self.dest, marker, hex::encode(hash))
                    .as_bytes()
                    .to_vec(),
            ));
        }
        Ok(None)
    }
}