首页 > 解决方案 > Kraken API 无效签名

问题描述

我在 javascript 中有下一个代码,可以使用 REST API 登录 kraken。当我调用“getDepositMethods”时,它会显示我和错误:“EAPI:Invalid 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;
}

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

标签: javascript

解决方案


使用库 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;
      }

推荐阅读