Skip to content

Commit

Permalink
More CLI BIP39 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 18, 2024
1 parent 50b685b commit 343e9a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
13 changes: 9 additions & 4 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}


}
15 changes: 9 additions & 6 deletions convex-cli/src/main/java/convex/cli/key/KeyGenerate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package convex.cli.key;

import java.io.Console;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 343e9a6

Please sign in to comment.