package org.knowm.xchange.bitmex.service;

import java.util.Base64;

import javax.ws.rs.HeaderParam;

import org.apache.commons.codec.binary.Hex;
import org.knowm.xchange.service.BaseParamsDigest;

import si.mazi.rescu.RestInvocation;

public class BitmexDigest extends BaseParamsDigest {

  private String apiKey;

  /**
   * Constructor
   *
   * @param secretKeyBase64 the secret key to sign requests
   */

  private BitmexDigest(byte[] secretKeyBase64) {

    super(Base64.getUrlEncoder().withoutPadding().encodeToString(secretKeyBase64), HMAC_SHA_256);
  }

  public static BitmexDigest createInstance(String secretKeyBase64) {

    if (secretKeyBase64 != null) {
      return new BitmexDigest(Base64.getUrlDecoder().decode(secretKeyBase64.getBytes()));
    }
    return null;
  }

  @Override
  public String digestParams(RestInvocation restInvocation) {

      String nonce = restInvocation.getParamValue(HeaderParam.class, "api-nonce").toString();
      String payload = restInvocation.getHttpMethod() + "/" + restInvocation.getPath() + nonce + restInvocation.getRequestBody();

      return new String(Hex.encodeHex(getMac().doFinal(payload.getBytes())));
  }

  private BitmexDigest(String secretKeyBase64, String apiKey) {

    super(secretKeyBase64, HMAC_SHA_256);
    this.apiKey = apiKey;
  }

  public static BitmexDigest createInstance(String secretKeyBase64, String apiKey) {

    return secretKeyBase64 == null ? null : new BitmexDigest(secretKeyBase64, apiKey);
  }

}

@@ -93,16 +93,47 @@
 erParam;
+%0Aimport javax.ws.rs.QueryParam;
 %0A%0Aimport
@@ -887,26 +887,24 @@
 ion) %7B%0A%0A    
-  
 String nonce
@@ -979,18 +979,159 @@
 ring();%0A
+%0A
   
+  String params = restInvocation.getParamsMap().get(QueryParam.class).toString();%0A    String query = params.isEmpty() ? %22%22 : %22?%22 + params;%0A%0A
     Stri
@@ -1208,16 +1208,24 @@
 Path() +
+ query +
  nonce +
@@ -1259,18 +1259,16 @@
 ody();%0A%0A
-  
     retu

support authenticated requests with querystrings in url