-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiveAPIExample.java
More file actions
326 lines (290 loc) · 11.4 KB
/
HiveAPIExample.java
File metadata and controls
326 lines (290 loc) · 11.4 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package org.hive.api;
import org.json.JSONException;
import org.json.JSONObject;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
public class HiveAPIExample {
private static final String UTF_8 = "UTF-8";
private static final String GET_RIGS = "getRigs";
private static final String GET_WALLETS = "getWallets";
private static final String GET_OC = "getOC";
private static final String GET_CURRENT_STATS = "getCurrentStats";
private static final String MULTI_ROCKET = "multiRocket";
/**
* Your secret API key, you sign the message with it
* @var string 64 hex chars
*/
private static String SECRET_KEY = "xxx";
/**
* This is a public key
* @var string 64 hex chars
*/
private static String PUBLIC_KEY = "xxx";
/**
* Api connection URL
* Please use HTTPS, not HTTP for calls for better security.
* @var string
*/
public static String HTTPS_URL = "https://api.hiveos.farm/worker/eypiay.php";
static class Response{
int responseCode = -1;
JSONObject responseData = null;
}
static class MultiRocketParams{
String rigIds; // coma separated string with rig ids "1,2,3,4"
String miner = null; // Miner to set. Leave it null if you do not want to change. "claymore", "claymore-z", "ewbf", ...
String miner2 = null; // Second miner to set. Leave it null if you do not want to change. "0" - if you want to unset it.
String idWal = null; // ID of wallet. Leave it null if you do not want to change.
String idOc = null; // ID of OC profile. Leave it null if you do not want to change.
}
private static void log(String msg) {
System.out.println(msg);
}
private final String[] args;
private HiveAPIExample(String[] args){
this.args = args;
}
private static void printHelp(){
System.out.println("Usage: java -jar HiveAPIExample.jar methodName aguments");
System.out.println("Examples:");
System.out.println("\tjava -jar HiveAPIExample.jar " + GET_RIGS);
System.out.println("\tjava -jar HiveAPIExample.jar " + GET_WALLETS);
System.out.println("\tjava -jar HiveAPIExample.jar " + GET_CURRENT_STATS);
System.out.println("\tjava -jar HiveAPIExample.jar " + MULTI_ROCKET + " \"rig1,rig2\" [miner] [miner2] [id_wal] [id_oc]");
}
public static void main(String[] args) throws Exception {
if(args.length >= 1){
HiveAPIExample h = new HiveAPIExample(args);
h.run();
}else{
printHelp();
}
}
private void run() throws Exception {
log("=== Hive API example ===");
String method = args[0];
JSONObject result = null;
if(GET_RIGS.equalsIgnoreCase(method)){
result = getRigs();
} else if(GET_WALLETS.equalsIgnoreCase(method)){
result = getWallets();
} else if(GET_OC.equalsIgnoreCase(method)){
result = getOC();
} else if(GET_CURRENT_STATS.equalsIgnoreCase(method)){
result = getCurrentStats();
} else if(MULTI_ROCKET.equalsIgnoreCase(method)){
MultiRocketParams params = new MultiRocketParams();
if(args.length >= 6){
params.rigIds = args[1];
params.miner = args[2];
params.miner2 = args[3];
params.idWal = args[4];
params.idOc = args[5];
result = multiRocket(params);
}else{
// To use rocket you have to know what you are doing. Then delete these lines and edit the following.
log("Please edit the source to use multiRocket method");
return;
//this is just an example, use some of your real ids which you get with other methods
//set everything to rigs ids 1, 2 and 3
//params.rigIds = "1,2,3";
//params.miner = "claymore";
//params.miner2 = "xmrig";
//params.idWal = "107800";
//params.idOc = "800";
//result = multiRocket(params);
//set bminer to rigs ids 4 and 5, unset second miner
//params.rigIds = "4,5";
//params.miner = "bminer";
//params.miner2 = "0";
//params.idWal = null;
//params.idOc = null;
//result = multiRocket(params);
}
} else {
printHelp();
return;
}
if(result != null) {
log(result.toString(4));
}
}
/**
* Rigs list
* @return
*/
static JSONObject getRigs(){
Map<String, String> httpParams = new HashMap<>();
httpParams.put("method", GET_RIGS);
Response result = sendPOST(HTTPS_URL, httpParams);
if(result.responseCode == 200) {
return ((JSONObject) result.responseData.get("result"));
}else{
return null;
}
}
/**
* Wallets list
* @return
*/
static JSONObject getWallets(){
Map<String, String> httpParams = new HashMap<>();
httpParams.put("method", GET_WALLETS);
Response result = sendPOST(HTTPS_URL, httpParams);
if(result.responseCode == 200) {
return ((JSONObject) result.responseData.get("result"));
}else{
return null;
}
}
/**
* Overclocking profiles
* @return
*/
static JSONObject getOC(){
Map<String, String> httpParams = new HashMap<>();
httpParams.put("method", GET_OC);
Response result = sendPOST(HTTPS_URL, httpParams);
if(result.responseCode == 200) {
return ((JSONObject) result.responseData.get("result"));
}else{
return null;
}
}
/**
* Monitor stats for all the rigs
* @return
*/
static JSONObject getCurrentStats(){
Map<String, String> httpParams = new HashMap<>();
httpParams.put("method", GET_CURRENT_STATS);
Response result = sendPOST(HTTPS_URL, httpParams);
if(result.responseCode == 200) {
return ((JSONObject) result.responseData.get("result"));
}else{
return null;
}
}
/**
* Sets parameters for rigs
* @param params see MultiRocketParams
* @return
*/
static JSONObject multiRocket(MultiRocketParams params){
Map<String, String> httpParams = new HashMap<>();
httpParams.put("method", MULTI_ROCKET);
if(params.rigIds != null) httpParams.put("rig_ids_str", params.rigIds);
if(params.miner != null) httpParams.put("miner", params.miner);
if(params.miner2 != null) httpParams.put("miner2", params.miner2);
if(params.idWal != null) httpParams.put("id_wal", params.idWal);
if(params.idOc != null) httpParams.put("id_oc", params.idOc);
Response result = sendPOST(HTTPS_URL, httpParams);
if(result.responseCode == 200) {
return ((JSONObject) result.responseData.get("result"));
}else{
return null;
}
}
/**
* Make API request with given params. Signs the request with secret key.
* @param params
* @return
*/
static Response sendPOST(String url, Map<String, String> params) {
params.put("public_key", PUBLIC_KEY);
String urlParameters = buildQueryString(params, UTF_8);
Response response = new Response();
StringBuffer buf = new StringBuffer();
URL obj = null;
try {
obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// add request header
con.setRequestProperty("HMAC", encodeHMAC(SECRET_KEY, urlParameters));
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
// Send post request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// Get result
response.responseCode = con.getResponseCode();
InputStream is;
if(response.responseCode == 200) {
is = con.getInputStream();
}else{
is = con.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
while ((inputLine = in.readLine()) != null) {
buf.append(inputLine);
}
in.close();
if(response.responseCode == 200) {
response.responseData = new JSONObject(buf.toString());
}else{
try{
response.responseData = new JSONObject(buf.toString());
log("ERROR: HTTP " + response.responseCode + ": \n" + response.responseData.toString(4));
}catch (JSONException e) {
log("ERROR: HTTP " + response.responseCode + ": " + buf.toString());
}
}
} catch (MalformedURLException e) {
log("ERROR: Invalid URL: " + url);
e.printStackTrace();
} catch (ProtocolException e) {
log("ERROR: Invalid request method");
e.printStackTrace();
} catch (IOException e) {
log("ERROR: input/output operation");
e.printStackTrace();
} catch (JSONException e) {
log("ERROR: Invalid json response: " + buf.toString());
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
log("ERROR: Can't find HmacSHA256 algorithm");
e.printStackTrace();
} catch (InvalidKeyException e) {
log("ERROR: Invalid secret key");
e.printStackTrace();
}
return response;
}
static String encodeHMAC(String key, String data) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(UTF_8), "HmacSHA256");
sha256_HMAC.init(secret_key);
return DatatypeConverter.printHexBinary(sha256_HMAC.doFinal(data.getBytes(UTF_8))).toLowerCase();
}
static String buildQueryString(Map<String, String> parameters, String encoding) {
return parameters.entrySet().stream()
.map(entry -> encodeParameter(entry.getKey(), entry.getValue(), encoding))
.reduce((param1, param2) -> param1 + "&" + param2)
.orElse("");
}
static String encodeParameter(String key, String value, String encoding) {
return urlEncode(key, encoding) + "=" + urlEncode(value, encoding);
}
static String urlEncode(String value, String encoding) {
try {
return URLEncoder.encode(value, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Cannot url encode " + value, e);
}
}
}