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 #[cfg(feature = "future_snark")]
34 use mithril_stm::VerificationKeyForSnark;
35 use mithril_stm::{
36 AggregateSignature, AggregateVerificationKeyForConcatenation, Initializer,
37 MithrilMembershipDigest, Parameters, SingleSignature, SingleSignatureWithRegisteredParty,
38 VerificationKeyForConcatenation, VerificationKeyProofOfPossessionForConcatenation,
39 };
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 Ok(self.to_bytes().to_vec())
48 }
49 }
50
51 impl TryFromBytes for Parameters {
52 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
53 Self::from_bytes(bytes).map_err(|e| e.into())
54 }
55 }
56
57 impl TryToBytes for SingleSignature {
58 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
59 Ok(self.to_bytes().to_vec())
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 Ok(self.to_bytes().to_vec())
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 Ok(self.to_bytes().to_vec())
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 Ok(self.to_bytes().to_vec())
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 impl TryToBytes for Initializer {
145 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
146 Ok(self.to_bytes().to_vec())
147 }
148 }
149
150 impl TryFromBytes for Initializer {
151 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
152 Self::from_bytes(bytes)
153 }
154 }
155}
156
157mod binary_ed25519 {
158 use ed25519_dalek::{Signature, SigningKey, VerifyingKey};
159
160 use super::*;
161
162 impl TryToBytes for Signature {
163 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
164 Ok(self.to_bytes().to_vec())
165 }
166 }
167
168 impl TryFromBytes for Signature {
169 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
170 Self::try_from(bytes).map_err(|e| e.into())
171 }
172 }
173
174 impl TryToBytes for SigningKey {
175 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
176 Ok(self.to_bytes().to_vec())
177 }
178 }
179
180 impl TryFromBytes for SigningKey {
181 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
182 Self::try_from(bytes).map_err(|e| e.into())
183 }
184 }
185
186 impl TryToBytes for VerifyingKey {
187 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
188 Ok(self.to_bytes().to_vec())
189 }
190 }
191
192 impl TryFromBytes for VerifyingKey {
193 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
194 Self::try_from(bytes).map_err(|e| e.into())
195 }
196 }
197}
198
199mod binary_kes_sig {
200 use anyhow::anyhow;
201 use kes_summed_ed25519::kes::Sum6KesSig;
202
203 use super::*;
204
205 impl TryToBytes for Sum6KesSig {
206 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
207 Ok(self.to_bytes().to_vec())
208 }
209 }
210
211 impl TryFromBytes for Sum6KesSig {
212 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
213 Self::from_bytes(bytes).map_err(|e| {
214 anyhow!("{e:?}").context("Could not deserialize KES signature from bytes")
215 })
216 }
217 }
218}
219
220mod binary_opcert {
221 use crate::crypto_helper::{OpCert, OpCertWithoutColdVerificationKey, SerDeShelleyFileFormat};
222
223 use super::*;
224
225 impl TryToBytes for OpCert {
226 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
227 self.to_cbor_bytes()
228 .with_context(|| "Could not serialize OpCert to bytes")
229 }
230 }
231
232 impl TryFromBytes for OpCert {
233 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
234 Self::from_cbor_bytes(bytes).with_context(|| "Could not deserialize OpCert from bytes")
235 }
236 }
237
238 impl TryToBytes for OpCertWithoutColdVerificationKey {
239 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
240 self.to_cbor_bytes()
241 .with_context(|| "Could not serialize OpCertWithoutColdVerificationKey to bytes")
242 }
243 }
244
245 impl TryFromBytes for OpCertWithoutColdVerificationKey {
246 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
247 Self::from_cbor_bytes(bytes).with_context(
248 || "Could not deserialize OpCertWithoutColdVerificationKey from bytes",
249 )
250 }
251 }
252}
253
254mod binary_mk_proof {
255 use serde::{Deserialize, Serialize};
256
257 use crate::crypto_helper::{MKMapKey, MKMapProof, MKProof};
258
259 use super::*;
260
261 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryToBytes for MKMapProof<T> {
262 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
263 self.to_bytes()
264 }
265 }
266
267 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryFromBytes for MKMapProof<T> {
268 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
269 Self::from_bytes(bytes)
270 }
271 }
272
273 impl TryToBytes for MKProof {
274 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
275 self.to_bytes()
276 }
277 }
278
279 impl TryFromBytes for MKProof {
280 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
281 Self::from_bytes(bytes)
282 }
283 }
284}