diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 8f10c36f..894eebef 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -458,18 +458,10 @@ public class SettingsController extends WalletFormController implements Initiali } Optional optWallet = AppServices.get().getOpenWallets().entrySet().stream() - .filter(entry -> walletForm.getWalletFile().equals(entry.getValue().getWalletFile()) && entry.getKey().isMasterWallet()).map(Map.Entry::getKey).findFirst(); + .filter(entry -> walletForm.getWalletFile().equals(entry.getValue().getWalletFile()) + && entry.getKey().getName().equals(walletForm.getWallet().getName())).map(Map.Entry::getKey).findFirst(); if(optWallet.isPresent()) { - Wallet wallet = optWallet.get(); - if(!walletForm.getWallet().getName().equals(wallet.getName())) { - wallet = wallet.getChildWallet(walletForm.getWallet().getName()); - } - - if(wallet == null) { - throw new IllegalStateException("Cannot find child wallet " + walletForm.getWallet().getFullDisplayName() + " to export"); - } - - WalletExportDialog dlg = new WalletExportDialog(wallet); + WalletExportDialog dlg = new WalletExportDialog(optWallet.get()); dlg.showAndWait(); } else { AppServices.showErrorDialog("Cannot export wallet", "Wallet cannot be exported, please save it first."); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java index e96cd629..b722c39b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java @@ -60,15 +60,21 @@ public class SettingsWalletForm extends WalletForm { //Clear node tree, detaching and saving any labels from the existing wallet walletCopy.clearNodes(wallet); + //Retrieve master or child wallets from current active wallet before overwriting + Wallet masterWallet = wallet.isMasterWallet() ? null : wallet.getMasterWallet(); Integer childIndex = wallet.isMasterWallet() ? null : wallet.getMasterWallet().getChildWallets().indexOf(wallet); + List childWallets = wallet.getChildWallets(); //Replace the SettingsWalletForm wallet reference - note that this reference is only shared with the WalletForm wallet with WalletAddressesChangedEvent below wallet = walletCopy.copy(); + //Restore bidirectional links between original master or child wallets if(wallet.isMasterWallet()) { + wallet.setChildWallets(childWallets); wallet.getChildWallets().forEach(childWallet -> childWallet.setMasterWallet(wallet)); - } else if(childIndex != null) { - wallet.getMasterWallet().getChildWallets().set(childIndex, wallet); + } else if(masterWallet != null && childIndex != null) { + wallet.setMasterWallet(masterWallet); + masterWallet.getChildWallets().set(childIndex, wallet); } save();