Skip to content

Commit

Permalink
fix(#91): Properly encode the data to sign it
Browse files Browse the repository at this point in the history
  • Loading branch information
Atem18 committed Feb 22, 2023
1 parent b432e36 commit c2b2f33
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 120 deletions.
14 changes: 12 additions & 2 deletions kraky/api.py
Expand Up @@ -2,10 +2,11 @@
import base64
import hashlib
import hmac
import urllib
from urllib.parse import urlencode
import time

import httpx
from httpx._utils import primitive_value_to_str
from typing import Any

from .log import get_module_logger
Expand Down Expand Up @@ -40,7 +41,16 @@ def __init__(
self.logger = get_module_logger(__name__, logging_level)

def _sign_message(self, api_path: str, data: dict) -> str:
post_data = urllib.parse.urlencode(data)
# START
# Taken from Httpx httpx/_content.py#L140
plain_data = []
for key, value in data.items():
if isinstance(value, (list, tuple)):
plain_data.extend([(key, primitive_value_to_str(item)) for item in value])
else:
plain_data.append((key, primitive_value_to_str(value)))
# END
post_data = urlencode(plain_data)
encoded = (str(data["nonce"]) + post_data).encode()
message = api_path.encode() + hashlib.sha256(encoded).digest()
signature = hmac.new(base64.b64decode(self.secret), message, hashlib.sha512)
Expand Down
2 changes: 2 additions & 0 deletions kraky/cli.py
@@ -1,7 +1,9 @@
from dotenv import load_dotenv
import typer

from kraky import KrakyApiClient

load_dotenv()

app = typer.Typer()

Expand Down

0 comments on commit c2b2f33

Please sign in to comment.