refactor to prepare for keystore encryption

This commit is contained in:
Craig Raw 2020-05-12 14:17:19 +02:00
parent d7c6f5d587
commit 84888b4a43
4 changed files with 16 additions and 15 deletions

2
drongo

@ -1 +1 @@
Subproject commit 242c83735a24456a4bd23fd556a56122c9b259ab
Subproject commit b951f79cfded507112cc7f213567c6a43c00e7e8

View file

@ -3,9 +3,10 @@ package com.sparrowwallet.sparrow;
import com.google.common.base.Charsets;
import com.google.common.eventbus.Subscribe;
import com.google.common.io.ByteSource;
import com.google.gson.JsonSyntaxException;
import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.crypto.InvalidPasswordException;
import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.protocol.Transaction;
@ -14,6 +15,7 @@ import com.sparrowwallet.drongo.psbt.PSBTParseException;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.control.TextAreaDialog;
import com.sparrowwallet.sparrow.control.WalletNameDialog;
import com.sparrowwallet.sparrow.control.WalletPasswordDialog;
import com.sparrowwallet.sparrow.event.TabEvent;
import com.sparrowwallet.sparrow.event.TransactionTabChangedEvent;
import com.sparrowwallet.sparrow.event.TransactionTabSelectedEvent;
@ -21,7 +23,6 @@ import com.sparrowwallet.sparrow.io.FileType;
import com.sparrowwallet.sparrow.io.IOUtils;
import com.sparrowwallet.sparrow.io.Storage;
import com.sparrowwallet.sparrow.transaction.TransactionController;
import com.sparrowwallet.sparrow.wallet.SettingsController;
import com.sparrowwallet.sparrow.wallet.WalletController;
import com.sparrowwallet.sparrow.wallet.WalletForm;
import javafx.event.ActionEvent;
@ -233,12 +234,13 @@ public class AppController implements Initializable {
if(FileType.JSON.equals(fileType)) {
wallet = Storage.getStorage().loadWallet(file);
} else if(FileType.BINARY.equals(fileType)) {
Optional<ECKey> optionalFullKey = SettingsController.askForWalletPassword(null, true);
if(!optionalFullKey.isPresent()) {
WalletPasswordDialog dlg = new WalletPasswordDialog(WalletPasswordDialog.PasswordRequirement.LOAD);
Optional<String> password = dlg.showAndWait();
if(!password.isPresent()) {
return;
}
ECKey encryptionFullKey = optionalFullKey.get();
ECKey encryptionFullKey = ECIESKeyCrypter.deriveECKey(password.get());
wallet = Storage.getStorage().loadWallet(file, encryptionFullKey);
encryptionPubKey = ECKey.fromPublicOnly(encryptionFullKey);
} else {
@ -247,8 +249,9 @@ public class AppController implements Initializable {
Tab tab = addWalletTab(file, encryptionPubKey, wallet);
tabs.getSelectionModel().select(tab);
} catch (InvalidPasswordException e) {
showErrorDialog("Invalid Password", "The password was invalid.");
} catch (Exception e) {
e.printStackTrace();
showErrorDialog("Error opening wallet", e.getMessage());
}
}

View file

@ -1,7 +1,7 @@
package com.sparrowwallet.sparrow.control;
import com.google.gson.JsonParseException;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.crypto.InvalidPasswordException;
import com.sparrowwallet.drongo.wallet.Keystore;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.EventManager;
@ -83,7 +83,7 @@ public class FileKeystoreImportPane extends KeystoreImportPane {
if(e.getCause() != null && e.getCause().getMessage() != null && !e.getCause().getMessage().isEmpty()) {
errorMessage = e.getCause().getMessage();
}
if(e instanceof ECIESKeyCrypter.InvalidPasswordException || e.getCause() instanceof ECIESKeyCrypter.InvalidPasswordException) {
if(e instanceof InvalidPasswordException || e.getCause() instanceof InvalidPasswordException) {
errorMessage = "Invalid wallet password";
}
if(e instanceof JsonParseException || e.getCause() instanceof JsonParseException) {

View file

@ -159,7 +159,7 @@ public class SettingsController extends WalletFormController implements Initiali
apply.setOnAction(event -> {
try {
Optional<ECKey> optionalPubKey = askForWalletPassword(walletForm.getEncryptionPubKey(), false);
Optional<ECKey> optionalPubKey = askForWalletPassword(walletForm.getEncryptionPubKey());
if(optionalPubKey.isPresent()) {
walletForm.setEncryptionPubKey(ECKey.fromPublicOnly(optionalPubKey.get()));
walletForm.save();
@ -261,11 +261,9 @@ public class SettingsController extends WalletFormController implements Initiali
Platform.runLater(() -> apply.setDisable(!tabsValidate()));
}
public static Optional<ECKey> askForWalletPassword(ECKey existingPubKey, boolean required) {
private Optional<ECKey> askForWalletPassword(ECKey existingPubKey) {
WalletPasswordDialog.PasswordRequirement requirement;
if(existingPubKey == null && required) {
requirement = WalletPasswordDialog.PasswordRequirement.LOAD;
} else if(existingPubKey == null) {
if(existingPubKey == null) {
requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_NEW;
} else if(WalletForm.NO_PASSWORD_KEY.equals(existingPubKey)) {
requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_EMPTY;
@ -276,7 +274,7 @@ public class SettingsController extends WalletFormController implements Initiali
WalletPasswordDialog dlg = new WalletPasswordDialog(requirement);
Optional<String> password = dlg.showAndWait();
if(password.isPresent()) {
if(!required && password.get().isEmpty()) {
if(password.get().isEmpty()) {
return Optional.of(WalletForm.NO_PASSWORD_KEY);
}