-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencodeRequest.py
More file actions
34 lines (31 loc) · 1.21 KB
/
Copy pathencodeRequest.py
File metadata and controls
34 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import copy
import subprocess
import time
import sys
import json
import os
def createFullUrlAndHeaders(prefix, dataWithoutTime):
data = copy.deepcopy(dataWithoutTime)
data["timestamp"] = int(round(time.time() * 1000))
body = ""
for key, value in data.items():
body = body + key + "=" + str(value) + "&"
body = body[:-1]
tup = generateApiKeyAndSignature(body, "../keys.json")
headers = {"X-MBX-APIKEY": tup[0]}
fullString = prefix + "?" + body + "&signature=" + tup[1]
return (fullString, headers)
# returns (apiKey, signature)
# requestBody should look like "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559"
def generateApiKeyAndSignature(requestBody, filePath):
datastore = {}
with open(filePath, 'r') as f:
datastore = json.load(f)
signature = encode(requestBody, datastore["secret"])
apiKey = datastore["api-key"]
return (apiKey, signature)
def encode(requestBody, secret):
toEmit = "echo \"" + requestBody + "\\c\" | openssl dgst -sha256 -hmac \"" + secret + "\""
standardOut = str(subprocess.check_output(toEmit, shell=True))
randomSubstring = standardOut[2:-3]
return randomSubstring