nautilus_blockchain/validation.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3// https://poseitrader.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/// Returns an error if:
31/// - The address does not start with '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#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_validate_address_invalid_prefix() {
58 let invalid_address = "742d35Cc6634C0532925a3b844Bc454e4438f44e";
59 let result = validate_address(invalid_address);
60 assert!(result.is_err());
61 assert_eq!(
62 result.unwrap_err().to_string(),
63 "Ethereum address must start with '0x': 742d35Cc6634C0532925a3b844Bc454e4438f44e"
64 );
65 }
66
67 #[test]
68 fn test_validate_invalid_address_format() {
69 let invalid_length_address = "0x1233";
70 let invalid_characters_address = "0xZZZd35Cc6634C0532925a3b844Bc454e4438f44e";
71
72 assert_eq!(
73 validate_address(invalid_length_address)
74 .unwrap_err()
75 .to_string(),
76 "Blockchain address '0x1233' is incorrect: invalid string length"
77 );
78 assert_eq!(
79 validate_address(invalid_characters_address)
80 .unwrap_err()
81 .to_string(),
82 "Blockchain address '0xZZZd35Cc6634C0532925a3b844Bc454e4438f44e' is incorrect: invalid character 'Z' at position 0"
83 );
84 }
85
86 #[test]
87 fn test_validate_invalid_checksum() {
88 let invalid_checksum_address = "0x742d35cc6634c0532925a3b844bc454e4438f44e";
89 assert_eq!(
90 validate_address(invalid_checksum_address)
91 .unwrap_err()
92 .to_string(),
93 "Blockchain address '0x742d35cc6634c0532925a3b844bc454e4438f44e' has incorrect checksum"
94 );
95 }
96}