@Test public void peerGenerate_shouldDisplayErrorIfFileNotFound() { PeerCommand peerCommand = new PeerCommand(); final String fileWhichDoesNotExist = "directory-does-not-exist/config.dat"; assertThatThrownBy( () -> { peerCommand.validateParamsAndGenerate(fileWhichDoesNotExist, 3); }) .isInstanceOf(InvalidConfigurationException.class) .hasMessageContaining("existing directory"); }
void validateParamsAndGenerate(String outputFile, int number) throws IOException { try { File f = new File(outputFile); if (f.exists()) { throw new InvalidConfigurationException( String.format( "Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile)); } FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset()); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)"); for (int i = 0; i < number; i++) { PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); PubKey pubKey = privKey.publicKey(); PeerId peerId = PeerId.fromPubKey(pubKey); printWriter.println( Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58()); } printWriter.close(); } catch (final FileNotFoundException ex) { throw new InvalidConfigurationException( "use --output-file to point to a file in an existing directory " + ex.getMessage()); } }
PeerCommand { void validateParamsAndGenerate(String outputFile, int number) throws IOException { try { File f = new File(outputFile); if (f.exists()) { throw new InvalidConfigurationException( String.format( "Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile)); } FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset()); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)"); for (int i = 0; i < number; i++) { PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); PubKey pubKey = privKey.publicKey(); PeerId peerId = PeerId.fromPubKey(pubKey); printWriter.println( Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58()); } printWriter.close(); } catch (final FileNotFoundException ex) { throw new InvalidConfigurationException( "use --output-file to point to a file in an existing directory " + ex.getMessage()); } } }
PeerCommand { void validateParamsAndGenerate(String outputFile, int number) throws IOException { try { File f = new File(outputFile); if (f.exists()) { throw new InvalidConfigurationException( String.format( "Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile)); } FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset()); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)"); for (int i = 0; i < number; i++) { PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); PubKey pubKey = privKey.publicKey(); PeerId peerId = PeerId.fromPubKey(pubKey); printWriter.println( Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58()); } printWriter.close(); } catch (final FileNotFoundException ex) { throw new InvalidConfigurationException( "use --output-file to point to a file in an existing directory " + ex.getMessage()); } }  }
PeerCommand { void validateParamsAndGenerate(String outputFile, int number) throws IOException { try { File f = new File(outputFile); if (f.exists()) { throw new InvalidConfigurationException( String.format( "Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile)); } FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset()); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)"); for (int i = 0; i < number; i++) { PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); PubKey pubKey = privKey.publicKey(); PeerId peerId = PeerId.fromPubKey(pubKey); printWriter.println( Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58()); } printWriter.close(); } catch (final FileNotFoundException ex) { throw new InvalidConfigurationException( "use --output-file to point to a file in an existing directory " + ex.getMessage()); } }  @Command( name = "generate", description = "Generate a list of peer ids", mixinStandardHelpOptions = true, showDefaultValues = true, abbreviateSynopsis = true, versionProvider = PicoCliVersionProvider.class, synopsisHeading = "%n", descriptionHeading = "%nDescription:%n%n", optionListHeading = "%nOptions:%n", footerHeading = "%n", footer = "Teku is licensed under the Apache License 2.0") void generate(
      @Mixin PeerGenerationParams params,
      @Option(
              names = {"-n", "--number"},
              arity = "1",
              required = true,
              description = "number of peerIDs to generate")
          int number); }
PeerCommand { void validateParamsAndGenerate(String outputFile, int number) throws IOException { try { File f = new File(outputFile); if (f.exists()) { throw new InvalidConfigurationException( String.format( "Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile)); } FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset()); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)"); for (int i = 0; i < number; i++) { PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1(); PubKey pubKey = privKey.publicKey(); PeerId peerId = PeerId.fromPubKey(pubKey); printWriter.println( Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58()); } printWriter.close(); } catch (final FileNotFoundException ex) { throw new InvalidConfigurationException( "use --output-file to point to a file in an existing directory " + ex.getMessage()); } }  @Command( name = "generate", description = "Generate a list of peer ids", mixinStandardHelpOptions = true, showDefaultValues = true, abbreviateSynopsis = true, versionProvider = PicoCliVersionProvider.class, synopsisHeading = "%n", descriptionHeading = "%nDescription:%n%n", optionListHeading = "%nOptions:%n", footerHeading = "%n", footer = "Teku is licensed under the Apache License 2.0") void generate(
      @Mixin PeerGenerationParams params,
      @Option(
              names = {"-n", "--number"},
              arity = "1",
              required = true,
              description = "number of peerIDs to generate")
          int number);  }