Skip to content

Commit

Permalink
refactor: Moved address string deserialization to access_list module
Browse files Browse the repository at this point in the history
  • Loading branch information
orlowskilp committed Oct 27, 2024
1 parent 169a6fd commit d4c6b13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
19 changes: 0 additions & 19 deletions src/evm_account/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,6 @@ fn validate_address_checksum(address: &str) -> bool {
}
}

fn deserialize_address_string<'de, D>(deserializer: D) -> Result<AccountAddress, D::Error>
where
D: Deserializer<'de>,
{
let address_string = String::deserialize(deserializer)?;

if !validate_address_checksum(&address_string) {
return Err(serde::de::Error::custom("Invalid address checksum"));
}

hex_data_string_to_bytes(&address_string)
.map_err(|error| {
serde::de::Error::custom(format!("Failed to deserialize address: {}", error))
})?
// Checks whether address is of proper length
.try_into()
.map_err(|_| serde::de::Error::custom("Invalid address length"))
}

fn deserialize_address_string_option<'de, D>(
deserializer: D,
) -> Result<Option<AccountAddress>, D::Error>
Expand Down
21 changes: 20 additions & 1 deletion src/evm_account/transaction/access_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rlp::Encodable;
use serde::{Deserialize, Deserializer, Serialize};

use super::{deserialize_address_string, hex_data_string_to_bytes, AccountAddress};
use super::{hex_data_string_to_bytes, validate_address_checksum, AccountAddress};

const STORAGE_KEY_LEN: usize = 32;

Expand Down Expand Up @@ -31,6 +31,25 @@ impl Encodable for Access {
}
}

fn deserialize_address_string<'de, D>(deserializer: D) -> Result<AccountAddress, D::Error>
where
D: Deserializer<'de>,
{
let address_string = String::deserialize(deserializer)?;

if !validate_address_checksum(&address_string) {
return Err(serde::de::Error::custom("Invalid address checksum"));
}

hex_data_string_to_bytes(&address_string)
.map_err(|error| {
serde::de::Error::custom(format!("Failed to deserialize address: {}", error))
})?
// Checks whether address is of proper length
.try_into()
.map_err(|_| serde::de::Error::custom("Invalid address length"))
}

fn deserialize_storage_keys_string_list<'de, D>(
deserializer: D,
) -> Result<Vec<StorageKey>, D::Error>
Expand Down

0 comments on commit d4c6b13

Please sign in to comment.