Skip to content

Commit

Permalink
Merge pull request #32 from h6x0r/tuning-v1
Browse files Browse the repository at this point in the history
Optimizations Address, Utils, additional tests TestAddress, TestUtils
  • Loading branch information
neodix42 authored Aug 10, 2024
2 parents 6c654f1 + 1bd3607 commit 5bf3c72
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 125 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
Loading

0 comments on commit 5bf3c72

Please sign in to comment.