@@ -63,7 +63,7 @@ use bdk_wallet::Wallet as BdkWallet;
6363use bip39:: Mnemonic ;
6464
6565use bitcoin:: secp256k1:: PublicKey ;
66- use bitcoin:: { BlockHash , Network } ;
66+ use bitcoin:: { constants , BlockHash , Network } ;
6767
6868use bitcoin:: bip32:: { ChildNumber , Xpriv } ;
6969use 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
173175impl 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