
import hmac
import hashlib
import time


class ExchangeAuth:
    def __init__(self, api_key: str, api_secret: str):
        self.api_key = api_key
        self.api_secret = api_secret.encode()

    def sign(self, query: str) -> str:
        return hmac.new(self.api_secret, query.encode(), hashlib.sha256).hexdigest()

    def build_headers(self):
        return {
            "X-MBX-APIKEY": self.api_key,
        }

    def build_signed_params(self, params: dict):
        params["timestamp"] = int(time.time() * 1000)
        query = "&".join(f"{k}={v}" for k, v in params.items())
        signature = self.sign(query)
        params["signature"] = signature
        return params
