Kraken API MATLAB客户端无效签名错误 [英] Kraken API MATLAB client invalid signature error
问题描述
我正在尝试对Kraken专用端点进行一些经过身份验证的调用,但未成功.我仍然收到错误的EAPI:无效签名. 有人知道怎么了吗?
I'm trying to do some authenticated calls to Kraken private endpoints but without success. I'm still getting an error EAPI:Invalid signature. Does anybody know what's wrong?
代码如下:
function [response,status]=kraken_authenticated(uri,postdata)
% test uri='0/private/AddOrder'
% test postdata='&pair=XBTEUR&type=buy&ordertype=limit&price=345.214&volume=0.65412&leverage=1.5&oflags=post'
url=['https://api.kraken.com/',uri];
% nonce
nonce = num2str(floor((now-datenum('1970', 'yyyy'))*8640000000));
[key,secret]=key_secret('kraken');
% 1st hash
Opt.Method = 'SHA-256';
Opt.Input = 'ascii';
sha256string = DataHash(['nonce=',nonce,postdata],Opt);
% 2nd hash
sign = crypto([uri,sha256string], secret, 'HmacSHA512');
header_1=http_createHeader('API-Key',key);
header_2=http_createHeader('API-Sign',char(sign));
header=[header_1 header_2];
[response,status] = urlread2(url,'POST',['nonce=',nonce,postdata],header);
end
加密功能在另一个文件中:
Crypto function is in another file:
function signStr = crypto(str, key, algorithm)
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.apache.commons.codec.binary.*
keyStr = java.lang.String(key);
key = SecretKeySpec(keyStr.getBytes('UTF-8'), algorithm);
mac = Mac.getInstance(algorithm);
mac.init(key);
toSignStr = java.lang.String(str);
signStr = java.lang.String(Hex.encodeHex( mac.doFinal( toSignStr.getBytes('UTF-8'))));
end
我也尝试过
sign = crypto([uri,sha256string], base64decode(secret), 'HmacSHA512');
但没有成功.
这是经过身份验证的呼叫HTTPS标头的指南:
This is guide for authenticated call HTTPS Header:
API-Key = API key
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
这是经过身份验证的呼叫POST数据的指南:
This is guide for authenticated call POST Data:
nonce = always increasing unsigned 64 bit integer
otp = two-factor password (if two-factor enabled, otherwise not required)
我试图将"nonce"参数或"postdata"中的所有参数传递给POST数据,但是没有成功.
I've tried to pass "nonce" parameter or all parameters in "postdata" to POST data but without success.
感谢您的帮助.
推荐答案
问题出在这里的加密函数中:
The problem is in function crypto here:
keyStr = java.lang.String(key);
key = SecretKeySpec(keyStr.getBytes('UTF-8'), algorithm);
由于来自kraken的base64编码的私钥不一定是UTF-8编码的,因此您不能使用UTF-8编码来提取密钥并将UTF-8字符串传递给SecretKeySpec函数.您需要改用字节数组.
As the base64 encoded private key from kraken is not necessarily UTF-8 encoded, you cannot use UTF-8 encoding to extract the key and pass UTF-8 string to the SecretKeySpec function. You need to use byte array instead.
类似的问题
https://code.google. com/p/google-apps-script-issues/issues/detail?id = 5113 https://code.google.com/p /google-apps-script-issues/issues/detail?id = 3121
JavaScript解决方案
Solution for javascript
github.com/Caligatio/jsSHA
github.com/Caligatio/jsSHA
这篇关于Kraken API MATLAB客户端无效签名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!