Hampie
Hampie

Reputation: 1

Kraken API "Invalid signature" error using Matlab

I am trying to implement the API of the bitcoin exchange Kraken in MATLAB. Unfortunately I got stuck at trying to execute an authentication in order to retrieve private user data.

In particular, I was playing with the following Implementation: Kraken API MATLAB client invalid signature error. The documentation of Kraken's API is here: https://www.kraken.com/help/api

When connecting with the Private user data but I continuously run into the error: {"error":["EAPI:Invalid signature"]}. Could someone maybe have a quick look at the implementation below and look for flaws in the code? Or has someone successfully implemented the Kraken API for Matlab?

Many thanks!

% Private
 uri = '0/private/Balance';
 postdata='';
 [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'

function [response,status]=kraken_authenticated(uri,postdata)

% Generate URL
 url=['https://api.kraken.com/',uri];

% nonce
 nonce = num2str(floor((now-datenum('1970', 'yyyy'))*8640000000));
 key = ' '
 secret = ' '

 % 1st hash
 Opt.Method = 'SHA-256';
 Opt.Input = 'ascii';
 sha256string = DataHash(['nonce=',nonce,postdata],Opt);

% 2nd hash
 %sign = crypto([uri,sha256string], secret, 'HmacSHA512');
 sign = crypto([uri,sha256string], base64decode(secret), 'HmacSHA512')
 %sign = HMAC([uri,sha256string], base64decode(secret), 'SHA-512');

 %header_0=http_createHeader('Content-Type','application/x-www-form-urlencoded');
 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

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); 
 %key = SecretKeySpec(keyStr.getBytes(), 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'))))
 %signStr = java.lang.String(Hex.encodeHex( mac.doFinal( toSignStr.getBytes())))
 end

function header = http_createHeader(name,value)
 header = struct('name',name,'value',value);
 end

Upvotes: 0

Views: 556

Answers (1)

FabioB
FabioB

Reputation: 133

I'm actually trying to do my owm implementation in c++, and I was here for another error I get, but here is a possible cause I noticed in your code :

The first sha256 hash should be of a concatenation of nonce and postdata. As postdata is also containing the nonce, if the nonce value is 123456789, then you should do (pseudo-code) :

sha256("123456789nonce=123456789")

or in Matlab :

sha256string = DataHash([nonce,'nonce=',nonce],Opt);

I hope it helps.

Upvotes: 2

Related Questions