From d4c6b13fee191cd5b374a0681fddd1d2f9871ff0 Mon Sep 17 00:00:00 2001 From: orlowskilp Date: Mon, 28 Oct 2024 07:55:08 +0800 Subject: [PATCH] refactor: Moved address string deserialization to `access_list` module --- src/evm_account/transaction.rs | 19 ------------------- src/evm_account/transaction/access_list.rs | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/evm_account/transaction.rs b/src/evm_account/transaction.rs index bdcc7f1..0df09bf 100644 --- a/src/evm_account/transaction.rs +++ b/src/evm_account/transaction.rs @@ -221,25 +221,6 @@ fn validate_address_checksum(address: &str) -> bool { } } -fn deserialize_address_string<'de, D>(deserializer: D) -> Result -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, D::Error> diff --git a/src/evm_account/transaction/access_list.rs b/src/evm_account/transaction/access_list.rs index e4624c1..148e6b2 100644 --- a/src/evm_account/transaction/access_list.rs +++ b/src/evm_account/transaction/access_list.rs @@ -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; @@ -31,6 +31,25 @@ impl Encodable for Access { } } +fn deserialize_address_string<'de, D>(deserializer: D) -> Result +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, D::Error>