mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-08-01 12:31:23 +00:00
Move WrongNetworkException to AddressFormatException.WrongNetwork.
This commit is contained in:
@@ -47,19 +47,19 @@ public abstract class Address extends PrefixedChecksummedBytes {
|
|||||||
* @return constructed address
|
* @return constructed address
|
||||||
* @throws AddressFormatException
|
* @throws AddressFormatException
|
||||||
* if the given string doesn't parse or the checksum is invalid
|
* 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)
|
* 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)
|
public static Address fromString(@Nullable NetworkParameters params, String str)
|
||||||
throws AddressFormatException {
|
throws AddressFormatException {
|
||||||
try {
|
try {
|
||||||
return LegacyAddress.fromBase58(params, str);
|
return LegacyAddress.fromBase58(params, str);
|
||||||
} catch (WrongNetworkException x) {
|
} catch (AddressFormatException.WrongNetwork x) {
|
||||||
throw x;
|
throw x;
|
||||||
} catch (AddressFormatException x) {
|
} catch (AddressFormatException x) {
|
||||||
try {
|
try {
|
||||||
return SegwitAddress.fromBech32(params, str);
|
return SegwitAddress.fromBech32(params, str);
|
||||||
} catch (WrongNetworkException x2) {
|
} catch (AddressFormatException.WrongNetwork x2) {
|
||||||
throw x;
|
throw x;
|
||||||
} catch (AddressFormatException x2) {
|
} catch (AddressFormatException x2) {
|
||||||
throw new AddressFormatException(str);
|
throw new AddressFormatException(str);
|
||||||
|
@@ -26,4 +26,20 @@ public class AddressFormatException extends IllegalArgumentException {
|
|||||||
public AddressFormatException(String message) {
|
public AddressFormatException(String message) {
|
||||||
super(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -40,11 +40,11 @@ public class DumpedPrivateKey extends PrefixedChecksummedBytes {
|
|||||||
* The textual form of the private key.
|
* The textual form of the private key.
|
||||||
* @throws AddressFormatException
|
* @throws AddressFormatException
|
||||||
* if the given base58 doesn't parse or the checksum is invalid
|
* 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)
|
* 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)
|
public static DumpedPrivateKey fromBase58(@Nullable NetworkParameters params, String base58)
|
||||||
throws AddressFormatException {
|
throws AddressFormatException, AddressFormatException.WrongNetwork {
|
||||||
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);
|
||||||
@@ -56,7 +56,7 @@ public class DumpedPrivateKey extends PrefixedChecksummedBytes {
|
|||||||
} else {
|
} else {
|
||||||
if (version == params.getDumpedPrivateKeyHeader())
|
if (version == params.getDumpedPrivateKeyHeader())
|
||||||
return new DumpedPrivateKey(params, bytes);
|
return new DumpedPrivateKey(params, bytes);
|
||||||
throw new WrongNetworkException(version);
|
throw new AddressFormatException.WrongNetwork(version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -135,11 +135,11 @@ public class LegacyAddress extends Address {
|
|||||||
* base58-encoded textual form of the address
|
* base58-encoded textual form of the address
|
||||||
* @throws AddressFormatException
|
* @throws AddressFormatException
|
||||||
* if the given base58 doesn't parse or the checksum is invalid
|
* 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)
|
* if the given address is valid but for a different chain (eg testnet vs mainnet)
|
||||||
*/
|
*/
|
||||||
public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58)
|
public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58)
|
||||||
throws AddressFormatException, WrongNetworkException {
|
throws AddressFormatException, AddressFormatException.WrongNetwork {
|
||||||
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);
|
||||||
@@ -156,7 +156,7 @@ public class LegacyAddress extends Address {
|
|||||||
return new LegacyAddress(params, false, bytes);
|
return new LegacyAddress(params, false, bytes);
|
||||||
else if (version == params.getP2SHHeader())
|
else if (version == params.getP2SHHeader())
|
||||||
return new LegacyAddress(params, true, bytes);
|
return new LegacyAddress(params, true, bytes);
|
||||||
throw new WrongNetworkException(version);
|
throw new AddressFormatException.WrongNetwork(version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -180,7 +180,7 @@ public class SegwitAddress extends Address {
|
|||||||
} else {
|
} else {
|
||||||
if (bechData.hrp.equals(params.getSegwitAddressHrp()))
|
if (bechData.hrp.equals(params.getSegwitAddressHrp()))
|
||||||
return new SegwitAddress(params, bechData.data);
|
return new SegwitAddress(params, bechData.data);
|
||||||
throw new WrongNetworkException(bechData.hrp);
|
throw new AddressFormatException.WrongNetwork(bechData.hrp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -40,7 +40,7 @@ public class DumpedPrivateKeyTest {
|
|||||||
DumpedPrivateKey.fromBase58(MAINNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk");
|
DumpedPrivateKey.fromBase58(MAINNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = WrongNetworkException.class)
|
@Test(expected = AddressFormatException.WrongNetwork.class)
|
||||||
public void checkNetworkWrong() throws Exception {
|
public void checkNetworkWrong() throws Exception {
|
||||||
DumpedPrivateKey.fromBase58(TESTNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk");
|
DumpedPrivateKey.fromBase58(TESTNET, "5HtUCLMFWNueqN9unpgX2DzjMg6SDNZyKRb8s3LJgpFg5ubuMrk");
|
||||||
}
|
}
|
||||||
|
@@ -83,7 +83,7 @@ public class LegacyAddressTest {
|
|||||||
try {
|
try {
|
||||||
LegacyAddress.fromBase58(TESTNET, "this is not a valid address!");
|
LegacyAddress.fromBase58(TESTNET, "this is not a valid address!");
|
||||||
fail();
|
fail();
|
||||||
} catch (WrongNetworkException e) {
|
} catch (AddressFormatException.WrongNetwork e) {
|
||||||
fail();
|
fail();
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
// Success.
|
// Success.
|
||||||
@@ -93,7 +93,7 @@ public class LegacyAddressTest {
|
|||||||
try {
|
try {
|
||||||
LegacyAddress.fromBase58(TESTNET, "");
|
LegacyAddress.fromBase58(TESTNET, "");
|
||||||
fail();
|
fail();
|
||||||
} catch (WrongNetworkException e) {
|
} catch (AddressFormatException.WrongNetwork e) {
|
||||||
fail();
|
fail();
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
// Success.
|
// Success.
|
||||||
@@ -103,7 +103,7 @@ public class LegacyAddressTest {
|
|||||||
try {
|
try {
|
||||||
LegacyAddress.fromBase58(TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
LegacyAddress.fromBase58(TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||||
fail();
|
fail();
|
||||||
} catch (WrongNetworkException e) {
|
} catch (AddressFormatException.WrongNetwork e) {
|
||||||
// Success.
|
// Success.
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
fail();
|
fail();
|
||||||
|
@@ -603,7 +603,7 @@ public class WalletTool {
|
|||||||
} else {
|
} else {
|
||||||
t.addOutput(outputSpec.value, outputSpec.key);
|
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);
|
System.err.println("Malformed output specification, address is for a different network: " + spec);
|
||||||
return;
|
return;
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
@@ -725,7 +725,7 @@ public class WalletTool {
|
|||||||
value = outputSpec.value;
|
value = outputSpec.value;
|
||||||
byte[] refundPubKey = new BigInteger(refund, 16).toByteArray();
|
byte[] refundPubKey = new BigInteger(refund, 16).toByteArray();
|
||||||
refundKey = ECKey.fromPublicOnly(refundPubKey);
|
refundKey = ECKey.fromPublicOnly(refundPubKey);
|
||||||
} catch (WrongNetworkException e) {
|
} catch (AddressFormatException.WrongNetwork e) {
|
||||||
System.err.println("Malformed output specification, address is for a different network.");
|
System.err.println("Malformed output specification, address is for a different network.");
|
||||||
return;
|
return;
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
@@ -805,7 +805,7 @@ public class WalletTool {
|
|||||||
try {
|
try {
|
||||||
outputSpec = new OutputSpec(output);
|
outputSpec = new OutputSpec(output);
|
||||||
value = outputSpec.value;
|
value = outputSpec.value;
|
||||||
} catch (WrongNetworkException e) {
|
} catch (AddressFormatException.WrongNetwork e) {
|
||||||
System.err.println("Malformed output specification, address is for a different network.");
|
System.err.println("Malformed output specification, address is for a different network.");
|
||||||
return;
|
return;
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
@@ -909,7 +909,7 @@ public class WalletTool {
|
|||||||
try {
|
try {
|
||||||
outputSpec = new OutputSpec(output);
|
outputSpec = new OutputSpec(output);
|
||||||
value = outputSpec.value;
|
value = outputSpec.value;
|
||||||
} catch (WrongNetworkException e) {
|
} catch (AddressFormatException.WrongNetwork e) {
|
||||||
System.err.println("Malformed output specification, address is for a different network.");
|
System.err.println("Malformed output specification, address is for a different network.");
|
||||||
return;
|
return;
|
||||||
} catch (AddressFormatException e) {
|
} catch (AddressFormatException e) {
|
||||||
|
Reference in New Issue
Block a user