Class LegacyAddress

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<Address>

    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.

    See Also:
    Serialized Form
    • 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
      • p2sh

        public final boolean p2sh
        True if P2SH, false if P2PKH.
    • Method Detail

      • fromKey

        public static LegacyAddress fromKey​(NetworkParameters params,
                                            ECKey key)
        Construct a 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.
        Parameters:
        params - network this address is valid for
        key - only the public part is used
        Returns:
        constructed address
      • 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.
        Specified by:
        getHash in class Address
        Returns:
        hash that is encoded in the address
      • getParametersFromAddress

        public static NetworkParameters getParametersFromAddress​(java.lang.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
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • compareTo

        public int compareTo​(Address o)
        Comparison field order for addresses is:
        1. NetworkParameters.getId()
        2. Legacy vs. Segwit
        3. (Legacy only) Version byte
        4. remaining bytes

        Implementations may use compareAddressPartial for tests 1 and 2.

        Specified by:
        compareTo in interface java.lang.Comparable<Address>
        Specified by:
        compareTo in class Address
        Parameters:
        o - other Address object
        Returns:
        comparison result