· 5 years ago · Jan 31, 2020, 05:14 AM
1package com.novakapital.exchange;
2
3import java.io.UnsupportedEncodingException;
4import java.math.BigDecimal;
5import java.math.BigInteger;
6import java.security.InvalidKeyException;
7import java.security.NoSuchAlgorithmException;
8import java.util.HashMap;
9import java.util.Iterator;
10import java.util.Map;
11
12import javax.crypto.Mac;
13import javax.crypto.spec.SecretKeySpec;
14
15import org.json.JSONObject;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
19import com.jayway.jsonpath.JsonPath;
20import com.mashape.unirest.http.HttpResponse;
21import com.mashape.unirest.http.Unirest;
22import com.mashape.unirest.http.exceptions.UnirestException;
23
24public class BitmexService {
25 private static final Logger LOGGER = LoggerFactory.getLogger(BitmexService.class);
26
27 private static final String URI = "https://www.bitmex.com/api/v1";
28
29 private static final String DEFAULT_ENCODING = "UTF-8";
30 private static final String HMAC_SHA256 = "HmacSHA256";
31
32 private static byte[] hmacSha256(String key, String value) {
33 try {
34 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(DEFAULT_ENCODING), HMAC_SHA256);
35
36 Mac mac = Mac.getInstance(HMAC_SHA256);
37 mac.init(keySpec);
38
39 return mac.doFinal(value.getBytes(DEFAULT_ENCODING));
40
41 } catch (NoSuchAlgorithmException e) {
42 throw new RuntimeException(e);
43 } catch (InvalidKeyException e) {
44 throw new RuntimeException(e);
45 } catch (UnsupportedEncodingException e) {
46 throw new RuntimeException(e);
47 }
48 }
49
50 public static String getUnixTimestamp() {
51 return Long.valueOf((System.currentTimeMillis() + 86400L) / 1000L).toString();
52 }
53
54 private static Map<String, String> getKeyInfoFromAccount(String account) {
55 Map<String, String> returnMap = new HashMap<String, String>();
56
57 switch (account) {
58 case "anakemasdukun": // anakemasdukun@gmail.com
59 returnMap.put("apiKey", "123");
60 returnMap.put("apiSecret", "123");
61 break;
62 case "anakperakdukun": //
63 returnMap.put("apiKey", "1234");
64 returnMap.put("apiSecret", "1234");
65 break;
66 default:
67 returnMap.put("apiKey", "");
68 returnMap.put("apiSecret", "");
69 break;
70 }
71
72 return returnMap;
73 }
74
75 private static String getSignature (String verb, String path, String data, String expires, String secretKey) {
76 String param = verb+"/api/v1"+path+expires+data;
77
78 BigInteger bi = new BigInteger(1, hmacSha256(secretKey, param));
79 String signature = String.format("%0" + (hmacSha256(secretKey, param).length << 1) + "x", bi);
80
81 return signature;
82 }
83
84 private static String post(String path, JSONObject json, String account) {
85
86 Map<String, String> mapKey = getKeyInfoFromAccount(account);
87
88 String expires = getUnixTimestamp();
89
90 String responseBody = "";
91 try {
92 HttpResponse<String> response = Unirest.post(URI + path)
93 .header("Content-Type", "application/json")
94 .header("Content-Length", json.toString().length() + "")
95 .header("Accept", "application/json")
96 .header("api-expires", expires)
97 .header("api-key", mapKey.get("apiKey"))
98 .header("api-signature", getSignature("POST", path, json.toString(), expires, mapKey.get("apiSecret")))
99 .header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36")
100 .body(json.toString())
101 .asString();
102
103 responseBody = response.getBody();
104 } catch (UnirestException e) {
105 LOGGER.error(e.getMessage(), e);
106 responseBody = e.getMessage();
107 }
108
109 return responseBody;
110 }
111
112 private static String get(String path, String account) {
113
114 Map<String, String> mapKey = getKeyInfoFromAccount(account);
115
116 String expires = getUnixTimestamp();
117
118 String responseBody = "";
119 try {
120 HttpResponse<String> response = Unirest.get(URI + path)
121 .header("Content-Type", "application/json")
122 .header("Accept", "application/json")
123 .header("api-expires", expires)
124 .header("api-key", mapKey.get("apiKey"))
125 .header("api-signature", getSignature("GET", path, "", expires, mapKey.get("apiSecret")))
126 .header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36")
127 .asString();
128
129 responseBody = response.getBody();
130 } catch (UnirestException e) {
131 LOGGER.error(e.getMessage(), e);
132 responseBody = e.getMessage();
133 }
134
135 return responseBody;
136 }
137
138 public static String getAccountInfo (String account) {
139 return get("/user/margin", account);
140 }
141}