[#14] add lombok and refactor exceptions. Provide validator.

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

@ -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);
}
}
}