Kraken API MATLAB 客户端无效签名错误 [英] Kraken API MATLAB client invalid signature error

查看:30
本文介绍了Kraken API MATLAB 客户端无效签名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 Kraken 私有端点进行一些经过身份验证的调用,但没有成功.我仍然收到错误 EAPI:Invalid signature.有人知道怎么回事吗?

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=5113https://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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆