Skip to content

Commit

Permalink
Refactor to common mixin base class
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 1, 2024
1 parent f877046 commit 98e2424
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 23 deletions.
4 changes: 2 additions & 2 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -297,7 +297,7 @@ public List<AKeyPair> 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;
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/key/KeyGenerate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ++) {
Expand Down
4 changes: 2 additions & 2 deletions convex-cli/src/main/java/convex/cli/key/KeyImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions convex-cli/src/main/java/convex/cli/key/KeyList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> aliases;
Expand All @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions convex-cli/src/main/java/convex/cli/mixins/AMixin.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/mixins/EtchMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 11 additions & 12 deletions convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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+ "}",
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
}
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/peer/PeerCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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 @@ -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());
}
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 @@ -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());
}

Expand Down

0 comments on commit 98e2424

Please sign in to comment.