-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
53 lines (44 loc) · 1.48 KB
/
index.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
51
52
53
const { Networks } = require('bitcore-lib');
const { Peer, Pool } = require('bitcore-p2p');
const contrib = require('blessed-contrib');
const blessed = require('blessed');
const screen = blessed.screen();
const grid = new contrib.grid({rows: 3, cols: 1, screen: screen});
const transactionsSet = new Set();
const peersLog = grid.set(2, 0, 1, 1, contrib.log, {
fg: "yellow",
// selectedFg: "green",
label: 'Peers'
});
const transactionsLog = grid.set(0, 0, 2, 1, contrib.log, {
fg: "green",
// selectedFg: "green",
label: 'transactions'
});
screen.key(['escape', 'q', 'C-c'], () => {
pool.disconnect();
process.exit(0);
});
const pool = new Pool({network: Networks.livenet});
pool.on('peerready', peer => {
peersLog.log(`${peer.host}:${peer.port} - ${peer.version} ${peer.subversion}`);
});
pool.on('peerinv', (peer, inv) => {
const transactions = inv.inventory.filter(obj => obj.type === 1);
transactions.forEach(object => {
const txHashStr = object.hash.toString();
if (!transactionsSet.has(txHashStr)) {
transactionsSet.add(txHashStr);
const msg = peer.messages.GetData.forTransaction(object.hash);
peer.sendMessage(msg);
}
});
});
pool.on('peertx', (peer, tx) => {
const transaction = tx.transaction.toObject();
// const formatted = util.inspect(transaction, {depth: 6, colors: true});
const txt = `${transaction.hash}: ${transaction.outputs.map(tx => tx.satoshis).join(', ')}`;
transactionsLog.log(txt);
});
screen.render();
pool.connect();