Skip to content

Commit

Permalink
Improve CLI option docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 10, 2024
1 parent 1df5837 commit 8c81469
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import convex.cli.mixins.AddressMixin;
import convex.cli.mixins.KeyMixin;
import convex.cli.mixins.RemotePeerMixin;
import convex.cli.mixins.StoreMixin;
import convex.cli.mixins.KeyStoreMixin;
import convex.core.crypto.AKeyPair;
import convex.core.data.AccountKey;
import convex.core.data.Address;
Expand All @@ -17,7 +17,7 @@
public abstract class AClientCommand extends ATopCommand {

@Mixin
protected StoreMixin storeMixin;
protected KeyStoreMixin storeMixin;

@Mixin
protected KeyMixin keyMixin;
Expand Down
8 changes: 2 additions & 6 deletions convex-cli/src/main/java/convex/cli/key/AKeyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import convex.cli.ACommand;
import convex.cli.Main;
import convex.cli.mixins.KeyMixin;
import convex.cli.mixins.StoreMixin;
import convex.cli.mixins.KeyStoreMixin;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.ParentCommand;

Expand All @@ -16,10 +15,7 @@ public abstract class AKeyCommand extends ACommand {
protected Key keyParent;

@Mixin
protected StoreMixin storeMixin;

@Mixin
protected KeyMixin keyMixin;
protected KeyStoreMixin storeMixin;


@Override
Expand Down
11 changes: 9 additions & 2 deletions convex-cli/src/main/java/convex/cli/key/KeyExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import org.slf4j.LoggerFactory;

import convex.cli.CLIError;
import convex.cli.mixins.KeyMixin;
import convex.core.crypto.AKeyPair;
import convex.core.crypto.PEMTools;
import convex.core.util.FileUtils;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;

Expand All @@ -33,6 +35,11 @@ public class KeyExport extends AKeyCommand {
@ParentCommand
protected Key keyParent;


@Mixin
protected KeyMixin keyMixin;


@Option(names={"-o", "--output-file"},
description="Output file for the private key. Use '-' for STDOUT (default).")
private String outputFilename;
Expand Down Expand Up @@ -94,8 +101,8 @@ public void run() {
if ("pem".equals(type)) {
ensureExportPassword();
try {
String pemText = PEMTools.encryptPrivateKeyToPEM(keyPair, exportPassword.toCharArray());
output=pemText;
String pemText = PEMTools.encryptPrivateKeyToPEM(keyPair, exportPassword.toCharArray());
output=pemText;
} catch (Exception e) {
throw new CLIError("Cannot encrypt PEM",e);
}
Expand Down
16 changes: 14 additions & 2 deletions convex-cli/src/main/java/convex/cli/key/KeyGenerate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import convex.core.data.Hash;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ScopeType;


/**
Expand Down Expand Up @@ -49,6 +50,12 @@ public class KeyGenerate extends AKeyCommand {
@Option(names="--passphrase",
description="BIP39 passphrase. If not provided, will be requested from user (or assumed blank in non-interactive mode).")
private String passphrase;

@Option(names = { "-p","--keypass" },
defaultValue = "${env:CONVEX_KEY_PASSWORD}",
scope = ScopeType.INHERIT,
description = "Key pair password for generated key. Can specify with CONVEX_KEY_PASSWORD.")
protected char[] keyPassword;

private AKeyPair generateKeyPair() {
if ("bip39".equals(type)) {
Expand Down Expand Up @@ -109,9 +116,14 @@ public void run() {

String publicKeyHexString = kp.getAccountKey().toHexString();
storeMixin.ensureKeyStore();
char[] keyPassword=keyMixin.getKeyPassword();

inform("Generated key pair with public key: 0x"+kp.getAccountKey().toChecksumHex());

if (keyPassword==null) {
keyPassword=readPassword("Enter password for generated key: ");
}

storeMixin.addKeyPairToStore(kp, keyPassword);
inform ("Public key added to store: 0x"+kp.getAccountKey().toChecksumHex());
println(publicKeyHexString); // Output generated public key
Arrays.fill(keyPassword, 'p');
}
Expand Down
14 changes: 13 additions & 1 deletion convex-cli/src/main/java/convex/cli/key/KeyImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParentCommand;
import picocli.CommandLine.ScopeType;


/**
Expand Down Expand Up @@ -47,6 +48,12 @@ public class KeyImport extends AKeyCommand {
@Option(names={"--type"},
description="Type of file imported. Supports: pem, seed, bip39. Will attempt to autodetect unless strict security is enabled")
private String type;

@Option(names = { "-p","--keypass" },
defaultValue = "${env:CONVEX_KEY_PASSWORD}",
scope = ScopeType.INHERIT,
description = "Key pair password for generated key. Can specify with CONVEX_KEY_PASSWORD.")
protected char[] keyPassword;

/**
* Import key pair
Expand Down Expand Up @@ -121,11 +128,16 @@ public void run() {
AKeyPair keyPair=importKeyPair();
if (keyPair==null) return; // returning without failure, presumably usage to show or otherwise cancelled

// Get password for key
if (keyPassword==null) {
keyPassword=readPassword("Enter password for imported key: ");
}

// Finally write to store
char[] keyPassword=keyMixin.getKeyPassword();
if (storeMixin.loadKeyStore()==null) {
throw new CLIError("Key store specified for import does not exist");
}

storeMixin.addKeyPairToStore(keyPair,keyPassword);
Arrays.fill(keyPassword, 'x');
storeMixin.saveKeyStore();
Expand Down
4 changes: 2 additions & 2 deletions convex-cli/src/main/java/convex/cli/local/ALocalCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import convex.cli.ACommand;
import convex.cli.Main;
import convex.cli.mixins.KeyMixin;
import convex.cli.mixins.StoreMixin;
import convex.cli.mixins.KeyStoreMixin;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.ParentCommand;

public abstract class ALocalCommand extends ACommand {

@Mixin
protected StoreMixin storeMixin;
protected KeyStoreMixin storeMixin;

@Mixin
protected KeyMixin keyMixin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public class ClientKeyMixin {
public KeyMixin keyMixin;

@Mixin
public StoreMixin storeMixin;
public KeyStoreMixin storeMixin;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import picocli.CommandLine.Option;
import picocli.CommandLine.ScopeType;

public class StoreMixin extends AMixin {
public class KeyStoreMixin extends AMixin {

/**
* Keystore option
Expand All @@ -43,7 +43,7 @@ public class StoreMixin extends AMixin {

KeyStore keyStore = null;

static Logger log = LoggerFactory.getLogger(StoreMixin.class);
static Logger log = LoggerFactory.getLogger(KeyStoreMixin.class);

/**
* Gets the keystore file name currently used for the CLI
Expand Down
4 changes: 2 additions & 2 deletions convex-cli/src/main/java/convex/cli/peer/APeerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import convex.cli.mixins.EtchMixin;
import convex.cli.mixins.KeyMixin;
import convex.cli.mixins.PeerKeyMixin;
import convex.cli.mixins.StoreMixin;
import convex.cli.mixins.KeyStoreMixin;
import convex.core.crypto.AKeyPair;
import etch.EtchStore;
import picocli.CommandLine.Mixin;
Expand All @@ -25,7 +25,7 @@ public abstract class APeerCommand extends ACommand {
protected PeerKeyMixin peerKeyMixin;

@Mixin
protected StoreMixin storeMixin;
protected KeyStoreMixin storeMixin;


@ParentCommand
Expand Down

0 comments on commit 8c81469

Please sign in to comment.