Skip to content

Commit 53a88e3

Browse files
committed
More CLI refactoring towards full mixin model
1 parent 98e2424 commit 53a88e3

File tree

9 files changed

+55
-58
lines changed

9 files changed

+55
-58
lines changed

convex-cli/src/main/java/convex/cli/ACommand.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import picocli.CommandLine;
1212

1313
/**
14-
* Base class for Convex CLI commands
14+
* Base class for Convex CLI command components and mixins
1515
*/
1616
public abstract class ACommand implements Runnable {
1717

@@ -153,7 +153,4 @@ public void inform(String message) {
153153
inform(1,message);
154154
}
155155

156-
157-
158-
159156
}

convex-cli/src/main/java/convex/cli/AccountCreate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void run() {
5656
AKeyPair keyPair = null;
5757

5858
if (!keystorePublicKey.isEmpty()) {
59-
keyPair = mainParent.loadKeyFromStore(keystorePublicKey);
59+
keyPair = mainParent.storeMixin.loadKeyFromStore(mainParent, keystorePublicKey);
6060
if (keyPair == null) {
6161
throw new CLIError("Cannot find the provided public key in keystore: "+keystorePublicKey);
6262
}

convex-cli/src/main/java/convex/cli/Main.java

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.PrintWriter;
6-
import java.security.KeyStore;
76
import java.util.ArrayList;
8-
import java.util.Enumeration;
97
import java.util.List;
108

119
import org.slf4j.Logger;
@@ -23,7 +21,6 @@
2321
import convex.cli.output.Coloured;
2422
import convex.cli.peer.Peer;
2523
import convex.core.crypto.AKeyPair;
26-
import convex.core.crypto.PFXTools;
2724
import convex.core.exceptions.TODOException;
2825
import convex.core.util.Utils;
2926
import picocli.CommandLine;
@@ -54,7 +51,7 @@
5451
description = "Convex Command Line Interface")
5552

5653
public class Main extends ACommand {
57-
static Logger log = LoggerFactory.getLogger(Main.class);
54+
private static Logger log = LoggerFactory.getLogger(Main.class);
5855

5956
public CommandLine commandLine = new CommandLine(this);
6057

@@ -240,50 +237,6 @@ public char[] getKeyPassword() {
240237
}
241238

242239

243-
/**
244-
* Loads a keypair from configured keystore
245-
*
246-
* @param publicKey String identifying the public key. May be a prefix
247-
* @return Keypair instance, or null if not found
248-
*/
249-
public AKeyPair loadKeyFromStore(String publicKey) {
250-
if (publicKey == null)
251-
return null;
252-
253-
AKeyPair keyPair = null;
254-
255-
publicKey = publicKey.trim();
256-
publicKey = publicKey.toLowerCase().replaceFirst("^0x", "").strip();
257-
if (publicKey.isEmpty()) {
258-
return null;
259-
}
260-
261-
char[] storePassword = storeMixin.getStorePassword();
262-
263-
File keyFile = storeMixin.getKeyStoreFile();
264-
try {
265-
if (!keyFile.exists()) {
266-
throw new CLIError("Cannot find keystore file " + keyFile.getCanonicalPath());
267-
}
268-
KeyStore keyStore = PFXTools.loadStore(keyFile, storePassword);
269-
270-
Enumeration<String> aliases = keyStore.aliases();
271-
272-
while (aliases.hasMoreElements()) {
273-
String alias = aliases.nextElement();
274-
if (alias.indexOf(publicKey) == 0) {
275-
log.trace("found keypair " + alias);
276-
keyPair = PFXTools.getKeyPair(keyStore, alias, getKeyPassword());
277-
break;
278-
}
279-
}
280-
} catch (Exception t) {
281-
throw new CLIError("Cannot load key store", t);
282-
}
283-
284-
return keyPair;
285-
}
286-
287240
/**
288241
* Generate key pairs and add to store. Does not save store!
289242
*

convex-cli/src/main/java/convex/cli/client/AClientCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected boolean ensureKeyPair(Convex convex) {
8888
}
8989

9090

91-
keyPair=mainParent.loadKeyFromStore(pk);
91+
keyPair=mainParent.storeMixin.loadKeyFromStore(mainParent, pk);
9292
if (keyPair==null) {
9393
// We didn't find required keypair
9494
throw new CLIError("Can't find keypair with public key "+pk+" for Address "+address);

convex-cli/src/main/java/convex/cli/key/KeyExport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void run() {
7676
}
7777

7878
String publicKey = keystorePublicKey;
79-
AKeyPair keyPair = cli().loadKeyFromStore(publicKey);
79+
AKeyPair keyPair = cli().storeMixin.loadKeyFromStore(cli(), publicKey);
8080
if (keyPair==null) {
8181
// TODO: maybe prompt?
8282
throw new CLIError("Key pair not found for key: "+keystorePublicKey);

convex-cli/src/main/java/convex/cli/local/LocalStart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private List<AKeyPair> getPeerKeyPairs(int n) {
8080
String keyPrefix = values.get(index);
8181
if (keyPrefix.isBlank()) continue;
8282

83-
AKeyPair keyPair = mainParent.loadKeyFromStore(keyPrefix);
83+
AKeyPair keyPair = mainParent.storeMixin.loadKeyFromStore(mainParent, keyPrefix);
8484
if (keyPair == null) {
8585
log.warn("Unable to find public key in store: "+keyPrefix);
8686
} else {

convex-cli/src/main/java/convex/cli/mixins/StoreMixin.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import java.security.GeneralSecurityException;
77
import java.security.KeyStore;
88
import java.security.UnrecoverableKeyException;
9+
import java.util.Enumeration;
910

1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213

1314
import convex.cli.CLIError;
1415
import convex.cli.Constants;
16+
import convex.cli.Main;
1517
import convex.core.crypto.AKeyPair;
1618
import convex.core.crypto.PFXTools;
1719
import convex.core.util.Utils;
@@ -171,4 +173,49 @@ public void addKeyPairToStore(AKeyPair keyPair, char[] keyPassword) {
171173

172174
}
173175

176+
/**
177+
* Loads a keypair from configured keystore
178+
*
179+
* @param main TODO
180+
* @param publicKey String identifying the public key. May be a prefix
181+
* @return Keypair instance, or null if not found
182+
*/
183+
public AKeyPair loadKeyFromStore(Main main, String publicKey) {
184+
if (publicKey == null)
185+
return null;
186+
187+
AKeyPair keyPair = null;
188+
189+
publicKey = publicKey.trim();
190+
publicKey = publicKey.toLowerCase().replaceFirst("^0x", "").strip();
191+
if (publicKey.isEmpty()) {
192+
return null;
193+
}
194+
195+
char[] storePassword = getStorePassword();
196+
197+
File keyFile = getKeyStoreFile();
198+
try {
199+
if (!keyFile.exists()) {
200+
throw new CLIError("Cannot find keystore file " + keyFile.getCanonicalPath());
201+
}
202+
KeyStore keyStore = PFXTools.loadStore(keyFile, storePassword);
203+
204+
Enumeration<String> aliases = keyStore.aliases();
205+
206+
while (aliases.hasMoreElements()) {
207+
String alias = aliases.nextElement();
208+
if (alias.indexOf(publicKey) == 0) {
209+
log.trace("found keypair " + alias);
210+
keyPair = PFXTools.getKeyPair(keyStore, alias, main.getKeyPassword());
211+
break;
212+
}
213+
}
214+
} catch (Exception t) {
215+
throw new CLIError("Cannot load key store", t);
216+
}
217+
218+
return keyPair;
219+
}
220+
174221
}

convex-cli/src/main/java/convex/cli/peer/PeerGenesis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void run() {
4444

4545
AKeyPair keyPair = null;
4646
if (genesisKey!=null) {
47-
keyPair = cli().loadKeyFromStore(genesisKey);
47+
keyPair = cli().storeMixin.loadKeyFromStore(cli(), genesisKey);
4848
if (keyPair == null) {
4949
throw new CLIError("Cannot find specified key pair to perform peer start: "+genesisKey);
5050
}

convex-cli/src/main/java/convex/cli/peer/PeerStart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void run() {
7272

7373
AKeyPair keyPair = null;
7474
if (keystorePublicKey!=null) {
75-
keyPair = mainParent.loadKeyFromStore(keystorePublicKey);
75+
keyPair = mainParent.storeMixin.loadKeyFromStore(mainParent, keystorePublicKey);
7676
if (keyPair == null) {
7777
log.warn("Cannot load specified key pair to perform peer start: "+keystorePublicKey);
7878
return;

0 commit comments

Comments
 (0)