Skip to content

Commit

Permalink
Use Bisq's timer in API's 'unlockwallet timeout(s)'
Browse files Browse the repository at this point in the history
This change was requested in
chimp1984@961703e

This replaces and closes PR #4558
  • Loading branch information
ghubstan committed Oct 31, 2020
1 parent 3cd3bf0 commit c27d5a6
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions core/src/main/java/bisq/core/api/CoreWalletsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -57,7 +58,7 @@ class CoreWalletsService {
private final BtcWalletService btcWalletService;

@Nullable
private TimerTask lockTask;
private Timer lockTimer;

@Nullable
private KeyParameter tempAesKey;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c27d5a6

Please sign in to comment.