-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.py
51 lines (38 loc) · 1.32 KB
/
balance.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import base64
import hashlib
import hmac
import json
import time
from urllib.request import urlopen, Request
ACCESS_TOKEN = ''
SECRET_KEY = ''
UPPERCASE_SECRET_KEY = SECRET_KEY.upper()
HOST = 'https://api.coinone.co.kr/'
def get_base_payload():
return {
'access_token': ACCESS_TOKEN,
}
def str_2_byte(s, encode='utf-8'):
return bytes(s, encode)
def get_encoded_payload(payload):
payload['nonce'] = int(time.time()*1000)
dumped_json = json.dumps(payload)
encoded_json = base64.b64encode(str_2_byte(dumped_json))
return encoded_json
def get_signature(encoded_payload):
signature = hmac.new(str_2_byte(UPPERCASE_SECRET_KEY), encoded_payload, hashlib.sha512)
return signature.hexdigest()
def get_response(url, payload):
encoded_payload = get_encoded_payload(payload)
signature = get_signature(encoded_payload)
headers = {
'Content-Type': 'application/json',
'X-COINONE-PAYLOAD': encoded_payload,
'X-COINONE-SIGNATURE': signature,
}
api_url = HOST + url
req = Request(api_url, data=encoded_payload, headers=headers, method='POST')
with urlopen(req) as res:
data = res.read().decode('utf-8')
return json.loads(data)
data = get_response('v2/account/balance/', get_base_payload())