Skip to content

Commit

Permalink
Remove the concept of "acceptable Base58 address headers" (version he…
Browse files Browse the repository at this point in the history
…aders) for a given network.

The concept was always weakly defined, as version headers have been used for different types of
data too (which wasn't included in the list). What's acceptable is always dependent on the
usecase, not only the network.

Also, with the coming Bech32 addresses there are no int version headers any more. Rather, it
uses an HRP (human readable part) which is a string.

This is an API breaking change, but I doubt many users have used this in the form of a list.
  • Loading branch information
Andreas Schildbach committed Feb 21, 2018
1 parent b87d22e commit 2241253
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 32 deletions.
13 changes: 6 additions & 7 deletions core/src/main/java/org/bitcoinj/core/Address.java
Expand Up @@ -59,7 +59,7 @@ public Address(NetworkParameters params, int version, byte[] hash160) throws Wro
checkNotNull(params);
checkArgument(hash160.length == 20, "Addresses are 160-bit hashes, so you must provide 20 bytes");
if (!isAcceptableVersion(params, version))
throw new WrongNetworkException(version, params.getAcceptableAddressCodes());
throw new WrongNetworkException(version);
this.params = params;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public Address(@Nullable NetworkParameters params, String address) throws Addres
super(address);
if (params != null) {
if (!isAcceptableVersion(params, version)) {
throw new WrongNetworkException(version, params.getAcceptableAddressCodes());
throw new WrongNetworkException(version);
}
this.params = params;
} else {
Expand Down Expand Up @@ -173,11 +173,10 @@ public static NetworkParameters getParametersFromAddress(String address) throws
* Check if a given address version is valid given the NetworkParameters.
*/
private static boolean isAcceptableVersion(NetworkParameters params, int version) {
for (int v : params.getAcceptableAddressCodes()) {
if (version == v) {
return true;
}
}
if (version == params.getAddressHeader())
return true;
if (version == params.getP2SHHeader())
return true;
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/DumpedPrivateKey.java
Expand Up @@ -69,7 +69,7 @@ private static byte[] encode(byte[] keyBytes, boolean compressed) {
public DumpedPrivateKey(@Nullable NetworkParameters params, String encoded) throws AddressFormatException {
super(encoded);
if (params != null && version != params.getDumpedPrivateKeyHeader())
throw new WrongNetworkException(version, new int[]{ params.getDumpedPrivateKeyHeader() });
throw new WrongNetworkException(version);
if (bytes.length != 32 && bytes.length != 33) {
throw new AddressFormatException("Wrong number of bytes for a private key, not 32 or 33");
}
Expand Down
10 changes: 0 additions & 10 deletions core/src/main/java/org/bitcoinj/core/NetworkParameters.java
Expand Up @@ -100,7 +100,6 @@ public abstract class NetworkParameters {
protected int spendableCoinbaseDepth;
protected int subsidyDecreaseBlockCount;

protected int[] acceptableAddressCodes;
protected String[] dnsSeeds;
protected int[] addrSeeds;
protected HttpDiscovery.Details[] httpSeeds = {};
Expand Down Expand Up @@ -346,15 +345,6 @@ public int getTargetTimespan() {
return targetTimespan;
}

/**
* The version codes that prefix addresses which are acceptable on this network. Although Satoshi intended these to
* be used for "versioning", in fact they are today used to discriminate what kind of data is contained in the
* address and to prevent accidentally sending coins across chains which would destroy them.
*/
public int[] getAcceptableAddressCodes() {
return acceptableAddressCodes;
}

/**
* If we are running in testnet-in-a-box mode, we allow connections to nodes with 0 non-genesis blocks.
*/
Expand Down
10 changes: 2 additions & 8 deletions core/src/main/java/org/bitcoinj/core/WrongNetworkException.java
Expand Up @@ -16,8 +16,6 @@

package org.bitcoinj.core;

import java.util.Arrays;

/**
* 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
Expand All @@ -26,13 +24,9 @@
public class WrongNetworkException extends AddressFormatException {
/** The version code that was provided in the address. */
public int verCode;
/** The list of acceptable versions that were expected given the addresses network parameters. */
public int[] acceptableVersions;

public WrongNetworkException(int verCode, int[] acceptableVersions) {
super("Version code of address did not match acceptable versions for network: " + verCode + " not in " +
Arrays.toString(acceptableVersions));
public WrongNetworkException(int verCode) {
super("Version code of address did not match acceptable versions for network: " + verCode);
this.verCode = verCode;
this.acceptableVersions = acceptableVersions;
}
}
1 change: 0 additions & 1 deletion core/src/main/java/org/bitcoinj/params/MainNetParams.java
Expand Up @@ -40,7 +40,6 @@ public MainNetParams() {
dumpedPrivateKeyHeader = 128;
addressHeader = 0;
p2shHeader = 5;
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
port = 8333;
packetMagic = 0xf9beb4d9L;
bip32HeaderPub = 0x0488B21E; //The 4 byte header that serializes in base58 to "xpub".
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/bitcoinj/params/TestNet2Params.java
Expand Up @@ -36,7 +36,6 @@ public TestNet2Params() {
port = 18333;
addressHeader = 111;
p2shHeader = 196;
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
interval = INTERVAL;
targetTimespan = TARGET_TIMESPAN;
maxTarget = Utils.decodeCompactBits(0x1d0fffffL);
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/bitcoinj/params/TestNet3Params.java
Expand Up @@ -46,7 +46,6 @@ public TestNet3Params() {
port = 18333;
addressHeader = 111;
p2shHeader = 196;
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
dumpedPrivateKeyHeader = 239;
genesisBlock.setTime(1296688602L);
genesisBlock.setDifficultyTarget(0x1d00ffffL);
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/bitcoinj/params/UnitTestParams.java
Expand Up @@ -35,7 +35,6 @@ public UnitTestParams() {
packetMagic = 0x0b110907;
addressHeader = 111;
p2shHeader = 196;
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
maxTarget = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
genesisBlock.setTime(System.currentTimeMillis() / 1000);
genesisBlock.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET);
Expand Down
2 changes: 0 additions & 2 deletions core/src/test/java/org/bitcoinj/core/AddressTest.java
Expand Up @@ -105,7 +105,6 @@ public void errorPaths() {
} catch (WrongNetworkException e) {
// Success.
assertEquals(e.verCode, MainNetParams.get().getAddressHeader());
assertTrue(Arrays.equals(e.acceptableVersions, TestNet3Params.get().getAcceptableAddressCodes()));
} catch (AddressFormatException e) {
fail();
}
Expand All @@ -128,7 +127,6 @@ class AltNetwork extends MainNetParams {
id = "alt.network";
addressHeader = 48;
p2shHeader = 5;
acceptableAddressCodes = new int[] { addressHeader, p2shHeader };
}
}
AltNetwork altNetwork = new AltNetwork();
Expand Down

0 comments on commit 2241253

Please sign in to comment.