[#14] add lombok and refactor exceptions. Provide validator.
All checks were successful
DCO / DCO (pull_request) Successful in 34s
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:
parent
15cf0893c7
commit
388428af76
87 changed files with 819 additions and 970 deletions
|
@ -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];
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue