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 use anyhow::anyhow;
33 use mithril_stm::{
34 AggregateSignature, AggregateVerificationKey, Initializer, MithrilMembershipDigest,
35 Parameters, SingleSignature, SingleSignatureWithRegisteredParty, VerificationKey,
36 VerificationKeyProofOfPossession,
37 };
38
39 use super::*;
40
41 type D = MithrilMembershipDigest;
42
43 impl TryToBytes for Parameters {
44 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
45 Ok(self.to_bytes().to_vec())
46 }
47 }
48
49 impl TryFromBytes for Parameters {
50 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
51 Self::from_bytes(bytes).map_err(|e| e.into())
52 }
53 }
54
55 impl TryToBytes for SingleSignature {
56 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
57 Ok(self.to_bytes().to_vec())
58 }
59 }
60
61 impl TryFromBytes for SingleSignature {
62 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
63 Self::from_bytes::<D>(bytes)
64 }
65 }
66
67 impl TryToBytes for SingleSignatureWithRegisteredParty {
68 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
69 Ok(self.to_bytes().to_vec())
70 }
71 }
72
73 impl TryFromBytes for SingleSignatureWithRegisteredParty {
74 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
75 Self::from_bytes::<D>(bytes)
76 }
77 }
78
79 impl TryToBytes for AggregateSignature<D> {
80 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
81 Ok(self.to_bytes().to_vec())
82 }
83 }
84
85 impl TryFromBytes for AggregateSignature<D> {
86 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
87 Self::from_bytes(bytes)
88 .with_context(|| "Could not deserialize aggregate signature from bytes")
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)
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)
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)
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| {
204 anyhow!("{e:?}").context("Could not deserialize KES signature from bytes")
205 })
206 }
207 }
208}
209
210mod binary_opcert {
211 use crate::crypto_helper::{OpCert, OpCertWithoutColdVerificationKey, SerDeShelleyFileFormat};
212
213 use super::*;
214
215 impl TryToBytes for OpCert {
216 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
217 self.to_cbor_bytes()
218 .with_context(|| "Could not serialize OpCert to bytes")
219 }
220 }
221
222 impl TryFromBytes for OpCert {
223 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
224 Self::from_cbor_bytes(bytes).with_context(|| "Could not deserialize OpCert from bytes")
225 }
226 }
227
228 impl TryToBytes for OpCertWithoutColdVerificationKey {
229 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
230 self.to_cbor_bytes()
231 .with_context(|| "Could not serialize OpCertWithoutColdVerificationKey to bytes")
232 }
233 }
234
235 impl TryFromBytes for OpCertWithoutColdVerificationKey {
236 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
237 Self::from_cbor_bytes(bytes).with_context(
238 || "Could not deserialize OpCertWithoutColdVerificationKey from bytes",
239 )
240 }
241 }
242}
243
244mod binary_mk_proof {
245 use serde::{Deserialize, Serialize};
246
247 use crate::crypto_helper::{MKMapKey, MKMapProof, MKProof};
248
249 use super::*;
250
251 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryToBytes for MKMapProof<T> {
252 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
253 self.to_bytes()
254 }
255 }
256
257 impl<T: MKMapKey + Serialize + for<'de> Deserialize<'de>> TryFromBytes for MKMapProof<T> {
258 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
259 Self::from_bytes(bytes)
260 }
261 }
262
263 impl TryToBytes for MKProof {
264 fn to_bytes_vec(&self) -> StdResult<Vec<u8>> {
265 self.to_bytes()
266 }
267 }
268
269 impl TryFromBytes for MKProof {
270 fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
271 Self::from_bytes(bytes)
272 }
273 }
274}