From 2972f1a4d7057f74cc265c1dbc153721759c1867 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 8 Nov 2022 15:51:59 +0200 Subject: [PATCH] fix export from settings tabs for new account by ensuring bidirectional links are restored on save --- .../sparrow/wallet/SettingsController.java | 14 +++----------- .../sparrow/wallet/SettingsWalletForm.java | 10 ++++++++-- 2 files changed, 11 insertions(+), 13 deletions(-) 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();