3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 11:15:51 +00:00

AddressFormatException: Add InvalidCharacter exception that is thrown when a character is used that is invalid in the encoding.

This commit is contained in:
Andreas Schildbach 2018-03-02 11:31:50 +01:00
parent 0297ba4d58
commit 15a15ac024
5 changed files with 45 additions and 7 deletions

View File

@ -27,6 +27,22 @@ public class AddressFormatException extends IllegalArgumentException {
super(message);
}
/**
* This exception is thrown by {@link Base58}, {@link Bech32} and the {@link PrefixedChecksummedBytes} hierarchy of
* classes when you try to decode data and a character isn't valid. You shouldn't allow the user to proceed in this
* case.
*/
public static class InvalidCharacter extends AddressFormatException {
public final char character;
public final int position;
public InvalidCharacter(char character, int position) {
super("Invalid character '" + Character.toString(character) + "' at position " + position);
this.character = character;
this.position = position;
}
}
/**
* This exception is thrown by {@link Base58}, {@link Bech32} and the {@link PrefixedChecksummedBytes} hierarchy of
* classes when you try to decode data and the checksum isn't valid. You shouldn't allow the user to proceed in this

View File

@ -129,7 +129,7 @@ public class Base58 {
char c = input.charAt(i);
int digit = c < 128 ? INDEXES[c] : -1;
if (digit < 0) {
throw new AddressFormatException("Illegal character " + c + " at position " + i);
throw new AddressFormatException.InvalidCharacter(c, i);
}
input58[i] = (byte) digit;
}

View File

@ -126,18 +126,25 @@ public class Bech32 {
if (str.length() > 90) throw new AddressFormatException("Input too long");
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
if (c < 33 || c > 126) throw new AddressFormatException("Characters out of range");
if (c >= 'a' && c <= 'z') lower = true;
if (c >= 'A' && c <= 'Z') upper = true;
if (c < 33 || c > 126) throw new AddressFormatException.InvalidCharacter(c, i);
if (c >= 'a' && c <= 'z') {
if (upper)
throw new AddressFormatException.InvalidCharacter(c, i);
lower = true;
}
if (c >= 'A' && c <= 'Z') {
if (lower)
throw new AddressFormatException.InvalidCharacter(c, i);
upper = true;
}
}
if (lower && upper) throw new AddressFormatException("Cannot mix upper and lower cases");
int pos = str.lastIndexOf('1');
final int pos = str.lastIndexOf('1');
if (pos < 1) throw new AddressFormatException("Missing human-readable part");
if (pos + 7 > str.length()) throw new AddressFormatException("Data part too short");
byte[] values = new byte[str.length() - 1 - pos];
for (int i = 0; i < str.length() - 1 - pos; ++i) {
char c = str.charAt(i + pos + 1);
if (CHARSET_REV[c] == -1) throw new AddressFormatException("Characters out of range");
if (CHARSET_REV[c] == -1) throw new AddressFormatException.InvalidCharacter(c, i + pos + 1);
values[i] = CHARSET_REV[c];
}
String hrp = str.substring(0, pos).toLowerCase(Locale.ROOT);

View File

@ -83,6 +83,11 @@ public class Base58Test {
Base58.decodeChecked("93VYUMzRG9DdbRP72uQXjaWibbQwygnvaCu9DumcqDjGybD864T");
}
@Test(expected = AddressFormatException.InvalidCharacter.class)
public void decode_invalidCharacter_notInAlphabet() {
Base58.decodeChecked("J0F12TrwUP45BMd");
}
@Test(expected = AddressFormatException.InvalidChecksum.class)
public void testDecodeChecked_invalidChecksum() {
Base58.decodeChecked("4stwEBjT6FYyVW");

View File

@ -71,6 +71,16 @@ public class Bech32Test {
"de1lg7wt" + new String(new char[] { 0xff }),
};
@Test(expected = AddressFormatException.InvalidCharacter.class)
public void decode_invalidCharacter_notInAlphabet() {
Bech32.decode("A12OUEL5X");
}
@Test(expected = AddressFormatException.InvalidCharacter.class)
public void decode_invalidCharacter_upperLowerMix() {
Bech32.decode("A12UeL5X");
}
@Test(expected = AddressFormatException.InvalidChecksum.class)
public void decode_invalidNetwork() {
Bech32.decode("A12UEL5X");