-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrypt_listener.js
114 lines (82 loc) · 1.98 KB
/
scrypt_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
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) {
handleTransaction(tx)
}
};
listener.on("mempool_tx", async ({ transaction, size }) => {
handleTransaction(transaction)
})
async function handleTransaction(transaction) {
// detect the smart contract instance here
//
//
const hex = tx.toHex()
}
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 });