Uncomment wallet password prompt, and validate input during startup

- When bot is run from a *nix terminal, password input will not be echoed.
- When bot is run from an IDE, it's virtual terminal will echo the input
  (cannot be avoided).

Validate the input during startup, to fail early instead of waiting for
the right offer to take, then failing to unlock the wallet with an
incorrect password.
This commit is contained in:
ghubstan 2022-06-25 11:00:28 -03:00
parent d08dc57ab8
commit b0b6c9c771
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
7 changed files with 32 additions and 14 deletions

View File

@ -326,6 +326,27 @@ public abstract class AbstractBot {
}
}
/**
* Attempt to unlock the wallet for 1 second using the given password, and shut down the bot if the
* password check fails for any reason.
*
* @param walletPassword String API daemon's wallet password
*/
protected void validateWalletPassword(String walletPassword) {
try {
var request = UnlockWalletRequest.newBuilder()
.setPassword(walletPassword)
.setTimeout(1)
.build();
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.unlockWallet(request);
} catch (StatusRuntimeException grpcException) {
log.error("Wallet password check failed.");
log.error((toCleanErrorMessage.apply(grpcException)));
exit(1);
}
}
/**
* Returns PaymentAccount for given paymentAccountId, else throws an IllegalArgumentException.
*

View File

@ -253,7 +253,7 @@ public class BotUtils {
*/
public static String readWalletPassword(String prompt) {
String walletPassword;
var console = console(); // Returns null in IDE console!
var console = console();
// System.console() returns null if you do not launch your java application with a real console.
if (console == null) {
// Have to read it in a less secure way in the IDE's virtual console.

View File

@ -237,7 +237,7 @@ class OfferTaker {
+ " Shut down the API bot and server, then check the server log."));
}
}
/**
* Wait and block until a new trade is fully initialized, with a trade contract and the user's trade role.
* <p>

View File

@ -262,9 +262,8 @@ public class TakeBestPricedOfferToBuyBsq extends AbstractBot {
public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ " Please enter your wallet password:";
String walletPassword = "be careful"; // readWalletPassword(prompt);
log.info("Your wallet password is {}", walletPassword.isBlank() ? "blank" : walletPassword);
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToBuyBsq bot = new TakeBestPricedOfferToBuyBsq(appendWalletPasswordOpt(args, walletPassword));
bot.run();
}

View File

@ -133,6 +133,7 @@ public class TakeBestPricedOfferToBuyBtc extends AbstractBot {
public void run() {
var startTime = new Date().getTime();
validatePollingInterval(pollingInterval);
validateWalletPassword(walletPassword);
validateTradeFeeCurrencyCode(bisqTradeFeeCurrency);
validatePaymentAccount(paymentAccount);
printBotConfiguration();
@ -329,9 +330,8 @@ public class TakeBestPricedOfferToBuyBtc extends AbstractBot {
public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ " Please enter your wallet password:";
String walletPassword = "be careful"; // readWalletPassword(prompt);
log.info("Your wallet password is {}", walletPassword.isBlank() ? "blank" : walletPassword);
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToBuyBtc bot = new TakeBestPricedOfferToBuyBtc(appendWalletPasswordOpt(args, walletPassword));
bot.run();
}

View File

@ -262,9 +262,8 @@ public class TakeBestPricedOfferToSellBsq extends AbstractBot {
public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ " Please enter your wallet password:";
String walletPassword = "be careful"; // readWalletPassword(prompt);
log.info("Your wallet password is {}", walletPassword.isBlank() ? "blank" : walletPassword);
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToSellBsq bot = new TakeBestPricedOfferToSellBsq(appendWalletPasswordOpt(args, walletPassword));
bot.run();
}

View File

@ -330,9 +330,8 @@ public class TakeBestPricedOfferToSellBtc extends AbstractBot {
public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ " Please enter your wallet password:";
String walletPassword = "be careful"; // readWalletPassword(prompt);
log.info("Your wallet password is {}", walletPassword.isBlank() ? "blank" : walletPassword);
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToSellBtc bot = new TakeBestPricedOfferToSellBtc(appendWalletPasswordOpt(args, walletPassword));
bot.run();
}