Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,20 +25,25 @@
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
1 change: 1 addition & 0 deletions pantheon/src/test/resources/invalidPrivateKey.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is not a valid private key
1 change: 1 addition & 0 deletions pantheon/src/test/resources/validPrivateKey.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
000000000000000000000000000000000000000000000000000000000000000A