Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 206a092

Browse files
committedMar 19, 2025·
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 206a092

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎bindings/ldk_node.udl

+1
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

+16-4
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.
172+
NetworkMismatch,
171173
}
172174

173175
impl fmt::Display for BuildError {
@@ -189,6 +191,7 @@ 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 => write!(f, "Given network does not match the node's network."),
192195
}
193196
}
194197
}
@@ -903,10 +906,19 @@ fn build_with_store_internal(
903906
.descriptor(KeychainKind::Internal, Some(change_descriptor.clone()))
904907
.extract_keys()
905908
.check_network(config.network)
909+
.check_genesis_hash(constants::genesis_block(config.network).block_hash())
906910
.load_wallet(&mut wallet_persister)
907-
.map_err(|e| {
908-
log_error!(logger, "Failed to set up wallet: {}", e);
909-
BuildError::WalletSetupFailed
911+
.map_err(|e| match e {
912+
bdk_wallet::LoadWithPersistError::InvalidChangeSet(
913+
bdk_wallet::LoadError::Mismatch(bdk_wallet::LoadMismatch::Network { .. }),
914+
) => BuildError::NetworkMismatch,
915+
bdk_wallet::LoadWithPersistError::InvalidChangeSet(
916+
bdk_wallet::LoadError::Mismatch(bdk_wallet::LoadMismatch::Genesis { .. }),
917+
) => BuildError::NetworkMismatch,
918+
_ => {
919+
log_error!(logger, "Failed to set up wallet: {}", e);
920+
BuildError::WalletSetupFailed
921+
},
910922
})?;
911923
let bdk_wallet = match wallet_opt {
912924
Some(wallet) => wallet,

0 commit comments

Comments
 (0)
Please sign in to comment.