com.google.bitcoin.core
Class Address

java.lang.Object
  extended by com.google.bitcoin.core.VersionedChecksummedBytes
      extended by com.google.bitcoin.core.Address

public class Address
extends VersionedChecksummedBytes

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 Summary
static int LENGTH
          An address is a RIPEMD160 hash of a public key, therefore is always 160 bits or 20 bytes.
 
Fields inherited from class com.google.bitcoin.core.VersionedChecksummedBytes
bytes, version
 
Constructor Summary
Address(NetworkParameters params, byte[] hash160)
          Construct an address from parameters and the hash160 form.
Address(NetworkParameters params, int version, byte[] hash160)
          Construct an address from parameters, the address version, and the hash160 form.
Address(NetworkParameters params, String address)
          Construct an address from parameters and the standard "human readable" form.
 
Method Summary
static Address fromP2SHHash(NetworkParameters params, byte[] hash160)
          Returns an Address that represents the given P2SH script hash.
static Address fromP2SHScript(NetworkParameters params, Script scriptPubKey)
          Returns an Address that represents the script hash extracted from the given scriptPubKey
 byte[] getHash160()
          The (big endian) 20 byte hash that is the core of a Bitcoin address.
 NetworkParameters getParameters()
          Examines the version byte of the address and attempts to find a matching NetworkParameters.
static NetworkParameters getParametersFromAddress(String address)
          Given an address, examines the version byte and attempts to find a matching NetworkParameters.
 boolean isP2SHAddress()
           
 
Methods inherited from class com.google.bitcoin.core.VersionedChecksummedBytes
equals, getVersion, hashCode, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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
Constructor Detail

Address

public Address(NetworkParameters params,
               int version,
               byte[] hash160)
        throws WrongNetworkException
Construct an address from parameters, the address version, and the hash160 form. Example:

new Address(NetworkParameters.prodNet(), NetworkParameters.getAddressHeader(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));

Throws:
WrongNetworkException

Address

public Address(NetworkParameters params,
               byte[] hash160)
Construct an address from parameters and the hash160 form. Example:

new Address(NetworkParameters.prodNet(), Hex.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));


Address

public Address(@Nullable
               NetworkParameters params,
               String address)
        throws AddressFormatException
Construct an address from parameters and the standard "human readable" form. Example:

new Address(NetworkParameters.prodNet(), "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");

Parameters:
params - The expected NetworkParameters or null if you don't want validation.
address - The textual form of the address, such as "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL"
Throws:
AddressFormatException - if the given address doesn't parse or the checksum is invalid
WrongNetworkException - if the given address is valid but for a different chain (eg testnet vs prodnet)
Method Detail

fromP2SHHash

public static Address fromP2SHHash(NetworkParameters params,
                                   byte[] hash160)
Returns an Address that represents the given P2SH script hash.


fromP2SHScript

public static Address fromP2SHScript(NetworkParameters params,
                                     Script scriptPubKey)
Returns an Address that represents the script hash extracted from the given scriptPubKey


getHash160

public byte[] getHash160()
The (big endian) 20 byte hash that is the core of a Bitcoin address.


isP2SHAddress

public boolean isP2SHAddress()

getParameters

@Nullable
public NetworkParameters getParameters()
Examines the version byte of the address 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. You should be able to handle a null response from this method. Note that the parameters returned is not necessarily the same as the one the Address was created with.

Returns:
a NetworkParameters representing the network the address is intended for, or null if unknown.

getParametersFromAddress

@Nullable
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:
a NetworkParameters or null if the string wasn't of a known version.
Throws:
AddressFormatException


Copyright © 2014. All rights reserved.