Skip to content

Commit

Permalink
The idea of this patchset is to check if base has no unwanted depende…
Browse files Browse the repository at this point in the history
…ncies 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.
  • Loading branch information
schildbach committed Jun 25, 2024
1 parent 32e9145 commit 0e45ada
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 251 deletions.
48 changes: 0 additions & 48 deletions core/src/main/java/org/bitcoinj/base/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,52 +27,6 @@
* Use {@link AddressParser} to construct any kind of address from its textual form.
*/
public interface Address extends Comparable<Address> {
/**
* 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.
*
Expand Down
16 changes: 0 additions & 16 deletions core/src/main/java/org/bitcoinj/base/AddressParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.bitcoinj.base;

import org.bitcoinj.base.exceptions.AddressFormatException;
import org.bitcoinj.core.NetworkParameters;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -92,19 +91,4 @@ static AddressParser getLegacy() {
static AddressParser getLegacy(Network network) {
return DefaultAddressParserProvider.fromNetworks().forNetwork(network);
}

/**
* Get a <i>legacy</i> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +76,7 @@ public AddressParser forNetwork(Network network) {
/**
* Dynamically create a new AddressParser using a snapshot of currently configured networks
* from Networks.get().
*
* @return A backward-compatible parser
*/
static DefaultAddressParserProvider fromNetworks() {
Expand Down
82 changes: 0 additions & 82 deletions core/src/main/java/org/bitcoinj/base/LegacyAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)
Expand Down
71 changes: 0 additions & 71 deletions core/src/main/java/org/bitcoinj/base/SegwitAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

Expand Down
13 changes: 0 additions & 13 deletions core/src/main/java/org/bitcoinj/core/NetworkParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 0 additions & 10 deletions core/src/test/java/org/bitcoinj/base/LegacyAddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0e45ada

Please sign in to comment.