@ -17,9 +17,12 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.MANDATORY_FILE_FORMAT_HELP ;
import static tech.pegasys.pantheon.cli.PublicKeySubCommand.COMMAND_NAME ;
import tech.pegasys.pantheon.cli.PublicKeySubCommand.AddressSubCommand ;
import tech.pegasys.pantheon.cli.PublicKeySubCommand.ExportSubCommand ;
import tech.pegasys.pantheon.controller.PantheonController ;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair ;
import tech.pegasys.pantheon.ethereum.core.Address ;
import tech.pegasys.pantheon.ethereum.core.Util ;
import java.io.BufferedWriter ;
import java.io.File ;
@ -41,7 +44,7 @@ import picocli.CommandLine.Spec;
name = COMMAND_NAME ,
description = "This command provides node public key related actions." ,
mixinStandardHelpOptions = true ,
subcommands = { ExportSubCommand . class } )
subcommands = { ExportSubCommand . class , AddressSubCommand . class } )
class PublicKeySubCommand implements Runnable {
private static final Logger LOG = LogManager . getLogger ( ) ;
@ -69,23 +72,23 @@ class PublicKeySubCommand implements Runnable {
/ * *
* Public key export sub - command
*
* < p > Export of the public key takes a file as parameter to export directly by writing the key in
* the file . A direct output of the key to standard out is not done because we don ' t want the key
* value to be polluted by other information like logs that are in KeyPairUtil that is inevitable .
* < p > Export of the public key is writing the key to the standard output by default . An option
* enables to write it in a file . Indeed , a direct output of the value to standard out is not
* always recommended as reading can be made difficult as the value can be mixed with other
* information like logs that are in KeyPairUtil that is inevitable .
* /
@Command (
name = "export" ,
description = "This command exports the node public key to a file ." ,
description = "This command outputs the node public key. Default output is standard output ." ,
mixinStandardHelpOptions = true )
static class ExportSubCommand implements Runnable {
@Option (
names = "--to" ,
required = true ,
paramLabel = MANDATORY_FILE_FORMAT_HELP ,
description = "File to write public key to" ,
description = "File to write public key to instead of standard output " ,
arity = "1..1" )
private final File publicKeyExportFile = null ;
private File publicKeyExportFile = null ;
@SuppressWarnings ( "unused" )
@ParentCommand
@ -99,8 +102,9 @@ class PublicKeySubCommand implements Runnable {
final PantheonController < ? > controller = parentCommand . parentCommand . buildController ( ) ;
final KeyPair keyPair = controller . getLocalNodeKeyPair ( ) ;
// this publicKeyExportFile can never be null because of Picocli arity requirement
//noinspection ConstantConditions
// if we have an output file defined, print to it
// otherwise print to standard output.
if ( publicKeyExportFile ! = null ) {
final Path path = publicKeyExportFile . toPath ( ) ;
try ( final BufferedWriter fileWriter = Files . newBufferedWriter ( path , UTF_8 ) ) {
@ -108,6 +112,61 @@ class PublicKeySubCommand implements Runnable {
} catch ( final IOException e ) {
LOG . error ( "An error occurred while trying to write the public key" , e ) ;
}
} else {
parentCommand . out . println ( keyPair . getPublicKey ( ) . toString ( ) ) ;
}
}
}
/ * *
* Public key address export sub - command
*
* < p > Export of the public key address is writing the address to the standard output by default .
* An option enables to write it in a file . Indeed , a direct output of the value to standard out
* is not always recommended as reading can be made difficult as the value can be mixed with other
* information like logs that are in KeyPairUtil that is inevitable .
* /
@Command (
name = "export-address" ,
description =
"This command outputs the node's public key address. "
+ "Default output is standard output." ,
mixinStandardHelpOptions = true )
static class AddressSubCommand implements Runnable {
@Option (
names = "--to" ,
paramLabel = MANDATORY_FILE_FORMAT_HELP ,
description = "File to write address to instead of standard output" ,
arity = "1..1" )
private File addressExportFile = null ;
@SuppressWarnings ( "unused" )
@ParentCommand
private PublicKeySubCommand parentCommand ; // Picocli injects reference to parent command
@Override
public void run ( ) {
checkNotNull ( parentCommand ) ;
checkNotNull ( parentCommand . parentCommand ) ;
final PantheonController < ? > controller = parentCommand . parentCommand . buildController ( ) ;
final KeyPair keyPair = controller . getLocalNodeKeyPair ( ) ;
final Address address = Util . publicKeyToAddress ( keyPair . getPublicKey ( ) ) ;
// if we have an output file defined, print to it
// otherwise print to standard output.
if ( addressExportFile ! = null ) {
final Path path = addressExportFile . toPath ( ) ;
try ( final BufferedWriter fileWriter = Files . newBufferedWriter ( path , UTF_8 ) ) {
fileWriter . write ( address . toString ( ) ) ;
} catch ( final IOException e ) {
LOG . error ( "An error occurred while trying to write the public key address" , e ) ;
}
} else {
parentCommand . out . println ( address . toString ( ) ) ;
}
}
}
}