From 612589028912d3f7753158c078c61de501a3b254 Mon Sep 17 00:00:00 2001 From: neodiX Date: Wed, 7 Aug 2024 21:13:58 +0200 Subject: [PATCH 1/6] bump up version --- README.md | 4 ++-- address/README.md | 4 ++-- address/pom.xml | 2 +- bitstring/README.md | 4 ++-- bitstring/pom.xml | 2 +- cell/README.md | 4 ++-- cell/pom.xml | 2 +- emulator/README.md | 4 ++-- emulator/pom.xml | 10 +++++----- liteclient/README.md | 4 ++-- liteclient/pom.xml | 2 +- mnemonic/README.md | 4 ++-- mnemonic/pom.xml | 2 +- pom.xml | 2 +- smartcontract/README.md | 4 ++-- smartcontract/pom.xml | 2 +- tl/pom.xml | 4 ++-- tonlib/README.md | 4 ++-- tonlib/pom.xml | 2 +- tweetnacl-java/pom.xml | 2 +- utils/README.md | 4 ++-- utils/pom.xml | 2 +- 22 files changed, 37 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 7b0b02cf..abc047fc 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ found [here](https://github.com/ton-blockchain/ton/actions). io.github.neodix42 smartcontract - 0.4.8 + 0.4.9 ``` @@ -36,7 +36,7 @@ found [here](https://github.com/ton-blockchain/ton/actions). io.github.neodix42 ton4j - 0.4.8 + 0.4.9 ``` diff --git a/address/README.md b/address/README.md index b9c2c40f..3319acae 100644 --- a/address/README.md +++ b/address/README.md @@ -7,7 +7,7 @@ io.github.neodix42 address - 0.4.8 + 0.4.9 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j address - 0.4.8 + 0.4.9 ``` diff --git a/address/pom.xml b/address/pom.xml index 6dad401f..b8221313 100644 --- a/address/pom.xml +++ b/address/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 4.0.0 diff --git a/bitstring/README.md b/bitstring/README.md index 7dbdec61..8ff6740e 100644 --- a/bitstring/README.md +++ b/bitstring/README.md @@ -7,7 +7,7 @@ io.github.neodix42 bitstring - 0.4.8 + 0.4.9 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j bitstring - 0.4.8 + 0.4.9 ``` diff --git a/bitstring/pom.xml b/bitstring/pom.xml index e073390a..1294ea69 100644 --- a/bitstring/pom.xml +++ b/bitstring/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 4.0.0 diff --git a/cell/README.md b/cell/README.md index 5ed2e786..d860e090 100644 --- a/cell/README.md +++ b/cell/README.md @@ -7,7 +7,7 @@ io.github.neodix42 cell - 0.4.8 + 0.4.9 ``` @@ -17,7 +17,7 @@ io.github.neodix42.ton4j cell - 0.4.8 + 0.4.9 ``` diff --git a/cell/pom.xml b/cell/pom.xml index 19c8295a..ad3bf0df 100644 --- a/cell/pom.xml +++ b/cell/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 4.0.0 diff --git a/emulator/README.md b/emulator/README.md index edaeaf5b..44390906 100644 --- a/emulator/README.md +++ b/emulator/README.md @@ -9,7 +9,7 @@ Java Emulator wrapper uses JNA to access methods in native emulator shared libra io.github.neodix42 emulator - 0.4.8 + 0.4.9 ``` @@ -19,7 +19,7 @@ Java Emulator wrapper uses JNA to access methods in native emulator shared libra io.github.neodix42.ton4j emulator - 0.4.8 + 0.4.9 ``` diff --git a/emulator/pom.xml b/emulator/pom.xml index 44751f4f..712d0a0d 100644 --- a/emulator/pom.xml +++ b/emulator/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 emulator @@ -88,25 +88,25 @@ io.github.neodix42 tonlib - 0.4.8 + 0.4.9 test io.github.neodix42 liteclient - 0.4.8 + 0.4.9 test io.github.neodix42 smartcontract - 0.4.8 + 0.4.9 test io.github.neodix42 cell - 0.4.8 + 0.4.9 compile diff --git a/liteclient/README.md b/liteclient/README.md index 2c03c977..5e426f39 100644 --- a/liteclient/README.md +++ b/liteclient/README.md @@ -9,7 +9,7 @@ Java Lite-client wrapper uses JNA to access methods in native lite-client binary io.github.neodix42 lite-client - 0.4.8 + 0.4.9 ``` @@ -20,7 +20,7 @@ Java Lite-client wrapper uses JNA to access methods in native lite-client binary io.github.neodix42.ton4j lite-client - 0.4.8 + 0.4.9 ``` diff --git a/liteclient/pom.xml b/liteclient/pom.xml index 482ff20c..866dab5b 100644 --- a/liteclient/pom.xml +++ b/liteclient/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 liteclient diff --git a/mnemonic/README.md b/mnemonic/README.md index 3ace5da5..9d11460e 100644 --- a/mnemonic/README.md +++ b/mnemonic/README.md @@ -7,7 +7,7 @@ io.github.neodix42 mnemonic - 0.4.8 + 0.4.9 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j mnemonic - 0.4.8 + 0.4.9 ``` diff --git a/mnemonic/pom.xml b/mnemonic/pom.xml index 34d5a245..46f44474 100644 --- a/mnemonic/pom.xml +++ b/mnemonic/pom.xml @@ -5,7 +5,7 @@ top io.github.neodix42 - 0.4.8 + 0.4.9 4.0.0 diff --git a/pom.xml b/pom.xml index d5e03b51..b63caa95 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ io.github.neodix42 top pom - 0.4.8 + 0.4.9 bitstring diff --git a/smartcontract/README.md b/smartcontract/README.md index dad3c4fa..94725a4f 100644 --- a/smartcontract/README.md +++ b/smartcontract/README.md @@ -7,7 +7,7 @@ io.github.neodix42 smartcontract - 0.4.8 + 0.4.9 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j smartcontract - 0.4.8 + 0.4.9 ``` diff --git a/smartcontract/pom.xml b/smartcontract/pom.xml index cff4061c..c1ffe511 100644 --- a/smartcontract/pom.xml +++ b/smartcontract/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 4.0.0 diff --git a/tl/pom.xml b/tl/pom.xml index 31355121..a578ebf8 100644 --- a/tl/pom.xml +++ b/tl/pom.xml @@ -5,7 +5,7 @@ top io.github.neodix42 - 0.4.8 + 0.4.9 4.0.0 @@ -47,7 +47,7 @@ io.github.neodix42 cell - 0.4.8 + 0.4.9 compile diff --git a/tonlib/README.md b/tonlib/README.md index c882a248..a58b7eea 100644 --- a/tonlib/README.md +++ b/tonlib/README.md @@ -16,7 +16,7 @@ You can get the latest tonlib library by: io.github.neodix42 tonlib - 0.4.8 + 0.4.9 ``` @@ -27,7 +27,7 @@ You can get the latest tonlib library by: io.github.neodix42.ton4j tonlib - 0.4.8 + 0.4.9 ``` diff --git a/tonlib/pom.xml b/tonlib/pom.xml index 71291f98..9a7ca02b 100644 --- a/tonlib/pom.xml +++ b/tonlib/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 4.0.0 diff --git a/tweetnacl-java/pom.xml b/tweetnacl-java/pom.xml index f476453c..632520eb 100644 --- a/tweetnacl-java/pom.xml +++ b/tweetnacl-java/pom.xml @@ -4,7 +4,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 tweetnacl-java-8 diff --git a/utils/README.md b/utils/README.md index ca26ab9d..a7c27b26 100644 --- a/utils/README.md +++ b/utils/README.md @@ -7,7 +7,7 @@ io.github.neodix42 utils - 0.4.8 + 0.4.9 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j utils - 0.4.8 + 0.4.9 ``` diff --git a/utils/pom.xml b/utils/pom.xml index 12dc99dc..0359b64a 100644 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.8 + 0.4.9 4.0.0 From eac76500fee703a880ad77e7fb08a76b725ed524 Mon Sep 17 00:00:00 2001 From: neodiX Date: Thu, 8 Aug 2024 09:52:59 +0200 Subject: [PATCH 2/6] bump up version (there was a mistake in merging fixes) --- README.md | 4 ++-- address/README.md | 4 ++-- address/pom.xml | 2 +- bitstring/README.md | 4 ++-- bitstring/pom.xml | 2 +- cell/README.md | 4 ++-- cell/pom.xml | 2 +- emulator/README.md | 4 ++-- emulator/pom.xml | 10 +++++----- liteclient/README.md | 4 ++-- liteclient/pom.xml | 2 +- mnemonic/README.md | 4 ++-- mnemonic/pom.xml | 2 +- pom.xml | 2 +- smartcontract/README.md | 4 ++-- smartcontract/pom.xml | 2 +- tl/pom.xml | 4 ++-- tonlib/README.md | 4 ++-- tonlib/pom.xml | 2 +- tweetnacl-java/pom.xml | 2 +- utils/README.md | 4 ++-- utils/pom.xml | 2 +- 22 files changed, 37 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 5dbb938c..5e0ec67b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ found [here](https://github.com/ton-blockchain/ton/actions). io.github.neodix42 smartcontract - 0.4.9 + 0.5.0 ``` @@ -36,7 +36,7 @@ found [here](https://github.com/ton-blockchain/ton/actions). io.github.neodix42 ton4j - 0.4.9 + 0.5.0 ``` diff --git a/address/README.md b/address/README.md index 3319acae..5a9983d7 100644 --- a/address/README.md +++ b/address/README.md @@ -7,7 +7,7 @@ io.github.neodix42 address - 0.4.9 + 0.5.0 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j address - 0.4.9 + 0.5.0 ``` diff --git a/address/pom.xml b/address/pom.xml index b8221313..da6e7d17 100644 --- a/address/pom.xml +++ b/address/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 4.0.0 diff --git a/bitstring/README.md b/bitstring/README.md index 8ff6740e..408062fa 100644 --- a/bitstring/README.md +++ b/bitstring/README.md @@ -7,7 +7,7 @@ io.github.neodix42 bitstring - 0.4.9 + 0.5.0 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j bitstring - 0.4.9 + 0.5.0 ``` diff --git a/bitstring/pom.xml b/bitstring/pom.xml index 1294ea69..93895607 100644 --- a/bitstring/pom.xml +++ b/bitstring/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 4.0.0 diff --git a/cell/README.md b/cell/README.md index d860e090..b00bbe29 100644 --- a/cell/README.md +++ b/cell/README.md @@ -7,7 +7,7 @@ io.github.neodix42 cell - 0.4.9 + 0.5.0 ``` @@ -17,7 +17,7 @@ io.github.neodix42.ton4j cell - 0.4.9 + 0.5.0 ``` diff --git a/cell/pom.xml b/cell/pom.xml index ad3bf0df..898b0f11 100644 --- a/cell/pom.xml +++ b/cell/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 4.0.0 diff --git a/emulator/README.md b/emulator/README.md index 44390906..e2afee96 100644 --- a/emulator/README.md +++ b/emulator/README.md @@ -9,7 +9,7 @@ Java Emulator wrapper uses JNA to access methods in native emulator shared libra io.github.neodix42 emulator - 0.4.9 + 0.5.0 ``` @@ -19,7 +19,7 @@ Java Emulator wrapper uses JNA to access methods in native emulator shared libra io.github.neodix42.ton4j emulator - 0.4.9 + 0.5.0 ``` diff --git a/emulator/pom.xml b/emulator/pom.xml index 712d0a0d..97dcb0c5 100644 --- a/emulator/pom.xml +++ b/emulator/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 emulator @@ -88,25 +88,25 @@ io.github.neodix42 tonlib - 0.4.9 + 0.5.0 test io.github.neodix42 liteclient - 0.4.9 + 0.5.0 test io.github.neodix42 smartcontract - 0.4.9 + 0.5.0 test io.github.neodix42 cell - 0.4.9 + 0.5.0 compile diff --git a/liteclient/README.md b/liteclient/README.md index 5e426f39..95c67767 100644 --- a/liteclient/README.md +++ b/liteclient/README.md @@ -9,7 +9,7 @@ Java Lite-client wrapper uses JNA to access methods in native lite-client binary io.github.neodix42 lite-client - 0.4.9 + 0.5.0 ``` @@ -20,7 +20,7 @@ Java Lite-client wrapper uses JNA to access methods in native lite-client binary io.github.neodix42.ton4j lite-client - 0.4.9 + 0.5.0 ``` diff --git a/liteclient/pom.xml b/liteclient/pom.xml index 866dab5b..78af15a9 100644 --- a/liteclient/pom.xml +++ b/liteclient/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 liteclient diff --git a/mnemonic/README.md b/mnemonic/README.md index 9d11460e..19f9396d 100644 --- a/mnemonic/README.md +++ b/mnemonic/README.md @@ -7,7 +7,7 @@ io.github.neodix42 mnemonic - 0.4.9 + 0.5.0 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j mnemonic - 0.4.9 + 0.5.0 ``` diff --git a/mnemonic/pom.xml b/mnemonic/pom.xml index 46f44474..2178639f 100644 --- a/mnemonic/pom.xml +++ b/mnemonic/pom.xml @@ -5,7 +5,7 @@ top io.github.neodix42 - 0.4.9 + 0.5.0 4.0.0 diff --git a/pom.xml b/pom.xml index b63caa95..5279b2ef 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ io.github.neodix42 top pom - 0.4.9 + 0.5.0 bitstring diff --git a/smartcontract/README.md b/smartcontract/README.md index 94725a4f..6ff8d47c 100644 --- a/smartcontract/README.md +++ b/smartcontract/README.md @@ -7,7 +7,7 @@ io.github.neodix42 smartcontract - 0.4.9 + 0.5.0 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j smartcontract - 0.4.9 + 0.5.0 ``` diff --git a/smartcontract/pom.xml b/smartcontract/pom.xml index c1ffe511..c211f148 100644 --- a/smartcontract/pom.xml +++ b/smartcontract/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 4.0.0 diff --git a/tl/pom.xml b/tl/pom.xml index a578ebf8..beae584d 100644 --- a/tl/pom.xml +++ b/tl/pom.xml @@ -5,7 +5,7 @@ top io.github.neodix42 - 0.4.9 + 0.5.0 4.0.0 @@ -47,7 +47,7 @@ io.github.neodix42 cell - 0.4.9 + 0.5.0 compile diff --git a/tonlib/README.md b/tonlib/README.md index a58b7eea..d0aeb1c4 100644 --- a/tonlib/README.md +++ b/tonlib/README.md @@ -16,7 +16,7 @@ You can get the latest tonlib library by: io.github.neodix42 tonlib - 0.4.9 + 0.5.0 ``` @@ -27,7 +27,7 @@ You can get the latest tonlib library by: io.github.neodix42.ton4j tonlib - 0.4.9 + 0.5.0 ``` diff --git a/tonlib/pom.xml b/tonlib/pom.xml index 9a7ca02b..e8a0a3cc 100644 --- a/tonlib/pom.xml +++ b/tonlib/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 4.0.0 diff --git a/tweetnacl-java/pom.xml b/tweetnacl-java/pom.xml index 632520eb..fbd30d83 100644 --- a/tweetnacl-java/pom.xml +++ b/tweetnacl-java/pom.xml @@ -4,7 +4,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 tweetnacl-java-8 diff --git a/utils/README.md b/utils/README.md index a7c27b26..cdf53b90 100644 --- a/utils/README.md +++ b/utils/README.md @@ -7,7 +7,7 @@ io.github.neodix42 utils - 0.4.9 + 0.5.0 ``` @@ -18,7 +18,7 @@ io.github.neodix42.ton4j utils - 0.4.9 + 0.5.0 ``` diff --git a/utils/pom.xml b/utils/pom.xml index 0359b64a..094b549c 100644 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -6,7 +6,7 @@ io.github.neodix42 top - 0.4.9 + 0.5.0 4.0.0 From 346737feff7472408bb24b1df7f8907ee18ee6ff Mon Sep 17 00:00:00 2001 From: IsyaHex Date: Sat, 10 Aug 2024 10:28:49 +0500 Subject: [PATCH 3/6] small optimizations Address, Utils, additional tests TestAddress, TestUtils --- .../java/org/ton/java/address/Address.java | 24 ++++--- .../org/ton/java/address/TestAddress.java | 1 + .../main/java/org/ton/java/utils/Utils.java | 71 ++++++++++--------- .../org/ton/java/utils/TestEncryption.java | 12 ++++ .../java/org/ton/java/utils/TestUtils.java | 61 ++++++++++++++++ 5 files changed, 123 insertions(+), 46 deletions(-) diff --git a/address/src/main/java/org/ton/java/address/Address.java b/address/src/main/java/org/ton/java/address/Address.java index 6f0b90eb..22d1ebe0 100644 --- a/address/src/main/java/org/ton/java/address/Address.java +++ b/address/src/main/java/org/ton/java/address/Address.java @@ -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 { @@ -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); diff --git a/address/src/test/java/org/ton/java/address/TestAddress.java b/address/src/test/java/org/ton/java/address/TestAddress.java index 6690c994..fdc768e2 100644 --- a/address/src/test/java/org/ton/java/address/TestAddress.java +++ b/address/src/test/java/org/ton/java/address/TestAddress.java @@ -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"); }); } diff --git a/utils/src/main/java/org/ton/java/utils/Utils.java b/utils/src/main/java/org/ton/java/utils/Utils.java index ee97acac..34157dcd 100644 --- a/utils/src/main/java/org/ton/java/utils/Utils.java +++ b/utils/src/main/java/org/ton/java/utils/Utils.java @@ -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; @@ -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); } @@ -289,7 +286,7 @@ 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); } @@ -297,7 +294,7 @@ public static String bitsToDec(boolean[] bits) { 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); } @@ -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))); @@ -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))); } } @@ -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]; } @@ -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; } @@ -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); } @@ -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()); @@ -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); } @@ -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) { diff --git a/utils/src/test/java/org/ton/java/utils/TestEncryption.java b/utils/src/test/java/org/ton/java/utils/TestEncryption.java index c53d09dc..31e539a3 100644 --- a/utils/src/test/java/org/ton/java/utils/TestEncryption.java +++ b/utils/src/test/java/org/ton/java/utils/TestEncryption.java @@ -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"); diff --git a/utils/src/test/java/org/ton/java/utils/TestUtils.java b/utils/src/test/java/org/ton/java/utils/TestUtils.java index c16be296..7d933a1b 100644 --- a/utils/src/test/java/org/ton/java/utils/TestUtils.java +++ b/utils/src/test/java/org/ton/java/utils/TestUtils.java @@ -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 @@ -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"); @@ -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); + + } } From 6b41095750466ade4b85f6469ab8a9baebc6d835 Mon Sep 17 00:00:00 2001 From: IsyaHex Date: Sat, 10 Aug 2024 11:41:06 +0500 Subject: [PATCH 4/6] test fix --- .../test/java/org/ton/java/cell/TestBocDeserealization.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java b/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java index 23f79fb5..b2eac1a9 100644 --- a/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java +++ b/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java @@ -59,7 +59,10 @@ public void testBocDeserializationAllConfig() throws IOException { log.info(cell.print()); log.info(Utils.bytesToHex(cell.toBoc(true))); - assertThat(StringUtils.trim(cell.print())).isEqualTo(cellOutput); + String expected = StringUtils.normalizeSpace(cellOutput); + String actual = StringUtils.normalizeSpace(cell.print()); + + assertThat(expected).isEqualTo(actual); } From b60b43f5875548a1c7afea30a69eaa5d65907fb2 Mon Sep 17 00:00:00 2001 From: IsyaHex Date: Sat, 10 Aug 2024 14:24:56 +0500 Subject: [PATCH 5/6] test fix updates --- .../test/java/org/ton/java/cell/TestBocDeserealization.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java b/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java index b2eac1a9..e3496617 100644 --- a/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java +++ b/cell/src/test/java/org/ton/java/cell/TestBocDeserealization.java @@ -59,10 +59,10 @@ public void testBocDeserializationAllConfig() throws IOException { log.info(cell.print()); log.info(Utils.bytesToHex(cell.toBoc(true))); - String expected = StringUtils.normalizeSpace(cellOutput); - String actual = StringUtils.normalizeSpace(cell.print()); + String actual = StringUtils.trim(cell.print()); + String expected = StringUtils.replaceChars(cellOutput, "\r\n", "\n"); - assertThat(expected).isEqualTo(actual); + assertThat(actual).isEqualTo(expected); } From 1bd360711c3bd57caf227e45d89a2d158b14c988 Mon Sep 17 00:00:00 2001 From: IsyaHex Date: Sat, 10 Aug 2024 18:05:52 +0500 Subject: [PATCH 6/6] Optimizations BitString, additional tests TestBitString, TestEncryption --- .../org/ton/java/bitstring/BitString.java | 174 ++++++++++-------- .../org/ton/java/bitstring/TestBitString.java | 18 ++ 2 files changed, 114 insertions(+), 78 deletions(-) diff --git a/bitstring/src/main/java/org/ton/java/bitstring/BitString.java b/bitstring/src/main/java/org/ton/java/bitstring/BitString.java index 3f2dafc8..2f4f66a3 100644 --- a/bitstring/src/main/java/org/ton/java/bitstring/BitString.java +++ b/bitstring/src/main/java/org/ton/java/bitstring/BitString.java @@ -28,7 +28,7 @@ public BitString(BitString bs) { for (Boolean b : bs.array) { writeBit(b); } - initialLength = bs.array.size() == 0 ? MAX_LENGTH : bs.array.size(); + initialLength = bs.array.isEmpty() ? MAX_LENGTH : bs.array.size(); } public BitString(byte[] bytes) { @@ -40,19 +40,19 @@ public BitString(int[] bytes) { array = new ArrayDeque<>(0); initialLength = 0; } else { - String bits = StringUtils.leftPad(Utils.bytesToBitString(bytes), bytes.length * 8, '0'); + byte[] bits = leftPadBytes(Utils.bytesToBitString(bytes).getBytes(), bytes.length * 8, '0'); - array = new ArrayDeque<>(bits.length()); - for (int i = 0; i < bits.length(); i++) { // whole length - if (bits.charAt(i) == '1') { + array = new ArrayDeque<>(bits.length); + for (byte bit : bits) { // whole length + if (bit == (byte) '1') { array.addLast(true); - } else if (bits.charAt(i) == '0') { + } else if (bit == (byte) '0') { array.addLast(false); } else { // else '-' sign - do nothing } } - initialLength = bits.length(); + initialLength = bits.length; } } @@ -65,20 +65,32 @@ public BitString(byte[] bytes, int size) { array = new ArrayDeque<>(0); initialLength = 0; } else { - String bits = StringUtils.leftPad(Utils.bytesToBitString(bytes), bytes.length * 8, '0'); - - array = new ArrayDeque<>(bits.length()); + byte[] bits = leftPadBytes(Utils.bytesToBitString(bytes).getBytes(), bytes.length * 8, '0'); + array = new ArrayDeque<>(bits.length); for (int i = 0; i < size; i++) { // specified length - if (bits.charAt(i) == '1') { + if (bits[i] == (byte) '1') { array.addLast(true); - } else if (bits.charAt(i) == '0') { + } else if (bits[i] == (byte) '0') { array.addLast(false); } else { // else '-' sign - do nothing } } - initialLength = bits.length(); + initialLength = bits.length; + } + } + + private byte[] leftPadBytes(byte[] bits, int sz, char c) { + if (sz <= bits.length) { + return bits; } + + int diff = sz - bits.length; + byte[] b = new byte[diff + bits.length]; + Arrays.fill(b, 0, diff, (byte) c); + System.arraycopy(bits, 0, b, diff, bits.length); + + return b; } /** @@ -118,7 +130,7 @@ public int getUsedBits() { * @return int */ public int getUsedBytes() { - return (int) Math.ceil(array.size() / (double) 8); + return (array.size() + 7) / 8; } /** @@ -162,11 +174,7 @@ public void writeBits(String b) { * @param b byte */ void writeBit(byte b) { - if ((b) > 0) { - array.addLast(true); - } else { - array.addLast(false); - } + array.addLast(b > 0); } /** @@ -197,28 +205,29 @@ public void writeUint(BigInteger number, int bitLength) { if (number.compareTo(BigInteger.ZERO) < 0) { throw new Error("Unsigned number cannot be less than 0"); } - if (bitLength == 0 || (number.bitLength() > bitLength)) { + if (bitLength == 0 || number.bitLength() > bitLength) { if (number.compareTo(BigInteger.ZERO) == 0) { return; } throw new Error("bitLength is too small for number, got number=" + number + ", bitLength=" + bitLength); } - String s = number.toString(2); + byte[] s = number.toString(2).getBytes(); - if (s.length() != bitLength) { - s = repeatZeros(bitLength - s.length()) + s; + if (s.length != bitLength) { + s = repeatZerosAndMerge(bitLength - s.length, s); } for (int i = 0; i < bitLength; i++) { - writeBit(s.charAt(i) == '1'); + writeBit(s[i] == (byte) '1'); } } - public String repeatZeros(int count) { - char[] zeros = new char[count]; - Arrays.fill(zeros, '0'); - return new String(zeros); + private byte[] repeatZerosAndMerge(int count, byte[] s) { + byte[] a = new byte[count + s.length]; + Arrays.fill(a, 0, count, (byte) '0'); + System.arraycopy(s, 0, a, count, s.length); + return a; } /** @@ -312,7 +321,7 @@ public void writeCoins(BigInteger amount) { if (amount.compareTo(BigInteger.ZERO) == 0) { writeUint(BigInteger.ZERO, 4); } else { - int bytesSize = (int) Math.ceil((amount.bitLength() / (double) 8)); + int bytesSize = (amount.bitLength() + 7) / 8; if (bytesSize >= 16) { throw new Error("Amount is too big. Maximum amount 2^120-1"); } @@ -326,7 +335,7 @@ public void writeVarUint(BigInteger value, int bitLength) { if (value.compareTo(BigInteger.ZERO) == 0) { writeUint(BigInteger.ZERO, bitLength); } else { - int bytesSize = (int) Math.ceil((value.bitLength() / (double) 8)); + int bytesSize = (value.bitLength() + 7) / 8; if (bytesSize >= bitLength) { throw new Error("Amount is too big. Should fit in " + bitLength + " bits"); } @@ -372,7 +381,7 @@ public void writeBitString(BitString anotherBitString) { * * @return true or false */ - public Boolean prereadBit() { + public Boolean preReadBit() { return get(); } @@ -382,7 +391,6 @@ public Boolean prereadBit() { * @return true or false */ public Boolean readBit() { -// return array.getFirst(); return array.pollFirst(); } @@ -420,20 +428,18 @@ public BitString readBits() { * @return BigInteger */ public BigInteger preReadUint(int bitLength) { - BitString cloned = new BitString(this); - if (bitLength < 1) { throw new Error("Incorrect bitLength"); } + + BitString cloned = clone(); StringBuilder s = new StringBuilder(); + for (int i = 0; i < bitLength; i++) { Boolean b = cloned.readBit(); - if (b) { - s.append("1"); - } else { - s.append("0"); - } + s.append(b ? 1 : 0); } + return new BigInteger(s.toString(), 2); } @@ -447,15 +453,13 @@ public BigInteger readUint(int bitLength) { if (bitLength < 1) { throw new Error("Incorrect bitLength"); } + StringBuilder s = new StringBuilder(); for (int i = 0; i < bitLength; i++) { Boolean b = readBit(); - if (b) { - s.append("1"); - } else { - s.append("0"); - } + s.append(b ? 1 : 0); } + return new BigInteger(s.toString(), 2); } @@ -481,6 +485,7 @@ public BigInteger readInt(int bitLength) { BigInteger nb = b.pow(bitLength - 1); number = number.subtract(nb); } + return number; } @@ -556,7 +561,7 @@ public String toString() { * @return BitString from 0 to writeCursor */ public String toBitString() { - BitString cloned = new BitString(this); + BitString cloned = clone(); StringBuilder s = new StringBuilder(); for (Boolean b : cloned.array) { s.append(b ? '1' : '0'); @@ -565,72 +570,85 @@ public String toBitString() { } public int getLength() { - return toBitString().length(); + return array.size(); } /** * @return BitString from current position to writeCursor */ public String getBitString() { - BitString cloned = clone(); StringBuilder s = new StringBuilder(); - for (Boolean b : cloned.array) { + for (Boolean b : array) { s.append(b ? '1' : '0'); } return s.toString(); } public int[] toUnsignedByteArray() { - if (array.size() == 0) { + if (array.isEmpty()) { return new int[0]; } + String bin = getBitString(); - int[] result = new int[(int) Math.ceil(bin.length() / (double) 8)]; - int j = 0; - for (String str : bin.split("(?<=\\G.{8})")) { - result[j++] = Integer.parseInt(str, 2); + int sz = bin.length(); + int[] result = new int[(sz + 7) / 8]; + + for (int i = 0; i < sz; i += 8) { + String str = bin.substring(i, Math.min(sz, i + 8)); + result[i / 8] = Integer.parseInt(str, 2); } + return result; } public byte[] toSignedByteArray() { - if (array.size() == 0) { + if (array.isEmpty()) { return new byte[0]; } + String bin = getBitString(); - byte[] result = new byte[(int) Math.ceil(bin.length() / (double) 8)]; - int j = 0; - for (String str : bin.split("(?<=\\G.{8})")) { - result[j++] = (byte) (Integer.parseInt(str, 2) & 0xFF); + int sz = bin.length(); + byte[] result = new byte[(sz + 7) / 8]; + + for (int i = 0; i < sz; i += 8) { + String str = bin.substring(i, Math.min(sz, i + 8)); + result[i / 8] = (byte) (Integer.parseInt(str, 2) & 0xFF); } + return result; } public List toByteList() { - if (array.size() == 0) { + if (array.isEmpty()) { return new ArrayList<>(); } + String bin = getBitString(); - List result = new ArrayList<>((int) Math.ceil(bin.length() / (double) 8)); - int j = 0; - for (String str : bin.split("(?<=\\G.{8})")) { + int sz = bin.length(); + List result = new ArrayList<>((sz + 7) / 8); + + for (int i = 0; i < sz; i += 8) { + String str = bin.substring(i, Math.min(sz, i + 8)); result.add(new BigInteger(str, 2)); } + return result; } public byte[] toByteArray() { - if (array.size() == 0) { + if (array.isEmpty()) { return new byte[0]; } - String bin = getBitString(); - byte[] result = new byte[(int) Math.ceil(bin.length() / (double) 8)]; - for (int i = 0; i < bin.length(); i++) { - if (bin.charAt(i) == '1') { - result[(i / 8)] |= 1 << (7 - (i % 8)); + byte[] bin = getBitString().getBytes(); + int sz = bin.length; + byte[] result = new byte[(sz + 7) / 8]; + + for (int i = 0; i < sz; i++) { + if (bin[i] == (byte) '1') { + result[(i / 8)] |= (byte) (1 << (7 - (i % 8))); } else { - result[(i / 8)] &= ~(1 << (7 - (i % 8))); + result[(i / 8)] &= (byte) ~(1 << (7 - (i % 8))); } } @@ -638,14 +656,16 @@ public byte[] toByteArray() { } public int[] toUintArray() { - if (array.size() == 0) { + if (array.isEmpty()) { return new int[0]; } - String bin = getBitString(); - int[] result = new int[(int) Math.ceil(bin.length() / (double) 8)]; - for (int i = 0; i < bin.length(); i++) { - if (bin.charAt(i) == '1') { + byte[] bin = getBitString().getBytes(); + int sz = bin.length; + int[] result = new int[(sz + 7) / 8]; + + for (int i = 0; i < sz; i++) { + if (bin[i] == (byte) '1') { result[(i / 8)] |= 1 << (7 - (i % 8)); } else { result[(i / 8)] &= ~(1 << (7 - (i % 8))); @@ -656,12 +676,10 @@ public int[] toUintArray() { } public Boolean[] toBooleanArray() { - String bin = getBitString(); - Boolean[] result = new Boolean[bin.length()]; + Boolean[] result = new Boolean[getLength()]; int i = 0; for (Boolean b : array) { - result[i] = b; - i++; + result[i++] = b; } return result; } diff --git a/bitstring/src/test/java/org/ton/java/bitstring/TestBitString.java b/bitstring/src/test/java/org/ton/java/bitstring/TestBitString.java index 09076e4e..11af9904 100644 --- a/bitstring/src/test/java/org/ton/java/bitstring/TestBitString.java +++ b/bitstring/src/test/java/org/ton/java/bitstring/TestBitString.java @@ -233,6 +233,24 @@ public void testBitStringByteArray3() { assertThat(bitString9.toHex()).isEqualTo("80000020"); } + @Test + public void testBitStringByArrayAndSize() { + BitString bs = new BitString(new byte[]{7, 7, 7, 7}, 32); + assertThat(bs.toBitString()).isEqualTo("00000111000001110000011100000111"); + assertThat(Utils.bytesToHex(bs.toByteArray())).isEqualTo("07070707"); + assertThat(bs.toHex()).isEqualTo("07070707"); + + bs = new BitString(new byte[]{-126, 7, 7, 7}, 16); + assertThat(bs.toBitString()).isEqualTo("1000001000000111"); + assertThat(Utils.bytesToHex(bs.toUnsignedByteArray())).isEqualTo("8207"); + assertThat(bs.toHex()).isEqualTo("8207"); + + bs = new BitString(new byte[]{-128, 0, 0, 0}, 2); + assertThat(bs.toBitString()).isEqualTo("10"); + assertThat(Utils.bytesToHex(bs.toByteArray())).isEqualTo("80"); + assertThat(bs.toHex()).isEqualTo("A_"); + } + @Test public void testBitStringAll() { BitString bitString = new BitString(1023);