GenerateNodeIdRandomly implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { ECKey key = new ECKey(); Properties props = new Properties(); props.setProperty("nodeIdPrivateKey", Hex.toHexString(key.getPrivKeyBytes())); props.setProperty("nodeId", Hex.toHexString(key.getNodeId())); File file = new File(databaseDir, "nodeId.properties"); file.getParentFile().mkdirs(); try (Writer writer = new FileWriter(file)) { props.store(writer, "Generated NodeID. To use your own nodeId please refer to 'peer.privateKey' config option."); } catch (IOException e) { throw new RuntimeException(e); } logger.info("New nodeID generated: " + props.getProperty("nodeId")); logger.info("Generated nodeID and its private key stored in " + file); return props.getProperty("nodeIdPrivateKey"); } GenerateNodeIdRandomly(String databaseDir); @Override String getNodePrivateKey();  }
@Test public void testGenerateNodeIdRandomlyCreatesFileWithNodeIdAndPrivateKey() throws Exception { File nodeIdPropertiesFile = new File("database-test/nodeId.properties"); nodeIdPropertiesFile.delete(); new GenerateNodeIdRandomly("database-test").getNodePrivateKey(); assertTrue(nodeIdPropertiesFile.exists()); String contents = FileCopyUtils.copyToString(new FileReader(nodeIdPropertiesFile)); String[] lines = StringUtils.tokenizeToStringArray(contents, "\n"); assertEquals(4, lines.length); assertTrue(lines[0].startsWith("#Generated NodeID.")); assertTrue(lines[1].startsWith("#")); assertTrue(lines[2].startsWith("nodeIdPrivateKey=")); assertEquals("nodeIdPrivateKey=".length() + 64, lines[2].length()); assertTrue(lines[3].startsWith("nodeId=")); assertEquals("nodeId=".length() + 128, lines[3].length()); nodeIdPropertiesFile.delete(); }