Skip to content

Commit

Permalink
feat: Plugged in checksum verification
Browse files Browse the repository at this point in the history
  • Loading branch information
orlowskilp committed Oct 27, 2024
1 parent 501be63 commit 3525881
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/evm_account/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ where
})
}

fn _compute_address_checksum(address: &str) -> Result<String, Error> {
fn compute_address_checksum(address: &str) -> Result<String, Error> {
let address_ascii_lowercase = address.trim_start_matches(HEX_PREFIX).to_ascii_lowercase();

// Compute the hash of the address and represent it as a string of hex digits
Expand Down Expand Up @@ -207,15 +207,15 @@ fn _compute_address_checksum(address: &str) -> Result<String, Error> {
Ok(address_checksum)
}

fn _validate_address_checksum(address: &str) -> bool {
fn validate_address_checksum(address: &str) -> bool {
// If the address is all in lowercase, this means no checksum was applied and no validation is
// needed. This is acceptable, however a warning should be logged.
if address == address.to_ascii_lowercase() {
return true;
}

// Otherwise strict chgecksum validation is required.
match _compute_address_checksum(address) {
match compute_address_checksum(address) {
Ok(checksum) => checksum == address,
Err(_) => false,
}
Expand All @@ -227,7 +227,9 @@ where
{
let address_string = String::deserialize(deserializer)?;

// TODO: Perform checksum verification here
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| {
Expand All @@ -246,6 +248,10 @@ where
{
let address_string = String::deserialize(deserializer)?;

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

let address_bytes = hex_data_string_to_bytes(&address_string).map_err(|error| {
serde::de::Error::custom(format!("Failed to deserialize address: {}", error))
})?;
Expand Down Expand Up @@ -289,65 +295,65 @@ mod unit_tests {
fn validate_recipient_address_test_1_succeed() {
let input = TEST_ADDR_STR_1;

assert!(_validate_address_checksum(input));
assert!(validate_address_checksum(input));
}

#[test]
fn validate_recipient_address_test_2_succeed() {
let input = TEST_ADDR_STR_2;

assert!(_validate_address_checksum(input));
assert!(validate_address_checksum(input));
}

#[test]
fn validate_recipient_address_test_3_succeed() {
let input = TEST_ADDR_STR_3;

assert!(_validate_address_checksum(input));
assert!(validate_address_checksum(input));
}

#[test]
fn validate_recipient_address_test_1_no_checksum_succeed() {
let input = TEST_ADDR_STR_1.to_ascii_lowercase();

assert!(_validate_address_checksum(&input));
assert!(validate_address_checksum(&input));
}

#[test]
fn validate_recipient_address_test_2_no_checksum_succeed() {
let input = TEST_ADDR_STR_2.to_ascii_lowercase();

assert!(_validate_address_checksum(&input));
assert!(validate_address_checksum(&input));
}

#[test]
fn validate_recipient_address_test_3_no_checksum_succeed() {
let input = TEST_ADDR_STR_3.to_ascii_lowercase();

assert!(_validate_address_checksum(&input));
assert!(validate_address_checksum(&input));
}

#[test]
#[should_panic]
fn validate_recipient_address_test_1_fail() {
let input = "0xA9d89186caA663C8Ef0352Fd1Db3596280625573";

assert!(_validate_address_checksum(&input));
assert!(validate_address_checksum(&input));
}

#[test]
#[should_panic]
fn validate_recipient_address_test_2_fail() {
let input = "0x5aAeb6053F3E94c9b9A09F33669435E7Ef1BeAed";

assert!(_validate_address_checksum(&input));
assert!(validate_address_checksum(&input));
}

#[test]
#[should_panic]
fn validate_recipient_address_test_3_fail() {
let input = "0xfb6916095CA1df60bB79Ce92cE3Ea74c37c5d359";

assert!(_validate_address_checksum(&input));
assert!(validate_address_checksum(&input));
}
}

0 comments on commit 3525881

Please sign in to comment.