Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dyn-abi): cfg CustomStruct for eip712, rm CustomValue #178

Merged
merged 5 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/dyn-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ hex.workspace = true
itoa.workspace = true

# eip712
derive_more = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }

Expand All @@ -36,7 +37,7 @@ ethabi = "18"
[features]
default = ["std"]
std = ["alloy-sol-types/std", "alloy-primitives/std", "hex/std", "serde?/std", "serde_json?/std"]
eip712 = ["alloy-sol-types/eip712-serde", "dep:serde", "dep:serde_json"]
eip712 = ["alloy-sol-types/eip712-serde", "dep:derive_more", "dep:serde", "dep:serde_json"]

[[bench]]
name = "abi"
Expand Down
102 changes: 35 additions & 67 deletions crates/dyn-abi/src/eip712/coerce.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
use crate::{DynAbiError, DynSolType, DynSolValue, Word};
use crate::{DynAbiError, DynAbiResult, DynSolType, DynSolValue, Word};
use alloc::{
borrow::ToOwned,
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use alloy_primitives::{Address, I256, U256};

impl DynSolType {
/// Coerce a json value to a sol value via this type.
pub fn coerce(&self, value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
/// Coerce a [`serde_json::Value`] to a [`DynSolValue`] via this type.
pub fn coerce(&self, value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
match self {
DynSolType::Address => address(value),
DynSolType::Bytes => bytes(value),
DynSolType::Bool => bool(value),
DynSolType::Int(n) => int(*n, value),
DynSolType::Uint(n) => uint(*n, value),
DynSolType::Bool => bool(value),
DynSolType::Array(inner) => array(inner, value),
DynSolType::String => string(value),
DynSolType::FixedBytes(n) => fixed_bytes(*n, value),
DynSolType::String => string(value),
DynSolType::Bytes => bytes(value),
DynSolType::Array(inner) => array(inner, value),
DynSolType::FixedArray(inner, n) => fixed_array(inner, *n, value),
DynSolType::Tuple(inner) => tuple(inner, value),
DynSolType::CustomStruct {
name,
prop_names,
tuple,
} => coerce_custom_struct(name, prop_names, tuple, value),
DynSolType::CustomValue { name } => coerce_custom_value(name, value),
}
}
}

/// Coerce a `serde_json::Value` to a `DynSolValue::Address`
pub(crate) fn address(value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
fn address(value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
let address = value
.as_str()
.map(|s| {
Expand All @@ -44,7 +41,7 @@ pub(crate) fn address(value: &serde_json::Value) -> Result<DynSolValue, DynAbiEr
Ok(DynSolValue::Address(address))
}

pub(crate) fn bool(value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
fn bool(value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
if let Some(bool) = value.as_bool() {
return Ok(DynSolValue::Bool(bool))
}
Expand All @@ -59,26 +56,7 @@ pub(crate) fn bool(value: &serde_json::Value) -> Result<DynSolValue, DynAbiError
Ok(DynSolValue::Bool(bool))
}

pub(crate) fn bytes(value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
let bytes = value
.as_str()
.map(|s| hex::decode(s).map_err(|_| DynAbiError::type_mismatch(DynSolType::Bytes, value)))
.ok_or_else(|| DynAbiError::type_mismatch(DynSolType::Bytes, value))??;
Ok(DynSolValue::Bytes(bytes))
}

pub(crate) fn fixed_bytes(n: usize, value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
if let Some(Ok(buf)) = value.as_str().map(hex::decode) {
let mut word: Word = Default::default();
let min = n.min(buf.len());
word[..min].copy_from_slice(&buf[..min]);
return Ok(DynSolValue::FixedBytes(word, n))
}

Err(DynAbiError::type_mismatch(DynSolType::FixedBytes(n), value))
}

pub(crate) fn int(n: usize, value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
fn int(n: usize, value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
if let Some(num) = value.as_i64() {
return Ok(DynSolValue::Int(I256::try_from(num).unwrap(), n))
}
Expand All @@ -90,7 +68,7 @@ pub(crate) fn int(n: usize, value: &serde_json::Value) -> Result<DynSolValue, Dy
Err(DynAbiError::type_mismatch(DynSolType::Int(n), value))
}

pub(crate) fn uint(n: usize, value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
fn uint(n: usize, value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
if let Some(num) = value.as_u64() {
return Ok(DynSolValue::Uint(U256::from(num), n))
}
Expand All @@ -108,18 +86,34 @@ pub(crate) fn uint(n: usize, value: &serde_json::Value) -> Result<DynSolValue, D
Err(DynAbiError::type_mismatch(DynSolType::Uint(n), value))
}

pub(crate) fn string(value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
fn fixed_bytes(n: usize, value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
if let Some(Ok(buf)) = value.as_str().map(hex::decode) {
let mut word: Word = Default::default();
let min = n.min(buf.len());
word[..min].copy_from_slice(&buf[..min]);
return Ok(DynSolValue::FixedBytes(word, n))
}

Err(DynAbiError::type_mismatch(DynSolType::FixedBytes(n), value))
}

fn string(value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
let string = value
.as_str()
.map(|s| s.to_string())
.ok_or_else(|| DynAbiError::type_mismatch(DynSolType::String, value))?;
Ok(DynSolValue::String(string))
}

pub(crate) fn tuple(
inner: &[DynSolType],
value: &serde_json::Value,
) -> Result<DynSolValue, DynAbiError> {
fn bytes(value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
let bytes = value
.as_str()
.map(|s| hex::decode(s).map_err(|_| DynAbiError::type_mismatch(DynSolType::Bytes, value)))
.ok_or_else(|| DynAbiError::type_mismatch(DynSolType::Bytes, value))??;
Ok(DynSolValue::Bytes(bytes))
}

fn tuple(inner: &[DynSolType], value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
if let Some(arr) = value.as_array() {
if inner.len() != arr.len() {
return Err(DynAbiError::type_mismatch(
Expand All @@ -143,10 +137,7 @@ pub(crate) fn tuple(
))
}

pub(crate) fn array(
inner: &DynSolType,
value: &serde_json::Value,
) -> Result<DynSolValue, DynAbiError> {
fn array(inner: &DynSolType, value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
if let Some(arr) = value.as_array() {
let array = arr
.iter()
Expand All @@ -162,11 +153,11 @@ pub(crate) fn array(
))
}

pub(crate) fn fixed_array(
fn fixed_array(
inner: &DynSolType,
n: usize,
value: &serde_json::Value,
) -> Result<DynSolValue, DynAbiError> {
) -> DynAbiResult<DynSolValue> {
if let Some(arr) = value.as_array() {
if arr.len() != n {
return Err(DynAbiError::type_mismatch(
Expand Down Expand Up @@ -228,29 +219,6 @@ pub(crate) fn coerce_custom_struct(
))
}

pub(crate) fn coerce_custom_value(
name: &str,
value: &serde_json::Value,
) -> Result<DynSolValue, DynAbiError> {
if let Some(Ok(buf)) = value.as_str().map(hex::decode) {
let mut word: Word = Default::default();
let amnt = if buf.len() > 32 { 32 } else { buf.len() };
word[..amnt].copy_from_slice(&buf[..amnt]);

return Ok(DynSolValue::CustomValue {
name: name.to_string(),
inner: word,
})
}

Err(DynAbiError::type_mismatch(
DynSolType::CustomValue {
name: name.to_owned(),
},
value,
))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/dyn-abi/src/eip712/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ mod typed_data;
pub use typed_data::{Eip712Types, TypedData};

mod resolver;
pub use resolver::{PropertyDef, Resolver};
pub use resolver::{PropertyDef, Resolver, TypeDef};

pub(crate) mod coerce;
1 change: 0 additions & 1 deletion crates/dyn-abi/src/eip712/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ impl<'a> TryFrom<&'a str> for PropDef<'a> {
let (ty, name) = input
.rsplit_once(' ')
.ok_or_else(|| DynAbiError::invalid_property_def(input))?;

Ok(PropDef {
ty: ty.trim().try_into()?,
name: name.trim(),
Expand Down
Loading