mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-13 11:36:15 +01:00
Bech32: private verifyChecksum throws on invalid
This eliminates the last @Nullable annotation in the `base` module. Since this is a private method and is called for every address that is parsed, let's not use Optional<>, but move the throw from decode() to verifyChecksum().
This commit is contained in:
parent
7bbb841730
commit
8e35f71ab4
1 changed files with 8 additions and 5 deletions
|
@ -19,7 +19,6 @@ package org.bitcoinj.base;
|
||||||
import org.bitcoinj.base.exceptions.AddressFormatException;
|
import org.bitcoinj.base.exceptions.AddressFormatException;
|
||||||
import org.bitcoinj.base.internal.ByteArray;
|
import org.bitcoinj.base.internal.ByteArray;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -189,8 +188,13 @@ public class Bech32 {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Verify a checksum. */
|
/**
|
||||||
@Nullable
|
* Verify a checksum.
|
||||||
|
* @param hrp human-readable part
|
||||||
|
* @param values data in 5-bit byte format
|
||||||
|
* @return Encoding.BECH32 or Encoding.BECH32M if valid
|
||||||
|
* @throws AddressFormatException.InvalidChecksum if invalid
|
||||||
|
*/
|
||||||
private static Encoding verifyChecksum(final String hrp, final byte[] values) {
|
private static Encoding verifyChecksum(final String hrp, final byte[] values) {
|
||||||
byte[] hrpExpanded = expandHrp(hrp);
|
byte[] hrpExpanded = expandHrp(hrp);
|
||||||
byte[] combined = new byte[hrpExpanded.length + values.length];
|
byte[] combined = new byte[hrpExpanded.length + values.length];
|
||||||
|
@ -202,7 +206,7 @@ public class Bech32 {
|
||||||
else if (check == BECH32M_CONST)
|
else if (check == BECH32M_CONST)
|
||||||
return Encoding.BECH32M;
|
return Encoding.BECH32M;
|
||||||
else
|
else
|
||||||
return null;
|
throw new AddressFormatException.InvalidChecksum();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create a checksum. */
|
/** Create a checksum. */
|
||||||
|
@ -319,7 +323,6 @@ public class Bech32 {
|
||||||
}
|
}
|
||||||
String hrp = str.substring(0, pos).toLowerCase(Locale.ROOT);
|
String hrp = str.substring(0, pos).toLowerCase(Locale.ROOT);
|
||||||
Encoding encoding = verifyChecksum(hrp, values);
|
Encoding encoding = verifyChecksum(hrp, values);
|
||||||
if (encoding == null) throw new AddressFormatException.InvalidChecksum();
|
|
||||||
return new Bech32Data(encoding, hrp, Arrays.copyOfRange(values, 0, values.length - 6));
|
return new Bech32Data(encoding, hrp, Arrays.copyOfRange(values, 0, values.length - 6));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue