diff --git a/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java b/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java index 29325592122..20478f15dd0 100644 --- a/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java +++ b/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/DebugToolsCommand.java @@ -15,11 +15,15 @@ import static tech.pegasys.teku.infrastructure.time.SystemTimeProvider.SYSTEM_TIME_PROVIDER; +import io.libp2p.core.PeerId; +import io.libp2p.core.crypto.KeyKt; +import io.libp2p.core.crypto.PrivKey; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Optional; import org.apache.tuweni.bytes.Bytes; import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; @@ -239,4 +243,33 @@ public int printCommittees( assignment.committee().indexOf(validatorIndex)); return 0; } + + @Command( + name = "get-peer-id", + description = "Gets the peerID from private key file.", + mixinStandardHelpOptions = true, + showDefaultValues = true, + abbreviateSynopsis = true, + versionProvider = PicoCliVersionProvider.class, + synopsisHeading = "%n", + descriptionHeading = "%nDescription:%n%n", + optionListHeading = "%nOptions:%n", + footerHeading = "%n", + footer = "Teku is licensed under the Apache License 2.0") + public int getPeerID( + @Option( + names = {"--input", "-i"}, + description = "File to read the privateKey from", + required = true) + final Path input) { + try { + final Bytes privateKeyBytes = Bytes.wrap(Files.readAllBytes(Paths.get(input.toUri()))); + final PrivKey privKey = KeyKt.unmarshalPrivateKey(privateKeyBytes.toArrayUnsafe()); + System.out.print("Peer ID: " + PeerId.fromPubKey(privKey.publicKey())); + } catch (IOException e) { + System.err.println("Failed to read private key file: " + e.getMessage()); + return 1; + } + return 0; + } }