28 lines
1.3 KiB
Python
28 lines
1.3 KiB
Python
import requests
|
|
import json
|
|
import ecdsa
|
|
import binascii
|
|
|
|
def get_btc_ohlc_data(server_url, user_private_key):
|
|
# Load user's ECDSA private key
|
|
user_private_key = ecdsa.SigningKey.from_string(binascii.unhexlify(user_private_key), curve=ecdsa.SECP256k1)
|
|
# Get server public key from endpoint
|
|
server_public_key_hex = requests.get(server_url + "/serverkey").text
|
|
server_public_key = ecdsa.VerifyingKey.from_string(binascii.unhexlify(server_public_key_hex), curve=ecdsa.SECP256k1)
|
|
# Get timestamp
|
|
timestamp = str(int(time.time()))
|
|
# Create signature using user's private key
|
|
signature = binascii.hexlify(user_private_key.sign(bytes(timestamp, 'utf-8'))).decode("utf-8")
|
|
# Create authentication header
|
|
auth_header = {"auth": timestamp + ":" + signature}
|
|
# Make request to server with auth header
|
|
response = requests.get(server_url + "/t", headers=auth_header)
|
|
# Verify server's signature
|
|
server_signature = response.headers["signature"]
|
|
if server_public_key.verify(bytes(server_signature, 'utf-8'), bytes(timestamp, 'utf-8')):
|
|
# If signature is valid, return json data
|
|
return json.loads(response.text)
|
|
else:
|
|
# If signature is invalid, return error message
|
|
return {"error": "Invalid signature from server"}
|