forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 5
/
PeerManager.js
47 lines (36 loc) · 1009 Bytes
/
PeerManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore');
var Peer = bitcore.Peer;
var PeerManager = bitcore.PeerManager;
var handleBlock = function(info) {
console.log('** Block Received **');
console.log(info.message);
};
var handleTx = function(info) {
var tx = info.message.tx.getStandardizedObject();
console.log('** TX Received **');
console.log(tx);
};
var handleInv = function(info) {
console.log('** Inv **');
console.log(info.message);
var invs = info.message.invs;
info.conn.sendGetData(invs);
};
var peerman = new PeerManager({
network: 'testnet'
});
peerman.addPeer(new Peer('127.0.0.1', 18333));
peerman.on('connection', function(conn) {
conn.on('inv', handleInv);
conn.on('block', handleBlock);
conn.on('tx', handleTx);
});
peerman.start();
};
module.exports.run = run;
if (require.main === module) {
run();
}