109 lines
3.7 KiB
Rust
109 lines
3.7 KiB
Rust
|
|
use blockchain::common::cli_prompts::{prompt_hidden_nonempty, prompt_wallet_path};
|
||
|
|
use blockchain::common::network_startup::get_connections;
|
||
|
|
use blockchain::encode;
|
||
|
|
use blockchain::env;
|
||
|
|
use blockchain::json;
|
||
|
|
use blockchain::records::memory::response_channels::generate_uid;
|
||
|
|
use blockchain::standalone_tools::connections::handshake;
|
||
|
|
use blockchain::standalone_tools::loan_lookup_json::parse_loan_lookup_by_address;
|
||
|
|
use blockchain::standalone_tools::vanity_resolver::resolve_wallet_address_input;
|
||
|
|
use blockchain::wallets::structures::Wallet;
|
||
|
|
|
||
|
|
#[tokio::main]
|
||
|
|
async fn main() {
|
||
|
|
// Command 37 asks a peer for contract records tied to a wallet address.
|
||
|
|
let hashmap_key = generate_uid();
|
||
|
|
let rpc_command = 37;
|
||
|
|
|
||
|
|
// The address can be a long, short, or vanity address; request encoding normalizes it later.
|
||
|
|
let args: Vec<String> = env::args().collect();
|
||
|
|
if args.len() != 2 {
|
||
|
|
println!("Usage: ./lookup_loan_by_address <wallet_address>");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let wallet_address = match args[1].parse::<String>() {
|
||
|
|
Ok(address) => address,
|
||
|
|
Err(_) => {
|
||
|
|
println!("Please enter a wallet address");
|
||
|
|
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;
|
||
|
|
let wallet = match Wallet::try_obtain_wallet(encryption_key, Some(&wallet_path)).await {
|
||
|
|
Ok(wallet) => wallet,
|
||
|
|
Err(err) => {
|
||
|
|
eprintln!("Wallet decryption failed: {err}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
let wallet_address = match resolve_wallet_address_input(&wallet_address, &wallet).await {
|
||
|
|
Ok(address) => address,
|
||
|
|
Err(err) => {
|
||
|
|
eprintln!("wallet address is not valid: {err}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Try each configured peer until one returns a response.
|
||
|
|
let connections = get_connections().await;
|
||
|
|
let mut connected = false;
|
||
|
|
|
||
|
|
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,
|
||
|
|
wallet_address.clone(),
|
||
|
|
rpc_command,
|
||
|
|
handshake::HandshakeWallet::WalletParts {
|
||
|
|
public_key: wallet.saved.public_key.clone(),
|
||
|
|
private_key: wallet.saved.private_key.clone(),
|
||
|
|
},
|
||
|
|
hashmap_key,
|
||
|
|
)
|
||
|
|
.await;
|
||
|
|
|
||
|
|
match result {
|
||
|
|
Ok(response) => {
|
||
|
|
// Loan lookups return a key/value text summary; print successful data as JSON.
|
||
|
|
let response_text = String::from_utf8_lossy(&response);
|
||
|
|
let trimmed = response_text.trim();
|
||
|
|
if !trimmed.is_empty() {
|
||
|
|
if trimmed.starts_with("error:") {
|
||
|
|
println!("{trimmed}");
|
||
|
|
} else {
|
||
|
|
println!(
|
||
|
|
"{}",
|
||
|
|
serde_json::to_string_pretty(&parse_loan_lookup_by_address(trimmed))
|
||
|
|
.unwrap()
|
||
|
|
);
|
||
|
|
}
|
||
|
|
connected = true;
|
||
|
|
} else if !response.is_empty() {
|
||
|
|
println!(
|
||
|
|
"{}",
|
||
|
|
serde_json::to_string_pretty(&json!({ "hex": encode(response) })).unwrap()
|
||
|
|
);
|
||
|
|
connected = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Err(_) => {
|
||
|
|
connected = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if !connected {
|
||
|
|
eprintln!("failed to connect");
|
||
|
|
}
|
||
|
|
}
|