use alloy::{
dyn_abi::SolType,
primitives::{Bytes, ChainId},
sol,
};
use bincode::error;
use serde_json::Value;
pub mod chain;
#[cfg(feature = "state-tree")]
pub mod confidential_key;
#[cfg(feature = "state-tree")]
pub mod identity_key;
#[cfg(feature = "state-tree")]
pub mod secrets_key;
sol! {
type EvaluationResult is bool;
}
pub fn hex_encode_bool(b: bool) -> Bytes {
EvaluationResult::from(b).abi_encode().into()
}
pub fn hex_decode_bool(b: Bytes) -> bool {
EvaluationResult::abi_decode(&b).unwrap()
}
pub fn write_serialized(buffer: &mut Vec<u8>, data: &[u8]) -> Result<(), error::EncodeError> {
let mut input: Vec<u8> = Vec::new();
bincode::encode_into_slice(data, &mut input, bincode::config::standard())?;
buffer.extend_from_slice(&input);
Ok(())
}
pub fn json_recursive_hex(value: Value) -> Value {
match value {
Value::Array(arr) => {
let is_byte_array = arr
.iter()
.all(|v| if let Some(n) = v.as_u64() { n <= 255 } else { false });
if is_byte_array && !arr.is_empty() {
let bytes: Vec<u8> = arr.iter().map(|v| v.as_u64().unwrap() as u8).collect();
Value::String(format!("0x{}", hex::encode(bytes)))
} else {
Value::Array(arr.into_iter().map(json_recursive_hex).collect())
}
}
Value::Object(map) => Value::Object(map.into_iter().map(|(k, v)| (k, json_recursive_hex(v))).collect()),
other => other,
}
}
pub mod bytes_serde {
use alloy::primitives::Bytes;
use serde::{Deserialize, Deserializer, Serializer};
use std::str::FromStr;
pub fn deserialize<'de, D>(deserializer: D) -> Result<Bytes, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let padded = if s.len() % 2 != 0 { format!("0{}", s) } else { s };
Bytes::from_str(&padded).map_err(serde::de::Error::custom)
}
pub fn serialize<S>(bytes: &Bytes, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&crate::hex!(bytes))
}
}