Kraken API无效的Signatura

时间:2018-05-21 19:37:31

标签: javascript

我在javascript中使用REST API登录kraken。当我调用“getDepositMethods”时,它会显示我并显示错误:“EAPI:无效的signatura”。

class Kraken{

    constructor(key, secret){
        this.key = key;
        this.secret = secret;
    }
    
    getDepositMethods(){
        var nonce = new Date() * 1000; // spoof microsecond
        const signature = getMessageSignature("/0/private/DepositMethods", {}, this.secret, nonce);
        $.ajax({
            method: "POST",
            url: "https://api.kraken.com/0/private/DepositMethods",
            headers: { "API-Key" : this.key,
                       "API-Sign" : signature},
            data: {nonce: nonce,
                   asset: "xbt"},
            dataType: 'json', 
            success: function(data_response){ 
                console.log(data_response);
            },
            error: function(data){
                console.log(data);
            }
        });
    }
 }

function main(){
  const kraken = new Kraken(key,secret);
  kraken.getDepositMethods();
}

// Create a signature for a request
function getMessageSignature(path, request, secret, nonce){
    const crypto = require('crypto');
    var qs = require('qs');
    const message       = qs.stringify(request);
	const secret_buffer = new Buffer(secret, 'base64');
	const hash          = new crypto.createHash('sha256');
	const hmac          = new crypto.createHmac('sha512', secret_buffer);
	const hash_digest   = hash.update(nonce + message).digest('binary');;
	const hmac_digest   = hmac.update(path + hash_digest, 'binary').digest('base64');
    console.log(hmac);
	return hmac;
}

有人可以帮帮我吗?我需要资金来拨打私人方法吗?

1 个答案:

答案 0 :(得分:1)

使用库 CryptoJS (https://github.com/brix/crypto-js)

    getMessageSignature : function(api_path, api_post, apiSecret, api_nonce)  {
          api_secret = CryptoJS.enc.Base64.parse(apiSecret);
          api_sha256 = CryptoJS.SHA256(api_nonce + api_post);
          api_sign = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, api_secret);
          api_sign.update(api_path, api_secret);
          api_sign.update(api_sha256, api_secret);
          api_sign = api_sign.finalize().toString(CryptoJS.enc.Base64);
          return api_sign;
      }