nautilus_model/defi/
validation.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Validation utilities for blockchain data types.
17//!
18//! This module provides validation functions for ensuring the correctness and integrity
19//! of blockchain-related data, particularly Ethereum addresses and other EVM-compatible
20//! blockchain identifiers.
21
22use std::str::FromStr;
23
24use alloy_primitives::Address;
25
26/// Validates an Ethereum address format, checksum, and returns the parsed address.
27///
28/// # Errors
29///
30/// This function returns an error if:
31/// - The address does not start with the `0x` prefix.
32/// - The address has invalid length (must be 42 characters including `0x`).
33/// - The address contains invalid hexadecimal characters.
34/// - The address has an incorrect checksum (for checksummed addresses).
35pub fn validate_address(address: &str) -> anyhow::Result<Address> {
36    // Check if the address starts with "0x"
37    if !address.starts_with("0x") {
38        anyhow::bail!("Ethereum address must start with '0x': {address}");
39    }
40
41    // Check if the address is valid
42    let parsed_address = Address::from_str(address)
43        .map_err(|e| anyhow::anyhow!("Blockchain address '{address}' is incorrect: {e}"))?;
44
45    // Check if checksum is valid
46    Address::parse_checksummed(address, None)
47        .map_err(|_| anyhow::anyhow!("Blockchain address '{address}' has incorrect checksum"))?;
48
49    Ok(parsed_address)
50}
51
52////////////////////////////////////////////////////////////////////////////////
53// Tests
54////////////////////////////////////////////////////////////////////////////////
55
56#[cfg(test)]
57mod tests {
58    use rstest::rstest;
59
60    use super::*;
61
62    #[rstest]
63    fn test_validate_address_invalid_prefix() {
64        let invalid_address = "742d35Cc6634C0532925a3b844Bc454e4438f44e";
65        let result = validate_address(invalid_address);
66        assert!(result.is_err());
67        assert_eq!(
68            result.unwrap_err().to_string(),
69            "Ethereum address must start with '0x': 742d35Cc6634C0532925a3b844Bc454e4438f44e"
70        );
71    }
72
73    #[rstest]
74    fn test_validate_invalid_address_format() {
75        let invalid_length_address = "0x1233";
76        let invalid_characters_address = "0xZZZd35Cc6634C0532925a3b844Bc454e4438f44e";
77
78        assert_eq!(
79            validate_address(invalid_length_address)
80                .unwrap_err()
81                .to_string(),
82            "Blockchain address '0x1233' is incorrect: invalid string length"
83        );
84        assert_eq!(
85            validate_address(invalid_characters_address)
86                .unwrap_err()
87                .to_string(),
88            "Blockchain address '0xZZZd35Cc6634C0532925a3b844Bc454e4438f44e' is incorrect: invalid character 'Z' at position 0"
89        );
90    }
91
92    #[rstest]
93    fn test_validate_invalid_checksum() {
94        let invalid_checksum_address = "0x742d35cc6634c0532925a3b844bc454e4438f44e";
95        assert_eq!(
96            validate_address(invalid_checksum_address)
97                .unwrap_err()
98                .to_string(),
99            "Blockchain address '0x742d35cc6634c0532925a3b844bc454e4438f44e' has incorrect checksum"
100        );
101    }
102}