@Test void getP2pPrivateKeyBytes_generatedKeyTest() throws IOException { GlobalConfiguration globalConfiguration = GlobalConfiguration.builder().setDataPath(dataDir.getCanonicalPath()).build(); globalConfig.set(globalConfiguration); BeaconChainController controller = new BeaconChainController(serviceConfig, beaconChainConfiguration()); MemKeyValueStore<String, Bytes> store = new MemKeyValueStore<>(); Bytes generatedPK = controller.getP2pPrivateKeyBytes(store); assertThat(generatedPK).isNotNull(); assertThat(generatedPK.size()).isGreaterThanOrEqualTo(32); Bytes loadedPK = controller.getP2pPrivateKeyBytes(store); assertThat(loadedPK).isEqualTo(generatedPK); store = new MemKeyValueStore<>(); Bytes generatedAnotherPK = controller.getP2pPrivateKeyBytes(store); assertThat(generatedAnotherPK).isNotNull(); assertThat(generatedAnotherPK.size()).isGreaterThanOrEqualTo(32); assertThat(generatedAnotherPK).isNotEqualTo(generatedPK); Path customPKFile = dataDir.toPath().resolve("customPK.hex"); Files.writeString(customPKFile, generatedPK.toHexString()); GlobalConfiguration globalConfiguration1 = GlobalConfiguration.builder() .setDataPath(dataDir.getCanonicalPath()) .setP2pPrivateKeyFile(customPKFile.toString()) .build(); globalConfig.set(globalConfiguration1); BeaconChainController controller1 = new BeaconChainController(serviceConfig, beaconChainConfiguration()); Bytes customPK = controller1.getP2pPrivateKeyBytes(store); assertThat(customPK).isEqualTo(generatedPK); }
@VisibleForTesting Bytes getP2pPrivateKeyBytes(KeyValueStore<String, Bytes> keyValueStore) { final Bytes privateKey; final String p2pPrivateKeyFile = config.getP2pPrivateKeyFile(); if (p2pPrivateKeyFile != null) { try { privateKey = Bytes.fromHexString(Files.readString(Paths.get(p2pPrivateKeyFile))); } catch (IOException e) { throw new RuntimeException("p2p private key file not found - " + p2pPrivateKeyFile); } } else { final Optional<Bytes> generatedKeyBytes = keyValueStore.get(GENERATED_NODE_KEY_KEY); if (generatedKeyBytes.isEmpty()) { final PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); privateKey = Bytes.wrap(KeyKt.marshalPrivateKey(privKey)); keyValueStore.put(GENERATED_NODE_KEY_KEY, privateKey); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, true); } else { privateKey = generatedKeyBytes.get(); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, false); } } return privateKey; }
BeaconChainController extends Service implements TimeTickChannel { @VisibleForTesting Bytes getP2pPrivateKeyBytes(KeyValueStore<String, Bytes> keyValueStore) { final Bytes privateKey; final String p2pPrivateKeyFile = config.getP2pPrivateKeyFile(); if (p2pPrivateKeyFile != null) { try { privateKey = Bytes.fromHexString(Files.readString(Paths.get(p2pPrivateKeyFile))); } catch (IOException e) { throw new RuntimeException("p2p private key file not found - " + p2pPrivateKeyFile); } } else { final Optional<Bytes> generatedKeyBytes = keyValueStore.get(GENERATED_NODE_KEY_KEY); if (generatedKeyBytes.isEmpty()) { final PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); privateKey = Bytes.wrap(KeyKt.marshalPrivateKey(privKey)); keyValueStore.put(GENERATED_NODE_KEY_KEY, privateKey); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, true); } else { privateKey = generatedKeyBytes.get(); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, false); } } return privateKey; } }
BeaconChainController extends Service implements TimeTickChannel { @VisibleForTesting Bytes getP2pPrivateKeyBytes(KeyValueStore<String, Bytes> keyValueStore) { final Bytes privateKey; final String p2pPrivateKeyFile = config.getP2pPrivateKeyFile(); if (p2pPrivateKeyFile != null) { try { privateKey = Bytes.fromHexString(Files.readString(Paths.get(p2pPrivateKeyFile))); } catch (IOException e) { throw new RuntimeException("p2p private key file not found - " + p2pPrivateKeyFile); } } else { final Optional<Bytes> generatedKeyBytes = keyValueStore.get(GENERATED_NODE_KEY_KEY); if (generatedKeyBytes.isEmpty()) { final PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); privateKey = Bytes.wrap(KeyKt.marshalPrivateKey(privKey)); keyValueStore.put(GENERATED_NODE_KEY_KEY, privateKey); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, true); } else { privateKey = generatedKeyBytes.get(); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, false); } } return privateKey; } BeaconChainController(
      final ServiceConfig serviceConfig, final BeaconChainConfiguration beaconConfig); }
BeaconChainController extends Service implements TimeTickChannel { @VisibleForTesting Bytes getP2pPrivateKeyBytes(KeyValueStore<String, Bytes> keyValueStore) { final Bytes privateKey; final String p2pPrivateKeyFile = config.getP2pPrivateKeyFile(); if (p2pPrivateKeyFile != null) { try { privateKey = Bytes.fromHexString(Files.readString(Paths.get(p2pPrivateKeyFile))); } catch (IOException e) { throw new RuntimeException("p2p private key file not found - " + p2pPrivateKeyFile); } } else { final Optional<Bytes> generatedKeyBytes = keyValueStore.get(GENERATED_NODE_KEY_KEY); if (generatedKeyBytes.isEmpty()) { final PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); privateKey = Bytes.wrap(KeyKt.marshalPrivateKey(privKey)); keyValueStore.put(GENERATED_NODE_KEY_KEY, privateKey); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, true); } else { privateKey = generatedKeyBytes.get(); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, false); } } return privateKey; } BeaconChainController(
      final ServiceConfig serviceConfig, final BeaconChainConfiguration beaconConfig); void initAll(); void initMetrics(); void initDepositProvider(); void initValidatorApiHandler(); void initP2PNetwork(); void initAttestationPool(); void initRestAPI(); void initBlockImporter(); void initSyncManager(); @Override void onTick(); }
BeaconChainController extends Service implements TimeTickChannel { @VisibleForTesting Bytes getP2pPrivateKeyBytes(KeyValueStore<String, Bytes> keyValueStore) { final Bytes privateKey; final String p2pPrivateKeyFile = config.getP2pPrivateKeyFile(); if (p2pPrivateKeyFile != null) { try { privateKey = Bytes.fromHexString(Files.readString(Paths.get(p2pPrivateKeyFile))); } catch (IOException e) { throw new RuntimeException("p2p private key file not found - " + p2pPrivateKeyFile); } } else { final Optional<Bytes> generatedKeyBytes = keyValueStore.get(GENERATED_NODE_KEY_KEY); if (generatedKeyBytes.isEmpty()) { final PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); privateKey = Bytes.wrap(KeyKt.marshalPrivateKey(privKey)); keyValueStore.put(GENERATED_NODE_KEY_KEY, privateKey); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, true); } else { privateKey = generatedKeyBytes.get(); STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, false); } } return privateKey; } BeaconChainController(
      final ServiceConfig serviceConfig, final BeaconChainConfiguration beaconConfig); void initAll(); void initMetrics(); void initDepositProvider(); void initValidatorApiHandler(); void initP2PNetwork(); void initAttestationPool(); void initRestAPI(); void initBlockImporter(); void initSyncManager(); @Override void onTick();  }