AddressFormatException: Add InvalidChecksum exception that is thrown when a Base58 or Bech32 checksum is invalid.

This commit is contained in:
Andreas Schildbach 2018-03-02 11:06:03 +01:00
parent 0a7f346919
commit 0297ba4d58
5 changed files with 24 additions and 4 deletions

View File

@ -27,6 +27,21 @@ public class AddressFormatException extends IllegalArgumentException {
super(message); 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 the checksum isn't valid. You shouldn't allow the user to proceed in this
* case.
*/
public static class InvalidChecksum extends AddressFormatException {
public InvalidChecksum() {
super("Checksum does not validate");
}
public InvalidChecksum(String message) {
super(message);
}
}
/** /**
* This exception is thrown by the {@link PrefixedChecksummedBytes} hierarchy of classes when you try and decode an * This exception is thrown by the {@link PrefixedChecksummedBytes} hierarchy of classes when you try and decode an
* address with a version header that isn't used by that network. You shouldn't allow the user to proceed in this * address with a version header that isn't used by that network. You shouldn't allow the user to proceed in this

View File

@ -175,7 +175,7 @@ public class Base58 {
byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length); byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length);
byte[] actualChecksum = Arrays.copyOfRange(Sha256Hash.hashTwice(data), 0, 4); byte[] actualChecksum = Arrays.copyOfRange(Sha256Hash.hashTwice(data), 0, 4);
if (!Arrays.equals(checksum, actualChecksum)) if (!Arrays.equals(checksum, actualChecksum))
throw new AddressFormatException("Checksum does not validate"); throw new AddressFormatException.InvalidChecksum();
return data; return data;
} }

View File

@ -141,7 +141,7 @@ public class Bech32 {
values[i] = CHARSET_REV[c]; values[i] = CHARSET_REV[c];
} }
String hrp = str.substring(0, pos).toLowerCase(Locale.ROOT); String hrp = str.substring(0, pos).toLowerCase(Locale.ROOT);
if (!verifyChecksum(hrp, values)) throw new AddressFormatException("Invalid checksum"); if (!verifyChecksum(hrp, values)) throw new AddressFormatException.InvalidChecksum();
return new Bech32Data(hrp, Arrays.copyOfRange(values, 0, values.length - 6)); return new Bech32Data(hrp, Arrays.copyOfRange(values, 0, values.length - 6));
} }
} }

View File

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

View File

@ -70,4 +70,9 @@ public class Bech32Test {
"li1dgmt3", "li1dgmt3",
"de1lg7wt" + new String(new char[] { 0xff }), "de1lg7wt" + new String(new char[] { 0xff }),
}; };
@Test(expected = AddressFormatException.InvalidChecksum.class)
public void decode_invalidNetwork() {
Bech32.decode("A12UEL5X");
}
} }