Skip to main content

ethrex_rpc/clients/beacon/
types.rs

1use ethrex_common::{H256, U256};
2use serde::Deserialize;
3use sha2::Digest;
4
5/// `data` structure of `/eth/v2/beacon/blocks/{block_id}` endpoint's response
6#[derive(Deserialize, Debug)]
7pub struct GetBlockResponseData {
8    pub message: GetBlockResponseMessage,
9    // 96 bytes hex string
10    #[serde(rename = "signature", with = "ethrex_common::serde_utils::bytes")]
11    _signature: bytes::Bytes,
12}
13
14/// `data.message` structure of `/eth/v2/beacon/blocks/{block_id}` endpoint's response
15// Actual response has many more fields, but we only care about `slot` for now
16#[derive(Deserialize, Debug)]
17pub struct GetBlockResponseMessage {
18    #[serde(with = "ethrex_common::serde_utils::u256::dec_str")]
19    pub slot: U256,
20}
21
22/// Each element of `data` array of `/eth/v1/beacon/blob_sidecars/{block_id}` endpoint's response
23// Actual response has many more fields, but we only care about these for now
24#[derive(Deserialize, Debug)]
25pub struct BlobSidecar {
26    #[serde(deserialize_with = "ethrex_common::serde_utils::u64::deser_dec_str")]
27    pub index: u64,
28    #[serde(with = "ethrex_common::serde_utils::bytes")]
29    pub blob: bytes::Bytes,
30    #[serde(with = "ethrex_common::serde_utils::bytes")]
31    pub kzg_commitment: bytes::Bytes,
32}
33
34impl BlobSidecar {
35    pub fn versioned_hash(&self) -> H256 {
36        let mut hasher = sha2::Sha256::new();
37        hasher.update(&self.kzg_commitment);
38
39        let hash = &mut hasher.finalize();
40        hash[0] = 0x01;
41
42        H256::from_slice(hash)
43    }
44}