-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
114 lines (87 loc) · 3.6 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
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
const events = require('events');
const util = require('util');
//this is required because the prohashing certificate doesn't get a long well with node.js
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const prohashing = function (config) {
const self = this;
self.config = config;
self.wampConnection = false;
self.wampUser = config.wampUser || 'web';
self.wampPassword = config.wampPassword || 'web';
self.wampSession = false;
self.subscribe = config.subscribe || 'all';
if (config.debug) AUTOBAHN_DEBUG = true;
const autobahn = require('autobahn');
self.onChallenge = (session, method, extra) => {
self.wampSession = session;
if (method === 'wampcra')
return autobahn.auth_cra.sign(self.wampPassword, extra.challenge);
};
self.initialSessionUpdatesReceived = (updates) => {
self.onMinerUpdate(updates);
self.wampSession.subscribe(`miner_update_diffs_${self.config.apiKey}`, self.onMinerUpdate);
};
self.initialBalanceUpdatesReceived = (updates) => {
self.onBalanceUpdate(updates);
self.wampSession.subscribe(`balance_updates_${self.config.apiKey}`, self.onBalanceUpdate);
};
self.initialProfitabilityUpdatesReceived = (updates) => {
self.onProfitabilityUpdate(updates);
self.wampSession.subscribe('profitability_updates', self.onProfitabilityUpdate);
};
self.initialSystemStatusUpdatesReceived = (updates) => {
self.onSystemStatusUpdate(updates);
self.wampSession.subscribe('general_updates', self.onSystemStatusUpdate);
};
self.connectionOpen = (session, details) => {
if (self.subscribe.indexOf('miners') > -1 || self.subscribe === "all")
session.call('f_all_miner_updates', [self.config.apiKey]).then(self.initialSessionUpdatesReceived);
if (self.subscribe.indexOf('profitability') > -1 || self.subscribe === "all")
session.call('f_all_profitability_updates').then(self.initialProfitabilityUpdatesReceived);
if (self.subscribe.indexOf('systemStatus') > -1 || self.subscribe === "all")
session.call('f_all_general_updates').then(self.initialSystemStatusUpdatesReceived);
if (self.subscribe.indexOf('balanceStatus') > -1 || self.subscribe === "all")
session.call('f_all_balance_updates').then(self.initialBalanceUpdatesReceived);
if (self.subscribe.indexOf('blocks') > -1 || self.subscribe === "all")
session.subscribe('found_block_updates', self.onBlockUpdate);
if(self.subscribe.indexOf('miningFailures') > -1 || self.subscribe === "all")
session.subscribe(`mining_failures_${self.config.apiKey}`, self.onMiningFailure);
if(self.config.debug) console.log("Prohashing: Connected");
self.emit("connected", session, details);
};
self.onSessionUpdate = (update) => {
self.emit('sessionUpdate', update);
};
self.onBlockUpdate = (block) => {
self.emit("block", block);
};
self.onMinerUpdate = (update) => {
self.emit("minerStatus", update);
};
self.onBalanceUpdate = (update) => {
self.emit("balanceStatus", update);
};
self.onProfitabilityUpdate = (update) => {
self.emit("profitability", update);
};
self.onSystemStatusUpdate = (update) => {
self.emit("systemStatus", update);
};
self.onMiningFailure = (update) => {
self.emit('miningFailure', update);
};
const connDetails = {
url: self.config.url || 'wss://live.prohashing.com:443/ws',
realm: 'mining',
authmethods: ['wampcra'],
authid: self.wampUser,
onchallenge: self.onChallenge,
};
self.wampConnection = new autobahn.Connection(connDetails);
self.wampConnection.onopen = self.connectionOpen;
if(self.config.debug) console.log("Prohashing: Open Connection");
self.wampConnection.open();
return this;
};
util.inherits(prohashing, events.EventEmitter);
module.exports = prohashing;