Skip to content

Commit

Permalink
small optimizations Address, Utils, additional tests TestAddress, Tes…
Browse files Browse the repository at this point in the history
…tUtils
  • Loading branch information
Isyahex committed Aug 10, 2024
1 parent eac7650 commit 346737f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 46 deletions.
24 changes: 13 additions & 11 deletions address/src/main/java/org/ton/java/address/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,39 @@ public Address(String address) {
throw new IllegalArgumentException("Address is null");
}

if (!address.contains(":")) {
if (address.contains("-") || address.contains("_")) {
if (address.indexOf(':') == -1) {
if (address.indexOf('-') != -1 || address.indexOf('_') != -1) {
isUrlSafe = true;
//convert to unsafe URL
address = address.replace("-", "+").replace("_", "/");
address = address.replace('-', '+').replace('_', '/');
} else {
isUrlSafe = false;
}
}

if (address.indexOf(':') > -1) {
String[] arr = address.split(":");
int colonIndex = address.indexOf(':');
if (colonIndex != -1) {

if (arr.length != 2) {
if (colonIndex != address.lastIndexOf(':')) {
throw new Error("Invalid address " + address);
}

byte wcInternal = Byte.parseByte(arr[0]);
String wcPart = address.substring(0, colonIndex);
String hexPart = address.substring(colonIndex + 1);

byte wcInternal = Byte.parseByte(wcPart);

if (wcInternal != 0 && wcInternal != -1) {
throw new Error("Invalid address wc " + address);
}

String hex = arr[1];
if (hex.length() != 64) {
if (hexPart.length() != 64) {
throw new Error("Invalid address hex " + address);
}

isUserFriendly = false;
wc = wcInternal;
hashPart = Utils.hexToSignedBytes(hex);
hashPart = Utils.hexToSignedBytes(hexPart);
isTestOnly = false;
isBounceable = false;
} else {
Expand Down Expand Up @@ -206,7 +208,7 @@ public String toString(boolean isUserFriendly,
byte[] addr = new byte[34];
byte[] addressWithChecksum = new byte[36];
addr[0] = (byte) tag;
addr[1] = (byte) wc;
addr[1] = wc;

System.arraycopy(hashPart, 0, addr, 2, 32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void testBadAddress() {
Address.of("bad_input");
Address.of("kf_8uRo6OBbQ97jCx2EIuKm8Wmt6Vb15-KsQHFLbKSMiYInz");
Address.of("ov_8uRo6OBbQ97jCx2EIuKm8Wmt6Vb15-KsQHFLbKSMiYMg3");
Address.of("0:8uRo6OBbQ97jCx2EIuKm8Wmt6Vb15:KsQHFLbKSMiYMg3");
});
}

Expand Down
71 changes: 36 additions & 35 deletions utils/src/main/java/org/ton/java/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Base64;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -205,8 +204,6 @@ public static String sha256(final String base) {

public static String sha256(int[] bytes) {
byte[] converted = new byte[bytes.length];
Byte[] a;

for (int i = 0; i < bytes.length; i++) {
converted[i] = (byte) (bytes[i] & 0xff);
}
Expand Down Expand Up @@ -289,15 +286,15 @@ public static String sha1(byte[] bytes) {
public static String bitsToDec(boolean[] bits) {
StringBuilder s = new StringBuilder();
for (boolean b : bits) {
s.append(b ? "1" : "0");
s.append(b ? '1' : '0');
}
return new BigInteger(s.toString(), 2).toString(10);
}

public static String bitsToHex(boolean[] bits) {
StringBuilder s = new StringBuilder();
for (boolean b : bits) {
s.append(b ? "1" : "0");
s.append(b ? '1' : '0');
}
return new BigInteger(s.toString(), 2).toString(16);
}
Expand Down Expand Up @@ -442,11 +439,11 @@ public static int[] bitStringToIntArray(String bitString) {
if (bitString.isEmpty()) {
return new int[0];
}
String bin = bitString;
int[] result = new int[(int) Math.ceil(bin.length() / (double) 8)];
int sz = bitString.length();
int[] result = new int[(sz + 7) / 8];

for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == '1') {
for (int i = 0; i < sz; i++) {
if (bitString.charAt(i) == '1') {
result[(i / 8)] |= 1 << (7 - (i % 8));
} else {
result[(i / 8)] &= ~(1 << (7 - (i % 8)));
Expand All @@ -460,13 +457,14 @@ public static byte[] bitStringToByteArray(String bitString) {
if (bitString.isEmpty()) {
return new byte[0];
}
byte[] result = new byte[(byte) Math.ceil(bitString.length() / (double) 8)];
int sz = bitString.length();
byte[] result = new byte[(sz + 7) / 8];

for (int i = 0; i < bitString.length(); i++) {
for (int i = 0; i < sz; i++) {
if (bitString.charAt(i) == '1') {
result[(i / 8)] |= 1 << (7 - (i % 8));
result[(i / 8)] |= (byte) (1 << (7 - (i % 8)));
} else {
result[(i / 8)] &= ~(1 << (7 - (i % 8)));
result[(i / 8)] &= (byte) ~(1 << (7 - (i % 8)));
}
}

Expand Down Expand Up @@ -513,7 +511,6 @@ public static int dynInt(int[] data) {
public static byte[] dynamicIntBytes(BigInteger val, int sz) {
byte[] tmp = new byte[8];
byte[] valArray = val.toByteArray(); // test just return val.toByteArray()
Arrays.fill(tmp, 0, 8 - val.toByteArray().length, (byte) 0);
for (int i = 8 - valArray.length, j = 0; i < 8; i++, j++) {
tmp[i] = valArray[j];
}
Expand Down Expand Up @@ -549,11 +546,9 @@ private static byte[] hexStringToByteArray(String s) {
}

private static int[] hexStringToIntArray(String s) {
String hex = s;
int[] result = new int[hex.length() / 2];
int j = 0;
for (String str : hex.split("(?<=\\G.{2})")) {
result[j++] = Integer.parseInt(str, 16);
int[] result = new int[s.length() / 2];
for (int i = 0; i < s.length(); i += 2) {
result[i / 2] = Integer.parseInt(s.substring(i, i + 2), 16);
}
return result;
}
Expand Down Expand Up @@ -775,7 +770,6 @@ public static String formatJettonValue(String jettons, int decimals, int scale)
}

public static String formatJettonValue(BigInteger jettons, int decimals, int scale) {
// return String.format("%,.2f", new BigDecimal(jettons));
return String.format("%,." + scale + "f", new BigDecimal(jettons).divide(BigDecimal.valueOf(Math.pow(10, decimals))), scale, RoundingMode.HALF_UP);
}

Expand All @@ -786,7 +780,6 @@ public static String formatNanoValue(BigInteger nanoCoins, int scale) {

public static void sleep(long seconds) {
try {
// System.out.println("pause " + seconds + " seconds");
TimeUnit.SECONDS.sleep(seconds);
} catch (Throwable e) {
System.out.println(e.getMessage());
Expand All @@ -803,8 +796,14 @@ public static void sleep(long seconds, String text) {
}

public static int ip2int(String address) {
String[] parts = address.split(Pattern.quote("."));

if (parts.length != 4) {
throw new Error("Invalid IP address format.");
}

int result = 0;
for (String part : address.split(Pattern.quote("."))) {
for (String part : parts) {
result = result << 8;
result |= Integer.parseInt(part);
}
Expand All @@ -823,23 +822,25 @@ public static String int2ip(long ip) {
}

public static int[] reverseIntArray(int[] in) {
int[] temp = in.clone();
for (int i = 0; i < temp.length / 2; i++) {
int t = temp[i];
temp[i] = temp[temp.length - i - 1];
temp[temp.length - i - 1] = t;
int i = 0, j = in.length - 1;
while (i < j) {
int tmp = in[i];
in[i] = in[j];
in[j] = tmp;
i++; j--;
}
return temp;
return in;
}

public static byte[] reverseByteArray(byte[] in) {
byte[] temp = in.clone();
for (int i = 0; i < temp.length / 2; i++) {
byte t = temp[i];
temp[i] = temp[temp.length - i - 1];
temp[temp.length - i - 1] = t;
}
return temp;
int i = 0, j = in.length - 1;
while (i < j) {
byte tmp = in[i];
in[i] = in[j];
in[j] = tmp;
i++; j--;
}
return in;
}

public static long unsignedIntToLong(int x) {
Expand Down
12 changes: 12 additions & 0 deletions utils/src/test/java/org/ton/java/utils/TestEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
@RunWith(JUnit4.class)
public class TestEncryption {

@Test
public void testMd5() {
String md5 = Utils.md5("ABC".getBytes());
assertThat(md5).isEqualTo("902fbdd2b1df0c4f70b4a5d23525e932");
}

@Test
public void testSha1() {
String sha1 = Utils.sha1("ABC".getBytes());
assertThat(sha1).isEqualTo("3c01bdbb26f358bab27f267924aa2c9a03fcfdb8");
}

@Test
public void testSha256() {
String sha256 = Utils.sha256("ABC");
Expand Down
61 changes: 61 additions & 0 deletions utils/src/test/java/org/ton/java/utils/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.junit.Assert.assertThrows;
import static org.ton.java.utils.Utils.getCRC32ChecksumAsLong;
import static org.ton.java.utils.Utils.int2ip;
import static org.ton.java.utils.Utils.ip2int;

@RunWith(JUnit4.class)
@Slf4j
Expand Down Expand Up @@ -195,6 +196,18 @@ public void testHexStringToBase64() throws DecoderException {
assertThat(base642).isEqualTo("0QAs9VlT6S776tq3unJcP5Ogsj+ELLunLXuOb1EKcOQi4+QO");
}

@Test
public void testBinaryStringToIntArray() {
int[] converted = Utils.bitStringToIntArray("101010011");
assertThat(converted).isEqualTo(new int[] { 169, 128 });

int[] converted2 = Utils.bitStringToIntArray("111010101");
assertThat(converted2).isEqualTo(new int[] { 234, 128 });

int[] converted3 = Utils.bitStringToIntArray("100001001");
assertThat(converted3).isEqualTo(new int[] { 132, 128 });
}

@Test
public void testHex() {
assertThat(Utils.bytesToHex("Hello, World!".getBytes())).isEqualTo("48656c6c6f2c20576f726c6421");
Expand Down Expand Up @@ -373,4 +386,52 @@ public void testLongIpToString() {
ip = -2018135749; // mainnet [2] - 135.181.177.59
log.info("ip {}", int2ip(ip));
}

@Test
public void testStringIpToInt() {
String ip = "89.39.107.48";
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(1495755568);

ip = "185.86.79.9";
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(-1185526007);

ip = "139.162.201.65";
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(-1952265919);

ip = "168.119.95.207";
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(-1468571697);

ip = "94.237.45.107"; // testnet [0] - 94.237.45.107
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(1592601963);

ip = "69.67.151.218"; // testnet [1] - 69.67.151.218
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(1162057690);

ip = "178.63.63.122"; // testnet [2] - 178.63.63.122
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(-1304477830);

ip = "89.39.107.48"; // testnet [3] - 89.39.107.48
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(1495755568);

ip = "5.9.10.47"; // mainnet [0] - 5.9.10.47
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(84478511);

ip = "5.9.10.15"; // mainnet [1] - 5.9.10.15
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(84478479);

ip = "135.181.177.59"; // mainnet [2] - 135.181.177.59
log.info("ip {}", ip2int(ip));
assertThat(ip2int(ip)).isEqualTo(-2018135749);

}
}

0 comments on commit 346737f

Please sign in to comment.