Factor out two small duplcated code blocks

This commit is contained in:
ghubstan 2020-05-06 12:30:59 -03:00
parent 9b156b86dd
commit fbb025adb1
No known key found for this signature in database
GPG key ID: E35592D6800A861E

View file

@ -51,9 +51,7 @@ class CoreWalletService {
if (!walletsManager.areWalletsAvailable()) if (!walletsManager.areWalletsAvailable())
throw new IllegalStateException("wallet is not yet available"); throw new IllegalStateException("wallet is not yet available");
KeyCrypterScrypt keyCrypterScrypt = walletsManager.getKeyCrypterScrypt(); KeyCrypterScrypt keyCrypterScrypt = getKeyCrypterScrypt();
if (keyCrypterScrypt == null)
throw new IllegalStateException("wallet encrypter is not available");
if (newPassword != null && !newPassword.isEmpty()) { if (newPassword != null && !newPassword.isEmpty()) {
// TODO Validate new password before replacing old password. // TODO Validate new password before replacing old password.
@ -108,15 +106,8 @@ class CoreWalletService {
// Provided for automated wallet protection method testing, despite the // Provided for automated wallet protection method testing, despite the
// security risks exposed by providing users the ability to decrypt their wallets. // security risks exposed by providing users the ability to decrypt their wallets.
public void removeWalletPassword(String password) { public void removeWalletPassword(String password) {
if (!walletsManager.areWalletsAvailable()) verifyWalletIsAvailableAndEncrypted();
throw new IllegalStateException("wallet is not yet available"); KeyCrypterScrypt keyCrypterScrypt = getKeyCrypterScrypt();
if (!walletsManager.areWalletsEncrypted())
throw new IllegalStateException("wallet is not encrypted with a password");
KeyCrypterScrypt keyCrypterScrypt = walletsManager.getKeyCrypterScrypt();
if (keyCrypterScrypt == null)
throw new IllegalStateException("wallet encrypter is not available");
KeyParameter aesKey = keyCrypterScrypt.deriveKey(password); KeyParameter aesKey = keyCrypterScrypt.deriveKey(password);
if (!walletsManager.checkAESKey(aesKey)) if (!walletsManager.checkAESKey(aesKey))
@ -124,4 +115,20 @@ class CoreWalletService {
walletsManager.decryptWallets(aesKey); walletsManager.decryptWallets(aesKey);
} }
// Throws a RuntimeException if wallets are not available or not encrypted.
private void verifyWalletIsAvailableAndEncrypted() {
if (!walletsManager.areWalletsAvailable())
throw new IllegalStateException("wallet is not yet available");
if (!walletsManager.areWalletsEncrypted())
throw new IllegalStateException("wallet is not encrypted with a password");
}
private KeyCrypterScrypt getKeyCrypterScrypt() {
KeyCrypterScrypt keyCrypterScrypt = walletsManager.getKeyCrypterScrypt();
if (keyCrypterScrypt == null)
throw new IllegalStateException("wallet encrypter is not available");
return keyCrypterScrypt;
}
} }