diff --git a/convex-cli/src/main/java/convex/cli/Main.java b/convex-cli/src/main/java/convex/cli/Main.java index 0b9531ba0..3f6b19f01 100644 --- a/convex-cli/src/main/java/convex/cli/Main.java +++ b/convex-cli/src/main/java/convex/cli/Main.java @@ -229,8 +229,7 @@ public char[] getStorePassword() { storepass=this.keystorePassword.toCharArray(); } else { if (!nonInteractive) { - Console console = System.console(); - storepass= console.readPassword("Enter Keystore Password: "); + storepass= readPassword("Enter Keystore Password: "); } if (storepass==null) { @@ -252,8 +251,7 @@ public char[] getKeyPassword() { keypass=this.keystorePassword.toCharArray(); } else { if (!nonInteractive) { - Console console = System.console(); - keypass= console.readPassword("Private Key Encryption Password: "); + keypass= readPassword("Private Key Encryption Password: "); } if (keypass==null) { @@ -451,5 +449,12 @@ public boolean isInteractive() { return !nonInteractive; } + public char[] readPassword(String prompt) { + Console c=System.console(); + if (c==null) throw new CLIError("Unable to get user input because console is unavaiable."); + + return c.readPassword(prompt); + } + } diff --git a/convex-cli/src/main/java/convex/cli/key/KeyGenerate.java b/convex-cli/src/main/java/convex/cli/key/KeyGenerate.java index 94379d910..b35b99be0 100644 --- a/convex-cli/src/main/java/convex/cli/key/KeyGenerate.java +++ b/convex-cli/src/main/java/convex/cli/key/KeyGenerate.java @@ -1,5 +1,6 @@ package convex.cli.key; +import java.io.Console; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.NoSuchAlgorithmException; @@ -9,6 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import convex.cli.CLIError; import convex.cli.Constants; import convex.core.crypto.AKeyPair; import convex.core.crypto.BIP39; @@ -45,19 +47,20 @@ public class KeyGenerate extends AKeyCommand { private boolean bip39; @Option(names="--passphrase", - description="BIP39 optional passphrase") + description="BIP39 passphrase. If not provided, will be requested from user (or assumed blank in non-interactive mode).") private String passphrase; - private AKeyPair generateKeyPair() { try { if (bip39) { String mnemonic=BIP39.createSecureRandom(12); cli().println(mnemonic); - if (cli().isInteractive()) { - passphrase=new String(System.console().readPassword("Enter BIP39 passphrase: ")); - } else { - if (passphrase==null) passphrase=""; + if (passphrase==null) { + if (cli().isInteractive()) { + passphrase=new String(cli().readPassword("Enter BIP39 passphrase: ")); + } else { + passphrase=""; + } } Blob bipseed; bipseed = BIP39.getSeed(mnemonic, passphrase);