Skip to content

Commit b1b2caf

Browse files
committed
Add NetworkMismatch error type
Return specific error if the node is restarted with a different network from the one it was first created with. This will be caught when trying to load the `BDK` wallet.
1 parent f0338d1 commit b1b2caf

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ enum BuildError {
321321
"KVStoreSetupFailed",
322322
"WalletSetupFailed",
323323
"LoggerSetupFailed",
324+
"NetworkMismatch",
324325
};
325326

326327
[Trait]

src/builder.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use bdk_wallet::Wallet as BdkWallet;
6363
use bip39::Mnemonic;
6464

6565
use bitcoin::secp256k1::PublicKey;
66-
use bitcoin::{BlockHash, Network};
66+
use bitcoin::{constants, BlockHash, Network};
6767

6868
use bitcoin::bip32::{ChildNumber, Xpriv};
6969
use std::collections::HashMap;
@@ -168,6 +168,8 @@ pub enum BuildError {
168168
WalletSetupFailed,
169169
/// We failed to setup the logger.
170170
LoggerSetupFailed,
171+
/// The given network does not match the node's previously configured network.
172+
NetworkMismatch,
171173
}
172174

173175
impl fmt::Display for BuildError {
@@ -189,6 +191,9 @@ impl fmt::Display for BuildError {
189191
Self::WalletSetupFailed => write!(f, "Failed to setup onchain wallet."),
190192
Self::LoggerSetupFailed => write!(f, "Failed to setup the logger."),
191193
Self::InvalidNodeAlias => write!(f, "Given node alias is invalid."),
194+
Self::NetworkMismatch => {
195+
write!(f, "Given network does not match the node's previously configured network.")
196+
},
192197
}
193198
}
194199
}
@@ -903,10 +908,19 @@ fn build_with_store_internal(
903908
.descriptor(KeychainKind::Internal, Some(change_descriptor.clone()))
904909
.extract_keys()
905910
.check_network(config.network)
911+
.check_genesis_hash(constants::genesis_block(config.network).block_hash())
906912
.load_wallet(&mut wallet_persister)
907-
.map_err(|e| {
908-
log_error!(logger, "Failed to set up wallet: {}", e);
909-
BuildError::WalletSetupFailed
913+
.map_err(|e| match e {
914+
bdk_wallet::LoadWithPersistError::InvalidChangeSet(
915+
bdk_wallet::LoadError::Mismatch(bdk_wallet::LoadMismatch::Network { .. }),
916+
) => BuildError::NetworkMismatch,
917+
bdk_wallet::LoadWithPersistError::InvalidChangeSet(
918+
bdk_wallet::LoadError::Mismatch(bdk_wallet::LoadMismatch::Genesis { .. }),
919+
) => BuildError::NetworkMismatch,
920+
_ => {
921+
log_error!(logger, "Failed to set up wallet: {}", e);
922+
BuildError::WalletSetupFailed
923+
},
910924
})?;
911925
let bdk_wallet = match wallet_opt {
912926
Some(wallet) => wallet,

0 commit comments

Comments
 (0)