mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-19 01:40:26 +01:00
Address: construct with and store Network, rather than NetworkParametesr
LegacyAddress, SegwitAddress: replace static factories that take NetworkParameters * Add static factory methods that take `Network` * Deprecate static factory methods that take `NetworkParameters` * Add `Networks` support to `NetworkParameters.of(Network)` * Update all usages to use the new methods
This commit is contained in:
parent
55d1919227
commit
f59dc54ac8
@ -36,7 +36,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
*/
|
||||
public abstract class Address implements Comparable<Address> {
|
||||
protected static final AddressParser addressParser = new DefaultAddressParser();
|
||||
protected final NetworkParameters params;
|
||||
protected final Network network;
|
||||
protected final byte[] bytes;
|
||||
|
||||
/**
|
||||
@ -44,9 +44,22 @@ public abstract class Address implements Comparable<Address> {
|
||||
*
|
||||
* @param params the network this address is valid for
|
||||
* @param bytes the binary address data
|
||||
* @deprecated Use {@link Address#Address(Network, byte[])}
|
||||
*/
|
||||
@Deprecated
|
||||
protected Address(NetworkParameters params, byte[] bytes) {
|
||||
this.params = checkNotNull(params);
|
||||
this.network = checkNotNull(params).network();
|
||||
this.bytes = checkNotNull(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an address from its binary form.
|
||||
*
|
||||
* @param network the network this address is valid for
|
||||
* @param bytes the binary address data
|
||||
*/
|
||||
protected Address(Network network, byte[] bytes) {
|
||||
this.network = checkNotNull(network);
|
||||
this.bytes = checkNotNull(bytes);
|
||||
}
|
||||
|
||||
@ -95,12 +108,12 @@ public abstract class Address implements Comparable<Address> {
|
||||
*/
|
||||
@Deprecated
|
||||
public final NetworkParameters getParameters() {
|
||||
return params;
|
||||
return NetworkParameters.of(network);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(params, Arrays.hashCode(bytes));
|
||||
return Objects.hash(network, Arrays.hashCode(bytes));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -108,7 +121,7 @@ public abstract class Address implements Comparable<Address> {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Address other = (Address) o;
|
||||
return this.params.equals(other.params) && Arrays.equals(this.bytes, other.bytes);
|
||||
return this.network == other.network && Arrays.equals(this.bytes, other.bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,7 +141,7 @@ public abstract class Address implements Comparable<Address> {
|
||||
/**
|
||||
* Comparison field order for addresses is:
|
||||
* <ol>
|
||||
* <li>{@link NetworkParameters#getId()}</li>
|
||||
* <li>{@link Network#id()}</li>
|
||||
* <li>Legacy vs. Segwit</li>
|
||||
* <li>(Legacy only) Version byte</li>
|
||||
* <li>remaining {@code bytes}</li>
|
||||
@ -148,7 +161,7 @@ public abstract class Address implements Comparable<Address> {
|
||||
* @return the Network.
|
||||
*/
|
||||
public Network network() {
|
||||
return params.network();
|
||||
return network;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,7 +169,7 @@ public abstract class Address implements Comparable<Address> {
|
||||
* Used by {@link LegacyAddress#compareTo(Address)} and {@link SegwitAddress#compareTo(Address)}.
|
||||
*/
|
||||
protected static final Comparator<Address> PARTIAL_ADDRESS_COMPARATOR = Comparator
|
||||
.comparing((Address a) -> a.params.getId()) // First compare netParams
|
||||
.comparing((Address a) -> a.network.id()) // First compare network
|
||||
.thenComparing(Address::compareTypes); // Then compare address type (subclass)
|
||||
|
||||
private static int compareTypes(Address a, Address b) {
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.BitcoinNetwork;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.exceptions.AddressFormatException;
|
||||
|
||||
@ -31,14 +30,13 @@ public class DefaultAddressParser implements AddressParser {
|
||||
|
||||
@Override
|
||||
public Address parseAddress(String addressString, Network network) throws AddressFormatException {
|
||||
NetworkParameters params = (network != null) ? NetworkParameters.of(network) : null;
|
||||
try {
|
||||
return LegacyAddress.fromBase58(params, addressString);
|
||||
return LegacyAddress.fromBase58(network, addressString);
|
||||
} catch (AddressFormatException.WrongNetwork x) {
|
||||
throw x;
|
||||
} catch (AddressFormatException x) {
|
||||
try {
|
||||
return SegwitAddress.fromBech32(params, addressString);
|
||||
return SegwitAddress.fromBech32(network, addressString);
|
||||
} catch (AddressFormatException.WrongNetwork x2) {
|
||||
throw x;
|
||||
} catch (AddressFormatException x2) {
|
||||
|
@ -420,12 +420,11 @@ public class ECKey implements EncryptableItem {
|
||||
}
|
||||
|
||||
public Address toAddress(ScriptType scriptType, Network network) {
|
||||
NetworkParameters params = NetworkParameters.of(network);
|
||||
if (scriptType == ScriptType.P2PKH) {
|
||||
return LegacyAddress.fromPubKeyHash(params, this.getPubKeyHash());
|
||||
return LegacyAddress.fromPubKeyHash(network, this.getPubKeyHash());
|
||||
} else if (scriptType == ScriptType.P2WPKH) {
|
||||
checkArgument(this.isCompressed(), "only compressed keys allowed");
|
||||
return SegwitAddress.fromHash(params, this.getPubKeyHash());
|
||||
return SegwitAddress.fromHash(network, this.getPubKeyHash());
|
||||
} else {
|
||||
throw new IllegalArgumentException(scriptType.toString());
|
||||
}
|
||||
|
@ -51,19 +51,19 @@ public class LegacyAddress extends Address {
|
||||
public final boolean p2sh;
|
||||
|
||||
/**
|
||||
* Private constructor. Use {@link #fromBase58(NetworkParameters, String)},
|
||||
* {@link #fromPubKeyHash(NetworkParameters, byte[])}, {@link #fromScriptHash(NetworkParameters, byte[])} or
|
||||
* Private constructor. Use {@link #fromBase58(Network, String)},
|
||||
* {@link #fromPubKeyHash(Network, byte[])}, {@link #fromScriptHash(Network, byte[])} or
|
||||
* {@link ECKey#toAddress(ScriptType, Network)}.
|
||||
*
|
||||
* @param params
|
||||
*
|
||||
* @param network
|
||||
* network this address is valid for
|
||||
* @param p2sh
|
||||
* true if hash160 is hash of a script, false if it is hash of a pubkey
|
||||
* @param hash160
|
||||
* 20-byte hash of pubkey or script
|
||||
*/
|
||||
private LegacyAddress(NetworkParameters params, boolean p2sh, byte[] hash160) throws AddressFormatException {
|
||||
super(params, hash160);
|
||||
private LegacyAddress(Network network, boolean p2sh, byte[] hash160) throws AddressFormatException {
|
||||
super(network, hash160);
|
||||
if (hash160.length != 20)
|
||||
throw new AddressFormatException.InvalidDataLength(
|
||||
"Legacy addresses are 20 byte (160 bit) hashes, but got: " + hash160.length);
|
||||
@ -79,9 +79,23 @@ public class LegacyAddress extends Address {
|
||||
* @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 new LegacyAddress(params, false, hash160);
|
||||
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.
|
||||
*
|
||||
* @param network network this address is valid for
|
||||
* @param hash160 20-byte pubkey hash
|
||||
* @return constructed address
|
||||
*/
|
||||
public static LegacyAddress fromPubKeyHash(Network network, byte[] hash160) throws AddressFormatException {
|
||||
return new LegacyAddress(network, false, hash160);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,9 +122,22 @@ public class LegacyAddress extends Address {
|
||||
* @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 new LegacyAddress(params, true, hash160);
|
||||
return fromScriptHash(params.network(), hash160);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link LegacyAddress} that represents the given P2SH script hash.
|
||||
*
|
||||
* @param network network this address is valid for
|
||||
* @param hash160 P2SH script hash
|
||||
* @return constructed address
|
||||
*/
|
||||
public static LegacyAddress fromScriptHash(Network network, byte[] hash160) throws AddressFormatException {
|
||||
return new LegacyAddress(network, true, hash160);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,25 +152,41 @@ public class LegacyAddress extends Address {
|
||||
* 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 (eg testnet vs mainnet)
|
||||
* @deprecated Use {@link #fromBase58(Network, String)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58)
|
||||
throws AddressFormatException, AddressFormatException.WrongNetwork {
|
||||
return fromBase58( (params != null) ? params.network() : null, base58);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link LegacyAddress} from its base58 form.
|
||||
*
|
||||
* @param network 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 (eg testnet vs mainnet)
|
||||
*/
|
||||
public static LegacyAddress fromBase58(@Nullable Network network, String base58)
|
||||
throws AddressFormatException, AddressFormatException.WrongNetwork {
|
||||
NetworkParameters params = (network != null) ? NetworkParameters.of(network) : null;
|
||||
byte[] versionAndDataBytes = Base58.decodeChecked(base58);
|
||||
int version = versionAndDataBytes[0] & 0xFF;
|
||||
byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length);
|
||||
if (params == null) {
|
||||
if (network == null) {
|
||||
for (NetworkParameters p : Networks.get()) {
|
||||
if (version == p.getAddressHeader())
|
||||
return new LegacyAddress(p, false, bytes);
|
||||
return new LegacyAddress(p.network(), false, bytes);
|
||||
else if (version == p.getP2SHHeader())
|
||||
return new LegacyAddress(p, true, bytes);
|
||||
return new LegacyAddress(p.network(), true, bytes);
|
||||
}
|
||||
throw new AddressFormatException.InvalidPrefix("No network found for " + base58);
|
||||
} else {
|
||||
if (version == params.getAddressHeader())
|
||||
return new LegacyAddress(params, false, bytes);
|
||||
return new LegacyAddress(network, false, bytes);
|
||||
else if (version == params.getP2SHHeader())
|
||||
return new LegacyAddress(params, true, bytes);
|
||||
return new LegacyAddress(network, true, bytes);
|
||||
throw new AddressFormatException.WrongNetwork(version);
|
||||
}
|
||||
}
|
||||
@ -154,6 +197,7 @@ public class LegacyAddress extends Address {
|
||||
* @return version header as one byte
|
||||
*/
|
||||
public int getVersion() {
|
||||
NetworkParameters params = NetworkParameters.of(network);
|
||||
return p2sh ? params.getP2SHHeader() : params.getAddressHeader();
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.params.BitcoinNetworkParams;
|
||||
import org.bitcoinj.params.Networks;
|
||||
import org.bitcoinj.protocols.payments.PaymentProtocol;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.store.BlockStore;
|
||||
@ -176,17 +177,17 @@ public abstract class NetworkParameters {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return network parameters for a {@link BitcoinNetwork} enum
|
||||
* Return network parameters for a {@link Network}.
|
||||
* <p>
|
||||
* Alternative networks will be found if they have been registered with {@link Networks} registry.
|
||||
* @param network the network
|
||||
* @return the network parameters for the given string ID
|
||||
* @throws IllegalArgumentException if unknown network
|
||||
*/
|
||||
public static NetworkParameters of(Network network) {
|
||||
if (network instanceof BitcoinNetwork) {
|
||||
return BitcoinNetworkParams.of((BitcoinNetwork) network);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown network");
|
||||
}
|
||||
return (network instanceof BitcoinNetwork)
|
||||
? BitcoinNetworkParams.of((BitcoinNetwork) network)
|
||||
: Networks.find(network).orElseThrow(() -> new IllegalArgumentException("Unknown network"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,6 +18,7 @@ package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.primitives.UnsignedBytes;
|
||||
import org.bitcoinj.base.Bech32;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.ScriptType;
|
||||
import org.bitcoinj.base.exceptions.AddressFormatException;
|
||||
import org.bitcoinj.params.Networks;
|
||||
@ -41,8 +42,8 @@ import static com.google.common.base.Preconditions.checkArgument;
|
||||
* <p>See <a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki">BIP350</a> and
|
||||
* <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki">BIP173</a> for details.</p>
|
||||
*
|
||||
* <p>However, you don't need to care about the internals. Use {@link #fromBech32(NetworkParameters, String)},
|
||||
* {@link #fromHash(NetworkParameters, byte[])} or {@link ECKey#toAddress(ScriptType, org.bitcoinj.base.Network)}
|
||||
* <p>However, you don't need to care about the internals. Use {@link #fromBech32(Network, String)},
|
||||
* {@link #fromHash(org.bitcoinj.base.Network, byte[])} or {@link ECKey#toAddress(ScriptType, Network)}
|
||||
* to construct a native segwit address.</p>
|
||||
*/
|
||||
public class SegwitAddress extends Address {
|
||||
@ -53,19 +54,19 @@ public class SegwitAddress extends Address {
|
||||
public static final int WITNESS_PROGRAM_MAX_LENGTH = 40;
|
||||
|
||||
/**
|
||||
* Private constructor. Use {@link #fromBech32(NetworkParameters, String)},
|
||||
* {@link #fromHash(NetworkParameters, byte[])} or {@link ECKey#toAddress(ScriptType, org.bitcoinj.base.Network)}.
|
||||
* Private constructor. Use {@link #fromBech32(Network, String)},
|
||||
* {@link #fromHash(Network, byte[])} or {@link ECKey#toAddress(ScriptType, Network)}.
|
||||
*
|
||||
* @param params
|
||||
* @param network
|
||||
* network this address is valid for
|
||||
* @param witnessVersion
|
||||
* version number between 0 and 16
|
||||
* @param witnessProgram
|
||||
* hash of pubkey, pubkey or script (depending on version)
|
||||
*/
|
||||
private SegwitAddress(NetworkParameters params, int witnessVersion, byte[] witnessProgram)
|
||||
private SegwitAddress(Network network, int witnessVersion, byte[] witnessProgram)
|
||||
throws AddressFormatException {
|
||||
this(params, encode(witnessVersion, witnessProgram));
|
||||
this(network, encode(witnessVersion, witnessProgram));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,18 +81,18 @@ public class SegwitAddress extends Address {
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor. Use {@link #fromBech32(NetworkParameters, String)},
|
||||
* {@link #fromHash(NetworkParameters, byte[])} or {@link ECKey#toAddress(ScriptType, org.bitcoinj.base.Network)}.
|
||||
* Private constructor. Use {@link #fromBech32(Network, String)},
|
||||
* {@link #fromHash(Network, byte[])} or {@link ECKey#toAddress(ScriptType, Network)}.
|
||||
*
|
||||
* @param params
|
||||
* @param network
|
||||
* network this address is valid for
|
||||
* @param data
|
||||
* in segwit address format, before bit re-arranging and bech32 encoding
|
||||
* @throws AddressFormatException
|
||||
* if any of the sanity checks fail
|
||||
*/
|
||||
private SegwitAddress(NetworkParameters params, byte[] data) throws AddressFormatException {
|
||||
super(params, data);
|
||||
private SegwitAddress(Network network, byte[] data) throws AddressFormatException {
|
||||
super(network, data);
|
||||
if (data.length < 1)
|
||||
throw new AddressFormatException.InvalidDataLength("Zero data found");
|
||||
final int witnessVersion = getWitnessVersion();
|
||||
@ -171,25 +172,41 @@ public class SegwitAddress extends Address {
|
||||
* @return constructed address
|
||||
* @throws AddressFormatException
|
||||
* if something about the given bech32 address isn't right
|
||||
* @deprecated Use {@link #fromBech32(Network, String)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static SegwitAddress fromBech32(@Nullable NetworkParameters params, String bech32)
|
||||
throws AddressFormatException {
|
||||
return fromBech32(params != null ? params.network : null, bech32);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a {@link SegwitAddress} from its textual form.
|
||||
*
|
||||
* @param network 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
|
||||
*/
|
||||
public static SegwitAddress fromBech32(@Nullable Network network, String bech32)
|
||||
throws AddressFormatException {
|
||||
NetworkParameters params = (network != null) ? NetworkParameters.of(network) : null;
|
||||
Bech32.Bech32Data bechData = Bech32.decode(bech32);
|
||||
if (params == null) {
|
||||
if (network == null) {
|
||||
for (NetworkParameters p : Networks.get()) {
|
||||
if (bechData.hrp.equals(p.getSegwitAddressHrp()))
|
||||
return fromBechData(p, bechData);
|
||||
return fromBechData(p.network(), bechData);
|
||||
}
|
||||
throw new AddressFormatException.InvalidPrefix("No network found for " + bech32);
|
||||
} else {
|
||||
if (bechData.hrp.equals(params.getSegwitAddressHrp()))
|
||||
return fromBechData(params, bechData);
|
||||
return fromBechData(network, bechData);
|
||||
throw new AddressFormatException.WrongNetwork(bechData.hrp);
|
||||
}
|
||||
}
|
||||
|
||||
private static SegwitAddress fromBechData(NetworkParameters params, Bech32.Bech32Data bechData) {
|
||||
final SegwitAddress address = new SegwitAddress(params, bechData.data);
|
||||
private static SegwitAddress fromBechData(Network network, Bech32.Bech32Data bechData) {
|
||||
final SegwitAddress address = new SegwitAddress(network, bechData.data);
|
||||
final int witnessVersion = address.getWitnessVersion();
|
||||
if ((witnessVersion == 0 && bechData.encoding != Bech32.Encoding.BECH32) ||
|
||||
(witnessVersion != 0 && bechData.encoding != Bech32.Encoding.BECH32M))
|
||||
@ -206,9 +223,23 @@ public class SegwitAddress extends Address {
|
||||
* @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 new SegwitAddress(params, 0, 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.
|
||||
*
|
||||
* @param network network this address is valid for
|
||||
* @param hash 20-byte pubkey hash or 32-byte script hash
|
||||
* @return constructed address
|
||||
*/
|
||||
public static SegwitAddress fromHash(Network network, byte[] hash) {
|
||||
return new SegwitAddress(network, 0, hash);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -223,9 +254,25 @@ public class SegwitAddress extends Address {
|
||||
* @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 new SegwitAddress(params, witnessVersion, 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
|
||||
* a P2TR type of address.
|
||||
*
|
||||
* @param network network this address is valid for
|
||||
* @param witnessVersion version number between 0 and 16
|
||||
* @param witnessProgram version dependent witness program
|
||||
* @return constructed address
|
||||
*/
|
||||
public static SegwitAddress fromProgram(Network network, int witnessVersion, byte[] witnessProgram) {
|
||||
return new SegwitAddress(network, witnessVersion, witnessProgram);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -250,6 +297,7 @@ public class SegwitAddress extends Address {
|
||||
* @return textual form encoded in bech32
|
||||
*/
|
||||
public String toBech32() {
|
||||
NetworkParameters params = NetworkParameters.of(network);
|
||||
if (getWitnessVersion() == 0)
|
||||
return Bech32.encode(Bech32.Encoding.BECH32, params.getSegwitAddressHrp(), bytes);
|
||||
else
|
||||
|
@ -16,12 +16,14 @@
|
||||
|
||||
package org.bitcoinj.params;
|
||||
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -39,6 +41,17 @@ public class Networks {
|
||||
return networks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a {@link NetworkParameters} for a {@link Network}
|
||||
* @param network The network to find (convert)
|
||||
* @return Matching params if one was registered
|
||||
*/
|
||||
public static Optional<NetworkParameters> find(Network network) {
|
||||
return networks.stream()
|
||||
.filter(p -> p.getId().equals(network.id()))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a single network type by adding it to the {@code Set}.
|
||||
*
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
package org.bitcoinj.script;
|
||||
|
||||
import org.bitcoinj.base.BitcoinNetwork;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.ScriptType;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Address;
|
||||
@ -393,16 +395,17 @@ public class Script {
|
||||
* showing addresses rather than pubkeys.
|
||||
*/
|
||||
public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
|
||||
Network network = params.network();
|
||||
if (ScriptPattern.isP2PKH(this))
|
||||
return LegacyAddress.fromPubKeyHash(params, ScriptPattern.extractHashFromP2PKH(this));
|
||||
return LegacyAddress.fromPubKeyHash(network, ScriptPattern.extractHashFromP2PKH(this));
|
||||
else if (ScriptPattern.isP2SH(this))
|
||||
return LegacyAddress.fromScriptHash(params, ScriptPattern.extractHashFromP2SH(this));
|
||||
return LegacyAddress.fromScriptHash(network, ScriptPattern.extractHashFromP2SH(this));
|
||||
else if (forcePayToPubKey && ScriptPattern.isP2PK(this))
|
||||
return ECKey.fromPublicOnly(ScriptPattern.extractKeyFromP2PK(this)).toAddress(ScriptType.P2PKH, params.network());
|
||||
return ECKey.fromPublicOnly(ScriptPattern.extractKeyFromP2PK(this)).toAddress(ScriptType.P2PKH, network);
|
||||
else if (ScriptPattern.isP2WH(this))
|
||||
return SegwitAddress.fromHash(params, ScriptPattern.extractHashFromP2WH(this));
|
||||
return SegwitAddress.fromHash(network, ScriptPattern.extractHashFromP2WH(this));
|
||||
else if (ScriptPattern.isP2TR(this))
|
||||
return SegwitAddress.fromProgram(params, 1, ScriptPattern.extractOutputKeyFromP2TR(this));
|
||||
return SegwitAddress.fromProgram(network, 1, ScriptPattern.extractOutputKeyFromP2TR(this));
|
||||
else
|
||||
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Cannot cast this script to an address");
|
||||
}
|
||||
|
@ -437,7 +437,7 @@ public class KeyChainGroup implements KeyBag {
|
||||
if (chain.isMarried()) {
|
||||
Script outputScript = chain.freshOutputScript(purpose);
|
||||
checkState(ScriptPattern.isP2SH(outputScript)); // Only handle P2SH for now
|
||||
Address freshAddress = LegacyAddress.fromScriptHash(params,
|
||||
Address freshAddress = LegacyAddress.fromScriptHash(params.network(),
|
||||
ScriptPattern.extractHashFromP2SH(outputScript));
|
||||
maybeLookaheadScripts();
|
||||
currentAddresses.put(purpose, freshAddress);
|
||||
|
@ -1240,7 +1240,7 @@ public class Wallet extends BaseTaggableObject
|
||||
byte[] pubkeyHash = ScriptPattern.extractHashFromP2PKH(script);
|
||||
keyChainGroup.markPubKeyHashAsUsed(pubkeyHash);
|
||||
} else if (ScriptPattern.isP2SH(script)) {
|
||||
LegacyAddress a = LegacyAddress.fromScriptHash(tx.getParams(),
|
||||
LegacyAddress a = LegacyAddress.fromScriptHash(tx.getParams().network(),
|
||||
ScriptPattern.extractHashFromP2SH(script));
|
||||
keyChainGroup.markP2SHAddressAsUsed(a);
|
||||
} else if (ScriptPattern.isP2WH(script)) {
|
||||
|
@ -231,7 +231,7 @@ public class ECKeyTest {
|
||||
// Test vector generated by Bitcoin-Qt.
|
||||
String message = "hello";
|
||||
String sigBase64 = "HxNZdo6ggZ41hd3mM3gfJRqOQPZYcO8z8qdX2BwmpbF11CaOQV+QiZGGQxaYOncKoNW61oRuSMMF8udfK54XqI8=";
|
||||
Address expectedAddress = LegacyAddress.fromBase58(MAINNET, "14YPSNPi6NSXnUxtPAsyJSuw3pv7AU3Cag");
|
||||
Address expectedAddress = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "14YPSNPi6NSXnUxtPAsyJSuw3pv7AU3Cag");
|
||||
ECKey key = ECKey.signedMessageToKey(message, sigBase64);
|
||||
Address gotAddress = key.toAddress(ScriptType.P2PKH, BitcoinNetwork.MAINNET);
|
||||
assertEquals(expectedAddress, gotAddress);
|
||||
|
@ -19,6 +19,8 @@ package org.bitcoinj.core;
|
||||
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
import org.bitcoinj.base.BitcoinNetwork;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.exceptions.AddressFormatException;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
@ -59,21 +61,21 @@ public class LegacyAddressTest {
|
||||
@Test
|
||||
public void stringification() {
|
||||
// Test a testnet address.
|
||||
LegacyAddress a = LegacyAddress.fromPubKeyHash(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
|
||||
LegacyAddress a = LegacyAddress.fromPubKeyHash(BitcoinNetwork.TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
|
||||
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
|
||||
assertEquals(ScriptType.P2PKH, a.getOutputScriptType());
|
||||
|
||||
LegacyAddress b = LegacyAddress.fromPubKeyHash(MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
|
||||
LegacyAddress b = LegacyAddress.fromPubKeyHash(BitcoinNetwork.MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
|
||||
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
|
||||
assertEquals(ScriptType.P2PKH, b.getOutputScriptType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decoding() {
|
||||
LegacyAddress a = LegacyAddress.fromBase58(TESTNET, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
|
||||
LegacyAddress a = LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
|
||||
assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", ByteUtils.HEX.encode(a.getHash()));
|
||||
|
||||
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", ByteUtils.HEX.encode(b.getHash()));
|
||||
}
|
||||
|
||||
@ -81,7 +83,7 @@ public class LegacyAddressTest {
|
||||
public void errorPaths() {
|
||||
// Check what happens if we try and decode garbage.
|
||||
try {
|
||||
LegacyAddress.fromBase58(TESTNET, "this is not a valid address!");
|
||||
LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "this is not a valid address!");
|
||||
fail();
|
||||
} catch (AddressFormatException.WrongNetwork e) {
|
||||
fail();
|
||||
@ -91,7 +93,7 @@ public class LegacyAddressTest {
|
||||
|
||||
// Check the empty case.
|
||||
try {
|
||||
LegacyAddress.fromBase58(TESTNET, "");
|
||||
LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "");
|
||||
fail();
|
||||
} catch (AddressFormatException.WrongNetwork e) {
|
||||
fail();
|
||||
@ -101,7 +103,7 @@ public class LegacyAddressTest {
|
||||
|
||||
// Check the case of a mismatched network.
|
||||
try {
|
||||
LegacyAddress.fromBase58(TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
fail();
|
||||
} catch (AddressFormatException.WrongNetwork e) {
|
||||
// Success.
|
||||
@ -126,17 +128,17 @@ public class LegacyAddressTest {
|
||||
Networks.register(altNetParams);
|
||||
try {
|
||||
// Check if can parse address
|
||||
NetworkParameters params = LegacyAddress.getParametersFromAddress("LLxSnHLN2CYyzB5eWTR9K9rS9uWtbTQFb6");
|
||||
assertEquals(altNetParams.getId(), params.getId());
|
||||
Address altAddress = new DefaultAddressParser().parseAddressAnyNetwork("LLxSnHLN2CYyzB5eWTR9K9rS9uWtbTQFb6");
|
||||
assertEquals(altNetParams.getId(), altAddress.network().id());
|
||||
// Check if main network works as before
|
||||
params = LegacyAddress.getParametersFromAddress("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
assertEquals(MAINNET.getId(), params.getId());
|
||||
Address mainAddress = new DefaultAddressParser().parseAddressAnyNetwork("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
assertEquals(MAINNET.getId(), mainAddress.network().id());
|
||||
} finally {
|
||||
// Unregister network. Do this in a finally block so other tests don't fail if the try block fails to complete
|
||||
Networks.unregister(altNetParams);
|
||||
}
|
||||
try {
|
||||
LegacyAddress.getParametersFromAddress("LLxSnHLN2CYyzB5eWTR9K9rS9uWtbTQFb6");
|
||||
new DefaultAddressParser().parseAddressAnyNetwork("LLxSnHLN2CYyzB5eWTR9K9rS9uWtbTQFb6");
|
||||
fail();
|
||||
} catch (AddressFormatException e) { }
|
||||
}
|
||||
@ -144,10 +146,10 @@ public class LegacyAddressTest {
|
||||
@Test
|
||||
public void p2shAddress() {
|
||||
// Test that we can construct P2SH addresses
|
||||
LegacyAddress mainNetP2SHAddress = LegacyAddress.fromBase58(MainNetParams.get(), "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
|
||||
LegacyAddress mainNetP2SHAddress = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
|
||||
assertEquals(mainNetP2SHAddress.getVersion(), MAINNET.p2shHeader);
|
||||
assertEquals(ScriptType.P2SH, mainNetP2SHAddress.getOutputScriptType());
|
||||
LegacyAddress testNetP2SHAddress = LegacyAddress.fromBase58(TestNet3Params.get(), "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
|
||||
LegacyAddress testNetP2SHAddress = LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
|
||||
assertEquals(testNetP2SHAddress.getVersion(), TESTNET.p2shHeader);
|
||||
assertEquals(ScriptType.P2SH, testNetP2SHAddress.getOutputScriptType());
|
||||
|
||||
@ -159,11 +161,11 @@ public class LegacyAddressTest {
|
||||
|
||||
// Test that we can convert them from hashes
|
||||
byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e");
|
||||
LegacyAddress a = LegacyAddress.fromScriptHash(MAINNET, hex);
|
||||
LegacyAddress a = LegacyAddress.fromScriptHash(BitcoinNetwork.MAINNET, hex);
|
||||
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString());
|
||||
LegacyAddress b = LegacyAddress.fromScriptHash(TESTNET, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
|
||||
LegacyAddress b = LegacyAddress.fromScriptHash(BitcoinNetwork.TESTNET, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
|
||||
assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString());
|
||||
LegacyAddress c = LegacyAddress.fromScriptHash(MAINNET,
|
||||
LegacyAddress c = LegacyAddress.fromScriptHash(BitcoinNetwork.MAINNET,
|
||||
ScriptPattern.extractHashFromP2SH(ScriptBuilder.createP2SHOutputScript(hex)));
|
||||
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString());
|
||||
}
|
||||
@ -180,7 +182,7 @@ public class LegacyAddressTest {
|
||||
|
||||
List<ECKey> keys = Arrays.asList(key1, key2, key3);
|
||||
Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys);
|
||||
LegacyAddress address = LegacyAddress.fromScriptHash(MAINNET,
|
||||
LegacyAddress address = LegacyAddress.fromScriptHash(BitcoinNetwork.MAINNET,
|
||||
ScriptPattern.extractHashFromP2SH(p2shScript));
|
||||
assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString());
|
||||
}
|
||||
@ -188,13 +190,13 @@ public class LegacyAddressTest {
|
||||
@Test
|
||||
public void roundtripBase58() {
|
||||
String base58 = "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL";
|
||||
assertEquals(base58, LegacyAddress.fromBase58(null, base58).toBase58());
|
||||
assertEquals(base58, LegacyAddress.fromBase58((Network) null, base58).toBase58());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void comparisonLessThan() {
|
||||
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
|
||||
LegacyAddress a = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
|
||||
|
||||
int result = a.compareTo(b);
|
||||
assertTrue(result < 0);
|
||||
@ -202,8 +204,8 @@ public class LegacyAddressTest {
|
||||
|
||||
@Test
|
||||
public void comparisonGreaterThan() {
|
||||
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
|
||||
LegacyAddress a = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
|
||||
|
||||
int result = a.compareTo(b);
|
||||
assertTrue(result > 0);
|
||||
@ -212,8 +214,8 @@ public class LegacyAddressTest {
|
||||
@Test
|
||||
public void comparisonNotEquals() {
|
||||
// These addresses only differ by version byte
|
||||
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "14wivxvNTv9THhewPotsooizZawaWbEKE2");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "35djrWQp1pTqNsMNWuZUES5vi7EJ74m9Eh");
|
||||
LegacyAddress a = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "14wivxvNTv9THhewPotsooizZawaWbEKE2");
|
||||
LegacyAddress b = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "35djrWQp1pTqNsMNWuZUES5vi7EJ74m9Eh");
|
||||
|
||||
int result = a.compareTo(b);
|
||||
assertTrue(result != 0);
|
||||
@ -226,8 +228,8 @@ public class LegacyAddressTest {
|
||||
String line;
|
||||
while ((line = dataSetReader.readLine()) != null) {
|
||||
String addr[] = line.split(",");
|
||||
LegacyAddress first = LegacyAddress.fromBase58(MAINNET, addr[0]);
|
||||
LegacyAddress second = LegacyAddress.fromBase58(MAINNET, addr[1]);
|
||||
LegacyAddress first = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, addr[0]);
|
||||
LegacyAddress second = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, addr[1]);
|
||||
assertTrue(first.compareTo(second) < 0);
|
||||
assertTrue(first.toString().compareTo(second.toString()) < 0);
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ package org.bitcoinj.core;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
import org.bitcoinj.base.BitcoinNetwork;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.exceptions.AddressFormatException;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
@ -52,9 +54,9 @@ public class SegwitAddressTest {
|
||||
public void example_p2wpkh_mainnet() {
|
||||
String bech32 = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";
|
||||
|
||||
SegwitAddress address = SegwitAddress.fromBech32(MAINNET, bech32);
|
||||
SegwitAddress address = SegwitAddress.fromBech32(BitcoinNetwork.MAINNET, bech32);
|
||||
|
||||
assertEquals(MAINNET, address.params);
|
||||
assertEquals(MAINNET, address.getParameters());
|
||||
assertEquals("0014751e76e8199196d454941c45d1b3a323f1433bd6",
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WPKH, address.getOutputScriptType());
|
||||
@ -66,9 +68,9 @@ public class SegwitAddressTest {
|
||||
public void example_p2wsh_mainnet() {
|
||||
String bech32 = "bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3";
|
||||
|
||||
SegwitAddress address = SegwitAddress.fromBech32(MAINNET, bech32);
|
||||
SegwitAddress address = SegwitAddress.fromBech32(BitcoinNetwork.MAINNET, bech32);
|
||||
|
||||
assertEquals(MAINNET, address.params);
|
||||
assertEquals(MAINNET, address.getParameters());
|
||||
assertEquals("00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262",
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WSH, address.getOutputScriptType());
|
||||
@ -80,9 +82,9 @@ public class SegwitAddressTest {
|
||||
public void example_p2wpkh_testnet() {
|
||||
String bech32 = "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx";
|
||||
|
||||
SegwitAddress address = SegwitAddress.fromBech32(TESTNET, bech32);
|
||||
SegwitAddress address = SegwitAddress.fromBech32(BitcoinNetwork.TESTNET, bech32);
|
||||
|
||||
assertEquals(TESTNET, address.params);
|
||||
assertEquals(TESTNET, address.getParameters());
|
||||
assertEquals("0014751e76e8199196d454941c45d1b3a323f1433bd6",
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WPKH, address.getOutputScriptType());
|
||||
@ -94,9 +96,9 @@ public class SegwitAddressTest {
|
||||
public void example_p2wsh_testnet() {
|
||||
String bech32 = "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7";
|
||||
|
||||
SegwitAddress address = SegwitAddress.fromBech32(TESTNET, bech32);
|
||||
SegwitAddress address = SegwitAddress.fromBech32(BitcoinNetwork.TESTNET, bech32);
|
||||
|
||||
assertEquals(TESTNET, address.params);
|
||||
assertEquals(TESTNET, address.getParameters());
|
||||
assertEquals("00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262",
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WSH, address.getOutputScriptType());
|
||||
@ -107,15 +109,15 @@ public class SegwitAddressTest {
|
||||
@Test
|
||||
public void validAddresses() {
|
||||
for (AddressData valid : VALID_ADDRESSES) {
|
||||
SegwitAddress address = SegwitAddress.fromBech32(null, valid.address);
|
||||
SegwitAddress address = SegwitAddress.fromBech32((Network) null, valid.address);
|
||||
|
||||
assertEquals(valid.expectedParams, address.params);
|
||||
assertEquals(valid.expectedParams, address.getParameters());
|
||||
assertEquals(valid.expectedScriptPubKey,
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(valid.address.toLowerCase(Locale.ROOT), address.toBech32());
|
||||
if (valid.expectedWitnessVersion == 0) {
|
||||
Script expectedScriptPubKey = new Script(ByteUtils.HEX.decode(valid.expectedScriptPubKey));
|
||||
assertEquals(address, SegwitAddress.fromHash(valid.expectedParams,
|
||||
assertEquals(address, SegwitAddress.fromHash(valid.expectedParams.network(),
|
||||
ScriptPattern.extractHashFromP2WH(expectedScriptPubKey)));
|
||||
}
|
||||
assertEquals(valid.expectedWitnessVersion, address.getWitnessVersion());
|
||||
@ -165,7 +167,7 @@ public class SegwitAddressTest {
|
||||
public void invalidAddresses() {
|
||||
for (String invalid : INVALID_ADDRESSES) {
|
||||
try {
|
||||
SegwitAddress.fromBech32(null, invalid);
|
||||
SegwitAddress.fromBech32((Network) null, invalid);
|
||||
fail(invalid);
|
||||
} catch (AddressFormatException x) {
|
||||
// expected
|
||||
@ -201,26 +203,26 @@ public class SegwitAddressTest {
|
||||
|
||||
@Test(expected = AddressFormatException.InvalidDataLength.class)
|
||||
public void fromBech32_version0_invalidLength() {
|
||||
SegwitAddress.fromBech32(null, "BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P");
|
||||
SegwitAddress.fromBech32((Network) null, "BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P");
|
||||
}
|
||||
|
||||
@Test(expected = AddressFormatException.InvalidDataLength.class)
|
||||
public void fromBech32_tooShort() {
|
||||
SegwitAddress.fromBech32(null, "bc1rw5uspcuh");
|
||||
SegwitAddress.fromBech32((Network) null, "bc1rw5uspcuh");
|
||||
}
|
||||
|
||||
@Test(expected = AddressFormatException.InvalidDataLength.class)
|
||||
public void fromBech32_tooLong() {
|
||||
SegwitAddress.fromBech32(null, "bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90");
|
||||
SegwitAddress.fromBech32((Network) null, "bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90");
|
||||
}
|
||||
|
||||
@Test(expected = AddressFormatException.InvalidPrefix.class)
|
||||
public void fromBech32_invalidHrp() {
|
||||
SegwitAddress.fromBech32(null, "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty");
|
||||
SegwitAddress.fromBech32((Network) null, "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty");
|
||||
}
|
||||
|
||||
@Test(expected = AddressFormatException.WrongNetwork.class)
|
||||
public void fromBech32_wrongNetwork() {
|
||||
SegwitAddress.fromBech32(TESTNET, "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj");
|
||||
SegwitAddress.fromBech32(BitcoinNetwork.TESTNET, "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj");
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ public class TransactionOutputTest extends TestWithWallet {
|
||||
@Test
|
||||
public void testP2SHOutputScript() {
|
||||
String P2SHAddressString = "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU";
|
||||
Address P2SHAddress = LegacyAddress.fromBase58(MAINNET, P2SHAddressString);
|
||||
Address P2SHAddress = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, P2SHAddressString);
|
||||
Script script = ScriptBuilder.createOutputScript(P2SHAddress);
|
||||
Transaction tx = new Transaction(MAINNET);
|
||||
tx.addOutput(Coin.COIN, script);
|
||||
|
@ -103,7 +103,7 @@ public class ScriptTest {
|
||||
byte[] pubkeyBytes = HEX.decode(pubkeyProg);
|
||||
Script pubkey = new Script(pubkeyBytes);
|
||||
assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString());
|
||||
Address toAddr = LegacyAddress.fromPubKeyHash(TESTNET, ScriptPattern.extractHashFromP2PKH(pubkey));
|
||||
Address toAddr = LegacyAddress.fromPubKeyHash(BitcoinNetwork.TESTNET, ScriptPattern.extractHashFromP2PKH(pubkey));
|
||||
assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString());
|
||||
}
|
||||
|
||||
@ -135,7 +135,7 @@ public class ScriptTest {
|
||||
|
||||
@Test
|
||||
public void testP2SHOutputScript() {
|
||||
Address p2shAddress = LegacyAddress.fromBase58(MAINNET, "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
|
||||
Address p2shAddress = LegacyAddress.fromBase58(BitcoinNetwork.MAINNET, "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
|
||||
assertTrue(ScriptPattern.isP2SH(ScriptBuilder.createOutputScript(p2shAddress)));
|
||||
}
|
||||
|
||||
@ -157,7 +157,7 @@ public class ScriptTest {
|
||||
Transaction transaction = TESTNET.getDefaultSerializer().makeTransaction(bytes);
|
||||
TransactionOutput output = transaction.getOutput(1);
|
||||
Transaction spendTx = new Transaction(TESTNET);
|
||||
Address address = LegacyAddress.fromBase58(TESTNET, "n3CFiCmBXVt5d3HXKQ15EFZyhPz4yj5F3H");
|
||||
Address address = LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "n3CFiCmBXVt5d3HXKQ15EFZyhPz4yj5F3H");
|
||||
Script outputScript = ScriptBuilder.createOutputScript(address);
|
||||
spendTx.addOutput(output.getValue(), outputScript);
|
||||
spendTx.addInput(output);
|
||||
@ -474,7 +474,7 @@ public class ScriptTest {
|
||||
assertEquals(toAddress, ScriptBuilder.createOutputScript(toAddress).getToAddress(TESTNET));
|
||||
// pay to script hash
|
||||
Script p2shScript = ScriptBuilder.createP2SHOutputScript(new byte[20]);
|
||||
Address scriptAddress = LegacyAddress.fromScriptHash(TESTNET,
|
||||
Address scriptAddress = LegacyAddress.fromScriptHash(BitcoinNetwork.TESTNET,
|
||||
ScriptPattern.extractHashFromP2SH(p2shScript));
|
||||
assertEquals(scriptAddress, p2shScript.getToAddress(TESTNET));
|
||||
// P2WPKH
|
||||
@ -482,10 +482,10 @@ public class ScriptTest {
|
||||
assertEquals(toAddress, ScriptBuilder.createOutputScript(toAddress).getToAddress(TESTNET));
|
||||
// P2WSH
|
||||
Script p2wshScript = ScriptBuilder.createP2WSHOutputScript(new byte[32]);
|
||||
scriptAddress = SegwitAddress.fromHash(TESTNET, ScriptPattern.extractHashFromP2WH(p2wshScript));
|
||||
scriptAddress = SegwitAddress.fromHash(BitcoinNetwork.TESTNET, ScriptPattern.extractHashFromP2WH(p2wshScript));
|
||||
assertEquals(scriptAddress, p2wshScript.getToAddress(TESTNET));
|
||||
// P2TR
|
||||
toAddress = SegwitAddress.fromProgram(TESTNET, 1, new byte[32]);
|
||||
toAddress = SegwitAddress.fromProgram(BitcoinNetwork.TESTNET, 1, new byte[32]);
|
||||
assertEquals(toAddress, ScriptBuilder.createOutputScript(toAddress).getToAddress(TESTNET));
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ import org.bitcoinj.core.Address;
|
||||
import org.bitcoinj.core.DefaultAddressParser;
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.params.Networks;
|
||||
import org.bitcoinj.testing.MockAltNetworkParams;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -85,8 +86,13 @@ public class BitcoinURITest {
|
||||
NetworkParameters alternativeParameters = new MockAltNetworkParams();
|
||||
String mockNetGoodAddress = MockAltNetworkParams.MOCKNET_GOOD_ADDRESS;
|
||||
|
||||
assertEquals("mockcoin:" + mockNetGoodAddress + "?amount=12.34&label=Hello&message=AMessage",
|
||||
BitcoinURI.convertToBitcoinURI(LegacyAddress.fromBase58(alternativeParameters, mockNetGoodAddress), parseCoin("12.34"), "Hello", "AMessage"));
|
||||
Networks.register(alternativeParameters);
|
||||
try {
|
||||
assertEquals("mockcoin:" + mockNetGoodAddress + "?amount=12.34&label=Hello&message=AMessage",
|
||||
BitcoinURI.convertToBitcoinURI(LegacyAddress.fromBase58(alternativeParameters.network(), mockNetGoodAddress), parseCoin("12.34"), "Hello", "AMessage"));
|
||||
} finally {
|
||||
Networks.unregister(alternativeParameters);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -95,7 +95,7 @@ public class DeterministicKeyChainTest {
|
||||
ECKey key2 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
|
||||
assertFalse(key2.isPubKeyOnly());
|
||||
|
||||
final Address address = LegacyAddress.fromBase58(TESTNET, "n1bQNoEx8uhmCzzA5JPG6sFdtsUQhwiQJV");
|
||||
final Address address = LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "n1bQNoEx8uhmCzzA5JPG6sFdtsUQhwiQJV");
|
||||
assertEquals(address, key1.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET));
|
||||
assertEquals("mnHUcqUVvrfi5kAaXJDQzBb9HsWs78b42R", key2.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET).toString());
|
||||
assertEquals(key1, chain.findKeyFromPubHash(address.getHash()));
|
||||
@ -128,7 +128,7 @@ public class DeterministicKeyChainTest {
|
||||
ECKey key1 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
|
||||
ECKey key2 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
|
||||
|
||||
final Address address = LegacyAddress.fromBase58(TESTNET, "n2nHHRHs7TiZScTuVhZUkzZfTfVgGYwy6X");
|
||||
final Address address = LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "n2nHHRHs7TiZScTuVhZUkzZfTfVgGYwy6X");
|
||||
assertEquals(address, key1.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET));
|
||||
assertEquals("mnp2j9za5zMuz44vNxrJCXXhZsCdh89QXn", key2.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET).toString());
|
||||
assertEquals(key1, chain1.findKeyFromPubHash(address.getHash()));
|
||||
@ -149,7 +149,7 @@ public class DeterministicKeyChainTest {
|
||||
.entropy(ENTROPY, secs).build();
|
||||
ECKey key1 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
|
||||
|
||||
final Address address = LegacyAddress.fromBase58(TESTNET, "n2nHHRHs7TiZScTuVhZUkzZfTfVgGYwy6X");
|
||||
final Address address = LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "n2nHHRHs7TiZScTuVhZUkzZfTfVgGYwy6X");
|
||||
assertEquals(address, key1.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET));
|
||||
|
||||
DeterministicKey watching = chain1.getWatchingKey();
|
||||
|
@ -201,7 +201,7 @@ public class WalletTest extends TestWithWallet {
|
||||
|
||||
@Test
|
||||
public void basicSpendingToP2SH() throws Exception {
|
||||
Address destination = LegacyAddress.fromScriptHash(TESTNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
|
||||
Address destination = LegacyAddress.fromScriptHash(BitcoinNetwork.TESTNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
|
||||
basicSpendingCommon(wallet, myAddress, destination, null);
|
||||
}
|
||||
|
||||
@ -546,7 +546,7 @@ public class WalletTest extends TestWithWallet {
|
||||
List<ScriptChunk> scriptSigChunks = t2.getInput(0).getScriptSig().getChunks();
|
||||
// check 'from address' -- in a unit test this is fine
|
||||
assertEquals(2, scriptSigChunks.size());
|
||||
assertEquals(myAddress, LegacyAddress.fromPubKeyHash(TESTNET, Utils.sha256hash160(scriptSigChunks.get(1).data)));
|
||||
assertEquals(myAddress, LegacyAddress.fromPubKeyHash(BitcoinNetwork.TESTNET, Utils.sha256hash160(scriptSigChunks.get(1).data)));
|
||||
assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType());
|
||||
|
||||
// We have NOT proven that the signature is correct!
|
||||
@ -2121,7 +2121,7 @@ public class WalletTest extends TestWithWallet {
|
||||
Coin v = CENT;
|
||||
// 3100 outputs to a random address.
|
||||
for (int i = 0; i < 3100; i++) {
|
||||
tx.addOutput(v, LegacyAddress.fromPubKeyHash(TESTNET, bits));
|
||||
tx.addOutput(v, LegacyAddress.fromPubKeyHash(BitcoinNetwork.TESTNET, bits));
|
||||
}
|
||||
SendRequest req = SendRequest.forTx(tx);
|
||||
wallet.completeTx(req);
|
||||
|
@ -115,7 +115,7 @@ public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup {
|
||||
Transaction tx1 = FakeTxBuilder.createFakeTx(TESTNET, Coin.COIN, key1);
|
||||
Transaction tx2 = FakeTxBuilder.createFakeTx(TESTNET, Coin.FIFTY_COINS, key2.toAddress(ScriptType.P2PKH,
|
||||
BitcoinNetwork.TESTNET));
|
||||
Block block = FakeTxBuilder.makeSolvedTestBlock(TESTNET.getGenesisBlock(), LegacyAddress.fromBase58(TESTNET, "msg2t2V2sWNd85LccoddtWysBTR8oPnkzW"), tx1, tx2);
|
||||
Block block = FakeTxBuilder.makeSolvedTestBlock(TESTNET.getGenesisBlock(), LegacyAddress.fromBase58(BitcoinNetwork.TESTNET, "msg2t2V2sWNd85LccoddtWysBTR8oPnkzW"), tx1, tx2);
|
||||
BloomFilter filter = new BloomFilter(4, 0.1, 1);
|
||||
filter.insert(key1);
|
||||
filter.insert(key2);
|
||||
|
@ -640,7 +640,7 @@ public class WalletTool implements Callable<Integer> {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Address address = LegacyAddress.fromBase58(params, addrStr);
|
||||
Address address = LegacyAddress.fromBase58(params.network(), addrStr);
|
||||
// If no creation time is specified, assume genesis (zero).
|
||||
wallet.addWatchedAddress(address, getCreationTimeSeconds());
|
||||
} catch (AddressFormatException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user