Skip to content

Commit

Permalink
fix sign error with space in query string
Browse files Browse the repository at this point in the history
  • Loading branch information
makarworld committed Jun 20, 2023
1 parent 672bad9 commit 65bc6bb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pymexc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Union, Literal
import hmac, hashlib
import requests
from urllib.parse import urlencode
import logging
import time

Expand Down Expand Up @@ -50,7 +51,7 @@ def __init__(self, api_key: str = None, api_secret: str = None, proxies: dict =
"X-MEXC-APIKEY": self.api_key
})

def sign(self, **kwargs) -> str:
def sign(self, query_string: str) -> str:
"""
Generates a signature for an API request using HMAC SHA256 encryption.
Expand All @@ -61,7 +62,6 @@ def sign(self, **kwargs) -> str:
A hexadecimal string representing the signature of the request.
"""
# Generate signature
query_string = "&".join([f"{k}={v}" for k, v in kwargs.items()])
signature = hmac.new(self.api_secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
return signature

Expand All @@ -83,9 +83,11 @@ def call(self, method: Union[Literal["GET"], Literal["POST"], Literal["PUT"], Li
kwargs['params']['recvWindow'] = self.recvWindow
kwargs['params']['timestamp'] = timestamp
kwargs['params'] = {k: v for k, v in sorted(kwargs['params'].items())}
kwargs['params']['signature'] = self.sign(**kwargs['params'])

params = urlencode(kwargs.pop('params'), doseq=True).replace('+', '%20')
params += "&signature=" + self.sign(params)

response = self.session.request(method, f"{self.base_url}{router}", *args, **kwargs)
response = self.session.request(method, f"{self.base_url}{router}", params = params, *args, **kwargs)

if not response.ok:
raise MexcAPIError(f'(code={response.json()["code"]}): {response.json()["msg"]}')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""


version = '1.0.7'
version = '1.0.8'

with open("README.md", "r") as f:
long_description = f.read()
Expand Down

0 comments on commit 65bc6bb

Please sign in to comment.