Skip to content

Commit

Permalink
[Huobi private] - Fix recomputation of signature in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Nov 19, 2024
1 parent 20c9c77 commit f45b0f1
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/api/exchanges/src/huobiprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <optional>
#include <string_view>
Expand Down Expand Up @@ -88,32 +89,36 @@ void SetNonceAndSignature(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequ
std::string_view endpoint, CurlPostData& postData, CurlPostData& signaturePostData) {
auto isNotEncoded = [](char ch) { return isalnum(ch) || ch == '-' || ch == '.' || ch == '_' || ch == '~'; };

static constexpr std::string_view kSignatureKey = "Signature";

signaturePostData.set("Timestamp", URLEncode(Nonce_LiteralDate(kTimeYearToSecondTSeparatedFormat), isNotEncoded));

if (!postData.empty() && requestType == HttpRequestType::kGet) {
// Warning: Huobi expects that all parameters of the query are ordered lexicographically
// We trust the caller for this. In case the order is not respected, error 'Signature not valid' will be
// returned from Huobi
signaturePostData.append(postData);
postData = CurlPostData();
postData.clear();
} else if (signaturePostData.back().key() == kSignatureKey) {
// signature needs to be erased (if we had an error) before computing the sha256
signaturePostData.pop_back();
}

static constexpr std::string_view kSignatureKey = "Signature";

signaturePostData.set_back(kSignatureKey,
URLEncode(B64Encode(ssl::Sha256Bin(BuildParamStr(requestType, curlHandle.getNextBaseUrl(),
endpoint, signaturePostData.str()),
apiKey.privateKey())),
isNotEncoded));
signaturePostData.emplace_back(
kSignatureKey, URLEncode(B64Encode(ssl::Sha256Bin(BuildParamStr(requestType, curlHandle.getNextBaseUrl(),
endpoint, signaturePostData.str()),
apiKey.privateKey())),
isNotEncoded));
}

json::container PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequestType requestType,
std::string_view endpoint, CurlPostData&& postData = CurlPostData()) {
CurlPostData signaturePostData{
{"AccessKeyId", apiKey.key()}, {"SignatureMethod", "HmacSHA256"}, {"SignatureVersion", 2}};

string method(endpoint);
method.push_back('?');
string method(endpoint.size() + 1U, '?');

std::memcpy(method.data(), endpoint.data(), endpoint.size());

CurlOptions::PostDataFormat postDataFormat = ComputePostDataFormat(requestType, postData);

Expand Down

0 comments on commit f45b0f1

Please sign in to comment.