Skip to content

Commit

Permalink
Assert that provided privateKey is hex encoded
Browse files Browse the repository at this point in the history
- Fixes #963
  • Loading branch information
Weisheme authored and weisheme committed Mar 31, 2018
1 parent aaba0de commit 29626ef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.ethereum.net.rlpx.Node;
import org.ethereum.util.BuildInfo;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.Utils;
import org.ethereum.validator.BlockCustomHashRule;
import org.ethereum.validator.BlockHeaderValidator;
import org.slf4j.Logger;
Expand Down Expand Up @@ -664,7 +665,7 @@ public String customSolcPath() {
public String privateKey() {
if (config.hasPath("peer.privateKey")) {
String key = config.getString("peer.privateKey");
if (key.length() != 64) {
if (key.length() != 64 || !Utils.isHexEncoded(key)) {
throw new RuntimeException("The peer.privateKey needs to be Hex encoded and 32 byte length");
}
return key;
Expand Down
13 changes: 13 additions & 0 deletions ethereumj-core/src/main/java/org/ethereum/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,17 @@ public static void sleep(long ms) {
Thread.currentThread().interrupt();
}
}

public static boolean isHexEncoded(String value) {
if (value == null) return false;
if ("".equals(value)) return true;

try {
//noinspection ResultOfMethodCallIgnored
new BigInteger(value, 16);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ private void assertInvalidPrivateKey(byte[] privateKey) {
} catch (RuntimeException ignore) { }
}

@Ignore
@Test
public void testExposeBugWhereNonHexEncodedIsAcceptedWithoutValidation() {
SystemProperties props = new SystemProperties();
Expand Down
21 changes: 20 additions & 1 deletion ethereumj-core/src/test/java/org/ethereum/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.math.BigInteger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Roman Mandeleil
Expand Down Expand Up @@ -105,8 +107,25 @@ public void testAddressStringToBytes() {
assertEquals(expected, result);
}

@Test
public void testIsHexEncoded() {
assertTrue(Utils.isHexEncoded("AAA"));
assertTrue(Utils.isHexEncoded("6c386a4b26f73c802f34673f7248bb118f97424a"));
assertFalse(Utils.isHexEncoded(null));
assertFalse(Utils.isHexEncoded("I am not hex"));
assertTrue(Utils.isHexEncoded(""));
assertTrue(Utils.isHexEncoded(
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a" +
"6c386a4b26f73c802f34673f7248bb118f97424a"));
}

@Test
public void testLongToTimePeriod() {
assertEquals("2.99s", Utils.longToTimePeriod(3000 - 12));
}
}
}

0 comments on commit 29626ef

Please sign in to comment.