Class LegacyAddress
- java.lang.Object
-
- org.bitcoinj.base.LegacyAddress
-
public class LegacyAddress extends java.lang.Object implements Address
A Bitcoin address looks like 1MsScoe2fTJoq4ZPdQgqyhgWeoNamYPevy and is derived from an elliptic curve public key plus a set of network parameters. Not to be confused with a
PeerAddress
orAddressMessage
which are about network (TCP) addresses.A standard address is built by taking the RIPE-MD160 hash of the public key bytes, with a version prefix and a checksum suffix, then encoding it textually as base58. The version prefix is used to both denote the network for which the address is valid (see
NetworkParameters
, and also to indicate how the bytes inside the address should be interpreted. Whilst almost all addresses today are hashes of public keys, another (currently unsupported type) can contain a hash of a script instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LegacyAddress.AddressHeader
Address header of legacy P2PKH addresses for standard Bitcoin networks.static class
LegacyAddress.P2SHHeader
Address header of legacy P2SH addresses for standard Bitcoin networks.
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
bytes
static int
LENGTH
An address is a RIPEMD160 hash of a public key, therefore is always 160 bits or 20 bytes.protected Network
network
boolean
p2sh
True if P2SH, false if P2PKH.-
Fields inherited from interface org.bitcoinj.base.Address
PARTIAL_ADDRESS_COMPARATOR
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
compareTo(Address o)
Comparison field order for addresses is:Network.id()
Legacy vs.boolean
equals(java.lang.Object o)
static LegacyAddress
fromBase58(java.lang.String base58, Network network)
Construct aLegacyAddress
from its base58 form.static LegacyAddress
fromBase58(NetworkParameters params, java.lang.String base58)
Deprecated.static LegacyAddress
fromKey(NetworkParameters params, ECKey key)
Deprecated.static LegacyAddress
fromPubKeyHash(Network network, byte[] hash160)
Construct aLegacyAddress
that represents the given pubkey hash.static LegacyAddress
fromPubKeyHash(NetworkParameters params, byte[] hash160)
Deprecated.static LegacyAddress
fromScriptHash(Network network, byte[] hash160)
Construct aLegacyAddress
that represents the given P2SH script hash.static LegacyAddress
fromScriptHash(NetworkParameters params, byte[] hash160)
Deprecated.byte[]
getHash()
The (big endian) 20 byte hash that is the core of a Bitcoin address.ScriptType
getOutputScriptType()
Get the type of output script that will be used for sending to the address.static NetworkParameters
getParametersFromAddress(java.lang.String address)
Deprecated.int
getVersion()
Get the version header of an address.int
hashCode()
Network
network()
Get the network this address works on.java.lang.String
toBase58()
Returns the base58-encoded textual form, including version and checksum bytes.java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.bitcoinj.base.Address
getParameters
-
-
-
-
Field Detail
-
LENGTH
public static final int LENGTH
An address is a RIPEMD160 hash of a public key, therefore is always 160 bits or 20 bytes.- See Also:
- Constant Field Values
-
network
protected final Network network
-
bytes
protected final byte[] bytes
-
p2sh
public final boolean p2sh
True if P2SH, false if P2PKH.
-
-
Method Detail
-
fromPubKeyHash
@Deprecated public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash160) throws AddressFormatException
Deprecated.Construct aLegacyAddress
that represents the given pubkey hash. The resulting address will be a P2PKH type of address.- Parameters:
params
- network this address is valid forhash160
- 20-byte pubkey hash- Returns:
- constructed address
- Throws:
AddressFormatException
-
fromPubKeyHash
public static LegacyAddress fromPubKeyHash(Network network, byte[] hash160) throws AddressFormatException
Construct aLegacyAddress
that represents the given pubkey hash. The resulting address will be a P2PKH type of address.- Parameters:
network
- network this address is valid forhash160
- 20-byte pubkey hash- Returns:
- constructed address
- Throws:
AddressFormatException
-
fromKey
@Deprecated public static LegacyAddress fromKey(NetworkParameters params, ECKey key)
Deprecated.Construct aLegacyAddress
that represents the public part of the givenECKey
. Note that an address is derived from a hash of the public key and is not the public key itself.- Parameters:
params
- network this address is valid forkey
- only the public part is used- Returns:
- constructed address
-
fromScriptHash
@Deprecated public static LegacyAddress fromScriptHash(NetworkParameters params, byte[] hash160) throws AddressFormatException
Deprecated.Construct aLegacyAddress
that represents the given P2SH script hash.- Parameters:
params
- network this address is valid forhash160
- P2SH script hash- Returns:
- constructed address
- Throws:
AddressFormatException
-
fromScriptHash
public static LegacyAddress fromScriptHash(Network network, byte[] hash160) throws AddressFormatException
Construct aLegacyAddress
that represents the given P2SH script hash.- Parameters:
network
- network this address is valid forhash160
- P2SH script hash- Returns:
- constructed address
- Throws:
AddressFormatException
-
fromBase58
@Deprecated public static LegacyAddress fromBase58(@Nullable NetworkParameters params, java.lang.String base58) throws AddressFormatException, AddressFormatException.WrongNetwork
Deprecated.Construct aLegacyAddress
from its base58 form.- Parameters:
params
- expected network this address is valid for, or null if the network should be derived from the base58base58
- base58-encoded textual form of the address- Throws:
AddressFormatException
- if the given base58 doesn't parse or the checksum is invalidAddressFormatException.WrongNetwork
- if the given address is valid but for a different chain (e.g. testnet vs mainnet)
-
fromBase58
public static LegacyAddress fromBase58(java.lang.String base58, @Nonnull Network network) throws AddressFormatException, AddressFormatException.WrongNetwork
Construct aLegacyAddress
from its base58 form.- Parameters:
base58
- base58-encoded textual form of the addressnetwork
- expected network this address is valid for- Throws:
AddressFormatException
- if the given base58 doesn't parse or the checksum is invalidAddressFormatException.WrongNetwork
- if the given address is valid but for a different chain (e.g. testnet vs mainnet)
-
network
public Network network()
Get the network this address works on. Use ofBitcoinNetwork
is preferred to use ofNetworkParameters
when you need to know what network an address is for.
-
getVersion
public int getVersion()
Get the version header of an address. This is the first byte of a base58 encoded address.- Returns:
- version header as one byte
-
toBase58
public java.lang.String toBase58()
Returns the base58-encoded textual form, including version and checksum bytes.- Returns:
- textual form
-
getHash
public byte[] getHash()
The (big endian) 20 byte hash that is the core of a Bitcoin address.
-
getOutputScriptType
public ScriptType getOutputScriptType()
Get the type of output script that will be used for sending to the address. This is eitherScriptType.P2PKH
orScriptType.P2SH
.- Specified by:
getOutputScriptType
in interfaceAddress
- Returns:
- type of output script
-
getParametersFromAddress
@Deprecated public static NetworkParameters getParametersFromAddress(java.lang.String address) throws AddressFormatException
Deprecated.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.- Returns:
- network the address is valid for
- Throws:
AddressFormatException
- if the given base58 doesn't parse or the checksum is invalid
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
compareTo
public int compareTo(Address o)
Comparison field order for addresses is:Network.id()
- Legacy vs. Segwit
- (Legacy only) Version byte
- remaining
bytes
Implementations use
Address.PARTIAL_ADDRESS_COMPARATOR
for tests 1 and 2.
-
-