1- import hmac
21import json
32import logging
4- import hashlib
53from json import JSONDecodeError
6-
74import requests
85from .__version__ import __version__
96from binance .error import ClientError , ServerError
107from binance .lib .utils import get_timestamp
118from binance .lib .utils import cleanNoneValue
129from binance .lib .utils import encoded_string
1310from binance .lib .utils import check_required_parameter
11+ from binance .lib .authentication import hmac_hashing , rsa_signature
1412
1513
1614class API (object ):
@@ -33,13 +31,17 @@ def __init__(
3331 proxies = None ,
3432 show_limit_usage = False ,
3533 show_header = False ,
34+ private_key = None ,
35+ private_key_passphrase = None ,
3636 ):
3737 self .key = key
3838 self .secret = secret
3939 self .timeout = timeout
4040 self .show_limit_usage = False
4141 self .show_header = False
4242 self .proxies = None
43+ self .private_key = private_key
44+ self .private_key_pass = private_key_passphrase
4345 self .session = requests .Session ()
4446 self .session .headers .update (
4547 {
@@ -77,8 +79,7 @@ def sign_request(self, http_method, url_path, payload=None, special=False):
7779 payload = {}
7880 payload ["timestamp" ] = get_timestamp ()
7981 query_string = self ._prepare_params (payload , special )
80- signature = self ._get_sign (query_string )
81- payload ["signature" ] = signature
82+ payload ["signature" ] = self ._get_sign (query_string )
8283 return self .send_request (http_method , url_path , payload , special )
8384
8485 def limited_encoded_sign_request (self , http_method , url_path , payload = None ):
@@ -94,8 +95,9 @@ def limited_encoded_sign_request(self, http_method, url_path, payload=None):
9495 payload = {}
9596 payload ["timestamp" ] = get_timestamp ()
9697 query_string = self ._prepare_params (payload )
97- signature = self ._get_sign (query_string )
98- url_path = url_path + "?" + query_string + "&signature=" + signature
98+ url_path = (
99+ url_path + "?" + query_string + "&signature=" + self ._get_sign (query_string )
100+ )
99101 return self .send_request (http_method , url_path )
100102
101103 def send_request (self , http_method , url_path , payload = None , special = False ):
@@ -145,9 +147,10 @@ def send_request(self, http_method, url_path, payload=None, special=False):
145147 def _prepare_params (self , params , special = False ):
146148 return encoded_string (cleanNoneValue (params ), special )
147149
148- def _get_sign (self , data ):
149- m = hmac .new (self .secret .encode ("utf-8" ), data .encode ("utf-8" ), hashlib .sha256 )
150- return m .hexdigest ()
150+ def _get_sign (self , payload ):
151+ if self .private_key :
152+ return rsa_signature (self .private_key , payload , self .private_key_pass )
153+ return hmac_hashing (self .secret , payload )
151154
152155 def _dispatch_request (self , http_method ):
153156 return {
0 commit comments