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

Move WrongNetworkException to AddressFormatException.WrongNetwork.

This commit is contained in:
Andreas Schildbach 2018-03-02 10:36:50 +01:00
parent 6a7a136d7c
commit 0a7f346919
9 changed files with 34 additions and 51 deletions

View File

@ -47,19 +47,19 @@ public abstract class Address extends PrefixedChecksummedBytes {
* @return constructed address
* @throws AddressFormatException
* if the given string doesn't parse or the checksum is invalid
* @throws WrongNetworkException
* @throws AddressFormatException.WrongNetwork
* if the given string is valid but not for the expected network (eg testnet vs mainnet)
*/
public static Address fromString(@Nullable NetworkParameters params, String str)
throws AddressFormatException {
try {
return LegacyAddress.fromBase58(params, str);
} catch (WrongNetworkException x) {
} catch (AddressFormatException.WrongNetwork x) {
throw x;
} catch (AddressFormatException x) {
try {
return SegwitAddress.fromBech32(params, str);
} catch (WrongNetworkException x2) {
} catch (AddressFormatException.WrongNetwork x2) {
throw x;
} catch (AddressFormatException x2) {
throw new AddressFormatException(str);

View File

@ -26,4 +26,20 @@ public class AddressFormatException extends IllegalArgumentException {
public AddressFormatException(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
* case as they are trying to send money across different chains, an operation that is guaranteed to destroy the
* money.
*/
public static class WrongNetwork extends AddressFormatException {
public WrongNetwork(int versionHeader) {
super("Version code of address did not match acceptable versions for network: " + versionHeader);
}
public WrongNetwork(String hrp) {
super("Human readable part of address did not match acceptable HRPs for network: " + hrp);
}
}
}

View File

@ -40,11 +40,11 @@ public class DumpedPrivateKey extends PrefixedChecksummedBytes {
* The textual form of the private key.
* @throws AddressFormatException
* if the given base58 doesn't parse or the checksum is invalid
* @throws WrongNetworkException
* @throws AddressFormatException.WrongNetwork
* if the given private key is valid but for a different chain (eg testnet vs mainnet)
*/
public static DumpedPrivateKey fromBase58(@Nullable NetworkParameters params, String base58)
throws AddressFormatException {
throws AddressFormatException, AddressFormatException.WrongNetwork {
byte[] versionAndDataBytes = Base58.decodeChecked(base58);
int version = versionAndDataBytes[0] & 0xFF;
byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length);
@ -56,7 +56,7 @@ public class DumpedPrivateKey extends PrefixedChecksummedBytes {
} else {
if (version == params.getDumpedPrivateKeyHeader())
return new DumpedPrivateKey(params, bytes);
throw new WrongNetworkException(version);
throw new AddressFormatException.WrongNetwork(version);
}
}

View File

@ -135,11 +135,11 @@ public class LegacyAddress extends Address {
* base58-encoded textual form of the address
* @throws AddressFormatException
* if the given base58 doesn't parse or the checksum is invalid
* @throws WrongNetworkException
* @throws AddressFormatException.WrongNetwork
* 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, WrongNetworkException {
throws AddressFormatException, AddressFormatException.WrongNetwork {
byte[] versionAndDataBytes = Base58.decodeChecked(base58);
int version = versionAndDataBytes[0] & 0xFF;
byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length);
@ -156,7 +156,7 @@ public class LegacyAddress extends Address {
return new LegacyAddress(params, false, bytes);
else if (version == params.getP2SHHeader())
return new LegacyAddress(params, true, bytes);
throw new WrongNetworkException(version);
throw new AddressFormatException.WrongNetwork(version);
}
}

View File

@ -180,7 +180,7 @@ public class SegwitAddress extends Address {
} else {
if (bechData.hrp.equals(params.getSegwitAddressHrp()))
return new SegwitAddress(params, bechData.data);
throw new WrongNetworkException(bechData.hrp);
throw new AddressFormatException.WrongNetwork(bechData.hrp);
}
}

View File

@ -1,33 +0,0 @@
/*
* Copyright 2012 Google Inc.
* Copyright 2018 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.core;
/**
* This exception is thrown by the address class when you try and decode an address with a version code that isn't
* used by that network. You shouldn't allow the user to proceed in this case as they are trying to send money across
* different chains, an operation that is guaranteed to destroy the money.
*/
public class WrongNetworkException extends AddressFormatException {
public WrongNetworkException(int verCode) {
super("Version code of address did not match acceptable versions for network: " + verCode);
}
public WrongNetworkException(String hrp) {
super("Human readable part of address did not match acceptable HRPs for network: " + hrp);
}
}

View File

@ -40,7 +40,7 @@ public class DumpedPrivateKeyTest {
DumpedPrivateKey.fromBase58(MAINNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk");
}
@Test(expected = WrongNetworkException.class)
@Test(expected = AddressFormatException.WrongNetwork.class)
public void checkNetworkWrong() throws Exception {
DumpedPrivateKey.fromBase58(TESTNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk");
}

View File

@ -83,7 +83,7 @@ public class LegacyAddressTest {
try {
LegacyAddress.fromBase58(TESTNET, "this is not a valid address!");
fail();
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
fail();
} catch (AddressFormatException e) {
// Success.
@ -93,7 +93,7 @@ public class LegacyAddressTest {
try {
LegacyAddress.fromBase58(TESTNET, "");
fail();
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
fail();
} catch (AddressFormatException e) {
// Success.
@ -103,7 +103,7 @@ public class LegacyAddressTest {
try {
LegacyAddress.fromBase58(TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
fail();
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
// Success.
} catch (AddressFormatException e) {
fail();

View File

@ -603,7 +603,7 @@ public class WalletTool {
} else {
t.addOutput(outputSpec.value, outputSpec.key);
}
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
System.err.println("Malformed output specification, address is for a different network: " + spec);
return;
} catch (AddressFormatException e) {
@ -725,7 +725,7 @@ public class WalletTool {
value = outputSpec.value;
byte[] refundPubKey = new BigInteger(refund, 16).toByteArray();
refundKey = ECKey.fromPublicOnly(refundPubKey);
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
System.err.println("Malformed output specification, address is for a different network.");
return;
} catch (AddressFormatException e) {
@ -805,7 +805,7 @@ public class WalletTool {
try {
outputSpec = new OutputSpec(output);
value = outputSpec.value;
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
System.err.println("Malformed output specification, address is for a different network.");
return;
} catch (AddressFormatException e) {
@ -909,7 +909,7 @@ public class WalletTool {
try {
outputSpec = new OutputSpec(output);
value = outputSpec.value;
} catch (WrongNetworkException e) {
} catch (AddressFormatException.WrongNetwork e) {
System.err.println("Malformed output specification, address is for a different network.");
return;
} catch (AddressFormatException e) {