Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make decrypt method read api_key and api_secret from secrets.json #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions bittrex/bittrex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import getpass
import ast
import json
import os

encrypted = True

Expand Down Expand Up @@ -89,19 +90,28 @@ def __init__(self, api_key, api_secret, calls_per_second=1, dispatch=using_reque
self.last_call = None
self.api_version = api_version

def decrypt(self):
def decrypt(self, export_fn='secrets.json'):
if encrypted:
cipher = AES.new(getpass.getpass(
'Input decryption password (string will not show)'))
try:
if isinstance(self.api_key, str):
self.api_key = ast.literal_eval(self.api_key)
if isinstance(self.api_secret, str):
self.api_secret = ast.literal_eval(self.api_secret)
except Exception:
pass
self.api_key = cipher.decrypt(self.api_key).decode()
self.api_secret = cipher.decrypt(self.api_secret).decode()
if (self.api_key == '' or self.api_secret == '') and os.path.isfile(export_fn) and os.access(export_fn, os.R_OK):
try:
with open(export_fn, 'r') as infile:
data = json.load(infile)
self.api_key = cipher.decrypt(ast.literal_eval(data['key'])).decode()
self.api_secret = cipher.decrypt(ast.literal_eval(data['secret'])).decode()
except json.decoder.JSONDecodeError:
exit('JSON data read from {0} fails to decode. Check JSON data format in {0}! Exiting.'.format(export_fn))
else:
try:
if isinstance(self.api_key, str):
self.api_key = ast.literal_eval(self.api_key)
if isinstance(self.api_secret, str):
self.api_secret = ast.literal_eval(self.api_secret)
except Exception:
pass
self.api_key = cipher.decrypt(self.api_key).decode()
self.api_secret = cipher.decrypt(self.api_secret).decode()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests asserting successful assignment of the decrypted data?

mock.patch can be used to simulate the keyboard input the feeds getpass

else:
raise ImportError('"pycrypto" module has to be installed')

Expand Down