[PAN-1878] Provide error message when invalid key specified in key file (#1328)

* [PAN-1878] Provide error message when invalid key specified in key file

- display explicit message when invalid key file is specified

* Update KeyPairUtilTest.java

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Abdelhamid Bakhta 6 years ago committed by GitHub
parent da8c3255eb
commit 4c873ef4af
  1. 32
      pantheon/src/main/java/tech/pegasys/pantheon/controller/KeyPairUtil.java
  2. 37
      pantheon/src/test/java/tech/pegasys/pantheon/util/KeyPairUtilTest.java
  3. 1
      pantheon/src/test/resources/invalidPrivateKey.txt
  4. 1
      pantheon/src/test/resources/validPrivateKey.txt

@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.controller;
import tech.pegasys.pantheon.crypto.InvalidSEC256K1PrivateKeyStoreException;
import tech.pegasys.pantheon.crypto.SECP256K1;
import java.io.File;
@ -24,20 +25,25 @@ import org.apache.logging.log4j.Logger;
public class KeyPairUtil {
private static final Logger LOG = LogManager.getLogger();
public static SECP256K1.KeyPair loadKeyPair(final File keyFile) throws IOException {
final SECP256K1.KeyPair key;
if (keyFile.exists()) {
key = SECP256K1.KeyPair.load(keyFile);
LOG.info("Loaded key {} from {}", key.getPublicKey().toString(), keyFile.getAbsolutePath());
} else {
key = SECP256K1.KeyPair.generate();
key.getPrivateKey().store(keyFile);
LOG.info(
"Generated new key {} and stored it to {}",
key.getPublicKey().toString(),
keyFile.getAbsolutePath());
public static SECP256K1.KeyPair loadKeyPair(final File keyFile)
throws IOException, IllegalArgumentException {
try {
final SECP256K1.KeyPair key;
if (keyFile.exists()) {
key = SECP256K1.KeyPair.load(keyFile);
LOG.info("Loaded key {} from {}", key.getPublicKey().toString(), keyFile.getAbsolutePath());
} else {
key = SECP256K1.KeyPair.generate();
key.getPrivateKey().store(keyFile);
LOG.info(
"Generated new key {} and stored it to {}",
key.getPublicKey().toString(),
keyFile.getAbsolutePath());
}
return key;
} catch (InvalidSEC256K1PrivateKeyStoreException e) {
throw new IllegalArgumentException("Supplied file does not contain valid key pair.");
}
return key;
}
public static SECP256K1.KeyPair loadKeyPair(final Path homeDirectory) throws IOException {

@ -0,0 +1,37 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.util;
import static org.junit.Assert.assertNotNull;
import tech.pegasys.pantheon.controller.KeyPairUtil;
import java.io.File;
import org.junit.Test;
public class KeyPairUtilTest {
@Test
public void shouldLoadValidKeyPair() throws Exception {
assertNotNull(
KeyPairUtil.loadKeyPair(
new File(this.getClass().getResource("/validPrivateKey.txt").toURI())));
}
@Test(expected = IllegalArgumentException.class)
public void shouldNotLoadInvalidKeyPair() throws Exception {
KeyPairUtil.loadKeyPair(
new File(this.getClass().getResource("/invalidPrivateKey.txt").toURI()));
}
}

@ -0,0 +1 @@
this is not a valid private key

@ -0,0 +1 @@
000000000000000000000000000000000000000000000000000000000000000A
Loading…
Cancel
Save