Class LegacyAddress
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparable<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
or AddressMessage
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.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
An address is a RIPEMD160 hash of a public key, therefore is always 160 bits or 20 bytes.final boolean
True if P2SH, false if P2PKH.Fields inherited from class org.bitcoinj.core.PrefixedChecksummedBytes
bytes, params
-
Method Summary
Modifier and TypeMethodDescriptionclone()
This implementation narrows the return type toPrefixedChecksummedBytes
and allows subclasses to throwCloneNotSupportedException
even though it is never thrown by this implementation.int
Comparison field order for addresses is:NetworkParameters.getId()
Legacy vs.boolean
static LegacyAddress
fromBase58
(NetworkParameters params, String base58) Construct aLegacyAddress
from its base58 form.static LegacyAddress
fromKey
(NetworkParameters params, ECKey key) Construct aLegacyAddress
that represents the public part of the givenECKey
.static LegacyAddress
fromPubKeyHash
(NetworkParameters params, byte[] hash160) Construct aLegacyAddress
that represents the given pubkey hash.static LegacyAddress
fromScriptHash
(NetworkParameters params, byte[] hash160) Construct aLegacyAddress
that represents the given P2SH script hash.byte[]
getHash()
The (big endian) 20 byte hash that is the core of a Bitcoin address.Get the type of output script that will be used for sending to the address.static NetworkParameters
getParametersFromAddress
(String address) Given an address, examines the version byte and attempts to find a matching NetworkParameters.int
Get the version header of an address.int
hashCode()
toBase58()
Returns the base58-encoded textual form, including version and checksum bytes.toString()
Methods inherited from class org.bitcoinj.core.Address
compareAddressPartial, fromKey, fromString
Methods inherited from class org.bitcoinj.core.PrefixedChecksummedBytes
getParameters
-
Field Details
-
LENGTH
public static final int LENGTHAn address is a RIPEMD160 hash of a public key, therefore is always 160 bits or 20 bytes.- See Also:
-
p2sh
public final boolean p2shTrue if P2SH, false if P2PKH.
-
-
Method Details
-
fromPubKeyHash
public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash160) throws AddressFormatException 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
-
fromKey
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
public static LegacyAddress fromScriptHash(NetworkParameters params, byte[] hash160) throws AddressFormatException 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
-
fromBase58
public static LegacyAddress fromBase58(@Nullable NetworkParameters params, String base58) throws AddressFormatException, AddressFormatException.WrongNetwork Construct aLegacyAddress
from its base58 form.- Parameters:
params
- expected network this address is valid for, or null if 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 (eg testnet vs mainnet)
-
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
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
Get the type of output script that will be used for sending to the address. This is eitherScript.ScriptType.P2PKH
orScript.ScriptType.P2SH
.- Specified by:
getOutputScriptType
in classAddress
- Returns:
- type of output script
-
getParametersFromAddress
public static NetworkParameters getParametersFromAddress(String address) throws AddressFormatException 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
- Overrides:
equals
in classPrefixedChecksummedBytes
-
hashCode
public int hashCode()- Overrides:
hashCode
in classPrefixedChecksummedBytes
-
toString
-
clone
Description copied from class:PrefixedChecksummedBytes
This implementation narrows the return type toPrefixedChecksummedBytes
and allows subclasses to throwCloneNotSupportedException
even though it is never thrown by this implementation.- Overrides:
clone
in classPrefixedChecksummedBytes
- Throws:
CloneNotSupportedException
-
compareTo
Comparison field order for addresses is:NetworkParameters.getId()
- Legacy vs. Segwit
- (Legacy only) Version byte
- remaining
bytes
Implementations may use
compareAddressPartial
for tests 1 and 2.- Specified by:
compareTo
in interfaceComparable<Address>
- Specified by:
compareTo
in classAddress
- Parameters:
o
- otherAddress
object- Returns:
- comparison result
-