0

I too like many others am having difficulty connecting Matlab to Kraken from bastardised sources and a clear answer does not exist as yet. Hoping someone can clarify what may be a type error which results in an "EAPI:Invalid signature" error in simple terms please. (I am not a Java programmer.)

uri='0/private/Balance';
postdata= '';
[response, status] = krakenAuthenticated(uri, postdata);

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

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

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

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

  % 2nd hash
  sign = crypto([uri, sha256string], matlab.net.base64decode(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

function header = http_createHeader(name,value)
    header = struct('name',name,'value',value);
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);
    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
bondtrade
  • 1
  • 1
  • The code lacks reproducibility. `keySecret` is undefined. – Tommaso Belluzzo Jan 22 '18 at 23:18
  • Thanks Tommaso. They're just public and private keys (strings) issued by the API provider. – bondtrade Jan 23 '18 at 00:05
  • Try to replace `sha256string = DataHash(['nonce=',nonce,postdata],Opt);` with `sha256string = DataHash([nonce,'nonce=',nonce],Opt);`. – Tommaso Belluzzo Jan 23 '18 at 00:10
  • Thanks Tommaso. Still get the same Invalid Signature error unfortunately. – bondtrade Jan 23 '18 at 00:13
  • This post: https://stackoverflow.com/questions/35999997/kraken-api-matlab-client-invalid-signature-error suggests its a problem here: key = SecretKeySpec(keyStr.getBytes('UTF-8'), algorithm); but a clear solution was not provided. – bondtrade Jan 23 '18 at 00:14

0 Answers0