mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-03 17:54:28 +00:00
48 byte array serde
This commit is contained in:
parent
82eef493f3
commit
9f155eec7a
@ -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(ð2_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()
|
||||
|
Loading…
Reference in New Issue
Block a user