Bot #2


Mon Jan 09 2023 20:53:57 GMT+0000 (UTC)

Saved by @alibaba #python

import asyncio import websockets # Helper function to sign a payload using the secret key def sign_payload(payload, secret_key): # Convert the payload to a JSON string payload_str = json.dumps(payload) # Encode the payload as base64 payload_b64 = base64.b64encode(payload_str.encode("utf-8")) # Create a SHA256 HMAC of the base64-encoded payload using the secret key hmac_key = base64.b64decode(secret_key) signature =, payload_b64, hashlib.sha256) signature_b64 = base64.b64encode(signature.digest()) return signature_b64, payload_b64 # Async function to send a signed request to the API async def send_signed_request(url, headers, payload, secret_key): # Sign the payload signature_b64, payload_b64 = sign_payload(payload, secret_key) # Add the HMAC signature and the encoded payload to the request headers headers["X-Signature"] = signature_b64.decode("utf-8") headers["X-Payload"] = payload_b64.decode("utf-8") # Send the POST request async with websockets.connect(url) as websocket: await websocket.send(json.dumps({"type": "request", "headers": headers, "payload": payload})) response = await websocket.recv() response_data = json.loads(response) if "error" in response_data: raise Exception(response_data["error"])