Recent

Author Topic: [SOLVED] REST API Consuming Help needed  (Read 4644 times)

Blestan

  • Sr. Member
  • ****
  • Posts: 461
[SOLVED] REST API Consuming Help needed
« on: November 04, 2017, 08:56:59 pm »
HI guys im writing a lib to access Kraken.com tradind lib but i have problems with sha256 and sha512 functions to generate signing for the private functions ... can.you point me a good and consistent across languages hash lib? i tried. hash4pascal but cannot make it to work ... i need sha256 w/o key ..

thanks in advance
« Last Edit: November 05, 2017, 03:58:37 pm by Blestan »
Speak postscript or die!
Translate to pdf and live!


Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: REST API Consuming Help needed
« Reply #2 on: November 05, 2017, 01:15:52 pm »
according to the api docs the requests must be signned as follow

API-Key = API key
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key

here is my code but i still get error EAPI Invalid Key

Code: [Select]
function TKrakenAPI.QueryPrivate(const URI: string; Params: String): XVar;
var S: TMemoryStream;
    J: TJSONParser;

    URL,
    Sign,
    Sha256,
    Nonce: String;

    R,I,L: Integer;
    LHMAC: IHMAC;
    sha512: PChar;
    buf: array [00..64] of char;
begin

  Nonce:=IntToStr(DateTimeToUnix(Now));
  Params:='nonce='+nonce+'&'+params;
  sha256:=Nonce+Params;
  URL:=kraken_private+URI;
  L:=Length(URL);
  Getmem(sha512,L+32);
  Move(URL[1],Sha512^,L);

  with THashFactory.TCrypto.CreateSHA2_256().ComputeUntyped(sha256[1],Length(Sha256)) do
       move(getbytes[0],sha512[L],32);

  LHMAC:= THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);
  LHMAC.Key := TConverters.ConvertStringToBytes(DecodeStringBase64(FSecret), TEncoding.UTF8);


  // sign:=EncodeStringBase64(LHMAC.ComputeUntyped(sha512,L+32).ToString);

  move(LHMAC.ComputeUntyped(sha512,L+32).GetBytes[0],Buf[0],64);
  Buf[64]:=#0;
  sign:=EncodeStringBase64(Pchar(@Buf[0]));

  try
  FNET.AddHeader('API-Key:',FKEY);
  FNET.AddHeader('API-Sign:',Sign);
  FNET.AddHeader('Content-Type', 'application/x-www-form-urlencoded');
  FNET.AddHeader('Content-Length', IntToStr(Length(Params)));
   writeln(Nonce);
   writeln(URL);
   writeln(sha512);
   writeln(sign);


   S:=TMemoryStream.Create;
   J:=TJSONParser.Create;
   FNet.FormPOST(KrakenURL+URL,Params,S);
   R:=J.Parse(S.Memory,S.Size);
   writeln(R);
   if R>0 then Result:=J.UseXON
          else Result:=XVar.Null;

   with Result['result'] do
    for i:=0 to Count-1 do writeln(Keys[i].AsString,'=',Vars[i].AsString);
  finally
    J.Free;
    S.Free;
  end;
end;                           
Speak postscript or die!
Translate to pdf and live!

Blestan

  • Sr. Member
  • ****
  • Posts: 461
[SOLVED] REST API Consuming Help needed
« Reply #3 on: November 05, 2017, 03:58:11 pm »
problem solved! :))

if someone is interested in kraken.com API access from fpc feel free to contact me

Speak postscript or die!
Translate to pdf and live!

william22

  • Newbie
  • Posts: 2
Re: [SOLVED] REST API Consuming Help needed
« Reply #4 on: October 08, 2018, 11:57:20 am »
Dear Blestan,

Could you please share your code?
I'm having throuble posting the private functions.

 

TinyPortal © 2005-2018