forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-to-peer.js
50 lines (39 loc) · 991 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
48
49
50
'use strict';
/*
* Usage:
* Run another Bitcoin node on local regtest network, for example
* $ ../../bin/bcoin --network=regtest
* Execute this script with the other node's address and port
* $ node connect-to-peer.js 127.0.0.1:48444
*/
const bcoin = require('../..');
const network = bcoin.Network.get('regtest');
const peer = bcoin.Peer.fromOptions({
network: 'regtest',
agent: 'my-subversion',
hasWitness: () => {
return false;
}
});
const addr = bcoin.net.NetAddress.fromHostname(process.argv[2], 'regtest');
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]);
});