Skip to content

Commit f07d0e8

Browse files
committed
Add nonce support
1 parent e574c17 commit f07d0e8

2 files changed

Lines changed: 42 additions & 30 deletions

File tree

biscoint_api_python/__init__.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
from urllib.parse import urljoin
66
import requests
7+
import time
78

89

910
class Biscoint:
@@ -128,7 +129,7 @@ def get_balance(self, **kwargs):
128129
"BTC": "0.01138164"
129130
}
130131
"""
131-
return self._call('balance', addAuth=True)
132+
return self._call('balance', add_auth=True, method='post')
132133

133134
def get_trades(self, op: str = None, length: int = None):
134135
"""Gets last `length` trades. Current API default is 10.
@@ -157,7 +158,7 @@ def get_trades(self, op: str = None, length: int = None):
157158
return self._call('trades', {
158159
'op': op,
159160
'length': length,
160-
}, addAuth=True)
161+
}, add_auth=True, method='post')
161162

162163
def get_offer(
163164
self,
@@ -201,7 +202,7 @@ def get_offer(
201202
'isQuote': isQuote,
202203
'base': base,
203204
'quote': quote,
204-
}, addAuth=True)
205+
}, add_auth=True, method='post')
205206

206207
def confirm_offer(self, offer_id: str):
207208
"""Confirms offer and execute operation.
@@ -226,36 +227,51 @@ def confirm_offer(self, offer_id: str):
226227
"apiKeyId": "BdFABxNakZyxPwnRu"
227228
}
228229
"""
229-
return self._call('offer', {
230+
return self._call('offer/confirm', {
230231
'offerId': offer_id,
231-
}, addAuth=True, method='post')
232+
}, add_auth=True, method='post')
232233

233234
# PRIVATE
234235

235-
def _call(self, endpoint: str, params: dict = {}, method: str = 'get', addAuth: bool = False):
236-
headers = None
236+
def _call(self, endpoint: str, params: dict = {}, method: str = 'get', add_auth: bool = False):
237+
headers = {
238+
'Content-Type': 'application/json',
239+
}
240+
data = None
241+
nonce = 0
237242

238243
v1_endpoint = 'v1/%s' % (endpoint)
239244
url = urljoin(self.api_url, v1_endpoint)
240-
params['request'] = v1_endpoint
245+
reqParams = self._remove_null_params(params)
246+
247+
# print(reqParams)
248+
249+
if method == 'get':
250+
data = None
251+
reqParams = self._normalize_params(reqParams)
241252

242-
params = self._remove_null_params(params)
253+
if method == 'post':
254+
data = json.dumps(
255+
reqParams,
256+
sort_keys=True,
257+
separators=(',', ':'),
258+
)
259+
reqParams = None
243260

244-
# print(params)
261+
if add_auth:
262+
nonce = int(time.time() * 1000000)
263+
signedParams = self._sign(endpoint, nonce, data)
264+
headers['BSCNT-NONCE'] = str(nonce)
265+
headers['BSCNT-APIKEY'] = self.api_key
266+
headers['BSCNT-SIGN'] = signedParams
245267

246-
if addAuth:
247-
signedParams = self._sign(params)
248-
headers = {
249-
'BSCNT-APIKEY': self.api_key,
250-
'BSCNT-SIGN': signedParams,
251-
}
252-
# print(headers)
268+
# print(headers)
253269

254270
res = requests.request(
255271
method=method,
256272
url=url,
257-
params=self._normalize_params(params) if method == 'get' else None,
258-
data=self._normalize_params(params) if method == 'post' else None,
273+
params=reqParams,
274+
data=data,
259275
headers=headers,
260276
)
261277

@@ -267,21 +283,17 @@ def _call(self, endpoint: str, params: dict = {}, method: str = 'get', addAuth:
267283

268284
return res_json['data']
269285

270-
def _sign(self, params: dict):
271-
jsonString = json.dumps(
272-
params,
273-
sort_keys=True,
274-
separators=(',', ':'),
275-
).encode('utf-8')
276-
hashBuffer = base64.b64encode(jsonString)
286+
def _sign(self, endpoint: str, nonce: int, data: str):
287+
strToBeSigned = ('v1/%s%d%s' % (endpoint, nonce, data)).encode('utf-8')
288+
hashBuffer = base64.b64encode(strToBeSigned)
277289

278-
# print(jsonString)
290+
# print(strToBeSigned)
279291
# print(hashBuffer)
280292

281293
sign_data = hmac.new(
282294
self.api_secret.encode(),
283295
hashBuffer,
284-
hashlib.sha256
296+
hashlib.sha384
285297
).hexdigest()
286298

287299
# print(sign_data)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="biscoint-api-python",
8-
version="0.5.1",
8+
version="0.6.0",
99
author="Thiago Borges Abdnur",
1010
author_email="bolaum@gmail.com",
1111
description="Biscoint API wrapper for python",
@@ -20,7 +20,7 @@
2020
"Programming Language :: Python :: 3",
2121
"License :: OSI Approved :: MIT License",
2222
"Operating System :: OS Independent",
23-
"Development Status :: 3 - Alpha",
23+
"Development Status :: 4 - Beta",
2424
"Intended Audience :: Developers",
2525
"Topic :: Software Development :: Libraries :: Python Modules",
2626
"Topic :: Office/Business :: Financial :: Investment",

0 commit comments

Comments
 (0)