Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Huobi private] - Fix recomputation of signature in case of error #625

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading