zbxcat/utils.py

54 lines
1.3 KiB
Python
Raw Normal View History

2017-05-22 18:00:34 -07:00
import hashlib
2017-05-23 11:22:21 -07:00
import json
import random
import binascii
def hex2str(hexstring):
return binascii.unhexlify(hexstring).decode('utf-8')
2017-05-22 18:00:34 -07:00
def sha256(secret):
preimage = secret.encode('utf8')
h = hashlib.sha256(preimage).digest()
return h
def generate_password():
s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
passlen = 8
p = "".join(random.sample(s,passlen))
return p
# TODO: Port these over to leveldb or some other database
def save_trade(trade):
with open('xcat.json', 'w') as outfile:
json.dump(trade, outfile)
def get_trade():
with open('xcat.json') as data_file:
2017-05-23 13:34:48 -07:00
try:
xcatdb = json.load(data_file)
return xcatdb
except:
return None
2017-05-23 14:12:01 -07:00
def erase_trade():
with open('xcat.json', 'w') as outfile:
outfile.write('')
# caching the secret locally for now...
def get_secret():
with open('secret.json') as data_file:
for line in data_file:
return line.strip('\n')
def save_secret(secret):
with open('secret.json', 'w') as outfile:
outfile.write(secret)
def save(trade):
print("Saving trade")
trade = {
'sell': trade.sellContract.__dict__,
'buy': trade.buyContract.__dict__
}
save_trade(trade)