-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnicehash.js
155 lines (127 loc) · 3.71 KB
/
nicehash.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
152
153
154
155
// The code is mostly from https://github.com/dannychua/nicehashjs2
// also it has pieces from https://github.com/bhusalb/nicehash-api
const axios = require('axios')
const buildURL = require('axios/lib/helpers/buildURL')
const crypto = require('crypto')
const API_BASE_URL = 'https://api2.nicehash.com';
const axiosConfig = {
baseURL: API_BASE_URL,
timeout: 1000 * 10,
}
class NiceHashClient {
/**
* Creates a new client
* @param options Object
* @param options.apiKey String - API Key
* @param options.apiSecret String - API Secret
* @param options.organizationId String - Organization Id
*/
constructor(options) {
this.apiKey = options.apiKey;
this.apiSecret = options.apiSecret;
this.organizationId = options.organizationId || '';
this.axios = axios.create(axiosConfig);
}
hasAuthTokens() {
return !!this.apiKey && !!this.apiSecret;
}
getAuthParams() {
return { key: this.apiKey, secret: this.apiSecret };
}
getRandomString() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
getServerTimestamp() {
return Date.now().toString()
}
hmacSha256BySegments(input) {
let signature = crypto.createHmac('sha256', this.apiSecret)
for (let index in input) {
if (+index) {
signature.update(Buffer.from([0]))
}
if (input[index] !== null) {
signature.update(Buffer.from(input[index]))
}
}
return signature.digest('hex')
}
getHeaders(httpMethod, requestPath, params) {
const ts = this.getServerTimestamp()
const query = buildURL('',params).substring(1)
const nonce = this.getRandomString()
const input = [
this.apiKey,
ts,
nonce,
null,
this.organizationId,
null,
httpMethod.toUpperCase(),
requestPath,
query
]
return {
'X-Request-Id': ts,
'X-Time': ts,
'X-Nonce': nonce,
'X-Organization-ID': this.organizationId,
'X-Auth': `${this.apiKey}:${this.hmacSha256BySegments(input)}`
}
}
getRequestPromise(httpMethod, requestPath, params) {
const payload =
{
headers: this.getHeaders(httpMethod, requestPath, params),
params: params
};
return this.axios.get(requestPath, payload)
}
getUnsignedRequestPromise(httpMethod, requestPath, params) {
const payload =
{
headers: {},
params: params
};
return this.axios.get(requestPath, payload)
}
getWallets() {
return this.getRequestPromise('GET', '/main/api/v2/accounting/accounts2', {})
}
getWallet(currency) {
return this.getRequestPromise('GET', `/main/api/v2/accounting/account2/${currency}`, {extendedResponse: true})
}
getPayouts() {
return this.getRequestPromise('GET', '/main/api/v2/mining/rigs/payouts', {})
}
getHashpowerEarnings() {
const currency = 'BTC'
const params = {
op: 'LT',
timestamp: Date.now(),
}
return this.getRequestPromise('GET', '/main/api/v2/accounting/hashpowerEarnings/'+currency, params)
}
getMiningRigs() {
return this.getRequestPromise('GET', '/main/api/v2/mining/rigs2', {})
}
getMiningRigsStats(afterTimestamp) {
const params = {}
if(afterTimestamp) {
params['afterTimestamp'] = afterTimestamp
}
return this.getRequestPromise('GET', '/main/api/v2/mining/rigs/stats/unpaid', params)
}
getMiningUnpaid() {
return this.getRequestPromise('GET', '/main/api/v2/mining/rigs/stats/unpaid', {})
}
/**
* Get the latest forex exchange rates
*
* - This is a public API endpoint
*/
getExchangeRates() {
return this.getUnsignedRequestPromise('GET', '/exchange/api/v2/info/prices', {});
}
}
module.exports = NiceHashClient;