fix(binance_client): 优化签名计算逻辑以符合币安要求
在 `binance_client.py` 中更新了签名计算逻辑,确保参与签名的参数格式与币安REST API一致。新增 `_param_val_for_signature` 函数处理布尔值和空值,提升了签名的准确性和安全性。此改动增强了系统的稳定性和合规性。
This commit is contained in:
parent
e5bc2547aa
commit
c750478af9
|
|
@ -2037,12 +2037,22 @@ class BinanceClient:
|
||||||
ws_params["apiKey"] = self.api_key
|
ws_params["apiKey"] = self.api_key
|
||||||
if "timestamp" not in ws_params:
|
if "timestamp" not in ws_params:
|
||||||
ws_params["timestamp"] = int(time.time() * 1000)
|
ws_params["timestamp"] = int(time.time() * 1000)
|
||||||
# 计算签名(和 REST 一样的方式)
|
# 计算签名:币安要求参与签名的值格式与 REST 一致(布尔小写、空值不参与或为空串)
|
||||||
if "signature" not in ws_params:
|
if "signature" not in ws_params:
|
||||||
import hmac
|
import hmac
|
||||||
import hashlib
|
import hashlib
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
query_string = urlencode(sorted([(k, str(v)) for k, v in ws_params.items() if k != "signature"]))
|
def _param_val_for_signature(v):
|
||||||
|
if v is True:
|
||||||
|
return "true"
|
||||||
|
if v is False:
|
||||||
|
return "false"
|
||||||
|
if v is None:
|
||||||
|
return ""
|
||||||
|
s = str(v).strip()
|
||||||
|
return s if s else ""
|
||||||
|
sign_params = [(k, _param_val_for_signature(v)) for k, v in ws_params.items() if k != "signature"]
|
||||||
|
query_string = urlencode(sorted(sign_params))
|
||||||
signature = hmac.new(
|
signature = hmac.new(
|
||||||
self.api_secret.encode('utf-8'),
|
self.api_secret.encode('utf-8'),
|
||||||
query_string.encode('utf-8'),
|
query_string.encode('utf-8'),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user