-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplies_listener.js
144 lines (102 loc) · 2.59 KB
/
replies_listener.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require('dotenv').config()
const { Listener } = require("bsv-spv");
const name = "bitchat-listener-2";
const ticker = "BSV";
const blockHeight = -10; // Number. If negative then it's number from the tip.
const dataDir = __dirname;
const port = 8080; // Same as Masters port above
const listener = new Listener({ name, ticker, blockHeight, dataDir });
const { connect } = require('amqplib')
var amqp;
async function startAmqp() {
const connection = await connect(process.env.amqp_url)
amqp = await connection.createChannel()
await amqp.assertExchange('powco')
}
startAmqp()
const { TransformTx, bobFromRawTx } = require('bmapjs')
/** Example
*
*
* {
tx: {
h: 'f9fc39e00bdfb26762d6d70ffe4fdba4841dec83c65bc06a9f9a7cbaa1c5b49e'
},
in: [ { i: 0, e: [Object], seq: 4294967295 } ],
B: [
{
content: 'Success! I am detecting bitchat transactions in real time using bsv-spv :D',
'content-type': 'text/plain',
encoding: 'utf-8'
}
],
MAP: [
{
cmd: 'SET',
app: 'chat.pow.co',
type: 'message',
paymail: '[email protected]',
context: 'channel',
channel: 'test'
}
],
lock: 0
}
****/
async function parseTransaction(tx) {
const hex = tx.toHex()
const bob = await bobFromRawTx(hex)
let transformed = await TransformTx(bob)
return [hex, bob, transformed]
}
const onBlock = ({
header,
started,
finished,
size,
height,
txCount,
transactions,
startDate,
}) => {
for (const [index, tx, pos, len] of transactions) {
console.log(`#${index} tx ${tx.getTxid()} in block ${height}`);
handleTransaction(tx)
}
};
listener.on("mempool_tx", async ({ transaction, size }) => {
handleTransaction(transaction)
})
async function handleTransaction(transaction) {
try {
const [hex, bob, bmap] = await parseTransaction(transaction)
console.log(bmap, 'bmap.transaction.discovered')
amqp.publish('powco', 'bmap.transaction.discovered', Buffer.from(
JSON.stringify({ bmap })
))
if (bmap['B']) {
//console.log(transformed, 'B')
}
if (bmap['MAP']) {
//console.log(transformed, 'MAP')
if (bmap['MAP'][0].cmd == 'SET' &&
bmap['MAP'][0].type == 'message') {
console.log(bmap, 'Bitchat Detected')
amqp.publish('proofofwork',
'bmap.bitchat.transaction.detected',
Buffer.from(
JSON.stringify({ bmap })
))
}
}
} catch(error) {
}
}
listener.on("block_reorg", ({ height, hash }) => {
// Re-org after height
});
listener.on("block_saved", ({ height, hash }) => {
listener.syncBlocks(onBlock);
});
listener.syncBlocks(onBlock);
listener.connect({ port });