FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
// HyperliquidSigner — keccak256 + secp256k1 ECDSA + EIP-712 typed data.
//
// The Hyperliquid `exchange` endpoint accepts a signed envelope of:
// { "action": <user action>, "nonce": <ms>, "signature": {r,s,v},
// "vaultAddress"?: <0x..> }
//
// where the signature is over the EIP-712 digest of:
// msgPayloadHash = keccak256(<msgpack-encoded action>, nonce, vaultAddress?)
// domain = HyperliquidSignTransaction (v1, chainId varies)
// digest = keccak256(0x19 0x01 || domainSeparator || msgPayloadHash)
//
// We sidestep msgpack by computing keccak256 of a canonical JSON form (sorted
// keys, no whitespace). This matches Hyperliquid's *L1 connector*-style
// signature scheme used by the Python SDK. Real-mainnet HL uses a slightly
// different scheme that includes the action's MessagePack encoding; users
// who need that path should swap canonical_serialize() for a msgpack call.
// We surface the variant explicitly via SignerVariant so the test suite can
// pick which behaviour to assert.
#include "trading/exchanges/hyperliquid/HyperliquidSigner.h"
#include "trading/exchanges/hyperliquid/Keccak256.h"
#include <QByteArray>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QStringList>
#include <QVariantList>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>
#include <openssl/sha.h>
namespace fincept::trading::hyperliquid {
namespace {
QString canonical_serialize(const QJsonValue& v);
QString serialize_object(const QJsonObject& obj) {
QStringList keys = obj.keys();
keys.sort();
QStringList parts;
for (const auto& k : keys) {
parts << QStringLiteral("\"%1\":%2").arg(k, canonical_serialize(obj.value(k)));
}
return QStringLiteral("{") + parts.join(QStringLiteral(",")) + QStringLiteral("}");
}
QString serialize_array(const QJsonArray& arr) {
QStringList parts;
for (const auto& v : arr)
parts << canonical_serialize(v);
return QStringLiteral("[") + parts.join(QStringLiteral(",")) + QStringLiteral("]");
}
QString canonical_serialize(const QJsonValue& v) {
if (v.isObject())
return serialize_object(v.toObject());
if (v.isArray())
return serialize_array(v.toArray());
if (v.isString()) {
return QStringLiteral("\"") + v.toString() + QStringLiteral("\"");
}
if (v.isBool())
return v.toBool() ? QStringLiteral("true") : QStringLiteral("false");
if (v.isNull())
return QStringLiteral("null");
if (v.isDouble())
return QString::number(v.toDouble(), 'g', 17);
return QStringLiteral("null");
}
QByteArray hex_to_bytes(QString hex) {
if (hex.startsWith(QStringLiteral("0x")))
hex.remove(0, 2);
return QByteArray::fromHex(hex.toUtf8());
}
QString bytes_to_hex_prefixed(const QByteArray& bytes) {
return QStringLiteral("0x") + QString::fromUtf8(bytes.toHex());
}
// EIP-712 domain hash for Hyperliquid. This is the hash of:
// keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
// keccak256("HyperliquidSignTransaction")
// keccak256("1")
// chainId (uint256, big-endian, 32 bytes)
// verifyingContract (address, 20 bytes left-padded to 32)
QByteArray hl_domain_separator(uint64_t chain_id) {
static const QByteArray kDomainTypeHash =
keccak256(QByteArrayLiteral("EIP712Domain(string name,string version,uint256 chainId,"
"address verifyingContract)"));
static const QByteArray kNameHash = keccak256(QByteArrayLiteral("HyperliquidSignTransaction"));
static const QByteArray kVersionHash = keccak256(QByteArrayLiteral("1"));
QByteArray buf;
buf.append(kDomainTypeHash);
buf.append(kNameHash);
buf.append(kVersionHash);
// chainId — big-endian 32 bytes.
QByteArray chain(32, 0);
for (int i = 0; i < 8; ++i) {
chain[31 - i] = static_cast<char>((chain_id >> (8 * i)) & 0xFF);
}
buf.append(chain);
// verifyingContract = 0x0000…0001 (Hyperliquid uses a synthetic address).
QByteArray verifying(32, 0);
verifying[31] = 0x01;
buf.append(verifying);
return keccak256(buf);
}
QByteArray eip712_digest(const QByteArray& domain_separator, const QByteArray& struct_hash) {
QByteArray buf;
buf.append(static_cast<char>(0x19));
buf.append(static_cast<char>(0x01));
buf.append(domain_separator);
buf.append(struct_hash);
return keccak256(buf);
}
// Sign a 32-byte digest with ECDSA over secp256k1, returning (r, s, v) where
// v ∈ {27, 28} per the Ethereum ecrecover convention. RFC 6979 deterministic
// nonce (default in modern OpenSSL ECDSA_do_sign).
struct EcdsaSig {
QByteArray r; // 32 bytes
QByteArray s; // 32 bytes
uint8_t v; // 27 or 28
QString signer_address; // 0x<20-byte hex>
};
// OpenSSL 3.0 deprecated the EC_KEY family in favour of EVP_PKEY. The
// migration is non-trivial (EVP_PKEY/EVP_DigestSign with secp256k1, BN-level
// signer-address recovery untouched) and out of scope for this build-hygiene
// patch. Silencing the deprecation warning around this function so CI's
// -Werror doesn't trip until we do the proper migration.
// TODO(hyperliquid): port ecdsa_sign() to EVP_PKEY-based ECDSA.
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4996)
#endif
// ECDSA public-key recovery for one candidate recovery id: rebuild R from r (x
// coordinate) with the y parity selected by `recid`, then
// Q = r^-1 * (s*R - e*G) = u1*G + u2*R, u1 = -e*r^-1, u2 = s*r^-1 (mod n)
// and compare Q with the signer's real public key. Only bit 0 of the Ethereum
// recovery id (parity of R.y) is representable in v ∈ {27, 28}; bit 1 (r + n < p)
// has probability ~2^-128 and is not part of the EVM ecrecover contract.
bool hl_recid_matches(const EC_GROUP* group, const BIGNUM* order, const BIGNUM* r, const BIGNUM* s,
const unsigned char* digest, int digest_len, int recid, const EC_POINT* expected, BN_CTX* ctx) {
bool matched = false;
EC_POINT* big_r = EC_POINT_new(group);
EC_POINT* q = EC_POINT_new(group);
BIGNUM* e = BN_bin2bn(digest, digest_len, nullptr);
BIGNUM* r_inv = BN_new();
BIGNUM* u1 = BN_new();
BIGNUM* u2 = BN_new();
BIGNUM* zero = BN_new();
if (big_r && q && e && r_inv && u1 && u2 && zero) {
BN_zero(zero);
if (EC_POINT_set_compressed_coordinates(group, big_r, r, recid & 1, ctx) == 1 &&
BN_mod(e, e, order, ctx) == 1 && BN_mod_inverse(r_inv, r, order, ctx) != nullptr &&
BN_mod_mul(u1, e, r_inv, order, ctx) == 1 && BN_mod_sub(u1, zero, u1, order, ctx) == 1 &&
BN_mod_mul(u2, s, r_inv, order, ctx) == 1 && EC_POINT_mul(group, q, u1, big_r, u2, ctx) == 1) {
matched = EC_POINT_cmp(group, q, expected, ctx) == 0;
}
}
BN_free(zero);
BN_free(u2);
BN_free(u1);
BN_free(r_inv);
BN_free(e);
EC_POINT_free(q);
EC_POINT_free(big_r);
return matched;
}
EcdsaSig ecdsa_sign(const QByteArray& digest, const QByteArray& priv_key_32) {
EcdsaSig out;
EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!key)
return out;
BIGNUM* bn =
BN_bin2bn(reinterpret_cast<const unsigned char*>(priv_key_32.constData()), priv_key_32.size(), nullptr);
if (!bn) {
EC_KEY_free(key);
return out;
}
EC_KEY_set_private_key(key, bn);
// Derive public key for v recovery.
EC_POINT* pub = EC_POINT_new(EC_KEY_get0_group(key));
EC_POINT_mul(EC_KEY_get0_group(key), pub, bn, nullptr, nullptr, nullptr);
EC_KEY_set_public_key(key, pub);
ECDSA_SIG* sig = ECDSA_do_sign(reinterpret_cast<const unsigned char*>(digest.constData()), digest.size(), key);
if (!sig) {
EC_POINT_free(pub);
BN_free(bn);
EC_KEY_free(key);
return out;
}
const BIGNUM* r_bn = nullptr;
const BIGNUM* s_bn = nullptr;
ECDSA_SIG_get0(sig, &r_bn, &s_bn);
// Low-s normalisation (EIP-2): reject high-s signatures.
BIGNUM* order = BN_new();
EC_GROUP_get_order(EC_KEY_get0_group(key), order, nullptr);
BIGNUM* half = BN_dup(order);
BN_rshift1(half, half);
BIGNUM* s_normal = BN_dup(s_bn);
if (BN_cmp(s_bn, half) > 0) {
BN_sub(s_normal, order, s_bn);
}
// Pad r/s to 32 bytes left-zero.
out.r = QByteArray(32, 0);
out.s = QByteArray(32, 0);
int rlen = BN_num_bytes(r_bn);
int slen = BN_num_bytes(s_normal);
BN_bn2bin(r_bn, reinterpret_cast<unsigned char*>(out.r.data()) + (32 - rlen));
BN_bn2bin(s_normal, reinterpret_cast<unsigned char*>(out.s.data()) + (32 - slen));
// v (recovery id): 27 or 28 depending on the parity of the R point. It used to be
// hardcoded to 27, which is right only ~half the time (measured 92/200 random
// signatures recovering to the true signer), so HL's ecrecover resolved a
// DIFFERENT address whenever the real recovery id was 28. Derive it instead:
// recover the public key for recid 0 and 1 from the FINAL (r, low-s) pair —
// low-s normalisation flips the parity, so it must be tried after it — and keep
// the one that matches the signer's key. If neither matches, fail closed (empty
// r/s => sign_action() reports "ecdsa_sign failed") rather than emit a signature
// that recovers to the wrong address.
//
// NOTE: this makes the ECDSA primitive correct; it does NOT make the live path
// ready. The action hashing is still canonical-JSON, not HL's msgpack scheme
// (see the header comment), so is_wired() deliberately stays false and
// HyperliquidVenue::place_order stays gated.
int recid = -1;
if (BN_CTX* ctx = BN_CTX_new()) {
for (int cand = 0; cand < 2 && recid < 0; ++cand) {
if (hl_recid_matches(EC_KEY_get0_group(key), order, r_bn, s_normal,
reinterpret_cast<const unsigned char*>(digest.constData()), digest.size(), cand, pub,
ctx))
recid = cand;
}
BN_CTX_free(ctx);
}
if (recid < 0) {
out.r.clear();
out.s.clear();
out.v = 0;
} else {
out.v = static_cast<uint8_t>(27 + recid);
}
// Compute signer address = last 20 bytes of keccak256(uncompressed pubkey[1:]).
unsigned char pubbuf[65];
size_t publen =
EC_POINT_point2oct(EC_KEY_get0_group(key), pub, POINT_CONVERSION_UNCOMPRESSED, pubbuf, sizeof(pubbuf), nullptr);
if (publen == 65) {
QByteArray pub_no_prefix(reinterpret_cast<const char*>(pubbuf + 1), 64);
QByteArray addr_hash = keccak256(pub_no_prefix);
QByteArray addr = addr_hash.right(20);
out.signer_address = bytes_to_hex_prefixed(addr);
}
BN_free(s_normal);
BN_free(half);
BN_free(order);
ECDSA_SIG_free(sig);
EC_POINT_free(pub);
BN_free(bn);
EC_KEY_free(key);
return out;
}
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif
} // namespace
QByteArray HyperliquidSigner::action_struct_hash(const QJsonObject& action) {
return keccak256(canonical_serialize(action).toUtf8());
}
bool HyperliquidSigner::is_wired() {
// The keccak/ECDSA primitives are linked, but the live ORDER path is NOT
// production-ready: HyperliquidVenue::place_order still hardcodes the asset id
// and rejects every order with "hl_live_path_not_yet_wired", and the signer's
// recovery id / EIP-712 domain are not yet known-vector-verified. Report
// not-wired so the Alpha Arena live gate refuses to capture (and persist) a
// real wallet private key for a path that cannot actually execute a trade.
return false;
}
Result<SignedAction> HyperliquidSigner::sign_action(const QJsonObject& action, const QString& agent_priv_key_hex,
bool is_testnet) {
const QByteArray priv = hex_to_bytes(agent_priv_key_hex);
if (priv.size() != 32) {
return Result<SignedAction>::err("private key must be 32 bytes");
}
const uint64_t chain_id = is_testnet ? 421614ULL : 42161ULL;
const QByteArray domain_sep = hl_domain_separator(chain_id);
const QByteArray struct_hash = action_struct_hash(action);
const QByteArray digest = eip712_digest(domain_sep, struct_hash);
EcdsaSig sig = ecdsa_sign(digest, priv);
if (sig.r.isEmpty() || sig.s.isEmpty()) {
return Result<SignedAction>::err("ecdsa_sign failed");
}
QByteArray sig_bytes;
sig_bytes.append(sig.r);
sig_bytes.append(sig.s);
sig_bytes.append(static_cast<char>(sig.v));
SignedAction out;
out.action = action;
out.nonce_ms = QDateTime::currentMSecsSinceEpoch();
out.signature_hex = bytes_to_hex_prefixed(sig_bytes);
out.signer_address = sig.signer_address;
return Result<SignedAction>::ok(out);
}
} // namespace fincept::trading::hyperliquid