mithril_common/crypto_helper/codec/
binary.rs1use hex::{FromHex, ToHex};
2
3use crate::StdResult;
4
5pub trait TryToBytes {
7 fn to_bytes_vec(&self) -> StdResult<Vec<u8>>;
9
10 fn to_bytes_hex(&self) -> StdResult<String> {
12 Ok(self.to_bytes_vec()?.encode_hex::<String>())
13 }
14}
15
16pub trait TryFromBytes: Sized {
18 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self>;
20
21 fn try_from_bytes_hex(hex_string: &str) -> StdResult<Self> {
23 let bytes = Vec::from_hex(hex_string).map_err(|e| {
24 anyhow::anyhow!(
25 "Could not deserialize binary from hex string: {}",
26 e.to_string()
27 )
28 })?;
29
30 Self::try_from_bytes(&bytes)
31 }
32}
33
34mod binary_mithril_stm {
35 use anyhow::anyhow;
36 use blake2::Blake2b;
37
38 use digest::consts::U32;
39 use mithril_stm::{
40 StmAggrSig, StmAggrVerificationKey, StmInitializer, StmParameters, StmSig, StmSigRegParty,
41 StmVerificationKey, StmVerificationKeyPoP,
42 };
43
44 use super::*;
45
46 type D = Blake2b<U32>;
47
48 impl TryToBytes for StmParameters {
49 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
50 Ok(self.to_bytes().to_vec())
51 }
52 }
53
54 impl TryFromBytes for StmParameters {
55 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
56 Self::from_bytes(bytes).map_err(|e| e.into())
57 }
58 }
59
60 impl TryToBytes for StmSig {
61 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
62 Ok(self.to_bytes().to_vec())
63 }
64 }
65
66 impl TryFromBytes for StmSig {
67 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
68 Self::from_bytes::<D>(bytes).map_err(|e| e.into())
69 }
70 }
71
72 impl TryToBytes for StmSigRegParty {
73 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
74 Ok(self.to_bytes().to_vec())
75 }
76 }
77
78 impl TryFromBytes for StmSigRegParty {
79 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
80 Self::from_bytes::<D>(bytes).map_err(|e| e.into())
81 }
82 }
83
84 impl TryToBytes for StmAggrSig<D> {
85 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
86 Ok(self.to_bytes().to_vec())
87 }
88 }
89
90 impl TryFromBytes for StmAggrSig<D> {
91 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
92 Self::from_bytes(bytes).map_err(|e| anyhow!("{e}"))
93 }
94 }
95
96 impl TryToBytes for StmVerificationKey {
97 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
98 Ok(self.to_bytes().to_vec())
99 }
100 }
101
102 impl TryFromBytes for StmVerificationKey {
103 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
104 Self::from_bytes(bytes).map_err(|e| e.into())
105 }
106 }
107
108 impl TryToBytes for StmVerificationKeyPoP {
109 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
110 Ok(self.to_bytes().to_vec())
111 }
112 }
113
114 impl TryFromBytes for StmVerificationKeyPoP {
115 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
116 Self::from_bytes(bytes).map_err(|e| e.into())
117 }
118 }
119
120 impl TryToBytes for StmAggrVerificationKey<D> {
121 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
122 bincode::serde::encode_to_vec(self, bincode::config::standard()).map_err(|e| e.into())
123 }
124 }
125
126 impl TryFromBytes for StmAggrVerificationKey<D> {
127 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
128 let (res, _) = bincode::serde::decode_from_slice::<StmAggrVerificationKey<D>, _>(
129 bytes,
130 bincode::config::standard(),
131 )
132 .map_err(|e| anyhow!(e))?;
133
134 Ok(res)
135 }
136 }
137
138 impl TryToBytes for StmInitializer {
139 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
140 Ok(self.to_bytes().to_vec())
141 }
142 }
143
144 impl TryFromBytes for StmInitializer {
145 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
146 Self::from_bytes(bytes).map_err(|e| e.into())
147 }
148 }
149}
150
151mod binary_ed25519 {
152 use ed25519_dalek::{Signature, SigningKey, VerifyingKey};
153
154 use super::*;
155
156 impl TryToBytes for Signature {
157 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
158 Ok(self.to_bytes().to_vec())
159 }
160 }
161
162 impl TryFromBytes for Signature {
163 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
164 Self::try_from(bytes).map_err(|e| e.into())
165 }
166 }
167
168 impl TryToBytes for SigningKey {
169 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
170 Ok(self.to_bytes().to_vec())
171 }
172 }
173
174 impl TryFromBytes for SigningKey {
175 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
176 Self::try_from(bytes).map_err(|e| e.into())
177 }
178 }
179
180 impl TryToBytes for VerifyingKey {
181 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
182 Ok(self.to_bytes().to_vec())
183 }
184 }
185
186 impl TryFromBytes for VerifyingKey {
187 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
188 Self::try_from(bytes).map_err(|e| e.into())
189 }
190 }
191}
192
193mod binary_kes_sig {
194 use anyhow::anyhow;
195 use kes_summed_ed25519::kes::Sum6KesSig;
196
197 use super::*;
198
199 impl TryToBytes for Sum6KesSig {
200 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
201 Ok(self.to_bytes().to_vec())
202 }
203 }
204
205 impl TryFromBytes for Sum6KesSig {
206 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
207 Self::from_bytes(bytes).map_err(|e| anyhow!(format!("{e:?}")))
208 }
209 }
210}
211
212mod binary_opcert {
213 use crate::crypto_helper::{OpCert, SerDeShelleyFileFormat};
214 use anyhow::anyhow;
215
216 use super::*;
217
218 impl TryToBytes for OpCert {
219 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
220 self.to_cbor_bytes().map_err(|e| anyhow!(format!("{e:?}")))
221 }
222 }
223
224 impl TryFromBytes for OpCert {
225 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
226 Self::from_cbor_bytes(bytes).map_err(|e| anyhow!(format!("{e:?}")))
227 }
228 }
229}
230
231mod binary_mk_proof {
232 use serde::{Deserialize, Serialize};
233
234 use crate::crypto_helper::{MKMapKey, MKMapProof, MKProof};
235
236 use super::*;
237
238 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryToBytes for MKMapProof<T> {
239 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
240 self.to_bytes()
241 }
242 }
243
244 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryFromBytes for MKMapProof<T> {
245 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
246 Self::from_bytes(bytes)
247 }
248 }
249
250 impl TryToBytes for MKProof {
251 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
252 self.to_bytes()
253 }
254 }
255
256 impl TryFromBytes for MKProof {
257 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
258 Self::from_bytes(bytes)
259 }
260 }
261}