Peer

Represents a node from the p2p bitcoin network. The Peer class supports connecting directly to other nodes or through a socks5 proxy like Tor.

Creating a peer

The code to create a new peer looks like this:

var Peer = require('bitcore-p2p').Peer;

// default port
var livenetPeer = new Peer({host: '5.9.85.34'});
var testnetPeer = new Peer({host: '5.9.85.34', network: Networks.testnet});

// custom port
var livenetPeer = new Peer({host: '5.9.85.34', port: 8334});
var testnetPeer = new Peer({host: '5.9.85.34', port: 18334, network: Networks.testnet});

// use sock5 proxy (Tor)
var peer = new Peer({host: '5.9.85.34'}).setProxy('localhost', 9050);

States

A peer instance is always in one of the following states:

  • disconnected: No connection with the remote node.
  • connecting: While establishing the connection.
  • connected: Exchanging version packages.
  • ready: Connection ready for sending and receiving messages.

You can subscribe to the change of those states as follows:

var Peer = require('bitcore-p2p').Peer;

var peer = new Peer({host: '5.9.85.34'});

peer.on('ready', function() {
  // peer info
  console.log(peer.version, peer.subversion, peer.bestHeight);
});

peer.on('disconnect', function() {
  console.log('connection closed');
});

peer.connect();

Handle messages

Once connected, a peer instance can send and receive messages. Every time a message arrives it's emitted as a new event. Let's see an example of this:

var Peer = require('bitcore-p2p').Peer;
var peer = new Peer({host: '5.9.85.34'});

// handle events
peer.on('inv', function(message) {
  // message.inventory[]
});

peer.on('tx', function(message) {
  // message.transaction
});

peer.on('addr', function(message) {
  // message.addresses[]
});

peer.connect();

Sending messages

In order to send messages the Peer class offers the sendMessage(message) method, which receives an instance of a message. All supported messages can be found in the Messages module. For more information about messages refer to the protocol specification.

An example for requesting other connected nodes to a peers looks like this:

var p2p = require('bitcore-p2p')
var Peer = p2p.Peer;
var Messages = p2p.Messages;
var peer = new Peer({host: '5.9.85.34'});

peer.on('ready', function() {
  var message = new Messages.GetAddresses();
  peer.sendMessage(message);
});

peer.on('addr', function(message) {
  message.addresses.forEach(function(address) {
    // do something
  });
});

peer.connect();

Peer

Kind: global class

new Peer(options)

The Peer constructor will create an instance of Peer to send and receive messages using the standard Bitcoin protocol. A Peer instance represents one connection on the Bitcoin network. To create a new peer connection provide the host and port options and then invoke the connect method. Additionally, a newly connected socket can be provided instead of host and port.

Returns: Peer - A new instance of Peer.

Param Type Description
options Object
options.host String IP address of the remote host
options.port Number Port number of the remote host
options.network Network The network configuration
[options.relay] Boolean An option to disable automatic inventory relaying from the remote peer
[options.socket] Socket An existing connected socket

Example


var peer = new Peer({host: '127.0.0.1'}).setProxy('127.0.0.1', 9050);
peer.on('tx', function(tx) {
 console.log('New transaction: ', tx.id);
});
peer.connect();

peer.setProxy(host, port) ⇒ Peer

Set a socks5 proxy for the connection. Enables the use of the TOR network.

Kind: instance method of Peer
Returns: Peer - The same Peer instance.

Param Type Description
host String IP address of the proxy
port Number Port number of the proxy

peer.connect() ⇒ Peer

Init the connection with the remote peer.

Kind: instance method of Peer
Returns: Peer - The same peer instance.

peer.disconnect() ⇒ Peer

Disconnects the remote connection.

Kind: instance method of Peer
Returns: Peer - The same peer instance.

peer.sendMessage(message)

Send a Message to the remote peer.

Kind: instance method of Peer

Param Type Description
message Message A message instance

peer._sendVersion()

Internal function that sends VERSION message to the remote peer.

Kind: instance method of Peer

peer._sendPong()

Send a PONG message to the remote peer.

Kind: instance method of Peer

peer._readMessage()

Internal function that tries to read a message from the data buffer

Kind: instance method of Peer

peer._getSocket() ⇒ Socket

Internal function that creates a socket using a proxy if necessary.

Kind: instance method of Peer
Returns: Socket - A Socket instance not yet connected.