我从 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 似乎请求是相同的(当然除了签名和随机数,它们会并且应该随着每个请求而改变)。