From 53a88e3e3ec2b9d4d732a0c15fe80d7fe8c5c8dd Mon Sep 17 00:00:00 2001 From: mikera Date: Thu, 1 Aug 2024 13:47:29 +0100 Subject: [PATCH] More CLI refactoring towards full mixin model --- .../src/main/java/convex/cli/ACommand.java | 5 +- .../main/java/convex/cli/AccountCreate.java | 2 +- convex-cli/src/main/java/convex/cli/Main.java | 49 +------------------ .../convex/cli/client/AClientCommand.java | 2 +- .../main/java/convex/cli/key/KeyExport.java | 2 +- .../java/convex/cli/local/LocalStart.java | 2 +- .../java/convex/cli/mixins/StoreMixin.java | 47 ++++++++++++++++++ .../java/convex/cli/peer/PeerGenesis.java | 2 +- .../main/java/convex/cli/peer/PeerStart.java | 2 +- 9 files changed, 55 insertions(+), 58 deletions(-) diff --git a/convex-cli/src/main/java/convex/cli/ACommand.java b/convex-cli/src/main/java/convex/cli/ACommand.java index a67adee9e..6beb16a9d 100644 --- a/convex-cli/src/main/java/convex/cli/ACommand.java +++ b/convex-cli/src/main/java/convex/cli/ACommand.java @@ -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 { @@ -153,7 +153,4 @@ public void inform(String message) { inform(1,message); } - - - } diff --git a/convex-cli/src/main/java/convex/cli/AccountCreate.java b/convex-cli/src/main/java/convex/cli/AccountCreate.java index e596d9f17..05e968b8f 100644 --- a/convex-cli/src/main/java/convex/cli/AccountCreate.java +++ b/convex-cli/src/main/java/convex/cli/AccountCreate.java @@ -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); } diff --git a/convex-cli/src/main/java/convex/cli/Main.java b/convex-cli/src/main/java/convex/cli/Main.java index 77c72054d..d0e42d4f4 100644 --- a/convex-cli/src/main/java/convex/cli/Main.java +++ b/convex-cli/src/main/java/convex/cli/Main.java @@ -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; @@ -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; @@ -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); @@ -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 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! * diff --git a/convex-cli/src/main/java/convex/cli/client/AClientCommand.java b/convex-cli/src/main/java/convex/cli/client/AClientCommand.java index fe31a4868..56f4d7d9e 100644 --- a/convex-cli/src/main/java/convex/cli/client/AClientCommand.java +++ b/convex-cli/src/main/java/convex/cli/client/AClientCommand.java @@ -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); diff --git a/convex-cli/src/main/java/convex/cli/key/KeyExport.java b/convex-cli/src/main/java/convex/cli/key/KeyExport.java index 9ce9f28dc..24d12664f 100644 --- a/convex-cli/src/main/java/convex/cli/key/KeyExport.java +++ b/convex-cli/src/main/java/convex/cli/key/KeyExport.java @@ -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); diff --git a/convex-cli/src/main/java/convex/cli/local/LocalStart.java b/convex-cli/src/main/java/convex/cli/local/LocalStart.java index 7c5004a6a..bab57d904 100644 --- a/convex-cli/src/main/java/convex/cli/local/LocalStart.java +++ b/convex-cli/src/main/java/convex/cli/local/LocalStart.java @@ -80,7 +80,7 @@ private List 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 { diff --git a/convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java b/convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java index bdaace224..85caa4449 100644 --- a/convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java +++ b/convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java @@ -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; @@ -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 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; + } + } diff --git a/convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java b/convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java index ebc0208b9..18ff42c0d 100644 --- a/convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java +++ b/convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java @@ -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); } diff --git a/convex-cli/src/main/java/convex/cli/peer/PeerStart.java b/convex-cli/src/main/java/convex/cli/peer/PeerStart.java index 8edfceb58..352d4cb8d 100644 --- a/convex-cli/src/main/java/convex/cli/peer/PeerStart.java +++ b/convex-cli/src/main/java/convex/cli/peer/PeerStart.java @@ -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;