This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbitcoind-zeromq.js
151 lines (116 loc) · 3.43 KB
/
bitcoind-zeromq.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
145
146
147
148
149
150
151
'use strict';
var _ = require('underscore');
var bitcoin = require('bitcoinjs-lib');
var BigNumber = require('bignumber.js');
var EventEmitter = require('events').EventEmitter || require('events');
var request = require('request');
var zmq = require('zeromq');
var BitcoindZeroMQ = module.exports = function(options) {
this.options = _.defaults({}, options || {
network: 'bitcoin',
statusPollInterval: 10 * 1000,
statusPollTimeout: 4000,
});
this.closed = false;
_.bindAll(this, 'onMessage');
var sock = this.sock = zmq.socket('sub');
if (this.options.dataUrl) {
sock.connect(this.options.dataUrl);
sock.subscribe('rawtx');
sock.on('message', this.onMessage);
}
if (this.options.statusUrl) {
this.initializeStatusCheck();
}
};
// Provide event emitter methods.
_.extend(BitcoindZeroMQ.prototype, EventEmitter.prototype);
// Network constants for bitcoinjs-lib.
BitcoindZeroMQ.prototype.networks = {
bitcoin: bitcoin.networks.bitcoin,
bitcoinTestnet: bitcoin.networks.testnet,
litecoin: {
bech32: 'ltc',
pubKeyHash: 48,
scriptHash: 50,
wif: 128,
},
};
BitcoindZeroMQ.prototype.onMessage = function(topic, message) {
if (topic.toString() === 'rawtx') {
var rawTx = message.toString('hex');
var outputs = this.getOutputsFromRawTx(rawTx, this.options.network);
var isReplaceable = this.txIsReplaceable(rawTx);
_.each(outputs, function(output) {
var txOutput = _.extend({},
output,
{
isReplaceable: isReplaceable,
}
)
this.emit('tx', txOutput);
}, this);
}
};
BitcoindZeroMQ.prototype.txIsReplaceable = function(rawTx) {
var tx = this.decodeRawTx(rawTx);
// The minimum value is 0 and the maximum value is (2^32 - 1)
// Any value of sequence that is less than the maximum, can be replaced.
// NOTE: 2^32 - 2 is also considered non-RBF.
// This is the max sequence value used by Electrum.
var maxSequence = BigNumber('2').pow('32').minus('2');
return _.some(tx.ins, function(input) {
var sequence = new BigNumber(input.sequence);
return sequence.lt(maxSequence);
});
};
BitcoindZeroMQ.prototype.getOutputsFromRawTx = function(rawTx, networkName) {
var tx = this.decodeRawTx(rawTx);
if (!tx) return [];
var txid = tx.getId();
var network = this.networks[networkName || 'bitcoin'];
return _.chain(tx.outs).map(function(out) {
try {
var address = bitcoin.address.fromOutputScript(out.script, network);
} catch (error) {
return null;
}
return {
amount: out.value,
address: address,
txid: txid,
};
}).compact().value();
};
BitcoindZeroMQ.prototype.decodeRawTx = function(rawTx) {
try {
var tx = bitcoin.Transaction.fromHex(rawTx);
} catch (error) {
return null;
}
return tx;
};
BitcoindZeroMQ.prototype.close = function() {
// Remove all event listeners.
this.removeAllListeners();
clearTimeout(this.statusTimer);
if (this.sock) {
this.sock.close();
this.sock = null;
}
this.closed = true;
};
BitcoindZeroMQ.prototype.initializeStatusCheck = function() {
var initializeStatusCheck = _.bind(this.initializeStatusCheck, this);
var uri = this.options.statusUrl;
var onResponse = _.bind(function(error, response, data) {
if (error) {
console.error('Failed to connect to', uri, error);
}
if (!this.closed) {
this.active = response && response.statusCode === 200;
this.statusTimer = setTimeout(initializeStatusCheck, this.options.statusPollInterval);
}
}, this);
request(uri, { timeout: this.options.statusPollTimeout }, onResponse);
};