Class PeerAddress

java.lang.Object
org.bitcoinj.core.PeerAddress

public class PeerAddress extends Object
A PeerAddress holds an IP address and port number representing the network location of a peer in the Bitcoin P2P network. It exists primarily for serialization purposes.

Instances of this class are not safe for use by multiple threads.

  • Method Details

    • simple

      public static PeerAddress simple(InetAddress addr, int port)
      Constructs a simple peer address from the given IP address and port, but without services. The time is set to current time.
      Parameters:
      addr - ip address of peer
      port - port the peer is listening on
      Returns:
      simple peer address
    • simple

      public static PeerAddress simple(InetSocketAddress addr)
      Constructs a simple peer address from the given socket address, but without services. The time is set to current time.
      Parameters:
      addr - ip address and port of peer
      Returns:
      simple peer address
    • inet

      public static PeerAddress inet(InetAddress addr, int port, Services services, Instant time)
      Constructs a peer address from the given IP address, port, services and time. Such addresses are used for `addr` and `addrv2` messages of types IPv4 and IPv6.
      Parameters:
      addr - ip address of the peer
      port - port the peer is listening on
      services - node services the peer is providing
      time - last-seen time of the peer
    • inet

      public static PeerAddress inet(InetSocketAddress addr, Services services, Instant time)
      Constructs a peer address from the given IP address, port, services and time. Such addresses are used for `addr` and `addrv2` messages of types IPv4 and IPv6.
      Parameters:
      addr - ip address and port of the peer
      services - node services the peer is providing
      time - last-seen time of the peer
    • read

      public static PeerAddress read(ByteBuffer payload, int protocolVariant) throws BufferUnderflowException, ProtocolException
      Deserialize this peer address from a given payload, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message).
      Parameters:
      payload - payload to deserialize from
      protocolVariant - variant of protocol to use for parsing
      Returns:
      read message
      Throws:
      BufferUnderflowException - if the read message extends beyond the remaining bytes of the payload
      ProtocolException
    • localhost

      public static PeerAddress localhost(NetworkParameters params)
    • write

      public ByteBuffer write(ByteBuffer buf, int protocolVariant) throws BufferOverflowException
      Write this peer address into the given buffer, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message)..
      Parameters:
      buf - buffer to write into
      protocolVariant - variant of protocol used
      Returns:
      the buffer
      Throws:
      BufferOverflowException - if the peer addressdoesn't fit the remaining buffer
    • serialize

      public byte[] serialize(int protocolVariant)
      Allocates a byte array and writes this peer address into it, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message).
      Parameters:
      protocolVariant - variant of protocol used
      Returns:
      byte array containing the peer address
    • getMessageSize

      public int getMessageSize(int protocolVariant)
      Return the size of the serialized message, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message).. Note that if the message was deserialized from a payload, this size can differ from the size of the original payload.
      Parameters:
      protocolVariant - variant of protocol used
      Returns:
      size of the serialized message in bytes
    • getByAddress

      public static InetAddress getByAddress(byte[] addrBytes)
    • mapIntoIPv6

      public static byte[] mapIntoIPv6(byte[] ip)
      Map given IPv4 address into IPv6 space.
      Parameters:
      ip - IPv4 to map into IPv6 space
      Returns:
      mapped IP
    • getHostname

      public String getHostname()
    • getAddr

      public InetAddress getAddr()
    • getSocketAddress

      public InetSocketAddress getSocketAddress()
    • getPort

      public int getPort()
    • getServices

      public Services getServices()
    • time

      public Instant time()
      Gets the time that the node was last seen as connected to the network.
      Returns:
      time that the node was last seen
    • getTime

      @Deprecated public long getTime()
      Deprecated.
      use time()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toSocketAddress

      public InetSocketAddress toSocketAddress()