Class LegacyAddress

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

    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 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.

    • 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

        public static LegacyAddress fromPubKeyHash​(Network network,
                                                   byte[] hash160)
                                            throws AddressFormatException
        Construct a LegacyAddress that represents the given pubkey hash. The resulting address will be a P2PKH type of address.
        Parameters:
        network - network this address is valid for
        hash160 - 20-byte pubkey hash
        Returns:
        constructed address
        Throws:
        AddressFormatException
      • fromKey

        @Deprecated
        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 interface Address
        Returns:
        hash that is encoded in the address
      • 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 class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • 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. Network.id()
        2. Legacy vs. Segwit
        3. (Legacy only) Version byte
        4. remaining bytes

        Implementations use Address.PARTIAL_ADDRESS_COMPARATOR for tests 1 and 2.

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