cyfs_lib/crypto/
output_request_codec.rs

1use super::output_request::*;
2use cyfs_base::*;
3
4use serde_json::{Map, Value};
5
6impl JsonCodec<VerifySigns> for VerifySigns {
7    fn encode_json(&self) -> Map<String, Value> {
8        let mut obj = Map::new();
9
10        if let Some(signs) = &self.body_signs {
11            JsonCodecHelper::encode_number_array_field(&mut obj, "body_signs", &signs);
12        }
13        if let Some(signs) = &self.desc_signs {
14            JsonCodecHelper::encode_number_array_field(&mut obj, "desc_signs", &signs);
15        }
16        obj
17    }
18
19    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
20        Ok(Self {
21            desc_signs: JsonCodecHelper::decode_option_int_array_field(&obj, "desc_signs")?,
22            body_signs: JsonCodecHelper::decode_option_int_array_field(&obj, "body_signs")?,
23        })
24    }
25}
26
27impl JsonCodec<VerifySignResult> for VerifySignResult {
28    fn encode_json(&self) -> Map<String, Value> {
29        let mut obj = Map::new();
30
31        obj.insert("index".to_owned(), Value::String(self.index.to_string()));
32
33        obj.insert("valid".to_owned(), Value::Bool(self.valid));
34
35        obj.insert(
36            "sign_object_id".to_owned(),
37            Value::String(self.sign_object_id.to_string()),
38        );
39
40        obj
41    }
42
43    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
44        let mut index: Option<u8> = None;
45        let mut valid: Option<bool> = None;
46        let mut sign_object_id: Option<ObjectId> = None;
47
48        for (k, v) in obj {
49            match k.as_str() {
50                "index" => {
51                    index = Some(JsonCodecHelper::decode_from_string(v)?);
52                }
53
54                "valid" => {
55                    valid = Some(JsonCodecHelper::decode_from_boolean(v)?);
56                }
57
58                "sign_object_id" => {
59                    sign_object_id = Some(JsonCodecHelper::decode_from_string(v)?);
60                }
61
62                u @ _ => {
63                    warn!("unknown verify result field: {}", u);
64                }
65            }
66        }
67
68        if index.is_none() || valid.is_none() || sign_object_id.is_none() {
69            let msg = format!(
70                "verify result item field missing: index/valid/sign_object_id, value={:?}",
71                obj
72            );
73            error!("{}", msg);
74
75            return Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg));
76        }
77
78        Ok(Self {
79            index: index.unwrap(),
80            valid: valid.unwrap(),
81            sign_object_id: sign_object_id.unwrap(),
82        })
83    }
84}
85
86impl JsonCodec<VerifyObjectResult> for VerifyObjectResult {
87    fn encode_json(&self) -> Map<String, Value> {
88        let mut obj = Map::new();
89
90        obj.insert("valid".to_owned(), Value::Bool(self.valid));
91
92        JsonCodecHelper::encode_as_list(&mut obj, "desc_signs", &self.desc_signs);
93        JsonCodecHelper::encode_as_list(&mut obj, "body_signs", &self.body_signs);
94
95        obj
96    }
97
98    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
99        let mut valid: Option<bool> = None;
100        let mut desc_signs = Vec::new();
101        let mut body_signs = Vec::new();
102
103        for (k, v) in obj {
104            match k.as_str() {
105                "valid" => {
106                    valid = Some(JsonCodecHelper::decode_from_boolean(v)?);
107                }
108
109                "desc_signs" => {
110                    desc_signs = JsonCodecHelper::decode_from_array(v)?;
111                }
112
113                "body_signs" => {
114                    body_signs = JsonCodecHelper::decode_from_array(v)?;
115                }
116
117                u @ _ => {
118                    warn!("unknown verify result field: {}", u);
119                }
120            }
121        }
122
123        if valid.is_none() {
124            let msg = format!("verify result item field missing: valid, value={:?}", obj);
125            error!("{}", msg);
126
127            return Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg));
128        }
129
130        Ok(Self {
131            valid: valid.unwrap(),
132            desc_signs,
133            body_signs,
134        })
135    }
136}
137
138impl JsonCodec<CryptoVerifyObjectOutputResponse> for CryptoVerifyObjectOutputResponse {
139    fn encode_json(&self) -> Map<String, Value> {
140        let mut obj = Map::new();
141
142        JsonCodecHelper::encode_field(&mut obj, "result", &self.result);
143
144        obj
145    }
146
147    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
148        Ok(Self {
149            result: JsonCodecHelper::decode_field(&obj, "result")?,
150        })
151    }
152}
153
154
155// verify_object
156impl JsonCodec<VerifyObjectType> for VerifyObjectType {
157    fn encode_json(&self) -> Map<String, Value> {
158        let mut obj = Map::new();
159
160        JsonCodecHelper::encode_string_field(&mut obj, "type", self);
161
162        match &self {
163            VerifyObjectType::Owner | VerifyObjectType::Own => {}
164            VerifyObjectType::Object(sign_object) => {
165                JsonCodecHelper::encode_field(&mut obj, "sign_object", sign_object);
166            }
167            VerifyObjectType::Sign(verify_signs) => {
168                JsonCodecHelper::encode_field(&mut obj, "verify_signs", verify_signs);
169            }
170        }
171
172        obj
173    }
174
175    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<VerifyObjectType> {
176        let sign_object_type: String = JsonCodecHelper::decode_string_field(obj, "type")?;
177        let ret = match sign_object_type.as_str() {
178            "owner" => VerifyObjectType::Owner,
179            "own" => VerifyObjectType::Own,
180            "object" => {
181                let sign_object = JsonCodecHelper::decode_field(obj, "sign_object")?;
182                VerifyObjectType::Object(sign_object)
183            }
184            "sign" => {
185                let verify_signs = JsonCodecHelper::decode_field(obj, "verify_signs")?;
186                VerifyObjectType::Sign(verify_signs)
187            }
188            _ => {
189                let msg = format!("unknown VerifyObjectType type: {}", sign_object_type);
190                error!("{}", msg);
191                return Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg));
192            }
193        };
194
195        Ok(ret)
196    }
197}