use blockchain::common::cli_prompts::{prompt_hidden_nonempty, prompt_wallet_path}; use blockchain::common::network_startup::get_connections; use blockchain::env; use blockchain::records::memory::response_channels::generate_uid; use blockchain::standalone_tools::connections::handshake; #[tokio::main] async fn main() { let hashmap_key = generate_uid(); // Get command-line arguments let args: Vec = env::args().collect(); // Check if a command-line argument is provided if args.len() != 1 { println!("Usage: ./request_height"); return; } let wallet_path = prompt_wallet_path().await; let encryption_key = prompt_hidden_nonempty( "What is your wallet decryption key? ", "Wallet key cannot be empty. Please try again.", ) .await; // set the rpc command number let rpc_command = 2; let json = "".to_string(); let connections = get_connections().await; let mut connected: bool = false; // Iterate over the returned Vec and print each connection for conn in connections { if connected { break; } let socket_address = conn.parse().expect("Failed to parse the socket address"); let result = handshake::connect_and_handshake( socket_address, json.clone(), rpc_command, handshake::HandshakeWallet::WalletKey { encryption_key: encryption_key.clone(), wallet_path: wallet_path.clone(), }, hashmap_key, ) .await; match result { Ok(response) => { if response.len() == 4 { let height = u32::from_le_bytes(response[..4].try_into().unwrap()); println!("{height}"); connected = true; } else { let response_text = String::from_utf8_lossy(&response); let trimmed = response_text.trim(); if !trimmed.is_empty() { println!("{trimmed}"); connected = true; } } } Err(_) => { connected = false; } } } if !connected { eprintln!("failed to connect"); } }