-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(deps): change ethereum-types to alloy-primitives (#7) f f * Add additional utils (#9) * add bytes_20_hex * add bytes_20_hex * add bytes_20_hex * add address utils * add address utils * add b256 utils * remove ssz feature flag * Add tests for B256 --------- Co-authored-by: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Co-authored-by: Eitan Seri-Levi <eserilev@gmail.com>
- Loading branch information
1 parent
9dbafae
commit c74ceb5
Showing
7 changed files
with
196 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use alloy_primitives::Address; | ||
use serde::de::Error; | ||
use serde::{Deserializer, Serializer}; | ||
|
||
use crate::hex::PrefixedHexVisitor; | ||
|
||
pub fn serialize<S>(address: &Address, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut hex_string: String = "0x".to_string(); | ||
hex_string.push_str(&hex::encode(&address)); | ||
|
||
serializer.serialize_str(&hex_string) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Address, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let decoded = deserializer.deserialize_str(PrefixedHexVisitor)?; | ||
|
||
if decoded.len() != 20 { | ||
return Err(D::Error::custom(format!( | ||
"expected {} bytes for array, got {}", | ||
20, | ||
decoded.len() | ||
))); | ||
} | ||
|
||
let mut array = [0; 20]; | ||
array.copy_from_slice(&decoded); | ||
Ok(array.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use alloy_primitives::B256; | ||
use serde::de::Error; | ||
use serde::{Deserializer, Serializer}; | ||
|
||
use crate::hex::PrefixedHexVisitor; | ||
|
||
pub fn serialize<S>(hash: &B256, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut hex_string: String = "0x".to_string(); | ||
hex_string.push_str(&hex::encode(&hash)); | ||
|
||
serializer.serialize_str(&hex_string) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<B256, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let decoded = deserializer.deserialize_str(PrefixedHexVisitor)?; | ||
|
||
if decoded.len() != 32 { | ||
return Err(D::Error::custom(format!( | ||
"expected {} bytes for array, got {}", | ||
32, | ||
decoded.len() | ||
))); | ||
} | ||
|
||
let mut array = [0; 32]; | ||
array.copy_from_slice(&decoded); | ||
Ok(array.into()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
struct Wrapper { | ||
#[serde(with = "super")] | ||
val: B256, | ||
} | ||
|
||
#[test] | ||
fn encoding() { | ||
assert_eq!( | ||
&serde_json::to_string(&Wrapper { val: B256::ZERO }).unwrap(), | ||
"\"0x0000000000000000000000000000000000000000000000000000000000000000\"" | ||
); | ||
assert_eq!( | ||
&serde_json::to_string(&Wrapper { | ||
val: B256::with_last_byte(0x03) | ||
}) | ||
.unwrap(), | ||
"\"0x0000000000000000000000000000000000000000000000000000000000000003\"" | ||
); | ||
assert_eq!( | ||
&serde_json::to_string(&Wrapper { | ||
val: B256::repeat_byte(0x03) | ||
}) | ||
.unwrap(), | ||
"\"0x0303030303030303030303030303030303030303030303030303030303030303\"" | ||
); | ||
} | ||
|
||
#[test] | ||
fn decoding() { | ||
assert_eq!( | ||
serde_json::from_str::<Wrapper>( | ||
"\"0x0000000000000000000000000000000000000000000000000000000000000000\"" | ||
) | ||
.unwrap(), | ||
Wrapper { val: B256::ZERO }, | ||
); | ||
assert_eq!( | ||
serde_json::from_str::<Wrapper>( | ||
"\"0x0000000000000000000000000000000000000000000000000000000000000003\"" | ||
) | ||
.unwrap(), | ||
Wrapper { | ||
val: B256::with_last_byte(0x03) | ||
}, | ||
); | ||
|
||
// Require 0x. | ||
serde_json::from_str::<Wrapper>( | ||
"\"0000000000000000000000000000000000000000000000000000000000000000\"", | ||
) | ||
.unwrap_err(); | ||
// Wrong length. | ||
serde_json::from_str::<Wrapper>( | ||
"\"0x00000000000000000000000000000000000000000000000000000000000000\"", | ||
) | ||
.unwrap_err(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod quoted_int; | ||
|
||
pub mod address_hex; | ||
pub mod b256_hex; | ||
pub mod fixed_bytes_hex; | ||
pub mod hex; | ||
pub mod hex_vec; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.