mithril_common/crypto_helper/codec/
binary.rs1use anyhow::Context;
2use hex::{FromHex, ToHex};
3
4use crate::StdResult;
5
6pub trait TryToBytes {
8 fn to_bytes_vec(&self) -> StdResult<Vec<u8>>;
10
11 fn to_bytes_hex(&self) -> StdResult<String> {
13 Ok(self.to_bytes_vec()?.encode_hex::<String>())
14 }
15}
16
17pub trait TryFromBytes: Sized {
19 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self>;
21
22 fn try_from_bytes_hex(hex_string: &str) -> StdResult<Self> {
24 let bytes = Vec::from_hex(hex_string)
25 .with_context(|| "Could not deserialize binary from hex string")?;
26
27 Self::try_from_bytes(&bytes)
28 }
29}
30
31mod binary_mithril_stm {
32
33 use mithril_stm::{
34 AggregateSignature, AggregateVerificationKeyForConcatenation, Initializer,
35 MithrilMembershipDigest, Parameters, SingleSignature, SingleSignatureWithRegisteredParty,
36 VerificationKeyForConcatenation, VerificationKeyProofOfPossessionForConcatenation,
37 };
38 #[cfg(feature = "future_snark")]
39 use mithril_stm::{AggregateVerificationKeyForSnark, VerificationKeyForSnark};
40
41 use super::*;
42
43 type D = MithrilMembershipDigest;
44
45 impl TryToBytes for Parameters {
46 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
47 self.to_bytes()
48 }
49 }
50
51 impl TryFromBytes for Parameters {
52 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
53 Self::from_bytes(bytes)
54 }
55 }
56
57 impl TryToBytes for SingleSignature {
58 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
59 self.to_bytes()
60 }
61 }
62
63 impl TryFromBytes for SingleSignature {
64 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
65 Self::from_bytes::<D>(bytes)
66 }
67 }
68
69 impl TryToBytes for SingleSignatureWithRegisteredParty {
70 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
71 self.to_bytes()
72 }
73 }
74
75 impl TryFromBytes for SingleSignatureWithRegisteredParty {
76 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
77 Self::from_bytes::<D>(bytes)
78 }
79 }
80
81 impl TryToBytes for AggregateSignature<D> {
82 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
83 self.to_bytes()
84 }
85 }
86
87 impl TryFromBytes for AggregateSignature<D> {
88 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
89 Self::from_bytes(bytes)
90 .with_context(|| "Could not deserialize aggregate signature from bytes")
91 }
92 }
93
94 impl TryToBytes for VerificationKeyForConcatenation {
95 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
96 Ok(self.to_bytes().to_vec())
97 }
98 }
99
100 impl TryFromBytes for VerificationKeyForConcatenation {
101 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
102 Self::from_bytes(bytes)
103 }
104 }
105
106 impl TryToBytes for VerificationKeyProofOfPossessionForConcatenation {
107 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
108 Ok(self.to_bytes().to_vec())
109 }
110 }
111
112 impl TryFromBytes for VerificationKeyProofOfPossessionForConcatenation {
113 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
114 Self::from_bytes(bytes)
115 }
116 }
117
118 #[cfg(feature = "future_snark")]
119 impl TryToBytes for VerificationKeyForSnark {
120 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
121 Ok(self.to_bytes().to_vec())
122 }
123 }
124
125 #[cfg(feature = "future_snark")]
126 impl TryFromBytes for VerificationKeyForSnark {
127 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
128 Self::from_bytes(bytes)
129 }
130 }
131
132 impl TryToBytes for AggregateVerificationKeyForConcatenation<D> {
133 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
134 self.to_bytes()
135 }
136 }
137
138 impl TryFromBytes for AggregateVerificationKeyForConcatenation<D> {
139 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
140 Self::from_bytes(bytes)
141 }
142 }
143
144 #[cfg(feature = "future_snark")]
145 impl TryToBytes for AggregateVerificationKeyForSnark<D> {
146 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
147 self.to_bytes()
148 }
149 }
150
151 #[cfg(feature = "future_snark")]
152 impl TryFromBytes for AggregateVerificationKeyForSnark<D> {
153 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
154 Self::from_bytes(bytes)
155 }
156 }
157
158 impl TryToBytes for Initializer {
159 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
160 self.to_bytes()
161 }
162 }
163
164 impl TryFromBytes for Initializer {
165 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
166 Self::from_bytes(bytes)
167 }
168 }
169}
170
171mod binary_ed25519 {
172 use ed25519_dalek::{Signature, SigningKey, VerifyingKey};
173
174 use super::*;
175
176 impl TryToBytes for Signature {
177 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
178 Ok(self.to_bytes().to_vec())
179 }
180 }
181
182 impl TryFromBytes for Signature {
183 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
184 Self::try_from(bytes).map_err(|e| e.into())
185 }
186 }
187
188 impl TryToBytes for SigningKey {
189 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
190 Ok(self.to_bytes().to_vec())
191 }
192 }
193
194 impl TryFromBytes for SigningKey {
195 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
196 Self::try_from(bytes).map_err(|e| e.into())
197 }
198 }
199
200 impl TryToBytes for VerifyingKey {
201 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
202 Ok(self.to_bytes().to_vec())
203 }
204 }
205
206 impl TryFromBytes for VerifyingKey {
207 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
208 Self::try_from(bytes).map_err(|e| e.into())
209 }
210 }
211}
212
213mod binary_kes_sig {
214 use anyhow::anyhow;
215 use kes_summed_ed25519::kes::Sum6KesSig;
216
217 use super::*;
218
219 impl TryToBytes for Sum6KesSig {
220 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
221 Ok(self.to_bytes().to_vec())
222 }
223 }
224
225 impl TryFromBytes for Sum6KesSig {
226 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
227 Self::from_bytes(bytes).map_err(|e| {
228 anyhow!("{e:?}").context("Could not deserialize KES signature from bytes")
229 })
230 }
231 }
232}
233
234mod binary_opcert {
235 use crate::crypto_helper::{OpCert, OpCertWithoutColdVerificationKey, SerDeShelleyFileFormat};
236
237 use super::*;
238
239 impl TryToBytes for OpCert {
240 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
241 self.to_cbor_bytes()
242 .with_context(|| "Could not serialize OpCert to bytes")
243 }
244 }
245
246 impl TryFromBytes for OpCert {
247 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
248 Self::from_cbor_bytes(bytes).with_context(|| "Could not deserialize OpCert from bytes")
249 }
250 }
251
252 impl TryToBytes for OpCertWithoutColdVerificationKey {
253 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
254 self.to_cbor_bytes()
255 .with_context(|| "Could not serialize OpCertWithoutColdVerificationKey to bytes")
256 }
257 }
258
259 impl TryFromBytes for OpCertWithoutColdVerificationKey {
260 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
261 Self::from_cbor_bytes(bytes).with_context(
262 || "Could not deserialize OpCertWithoutColdVerificationKey from bytes",
263 )
264 }
265 }
266}
267
268mod binary_mk_proof {
269 use serde::{Deserialize, Serialize};
270
271 use crate::crypto_helper::{MKMapKey, MKMapProof, MKProof};
272
273 use super::*;
274
275 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryToBytes for MKMapProof<T> {
276 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
277 self.to_bytes()
278 }
279 }
280
281 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryFromBytes for MKMapProof<T> {
282 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
283 Self::from_bytes(bytes)
284 }
285 }
286
287 impl TryToBytes for MKProof {
288 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
289 self.to_bytes()
290 }
291 }
292
293 impl TryFromBytes for MKProof {
294 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
295 Self::from_bytes(bytes)
296 }
297 }
298}