I throw this question out here in hope of some input, I am totally stuck here ....
I am trying to access the Poloniex tradingApi from JAVA code, and all I get is the error "invalid command". A standalone static (utilities) method that illustrates the problem
public static final long generateNonce(){
Date d = new Date();
return d.getTime();
}
public static final String getAddressPoloniex(String currency, double amount, String address) throws IOException{
String result = null;
String nonce = new BigDecimal(WithdrawUtils.generateNonce()).toString();
String connectionString = "
https://poloniex.com/trading?command=returnDepositAddresses&nonce="+nonce;
String hmac512 = MyUtils.hmac512Digest(connectionString, Keys.POLONIEX_SECRET_KEY);
HttpsURLConnection poloniexCon = null;
try {
poloniexCon = (HttpsURLConnection)new URL(connectionString).openConnection();
poloniexCon.addRequestProperty("Key", Keys.POLONIEX_API_KEY);
poloniexCon.addRequestProperty("Sign", hmac512);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
poloniexCon.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
poloniexCon.setRequestProperty("Content-length", String.valueOf(connectionString.length()));
poloniexCon.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
poloniexCon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
poloniexCon.setDoInput(true);
DataInputStream input = new DataInputStream( poloniexCon.getInputStream() );
StringBuffer s = new StringBuffer();
for( int c = input.read(); c != -1; c = input.read() ) {
s.append((char)c);
}
result = s.toString();
input.close();
return result; // here result has {"error":"Invalid command."}
}
This approach works well with for example Bittrex exchange. My feeling is that the problem is in the HMAC SHA512 signing. I do that like this:
public static String hmac512Digest(String msg, String keyString) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA512");
Mac mac = Mac.getInstance("HmacSHA512");
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes
);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (UnsupportedEncodingException e) {
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
}
return digest;
}
If it is not that, I am out of ideas. This HMAC digest works fine with Bittrex. The Poloniex API doc is relatively brief on how this should be calculated. The error is confusing and indicates there is a problem with the command, but all other commands to the trading (private) API gives me the same error. All calls to the public API works fine. I have spent several hours on this now. I know my keys are fine, they work with python code.
The first person with a solution that actually fixes this and send me / post their BTC address will get a 0.1 BTC bounty!
/