Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
7518893388
commit
ab8a574d0d
14 changed files with 139 additions and 25 deletions
|
@ -10,8 +10,12 @@ 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 int[] INDEXES = new int[128];
|
||||
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);
|
||||
|
@ -69,7 +73,7 @@ public class Base58 {
|
|||
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, 256, 58)];
|
||||
encoded[--outputStart] = ALPHABET[divmod(input, inputStart, BASE256_SYMBOL_COUNT, BASE58_SYMBOL_COUNT)];
|
||||
if (input[inputStart] == 0) {
|
||||
++inputStart; // optimization - skip leading zeros
|
||||
}
|
||||
|
@ -93,7 +97,7 @@ public class Base58 {
|
|||
byte[] input58 = new byte[input.length()];
|
||||
for (int i = 0; i < input.length(); ++i) {
|
||||
char c = input.charAt(i);
|
||||
int digit = c < 128 ? INDEXES[c] : -1;
|
||||
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
|
||||
if (digit < 0) {
|
||||
throw new IllegalArgumentException(String.format("Invalid character in Base58: 0x%04x", (int) c));
|
||||
}
|
||||
|
@ -108,7 +112,7 @@ public class Base58 {
|
|||
byte[] decoded = new byte[input.length()];
|
||||
int outputStart = decoded.length;
|
||||
for (int inputStart = zeros; inputStart < input58.length; ) {
|
||||
decoded[--outputStart] = divmod(input58, inputStart, 58, 256);
|
||||
decoded[--outputStart] = divmod(input58, inputStart, BASE58_SYMBOL_COUNT, BASE256_SYMBOL_COUNT);
|
||||
if (input58[inputStart] == 0) {
|
||||
++inputStart; // optimization - skip leading zeros
|
||||
}
|
||||
|
@ -125,7 +129,7 @@ public class Base58 {
|
|||
// 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] & 0xFF;
|
||||
int digit = (int) number[i] & BYTE_DIVISION;
|
||||
int temp = remainder * base + digit;
|
||||
number[i] = (byte) (temp / divisor);
|
||||
remainder = temp % divisor;
|
||||
|
|
|
@ -11,15 +11,17 @@ import java.security.NoSuchAlgorithmException;
|
|||
import static java.util.Objects.isNull;
|
||||
|
||||
public class Helper {
|
||||
private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20;
|
||||
|
||||
private Helper() {
|
||||
}
|
||||
|
||||
public static byte[] getRIPEMD160(byte[] value) {
|
||||
public static byte[] getRipemd160(byte[] value) {
|
||||
if (isNull(value)) {
|
||||
throw new IllegalArgumentException("Input value is missing");
|
||||
}
|
||||
|
||||
var hash = new byte[20];
|
||||
var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH];
|
||||
var digest = new RIPEMD160Digest();
|
||||
digest.update(value, 0, value.length);
|
||||
digest.doFinal(hash, 0);
|
||||
|
|
|
@ -24,13 +24,14 @@ 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.getRipemd160;
|
||||
import static info.frostfs.sdk.Helper.getSha256;
|
||||
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 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;
|
||||
|
@ -134,7 +135,7 @@ public class KeyExtension {
|
|||
checkInputValue(publicKey);
|
||||
|
||||
var script = createSignatureRedeemScript(publicKey);
|
||||
return getRIPEMD160(getSha256(script));
|
||||
return getRipemd160(getSha256(script));
|
||||
}
|
||||
|
||||
public static String publicKeyToAddress(byte[] publicKey) {
|
||||
|
@ -161,7 +162,7 @@ public class KeyExtension {
|
|||
byte[] buffer = new byte[4];
|
||||
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = (byte) (value >> i * 8);
|
||||
buffer[i] = (byte) (value >> i * Byte.SIZE);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
|
@ -176,10 +177,10 @@ public class KeyExtension {
|
|||
);
|
||||
}
|
||||
|
||||
var script = new byte[]{0x0c, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
|
||||
var script = new byte[]{PS_IN_HASH160, COMPRESSED_PUBLIC_KEY_LENGTH}; //PUSHDATA1 33
|
||||
|
||||
script = ArrayHelper.concat(script, publicKey);
|
||||
script = ArrayHelper.concat(script, new byte[]{0x41}); //SYSCALL
|
||||
script = ArrayHelper.concat(script, new byte[]{UNCOMPRESSED_PUBLIC_KEY_LENGTH}); //SYSCALL
|
||||
script = ArrayHelper.concat(script, getBytes(CHECK_SIG_DESCRIPTOR)); //Neo_Crypto_CheckSig
|
||||
return script;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue