Skip to content

Commit

Permalink
Implement deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
rakanalh committed Apr 4, 2024
1 parent b52feee commit 04290e6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion crates/sequencer-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use jsonrpsee::rpc_params;
use reth_primitives::B256;
use serde::Deserialize;
use sov_rollup_interface::rpc::utils::tx_hex;
use sov_rollup_interface::soft_confirmation::SignedSoftConfirmationBatch;

/// Configuration for SequencerClient.
Expand Down Expand Up @@ -76,7 +77,7 @@ pub struct GetSoftBatchResponse {
pub da_slot_hash: [u8; 32],
#[serde(with = "hex::serde")]
pub da_slot_txs_commitment: [u8; 32],
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", with = "tx_hex")]
pub txs: Option<Vec<Vec<u8>>>,
#[serde(with = "hex::serde")]
pub pre_state_root: Vec<u8>,
Expand Down
57 changes: 51 additions & 6 deletions crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ pub struct SoftBatchResponse {
#[serde(with = "hex::serde")]
pub hash: [u8; 32],
/// The transactions in this batch.
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "utils::tx_hex::serialize"
)]
#[serde(skip_serializing_if = "Option::is_none", with = "utils::tx_hex")]
pub txs: Option<Vec<Vec<u8>>>,
/// Pre-state root of the soft batch.
#[serde(with = "hex::serde")]
Expand Down Expand Up @@ -486,9 +483,13 @@ pub mod utils {
/// the actual vector of bytes by returning the hexidecimal representation of those bytes
/// for each item.
pub mod tx_hex {
use hex::ToHex;
use core::fmt;
use core::marker::PhantomData;

use hex::{FromHex, ToHex};
use serde::de::{self, Error, SeqAccess, Visitor};
use serde::ser::SerializeSeq;
use serde::Serializer;
use serde::{Deserialize, Deserializer, Serializer};

use crate::maybestd::format;
use crate::maybestd::string::String;
Expand All @@ -511,6 +512,50 @@ pub mod utils {
}
serializer.serialize_none()
}

/// Deserializes a vector of hex string into raw bytes.
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<Vec<u8>>>, D::Error>
where
D: Deserializer<'de>,
{
struct HexStrVisitor(PhantomData<Option<Vec<Vec<u8>>>>);

impl<'de> Visitor<'de> for HexStrVisitor {
type Value = Option<Vec<Vec<u8>>>;

fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "a hex encoded string")
}

fn visit_str<E>(self, data: &str) -> Result<Self::Value, E>
where
E: Error,
{
let data = data.trim_start_matches("0x");
FromHex::from_hex(data)
.map_err(Error::custom)
.map(|res| Some(vec![res]))
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut result = vec![];
while let Some(item) = seq.next_element::<String>()? {
let item = item.trim_start_matches("0x");
result.push(
FromHex::from_hex(&item)
.map_err(|_| de::Error::custom("Could not convert from hex"))?,
);
}

Ok(Some(result))
}
}

deserializer.deserialize_seq(HexStrVisitor(PhantomData))
}
}
}

Expand Down

0 comments on commit 04290e6

Please sign in to comment.