2016-09-19 4 views
2

Ich versuche, ein funktionierendes Beispiel von Cis zu Java zu ändern, aber ich kämpfe und weiß nicht, wo das Problem liegt. Ich kontaktierte Kraken und sie sagte mir, dass ich die falsche Unterschrift haben ... Die Antwort ist:Kraken API - Ungültige Signatur

{"error":["EAPI:Invalid signature"]} 

Hier ist die Cis-Version:

private JsonObject QueryPrivate(string a_sMethod, string props = null) 
    { 
     // generate a 64 bit nonce using a timestamp at tick resolution 
     Int64 nonce = DateTime.Now.Ticks; 
     props = "nonce=" + nonce + props; 


     string path = string.Format("/{0}/private/{1}", _version, a_sMethod); 
     string address = _url + path; 
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address); 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.Method = "POST"; 
     webRequest.Headers.Add("API-Key", _key); 


     byte[] base64DecodedSecred = Convert.FromBase64String(_secret); 

     var np = nonce + Convert.ToChar(0) + props; 

     var pathBytes = Encoding.UTF8.GetBytes(path); 
     var hash256Bytes = sha256_hash(np); 
     var z = new byte[pathBytes.Count() + hash256Bytes.Count()]; 
     pathBytes.CopyTo(z, 0); 
     hash256Bytes.CopyTo(z, pathBytes.Count()); 

     var signature = getHash(base64DecodedSecred, z); 

     webRequest.Headers.Add("API-Sign", Convert.ToBase64String(signature)); 

     if (props != null) 
     { 

      using (var writer = new StreamWriter(webRequest.GetRequestStream())) 
      { 
       writer.Write(props); 
      } 
     } 

     //Make the request 
     try 
     { 

      using (WebResponse webResponse = webRequest.GetResponse()) 
      { 
       using (Stream str = webResponse.GetResponseStream()) 
       { 
        using (StreamReader sr = new StreamReader(str)) 
        { 
         return (JsonObject)JsonConvert.Import(sr); 
        } 
       } 
      } 
     } 

    } 

Der vollständige Code ist hier an:

https://bitbucket.org/arrivets/krakenapi/src/cff138b017c38efde2db1a080fb765790a6d04c8/KrakenClient/KrakenClient.cs?at=master&fileviewer=file-view-default

Hier ist meine Java-Version:

private void fetch() throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException { 

    String version = "0"; 
    String key = ".....6"; 
    String secret = "....g=="; 
    long nonce = System.currentTimeMillis(); 

    String props = null; 
    props = "nonce=" + nonce + props; // I've tried this with and without the 'null' on the end 

    // url 
    String url = "https://api.kraken.com"; 
    String path = "/" + version + "/private/" + "Balance"; 
    String address = url + path; 

    // post req 
    HttpPost httpPost = new HttpPost(address); 

    // headers 
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
    httpPost.setHeader("API-Key", key); 

    // decode buffer 
    BASE64Decoder decoder = new BASE64Decoder(); 
    byte[] base64DecodedSecred = decoder.decodeBuffer(secret); 

    // nonce & props 
    String np = nonce + (char) 0 + props; 

    // create byte array 
    byte[] pathBytes = path.getBytes("UTF-8"); 
    byte[] hash256Bytes = sha256(np); 
    byte[] z = new byte[pathBytes.length + hash256Bytes.length]; 
    System.arraycopy(pathBytes, 0, z, 0, pathBytes.length); 
    System.arraycopy(hash256Bytes, 0, z, pathBytes.length, hash256Bytes.length); 

    // encrypt signature 
    byte[] signature = hmacEncrypt(z, base64DecodedSecred); // my hmacEncrypt is message, secret (opposite to the c sharp) 
    BASE64Encoder encoder = new BASE64Encoder(); 
    httpPost.setHeader("API-Sign", encoder.encode(signature)); 

    // Post 
    List<NameValuePair> nvps = new ArrayList<>(); 
    nvps.add(new BasicNameValuePair("nonce", String.valueOf(nonce))); 
    httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 

    // Client & Response 
    CloseableHttpClient httpClient = HttpClients.createDefault(); 
    CloseableHttpResponse response = httpClient.execute(httpPost); 

    HttpEntity entity = response.getEntity(); 

    // parse 
    JsonParser jp = new JsonParser(); 
    JsonElement root = jp.parse(EntityUtils.toString(entity)); 
    System.out.println(root); // {"error":["EAPI:Invalid signature"]} 

    // close client 
    httpClient.close(); 
} 

Es tut mir leid, eine große Bits Code geschrieben haben, jede Hilfe wäre willkommen. Vielen Dank!

Antwort

0

In Ihrem Code ist keine cchmac 512-Verschlüsselung enthalten. Deshalb erhalten Sie diese EAPI: Ungültige Signatur "

Verwandte Themen