Skip to content

Commit 1fadf9b

Browse files
authored
Support binary in `—p2p-private-key-file option (Consensys#8093)
1 parent c995869 commit 1fadf9b

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ the [releases page](https://github.com/Consensys/teku/releases).
1414
### Additions and Improvements
1515
- Introduced [Validator Slashing Prevention feature](https://docs.teku.consensys.io/how-to/prevent-slashing/detect-slashing).
1616
- If the EL supports the `engine_getClientVersionV1` Engine API method, the default graffiti (when no graffiti has been configured by the validator) will include EL as well as CL version information. For more details, please see https://github.com/ethereum/execution-apis/pull/517.
17+
- `—p2p-private-key-file` command line option supports reading a binary private key file.
1718

1819
### Bug Fixes

networking/p2p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/FilePrivateKeySource.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.libp2p.core.crypto.PrivKey;
2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.nio.charset.MalformedInputException;
2324
import java.nio.file.Files;
2425
import java.nio.file.Paths;
2526
import java.util.Objects;
@@ -37,7 +38,7 @@ public Bytes getOrGeneratePrivateKeyBytes() {
3738
try {
3839
File file = new File(fileName);
3940
if (!file.createNewFile()) {
40-
return getPrivateKeyBytesFromFile();
41+
return getPrivateKeyBytesFromTextFile();
4142
}
4243
final PrivKey privKey = KeyKt.generateKeyPair(KeyType.SECP256K1).component1();
4344
final Bytes privateKeyBytes = Bytes.wrap(KeyKt.marshalPrivateKey(privKey));
@@ -51,11 +52,23 @@ public Bytes getOrGeneratePrivateKeyBytes() {
5152
}
5253
}
5354

54-
private Bytes getPrivateKeyBytesFromFile() {
55+
private Bytes getPrivateKeyBytesFromTextFile() {
5556
try {
5657
final Bytes privateKeyBytes = Bytes.fromHexString(Files.readString(Paths.get(fileName)));
5758
STATUS_LOG.usingGeneratedP2pPrivateKey(fileName, false);
5859
return privateKeyBytes;
60+
} catch (MalformedInputException e) {
61+
return getPrivateKeyBytesFromBytesFile();
62+
} catch (IOException e) {
63+
throw new RuntimeException("p2p private key file not found - " + fileName);
64+
}
65+
}
66+
67+
private Bytes getPrivateKeyBytesFromBytesFile() {
68+
try {
69+
final Bytes privateKeyBytes = Bytes.wrap(Files.readAllBytes(Paths.get(fileName)));
70+
STATUS_LOG.usingGeneratedP2pPrivateKey(fileName, false);
71+
return privateKeyBytes;
5972
} catch (IOException e) {
6073
throw new RuntimeException("p2p private key file not found - " + fileName);
6174
}

networking/p2p/src/test/java/tech/pegasys/teku/networking/p2p/network/config/FilePrivateKeySourceTest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void shouldCreateKeyAndSaveToFile(@TempDir Path tempDir) throws IOException {
3737
}
3838

3939
@Test
40-
void shouldGetKeyFromSavedFile(@TempDir Path tempDir) throws IOException {
40+
void shouldGetKeyFromSavedTextFile(@TempDir Path tempDir) throws IOException {
4141
final Path file = tempDir.resolve("file.txt");
4242
final Bytes privateKey = Bytes.wrap(PrivateKeyGenerator.generate().bytes());
4343
Files.writeString(file, privateKey.toHexString());
@@ -46,6 +46,16 @@ void shouldGetKeyFromSavedFile(@TempDir Path tempDir) throws IOException {
4646
assertThat(privateKeySource.getOrGeneratePrivateKeyBytes()).isEqualTo(privateKey);
4747
}
4848

49+
@Test
50+
void shouldGetKeyFromBinaryFile(@TempDir Path tempDir) throws IOException {
51+
final Path file = tempDir.resolve("file.dat");
52+
final Bytes privateKey = Bytes.wrap(PrivateKeyGenerator.generate().bytes());
53+
Files.write(file, privateKey.toArray());
54+
final PrivateKeySource privateKeySource = new FilePrivateKeySource(file.toString());
55+
56+
assertThat(privateKeySource.getOrGeneratePrivateKeyBytes()).isEqualTo(privateKey);
57+
}
58+
4959
@Test
5060
void shouldThrowExceptionIfInvalidFileName(@TempDir Path tempDir) {
5161
final PrivateKeySource privateKeySource =

0 commit comments

Comments
 (0)