[#45] Add the ability to create a client via wallet and password
All checks were successful
DCO / DCO (pull_request) Successful in 27s
Verify code phase / Verify code (pull_request) Successful in 1m38s

Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
Ori Bruk 2025-03-04 17:01:51 +03:00
parent fe7d2968b8
commit db74919401
24 changed files with 141 additions and 558 deletions

View file

@ -1,141 +0,0 @@
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 {
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
public static final int BASE58_SYMBOL_COUNT = 58;
public static final int BASE256_SYMBOL_COUNT = 256;
private static final int BYTE_DIVISION = 0xFF;
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];
static {
Arrays.fill(INDEXES, -1);
for (int i = 0; i < ALPHABET.length; i++) {
INDEXES[ALPHABET[i]] = i;
}
}
private Base58() {
}
public static byte[] base58CheckDecode(String input) {
if (StringUtils.isEmpty(input)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
byte[] buffer = decode(input);
if (buffer.length < 4) {
throw new ProcessFrostFSException(String.format(DECODE_LENGTH_VALUE_TEMPLATE, buffer.length));
}
byte[] decode = Arrays.copyOfRange(buffer, 0, buffer.length - 4);
byte[] checksum = getSha256(getSha256(decode));
var bufferEnd = Arrays.copyOfRange(buffer, buffer.length - 4, buffer.length);
var checksumStart = Arrays.copyOfRange(checksum, 0, 4);
if (!Arrays.equals(bufferEnd, checksumStart)) {
throw new ProcessFrostFSException(INVALID_CHECKSUM);
}
return decode;
}
public static String base58CheckEncode(byte[] data) {
if (isNull(data)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
byte[] checksum = getSha256(getSha256(data));
var buffer = concat(data, Arrays.copyOfRange(checksum, 0, 4));
return encode(buffer);
}
public static String encode(byte[] input) {
if (input.length == 0) {
return "";
}
// Count leading zeros.
int zeros = 0;
while (zeros < input.length && input[zeros] == 0) {
++zeros;
}
// Convert base-256 digits to base-58 digits (plus conversion to ASCII characters)
input = Arrays.copyOf(input, input.length); // since we modify it in-place
char[] encoded = new char[input.length * 2]; // upper bound
int outputStart = encoded.length;
for (int inputStart = zeros; inputStart < input.length; ) {
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, BASE256_SYMBOL_COUNT, BASE58_SYMBOL_COUNT)];
if (input[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
}
// Preserve exactly as many leading encoded zeros in output as there were leading zeros in input.
while (outputStart < encoded.length && encoded[outputStart] == ENCODED_ZERO) {
++outputStart;
}
while (--zeros >= 0) {
encoded[--outputStart] = ENCODED_ZERO;
}
// Return encoded string (including encoded leading zeros).
return new String(encoded, outputStart, encoded.length - outputStart);
}
public static byte[] decode(String input) {
if (input.isEmpty()) {
return new byte[0];
}
// Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).
byte[] input58 = new byte[input.length()];
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
if (digit < 0) {
throw new ValidationFrostFSException(String.format(INVALID_BASE58_CHARACTER_TEMPLATE, (int) c));
}
input58[i] = (byte) digit;
}
// Count leading zeros.
int zeros = 0;
while (zeros < input58.length && input58[zeros] == 0) {
++zeros;
}
// Convert base-58 digits to base-256 digits.
byte[] decoded = new byte[input.length()];
int outputStart = decoded.length;
for (int inputStart = zeros; inputStart < input58.length; ) {
decoded[--outputStart] = divmod(input58, inputStart, BASE58_SYMBOL_COUNT, BASE256_SYMBOL_COUNT);
if (input58[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
}
// Ignore extra leading zeroes that were added during the calculation.
while (outputStart < decoded.length && decoded[outputStart] == 0) {
++outputStart;
}
// Return decoded data (including original number of leading zeros).
return Arrays.copyOfRange(decoded, outputStart - zeros, decoded.length);
}
private static byte divmod(byte[] number, int firstDigit, int base, int divisor) {
// this is just long division which accounts for the base of the input digits
int remainder = 0;
for (int i = firstDigit; i < number.length; i++) {
int digit = (int) number[i] & BYTE_DIVISION;
int temp = remainder * base + digit;
number[i] = (byte) (temp / divisor);
remainder = temp % divisor;
}
return (byte) remainder;
}
}

View file

@ -4,7 +4,6 @@ import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import java.math.BigInteger;
import java.security.MessageDigest;
@ -15,24 +14,11 @@ import static java.util.Objects.isNull;
public class Helper {
private static final String SHA256 = "SHA-256";
private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20;
private static final int HEX_RADIX = 16;
private Helper() {
}
public static byte[] getRipemd160(byte[] value) {
if (isNull(value)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH];
var digest = new RIPEMD160Digest();
digest.update(value, 0, value.length);
digest.doFinal(hash, 0);
return hash;
}
public static MessageDigest getSha256Instance() {
try {
return MessageDigest.getInstance(SHA256);

View file

@ -2,7 +2,6 @@ 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;
import org.bouncycastle.asn1.x9.X9ECParameters;
@ -10,12 +9,7 @@ import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
import org.bouncycastle.math.ec.ECPoint;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
@ -24,63 +18,20 @@ import java.security.spec.ECParameterSpec;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.ECPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
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 info.frostfs.sdk.constants.ErrorConst.COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE;
import static info.frostfs.sdk.constants.ErrorConst.INPUT_PARAM_IS_MISSING;
import static java.util.Objects.isNull;
import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray;
public class KeyExtension {
public static final byte NEO_ADDRESS_VERSION = 0x35;
private static final String CURVE_NAME = "secp256r1";
private static final String SECURITY_ALGORITHM = "EC";
private static final int PS_IN_HASH160 = 0x0C;
private static final int DECODE_ADDRESS_LENGTH = 21;
private static final int COMPRESSED_PUBLIC_KEY_LENGTH = 33;
private static final int UNCOMPRESSED_PUBLIC_KEY_LENGTH = 65;
private static final int CHECK_SIG_DESCRIPTOR = ByteBuffer
.wrap(getSha256("System.Crypto.CheckSig".getBytes(StandardCharsets.US_ASCII)))
.order(ByteOrder.LITTLE_ENDIAN).getInt();
private KeyExtension() {
}
public static byte[] compress(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
throw new ValidationFrostFSException(String.format(
UNCOMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, UNCOMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
var secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
var point = secp256R1.getCurve().decodePoint(publicKey);
return point.getEncoded(true);
}
public static byte[] getPrivateKeyFromWIF(String wif) {
if (StringUtils.isEmpty(wif)) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
}
var data = Base58.base58CheckDecode(wif);
return Arrays.copyOfRange(data, 1, data.length - 1);
}
public static byte[] loadPublicKey(byte[] privateKey) {
checkInputValue(privateKey);
X9ECParameters params = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
ECDomainParameters domain = new ECDomainParameters(
params.getCurve(), params.getG(), params.getN(), params.getH()
);
ECPoint q = domain.getG().multiply(new BigInteger(1, privateKey));
ECPublicKeyParameters publicParams = new ECPublicKeyParameters(q, domain);
return publicParams.getQ().getEncoded(true);
}
public static PrivateKey loadPrivateKey(byte[] privateKey) {
checkInputValue(privateKey);
@ -134,58 +85,6 @@ public class KeyExtension {
}
}
public static byte[] getScriptHash(byte[] publicKey) {
checkInputValue(publicKey);
var script = createSignatureRedeemScript(publicKey);
return getRipemd160(getSha256(script));
}
public static String publicKeyToAddress(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new ValidationFrostFSException(String.format(
ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
return toAddress(getScriptHash(publicKey));
}
private static String toAddress(byte[] scriptHash) {
checkInputValue(scriptHash);
byte[] data = new byte[DECODE_ADDRESS_LENGTH];
data[0] = NEO_ADDRESS_VERSION;
System.arraycopy(scriptHash, 0, data, 1, scriptHash.length);
return Base58.base58CheckEncode(data);
}
private static byte[] getBytes(int value) {
byte[] buffer = new byte[4];
for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) (value >> i * Byte.SIZE);
}
return buffer;
}
private static byte[] createSignatureRedeemScript(byte[] publicKey) {
checkInputValue(publicKey);
if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) {
throw new ValidationFrostFSException(String.format(
ENCODED_COMPRESSED_PUBLIC_KEY_WRONG_LENGTH_TEMPLATE, COMPRESSED_PUBLIC_KEY_LENGTH, publicKey.length
));
}
var script = new byte[]{PS_IN_HASH160, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
script = ArrayHelper.concat(script, publicKey);
script = ArrayHelper.concat(script, new byte[]{UNCOMPRESSED_PUBLIC_KEY_LENGTH}); //SYSCALL
script = ArrayHelper.concat(script, getBytes(CHECK_SIG_DESCRIPTOR)); //Neo_Crypto_CheckSig
return script;
}
private static void checkInputValue(byte[] data) {
if (isNull(data) || data.length == 0) {
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);

View file

@ -1,56 +0,0 @@
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;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class Base58Test {
private static final String WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ";
private static final byte[] DECODE = new byte[]{
-128, -128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77,
-15, 122, -88, 72, 109, -47, 125, -80, -40, -38, 1
};
@Test
void base58DecodeEncode() {
//When + Then
assertEquals(WIF, Base58.base58CheckEncode(Base58.base58CheckDecode(WIF)));
}
@Test
void base58Decode_success() {
//When
var decode = Base58.base58CheckDecode(WIF);
//Then
assertThat(decode).hasSize(34).containsExactly(DECODE);
}
@Test
void base58Decode_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode(null));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode(""));
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckDecode("WIF"));
assertThrows(ProcessFrostFSException.class, () -> Base58.base58CheckDecode("fh"));
}
@Test
void base58Encode_success() {
//When
var encode = Base58.base58CheckEncode(DECODE);
//Then
assertEquals(WIF, encode);
}
@Test
void base58Encode_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> Base58.base58CheckEncode(null));
}
}

View file

@ -10,38 +10,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class HelperTest {
@Test
void getRipemd160_success() {
//Given
var value = new byte[]{1, 2, 3, 4, 5};
var expected = new byte[]
{-21, -126, 92, 75, 36, -12, 37, 7, 122, 6, 124, -61, -66, -12, 87, 120, 63, 90, -41, 5};
//When
var result = Helper.getRipemd160(value);
//Then
assertThat(result).hasSize(20).containsExactly(expected);
}
@Test
void getRipemd160_givenParamIsNull() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> Helper.getRipemd160(null));
}
@Test
void getRipemd160_givenParamsIsEmpty() {
//Given
var value = new byte[]{};
var expected = new byte[]
{-100, 17, -123, -91, -59, -23, -4, 84, 97, 40, 8, -105, 126, -24, -11, 72, -78, 37, -115, 49};
//When
var result = Helper.getRipemd160(value);
//Then
assertThat(result).hasSize(20).containsExactly(expected);
}
@Test
void getSha256Instance() {
//When

View file

@ -8,8 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class KeyExtensionTest {
private static final String WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ";
private static final String OWNER_ID = "NVxUSpEEJzYXZZtUs18PrJTD9QZkLLNQ8S";
private static final byte[] PRIVATE_KEY = new byte[]{
-128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77, -15,
122, -88, 72, 109, -47, 125, -80, -40, -38
@ -24,38 +22,6 @@ public class KeyExtensionTest {
-68, -73, 65, -57, -26, 75, 4, -51, -40, -20, 75, 89, -59, 111, 96, -80, 56, 13
};
@Test
void getPrivateKeyFromWIF_success() {
//When
var privateKey = KeyExtension.getPrivateKeyFromWIF(WIF);
//Then
assertThat(privateKey).hasSize(32).containsExactly(PRIVATE_KEY);
}
@Test
void getPrivateKeyFromWIF_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(""));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPrivateKeyFromWIF(null));
}
@Test
void loadPublicKey_success() {
//When
var publicKey = KeyExtension.loadPublicKey(PRIVATE_KEY);
//Then
assertThat(publicKey).hasSize(33).containsExactly(PUBLIC_KEY);
}
@Test
void loadPublicKey_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.loadPublicKey(new byte[]{}));
}
@Test
void loadPrivateKey_success() {
//When
@ -92,61 +58,4 @@ public class KeyExtensionTest {
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getPublicKeyFromBytes(PRIVATE_KEY));
}
@Test
void getScriptHash_success() {
//Given
var expected = new byte[]{
110, 42, -125, -76, -25, -44, -94, 22, -98, 117, -100, -5, 103, 74, -128, -51, 37, -116, -102, 71
};
//When
var hash = KeyExtension.getScriptHash(PUBLIC_KEY);
//Then
assertThat(hash).hasSize(20).containsExactly(expected);
}
@Test
void getScriptHash_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.getScriptHash(PRIVATE_KEY));
}
@Test
void publicKeyToAddress_success() {
//When
var address = KeyExtension.publicKeyToAddress(PUBLIC_KEY);
//Then
assertEquals(OWNER_ID, address);
}
@Test
void publicKeyToAddress_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.publicKeyToAddress(PRIVATE_KEY));
}
@Test
void compress_success() {
//When
var publicKey = KeyExtension.compress(UNCOMPRESSED_PUBLIC_KEY);
//Then
assertThat(publicKey).hasSize(33).containsExactly(PUBLIC_KEY);
}
@Test
void compress_wrong() {
//When + Then
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(null));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(new byte[]{}));
assertThrows(ValidationFrostFSException.class, () -> KeyExtension.compress(PUBLIC_KEY));
}
}