Skip to content

Commit

Permalink
More CLI updates for key import
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 25, 2024
1 parent 1463977 commit 48b5d1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public char[] readPassword(String prompt) {
return c.readPassword(prompt);
}

public String loadTextFile(String fname) {
public String loadFileAsString(String fname) {
String result=null;
try {
fname=fname.trim();
Expand Down
5 changes: 2 additions & 3 deletions convex-cli/src/main/java/convex/cli/key/KeyImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void run() {
// Ensure importText is filled
if (importFilename != null && importFilename.length() > 0) {
if (importText!=null) throw new CLIError("Please provide either --import-file or --text, not both!");
importText=cli().loadTextFile(importFilename);
importText=cli().loadFileAsString(importFilename);
}
if (importText == null || importText.length() == 0) {
throw new CLIError("You need to provide '--text' or import filename '--import-file' to import a private key");
Expand All @@ -72,9 +72,8 @@ public void run() {
}
}

// Parse input as hex string, just in case
// Parse input as hex string, will be null if not parsed. For BIP39 is 64 bytes, Ed25519 32
ABlob hex=Blobs.parse(importText.trim());

if (type==null) {
cli().printErr("No import file type specified, attempting to auto-detect");
if (hex!=null) {
Expand Down
28 changes: 28 additions & 0 deletions convex-cli/src/test/java/convex/cli/key/KeyImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ public void testKeyImportPEM() {
assertTrue(t2.getOutput().contains(accountKey.toHexString()));
}

@Test
public void testKeyImportSeed() {
AKeyPair keyPair = AKeyPair.createSeeded(101);
AccountKey accountKey=keyPair.getAccountKey();

CLTester tester = CLTester.run(
"key",
"import",
"seed",
"--keystore-password", new String(KEYSTORE_PASSWORD),
"--keystore", KEYSTORE_FILENAME,
"--text", keyPair.getSeed().toString(),
"--import-password", new String("")
);
assertEquals(ExitCodes.SUCCESS,tester.getResult());

// Should give Ed25519 Seed: 616421a4ea27c65919faa5555e923f6005d76695c7d9ba0fe2a484b90e23de89

CLTester t2=CLTester.run(
"key" ,
"list",
"--keystore-password", new String(KEYSTORE_PASSWORD),
"--keystore", KEYSTORE_FILENAME);

assertEquals(ExitCodes.SUCCESS,t2.getResult());
assertTrue(t2.getOutput().contains(accountKey.toHexString()));
}

@Test
public void testKeyImportBIP39() {

Expand Down

0 comments on commit 48b5d1e

Please sign in to comment.