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