Skip to content

Commit cedf831

Browse files
committed
Support binary in `—p2p-private-key-file option
1 parent 5989775 commit cedf831

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Bytes getOrGeneratePrivateKeyBytes() {
3737
try {
3838
File file = new File(fileName);
3939
if (!file.createNewFile()) {
40-
return getPrivateKeyBytesFromFile();
40+
return getPrivateKeyBytesFromTextFile();
4141
}
4242
final PrivKey privKey = KeyKt.generateKeyPair(KeyType.SECP256K1).component1();
4343
final Bytes privateKeyBytes = Bytes.wrap(KeyKt.marshalPrivateKey(privKey));
@@ -51,11 +51,21 @@ public Bytes getOrGeneratePrivateKeyBytes() {
5151
}
5252
}
5353

54-
private Bytes getPrivateKeyBytesFromFile() {
54+
private Bytes getPrivateKeyBytesFromTextFile() {
5555
try {
5656
final Bytes privateKeyBytes = Bytes.fromHexString(Files.readString(Paths.get(fileName)));
5757
STATUS_LOG.usingGeneratedP2pPrivateKey(fileName, false);
5858
return privateKeyBytes;
59+
} catch (IOException e) {
60+
return getPrivateKeyBytesFromBytesFile();
61+
}
62+
}
63+
64+
private Bytes getPrivateKeyBytesFromBytesFile() {
65+
try {
66+
final Bytes privateKeyBytes = Bytes.wrap(Files.readAllBytes(Paths.get(fileName)));
67+
STATUS_LOG.usingGeneratedP2pPrivateKey(fileName, false);
68+
return privateKeyBytes;
5969
} catch (IOException e) {
6070
throw new RuntimeException("p2p private key file not found - " + fileName);
6171
}

Diff for: 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)