mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-18 21:32:35 +01:00
AddressFormatException: Add InvalidChecksum exception that is thrown when a Base58 or Bech32 checksum is invalid.
This commit is contained in:
parent
0a7f346919
commit
0297ba4d58
@ -27,6 +27,21 @@ 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 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
|
||||
* address with a version header that isn't used by that network. You shouldn't allow the user to proceed in this
|
||||
|
@ -175,7 +175,7 @@ public class Base58 {
|
||||
byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length);
|
||||
byte[] actualChecksum = Arrays.copyOfRange(Sha256Hash.hashTwice(data), 0, 4);
|
||||
if (!Arrays.equals(checksum, actualChecksum))
|
||||
throw new AddressFormatException("Checksum does not validate");
|
||||
throw new AddressFormatException.InvalidChecksum();
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -141,7 +141,7 @@ public class Bech32 {
|
||||
values[i] = CHARSET_REV[c];
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -83,8 +83,8 @@ public class Base58Test {
|
||||
Base58.decodeChecked("93VYUMzRG9DdbRP72uQXjaWibbQwygnvaCu9DumcqDjGybD864T");
|
||||
}
|
||||
|
||||
@Test(expected = AddressFormatException.class)
|
||||
public void testDecodeChecked_badChecksum() {
|
||||
@Test(expected = AddressFormatException.InvalidChecksum.class)
|
||||
public void testDecodeChecked_invalidChecksum() {
|
||||
Base58.decodeChecked("4stwEBjT6FYyVW");
|
||||
}
|
||||
|
||||
|
@ -70,4 +70,9 @@ public class Bech32Test {
|
||||
"li1dgmt3",
|
||||
"de1lg7wt" + new String(new char[] { 0xff }),
|
||||
};
|
||||
|
||||
@Test(expected = AddressFormatException.InvalidChecksum.class)
|
||||
public void decode_invalidNetwork() {
|
||||
Bech32.decode("A12UEL5X");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user