48 byte array serde

This commit is contained in:
realbigsean 2022-10-28 10:25:48 -04:00
parent 82eef493f3
commit 9f155eec7a
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC

View File

@ -1,8 +1,9 @@
use crate::test_utils::{RngCore, TestRandom};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use std::fmt;
use tree_hash::{PackedEncoding, TreeHash};
use serde_big_array::BigArray;
const KZG_PROOF_BYTES_LEN: usize = 48;
@ -34,40 +35,6 @@ impl Into<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
}
}
pub mod serde_kzg_proof {
use super::*;
use serde::de::Error;
pub fn serialize<S>(bytes: &[u8; KZG_PROOF_BYTES_LEN], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&eth2_serde_utils::hex::encode(bytes))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; KZG_PROOF_BYTES_LEN], D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
let bytes = eth2_serde_utils::hex::decode(&s).map_err(D::Error::custom)?;
if bytes.len() != KZG_PROOF_BYTES_LEN {
return Err(D::Error::custom(format!(
"incorrect byte length {}, expected {}",
bytes.len(),
KZG_PROOF_BYTES_LEN
)));
}
let mut array = [0; KZG_PROOF_BYTES_LEN];
array[..].copy_from_slice(&bytes);
Ok(array)
}
}
impl Encode for KzgProof {
fn is_ssz_fixed_len() -> bool {
<[u8; KZG_PROOF_BYTES_LEN] as Encode>::is_ssz_fixed_len()