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

Fix KuCoin signatures affected by HTTP methods #236

Merged
merged 2 commits into from Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions pybotters/auth.py
Expand Up @@ -383,16 +383,14 @@ def kucoin(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
passphrase: str = session.__dict__["_apis"][Hosts.items[url.host].name][2]

now = int(time.time() * 1000)
if method == "GET":
str_to_sign = str(now) + method + url.path_qs
else:
body = JsonPayload(data) if data else FormData(data)()
headers["Content-Type"] = "application/json"
kwargs.update({"data": body._value})
str_to_sign = str(now) + method + url.path + body._value.decode()
body = JsonPayload(data) if data else FormData(data)()
if body._value:
kwargs.update({"data": body})

str_to_sign = f"{now}{method}{url.path_qs}".encode() + body._value

signature = base64.b64encode(
hmac.new(secret, str_to_sign.encode("utf-8"), hashlib.sha256).digest()
hmac.new(secret, str_to_sign, hashlib.sha256).digest()
).decode()
passphrase = base64.b64encode(
hmac.new(secret, passphrase.encode("utf-8"), hashlib.sha256).digest()
Expand Down
5 changes: 2 additions & 3 deletions tests/test_auth.py
Expand Up @@ -1229,10 +1229,9 @@ def test_kucoin_post(mock_session, mocker: pytest_mock.MockerFixture):
"price": "19200",
"size": 1,
}
)._value,
),
"headers": CIMultiDict(
{
"Content-Type": "application/json",
"KC-API-SIGN": "aoxLuRURO0t1z9hhh9ERbHjVp6bJ1K5bfoU2xHH25Y4=",
"KC-API-TIMESTAMP": "2085848896000",
"KC-API-KEY": "CYdTygFbGgM1re2J54lU2t83",
Expand All @@ -1244,5 +1243,5 @@ def test_kucoin_post(mock_session, mocker: pytest_mock.MockerFixture):
}
args = pybotters.auth.Auth.kucoin(args, kwargs)
assert args == expected_args
assert kwargs["data"] == expected_kwargs["data"]
assert kwargs["data"]._value == expected_kwargs["data"]._value
assert kwargs["headers"] == expected_kwargs["headers"]