All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.knowm.xchange.okcoin.OkCoinDigest Maven / Gradle / Ivy

There is a newer version: 5.2.1
Show newest version
package org.knowm.xchange.okcoin;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.ws.rs.FormParam;
import si.mazi.rescu.Params;
import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.RestInvocation;

public class OkCoinDigest implements ParamsDigest {

  private static final char[] DIGITS_UPPER = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  };

  private final String apikey;
  private final String secretKey;
  private final MessageDigest md;

  public OkCoinDigest(String apikey, String secretKey) {

    this.apikey = apikey;
    this.secretKey = secretKey;

    try {
      md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException("Problem instantiating message digest.");
    }
  }

  private static char[] encodeHex(byte[] data, char[] toDigits) {

    int l = data.length;
    char[] out = new char[l << 1];
    // two characters form the hex value.
    for (int i = 0, j = 0; i < l; i++) {
      out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
      out[j++] = toDigits[0x0F & data[i]];
    }
    return out;
  }

  @Override
  public String digestParams(RestInvocation restInvocation) {

    final Params params = restInvocation.getParamsMap().get(FormParam.class);
    final Map nameValueMap = params.asHttpHeaders();

    nameValueMap.remove("sign");
    nameValueMap.put("api_key", apikey);

    // odd requirements for buy/sell market orders
    if (nameValueMap.containsKey("type") && nameValueMap.get("type").contains("market")) {
      if (nameValueMap.get("type").equals("buy_market")) {
        nameValueMap.remove("amount");
      } else if (nameValueMap.get("type").equals("sell_market")) {
        nameValueMap.remove("price");
      }
    }
    final List> nameValueList = new ArrayList<>(nameValueMap.entrySet());
    Collections.sort(nameValueList, (e1, e2) -> e1.getKey().compareTo(e2.getKey()));

    final Params newParams = Params.of();
    for (int i = 0; i < nameValueList.size(); i++) {
      Map.Entry param = nameValueList.get(i);
      newParams.add(param.getKey(), param.getValue());
    }

    // final String message = newParams.asQueryString() + "&secret_key=" + secretKey;
    final String message = newParams.toString() + "&secret_key=" + secretKey;

    try {
      md.reset();

      byte[] digest = md.digest(message.getBytes("US-ASCII"));

      return String.valueOf(encodeHex(digest, DIGITS_UPPER));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Codec error", e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy