API Coinbase Pro с Delphi (неверная подпись) - PullRequest
1 голос
/ 02 ноября 2019

Я пытаюсь сделать запрос «get» для Coinbase Pro с Delphi 10.3 и остальными компонентами, как описано в документации https://docs.pro.coinbase.com/#api-key-permissions.

Я всегда получаю следующее сообщение: недопустимая подпись. Я проверил все несколько раз, но не могу найти ошибку. Может быть, это проблема с дешифрованием / кодировкой Base64 в строку из секретного ключа и заголовка CB-ACCESS-SIGN? Кодировка sha256 HMAC и временная метка, кажется, работают хорошо. С остальными заголовками тоже вроде все в порядке.

Вот мой код. Ключи старые и только в качестве примера:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,IdHashSha, Vcl.StdCtrls,IdSSLOpenSSL,IdGlobal,IdHMAC,IdHMACSHA1,
  REST.Types, Data.Bind.Components, Data.Bind.ObjectScope,  REST.Client,EncdDecd,system.netencoding,json,idcoder,idcodermime;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button2: TButton;
    RESTClient1: TRESTClient;
    RESTRequest1: TRESTRequest;
    RESTResponse1: TRESTResponse;
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }

 function CalculateHMACSHA256(const value, skey: String): String;
 function DateTimeToUnix(ConvDate: TDateTime): Longint;
 function GetCurrentDateTime: TDateTime;
 procedure stringcreate;

end;
var
  Form1: TForm1;
  timestamp:string;
  date:tdatetime;
  skeydecode:string;
  sha256:string;
  encode:string;
  prehash:string;
  implementation
const UnixStartDate: TDateTime = 25569.0;
const skey:string='iG8mrRLIT9EGw0gMxjxo55U74RtAQeSUal28+VR+STmWvtrTGNunfL19M9M3mmPpBsJLu7LZmrFskb3q42ditQ==';
const apikey:string='3a829c8b4bddb75fb372183a5f63f2a4';
const passphrase:string='eevj5njvs65';


{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
  jValue:TJSONValue;

begin
stringcreate;
restclient1.BaseURL:='https://api.pro.coinbase.com/accounts';
restrequest1.Params.AddHeader('CB-ACCESS-KEY',apikey);
restrequest1.Params.AddHeader('CB-ACCESS-SIGN',encode);
restrequest1.Params.AddHeader('CB-ACCESS-TIMESTAMP',timestamp);
restrequest1.Params.AddHeader('CB-ACCESS-PASSPHRASE',passphrase);

RESTRequest1.Execute;
jValue:=RESTResponse1.JSONValue;
edit1.Text:= jValue.ToString;
end;

function tform1.CalculateHMACSHA256(const value, skey: String): String;
var
hmac: TIdHMACSHA256;
hash: TIdBytes;
begin
LoadOpenSSLLibrary;
if not TIdHashSHA256.IsAvailable then
showmessage('Error');
hmac := TIdHMACSHA256.Create;
try
hmac.Key := IndyTextEncoding_UTF8.GetBytes(skey);
hash := hmac.HashValue(IndyTextEncoding_UTF8.GetBytes(value));
Result := ToHex(hash);
finally
hmac.Free;
end;
end;

function tform1.DateTimeToUnix(ConvDate: TDateTime): Longint;
begin
 Result := Round((ConvDate - UnixStartDate) * 86400);
end;

 function tform1.GetCurrentDateTime: TDateTime;
var
  SystemTime: TSystemTime;
begin
  GetSystemTime(SystemTime);
  Result := system.SysUtils.Systemtimetodatetime(SystemTime);
end;

procedure tform1.stringcreate;
begin
date:=strtodatetime(system.SysUtils.DateTimeToStr(GetCurrentDateTime));
timestamp:=inttostr(DateTimeToUnix(date));
skeydecode:=TIdDecoderMIME.DecodeString(skey);
prehash:=timestamp+'GET'+'/accounts';
sha256:=CalculateHMACSHA256(prehash ,skeydecode);
encode:=Tidencodermime.EncodeString(sha256);

end;


end.

Пожалуйста, вы можете мне помочь?

...