Skip to content

Commit 480231f

Browse files
committed
[bitmex] sign requests correctly
1 parent 58f7b1e commit 480231f

File tree

4 files changed

+24
-37
lines changed

4 files changed

+24
-37
lines changed

xchange-bitmex/src/main/java/org/knowm/xchange/bitmex/Bitmex.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@ public interface Bitmex {
3030

3131
@GET
3232
@Path("user")
33-
BitmexAccount getAccount(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce, @HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest)
33+
BitmexAccount getAccount(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce, @HeaderParam("api-signature") ParamsDigest paramsDigest)
3434
throws IOException;
3535

3636
@GET
3737
@Path("user/wallet")
38-
BitmexWallet getWallet(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce, @HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest,
38+
BitmexWallet getWallet(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce, @HeaderParam("api-signature") ParamsDigest paramsDigest,
3939
@Nullable @QueryParam("currency") String currency) throws IOException;
4040

4141
// Get a history of all of your wallet transactions (deposits, withdrawals, PNL)
4242
@GET
4343
@Path("user/walletHistory")
44-
List<BitmexWalletTransaction> getWalletHistory(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce,
45-
@HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest, @Nullable @QueryParam("currency") String currency) throws IOException;
44+
List<BitmexWalletTransaction> getWalletHistory(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce,
45+
@HeaderParam("api-signature") ParamsDigest paramsDigest, @Nullable @QueryParam("currency") String currency) throws IOException;
4646

4747
// Get a summary of all of your wallet transactions (deposits, withdrawals, PNL)
4848
@GET
4949
@Path("user/walletSummary")
50-
List<BitmexWalletTransaction> getWalletSummary(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce,
51-
@HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest, @Nullable @QueryParam("currency") String currency) throws IOException;
50+
List<BitmexWalletTransaction> getWalletSummary(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce,
51+
@HeaderParam("api-signature") ParamsDigest paramsDigest, @Nullable @QueryParam("currency") String currency) throws IOException;
5252

5353
@GET
5454
@Path("user/margin")
55-
BitmexMarginAccount getMarginAccountStatus(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce,
56-
@HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest, @Nullable @QueryParam("currency") String currency) throws IOException;
55+
BitmexMarginAccount getMarginAccountStatus(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce,
56+
@HeaderParam("api-signature") ParamsDigest paramsDigest, @Nullable @QueryParam("currency") String currency) throws IOException;
5757

5858
@GET
5959
@Path("user/margin?currency=all")
60-
List<BitmexMarginAccount> getMarginAccountsStatus(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce,
61-
@HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest) throws IOException;
60+
List<BitmexMarginAccount> getMarginAccountsStatus(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce,
61+
@HeaderParam("api-signature") ParamsDigest paramsDigest) throws IOException;
6262

6363
@GET
6464
@Path("trade")
@@ -70,12 +70,12 @@ List<BitmexMarginAccount> getMarginAccountsStatus(@HeaderParam("API-KEY") String
7070

7171
@GET
7272
@Path("position")
73-
List<BitmexPosition> getPositions(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce, @HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest)
73+
List<BitmexPosition> getPositions(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce, @HeaderParam("api-signature") ParamsDigest paramsDigest)
7474
throws IOException;
7575

7676
@GET
7777
@Path("position")
78-
List<BitmexPosition> getPositions(@HeaderParam("API-KEY") String apiKey, @HeaderParam("API-NONCE") SynchronizedValueFactory<Long> nonce, @HeaderParam("API-SIGNATURE") ParamsDigest paramsDigest,
78+
List<BitmexPosition> getPositions(@HeaderParam("api-key") String apiKey, @HeaderParam("api-nonce") SynchronizedValueFactory<Long> nonce, @HeaderParam("api-signature") ParamsDigest paramsDigest,
7979
@Nullable @QueryParam("symbol") String symbol, @Nullable @QueryParam("filter") String filter) throws IOException;
8080

8181
@GET

xchange-bitmex/src/main/java/org/knowm/xchange/bitmex/service/BitmexAccountService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
import org.knowm.xchange.Exchange;
8+
import org.knowm.xchange.bitmex.dto.account.BitmexAccount;
89
import org.knowm.xchange.currency.Currency;
910
import org.knowm.xchange.dto.account.AccountInfo;
1011
import org.knowm.xchange.dto.account.FundingRecord;
@@ -26,9 +27,9 @@ public BitmexAccountService(Exchange exchange) {
2627
}
2728

2829
@Override
29-
public AccountInfo getAccountInfo() {
30-
AccountInfo accountInfo = getAccountInfo();
31-
return new AccountInfo(accountInfo.getUsername());
30+
public AccountInfo getAccountInfo() throws IOException {
31+
BitmexAccount account = super.getBitmexAccountInfo();
32+
return new AccountInfo(account.getUsername());
3233
}
3334

3435
@Override

xchange-bitmex/src/main/java/org/knowm/xchange/bitmex/service/BitmexAccountServiceRaw.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class BitmexAccountServiceRaw extends BitmexBaseService {
1515

16-
String apiKey = null;
16+
String apiKey = exchange.getExchangeSpecification().getApiKey();
1717

1818
/**
1919
* Constructor

xchange-bitmex/src/main/java/org/knowm/xchange/bitmex/service/BitmexDigest.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.knowm.xchange.bitmex.service;
22

3-
import java.security.MessageDigest;
4-
import java.security.NoSuchAlgorithmException;
53
import java.util.Base64;
64

7-
import javax.crypto.Mac;
8-
import javax.ws.rs.FormParam;
5+
import javax.ws.rs.HeaderParam;
96

7+
import org.apache.commons.codec.binary.Hex;
108
import org.knowm.xchange.service.BaseParamsDigest;
119

1210
import si.mazi.rescu.RestInvocation;
@@ -18,13 +16,12 @@ public class BitmexDigest extends BaseParamsDigest {
1816
/**
1917
* Constructor
2018
*
21-
* @param secretKeyBase64
22-
* @param apiKey @throws IllegalArgumentException if key is invalid (cannot be base-64-decoded or the decoded key is invalid).
19+
* @param secretKeyBase64 the secret key to sign requests
2320
*/
2421

2522
private BitmexDigest(byte[] secretKeyBase64) {
2623

27-
super(secretKeyBase64, HMAC_SHA_512);
24+
super(Base64.getUrlEncoder().withoutPadding().encodeToString(secretKeyBase64), HMAC_SHA_256);
2825
}
2926

3027
public static BitmexDigest createInstance(String secretKeyBase64) {
@@ -38,21 +35,10 @@ public static BitmexDigest createInstance(String secretKeyBase64) {
3835
@Override
3936
public String digestParams(RestInvocation restInvocation) {
4037

41-
MessageDigest sha256;
42-
try {
43-
sha256 = MessageDigest.getInstance("SHA-256");
44-
} catch (NoSuchAlgorithmException e) {
45-
throw new RuntimeException("Illegal algorithm for post body digest. Check the implementation.");
46-
}
47-
sha256.update(restInvocation.getParamValue(FormParam.class, "nonce").toString().getBytes());
48-
sha256.update(restInvocation.getRequestBody().getBytes());
49-
50-
Mac mac512 = getMac();
51-
mac512.update(("/" + restInvocation.getPath()).getBytes());
52-
mac512.update(sha256.digest());
53-
54-
return Base64.getUrlEncoder().encodeToString(mac512.doFinal()).trim();
38+
String nonce = restInvocation.getParamValue(HeaderParam.class, "api-nonce").toString();
39+
String payload = restInvocation.getHttpMethod() + "/" + restInvocation.getPath() + nonce + restInvocation.getRequestBody();
5540

41+
return new String(Hex.encodeHex(getMac().doFinal(payload.getBytes())));
5642
}
5743

5844
private BitmexDigest(String secretKeyBase64, String apiKey) {

0 commit comments

Comments
 (0)