diff --git a/core/src/main/java/bisq/core/api/CoreWalletsService.java b/core/src/main/java/bisq/core/api/CoreWalletsService.java index 29394aad8ac..fc15ec5062a 100644 --- a/core/src/main/java/bisq/core/api/CoreWalletsService.java +++ b/core/src/main/java/bisq/core/api/CoreWalletsService.java @@ -23,6 +23,9 @@ import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.WalletsManager; +import bisq.common.Timer; +import bisq.common.UserThread; + import org.bitcoinj.core.Address; import org.bitcoinj.core.TransactionConfidence; import org.bitcoinj.crypto.KeyCrypterScrypt; @@ -37,8 +40,6 @@ import java.util.List; import java.util.Optional; -import java.util.Timer; -import java.util.TimerTask; import java.util.function.Function; import java.util.stream.Collectors; @@ -57,7 +58,7 @@ class CoreWalletsService { private final BtcWalletService btcWalletService; @Nullable - private TimerTask lockTask; + private Timer lockTimer; @Nullable private KeyParameter tempAesKey; @@ -190,29 +191,22 @@ void unlockWallet(String password, long timeout) { if (!walletsManager.checkAESKey(tempAesKey)) throw new IllegalStateException("incorrect password"); - if (lockTask != null) { - // The user is overriding a prior unlock timeout. Cancel the existing - // lock TimerTask to prevent it from calling lockWallet() before or after the - // new timer task does. - lockTask.cancel(); - // Avoid the synchronized(lock) overhead of an unnecessary lockTask.cancel() - // call the next time 'unlockwallet' is called. - lockTask = null; + if (lockTimer != null) { + // The user has called unlockwallet again, before the prior unlockwallet + // timeout has expired. He's overriding it with a new timeout value. + // Remove the existing lock timer to prevent it from calling lockwallet + // before or after the new one does. + lockTimer.stop(); + lockTimer = null; } - lockTask = new TimerTask() { - @Override - public void run() { - if (tempAesKey != null) { - // Do not try to lock wallet after timeout if the user has already - // done so via 'lockwallet' - log.info("Locking wallet after {} second timeout expired.", timeout); - tempAesKey = null; - } + lockTimer = UserThread.runAfter(() -> { + if (tempAesKey != null) { + // The unlockwallet timeout has expired; re-lock the wallet. + log.info("Locking wallet after {} second timeout expired.", timeout); + tempAesKey = null; } - }; - Timer timer = new Timer("Lock Wallet Timer"); - timer.schedule(lockTask, SECONDS.toMillis(timeout)); + }, timeout, SECONDS); } // Provided for automated wallet protection method testing, despite the