Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
generateMnemonic command (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev authored Feb 23, 2018
1 parent 8ae772e commit d4c2c60
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/goobox/sync/storj/ipc/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public CommandResult execute() {
return new CreateAccountRequest(args).execute();
case CheckMnemonicRequest.METHOD:
return new CheckMnemonicRequest(args).execute();
case GenerateMnemonicRequest.METHOD:
return new GenerateMnemonicRequest().execute();
case QuitCommand.METHOD:
return new QuitCommand().execute();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ public void onError(int code, String message) {
}

if (error[0] != null) {
return new CreateAccountResult(Status.ERROR, error[0], null);
return new CommandResult(Status.ERROR, error[0]);
}

String mnemonic = Storj.generateMnemonic(256);

return new CreateAccountResult(Status.OK, null, mnemonic);
return new GenerateMnemonicRequest().execute();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.goobox.sync.storj.ipc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.storj.libstorj.Storj;

public class GenerateMnemonicRequest {

private static final Logger logger = LoggerFactory.getLogger(GenerateMnemonicRequest.class);

public static final String METHOD = "generateMnemonic";

public CommandResult execute() {
String mnemonic = Storj.generateMnemonic(256);

if (mnemonic == null) {
String msg = String.format("Failed to generate encryption key");
logger.error(msg);
return new CommandResult(Status.ERROR, msg);
}

return new NewMnemonicResult(Status.OK, null, mnemonic);
}

}
4 changes: 2 additions & 2 deletions src/main/java/io/goobox/sync/storj/ipc/IpcExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ private void log(String input, Map<String, String> args) {

private void log(String msg, String output, Object obj) {
if (logger.isDebugEnabled()) {
if (obj instanceof CreateAccountResult) {
String encryptionKey = ((CreateAccountResult) obj).encryptionKey;
if (obj instanceof NewMnemonicResult) {
String encryptionKey = ((NewMnemonicResult) obj).encryptionKey;
if (encryptionKey != null) {
output = output.replace("\"" + encryptionKey + "\"", "\"********\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package io.goobox.sync.storj.ipc;

public class CreateAccountResult extends CommandResult {
public class NewMnemonicResult extends CommandResult {

String encryptionKey;

public CreateAccountResult(Status status, String message, String encryptionKey) {
public NewMnemonicResult(Status status, String message, String encryptionKey) {
super(status, message);
this.encryptionKey = encryptionKey;
}
Expand Down

0 comments on commit d4c2c60

Please sign in to comment.