diff --git a/convex-cli/src/main/java/convex/cli/Main.java b/convex-cli/src/main/java/convex/cli/Main.java index 7aab7efa5..77c72054d 100644 --- a/convex-cli/src/main/java/convex/cli/Main.java +++ b/convex-cli/src/main/java/convex/cli/Main.java @@ -258,7 +258,7 @@ public AKeyPair loadKeyFromStore(String publicKey) { return null; } - char[] storePassword = storeMixin.getStorePassword(this); + char[] storePassword = storeMixin.getStorePassword(); File keyFile = storeMixin.getKeyStoreFile(); try { @@ -297,7 +297,7 @@ public List generateKeyPairs(int count, char[] keyPassword) { for (int index = 0; index < count; index++) { AKeyPair keyPair = AKeyPair.generate(); keyPairList.add(keyPair); - storeMixin.addKeyPairToStore(this, keyPair, keyPassword); + storeMixin.addKeyPairToStore(keyPair, keyPassword); } return keyPairList; 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 e419a49cc..682503c74 100644 --- a/convex-cli/src/main/java/convex/cli/key/KeyGenerate.java +++ b/convex-cli/src/main/java/convex/cli/key/KeyGenerate.java @@ -71,7 +71,7 @@ public void run() { return; } - char[] storePass=cli().storeMixin.getStorePassword(cli()); + char[] storePass=cli().storeMixin.getStorePassword(); try { KeyStore ks=cli().storeMixin.loadKeyStore(true,storePass); for ( int index = 0; index < count; index ++) { diff --git a/convex-cli/src/main/java/convex/cli/key/KeyImport.java b/convex-cli/src/main/java/convex/cli/key/KeyImport.java index de7251a55..48d8a0030 100644 --- a/convex-cli/src/main/java/convex/cli/key/KeyImport.java +++ b/convex-cli/src/main/java/convex/cli/key/KeyImport.java @@ -103,9 +103,9 @@ public void run() { if (keyPair==null) throw new CLIError("Unable to import keypair"); // Finally write to store - char[] storePassword=cli().storeMixin.getStorePassword(cli()); + char[] storePassword=cli().storeMixin.getStorePassword(); char[] keyPassword=cli().getKeyPassword(); - cli().storeMixin.addKeyPairToStore(cli(), keyPair,keyPassword); + cli().storeMixin.addKeyPairToStore(keyPair,keyPassword); Arrays.fill(keyPassword, 'x'); cli().storeMixin.saveKeyStore(storePassword); cli().println(keyPair.getAccountKey().toHexString()); diff --git a/convex-cli/src/main/java/convex/cli/key/KeyList.java b/convex-cli/src/main/java/convex/cli/key/KeyList.java index 09ff6d81e..8fdd0c0d3 100644 --- a/convex-cli/src/main/java/convex/cli/key/KeyList.java +++ b/convex-cli/src/main/java/convex/cli/key/KeyList.java @@ -27,7 +27,7 @@ public class KeyList extends AKeyCommand { @Override public void run() { - KeyStore keyStore = cli().storeMixin.loadKeyStore(cli()); + KeyStore keyStore = cli().storeMixin.loadKeyStore(); if (keyStore==null) throw new CLIError("Keystore does not exist. Specify a valid keystore or use `convex key gen` to create one."); Enumeration aliases; @@ -40,7 +40,7 @@ public void run() { output.addRow(String.format("%5d", index), alias); index ++; } - cli().println(output); + println(output); } catch (KeyStoreException e) { throw new CLIError("Unexpected error reading keystore",e); } diff --git a/convex-cli/src/main/java/convex/cli/mixins/AMixin.java b/convex-cli/src/main/java/convex/cli/mixins/AMixin.java new file mode 100644 index 000000000..e1a78c7b6 --- /dev/null +++ b/convex-cli/src/main/java/convex/cli/mixins/AMixin.java @@ -0,0 +1,22 @@ +package convex.cli.mixins; + +import convex.cli.ACommand; +import convex.cli.Main; +import picocli.CommandLine.ParentCommand; + +public class AMixin extends ACommand{ + + @ParentCommand + ACommand parentCommand; + + @Override + public void run() { + throw new Error("Mixin should not be called as command!"); + } + + @Override + public Main cli() { + return parentCommand.cli(); + } + +} diff --git a/convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java b/convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java index da8febbf2..0033f2335 100644 --- a/convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java +++ b/convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java @@ -10,7 +10,7 @@ import picocli.CommandLine.Option; import picocli.CommandLine.ScopeType; -public class EtchMixin { +public class EtchMixin extends AMixin { @Option(names={"-e", "--etch"}, scope = ScopeType.INHERIT, 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 89f3217d1..bdaace224 100644 --- a/convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java +++ b/convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java @@ -12,14 +12,13 @@ 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; import picocli.CommandLine.Option; import picocli.CommandLine.ScopeType; -public class StoreMixin { +public class StoreMixin extends AMixin { @Option(names = { "--keystore" }, defaultValue = "${env:CONVEX_KEYSTORE:-" + Constants.KEYSTORE_FILENAME+ "}", @@ -61,19 +60,19 @@ public File getKeyStoreFile() { * @param main TODO * @return Password string */ - public char[] getStorePassword(Main main) { + public char[] getStorePassword() { char[] storepass = null; if (keystorePassword != null) { storepass = keystorePassword.toCharArray(); } else { - if (main.isInteractive()) { - storepass = main.readPassword("Enter Keystore Password: "); + if (isInteractive()) { + storepass = readPassword("Enter Keystore Password: "); keystorePassword=new String(storepass); } if (storepass == null) { - main.paranoia("Keystore password must be explicitly provided"); + paranoia("Keystore password must be explicitly provided"); log.warn("No password for keystore: defaulting to blank password"); storepass = new char[0]; } @@ -87,9 +86,9 @@ public char[] getStorePassword(Main main) { * @param main TODO * @return KeyStore instance, or null if it does not exist */ - public KeyStore getKeystore(Main main) { + public KeyStore getKeystore() { if (keyStore == null) { - keyStore = loadKeyStore(false, getStorePassword(main)); + keyStore = loadKeyStore(false, getStorePassword()); } return keyStore; } @@ -100,8 +99,8 @@ public KeyStore getKeystore(Main main) { * @param main TODO * @return KeyStore instance, or null if does not exist */ - public KeyStore loadKeyStore(Main main) { - return main.storeMixin.loadKeyStore(false, getStorePassword(main)); + public KeyStore loadKeyStore() { + return loadKeyStore(false, getStorePassword()); } /** @@ -157,9 +156,9 @@ public void saveKeyStore(char[] storePassword) { * @param keyPair Keypair to add * @param keyPassword TODO */ - public void addKeyPairToStore(Main main, AKeyPair keyPair, char[] keyPassword) { + public void addKeyPairToStore(AKeyPair keyPair, char[] keyPassword) { - KeyStore keyStore = getKeystore(main); + KeyStore keyStore = getKeystore(); if (keyStore == null) { throw new CLIError("Trying to add key pair but keystore does not exist"); } diff --git a/convex-cli/src/main/java/convex/cli/peer/PeerCreate.java b/convex-cli/src/main/java/convex/cli/peer/PeerCreate.java index 74d37f4a4..6c55b2b98 100644 --- a/convex-cli/src/main/java/convex/cli/peer/PeerCreate.java +++ b/convex-cli/src/main/java/convex/cli/peer/PeerCreate.java @@ -75,7 +75,7 @@ public void run() { try { // create a keystore if it does not exist - keyStore = mainParent.storeMixin.loadKeyStore(mainParent); + keyStore = mainParent.storeMixin.loadKeyStore(); } catch (Error e) { log.info(e.getMessage()); return; 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 3ace48387..ebc0208b9 100644 --- a/convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java +++ b/convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java @@ -54,7 +54,7 @@ public void run() { // } if (cli().isParanoid()) throw new CLIError("Aborting due to strict security: no key pair specified"); keyPair=AKeyPair.generate(); - cli().storeMixin.addKeyPairToStore(cli(), keyPair,cli().getKeyPassword()); + cli().storeMixin.addKeyPairToStore(keyPair,cli().getKeyPassword()); cli().storeMixin.saveKeyStore(); cli().inform("Generated new Keypair with public key: "+keyPair.getAccountKey()); } 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 c211c82f2..8edfceb58 100644 --- a/convex-cli/src/main/java/convex/cli/peer/PeerStart.java +++ b/convex-cli/src/main/java/convex/cli/peer/PeerStart.java @@ -79,7 +79,7 @@ public void run() { } } else { keyPair=AKeyPair.generate(); - mainParent.storeMixin.addKeyPairToStore(mainParent, keyPair,mainParent.getKeyPassword()); + mainParent.storeMixin.addKeyPairToStore(keyPair,mainParent.getKeyPassword()); log.warn("Generated new Keypair with public key: "+keyPair.getAccountKey()); }