1

我从 Kraken API 获取余额的代码在 Python 中工作(基于 krakenex 库),但在 JS 版本中不起作用(松散地基于 kraken-api 库,但用crypto库代替crypto-js)。错误总是:Invalid Key

即使我将 Python 客户端发送的标头和随机数复制到 Postman 中,我也会得到 Invalid Key。

我相信签名和随机数是有效的,因为当它们不是时,Kraken 反驳说签名或随机数都是无效的。

Javascript 与 Python3 有什么fetch不同requests吗?因为正文和标题在其他方面是相同的。

生成验证数据的 JS 代码:

const getMessageSignature = (path, request, secret, nonce) => {
    // API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
    const message = qs.stringify(request);
    console.log(message);

    const secret_buffer = btoa(secret);
    const hash = CryptoJS.algo.SHA256.create();
    const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, secret_buffer);
    const hash_digest = hash.update(nonce + message).finalize().toString(CryptoJS.enc.Hex);
    const hmac_digest = hmac.update(path + hash_digest).finalize().toString(CryptoJS.enc.Base64);

    // CANNOT USE ORIGINAL LIB CODE (Buffer, got and crypto not supported)
    // 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');

    return hmac_digest;
};

更新: 事实上,以下观察结果很奇怪:

  • 正确的密钥+正确的签名=“错误的密钥”
  • 不正确的密钥+正确的签名=“不正确的密钥”
  • 不正确的密钥 + 不正确的签名 = “不正确的密钥”
  • 正确的密钥+错误的签名=“无效的签名”

是什么赋予了?

Update2 似乎请求是相同的(当然除了签名和随机数,它们会并且应该随着每个请求而改变)。

在此处输入图像描述

4

1 回答 1

0

事实证明它毕竟是签名,而 Kraken 根本没有给出非常准确的响应(这有一定道理,但如果你想弄清楚一些事情会很痛苦)。最后,我只能使用 CryptoJS 重写代码:

const getMessageSignature = (path, request, secret, nonce) => {
    // API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
    const message = JSON.stringify(request);
    const hash = CryptoJS.SHA256(nonce + message);
    const secret_buffer = CryptoJS.enc.Base64.parse(secret);
    const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, secret_buffer);
    hmac.update(path, secret_buffer);
    hmac.update(hash, secret_buffer);
    return hmac.finalize().toString(CryptoJS.enc.Base64);
};

这会产生正确的签名,并且 Kraken 不再抱怨。扎。

于 2018-09-27T08:34:19.853 回答