Skip to content

Commit

Permalink
Add custom serialization for Address
Browse files Browse the repository at this point in the history
  • Loading branch information
jenpaff committed Sep 20, 2024
1 parent 9867b55 commit ba8dcb1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
49 changes: 49 additions & 0 deletions crates/dyn-abi/src/eip712/typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,55 @@ mod tests {
);
}

#[test]
fn test_full_domain_contract_format() {
let json = json!({
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
},
{
"name": "salt",
"type": "bytes32"
}
]
},
"primaryType": "EIP712Domain",
"domain": {
"name": "example.metamask.io",
"version": "1",
"chainId": 1,
"verifyingContract": "0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359"
},
"message": {}
});

let typed_data: TypedData = serde_json::from_value(json).unwrap();

let serialized_contract = typed_data.domain.verifying_contract.unwrap();
assert_eq!(serialized_contract.to_string(), "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359");

let hash = typed_data.eip712_signing_hash().unwrap();
assert_eq!(
hex::encode(&hash[..]),
"4863a6e9735dee205f3010f78d613c425a26ae2db6e4cf207f88b5d26735d378",
);
}

#[test]
fn test_minimal_message() {
let json = json!({
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ macro_rules! impl_allocative {
#[macro_export]
#[cfg(feature = "serde")]
macro_rules! impl_serde {
(Address) => {
// Use custom implementation for Address
};
($t:ty) => {
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl $crate::private::serde::Serialize for $t {
Expand Down
35 changes: 33 additions & 2 deletions crates/primitives/src/bits/serde.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
use super::FixedBytes;
use core::fmt;
use super::{Address, FixedBytes};
use alloc::string::String;
use core::{fmt, str::FromStr};
use serde::{
de::{self, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};

impl Serialize for Address {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let checksum_address = self.to_checksum(None);
serializer.serialize_str(&checksum_address)
}
}

impl<'de> Deserialize<'de> for Address {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::from_str(&s).map_err(serde::de::Error::custom)
}
}

impl<const N: usize> Serialize for FixedBytes<N> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
Expand Down Expand Up @@ -67,6 +88,8 @@ impl<'de, const N: usize> Deserialize<'de> for FixedBytes<N> {

#[cfg(test)]
mod tests {
use core::str::FromStr;

use super::*;
use alloc::string::ToString;
use serde::Deserialize;
Expand All @@ -88,6 +111,14 @@ mod tests {
assert_eq!(serde_json::from_value::<FixedBytes<12>>(val).unwrap(), bytes);
}

#[test]
fn serde_address() {
let address = Address::from_str("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359").unwrap();
let ser = serde_json::to_string(&address).unwrap();
// serialize in checksum format
assert_eq!(ser, "\"0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359\"");
}

#[test]
fn serde_num_array() {
let json = serde_json::json! {{"fixed": [0,1,2,3,4]}};
Expand Down

0 comments on commit ba8dcb1

Please sign in to comment.