Return void vs Tuple2 from CoreWalletService#removeWalletPassword

Like the change in commits 163061a and 9164579, removeWalletPassword
now throws an IllegalStateException with an appropriate message if
the wallet password cannot be removed.

Also deletes unused StatusApi enums.
This commit is contained in:
ghubstan 2020-05-03 13:51:56 -03:00
parent 916457963f
commit feafd0c983
No known key found for this signature in database
GPG key ID: E35592D6800A861E
3 changed files with 16 additions and 25 deletions

View file

@ -28,16 +28,9 @@ import io.grpc.Status;
*/
enum ApiStatus {
INCORRECT_WALLET_PASSWORD(Status.INVALID_ARGUMENT, "incorrect password"),
OK(Status.OK, null),
WALLET_ALREADY_LOCKED(Status.FAILED_PRECONDITION, "wallet is already locked"),
WALLET_ENCRYPTER_NOT_AVAILABLE(Status.FAILED_PRECONDITION, "wallet encrypter is not available"),
WALLET_NOT_ENCRYPTED(Status.FAILED_PRECONDITION, "wallet is not encrypted with a password"),
WALLET_NOT_AVAILABLE(Status.UNAVAILABLE, "wallet is not available");
WALLET_ALREADY_LOCKED(Status.FAILED_PRECONDITION, "wallet is already locked");
private final Status grpcStatus;

View file

@ -18,7 +18,8 @@ import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
import static bisq.core.grpc.ApiStatus.*;
import static bisq.core.grpc.ApiStatus.OK;
import static bisq.core.grpc.ApiStatus.WALLET_ALREADY_LOCKED;
import static java.util.concurrent.TimeUnit.SECONDS;
@Slf4j
@ -95,9 +96,7 @@ class CoreWalletService {
}
public Tuple2<Boolean, ApiStatus> unlockWallet(String password, long timeout) {
Tuple2<Boolean, ApiStatus> decrypted = removeWalletPassword(password);
if (!decrypted.second.equals(OK))
return decrypted;
removeWalletPassword(password);
TimerTask timerTask = new TimerTask() {
@Override
@ -118,22 +117,21 @@ class CoreWalletService {
// Provided for automated wallet protection method testing, despite the
// security risks exposed by providing users the ability to decrypt their wallets.
public Tuple2<Boolean, ApiStatus> removeWalletPassword(String password) {
public void removeWalletPassword(String password) {
if (!walletsManager.areWalletsAvailable())
return new Tuple2<>(false, WALLET_NOT_AVAILABLE);
throw new IllegalStateException("wallet is not yet available");
if (!walletsManager.areWalletsEncrypted())
return new Tuple2<>(false, WALLET_NOT_ENCRYPTED);
throw new IllegalStateException("wallet is not encrypted with a password");
KeyCrypterScrypt keyCrypterScrypt = walletsManager.getKeyCrypterScrypt();
if (keyCrypterScrypt == null)
return new Tuple2<>(false, WALLET_ENCRYPTER_NOT_AVAILABLE);
throw new IllegalStateException("wallet encrypter is not available");
KeyParameter aesKey = keyCrypterScrypt.deriveKey(password);
if (!walletsManager.checkAESKey(aesKey))
return new Tuple2<>(false, INCORRECT_WALLET_PASSWORD);
throw new IllegalStateException("incorrect password");
walletsManager.decryptWallets(aesKey);
return new Tuple2<>(true, OK);
}
}

View file

@ -59,16 +59,16 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
@Override
public void removeWalletPassword(RemoveWalletPasswordRequest req,
StreamObserver<RemoveWalletPasswordReply> responseObserver) {
var result = walletService.removeWalletPassword(req.getPassword());
if (!result.second.equals(ApiStatus.OK)) {
StatusRuntimeException ex = new StatusRuntimeException(result.second.getGrpcStatus()
.withDescription(result.second.getDescription()));
try {
walletService.removeWalletPassword(req.getPassword());
var reply = RemoveWalletPasswordReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalStateException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
var reply = RemoveWalletPasswordReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
@Override