Last active
August 22, 2024 10:55
-
-
Save LimaniBhavik/82f6fdab2a4f11c5d087383ea1667cd6 to your computer and use it in GitHub Desktop.
Rust program that allows a user to input the name or ticker of a cryptocurrency, enter a crypto address, and then check if the address is valid or not for the given cryptocurrency. This example will use basic validations for five cryptocurrencies: Bitcoin (BTC), Ethereum (ETH), Litecoin (LTC), Bitcoin Cash (BCH), and Dogecoin (DOGE). Please note…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::io::{self, Write}; | |
fn main() { | |
// Define a map for the cryptocurrency tickers | |
let mut cryptos: HashMap<&str, &str> = HashMap::new(); | |
cryptos.insert("BTC", "Bitcoin"); | |
cryptos.insert("ETH", "Ethereum"); | |
cryptos.insert("LTC", "Litecoin"); | |
cryptos.insert("BCH", "Bitcoin Cash"); | |
cryptos.insert("DOGE", "Dogecoin"); | |
println!("Welcome to the Crypto Address Validator!"); | |
// Ask for the cryptocurrency ticker | |
print!("Enter the cryptocurrency name or ticker (e.g., BTC, ETH): "); | |
io::stdout().flush().unwrap(); | |
let mut ticker = String::new(); | |
io::stdin().read_line(&mut ticker).expect("Failed to read line"); | |
let ticker = ticker.trim().to_uppercase(); | |
// Check if the ticker is valid | |
if let Some(crypto_name) = cryptos.get(ticker.as_str()) { | |
println!("You selected: {}", crypto_name); | |
// Ask for the cryptocurrency address | |
print!("Enter the {} address: ", crypto_name); | |
io::stdout().flush().unwrap(); | |
let mut address = String::new(); | |
io::stdin().read_line(&mut address).expect("Failed to read line"); | |
let address = address.trim(); | |
// Validate the address (simplified) | |
let is_valid = validate_address(ticker.as_str(), address); | |
if is_valid { | |
println!("The address you entered is valid for {}.", crypto_name); | |
} else { | |
println!("The address you entered is not valid for {}.", crypto_name); | |
} | |
} else { | |
println!("Invalid cryptocurrency ticker or name entered."); | |
} | |
} | |
// A simplified address validation function | |
fn validate_address(ticker: &str, address: &str) -> bool { | |
match ticker { | |
"BTC" => address.len() == 34 && address.starts_with('1') || address.starts_with('3'), | |
"ETH" => address.len() == 42 && address.starts_with("0x"), | |
"LTC" => address.len() == 34 && address.starts_with('L') || address.starts_with('M'), | |
"BCH" => address.len() == 42 && address.starts_with("q") || address.starts_with("p"), | |
"DOGE" => address.len() == 34 && address.starts_with('D'), | |
_ => false, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment