Skip to content

Commit

Permalink
More CLI refactoring towards full mixin model
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 1, 2024
1 parent 98e2424 commit 53a88e3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 58 deletions.
5 changes: 1 addition & 4 deletions convex-cli/src/main/java/convex/cli/ACommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import picocli.CommandLine;

/**
* Base class for Convex CLI commands
* Base class for Convex CLI command components and mixins
*/
public abstract class ACommand implements Runnable {

Expand Down Expand Up @@ -153,7 +153,4 @@ public void inform(String message) {
inform(1,message);
}




}
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/AccountCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void run() {
AKeyPair keyPair = null;

if (!keystorePublicKey.isEmpty()) {
keyPair = mainParent.loadKeyFromStore(keystorePublicKey);
keyPair = mainParent.storeMixin.loadKeyFromStore(mainParent, keystorePublicKey);
if (keyPair == null) {
throw new CLIError("Cannot find the provided public key in keystore: "+keystorePublicKey);
}
Expand Down
49 changes: 1 addition & 48 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.slf4j.Logger;
Expand All @@ -23,7 +21,6 @@
import convex.cli.output.Coloured;
import convex.cli.peer.Peer;
import convex.core.crypto.AKeyPair;
import convex.core.crypto.PFXTools;
import convex.core.exceptions.TODOException;
import convex.core.util.Utils;
import picocli.CommandLine;
Expand Down Expand Up @@ -54,7 +51,7 @@
description = "Convex Command Line Interface")

public class Main extends ACommand {
static Logger log = LoggerFactory.getLogger(Main.class);
private static Logger log = LoggerFactory.getLogger(Main.class);

public CommandLine commandLine = new CommandLine(this);

Expand Down Expand Up @@ -240,50 +237,6 @@ public char[] getKeyPassword() {
}


/**
* Loads a keypair from configured keystore
*
* @param publicKey String identifying the public key. May be a prefix
* @return Keypair instance, or null if not found
*/
public AKeyPair loadKeyFromStore(String publicKey) {
if (publicKey == null)
return null;

AKeyPair keyPair = null;

publicKey = publicKey.trim();
publicKey = publicKey.toLowerCase().replaceFirst("^0x", "").strip();
if (publicKey.isEmpty()) {
return null;
}

char[] storePassword = storeMixin.getStorePassword();

File keyFile = storeMixin.getKeyStoreFile();
try {
if (!keyFile.exists()) {
throw new CLIError("Cannot find keystore file " + keyFile.getCanonicalPath());
}
KeyStore keyStore = PFXTools.loadStore(keyFile, storePassword);

Enumeration<String> aliases = keyStore.aliases();

while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (alias.indexOf(publicKey) == 0) {
log.trace("found keypair " + alias);
keyPair = PFXTools.getKeyPair(keyStore, alias, getKeyPassword());
break;
}
}
} catch (Exception t) {
throw new CLIError("Cannot load key store", t);
}

return keyPair;
}

/**
* Generate key pairs and add to store. Does not save store!
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected boolean ensureKeyPair(Convex convex) {
}


keyPair=mainParent.loadKeyFromStore(pk);
keyPair=mainParent.storeMixin.loadKeyFromStore(mainParent, pk);
if (keyPair==null) {
// We didn't find required keypair
throw new CLIError("Can't find keypair with public key "+pk+" for Address "+address);
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/key/KeyExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void run() {
}

String publicKey = keystorePublicKey;
AKeyPair keyPair = cli().loadKeyFromStore(publicKey);
AKeyPair keyPair = cli().storeMixin.loadKeyFromStore(cli(), publicKey);
if (keyPair==null) {
// TODO: maybe prompt?
throw new CLIError("Key pair not found for key: "+keystorePublicKey);
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/local/LocalStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private List<AKeyPair> getPeerKeyPairs(int n) {
String keyPrefix = values.get(index);
if (keyPrefix.isBlank()) continue;

AKeyPair keyPair = mainParent.loadKeyFromStore(keyPrefix);
AKeyPair keyPair = mainParent.storeMixin.loadKeyFromStore(mainParent, keyPrefix);
if (keyPair == null) {
log.warn("Unable to find public key in store: "+keyPrefix);
} else {
Expand Down
47 changes: 47 additions & 0 deletions convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.UnrecoverableKeyException;
import java.util.Enumeration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.cli.CLIError;
import convex.cli.Constants;
import convex.cli.Main;
import convex.core.crypto.AKeyPair;
import convex.core.crypto.PFXTools;
import convex.core.util.Utils;
Expand Down Expand Up @@ -171,4 +173,49 @@ public void addKeyPairToStore(AKeyPair keyPair, char[] keyPassword) {

}

/**
* Loads a keypair from configured keystore
*
* @param main TODO
* @param publicKey String identifying the public key. May be a prefix
* @return Keypair instance, or null if not found
*/
public AKeyPair loadKeyFromStore(Main main, String publicKey) {
if (publicKey == null)
return null;

AKeyPair keyPair = null;

publicKey = publicKey.trim();
publicKey = publicKey.toLowerCase().replaceFirst("^0x", "").strip();
if (publicKey.isEmpty()) {
return null;
}

char[] storePassword = getStorePassword();

File keyFile = getKeyStoreFile();
try {
if (!keyFile.exists()) {
throw new CLIError("Cannot find keystore file " + keyFile.getCanonicalPath());
}
KeyStore keyStore = PFXTools.loadStore(keyFile, storePassword);

Enumeration<String> aliases = keyStore.aliases();

while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (alias.indexOf(publicKey) == 0) {
log.trace("found keypair " + alias);
keyPair = PFXTools.getKeyPair(keyStore, alias, main.getKeyPassword());
break;
}
}
} catch (Exception t) {
throw new CLIError("Cannot load key store", t);
}

return keyPair;
}

}
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void run() {

AKeyPair keyPair = null;
if (genesisKey!=null) {
keyPair = cli().loadKeyFromStore(genesisKey);
keyPair = cli().storeMixin.loadKeyFromStore(cli(), genesisKey);
if (keyPair == null) {
throw new CLIError("Cannot find specified key pair to perform peer start: "+genesisKey);
}
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/peer/PeerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void run() {

AKeyPair keyPair = null;
if (keystorePublicKey!=null) {
keyPair = mainParent.loadKeyFromStore(keystorePublicKey);
keyPair = mainParent.storeMixin.loadKeyFromStore(mainParent, keystorePublicKey);
if (keyPair == null) {
log.warn("Cannot load specified key pair to perform peer start: "+keystorePublicKey);
return;
Expand Down

0 comments on commit 53a88e3

Please sign in to comment.