Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug command to retrieve peerId from a node key file #8727

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}