bitpay/bitcore — pull request #696 by yemel (2014-12-10 14:54:51+00:00)
Title: WIP: P2P Peer Connections

Adds `bitcore.transport.Peer` and `bitcore.transport.Messages` allowing p2p connections with the bitcoin network.

Peer creation:
```javascript
var bitcore = require('bitcore');
var Peer = bitcore.transport.Peer;

// default port
var livenetPeer = new Peer('5.9.85.34');
var testnetPeer = new Peer('5.9.85.34', bitcore.testnet);

// custom port
var livenetPeer = new Peer('5.9.85.34', 8334);
var testnetPeer = new Peer('5.9.85.34', 18334, bitcore.testnet);

// use sock5 proxy (Tor)
var peer = new Peer('5.9.85.34', 8334).setProxy('localhost', 9050);
```

The interface works as follows:
```javascript
var peer = new bitcore.transport.Peer('5.9.85.34');

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

var message = new bitcore.transport.Messages.GetAddresses();
peer.sendMessage(message);
});

// 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();
```

# To do:
- [ ] Add tests to messages.js
- [ ] Write docs