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