diff --git a/Java/pom.xml b/Java/pom.xml index 25fd5d7..16f644b 100644 --- a/Java/pom.xml +++ b/Java/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.ciyam AT - 1.4.0 + 1.4.1 jar diff --git a/Java/src/test/java/org/ciyam/at/test/Base58.java b/Java/src/test/java/org/ciyam/at/test/Base58.java new file mode 100644 index 0000000..7d171cd --- /dev/null +++ b/Java/src/test/java/org/ciyam/at/test/Base58.java @@ -0,0 +1,218 @@ +/** + * Copyright 2011 Google Inc. + * Copyright 2013-2014 Ronald W Hoffman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ciyam.at.test; + +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.util.Arrays; + +/** + * Provides Base-58 encoding and decoding + */ +public class Base58 { + + /** Alphabet used for encoding and decoding */ + + private static final String ALPHABET_STR = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + + private static final char[] ALPHABET = + ALPHABET_STR.toCharArray(); + + /** Lookup index for US-ASCII characters (code points 0-127) */ + private static final int[] INDEXES = new int[128]; + static { + for (int i=0; i=0 && codePoint + * If any byte is non-ASCII-printable, except trailing zeros, + * then we assume address needs Base58 encoding. + *

+ * Otherwise, assume bytes contain literal address. + */ public static String decodeAddress(byte[] encodedAddress) { + int lastNonZeroIndex = -1; + for (int i = encodedAddress.length - 1; i >= 0; --i) { + byte b = encodedAddress[i]; + + if (b == 0 && lastNonZeroIndex == -1) + continue; + + if (lastNonZeroIndex == -1) + lastNonZeroIndex = i; + + if (b < 32 || b > 126) + return Base58.encode(Arrays.copyOf(encodedAddress, lastNonZeroIndex + 1)); + } + String address = new String(encodedAddress, StandardCharsets.ISO_8859_1); return address.replace("\0", ""); }