commit 076dc90710086f24aa72fa0a9f84fb82a7491b8f
parent 171993db6cc36166d98b940354a960c287ef6895
Author: Antoine A <>
Date: Tue, 16 Nov 2021 12:01:04 +0100
Ugly network check
Diffstat:
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -21,8 +21,8 @@ pub enum Network {
impl Network {
pub fn dir(&self) -> &'static str {
match self {
- Network::MainNet => "mainnet",
- Network::TestNet => "testnet",
+ Network::MainNet => "main",
+ Network::TestNet => "testnet3",
Network::RegTest => "regtest",
}
}
@@ -63,12 +63,11 @@ struct Args {
wire: bool,
}
-fn common_rpc(network: Network) -> Client {
+fn common_rpc(network: Network) -> bitcoincore_rpc::Result<Client> {
Client::new(
RPC_URL,
Auth::CookieFile(network_dir_path(network).join(".cookie")),
)
- .expect("Failed to open common client")
}
fn wallet_rpc(network: Network, wallet: &str) -> Client {
@@ -89,7 +88,29 @@ fn last_transaction(rpc: &Client) -> bitcoincore_rpc::Result<Txid> {
}
fn main() {
- let network = Network::RegTest;
+ // Guess network by trying to connect to a JSON RPC server
+ let network = (|| {
+ let result_main = common_rpc(Network::MainNet).and_then(|rpc| rpc.get_network_info());
+ if result_main.is_ok() {
+ println!("Connected to the main chain");
+ return Network::MainNet;
+ }
+ let result_test = common_rpc(Network::TestNet).and_then(|rpc| rpc.get_network_info());
+ if result_test.is_ok() {
+ println!("Connected to the test chain");
+ return Network::TestNet;
+ }
+ let result_reg = common_rpc(Network::RegTest).and_then(|rpc| rpc.get_network_info());
+ if result_reg.is_ok() {
+ println!("Connected to the reg chain");
+ return Network::RegTest;
+ }
+
+ unreachable!(
+ "Failed to connect to any chain\nmain: {:?}\ntest: {:?}\nreg: {:?}",
+ result_main, result_test, result_reg
+ );
+ })();
{
let existing_wallets: HashSet<String> =
std::fs::read_dir(network_dir_path(network).join("wallets"))
@@ -98,7 +119,7 @@ fn main() {
.map(|it| it.file_name().to_string_lossy().to_string())
.collect();
- let rpc = common_rpc(network);
+ let rpc = common_rpc(network).expect("Failed to open common client");
if !existing_wallets.contains(CLIENT) || !existing_wallets.contains(WIRE) {
println!("Generate new wallets");
// Create wallets