LegacyAddress: Make sure the various by-hash constructors throw AddressFormatException if the hash length isn't 20 bytes.

This commit is contained in:
Andreas Schildbach
2018-03-02 12:32:41 +01:00
parent 82edec0978
commit 6a7a136d7c

View File

@@ -63,9 +63,10 @@ public class LegacyAddress extends Address {
* @param hash160 * @param hash160
* 20-byte hash of pubkey or script * 20-byte hash of pubkey or script
*/ */
private LegacyAddress(NetworkParameters params, boolean p2sh, byte[] hash160) throws WrongNetworkException { private LegacyAddress(NetworkParameters params, boolean p2sh, byte[] hash160) throws AddressFormatException {
super(params, hash160); super(params, hash160);
checkArgument(hash160.length == 20, "Addresses are 160-bit hashes, so you must provide 20 bytes"); if (hash160.length != 20)
throw new AddressFormatException("Legacy addresses are 160-bit hashes, so you must provide 20 bytes");
this.p2sh = p2sh; this.p2sh = p2sh;
} }
@@ -79,7 +80,7 @@ public class LegacyAddress extends Address {
* 20-byte pubkey hash * 20-byte pubkey hash
* @return constructed address * @return constructed address
*/ */
public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash160) { public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash160) throws AddressFormatException {
return new LegacyAddress(params, false, hash160); return new LegacyAddress(params, false, hash160);
} }
@@ -106,12 +107,8 @@ public class LegacyAddress extends Address {
* P2SH script hash * P2SH script hash
* @return constructed address * @return constructed address
*/ */
public static LegacyAddress fromP2SHHash(NetworkParameters params, byte[] hash160) { public static LegacyAddress fromP2SHHash(NetworkParameters params, byte[] hash160) throws AddressFormatException {
try { return new LegacyAddress(params, true, hash160);
return new LegacyAddress(params, true, hash160);
} catch (WrongNetworkException e) {
throw new RuntimeException(e); // Cannot happen.
}
} }
/** /**
@@ -141,7 +138,8 @@ public class LegacyAddress extends Address {
* @throws WrongNetworkException * @throws WrongNetworkException
* if the given address is valid but for a different chain (eg testnet vs mainnet) * if the given address is valid but for a different chain (eg testnet vs mainnet)
*/ */
public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58) throws AddressFormatException { public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58)
throws AddressFormatException, WrongNetworkException {
byte[] versionAndDataBytes = Base58.decodeChecked(base58); byte[] versionAndDataBytes = Base58.decodeChecked(base58);
int version = versionAndDataBytes[0] & 0xFF; int version = versionAndDataBytes[0] & 0xFF;
byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length); byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length);
@@ -164,7 +162,7 @@ public class LegacyAddress extends Address {
/** @deprecated use {@link #fromPubKeyHash(NetworkParameters, byte[])} */ /** @deprecated use {@link #fromPubKeyHash(NetworkParameters, byte[])} */
@Deprecated @Deprecated
public LegacyAddress(NetworkParameters params, byte[] hash160) { public LegacyAddress(NetworkParameters params, byte[] hash160) throws AddressFormatException {
this(params, false, hash160); this(params, false, hash160);
} }
@@ -221,14 +219,10 @@ public class LegacyAddress extends Address {
* compatible with the current wallet. * compatible with the current wallet.
* *
* @return network the address is valid for * @return network the address is valid for
* @throws AddressFormatException if the string wasn't of a known version * @throws AddressFormatException if the given base58 doesn't parse or the checksum is invalid
*/ */
public static NetworkParameters getParametersFromAddress(String address) throws AddressFormatException { public static NetworkParameters getParametersFromAddress(String address) throws AddressFormatException {
try { return LegacyAddress.fromBase58(null, address).getParameters();
return LegacyAddress.fromBase58(null, address).getParameters();
} catch (WrongNetworkException e) {
throw new RuntimeException(e); // Cannot happen.
}
} }
@Override @Override