File tree 2 files changed +23
-3
lines changed
main/java/tech/pegasys/teku/networking/p2p/network/config
test/java/tech/pegasys/teku/networking/p2p/network/config
2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public Bytes getOrGeneratePrivateKeyBytes() {
37
37
try {
38
38
File file = new File (fileName );
39
39
if (!file .createNewFile ()) {
40
- return getPrivateKeyBytesFromFile ();
40
+ return getPrivateKeyBytesFromTextFile ();
41
41
}
42
42
final PrivKey privKey = KeyKt .generateKeyPair (KeyType .SECP256K1 ).component1 ();
43
43
final Bytes privateKeyBytes = Bytes .wrap (KeyKt .marshalPrivateKey (privKey ));
@@ -51,11 +51,21 @@ public Bytes getOrGeneratePrivateKeyBytes() {
51
51
}
52
52
}
53
53
54
- private Bytes getPrivateKeyBytesFromFile () {
54
+ private Bytes getPrivateKeyBytesFromTextFile () {
55
55
try {
56
56
final Bytes privateKeyBytes = Bytes .fromHexString (Files .readString (Paths .get (fileName )));
57
57
STATUS_LOG .usingGeneratedP2pPrivateKey (fileName , false );
58
58
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 ;
59
69
} catch (IOException e ) {
60
70
throw new RuntimeException ("p2p private key file not found - " + fileName );
61
71
}
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ void shouldCreateKeyAndSaveToFile(@TempDir Path tempDir) throws IOException {
37
37
}
38
38
39
39
@ Test
40
- void shouldGetKeyFromSavedFile (@ TempDir Path tempDir ) throws IOException {
40
+ void shouldGetKeyFromSavedTextFile (@ TempDir Path tempDir ) throws IOException {
41
41
final Path file = tempDir .resolve ("file.txt" );
42
42
final Bytes privateKey = Bytes .wrap (PrivateKeyGenerator .generate ().bytes ());
43
43
Files .writeString (file , privateKey .toHexString ());
@@ -46,6 +46,16 @@ void shouldGetKeyFromSavedFile(@TempDir Path tempDir) throws IOException {
46
46
assertThat (privateKeySource .getOrGeneratePrivateKeyBytes ()).isEqualTo (privateKey );
47
47
}
48
48
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
+
49
59
@ Test
50
60
void shouldThrowExceptionIfInvalidFileName (@ TempDir Path tempDir ) {
51
61
final PrivateKeySource privateKeySource =
You can’t perform that action at this time.
0 commit comments