diff --git a/src/bitcoin/d/mod.rs b/src/bitcoin/d/mod.rs index 598412b6..f2b53d52 100644 --- a/src/bitcoin/d/mod.rs +++ b/src/bitcoin/d/mod.rs @@ -51,13 +51,10 @@ pub enum BitcoindError { Server(jsonrpc::error::Error), /// They replied to a batch request omitting some responses. BatchMissingResponse, - WalletCreation(String), - DescriptorImport(String), - WalletLoading(String), - MissingOrTooManyWallet, + /// Error while managing wallet. + Wallet(String /* watchonly wallet path */, WalletError), InvalidVersion(u64), NetworkMismatch(String /*config*/, String /*bitcoind*/), - MissingDescriptor, StartRescan, } @@ -98,14 +95,8 @@ impl std::fmt::Display for BitcoindError { f, "Bitcoind server replied without enough responses to our batched request" ), - BitcoindError::WalletCreation(s) => write!(f, "Error creating watchonly wallet: {}", s), - BitcoindError::DescriptorImport(s) => write!( - f, - "Error importing descriptor. Response from bitcoind: '{}'", - s - ), - BitcoindError::WalletLoading(s) => { - write!(f, "Error when loading watchonly wallet: '{}'.", s) + BitcoindError::Wallet(path, e) => { + write!(f, "Watchonly wallet (path: {}) error: {}", path, e) } BitcoindError::InvalidVersion(v) => { write!( @@ -121,15 +112,6 @@ impl std::fmt::Display for BitcoindError { conf_net, bitcoind_net ) } - BitcoindError::MissingOrTooManyWallet => { - write!( - f, - "No, or too many, watchonly wallet(s) loaded on bitcoind." - ) - } - BitcoindError::MissingDescriptor => { - write!(f, "The watchonly wallet loaded on bitcoind does not have the main descriptor imported.") - } BitcoindError::StartRescan => { write!( f, @@ -154,6 +136,42 @@ impl From for BitcoindError { } } +#[derive(Debug)] +pub enum WalletError { + Creating(String), + ImportingDescriptor(String), + Loading(String), + MissingOrTooManyWallet, + MissingDescriptor, +} + +impl std::fmt::Display for WalletError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + WalletError::Creating(s) => { + write!(f, "Error creating watchonly wallet: {}", s) + } + WalletError::ImportingDescriptor(s) => write!( + f, + "Error importing descriptor. Response from bitcoind: '{}'", + s + ), + WalletError::Loading(s) => { + write!(f, "Error when loading watchonly wallet: '{}'.", s) + } + WalletError::MissingOrTooManyWallet => { + write!( + f, + "No, or too many, watchonly wallet(s) loaded on bitcoind." + ) + } + WalletError::MissingDescriptor => { + write!(f, "The watchonly wallet loaded on bitcoind does not have the main descriptor imported.") + } + } + } +} + pub struct BitcoinD { /// Client for generalistic calls. node_client: Client, @@ -550,10 +568,16 @@ impl BitcoinD { // Now create the wallet and import the main descriptor. if let Some(err) = self.create_wallet(self.watchonly_wallet_path.clone()) { - return Err(BitcoindError::WalletCreation(err)); + return Err(BitcoindError::Wallet( + self.watchonly_wallet_path.clone(), + WalletError::Creating(err), + )); } if let Some(err) = self.import_descriptor(main_descriptor) { - return Err(BitcoindError::DescriptorImport(err)); + return Err(BitcoindError::Wallet( + self.watchonly_wallet_path.clone(), + WalletError::ImportingDescriptor(err), + )); } Ok(()) @@ -612,7 +636,10 @@ impl BitcoinD { .count() != 1 { - return Err(BitcoindError::MissingOrTooManyWallet); + return Err(BitcoindError::Wallet( + self.watchonly_wallet_path.clone(), + WalletError::MissingOrTooManyWallet, + )); } // Check our main descriptor is imported in this wallet. @@ -626,7 +653,10 @@ impl BitcoinD { if !desc_list.contains(&receive_desc.to_string()) || !desc_list.contains(&change_desc.to_string()) { - return Err(BitcoindError::MissingDescriptor); + return Err(BitcoindError::Wallet( + self.watchonly_wallet_path.clone(), + WalletError::MissingDescriptor, + )); } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index e4c823bb..6a0d3a06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,13 +12,11 @@ mod testutils; pub use miniscript; +pub use crate::bitcoin::d::{BitcoindError, WalletError}; #[cfg(feature = "jsonrpc_server")] use crate::jsonrpc::server::{rpcserver_loop, rpcserver_setup}; use crate::{ - bitcoin::{ - d::{BitcoinD, BitcoindError}, - poller, BitcoinInterface, - }, + bitcoin::{d::BitcoinD, poller, BitcoinInterface}, config::Config, database::{ sqlite::{FreshDbOptions, SqliteDb, SqliteDbError},