From d761be61084b6154c2ff659ce9b9d247c00b2dee Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sat, 25 Feb 2023 01:00:11 +0100 Subject: [PATCH] The idea of this patchset is to check if base has no unwanted dependencies to libraries or bitcoinj modules. It was created this way: - Remove all references of non-base packages (imports, fully qualified names) - Try fix "cannot resolve symbol" compiler errors only by removing deprecated methods or variables Optional: After the next release, we could merge this. --- .../main/java/org/bitcoinj/base/Address.java | 48 ----------- .../java/org/bitcoinj/base/AddressParser.java | 37 --------- .../base/DefaultAddressParserProvider.java | 16 ---- .../java/org/bitcoinj/base/LegacyAddress.java | 82 ------------------- .../java/org/bitcoinj/base/SegwitAddress.java | 71 ---------------- .../org/bitcoinj/base/internal/ByteUtils.java | 9 -- .../org/bitcoinj/core/NetworkParameters.java | 13 --- .../org/bitcoinj/base/LegacyAddressTest.java | 10 --- 8 files changed, 286 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/base/Address.java b/core/src/main/java/org/bitcoinj/base/Address.java index 582936d4ad8..fbd4b1b312b 100644 --- a/core/src/main/java/org/bitcoinj/base/Address.java +++ b/core/src/main/java/org/bitcoinj/base/Address.java @@ -17,8 +17,6 @@ package org.bitcoinj.base; import org.bitcoinj.base.exceptions.AddressFormatException; -import org.bitcoinj.crypto.ECKey; -import org.bitcoinj.core.NetworkParameters; import javax.annotation.Nullable; import java.util.Comparator; @@ -29,52 +27,6 @@ * Use {@link AddressParser} to construct any kind of address from its textual form. */ public interface Address extends Comparable
{ - /** - * Construct an address from its textual form. - * - * @param params the expected network this address is valid for, or null if the network should be derived from the - * textual form - * @param str the textual form of the address, such as "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL" or - * "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" - * @return constructed address - * @throws AddressFormatException - * if the given string doesn't parse or the checksum is invalid - * @throws AddressFormatException.WrongNetwork - * if the given string is valid but not for the expected network (e.g. testnet vs mainnet) - * @deprecated Use {@link org.bitcoinj.wallet.Wallet#parseAddress(String)} or {@link AddressParser#parseAddress(String)} - */ - @Deprecated - static Address fromString(@Nullable NetworkParameters params, String str) - throws AddressFormatException { - return AddressParser.getLegacy(params).parseAddress(str); - } - - /** - * Construct an {@link Address} that represents the public part of the given {@link ECKey}. - * - * @param params - * network this address is valid for - * @param key - * only the public part is used - * @param outputScriptType - * script type the address should use - * @return constructed address - * @deprecated Use {@link ECKey#toAddress(ScriptType, Network)} - */ - @Deprecated - static Address fromKey(final NetworkParameters params, final ECKey key, final ScriptType outputScriptType) { - return key.toAddress(outputScriptType, params.network()); - } - - /** - * @return network this data is valid for - * @deprecated Use {@link #network()} - */ - @Deprecated - default NetworkParameters getParameters() { - return NetworkParameters.of(network()); - } - /** * Get either the public key hash or script hash that is encoded in the address. * diff --git a/core/src/main/java/org/bitcoinj/base/AddressParser.java b/core/src/main/java/org/bitcoinj/base/AddressParser.java index 7e460d3a525..98ba5aa7bb6 100644 --- a/core/src/main/java/org/bitcoinj/base/AddressParser.java +++ b/core/src/main/java/org/bitcoinj/base/AddressParser.java @@ -17,7 +17,6 @@ package org.bitcoinj.base; import org.bitcoinj.base.exceptions.AddressFormatException; -import org.bitcoinj.core.NetworkParameters; import javax.annotation.Nullable; @@ -71,40 +70,4 @@ interface AddressParserProvider { */ AddressParser forNetwork(Network network); } - - /** - * Get a legacy address parser that knows about networks that have been - * dynamically added to the list maintained by {@link org.bitcoinj.params.Networks}. - * @return A parser for all known networks - */ - @Deprecated - static AddressParser getLegacy() { - return DefaultAddressParserProvider.fromNetworks().forKnownNetworks(); - } - - /** - * Get a legacy address parser that knows about networks that have been - * dynamically added to the list maintained by {@link org.bitcoinj.params.Networks}. - * @param network the network to parse for - * @return A parser that will throw for strings that are not valid for network. - */ - @Deprecated - static AddressParser getLegacy(Network network) { - return DefaultAddressParserProvider.fromNetworks().forNetwork(network); - } - - /** - * Get a legacy address parser that knows about networks that have been - * dynamically added to the list maintained by {@link org.bitcoinj.params.Networks}. - * @param params the network to parser for, or {@code null} for all networks. - * @return A parser that will throw for strings that are not valid for network. - */ - @Deprecated - static AddressParser getLegacy(@Nullable NetworkParameters params) { - AddressParser.AddressParserProvider provider = DefaultAddressParserProvider.fromNetworks(); - return (params == null) - ? provider.forKnownNetworks() - : provider.forNetwork(params.network()); - } - } diff --git a/core/src/main/java/org/bitcoinj/base/DefaultAddressParserProvider.java b/core/src/main/java/org/bitcoinj/base/DefaultAddressParserProvider.java index 1a023512eaf..e89ddedf11a 100644 --- a/core/src/main/java/org/bitcoinj/base/DefaultAddressParserProvider.java +++ b/core/src/main/java/org/bitcoinj/base/DefaultAddressParserProvider.java @@ -18,8 +18,6 @@ import org.bitcoinj.base.exceptions.AddressFormatException; import org.bitcoinj.base.internal.StreamUtils; -import org.bitcoinj.core.NetworkParameters; -import org.bitcoinj.params.Networks; import java.util.ArrayList; import java.util.Arrays; @@ -75,20 +73,6 @@ public AddressParser forNetwork(Network network) { return address -> this.parseAddress(address, network); } - /** - * Dynamically create a new AddressParser using a snapshot of currently configured networks - * from Networks.get(). - * @return A backward-compatible parser - * @deprecated This is only used by deprecated methods in {@link AddressParser} - */ - @Deprecated - static DefaultAddressParserProvider fromNetworks() { - List nets = Networks.get().stream() - .map(NetworkParameters::network) - .collect(StreamUtils.toUnmodifiableList()); - return new DefaultAddressParserProvider(nets, nets); - } - private Address parseAddress(String addressString) throws AddressFormatException { try { return parseBase58AnyNetwork(addressString); diff --git a/core/src/main/java/org/bitcoinj/base/LegacyAddress.java b/core/src/main/java/org/bitcoinj/base/LegacyAddress.java index e9d9acb599b..2888e9a4bab 100644 --- a/core/src/main/java/org/bitcoinj/base/LegacyAddress.java +++ b/core/src/main/java/org/bitcoinj/base/LegacyAddress.java @@ -20,8 +20,6 @@ import org.bitcoinj.base.exceptions.AddressFormatException; import org.bitcoinj.base.internal.ByteUtils; -import org.bitcoinj.crypto.ECKey; -import org.bitcoinj.core.NetworkParameters; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -87,22 +85,6 @@ private static Network normalizeNetwork(Network network) { return network; } - /** - * Construct a {@link LegacyAddress} that represents the given pubkey hash. The resulting address will be a P2PKH type of - * address. - * - * @param params - * network this address is valid for - * @param hash160 - * 20-byte pubkey hash - * @return constructed address - * @deprecated Use {@link #fromPubKeyHash(Network, byte[])} - */ - @Deprecated - public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash160) throws AddressFormatException { - return fromPubKeyHash(params.network(), hash160); - } - /** * Construct a {@link LegacyAddress} that represents the given pubkey hash. The resulting address will be a P2PKH type of * address. @@ -115,37 +97,6 @@ public static LegacyAddress fromPubKeyHash(Network network, byte[] hash160) thro return new LegacyAddress(network, false, hash160); } - /** - * Construct a {@link LegacyAddress} that represents the public part of the given {@link ECKey}. Note that an address is - * derived from a hash of the public key and is not the public key itself. - * - * @param params - * network this address is valid for - * @param key - * only the public part is used - * @return constructed address - * @deprecated Use {@link ECKey#toAddress(ScriptType, Network)} - */ - @Deprecated - public static LegacyAddress fromKey(NetworkParameters params, ECKey key) { - return (LegacyAddress) key.toAddress(ScriptType.P2PKH, params.network()); - } - - /** - * Construct a {@link LegacyAddress} that represents the given P2SH script hash. - * - * @param params - * network this address is valid for - * @param hash160 - * P2SH script hash - * @return constructed address - * @deprecated Use {@link #fromScriptHash(Network, byte[])} - */ - @Deprecated - public static LegacyAddress fromScriptHash(NetworkParameters params, byte[] hash160) throws AddressFormatException { - return fromScriptHash(params.network(), hash160); - } - /** * Construct a {@link LegacyAddress} that represents the given P2SH script hash. * @@ -157,26 +108,6 @@ public static LegacyAddress fromScriptHash(Network network, byte[] hash160) thro return new LegacyAddress(network, true, hash160); } - /** - * Construct a {@link LegacyAddress} from its base58 form. - * - * @param params - * expected network this address is valid for, or null if the network should be derived from the - * base58 - * @param base58 - * base58-encoded textual form of the address - * @throws AddressFormatException - * if the given base58 doesn't parse or the checksum is invalid - * @throws AddressFormatException.WrongNetwork - * if the given address is valid but for a different chain (e.g. testnet vs mainnet) - * @deprecated Use {@link #fromBase58(String, Network)} - */ - @Deprecated - public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58) - throws AddressFormatException, AddressFormatException.WrongNetwork { - return (LegacyAddress) AddressParser.getLegacy(params).parseAddress(base58); - } - /** * Construct a {@link LegacyAddress} from its base58 form. * @@ -242,19 +173,6 @@ public ScriptType getOutputScriptType() { return p2sh ? ScriptType.P2SH : ScriptType.P2PKH; } - /** - * Given an address, examines the version byte and attempts to find a matching NetworkParameters. If you aren't sure - * which network the address is intended for (eg, it was provided by a user), you can use this to decide if it is - * compatible with the current wallet. - * - * @return network the address is valid for - * @throws AddressFormatException if the given base58 doesn't parse or the checksum is invalid - */ - @Deprecated - public static NetworkParameters getParametersFromAddress(String address) throws AddressFormatException { - return NetworkParameters.fromAddress(AddressParser.getLegacy().parseAddress(address)); - } - @Override public boolean equals(Object o) { if (this == o) diff --git a/core/src/main/java/org/bitcoinj/base/SegwitAddress.java b/core/src/main/java/org/bitcoinj/base/SegwitAddress.java index c643e24c34e..bc49de1b753 100644 --- a/core/src/main/java/org/bitcoinj/base/SegwitAddress.java +++ b/core/src/main/java/org/bitcoinj/base/SegwitAddress.java @@ -18,8 +18,6 @@ import org.bitcoinj.base.exceptions.AddressFormatException; import org.bitcoinj.base.internal.ByteUtils; -import org.bitcoinj.crypto.ECKey; -import org.bitcoinj.core.NetworkParameters; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -233,24 +231,6 @@ public String toString() { return toBech32(); } - /** - * Construct a {@link SegwitAddress} from its textual form. - * - * @param params - * expected network this address is valid for, or null if the network should be derived from the bech32 - * @param bech32 - * bech32-encoded textual form of the address - * @return constructed address - * @throws AddressFormatException - * if something about the given bech32 address isn't right - * @deprecated Use {@link AddressParser} - */ - @Deprecated - public static SegwitAddress fromBech32(@Nullable NetworkParameters params, String bech32) - throws AddressFormatException { - return (SegwitAddress) AddressParser.getLegacy(params).parseAddress(bech32); - } - /** * Construct a {@link SegwitAddress} from its textual form. * @@ -279,22 +259,6 @@ static SegwitAddress fromBechData(@Nonnull Network network, Bech32.Bech32Data be return address; } - /** - * Construct a {@link SegwitAddress} that represents the given hash, which is either a pubkey hash or a script hash. - * The resulting address will be either a P2WPKH or a P2WSH type of address. - * - * @param params - * network this address is valid for - * @param hash - * 20-byte pubkey hash or 32-byte script hash - * @return constructed address - * @deprecated Use {@link #fromHash(Network, byte[])} - */ - @Deprecated - public static SegwitAddress fromHash(NetworkParameters params, byte[] hash) { - return fromHash(params.network(), hash); - } - /** * Construct a {@link SegwitAddress} that represents the given hash, which is either a pubkey hash or a script hash. * The resulting address will be either a P2WPKH or a P2WSH type of address. @@ -307,25 +271,6 @@ public static SegwitAddress fromHash(Network network, byte[] hash) { return new SegwitAddress(network, 0, hash); } - /** - * Construct a {@link SegwitAddress} that represents the given program, which is either a pubkey, a pubkey hash - * or a script hash – depending on the script version. The resulting address will be either a P2WPKH, a P2WSH or - * a P2TR type of address. - * - * @param params - * network this address is valid for - * @param witnessVersion - * version number between 0 and 16 - * @param witnessProgram - * version dependent witness program - * @return constructed address - * @deprecated Use {@link #fromProgram(Network, int, byte[])} - */ - @Deprecated - public static SegwitAddress fromProgram(NetworkParameters params, int witnessVersion, byte[] witnessProgram) { - return fromProgram(params.network(), witnessVersion, witnessProgram); - } - /** * Construct a {@link SegwitAddress} that represents the given program, which is either a pubkey, a pubkey hash * or a script hash – depending on the script version. The resulting address will be either a P2WPKH, a P2WSH or @@ -340,22 +285,6 @@ public static SegwitAddress fromProgram(Network network, int witnessVersion, byt return new SegwitAddress(network, witnessVersion, witnessProgram); } - /** - * Construct a {@link SegwitAddress} that represents the public part of the given {@link ECKey}. Note that an - * address is derived from a hash of the public key and is not the public key itself. - * - * @param params - * network this address is valid for - * @param key - * only the public part is used - * @return constructed address - * @deprecated Use {@link ECKey#toAddress(ScriptType, org.bitcoinj.base.Network)} - */ - @Deprecated - public static SegwitAddress fromKey(NetworkParameters params, ECKey key) { - return (SegwitAddress) key.toAddress(ScriptType.P2WPKH, params.network()); - } - /** * Get the network this address works on. Use of {@link BitcoinNetwork} is preferred to use of {@link NetworkParameters} * when you need to know what network an address is for. diff --git a/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java b/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java index 0c39013a852..b498673e694 100644 --- a/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java +++ b/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java @@ -16,8 +16,6 @@ package org.bitcoinj.base.internal; -import com.google.common.io.BaseEncoding; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -41,13 +39,6 @@ public class ByteUtils { /** Maximum unsigned value that can be expressed by 32 bits. */ public static final long MAX_UNSIGNED_INTEGER = Integer.toUnsignedLong(-1); - /** - * Hex encoding used throughout the framework. Use with ByteUtils.formatHex(byte[]) or ByteUtils.parseHex(CharSequence). - * @deprecated Use {@link ByteUtils#hexFormat} or {@link ByteUtils#parseHex(String)} or other available - * options. - */ - @Deprecated - public static final BaseEncoding HEX = BaseEncoding.base16().lowerCase(); // 00000001, 00000010, 00000100, 00001000, ... private static final int[] bitMask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; diff --git a/core/src/main/java/org/bitcoinj/core/NetworkParameters.java b/core/src/main/java/org/bitcoinj/core/NetworkParameters.java index 564ad7ca472..602fffa6cd5 100644 --- a/core/src/main/java/org/bitcoinj/core/NetworkParameters.java +++ b/core/src/main/java/org/bitcoinj/core/NetworkParameters.java @@ -205,19 +205,6 @@ public static NetworkParameters fromPmtProtocolID(String pmtProtocolId) { return PaymentProtocol.paramsFromPmtProtocolID(pmtProtocolId); } - /** - * Get a NetworkParameters from an Address. - * Addresses should not be used for storing NetworkParameters. In the future Address will - * be an {@code interface} that only makes a {@link Network} available. - * @param address An address - * @return network parameters - * @deprecated You should be using {@link Address#network()} instead - */ - @Deprecated - public static NetworkParameters fromAddress(Address address) { - return address.getParameters(); - } - public int getSpendableCoinbaseDepth() { return spendableCoinbaseDepth; } diff --git a/core/src/test/java/org/bitcoinj/base/LegacyAddressTest.java b/core/src/test/java/org/bitcoinj/base/LegacyAddressTest.java index 4842c81c825..d2cf433b81f 100644 --- a/core/src/test/java/org/bitcoinj/base/LegacyAddressTest.java +++ b/core/src/test/java/org/bitcoinj/base/LegacyAddressTest.java @@ -120,16 +120,6 @@ public void errorPaths() { } } - @Test - @Deprecated - // Test a deprecated method just to make sure we didn't break it - public void getNetworkViaParameters() { - NetworkParameters params = LegacyAddress.getParametersFromAddress("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL"); - assertEquals(MAINNET.id(), params.getId()); - params = LegacyAddress.getParametersFromAddress("n4eA2nbYqErp7H6jebchxAN59DmNpksexv"); - assertEquals(TESTNET.id(), params.getId()); - } - @Test public void getNetwork() { AddressParser parser = AddressParser.getDefault();