diff --git a/core/src/main/java/bisq/core/grpc/ApiStatus.java b/core/src/main/java/bisq/core/grpc/ApiStatus.java index a241ba75a51..675aaf51a9b 100644 --- a/core/src/main/java/bisq/core/grpc/ApiStatus.java +++ b/core/src/main/java/bisq/core/grpc/ApiStatus.java @@ -28,21 +28,14 @@ */ enum ApiStatus { - INCORRECT_OLD_WALLET_PASSWORD(Status.INVALID_ARGUMENT, "incorrect old password"), INCORRECT_WALLET_PASSWORD(Status.INVALID_ARGUMENT, "incorrect password"), - INTERNAL(Status.INTERNAL, "internal server error"), - OK(Status.OK, null), - UNKNOWN(Status.UNKNOWN, "unknown"), - WALLET_ALREADY_LOCKED(Status.FAILED_PRECONDITION, "wallet is already locked"), WALLET_ENCRYPTER_NOT_AVAILABLE(Status.FAILED_PRECONDITION, "wallet encrypter is not available"), - WALLET_IS_ENCRYPTED(Status.FAILED_PRECONDITION, "wallet is encrypted with a password"), - WALLET_NOT_ENCRYPTED(Status.FAILED_PRECONDITION, "wallet is not encrypted with a password"), WALLET_NOT_AVAILABLE(Status.UNAVAILABLE, "wallet is not available"); diff --git a/core/src/main/java/bisq/core/grpc/CoreWalletService.java b/core/src/main/java/bisq/core/grpc/CoreWalletService.java index 4aa4fef25d2..36e47195d22 100644 --- a/core/src/main/java/bisq/core/grpc/CoreWalletService.java +++ b/core/src/main/java/bisq/core/grpc/CoreWalletService.java @@ -50,51 +50,45 @@ public long getAvailableBalance() { return balance.getValue(); } - public Tuple2 setWalletPassword(String password, String newPassword) { + public void setWalletPassword(String password, String newPassword) { try { if (!walletsManager.areWalletsAvailable()) - return new Tuple2<>(false, WALLET_NOT_AVAILABLE); + throw new IllegalStateException("wallet is not yet available"); KeyCrypterScrypt keyCrypterScrypt = walletsManager.getKeyCrypterScrypt(); if (keyCrypterScrypt == null) - return new Tuple2<>(false, WALLET_ENCRYPTER_NOT_AVAILABLE); + throw new IllegalStateException("wallet encrypter is not available"); if (newPassword != null && !newPassword.isEmpty()) { // TODO Validate new password before replacing old password. if (!walletsManager.areWalletsEncrypted()) - return new Tuple2<>(false, WALLET_NOT_ENCRYPTED); + throw new IllegalStateException("wallet is not encrypted with a password"); KeyParameter aesKey = keyCrypterScrypt.deriveKey(password); if (!walletsManager.checkAESKey(aesKey)) - return new Tuple2<>(false, INCORRECT_OLD_WALLET_PASSWORD); + throw new IllegalStateException("incorrect old password"); walletsManager.decryptWallets(aesKey); aesKey = keyCrypterScrypt.deriveKey(newPassword); walletsManager.encryptWallets(keyCrypterScrypt, aesKey); - return new Tuple2<>(true, OK); } if (walletsManager.areWalletsEncrypted()) - return new Tuple2<>(false, WALLET_IS_ENCRYPTED); + throw new IllegalStateException("wallet is encrypted with a password"); // TODO Validate new password. KeyParameter aesKey = keyCrypterScrypt.deriveKey(password); walletsManager.encryptWallets(keyCrypterScrypt, aesKey); - return new Tuple2<>(true, OK); } catch (Throwable t) { - // TODO Derive new ApiStatus codes from server stack traces. t.printStackTrace(); - return new Tuple2<>(false, INTERNAL); + throw new IllegalStateException(t); } } public Tuple2 lockWallet() { if (tempLockWalletPassword != null) { - Tuple2 encrypted = setWalletPassword(tempLockWalletPassword, null); + setWalletPassword(tempLockWalletPassword, null); tempLockWalletPassword = null; - if (!encrypted.second.equals(OK)) - return encrypted; - return new Tuple2<>(true, OK); } return new Tuple2<>(false, WALLET_ALREADY_LOCKED); diff --git a/core/src/main/java/bisq/core/grpc/GrpcWalletService.java b/core/src/main/java/bisq/core/grpc/GrpcWalletService.java index a9766154274..fc301c5b464 100644 --- a/core/src/main/java/bisq/core/grpc/GrpcWalletService.java +++ b/core/src/main/java/bisq/core/grpc/GrpcWalletService.java @@ -44,16 +44,16 @@ public void getBalance(GetBalanceRequest req, StreamObserver re @Override public void setWalletPassword(SetWalletPasswordRequest req, StreamObserver responseObserver) { - var result = walletService.setWalletPassword(req.getPassword(), req.getNewPassword()); - if (!result.second.equals(ApiStatus.OK)) { - StatusRuntimeException ex = new StatusRuntimeException(result.second.getGrpcStatus() - .withDescription(result.second.getDescription())); + try { + walletService.setWalletPassword(req.getPassword(), req.getNewPassword()); + var reply = SetWalletPasswordReply.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 = SetWalletPasswordReply.newBuilder().build(); - responseObserver.onNext(reply); - responseObserver.onCompleted(); } @Override