[#14] add lombok and refactor exceptions. Provide validator.
All checks were successful
DCO / DCO (pull_request) Successful in 34s

Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
Ori Bruk 2024-09-17 17:47:03 +03:00
parent 15cf0893c7
commit 388428af76
87 changed files with 819 additions and 970 deletions

View file

@ -15,10 +15,14 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<protobuf.version>3.23.0</protobuf.version>
</properties>
<dependencies>
<dependency>
<groupId>info.frostfs.sdk</groupId>
<artifactId>exceptions</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
@ -29,11 +33,6 @@
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>
</project>

View file

@ -1,16 +1,17 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import static info.frostfs.sdk.constants.ErrorConst.SOME_PARAM_IS_MISSING;
import static java.util.Objects.isNull;
public class ArrayHelper {
private static final String ERROR_MESSAGE = "One of the input parameters is null";
private ArrayHelper() {
}
public static byte[] concat(byte[] startArray, byte[] endArray) {
if (isNull(startArray) || isNull(endArray)) {
throw new IllegalArgumentException(ERROR_MESSAGE);
throw new ValidationFrostFSException(SOME_PARAM_IS_MISSING);
}
byte[] joinedArray = new byte[startArray.length + endArray.length];

View file

@ -1,11 +1,14 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import static info.frostfs.sdk.ArrayHelper.concat;
import static info.frostfs.sdk.Helper.getSha256;
import static info.frostfs.sdk.constants.ErrorConst.*;
import static java.util.Objects.isNull;
public class Base58 {
@ -16,8 +19,6 @@ public class Base58 {
private static final char ENCODED_ZERO = ALPHABET[0];
private static final char BASE58_ASCII_MAX_VALUE = 128;
private static final int[] INDEXES = new int[BASE58_ASCII_MAX_VALUE];
private static final String ERROR_VALUE_MISSING_MESSAGE = "Input value is missing";
private static final String ERROR_INVALID_CHARACTER_TEMPLATE = "Invalid character in Base58: 0x%04x";
static {
Arrays.fill(INDEXES, -1);
@ -31,12 +32,12 @@ public class Base58 {
public static byte[] base58CheckDecode(String input) {
if (StringUtils.isEmpty(input)) {
throw new IllegalArgumentException(ERROR_VALUE_MISSING_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
byte[] buffer = decode(input);
if (buffer.length < 4) {
throw new IllegalArgumentException();
throw new ProcessFrostFSException(String.format(DECODE_LENGTH_VALUE_TEMPLATE, buffer.length));
}
byte[] decode = Arrays.copyOfRange(buffer, 0, buffer.length - 4);
@ -44,7 +45,7 @@ public class Base58 {
var bufferEnd = Arrays.copyOfRange(buffer, buffer.length - 4, buffer.length);
var checksumStart = Arrays.copyOfRange(checksum, 0, 4);
if (!Arrays.equals(bufferEnd, checksumStart)) {
throw new IllegalArgumentException();
throw new ProcessFrostFSException(INVALID_CHECKSUM);
}
return decode;
@ -52,7 +53,7 @@ public class Base58 {
public static String base58CheckEncode(byte[] data) {
if (isNull(data)) {
throw new IllegalArgumentException(ERROR_VALUE_MISSING_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
byte[] checksum = getSha256(getSha256(data));
@ -101,7 +102,7 @@ public class Base58 {
char c = input.charAt(i);
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
if (digit < 0) {
throw new IllegalArgumentException(String.format(ERROR_INVALID_CHARACTER_TEMPLATE, (int) c));
throw new ValidationFrostFSException(String.format(INVALID_BASE58_CHARACTER_TEMPLATE, (int) c));
}
input58[i] = (byte) digit;
}

View file

@ -2,16 +2,17 @@ package info.frostfs.sdk;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING;
import static java.util.Objects.isNull;
public class Helper {
private static final String ERROR_MESSAGE = "Input value is missing";
private static final String SHA256 = "SHA-256";
private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20;
@ -20,7 +21,7 @@ public class Helper {
public static byte[] getRipemd160(byte[] value) {
if (isNull(value)) {
throw new IllegalArgumentException(ERROR_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH];
@ -40,7 +41,7 @@ public class Helper {
public static byte[] getSha256(byte[] value) {
if (isNull(value)) {
throw new IllegalArgumentException(ERROR_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
return getSha256Instance().digest(value);
@ -48,7 +49,7 @@ public class Helper {
public static ByteString getSha256(Message value) {
if (isNull(value)) {
throw new IllegalArgumentException(ERROR_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
return ByteString.copyFrom(getSha256(value.toByteArray()));
@ -56,7 +57,7 @@ public class Helper {
public static String getHexString(byte[] value) {
if (isNull(value) || value.length == 0) {
throw new IllegalArgumentException(ERROR_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
return String.format("%0" + (value.length << 1) + "x", new BigInteger(1, value));

View file

@ -1,5 +1,7 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.sec.SECNamedCurves;
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
@ -26,6 +28,7 @@ import java.util.Arrays;
import static info.frostfs.sdk.Helper.getRipemd160;
import static info.frostfs.sdk.Helper.getSha256;
import static info.frostfs.sdk.constants.ErrorConst.*;
import static java.util.Objects.isNull;
import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray;
@ -40,13 +43,6 @@ public class KeyExtension {
private static final int CHECK_SIG_DESCRIPTOR = ByteBuffer
.wrap(getSha256("System.Crypto.CheckSig".getBytes(StandardCharsets.US_ASCII)))
.order(ByteOrder.LITTLE_ENDIAN).getInt();
private static final String ERROR_VALUE_MISSING_MESSAGE = "Input value is missing";
private static final String ERROR_ENCODED_COMPRESSED_PUBLIC_KEY_TEMPLATE =
"PublicKey isn't encoded compressed public key. Expected length=%s, actual=%s";
private static final String ERROR_UNCOMPRESSED_PUBLIC_KEY_TEMPLATE =
"Compress argument isn't uncompressed public key. Expected length=%s, actual=%s";
private static final String ERROR_COMPRESSED_PUBLIC_KEY_TEMPLATE =
"Decompress argument isn't compressed public key. Expected length=%s, actual=%s";
private KeyExtension() {
}
@ -54,8 +50,8 @@ public class KeyExtension {
public static byte[] compress(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
throw new IllegalArgumentException(String.format(
ERROR_UNCOMPRESSED_PUBLIC_KEY_TEMPLATE, UNCOMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
throw new ValidationFrostFSException(String.format(
UNCOMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, UNCOMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
@ -66,7 +62,7 @@ public class KeyExtension {
public static byte[] getPrivateKeyFromWIF(String wif) {
if (StringUtils.isEmpty(wif)) {
throw new IllegalArgumentException(ERROR_VALUE_MISSING_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
var data = Base58.base58CheckDecode(wif);
@ -102,7 +98,7 @@ public class KeyExtension {
KeyFactory kf = KeyFactory.getInstance(SECURITY_ALGORITHM);
return kf.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
throw new ProcessFrostFSException(e);
}
}
@ -110,9 +106,9 @@ public class KeyExtension {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new IllegalArgumentException(
String.format(ERROR_COMPRESSED_PUBLIC_KEY_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length)
);
throw new ValidationFrostFSException(String.format(
COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
X9ECParameters secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
@ -134,7 +130,7 @@ public class KeyExtension {
KeyFactory kf = KeyFactory.getInstance(SECURITY_ALGORITHM);
return kf.generatePublic(publicKeySpec);
} catch (Exception e) {
throw new RuntimeException(e);
throw new ProcessFrostFSException(e);
}
}
@ -148,8 +144,8 @@ public class KeyExtension {
public static String publicKeyToAddress(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new IllegalArgumentException(String.format(
ERROR_ENCODED_COMPRESSED_PUBLIC_KEY_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
throw new ValidationFrostFSException(String.format(
ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
@ -177,8 +173,8 @@ public class KeyExtension {
private static byte[] createSignatureRedeemScript(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new IllegalArgumentException(String.format(
ERROR_ENCODED_COMPRESSED_PUBLIC_KEY_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
throw new ValidationFrostFSException(String.format(
ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
@ -192,7 +188,7 @@ public class KeyExtension {
private static void checkInputValue(byte[] data) {
if (isNull(data) || data.length == 0) {
throw new IllegalArgumentException(ERROR_VALUE_MISSING_MESSAGE);
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
}
}

View file

@ -1,5 +1,6 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -59,8 +60,8 @@ public class ArrayHelperTest {
var endBytes = new byte[]{6, 7, 8, 9};
//When + Then
assertThrows(IllegalArgumentException.class, () -> ArrayHelper.concat(startBytes, null));
assertThrows(IllegalArgumentException.class, () -> ArrayHelper.concat(null, endBytes));
assertThrows(IllegalArgumentException.class, () -> ArrayHelper.concat(null, null));
assertThrows(ValidationFrostFSException.class, () -> ArrayHelper.concat(startBytes, null));
assertThrows(ValidationFrostFSException.class, () -> ArrayHelper.concat(null, endBytes));
assertThrows(ValidationFrostFSException.class, () -> ArrayHelper.concat(null, null));
}
}

View file

@ -1,5 +1,7 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,10 +33,10 @@ public class Base58Test {
@Test
void base58Decode_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode(null));
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode(""));
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode("WIF"));
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckDecode("fh"));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode(null));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode(""));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode("WIF"));
assertThrows(ProcessFrostFSException.class, () -> Base58.base58CheckDecode("fh"));
}
@Test
@ -49,6 +51,6 @@ public class Base58Test {
@Test
void base58Encode_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> Base58.base58CheckEncode(null));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckEncode(null));
}
}

View file

@ -1,6 +1,7 @@
package info.frostfs.sdk;
import com.google.protobuf.Message;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -25,7 +26,7 @@ public class HelperTest {
@Test
void getRipemd160_givenParamIsNull() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> Helper.getRipemd160(null));
assertThrows(ValidationFrostFSException.class, () -> Helper.getRipemd160(null));
}
@Test
@ -69,8 +70,8 @@ public class HelperTest {
@Test
void getSha256_bytes_givenParamIsNull() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> Helper.getSha256((byte[]) null));
assertThrows(IllegalArgumentException.class, () -> Helper.getSha256((Message) null));
assertThrows(ValidationFrostFSException.class, () -> Helper.getSha256((byte[]) null));
assertThrows(ValidationFrostFSException.class, () -> Helper.getSha256((Message) null));
}
@Test
@ -104,7 +105,7 @@ public class HelperTest {
@Test
void getHexString_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> Helper.getHexString(null));
assertThrows(IllegalArgumentException.class, () -> Helper.getHexString(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> Helper.getHexString(null));
assertThrows(ValidationFrostFSException.class, () -> Helper.getHexString(new byte[]{}));
}
}

View file

@ -1,5 +1,6 @@
package info.frostfs.sdk;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -35,8 +36,8 @@ public class KeyExtensionTest {
@Test
void getPrivateKeyFromWIF_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPrivateKeyFromWIF(""));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPrivateKeyFromWIF(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(""));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(null));
}
@Test
@ -51,8 +52,8 @@ public class KeyExtensionTest {
@Test
void loadPublicKey_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPublicKey(null));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPublicKey(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(new byte[]{}));
}
@Test
@ -69,8 +70,8 @@ public class KeyExtensionTest {
@Test
void loadPrivateKey_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPrivateKey(null));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.loadPrivateKey(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPrivateKey(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPrivateKey(new byte[]{}));
}
@Test
@ -87,9 +88,9 @@ public class KeyExtensionTest {
@Test
void getPublicKeyFromBytes_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPublicKeyFromBytes(null));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPublicKeyFromBytes(new byte[]{}));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getPublicKeyFromBytes(PRIVATE_KEY));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(PRIVATE_KEY));
}
@Test
@ -109,9 +110,9 @@ public class KeyExtensionTest {
@Test
void getScriptHash_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getScriptHash(null));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getScriptHash(new byte[]{}));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.getScriptHash(PRIVATE_KEY));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(PRIVATE_KEY));
}
@Test
@ -126,9 +127,9 @@ public class KeyExtensionTest {
@Test
void publicKeyToAddress_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.publicKeyToAddress(null));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.publicKeyToAddress(new byte[]{}));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.publicKeyToAddress(PRIVATE_KEY));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(PRIVATE_KEY));
}
@Test
@ -143,9 +144,9 @@ public class KeyExtensionTest {
@Test
void compress_wrong() {
//When + Then
assertThrows(IllegalArgumentException.class, () -> KeyExtension.compress(null));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.compress(new byte[]{}));
assertThrows(IllegalArgumentException.class, () -> KeyExtension.compress(PUBLIC_KEY));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(PUBLIC_KEY));
}
}