forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
connect-to-peer.js
47 lines (36 loc) · 889 Bytes
/
connect-to-peer.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';
// Usage: $ node ./docs/Examples/connect-to-peer.js [ip]:[port]
const bcoin = require('../..');
const Peer = bcoin.peer;
const NetAddress = bcoin.netaddress;
const Network = bcoin.network;
const network = Network.get('testnet');
const peer = Peer.fromOptions({
network: 'testnet',
agent: 'my-subversion',
hasWitness: () => {
return false;
}
});
const addr = NetAddress.fromHostname(process.argv[2], 'testnet');
console.log(`Connecting to ${addr.hostname}`);
peer.connect(addr);
peer.tryOpen();
peer.on('error', (err) => {
console.error(err);
});
peer.on('packet', (msg) => {
console.log(msg);
if (msg.cmd === 'block') {
console.log('Block!');
console.log(msg.block.toBlock());
return;
}
if (msg.cmd === 'inv') {
peer.getData(msg.items);
return;
}
});
peer.on('open', () => {
peer.getBlock([network.genesis.hash]);
});