Skip to content

Commit

Permalink
Move null check from HexKeyParam to the importRawKey method
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoreliovlabs committed Sep 19, 2023
1 parent d770ff7 commit 016b547
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public String[] listAccounts() {
@Override
public String importRawKey(HexKeyParam keyParam, String passphrase) {
String s = null;
String key = Optional.ofNullable(keyParam).map(HexKeyParam::getHexKey).orElse(null);
String key = Optional.ofNullable(keyParam).map(HexKeyParam::getHexKey)
.orElseThrow(
() -> RskJsonRpcRequestException.invalidParamError("Key cannot be null.")
);

try {
if (key != null && key.startsWith("0x")) {
key = key.substring(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public class HexKeyParam implements Serializable {
private final String hexKey;

public HexKeyParam(String hexKey) {
if (hexKey == null || hexKey.isEmpty()) {
throw RskJsonRpcRequestException.invalidParamError("Invalid key: empty or null.");
}

boolean hasPrefix = HexUtils.hasHexPrefix(hexKey);
if (!HexUtils.isHex(hexKey.toLowerCase(), hasPrefix ? 2 : 0)) {
throw RskJsonRpcRequestException.invalidParamError("Invalid param " + hexKey + ": value must be a valid hex.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import co.rsk.config.TestSystemProperties;
import co.rsk.core.RskAddress;
import co.rsk.core.Wallet;
import org.bouncycastle.util.encoders.DecoderException;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.ECKey;
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
import org.ethereum.rpc.parameters.HexKeyParam;
import org.ethereum.util.ByteUtil;
import org.junit.jupiter.api.Assertions;
Expand All @@ -26,7 +26,7 @@ class PersonalModuleWalletEnabledTest {
@Test
void importRawKey_KeyIsNull_ThrowsNullPointerException() {
PersonalModuleWalletEnabled personalModuleWalletEnabled = createPersonalModuleWalletEnabled(null);
Assertions.assertThrows(DecoderException.class, () -> personalModuleWalletEnabled.importRawKey(null, "passphrase1"));
Assertions.assertThrows(RskJsonRpcRequestException.class, () -> personalModuleWalletEnabled.importRawKey(null, "passphrase1"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ void testInvalidHexKeyParam() {
String invalidHexValue = "0xcd3376bb711cb332ee3fb2ca04c6a8b9f70c316fcdf7a1f44ef4c79994832zxt";

assertThrows(RskJsonRpcRequestException.class, () -> new HexKeyParam(invalidHexValue));
assertThrows(RskJsonRpcRequestException.class, () -> new HexKeyParam(null));
assertThrows(RskJsonRpcRequestException.class, () -> new HexKeyParam(""));
}
}

0 comments on commit 016b547

Please sign in to comment.