public class LegacyAddress extends 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.
Modifier and Type | Field and Description |
---|---|
static int |
LENGTH
An address is a RIPEMD160 hash of a public key, therefore is always 160 bits or 20 bytes.
|
boolean |
p2sh
True if P2SH, false if P2PKH.
|
bytes, params
Modifier and Type | Method and Description |
---|---|
LegacyAddress |
clone()
This implementation narrows the return type to
PrefixedChecksummedBytes
and allows subclasses to throw CloneNotSupportedException even though it
is never thrown by this implementation. |
int |
compareTo(Address o)
Comparison field order for addresses is:
NetworkParameters.getId()
Legacy vs. |
boolean |
equals(java.lang.Object o) |
static LegacyAddress |
fromBase58(NetworkParameters params,
java.lang.String base58)
Construct a
LegacyAddress from its base58 form. |
static LegacyAddress |
fromKey(NetworkParameters params,
ECKey key)
Construct a
LegacyAddress that represents the public part of the given ECKey . |
static LegacyAddress |
fromPubKeyHash(NetworkParameters params,
byte[] hash160)
Construct a
LegacyAddress that represents the given pubkey hash. |
static LegacyAddress |
fromScriptHash(NetworkParameters params,
byte[] hash160)
Construct a
LegacyAddress that represents the given P2SH script hash. |
byte[] |
getHash()
The (big endian) 20 byte hash that is the core of a Bitcoin address.
|
Script.ScriptType |
getOutputScriptType()
Get the type of output script that will be used for sending to the address.
|
static NetworkParameters |
getParametersFromAddress(java.lang.String address)
Given an address, examines the version byte and attempts to find a matching NetworkParameters.
|
int |
getVersion()
Get the version header of an address.
|
int |
hashCode() |
java.lang.String |
toBase58()
Returns the base58-encoded textual form, including version and checksum bytes.
|
java.lang.String |
toString() |
compareAddressPartial, fromKey, fromString
getParameters
public static final int LENGTH
public final boolean p2sh
public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash160) throws AddressFormatException
LegacyAddress
that represents the given pubkey hash. The resulting address will be a P2PKH type of
address.params
- network this address is valid forhash160
- 20-byte pubkey hashAddressFormatException
public static LegacyAddress fromKey(NetworkParameters params, ECKey key)
LegacyAddress
that represents the public part of the given ECKey
. Note that an address is
derived from a hash of the public key and is not the public key itself.params
- network this address is valid forkey
- only the public part is usedpublic static LegacyAddress fromScriptHash(NetworkParameters params, byte[] hash160) throws AddressFormatException
LegacyAddress
that represents the given P2SH script hash.params
- network this address is valid forhash160
- P2SH script hashAddressFormatException
public static LegacyAddress fromBase58(@Nullable NetworkParameters params, java.lang.String base58) throws AddressFormatException, AddressFormatException.WrongNetwork
LegacyAddress
from its base58 form.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 addressAddressFormatException
- 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)public int getVersion()
public java.lang.String toBase58()
public byte[] getHash()
public Script.ScriptType getOutputScriptType()
Script.ScriptType.P2PKH
or Script.ScriptType.P2SH
.getOutputScriptType
in class Address
public static NetworkParameters getParametersFromAddress(java.lang.String address) throws AddressFormatException
AddressFormatException
- if the given base58 doesn't parse or the checksum is invalidpublic boolean equals(java.lang.Object o)
equals
in class PrefixedChecksummedBytes
public int hashCode()
hashCode
in class PrefixedChecksummedBytes
public java.lang.String toString()
toString
in class java.lang.Object
public LegacyAddress clone() throws java.lang.CloneNotSupportedException
PrefixedChecksummedBytes
PrefixedChecksummedBytes
and allows subclasses to throw CloneNotSupportedException
even though it
is never thrown by this implementation.clone
in class PrefixedChecksummedBytes
java.lang.CloneNotSupportedException
public int compareTo(Address o)
NetworkParameters.getId()
bytes
Implementations may use compareAddressPartial
for tests 1 and 2.