mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-24 12:46:45 +00:00
terminal - lock wallet
This commit is contained in:
parent
e8fb676a24
commit
cbf847a57f
3 changed files with 69 additions and 3 deletions
|
@ -5,8 +5,13 @@ import com.googlecode.lanterna.gui2.ActionListBox;
|
||||||
import com.googlecode.lanterna.gui2.dialogs.ActionListDialogBuilder;
|
import com.googlecode.lanterna.gui2.dialogs.ActionListDialogBuilder;
|
||||||
import com.googlecode.lanterna.gui2.dialogs.FileDialogBuilder;
|
import com.googlecode.lanterna.gui2.dialogs.FileDialogBuilder;
|
||||||
import com.googlecode.lanterna.gui2.dialogs.TextInputDialogBuilder;
|
import com.googlecode.lanterna.gui2.dialogs.TextInputDialogBuilder;
|
||||||
|
import com.sparrowwallet.drongo.SecureString;
|
||||||
|
import com.sparrowwallet.drongo.crypto.InvalidPasswordException;
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
import com.sparrowwallet.sparrow.AppServices;
|
import com.sparrowwallet.sparrow.AppServices;
|
||||||
|
import com.sparrowwallet.sparrow.EventManager;
|
||||||
|
import com.sparrowwallet.sparrow.event.StorageEvent;
|
||||||
|
import com.sparrowwallet.sparrow.event.TimedEvent;
|
||||||
import com.sparrowwallet.sparrow.io.Config;
|
import com.sparrowwallet.sparrow.io.Config;
|
||||||
import com.sparrowwallet.sparrow.io.Storage;
|
import com.sparrowwallet.sparrow.io.Storage;
|
||||||
import com.sparrowwallet.sparrow.terminal.preferences.GeneralDialog;
|
import com.sparrowwallet.sparrow.terminal.preferences.GeneralDialog;
|
||||||
|
@ -15,12 +20,19 @@ import com.sparrowwallet.sparrow.terminal.preferences.ServerTypeDialog;
|
||||||
import com.sparrowwallet.sparrow.terminal.wallet.Bip39Dialog;
|
import com.sparrowwallet.sparrow.terminal.wallet.Bip39Dialog;
|
||||||
import com.sparrowwallet.sparrow.terminal.wallet.LoadWallet;
|
import com.sparrowwallet.sparrow.terminal.wallet.LoadWallet;
|
||||||
import com.sparrowwallet.sparrow.terminal.wallet.WatchOnlyDialog;
|
import com.sparrowwallet.sparrow.terminal.wallet.WatchOnlyDialog;
|
||||||
|
import javafx.application.Platform;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static com.sparrowwallet.sparrow.AppServices.showErrorDialog;
|
||||||
|
|
||||||
public class MasterActionListBox extends ActionListBox {
|
public class MasterActionListBox extends ActionListBox {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(MasterActionListBox.class);
|
||||||
|
|
||||||
public static final int MAX_RECENT_WALLETS = 6;
|
public static final int MAX_RECENT_WALLETS = 6;
|
||||||
|
|
||||||
public MasterActionListBox(SparrowTerminal sparrowTerminal) {
|
public MasterActionListBox(SparrowTerminal sparrowTerminal) {
|
||||||
|
@ -42,9 +54,9 @@ public class MasterActionListBox extends ActionListBox {
|
||||||
.filter(entry -> entry.getValue().getWalletFile().equals(recentWalletFile)).map(Map.Entry::getKey)
|
.filter(entry -> entry.getValue().getWalletFile().equals(recentWalletFile)).map(Map.Entry::getKey)
|
||||||
.map(wallet -> wallet.isMasterWallet() ? wallet : wallet.getMasterWallet()).findFirst();
|
.map(wallet -> wallet.isMasterWallet() ? wallet : wallet.getMasterWallet()).findFirst();
|
||||||
if(optWallet.isPresent()) {
|
if(optWallet.isPresent()) {
|
||||||
builder.addAction(storage.getWalletName(null) + "*", () -> {
|
Wallet wallet = optWallet.get();
|
||||||
SparrowTerminal.get().getGuiThread().invokeLater(() -> LoadWallet.getOpeningDialog(storage, optWallet.get()).showDialog(SparrowTerminal.get().getGui()));
|
Storage existingStorage = AppServices.get().getOpenWallets().get(wallet);
|
||||||
});
|
builder.addAction(storage.getWalletName(null) + "*", () -> openLoadedWallet(existingStorage, optWallet.get()));
|
||||||
} else {
|
} else {
|
||||||
builder.addAction(storage.getWalletName(null), new LoadWallet(storage));
|
builder.addAction(storage.getWalletName(null), new LoadWallet(storage));
|
||||||
}
|
}
|
||||||
|
@ -82,6 +94,41 @@ public class MasterActionListBox extends ActionListBox {
|
||||||
addItem("Quit", () -> sparrowTerminal.getGui().getMainWindow().close());
|
addItem("Quit", () -> sparrowTerminal.getGui().getMainWindow().close());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void openLoadedWallet(Storage storage, Wallet wallet) {
|
||||||
|
if(SparrowTerminal.get().isLocked(storage)) {
|
||||||
|
String walletId = storage.getWalletId(wallet);
|
||||||
|
|
||||||
|
TextInputDialogBuilder builder = new TextInputDialogBuilder().setTitle("Wallet Password");
|
||||||
|
builder.setDescription("Enter the wallet password:");
|
||||||
|
builder.setPasswordInput(true);
|
||||||
|
|
||||||
|
String password = builder.build().showDialog(SparrowTerminal.get().getGui());
|
||||||
|
if(password != null) {
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, new SecureString(password), true);
|
||||||
|
keyDerivationService.setOnSucceeded(workerStateEvent -> {
|
||||||
|
EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Done"));
|
||||||
|
keyDerivationService.getValue().clear();
|
||||||
|
SparrowTerminal.get().unlockWallet(storage);
|
||||||
|
SparrowTerminal.get().getGuiThread().invokeLater(() -> LoadWallet.getOpeningDialog(storage, wallet).showDialog(SparrowTerminal.get().getGui()));
|
||||||
|
});
|
||||||
|
keyDerivationService.setOnFailed(workerStateEvent -> {
|
||||||
|
EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Failed"));
|
||||||
|
if(keyDerivationService.getException() instanceof InvalidPasswordException) {
|
||||||
|
showErrorDialog("Invalid Password", "The wallet password was invalid.");
|
||||||
|
} else {
|
||||||
|
log.error("Error deriving wallet key", keyDerivationService.getException());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.START, "Decrypting wallet..."));
|
||||||
|
keyDerivationService.start();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SparrowTerminal.get().getGuiThread().invokeLater(() -> LoadWallet.getOpeningDialog(storage, wallet).showDialog(SparrowTerminal.get().getGui()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void openWallet() {
|
private static void openWallet() {
|
||||||
FileDialogBuilder openBuilder = new FileDialogBuilder().setTitle("Open Wallet");
|
FileDialogBuilder openBuilder = new FileDialogBuilder().setTitle("Open Wallet");
|
||||||
openBuilder.setShowHiddenDirectories(true);
|
openBuilder.setShowHiddenDirectories(true);
|
||||||
|
|
|
@ -40,6 +40,7 @@ public class SparrowTerminal extends Application {
|
||||||
private SparrowTextGui gui;
|
private SparrowTextGui gui;
|
||||||
|
|
||||||
private final Map<String, WalletData> walletData = new HashMap<>();
|
private final Map<String, WalletData> walletData = new HashMap<>();
|
||||||
|
private final Set<File> lockedWallets = new HashSet<>();
|
||||||
|
|
||||||
private static final javafx.stage.Window DEFAULT_WINDOW = new Window() { };
|
private static final javafx.stage.Window DEFAULT_WINDOW = new Window() { };
|
||||||
|
|
||||||
|
@ -147,4 +148,16 @@ public class SparrowTerminal extends Application {
|
||||||
|
|
||||||
EventManager.get().post(new WalletOpenedEvent(storage, wallet));
|
EventManager.get().post(new WalletOpenedEvent(storage, wallet));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isLocked(Storage storage) {
|
||||||
|
return lockedWallets.contains(storage.getWalletFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void lockWallet(Storage storage) {
|
||||||
|
lockedWallets.add(storage.getWalletFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unlockWallet(Storage storage) {
|
||||||
|
lockedWallets.remove(storage.getWalletFile());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,12 @@ public class WalletActionsDialog extends DialogWindow {
|
||||||
SettingsDialog settingsDialog = getWalletData().getSettingsDialog();
|
SettingsDialog settingsDialog = getWalletData().getSettingsDialog();
|
||||||
settingsDialog.showDialog(SparrowTerminal.get().getGui());
|
settingsDialog.showDialog(SparrowTerminal.get().getGui());
|
||||||
});
|
});
|
||||||
|
if(getWalletData().getWalletForm().getWallet().isEncrypted()) {
|
||||||
|
actions.addItem("Lock", () -> {
|
||||||
|
close();
|
||||||
|
SparrowTerminal.get().lockWallet(getWalletData().getWalletForm().getStorage());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Panel mainPanel = new Panel();
|
Panel mainPanel = new Panel();
|
||||||
mainPanel.setLayoutManager(new GridLayout(1).setLeftMarginSize(1).setRightMarginSize(1));
|
mainPanel.setLayoutManager(new GridLayout(1).setLeftMarginSize(1).setRightMarginSize(1));
|
||||||
|
|
Loading…
Reference in a new issue