From fbb025adb1abfd653fb1aec0ccfa5e739c2640ce Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Wed, 6 May 2020 12:30:59 -0300 Subject: [PATCH] Factor out two small duplcated code blocks --- .../bisq/core/grpc/CoreWalletService.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/CoreWalletService.java b/core/src/main/java/bisq/core/grpc/CoreWalletService.java index 7c77c55bfb..d7e460ed1b 100644 --- a/core/src/main/java/bisq/core/grpc/CoreWalletService.java +++ b/core/src/main/java/bisq/core/grpc/CoreWalletService.java @@ -51,9 +51,7 @@ class CoreWalletService { if (!walletsManager.areWalletsAvailable()) throw new IllegalStateException("wallet is not yet available"); - KeyCrypterScrypt keyCrypterScrypt = walletsManager.getKeyCrypterScrypt(); - if (keyCrypterScrypt == null) - throw new IllegalStateException("wallet encrypter is not available"); + KeyCrypterScrypt keyCrypterScrypt = getKeyCrypterScrypt(); if (newPassword != null && !newPassword.isEmpty()) { // TODO Validate new password before replacing old password. @@ -108,15 +106,8 @@ class CoreWalletService { // Provided for automated wallet protection method testing, despite the // security risks exposed by providing users the ability to decrypt their wallets. public void removeWalletPassword(String password) { - if (!walletsManager.areWalletsAvailable()) - throw new IllegalStateException("wallet is not yet available"); - - 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"); + verifyWalletIsAvailableAndEncrypted(); + KeyCrypterScrypt keyCrypterScrypt = getKeyCrypterScrypt(); KeyParameter aesKey = keyCrypterScrypt.deriveKey(password); if (!walletsManager.checkAESKey(aesKey)) @@ -124,4 +115,20 @@ class CoreWalletService { 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; + } }