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.
        network - network this address is valid for
        hash160 - 20-byte pubkey hash
        constructed address
      • 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.
        params - network this address is valid for
        key - only the public part is used
        constructed address
      • getVersion

        public int getVersion()
        Get the version header of an address. This is the first byte of a base58 encoded address.
        version header as one byte
      • toBase58

        public java.lang.String toBase58()
        Returns the base58-encoded textual form, including version and checksum bytes.
        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
        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.
        network the address is valid for
        AddressFormatException - if the given base58 doesn't parse or the checksum is invalid
      • equals

        public boolean equals​(java.lang.Object o)
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        hashCode in class java.lang.Object
      • toString

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

        public int compareTo​(Address o)
        Comparison field order for addresses is:
        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>
        o - other Address object
        comparison result